Skip to content

requesthandler: Measure GetStats CPU usage per session - #19

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:fix/cpu-usage-per-session
Oct 20, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:fix/cpu-usage-per-session

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Every client shared one os_cpu_usage_info_t (src/obs-websocket.cpp line 64). os_cpu_usage_info_query() moves its last times forward on every call, so a client's GetStats reported the CPU usage since the last GetStats from any client, not since its own. With two clients polling at different rates, the slower one saw only the slice since the faster one's last call: mostly 0, sometimes a whole core's worth. The query also ran without a lock, while requests run in parallel on the thread pool, which is a data race on the shared state. In the Rust obs-util it also means two &mut references to it at once.

Each session now has its own CPU usage state. It starts when the client connects and is queried under a lock, so cpuUsage covers the time since the same client's previous GetStats (or since it connected). Requests without a session (vendor requests) still use the shared state, now also under a lock. The cpuUsage description in the request docs and in docs/generated says so.

Measured end to end: OBS recording a 640x360 scene, client A polling GetStats every 50 ms and client B every 2 s. B's cpuUsage is compared with OBS's own CPU time over B's interval, read from /proc/<pid>/stat:

Build Client B's cpuUsage (actual)
v0.1.0-beta.20261018 AppImage (submodule 0d663ee) 0.00% (5.37), 0.00% (4.81), 6.25% (4.65), 0.00% (4.71), 3.12% (4.59), 0.00% (4.50)
This branch, C libobs lane 4.38% (4.37), 4.34% (4.34), 4.38% (4.37), 4.34% (4.34), 4.35% (4.37), 4.34% (4.34)
This branch, Rust libobs lane 4.34% (4.34), 4.34% (4.34), 4.34% (4.34), 4.31% (4.31), 4.34% (4.34), 4.35% (4.37)

Tested in obs-rust/obs-studio with test_ws_cpu_usage, which sends GetStats through RequestHandler with two sessions. It burns CPU, polls from session a, idles as long, then polls from session b. At 0d663ee, b reports 0.000% against a's 6.250% in both libobs lanes. With this branch, b reports about half of a's usage (3.047% against 6.094%), 5 of 5 runs in each lane. A second case sends GetStats from 8 threads at once, with and without a session.

Refs obs-rust/obs-studio#1346. The obs-rust/obs-studio PR that moves the submodule to this commit carries the regression test and closes the issue.

Every client shared one os_cpu_usage_info_t, and
os_cpu_usage_info_query() moves its last times forward on every call.
A client's GetStats reported the CPU usage since the last GetStats from
any client, not since its own: with two clients polling, each saw only
the slice since the other one's call, often 0. The query also ran
without a lock while requests run in parallel on the thread pool, a
data race on the shared state.

Each session now has its own CPU usage state, started when it connects
and queried under a lock, so cpuUsage covers the time since the same
client's previous GetStats. Requests without a session (vendor
requests) use the shared state, now also under a lock.

Refs obs-rust/obs-studio#1346

@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. The per-session os_cpu_usage_info_t and the mutex on the shared fallback are correct and fix a real race between parallel GetStats requests.

Two things need @zackees before this merges:

  1. The OBS WebSocket compatibility directive in obs-studio's AGENTS.md says the C++ plugin stays unmodified as a compatibility check and must not be changed to accommodate Rust OBS. The description cites the Rust obs-util aliasing (two &mut to one state) as part of the motivation; under the directive that should be fixed on the Rust side instead.
  2. It changes the protocol meaning of cpuUsage (since this client's previous call rather than since any client's), which is a v5 protocol behaviour change visible to existing clients.

@zackees
zackees merged commit e48c863 into obs-rust:master Oct 20, 2026
1 check passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 23, 2026
…bs-rust#1996)

* plugins/obs-websocket: bump to notify the parallel batch waiter under the lock

Bumps the submodule from 0d663eefb to 99dd8468b, which brings in:
- obs-rust/obs-websocket#18: list items without a name or value report empty strings (obs-rust#1266)
- obs-rust/obs-websocket#19: GetStats CPU usage is measured per session
- 99dd8468b: a parallel RequestBatch notifies its waiter while holding the
  lock, so the waiter cannot return and free the shared batch state while
  the last task is still notifying (the intermittent test_ws_batch_order
  SIGSEGV on macOS Tier 2).

* docs: record the parallel RequestBatch crash fix in the features ledger
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 24, 2026
…#2250)

obs-websocket used to keep one os_cpu_usage_info_t for every client, and
os_cpu_usage_info_query() moves its last times forward on every call. A
client's GetStats reported the CPU usage since the last GetStats from
any client, not since its own, and the query ran without a lock while
requests run in parallel on the thread pool. The fork fix,
obs-rust/obs-websocket#19 (one locked state per session, a shared locked
one for requests without a session), came in with the submodule bump in
obs-rust#1996.

test_ws_cpu_usage sends GetStats through RequestHandler with two
sessions: it burns CPU, polls from session a, idles as long, then polls
from session b. b's interval covers the burn too, so it must report
about half of a's usage. With the submodule at a4df0f3, the commit
before the fix, b reports only the idle slice (0.000% against 6.094%)
and the case fails; at the current pin it reports 3.047%. A second case
sends GetStats from 8 threads at once, with and without a session.

The fix gets its line in docs/features.md.

Fixes obs-rust#1346
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.

2 participants