Skip to content

cpu: calculate current CPU usage from delta instead of avg since boot#53

Merged
NotAShelf merged 3 commits intomainfrom
notashelf/push-qolpszrmslmu
Feb 16, 2026
Merged

cpu: calculate current CPU usage from delta instead of avg since boot#53
NotAShelf merged 3 commits intomainfrom
notashelf/push-qolpszrmslmu

Conversation

@NotAShelf
Copy link
Owner

@NotAShelf NotAShelf commented Feb 6, 2026

Fixes #51, at least partially. Did not yet implement time-delta measurements yet, but it is not exactly a part of the issue I was trying to fix. Will be implementing at a later date alongside a --dry-run flag.

This PR only introduces a new expression (cpu-usage-since) for evaluating CPU usage over a specified time interval and refactors CPU usage calculation to be based on recent usage deltas rather than historical averages. The time expression is backed by the humantime crate, which allows for intuitive time formats. Also adds support for logging CPU usage history.

Signed-off-by: NotAShelf raf@notashelf.dev
Change-Id: I2088217f2c2924fa9200f43033bb92426a6a6964

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I2088217f2c2924fa9200f43033bb92426a6a6964
@RGBCube
Copy link
Collaborator

RGBCube commented Feb 6, 2026

A few Q's:

  • is %cpu-usage still the "from first boot" one? We should remove that because people will shoot themselves in the foot again.
  • We need to append to CpuLog a lot more if the config has anything that is cpu-usage-since = "1sec" or similar. Otherwise the small calcs will just be 0 (not even sure it'll be zero, it should be the None expression)
  • In general we need to remove all "from boot" parts.

Allow rules to match on average CPU usage over a specified duration and
enables detecting sustained load patterns rather than just instantaneous
CPU usage.

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I9bea708d7ed5888a697e9a4ede7b8dfa6a6a6964
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I3385837946b5f4f19980b4bd5fe899696a6a6964
@NotAShelf NotAShelf force-pushed the notashelf/push-qolpszrmslmu branch from 7d20abf to 0abb36f Compare February 11, 2026 10:29
@NotAShelf
Copy link
Owner Author

NotAShelf commented Feb 11, 2026

is %cpu-usage still the "from first boot" one? We should remove that because people will shoot themselves in the foot again.

No, it's no longer from boot because we're changing it; cpu_log.usage now uses cpu.current_usage(), which is delta-based, and state.cpu_usage comes from cpu_log.usage so %cpu-usage returns delta-based usage

We need to append to CpuLog a lot more if the config has anything that is cpu-usage-since = "1sec" or similar. Otherwise the small calcs will just be 0 (not even sure it'll be zero, it should be the None expression)

I think the solution here is to sample at a fixed high frequency (like 100ms or 500ms) regardless of poll_interval, and then cpu_log.iter() can filter by the requested duration window.

In general we need to remove all "from boot" parts.

You're right, I'll remove those. Addressed the rest of your comments.

@RGBCube
Copy link
Collaborator

RGBCube commented Feb 12, 2026

No, it's no longer from boot because we're changing it; cpu_log.usage now uses cpu.current_usage(), which is delta-based, and state.cpu_usage comes from cpu_log.usage so %cpu-usage returns delta-based usage

How much of a delta? I don't think we should have that, everyone should set the delta themselves.

I think the solution here is to sample at a fixed high frequency (like 100ms or 500ms) regardless of poll_interval, and then cpu_log.iter() can filter by the requested duration window.

Won't that be too intensive? Also sounds like a waste

@NotAShelf
Copy link
Owner Author

Do you have a better idea?

@RGBCube
Copy link
Collaborator

RGBCube commented Feb 12, 2026

The idea I had was traverse the Expression and collect all of the cpu-usage-since = "..." durations and sample with some fraction of that delta. We can use a derive Visitor crate to make this really easy and get the min usage-since Duration

@NotAShelf
Copy link
Owner Author

It does not seem like I'll be receiving any more meaningful comments, so I'm merging. The issue is fixed, nitpicks can wait.

@NotAShelf NotAShelf merged commit 0506c77 into main Feb 16, 2026
2 checks passed
@NotAShelf NotAShelf deleted the notashelf/push-qolpszrmslmu branch February 16, 2026 06:03
@RGBCube
Copy link
Collaborator

RGBCube commented Feb 16, 2026

bro casually ignored the %cpu-usage variable being completely broken and useless ☠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cpu usage is incorrectly computed

2 participants