Skip to content

Use floats for tracking cpu time.#563

Closed
noncrab wants to merge 2 commits intotikv:masterfrom
noncrab:issue-560-fractional-cpu-seconds
Closed

Use floats for tracking cpu time.#563
noncrab wants to merge 2 commits intotikv:masterfrom
noncrab:issue-560-fractional-cpu-seconds

Conversation

@noncrab
Copy link
Copy Markdown

@noncrab noncrab commented Mar 28, 2026

Fixes #560.

The one thing i'm not sure about is trading the well-defined wrap-around semantics for the risks of running out of bits in the mantissa. But then again, this uses an AtomicF64 under the hood, whose mantissa is 53 bits wide (can accurately hold an integer of up to ~9e+15).

Practically, by my (hand-wavey) reckoning, that'll mean we'll not be able to accurately represent times at ~1µs of precision after ~285 CPU millenia. I'm not sure how much of an issue that might be, or how best to record that concern.

image

@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 28, 2026

Welcome @noncrab! It looks like this is your first PR to tikv/rust-prometheus 🎉

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6650f6cf-243f-4d5c-b785-873964879a76

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The ProcessCollector's cpu_total metric was changed from an integer counter to a gauge; CPU total is now computed and stored as a floating-point value and assigned directly to the gauge instead of incrementing a counter. Imports were adjusted to include Gauge.

Changes

Cohort / File(s) Summary
Process collector metric
src/process_collector.rs
Replaced cpu_total type from IntCounter to Gauge; changed initialization to Gauge::with_opts(...).
CPU total computation & update
src/process_collector.rs
Replaced integer-based (utime + stime)/CLK_TCK and delta/inc logic with floating-point (utime + stime) as f64 / CLK_TCK as f64 and cpu_total.set(total) assignment (removed prior delta/get/saturating_sub/inc_by flow).
Imports
src/process_collector.rs
Added Gauge to imports while retaining existing metric imports.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰
I swapped my counter coat for a gauge so neat,
Counting seconds now in floats, not discrete.
I set the total — no more deltas to chase,
Metrics hop forward with gentle grace.
Hooray for precision in every trace! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: converting CPU time tracking from integer to floating-point representation.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #560: converts cpu_total from IntCounter to Gauge, implements floating-point tracking, and replaces delta logic with direct assignment.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the CPU time tracking modification in process_collector.rs with no unrelated alterations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.


// cpu
let total = (stat.utime + stat.stime) / *CLK_TCK as u64;
let total = (stat.utime + stat.stime) as f64 / *CLK_TCK as f64;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

As noted in the description, I'm not quite sure how to represent the risks of precision loss here. Is it worth noting?

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/process_collector.rs (1)

9-9: Remove unused IntCounter import.

IntCounter is no longer used in this file after the refactoring.

Proposed fix
-use crate::counter::{Counter, IntCounter};
+use crate::counter::Counter;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/process_collector.rs` at line 9, Remove the unused IntCounter import from
the use statement in process_collector.rs: update the existing use
crate::counter::{Counter, IntCounter}; to only import the symbols actually used
(e.g., Counter) so the IntCounter identifier is no longer referenced in the
import list.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/process_collector.rs`:
- Around line 166-168: The update to cpu_total can underflow because total -
past may be negative; restore underflow protection by comparing total and past
before calling self.cpu_total.inc_by(...): compute total as (stat.utime +
stat.stime) as f64 / *CLK_TCK as f64, read past via self.cpu_total.get(), and
only call self.cpu_total.inc_by(total - past) when total > past (otherwise skip
or treat the delta as 0) to prevent negative increments and preserve counter
semantics of the cpu_total counter and inc_by usage.

---

Nitpick comments:
In `@src/process_collector.rs`:
- Line 9: Remove the unused IntCounter import from the use statement in
process_collector.rs: update the existing use crate::counter::{Counter,
IntCounter}; to only import the symbols actually used (e.g., Counter) so the
IntCounter identifier is no longer referenced in the import list.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e0c7d456-c04e-40c5-aacc-90011041a49b

📥 Commits

Reviewing files that changed from the base of the PR and between 8151418 and 89dc0d7.

📒 Files selected for processing (1)
  • src/process_collector.rs

@noncrab noncrab force-pushed the issue-560-fractional-cpu-seconds branch from 89dc0d7 to f82fa41 Compare March 28, 2026 13:23
@noncrab noncrab marked this pull request as draft March 28, 2026 13:23
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot bot commented Mar 28, 2026

Thanks for your pull request. Before we can look at it, you'll need to add a 'DCO signoff' to your commits.

📝 Please follow instructions in the contributing guide to update your commits with the DCO

Full details of the Developer Certificate of Origin can be found at developercertificate.org.

The list of commits missing DCO signoff:

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@noncrab
Copy link
Copy Markdown
Author

noncrab commented Mar 28, 2026

I clearly opened this prematurely.

@noncrab noncrab closed this Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

process_cpu_seconds_total is incorrectly registered as an IntCounter

1 participant