Skip to content

fix(obs-util): CPU usage is at most all of the machine (#3367) - #3435

Merged
vpacher merged 2 commits into
masterfrom
fix/3367-ws-cpu-usage
Nov 1, 2026
Merged

vpacher merged 2 commits into
masterfrom
fix/3367-ws-cpu-usage

Conversation

@vpacher

@vpacher vpacher commented Nov 1, 2026

Copy link
Copy Markdown
Member

Fixes #3367. Refs #3238, #1346.

Why

test_ws_cpu_usage (8 threads polling GetStats on one session, under ASan) now and then got a cpuUsage above 100. The formula is not wrong: os_cpu_usage_info_query() reports the process's share of the machine, process time over wall time over the core count, in percent, the same in C and in the Rust port. But on Linux both times come from times(), in 10 ms ticks. Since #1346 each session keeps its own sample, so 8 threads on one session query it every tick or two, and over one tick of wall time the busy threads can be charged one tick more than the cores could run: 5 ticks on 4 cores reads as 125 %. Upstream OBS does the same.

What changes

  • os_cpu_usage_info_query() caps its value at 100 on every platform: obs_util::cpu_usage::cap() in the Rust ports (nix, cocoa, Windows) and percent > 100.0 ? 100.0 : percent in the C of platform-nix, platform-cocoa and platform-windows, so the ports and C stay equal. A NaN from an empty interval passes through as before.
  • The flaky entry of test_ws_cpu_usage leaves the sanitizer known list.

This is an intentional difference from upstream OBS: a share of the machine cannot pass 100 %, and a reading above it is only the counters' resolution. Below 100 nothing changes.

Commits

  • RED aa55c04: obs_util::cpu_usage::percent() with the formula, and Tier 1 tests that a coarse sample (5 busy ticks in 1 wall tick on 4 cores) reads at most 100. Fails a_coarse_sample_is_at_most_all_of_the_machine (125.0, want 100.0).
  • GREEN 2f6fc47: the cap in the Rust ports and the C.

Tests

  • obs-util CPU usage and platform tests (Tier 1 and the Tier 3 parity against the C): 52 passed; clippy clean.
  • The cocoa characterization of a zero core count reads 100 now instead of infinity.
  • Test cost: three new Tier 1 tests on plain numbers, under 1 ms, no own threads.
  • Local gate, stamped head 2f6fc47: 4 min 29 s (lint 78 s, rust-test 85 s, cross 56 s, tier2-linux 268 s).

Checklist

🤖 Generated with Claude Code

vpacher and others added 2 commits November 1, 2026 16:38
os_cpu_usage_info_query() reports the process's share of the machine:
process time over wall time over the core count, in percent. On Linux
both times come from times(), in 10 ms ticks, so over a short interval
the quotient is coarse: with one tick of wall time the busy threads can
be charged one tick more than the cores could run, 5 ticks on 4 cores
reads as 125 %. test_ws_cpu_usage (8 threads polling GetStats on one
session under ASan) saw such values (#3367); C and the Rust port agree.

obs_util::cpu_usage::percent() is the formula, and a Tier 1 test asks
for at most 100 % from a coarse sample. The known-list entry of
test_ws_cpu_usage goes.

Fails: a_coarse_sample_is_at_most_all_of_the_machine (125.0, want 100.0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
os_cpu_usage_info_query() caps its value at 100 on every platform:
obs_util::cpu_usage::cap() in the Rust ports (nix through percent(),
cocoa's cpu_percent(), Windows' cpu_query()), and the same
`percent > 100.0 ? 100.0 : percent` in the C of platform-nix,
platform-cocoa and platform-windows, so the ports and C stay equal; a
NaN from an empty interval passes through as before. This differs from
upstream OBS on purpose: the share of the machine cannot pass 100 %, a
reading above it is only the counters' resolution.

The cocoa characterization of a zero core count now reads 100 instead
of infinity. obs-util cpu/platform tests: 52 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9 secs=269 lanes=lint:run,rust-test:run,cross:run,tier2-linux:run,macos-native:n/a,windows-native:n/a
Ci-Attestation: {"at":1793547776,"gate":"rust/all/fmt","host":"linux-x86_64","key":"683bae16d841f02c81d2ab0f69552bea99dedf9e365aa3c1a3960a3519c93fd9","lane":"lint","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":78,"stamp":"db7996236145e49bd4ce58677cd8539e","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"683bae16d841f02c81d2ab0f69552bea99dedf9e365aa3c1a3960a3519c93fd9","lane":"lint","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":78,"stamp":"204b342c6c702a7d5293423252a37a7c","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"53e955bd937e0505c8858af4f3dc45d61b87131a8a2a913dfbb5ada1378539aa","lane":"rust-test","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":85,"stamp":"2a8f1b6cbbe208c0a62d4de325e0d414","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/x86_64-pc-windows-msvc/clippy","host":"linux-x86_64","key":"96091aef01e00685659c4f7d8b5729a7294d9452d81ab0b6b7c226469c7dce05","lane":"cross","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":56,"stamp":"a1a6b068b17b9511c886294ed87fd753","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/x86_64-pc-windows-msvc/dylint","host":"linux-x86_64","key":"96091aef01e00685659c4f7d8b5729a7294d9452d81ab0b6b7c226469c7dce05","lane":"cross","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":56,"stamp":"3380a6591110793c757fd7ec828481e3","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/aarch64-apple-darwin/clippy","host":"linux-x86_64","key":"96091aef01e00685659c4f7d8b5729a7294d9452d81ab0b6b7c226469c7dce05","lane":"cross","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":56,"stamp":"7a086220cbcd9f2709c49d4f5b86bb39","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"rust/aarch64-apple-darwin/dylint","host":"linux-x86_64","key":"96091aef01e00685659c4f7d8b5729a7294d9452d81ab0b6b7c226469c7dce05","lane":"cross","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":56,"stamp":"5dde757c9419ccc4286d5871eb2fcb9d","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
Ci-Attestation: {"at":1793547776,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"f7cf923a4e96d155c0017b46b245c1db28d6f3ca7d9579d52f0160fc9a0a4703","lane":"tier2-linux","parents":["aa55c0461d4655d5b813a1e502192fc306a62592"],"secs":268,"stamp":"915bbc0ae844d9304f6b8db31bc0e18b","tree":"0f4aa3c6bab0aa485bd50b3ee5da5423c29426b9","v":1,"via":"run"}
@vpacher
vpacher requested a review from zackees as a code owner November 1, 2026 15:43
@vpacher
vpacher enabled auto-merge November 1, 2026 15:44

@zackees zackees left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coordinator review: LGTM. Capping at 100 hides a tick-quantization artifact, not real data. The cap is applied the same way in the C reference and in every Rust platform port, so the differential tests stay equal, and a NaN still passes through unchanged in both. The features ledger line is there, which matters because this deliberately differs from upstream. obs-websocket clients see a cpuUsage that is now bounded, which is still within what the v5 protocol allows.

@vpacher
vpacher added this pull request to the merge queue Nov 1, 2026
Merged via the queue into master with commit 49079fe Nov 1, 2026
37 checks passed
@vpacher
vpacher deleted the fix/3367-ws-cpu-usage branch November 1, 2026 16:19
@obs-rust-project-manager obs-rust-project-manager Bot moved this from In review to Done in obs-rust Multiview Nov 1, 2026
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.

obs-websocket: GetStats reports more than 100 % CPU in test_ws_cpu_usage under ASan

2 participants