Skip to content

rust: port util/profiler.c to obs-util - #248

Merged
zackees merged 5 commits into
obs-rust:masterfrom
nathan-v:port/profiler
Oct 11, 2026
Merged

zackees merged 5 commits into
obs-rust:masterfrom
nathan-v:port/profiler

Conversation

@nathan-v

Copy link
Copy Markdown
Contributor

Summary

Ports the util/profiler.c recording engine to obs-util. Print and the zlib CSV dump stay in util/profiler-io.c (always compiled). profile_store_name stays in util/profiler-variadic.c and pushes through the hidden profiler_name_store_push. source-profiler.c stays out of scope.

ENABLE_RUST_LIBOBS drops util/profiler.c and compiles util/profiler-variadic.c instead. Exports are in libobs/cmake/rust-exports.d/obs-util-profiler.txt.

Fixes #152

Commits

  • cee923a09 moves print and CSV/gz into profiler-io.c so the oracle can compile the engine without zlib. Behavior of the C build is unchanged. The existing test_profiler.c still covers CSV.
  • RED 371351bb2 adds Tier 1 test_profiler_cmocka against empty stubs. NameStore::store returns null, so the test fails at assertion failed: !a.is_null() in rust/obs-util/tests/profiler.rs before the lifecycle checks.
  • GREEN 126344b4d replaces the stubs. Tier 1 test_profiler_cmocka passes. Tier 3 matches the C oracle on the lifecycle script and on 48 random call scripts, including time buckets, under one scripted os_gettime_ns. Recording each merged call twice makes lifecycle_matches_oracle fail (overall_count 5 vs 10, times (3000, 5) vs (3000, 10)). That change was reverted.

Intentional differences

  • The root mutex is process-lifetime. C profiler_free destroys it, so the cmocka file runs lifecycle last and never reuses the profiler. Rust profiler_start after profiler_free is defined.
  • Name-store create does not fail the way pthread_mutex_init can.
  • max_probe_count is not stored. C only used it in a commented blog.
  • time_delta is compared only under the scripted clock. The unchanged cmocka test still does not assert time values.

Not run here

This machine has no Docker, so Tier 2 (ENABLE_RUST_LIBOBS OFF and ON, identical exports, run.sh --repeat 100 --tests test_profiler) was not run. The local-gate stamp was not applied. python ci/local_gate.py --lane lint --fix formatted profiler.c; the lane's own Python checks then failed because Windows has no bash or cmake. That is not a profiler result. Please do not treat CI as green until the GitHub checks say so.

Please add the claimed label on #152. This account cannot.

@nathan-v
nathan-v requested a review from zackees as a code owner October 11, 2026 17:14
The recording engine stays in profiler.c. Print and the zlib CSV dump move
verbatim into profiler-io.c, which both builds compile, so a later Rust port
can drop profiler.c without dropping those exports.
NameStore::store returns null and profile_snapshot_create returns no roots.
test_profiler_cmocka fails at the name-store assertion `!a.is_null()`
(rust/obs-util/tests/profiler.rs), before the lifecycle counts run.
The recording engine, open-addressing hashmap, and snapshot builder move
to obs-util. Print and the zlib CSV dump stay in profiler-io.c.
profile_store_name stays in profiler-variadic.c and pushes through the
hidden profiler_name_store_push.

Tier 1 test_profiler_cmocka passes. Tier 3 matches the C oracle on the
lifecycle script and on 48 random call scripts, including time buckets,
under one scripted os_gettime_ns. Recording each call twice makes
lifecycle_matches_oracle fail (overall_count 5 vs 10).

Local-Gate: v1 tree=0dc0c3182f363bc6ce90ca32acec173d5dec7a9c secs=93 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791739522,"gate":"rust/all/fmt","host":"linux-x86_64","key":"d0cae094ec9dd4baa5eb48dc48010134acb659e7a7627df9c62ba68c9548ba01","lane":"lint","parents":["6d3100ea7909c8f6647079090e9441c138464ca0"],"secs":16,"stamp":"f7951355116ea57bee7aa3d44f7471fd","tree":"0dc0c3182f363bc6ce90ca32acec173d5dec7a9c","v":1,"via":"run"}
Ci-Attestation: {"at":1791739522,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"d0cae094ec9dd4baa5eb48dc48010134acb659e7a7627df9c62ba68c9548ba01","lane":"lint","parents":["6d3100ea7909c8f6647079090e9441c138464ca0"],"secs":16,"stamp":"a1866794b0a0465bc9a5f46d716847a6","tree":"0dc0c3182f363bc6ce90ca32acec173d5dec7a9c","v":1,"via":"run"}
Ci-Attestation: {"at":1791739522,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"3bdf6e80a5a5d91e938b4fe76091405b843fdffc4b3e46fffbd2da9cfcc6a4e8","lane":"rust-test","parents":["6d3100ea7909c8f6647079090e9441c138464ca0"],"secs":28,"stamp":"0ab80e57c1193667f6fc23cc85997e90","tree":"0dc0c3182f363bc6ce90ca32acec173d5dec7a9c","v":1,"via":"run"}
Ci-Attestation: {"at":1791739522,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"093d2031b7d4670ade8f516ae5e8639fcd1a7db1d1b99ffff440779554c07b3b","lane":"tier2-linux","parents":["6d3100ea7909c8f6647079090e9441c138464ca0"],"secs":48,"stamp":"79c4e32e16d74b03e1147d5b008607a6","tree":"0dc0c3182f363bc6ce90ca32acec173d5dec7a9c","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Rebased onto master, fixed two Linux build breaks, verified and stamped. The new head is aafbc7b1e.

Fixes (each folded into the commit that introduced the break):

  1. util: split …: libobs/util/profiler-io.c:85 used profiler_time_entry, a typedef that is local to profiler.c. GCC rejects that (unknown type name 'profiler_time_entry'), so the refactor commit didn't build on Linux. It now uses the public profiler_time_entry_t.
  2. rust: port …: rust/obs-c-oracle/oracle/profiler.c includes util/threading.h. With -std=c11, glibc hides pthread_mutexattr_settype and PTHREAD_MUTEX_RECURSIVE, so the oracle failed to build. It now defines _GNU_SOURCE on non-Windows, like task.c and config_file.c.

MSVC accepts both, which is probably why they didn't show up on your side.

Evidence:

  • Refactor commit: Tier 2 passed 83 of 83 with ENABLE_RUST_LIBOBS OFF and 83 of 83 with it ON (still the C profiler).
  • RED (6d3100ea7): test_profiler_cmocka failed against the stubs.
  • GREEN: profiler 3 of 3, profiler_parity 1 of 1, profiler_snapshot 6 of 6, profiler_snapshot_parity 2 of 2 and export_list 3 of 3 passed. The local gate passed: lint, rust-test, and tier2-linux with 83 of 83 OFF and 83 of 83 ON.

Merging once CI is green.

zackees and others added 2 commits October 11, 2026 10:38
ProfileName::from_static takes a &'static str and stores its pointer,
which the profiler reads back as a C string. A &str has no NUL, so the
read runs past the name: for a 3-byte slice of "abcdef" it returns
hundreds of bytes of unrelated rodata. test_profiler_cmocka hit the
same thing on macOS, where the byte after "test_other" is not 0
(left: 2, right: 1 at profiler.rs:190).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ProfileName::from_static now takes a &'static CStr, so every name
carries the NUL the profiler reads back to; a &str no longer compiles.
The test names are c"..." literals. static_name_reads_back_exactly and
test_profiler_cmocka pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=89e9438fc43d58735e6463bf858e37c1d2208b48 secs=88 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791740449,"gate":"rust/all/fmt","host":"linux-x86_64","key":"513fa453b2efacc1a74d0f71c9418f827e8620ba98340bc16bbadd4ad0efa4ce","lane":"lint","parents":["fa456208c4b4a3a34135c9647d1a09cd3c4452ac"],"secs":13,"stamp":"abe7eb30c7b4a67f0875041e0eed97eb","tree":"89e9438fc43d58735e6463bf858e37c1d2208b48","v":1,"via":"run"}
Ci-Attestation: {"at":1791740449,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"513fa453b2efacc1a74d0f71c9418f827e8620ba98340bc16bbadd4ad0efa4ce","lane":"lint","parents":["fa456208c4b4a3a34135c9647d1a09cd3c4452ac"],"secs":13,"stamp":"2bc4bec3b8c50563c13427172c8856e5","tree":"89e9438fc43d58735e6463bf858e37c1d2208b48","v":1,"via":"run"}
Ci-Attestation: {"at":1791740449,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"12de692a3e155a147c43bb113996f7984f8f7786aca2ecfc76d2cf97e5ff1da4","lane":"rust-test","parents":["fa456208c4b4a3a34135c9647d1a09cd3c4452ac"],"secs":26,"stamp":"67bda1eff185ffa944211dbbba189152","tree":"89e9438fc43d58735e6463bf858e37c1d2208b48","v":1,"via":"run"}
Ci-Attestation: {"at":1791740449,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4dfc9331f77946d33924e016365a40c8ef81f5fab43eb67e3d38c66c0a91adf4","lane":"tier2-linux","parents":["fa456208c4b4a3a34135c9647d1a09cd3c4452ac"],"secs":48,"stamp":"16be5ae771ffcd50623805d6e78d4b53","tree":"89e9438fc43d58735e6463bf858e37c1d2208b48","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

macOS Rust Tests failed: test_profiler_cmocka panicked at profiler.rs:190 (left: 2, right: 1). The filter didn't remove test_other.

Cause: ProfileName::from_static(&'static str) stores the pointer of a Rust &str, and the profiler reads it back as a C string (CStr::from_ptr). A &str has no NUL, so the read runs past the name. On Linux the next byte happened to be 0. On macOS it isn't. This is undefined behaviour in the safe API, not just in the test.

Two commits on top:

  • RED: static_name_reads_back_exactly stores &"abcdef"[..3] and reads it back. It returned about 600 bytes of unrelated rodata instead of abc, on Linux too.
  • GREEN: from_static takes &'static CStr, so a name without a NUL no longer compiles. The test names are c"…" literals. profiler 2 of 2, profiler_parity 3 of 3 and export_list 3 of 3 passed. Clippy and rustfmt are clean, and the local gate passed.

Merging once CI, including macOS, is green.

@zackees
zackees merged commit 1ee7a81 into obs-rust:master Oct 11, 2026
23 checks passed
zackees added a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 12, 2026
Since obs-rust#248, obs-util exports the Rust profile_start, profile_end and
profile_reenable_thread, and the audio-io oracle's no-op hooks of the
same names collided at link time (rust-lld: duplicate symbol:
profile_start, in every obs-media-io test binary). Rename the oracle's
hooks before audio-io.c is included.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zackees added a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 12, 2026
oracle/profiler.c (obs-rust#248) defines a scripted os_gettime_ns for the
profiler parity tests, and the audio-io oracle defined the real one,
so profiler_parity failed to link (duplicate symbol: os_gettime_ns).
Audio-io keeps the real clock under private names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=4186644fea8923a5acfe9827b0d1724c6c4382ee secs=112 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791764047,"gate":"rust/all/fmt","host":"linux-x86_64","key":"15f0038c088e377d14cd58325a0e46f280243c729ff086205ac29805a56209e7","lane":"lint","parents":["6806dc73038a88c9265e5cc4deb58ef37cda1ddd"],"secs":16,"stamp":"d50d30a484537a6bc9ad91aef46d7b0b","tree":"4186644fea8923a5acfe9827b0d1724c6c4382ee","v":1,"via":"run"}
Ci-Attestation: {"at":1791764047,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"15f0038c088e377d14cd58325a0e46f280243c729ff086205ac29805a56209e7","lane":"lint","parents":["6806dc73038a88c9265e5cc4deb58ef37cda1ddd"],"secs":16,"stamp":"d1de8177ce400889c11035a11705397a","tree":"4186644fea8923a5acfe9827b0d1724c6c4382ee","v":1,"via":"run"}
Ci-Attestation: {"at":1791764047,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"36a0373dcf8c96780d5585411d8541906456a1362bfc70845691c22a19f1b3e5","lane":"rust-test","parents":["6806dc73038a88c9265e5cc4deb58ef37cda1ddd"],"secs":46,"stamp":"b5f961a801e226b4e43233150736f25c","tree":"4186644fea8923a5acfe9827b0d1724c6c4382ee","v":1,"via":"run"}
Ci-Attestation: {"at":1791764047,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"11b51f291675aaf52fe851484317638db6be7a1fb637e16059a64ec9f2676e79","lane":"tier2-linux","parents":["6806dc73038a88c9265e5cc4deb58ef37cda1ddd"],"secs":50,"stamp":"95692a6eeb235b95cf404f1d19dd69f6","tree":"4186644fea8923a5acfe9827b0d1724c6c4382ee","v":1,"via":"run"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

rust: Port util/profiler.c to obs-util

2 participants