Repository navigation
obs-core: Port the Linux system info parsing - #2721
Conversation
|
Coordinator tick — the only red on this head is a rustfmt nit, and it is a two-line change.
That is rustfmt's own output — one hunk, exactly what I did not push the fixup myself: One unrelated note while you are in there: the port is a |
8143efa to
c122e64
Compare
|
Thanks. The rustfmt fix is folded into the RED commit, and the branch is rebased onto current master (head c122e64), with the full six commits now. The lint, cross and Rust lanes passed locally; the head is unstamped, so PR CI runs the full suite. |
Behaviour-neutral extraction before the Rust port. The line rules and
sizes of obs-nix.c.h's startup log move to
libobs/obs-nix-system-info.c.h, as eleven functions on plain values
declared in the new libobs/obs-nix-system-info.h without EXPORT
(hidden, as -fvisibility=hidden keeps them):
- _is_name(), _is_physical_id() and _is_speed(): the strncmp() key
checks of log_processor_info() ("model name" 10, "physical id" 11,
"cpu MHz" 7);
- _value(): the byte after the first ':', or NULL when there is none or
the line ends there;
- _physical_id(): atoi() of that value;
- _prints(): a line that starts with '\n', with a physical id other
than the last one printed;
- _is_dmesg_cpu() and _dmesg_speed(): log_processor_speed()'s "CPU: "
check, and the byte after the last '(' with its strcspn() length up
to '-' (FreeBSD);
- _total_mb(), _free_mb() and _physmem_mb(): log_memory_info()'s MB
values in uint64_t, and OpenBSD's in int64_t.
Each key check is its own function, so the three checks of a cpuinfo
line, and each `continue`, stay where they were. obs-nix.c.h keeps
fopen(), getline(), fclose() and free(), every dstr copy, cut and
depad, sysinfo() and sysctl() with their casts, the flatpak, desktop
and data file pointer checks, and every blog(), each at its point.
Everything from the hotkey platform calls on is untouched.
Checked in a scratch harness (not committed). It compiles
log_processor_info(), log_processor_speed() and both branches of
log_memory_info() from origin/master's obs-nix.c.h verbatim, and this
branch's with obs-nix-system-info.c.h, over a stand-in that logs every
fopen(), getline(), fclose(), sysinfo(), sysctl(), dstr call (with its
result) and blog() line. Inputs: this host's /proc/cpuinfo, a FreeBSD
dmesg.boot excerpt, a missing file, and 83,000 generated files of up to
30 lines each from the cpuinfo alphabet (each key, the keys one byte
short and one byte long, ':', tabs, spaces, \r, \v, signs, digits up to
past INT_MAX, '(' and '-', UTF-8, 0xff and NUL bytes, blank lines, and
a last line with no newline) through both line loops, plus 83,000
sysinfo() results (totalram, freeram and bufferram over the edges and
the whole u64 range, mem_unit 0, 1, 2, 4096, 65536, UINT32_MAX and
random, a failing call) and 83,000 OpenBSD sizes over the whole i64
range. 5,841,694 logged lines each side over five seeds; the two logs
are identical with GCC and Clang at -O0 and -O2, and under ASan and
UBSan with no recovery (GCC -O0, Clang -O2).
30 deliberately broken moves were planted, and the harness fails all
30: each key length one short and one long, strrchr() for strchr() and
the reverse, the '\0' check dropped or moved after the increment, atoi()
read from the line or as strtol() base 0, the print rule without either
operand or also taking '\r', "CPU:" for "CPU: ", strcspn() over "-M" or
strlen(), each MB value divided by 1000, truncated to 32 bits, without
bufferram, multiplied after the division, unsigned or shifted, and six
in the callers (an argument swapped or dropped, the wrong key check,
dstr_copy() for dstr_ncopy()).
git blame on the moved lines shows upstream commits ("Add system
information for linux", "libobs: Log *nix system info more like
Windows/Mac", "Add OpenBSD support", "libobs: Implement logging
processor model on bsd"); they move as they are.
The new file has its own group, libobs.nix-system-info, in the Linux
sources (FreeBSD and OpenBSD build the same list), with a precompiled
header of only obs-nix-system-info.h and the C library, so the C test
can compile it alone; libobs-nix.pch.h includes the header for
obs-nix.c. The exported symbol set is unchanged: 1766 on the C backend,
with the eleven functions local.
Refs #2719.
test_nix_system_info_steps compiles libobs/obs-nix-system-info.c.h
(ASan and UBSan) and calls only its functions through
obs-nix-system-info.h, with libobs for the dstr calls. Saved cpuinfo
and dmesg.boot text runs through a copy of log_processor_info()'s and
log_processor_speed()'s loops, so the blocks the log would print are
pinned too. 11 cases:
- a two-socket x86 cpuinfo (two blocks, each with its first thread's
name and speed) and a one-socket one (one block);
- an arm64 cpuinfo with no physical id and no model name;
- a block that ends before any model name or cpu MHz line;
- a value on the last line of a file with no newline;
- the key prefixes ("model names" and "cpu MHz dynamic" match, "model
nam", case and a leading space do not, the keys never overlap);
- the values: the first ':', none without one or at the end of the
line, "model name\t:\n", depadding on both ends;
- the physical ids as atoi() reads them (spaces, tabs, signs, trailing
text, INT_MAX, INT_MIN, "x", "- 1", "0x10"), and a "physical id"
line with no value keeping the old id;
- the print rule over ids and line starts;
- the dmesg.boot speed: the last '(', the length up to '-', no '(',
a line that ends at it, no '-', and the last CPU line winning;
- the memory sizes with mem_unit 0, 1 and 4096, totalram near 2^32,
and OpenBSD's signed sizes truncated toward zero.
Characterized, not endorsed:
- arm64 logs no CPU name or speed: its physical id stays -1, the
starting "last id";
- a block that ends before a model name or cpu MHz line logs NULL for
them ("(null)" with glibc), and so does "model name\t:\n" (the cut
leaves nothing and dstr_resize() frees the name);
- the caller cuts every value's last byte as its newline, so the last
line of a file with no newline loses a real byte (never logged: no
blank line follows it);
- "physical id : x" reads as socket 0;
- a dmesg.boot speed with no '-' runs to the end of the line, ')' and
newline included;
- the memory products wrap in uint64_t (only above 16 EiB).
It is in the native test graph, in name order, with the header as a
watch input. No assert_in_range or fail_msg, so it builds with cmocka
1.x and 2.x, and no two enum types are compared. Passes 11/11 on the C
backend. No real-libobs test covers log_system_info() (ctest -N lists
only test_distribution_info, for read_distribution_info(), which stays
C); the log reads the host's own files.
Refs #2719.
RED for the port of libobs/obs-nix-system-info.c.h to a new nix_system_info module of rust/obs-core. The safe core is a stub (todo!()): is_name(), is_physical_id(), is_speed(), value(), physical_id(), prints(), is_dmesg_cpu(), dmesg_speed(), total_mb(), free_mb() and physmem_mb(). A line or value is the bytes before its NUL; value() returns the offset after the first ':' and dmesg_speed() the range of the speed, so the C keeps its own buffer and every dstr copy. physical_id() is atoi() as glibc defines it. tests/obs_core/nix_system_info.rs ports test_nix_system_info_steps 1:1: its 11 cases under the same names (steps_*), in the same order, with the same values and expectations, the characterized-not-endorsed cases included. Its copies of log_processor_info()'s and log_processor_speed()'s loops use obs-util's Dstr, the core of the dstr calls the C makes. Three new proptests (edge_*, 4096 cases each) check every line rule over lines from the cpuinfo and dmesg.boot alphabet, atoi() over any number text glibc reads (out-of-range values included), and the memory sizes over any u64 fields, mem_unit and i64, against models written from the C's expressions. soldr cargo test -p obs-data --test obs-tests-core -- nix_system_info:: fails 14 of 14 with "not yet implemented". Refs #2719.
Ports libobs/obs-nix-system-info.c.h, the /proc/cpuinfo and dmesg.boot
line rules and the memory sizes of obs-nix.c's startup log, to the
nix_system_info module of rust/obs-core. obs-nix.c.h stays C around
it: fopen(), getline(), fclose() and free(), every dstr copy, cut and
depad, sysinfo() and sysctl() with their casts, the pointer checks and
every blog(), each at its point. The hybrid libobs drops the C file
and links the Rust shims in its place.
Safe core (src/nix_system_info.rs), eleven functions on byte slices
(a line is its bytes before the NUL):
- is_name(), is_physical_id(), is_speed() and is_dmesg_cpu(): the line
starts with "model name", "physical id", "cpu MHz" or "CPU: ", as
strncmp() by the key's length;
- value(): the offset after the first ':', None without one or at the
end of the line;
- physical_id(): atoi() as glibc defines it, (int)strtol(): C-locale
white space (\v and \f included), a sign, decimal digits saturated at
the 64-bit long's bounds, then the low 32 bits;
- prints(): a first byte of '\n' and an id other than the last;
- dmesg_speed(): the range from after the last '(' up to the first '-'
or the end, None without one or at the end;
- total_mb(), free_mb() and physmem_mb(): wrapping u64 products and
sums, and i64 division truncated toward zero, each over 1024 twice.
The module denies clippy's arithmetic, indexing, as, unwrap, expect,
panic, todo, unimplemented and unreachable lints, keeps no state and
reads nothing past the slice it is given.
Shims (src/ffi/nix_system_info.rs): the eleven C signatures. Each line
or value is read as a C string up to its NUL, as the C's string calls
read it; value() and dmesg_speed() return a pointer into that same
string, and dmesg_speed() writes *len only when it returns one. No
struct is mirrored. No intentional difference from the C.
C side: the file leaves libobs.nix-system-info for
libobs.ported-nix-system-info, registered in its own fragment,
libobs/build/ported-source-sets/nix-system-info.json
(LIBOBS_LINUX_PORTED_SOURCES, which FreeBSD and OpenBSD build too), so
it edits no shared ported list. Its PCH is still only the header and
the C library. The functions are not EXPORT, so the shims are hidden:
rust-hidden.txt, the local: list of rust-exports.map and
rust-unexports-macos.txt. libobs-rust already links the crate's ffi.
ci/test_ported_sources.py checks the group on Linux.
The exported symbol sets are unchanged and equal: 1766 on the C
backend, the hybrid and libobs-c-reference, the same set on all three;
the eleven functions are local in all three, and the hybrid's come
from the Rust archive (the group's object is built only for
libobs-c-reference).
test_nix_system_info_steps still compiles the C file into itself,
unchanged, and passes 11/11. It has no _rust variant, as for
test_canvas_rules_steps: the shims' archive member also holds the
other obs-core ports. The Tier 3 parity tests compare the shims with
the C instead.
soldr cargo test -p obs-data --test obs-tests-core --
nix_system_info:: passes 14 of 14.
No user-visible change, so no docs/features.md line.
Refs #2719.
rust/obs-c-oracle/oracle/nix_system_info.c compiles the unchanged
libobs/obs-nix-system-info.c.h with its eleven functions renamed
oracle_*. It is plain C (strncmp(), strchr(), strrchr(), strcspn(),
atoi() and integer arithmetic), so it builds on every host.
tests/obs_core/nix_system_info_parity.rs (11 tests) runs the shim, the
C and the safe core on every input and compares the results exactly:
the C's bool as the byte it returns, a returned pointer as its offset
into the line or NULL, and *len as written or left untouched.
- every line of up to three pieces of the cpuinfo and dmesg.boot
alphabet (each key and the keys one byte short, ':', tabs, \r, \v,
signs, digits up to INT_MAX, '(' and '-', UTF-8 and 0xff), with and
without a newline: 28,850 lines;
- every byte alone, and before and after each key and its colon;
- this host's /proc/cpuinfo, line by line, when it has one;
- atoi() from -70,000 to 70,000, around both ends of int and of long,
with white space, signs and trailing text, and malformed numbers;
- the memory sizes over the edges of every field (0, 1, 2^20 - 1,
2^20, 2^32 - 1, 2^32, 2^63, u64::MAX, mem_unit 0 to UINT32_MAX, and
OpenBSD's negative sizes): 19,208 combinations;
- both callers' loops, run once on the shims and once on the C, over
the C test's texts, the host's /proc/cpuinfo and generated files: the
same blocks, the same cut values and the same dmesg.boot speed;
- proptests (20,000 cases each) over lines, number texts, memory sizes
and generated files.
atoi() past the bounds of int is undefined in C: glibc and macOS give
(int)strtol(), which the core follows, while MSVC saturates at int. So
only Linux compares number texts out of int range; every host compares
the rest. No input reaches a division by a variable or a 128-bit
multiply on either side, so nothing else is skipped on MSVC x64. No
intentional differences.
The parity module passed 3 runs in a row with fresh seeds.
test_nix_system_info_steps passes 11/11 under ASan and UBSan. The
committed regression file holds the seeds proptest saved while mutants
failed; all pass on the port.
benches/port_comparison/nix_system_info.rs compares the shims with the
C over 1024 cpuinfo lines, dmesg.boot lines and memory sizes each
(checksums equal on both sides; no allocation left live). On a shared
machine at a load average of about 16: cpuinfo 29.1 us Rust and 24.8 us
C, dmesg 21.9 and 20.2, memory 8.8 and 8.6.
Mutation check (the mutants are not committed): 28 mutants, 20 in the
core and 8 in the shims, each run against Tier 1 (14 tests) and Tier 3
(11). 26 fail. The two that survive are equivalent: the prints shim
with its two ids swapped (!= is symmetric), and the free_mb shim with
freeram and bufferram swapped (the sum commutes). Tier 1 does not
reach the shims; Tier 3 kills every other shim mutant. Each mutant and
the tests that fail on it:
1. core, is_name: "model nam"
T1: edge_line_rules_any_line, steps_key_prefixes
T3: every_line_of_three_pieces, proptest_callers, proptest_lines
2. core, is_physical_id: "physical id "
T1: edge_line_rules_any_line, steps_block_before_name,
steps_key_prefixes, steps_physical_ids, steps_values,
steps_x86_one_socket, steps_x86_two_sockets
T3: callers_over_saved_text, every_byte_after_each_key,
every_line_of_three_pieces, host_cpuinfo_lines,
proptest_callers, proptest_lines
3. core, is_speed: "cpu MH"
T1: as 1; T3: as 1
4. core, is_dmesg_cpu: "CPU:"
T1: edge_line_rules_any_line, steps_dmesg_lines
T3: as 1
5. core, value: the last ':'
T1: edge_line_rules_any_line, steps_values
T3: every_byte_after_each_key, every_line_of_three_pieces,
proptest_callers, proptest_lines
6. core, value: a line that ends at the ':' has one
T1: edge_line_rules_any_line, steps_physical_ids, steps_values
T3: callers_over_saved_text, every_byte_after_each_key,
every_line_of_three_pieces, proptest_lines
7. core, physical_id: \v not white space
T1: edge_line_rules_any_line, edge_physical_id_any_number
T3: every_line_of_three_pieces, every_physical_id,
proptest_callers, proptest_lines, proptest_physical_ids
8. core, physical_id: no '+'
T1: as 7, steps_physical_ids
T3: every_line_of_three_pieces, proptest_callers, proptest_lines,
proptest_physical_ids
9. core, physical_id: the sign ignored
T1: as 8; T3: as 7
10. core, physical_id: saturated at int
T1: as 7; T3: as 7
11. core, physical_id: saturated at i64::MAX for negatives too
T1: edge_physical_id_any_number
T3: every_physical_id, proptest_physical_ids
12. core, prints: no id check
T1: edge_line_rules_any_line, steps_arm64_logs_nothing,
steps_print_rule, steps_x86_one_socket, steps_x86_two_sockets
T3: as 2
13. core, prints: a newline anywhere
T1: edge_line_rules_any_line, steps_block_before_name,
steps_last_line_without_newline, steps_print_rule,
steps_values, steps_x86_one_socket
T3: as 2
14. core, dmesg_speed: the first '('
T1: as 4; T3: as 5
15. core, dmesg_speed: a line that ends at the '(' has one
T1: as 4
T3: every_byte_after_each_key, every_line_of_three_pieces,
proptest_lines
16. core, dmesg_speed: '-' ignored
T1: as 4
T3: callers_over_saved_text, every_byte_after_each_key,
every_line_of_three_pieces, proptest_callers, proptest_lines
17. core, total_mb: saturating multiply
T1: edge_memory_any_size, steps_memory_sizes
T3: every_memory_edge, proptest_memory
18. core, free_mb: bufferram dropped
T1: as 17; T3: as 17
19. core, physmem_mb: unsigned division
T1: as 17; T3: as 17
20. core, MB: / 1000
T1: as 17; T3: as 17
21. shim, value: one byte further
T3: as 2
22. shim, dmesg_speed: range.end for *len
T3: callers_over_saved_text, every_byte_after_each_key,
every_line_of_three_pieces, proptest_callers, proptest_lines
23. shim, dmesg_speed: *len written on NULL
T3: every_byte_after_each_key, every_line_of_three_pieces,
host_cpuinfo_lines, null_results, proptest_lines
24. shim, is_speed: is_name
T3: callers_over_saved_text, every_byte_after_each_key,
every_line_of_three_pieces, host_cpuinfo_lines,
proptest_callers, proptest_lines
25. shim, is_dmesg_cpu: is_speed
T3: as 24
26. shim, prints: ids swapped: SURVIVES (equivalent)
27. shim, physmem_mb: mem + 1
T3: every_memory_edge
28. shim, free_mb: freeram and bufferram swapped: SURVIVES
(equivalent)
Refs #2719.
rust/README.md: the obs-core row names libobs/obs-nix-system-info.c.h, and a new row for it describes the port: the group, the eleven hidden functions, what obs-nix.c.h keeps, the characterized-not-endorsed cases, the C test with no _rust variant, and what Tier 3 compares (out-of-range atoi() on Linux only). docs/rust-port/naming.md: the rust/obs-core row lists obs-nix-system-info.c.h, extracted from libobs/obs-nix.c.h (no row named obs-nix.c.h before). rust/obs-core: the Cargo.toml description lists the file, and the lib.rs module docs name the nix_system_info module and what it parses. Fixes #2719.
c122e64 to
a3b8500
Compare
|
Checked head This is still a draft, so I am treating it as yours to finish: mark it ready when the port is complete and the parity note in the body is written, and it comes back to the coordinator list for review and the queue. |
|
Thanks. The port is complete (all six commits), and the parity note is in the body: no intentional differences; atoi() past the bounds of int is undefined in C, so Tier 3 compares out-of-range physical ids on Linux only, where glibc's (int)strtol() is what the core follows. Marked ready. |
Fixes #2719
What and why
Ports the Linux and BSD system info parsing of
libobs/obs-nix.c.h(the/proc/cpuinfoanddmesg.bootline rules and the memory sizes of the startup log) to a newnix_system_infomodule ofrust/obs-core, in the standard six commits:libobs/obs-nix-system-info.c.h, declared inlibobs/obs-nix-system-info.hwithoutEXPORT. Each key check is its own function, so the three checks of a cpuinfo line and eachcontinuestay where they were.obs-nix.c.hkeepsfopen(),getline(),fclose(),free(), everydstrcopy, cut and depad,sysinfo()andsysctl(), the flatpak, desktop and data file pointer checks, and everyblog(). A scratch harness compiled origin/master's three log functions verbatim and the moved ones over a logging stand-in, and ran 83,000 generated cpuinfo/dmesg.boot files plus 166,000 memory cases (5,841,694 logged lines each side) with GCC and Clang at -O0 and -O2 and under ASan and UBSan: identical logs everywhere, and all 30 planted mutants caught.test_nix_system_info_steps, 11 cases (two-socket and one-socket x86, arm64, the line rules, the dmesg.boot speed, the memory sizes).obs_core::nix_system_infoTier 1 (the 11 C cases 1:1, plus 3 proptests) againsttodo!()stubs.libobs.ported-nix-system-info, registered inlibobs/build/ported-source-sets/nix-system-info.json(Linux ported sources, which FreeBSD and OpenBSD build too). The shims are hidden like the C functions.port_comparisonbench module, and a mutation check (28 mutants: 26 killed, the 2 survivors equivalent).rust/README.md,docs/rust-port/naming.md, the crate description and module docs.The plain NULL checks (the flatpak defaults, which extension lists are logged, the desktop names, the data file search) stay C: they are pointer checks, as in the canvas rules port (#2544). No real-libobs test covers
log_system_info()(ctest -Nlists onlytest_distribution_info, forread_distribution_info(), which stays C); real libobs runs it at everyobs_startup(), so every libobs test that starts OBS logs these lines through the shims.atoi()past the bounds ofintis undefined in C; the core follows glibc ((int)strtol()), and Tier 3 compares out-of-range number texts (in a line, a value or a caller's file) on Linux only. No intentional differences. Characterized, not endorsed, and kept: arm64 logs no CPU name or speed, a block that ends before amodel nameorcpu MHzline logs them as NULL, the last line of a file with no newline loses a byte to the newline cut,physical id : xreads as 0, admesg.bootspeed with no-runs to the end of the line, and the memory products wrap above 16 EiB.The moved code is upstream's ("Add system information for linux", "libobs: Log *nix system info more like Windows/Mac", "Add OpenBSD support", "libobs: Implement logging processor model on bsd"); it moves as it is. No upstream issue.
RED → GREEN
obs-core: Test the nix_system_info port.soldr cargo test -p obs-data --test obs-tests-core -- nix_system_info::fails 14 of 14 with "not yet implemented".nix_system_infopasses 25 of 25 (3 runs, fresh seeds).test_nix_system_info_stepspasses 11/11 on the C backend and in the hybrid tree. Exported symbol sets: 1766 on the C backend, the hybrid andlibobs-c-reference, the same set on all three, with the eleven functions local and the hybrid's from the Rust archive. Hybrid ctest: 598 tests, 9 skipped (PipeWire portal, v4l2 vivid, output tests that skip without their devices); 4 failed in the parallel run and passed alone (two load flakes, two that need an X display, passing under a private Xvfb), identically on the C backend. Local gates:--test-diff origin/master4719 passed, 6 skipped;--lane lintok;--lane crossok;cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targetsclean;port_comparisonbench test passes;./unittest's Rust lane ok. The Docker Tier 2 harness (rust/tools/linux-validate/run.py) did not run locally (no Docker access on the build host), so PR CI's Tier 2 jobs are its run. Runs were on a3b8500 (rebased onto current master, after the video_pacing port): the export sets are identical on all three builds again (1766), the C test passes 11/11, the nix_system_info and video_pacing tests 43/43, the bench test,--lane lint,--lane crossand the MSVC clippy pass. HEAD is not stamped: the local gate's Tier 2 lane needs Docker, which the build host doesn't give this user, so PR CI runs the full suite.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC (not applicable: no warning or flag change)atoi()is noted in both