Add CpuTime metric for per-thread CPU time measurement - #72
Conversation
Implements CpuTime as a new Metric alongside WallClock. Unlike WallClock, CpuTime excludes time spent sleeping, blocking on I/O, or waiting for locks — it measures only active CPU consumption of the current thread. Unix: clock_gettime(CLOCK_THREAD_CPUTIME_ID) Windows: GetThreadTimes(GetCurrentThread()) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks a lot. Will look into this shortly. |
bazhenov
left a comment
There was a problem hiding this comment.
Thanks for your time and effort. I really like the implementation, it's a simple and easy to follow.
2. Windows overhead minimized — GetCurrentThread() called once, GetThreadTimes() is the only thing in the hot path, FILETIME math deferred to after the measurement
3. Integration test deleted, replaced with two unit tests in lib.rs:
- cpu_time_excludes_sleep — 50ms sleep should use < 1ms CPU time
- cpu_time_tracks_wall_clock_during_busy_work — busy loop for 10ms wall-clock, assert CPU time within 1%
4. Stricter assertion — uses bazhenov's suggested approach (wall-clock bounded loop + 1% tolerance)
13 tests pass, clippy clean (only pre-existing too_many_arguments warning).
|
Thanks for the review! All 4 items addressed:
// ticktockbent |
bazhenov
left a comment
There was a problem hiding this comment.
Awesome, thanks a bunch for your contribution!
Summary
CpuTimemetric implementing theMetrictrait, measuring only active CPU time for the current thread (excludes sleep, I/O, lock waits)clock_gettime(CLOCK_THREAD_CPUTIME_ID)— nanosecond precision per-thread CPU clockGetThreadTimes(GetCurrentThread())— user + kernel time for current threadlibcandwindowscrate features already presentThis follows from the discussion in #57 where @bazhenov suggested implementing
CpuTimeas a separate metric after the pluggableMetrictrait landed in #60:Test plan
check_cpu_time_metric— verifies non-zero CPU time for busy workcpu_time_excludes_sleep— verifies CPU time duringthread::sleepis near-zero compared to a busy loop (separate binary, likerusage.rs)cargo test -p tango-bench— all 16 tests passcargo clippy -p tango-bench— no new warnings// ticktockbent