Skip to content

Use CPU_METER_STEAL for 'virtualized' CPU time in non-detailed mode#1922

Open
Explorer09 wants to merge 5 commits into
htop-dev:mainfrom
Explorer09:cpu-meter-items
Open

Use CPU_METER_STEAL for 'virtualized' CPU time in non-detailed mode#1922
Explorer09 wants to merge 5 commits into
htop-dev:mainfrom
Explorer09:cpu-meter-items

Conversation

@Explorer09

@Explorer09 Explorer09 commented Mar 17, 2026

Copy link
Copy Markdown
Contributor

The virtualized CPU time will now write to the CPU_METER_STEAL item in the non-detailed CPU meter mode. Also fix the CPU meter text in the help screen.

This pull request also includes code cleanups such as unnecessary assignments of v[CPU_METER_IRQ] in NetBSD and OpenBSD platforms.

I think my approach is better than what's proposed in #1921.

Resolves #1920.

Comment thread pcp/Platform.c
value = values[CPU_STEAL_PERIOD].ull + values[CPU_GUEST_PERIOD].ull;
v[CPU_METER_IRQ] = value / total * 100.0;
this->curItems = 4;
v[CPU_METER_STEAL] = value / total * 100.0;

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.

"value" here is not "steal" time though - its the sum of steal+guest now. So this hasn't really solved things for your unmerged graphing PR. IOW, there's a problem switching on/off detailed CPU mode because the historical data in the steal slot transitions between sum/not-sum/sum/not-sum ... so the "steal" value is incorrect whenever detailed mode changes state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't get it… What are you suggesting? I think the historical data having "sum/not-sum/sum/not-sum", will not present any visual display glitch.

Because "steal" and "guest" are presented as the same color in all color schemes of htop

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.

| Because "steal" and "guest" are presented as the same color

Good point, it'll work by luck until someone wants to separate out those categories like in most tools.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, it'll work by luck until someone wants to separate out those categories like in most tools.

I won't call it working "by luck", because I can anticipate that the "steal" and "guest" colors be separate like this:

[CPU_STEAL] = A_BOLD | ColorPair(Cyan, Black),
[CPU_GUEST] = ColorPair(Cyan, Black),

But then, how is it wrong by merging the "steal" and "guest" into a single item in the non-detailed CPU meter mode?

Isn't that the whole point of the non-detailed mode? That the unnecessary details of CPU time items can be merged (just like "irq" and "sort-irq" merged with "kernel")?

I really don't get what you are suggesting.

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.

| I won't call it working "by luck",

It is just luck - we very rarely have two items sharing a color in a Meter.

| But then, how is it wrong by merging the "steal" and "guest" into a single item

Its not wrong - it just remains obfuscated - like the existing code has always been, overloading one CPU values slot with multiple values from another. The bug can be fixed by a trivial change compared to this proposal, so its a nack for me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

| But then, how is it wrong by merging the "steal" and "guest" into a single item

Its not wrong - it just remains obfuscated - like the existing code has always been, overloading one CPU values slot with multiple values from another. The bug can be fixed by a trivial change compared to this proposal, so its a nack for me.

I would claim the use of IRQ slot for virtual CPU time is plain wrong despite your fix looks trivial. The CPU meter code has fixed ("fixing" for the "making things unmovable" meaning) that item/slot as CPU_METER_IRQ and it shouldn't be overloaded with another use such as "virtualized". It breaks forward compatibility - such as with the not-yet-merged #714.

#714 relies on an assumption that the meanings of item indices of a meter to remain stable. It's also so for the meter character allocation (|#*@$%&.) in the monochrome mode. It would be more of a UX surprise when the @ character in CPU meter suddenly means different things with the "detailed CPU time" toggle. I would rather have "virtual" CPU time permanently represented by % and & and document such in the manual.

@natoscott

Copy link
Copy Markdown
Member

| It breaks forward compatibility - such as with the not-yet-merged #714

Feel free to queue your proposed change up in that PR then - if that's ever merged, that'd be an appropriate time to add this complexity. In the meantime its not warranted.

@Explorer09

Copy link
Copy Markdown
Contributor Author

| It breaks forward compatibility - such as with the not-yet-merged #714

Feel free to queue your proposed change up in that PR then - if that's ever merged, that'd be an appropriate time to add this complexity. In the meantime its not warranted.

I already proposed this PR for the exact problem. I don't need to file another one. And this PR effectively supercedes your PR (if I make the changes on top of your PR they would effectively revert your changes anyway).

@natoscott

Copy link
Copy Markdown
Member

OK, sounds good.

@Explorer09 Explorer09 changed the title Fix 'virtualized' CPU bar color in non-detailed mode Use CPU_METER_STEAL for 'virtualized' CPU time in non-detailed mode Mar 18, 2026
@Explorer09

Copy link
Copy Markdown
Contributor Author

| It breaks forward compatibility - such as with the not-yet-merged #714
Feel free to queue your proposed change up in that PR then - if that's ever merged, that'd be an appropriate time to add this complexity. In the meantime its not warranted.

I already proposed this PR for the exact problem. I don't need to file another one. And this PR effectively supercedes your PR (if I make the changes on top of your PR they would effectively revert your changes anyway).

Sigh... I've told that this PR (#1922) requires reverting #1921 in order to build on top of it. What's the silly management decision that decided to merge #1921 first?

@Explorer09 Explorer09 force-pushed the cpu-meter-items branch 3 times, most recently from 6f72b42 to b7eb686 Compare April 13, 2026 17:04
@Explorer09 Explorer09 force-pushed the cpu-meter-items branch 3 times, most recently from c8b6017 to daf01bd Compare April 20, 2026 07:53
@Explorer09 Explorer09 force-pushed the cpu-meter-items branch 3 times, most recently from fae0cc2 to 18d95a2 Compare May 1, 2026 10:01
@Explorer09 Explorer09 force-pushed the cpu-meter-items branch 2 times, most recently from 820a417 to 6cc70ef Compare May 11, 2026 03:10
@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

This PR routes “virtualized” CPU time to CPU_METER_STEAL in non-detailed mode and fixes the corresponding help/CPU bar rendering so the “virtualized” (orange/virtualized) segment reflects steal/virtualized time rather than the IRQ slot.

Non-detailed CPU meter mapping

  • Linux + PCP: In non-detailed mode, the combined steal + guest percentage is assigned to the STEAL meter; IRQ and softIRQ are set to 0.0. curItems becomes 3 (or 6 when accountGuestInCPUMeter is enabled).
  • NetBSD + OpenBSD: Reduced the number of visible meter items in non-detailed mode (to 3) and trimmed zero/placeholder handling in the detailed path (NetBSD to 4, OpenBSD to 4). The extra components are now consistently zeroed after the detailed/non-detailed branch rather than being conditionally populated, avoiding unnecessary initialization and mapping noise.

Meter/help rendering

  • CPUMeter.c: Cleans up attribute handling by removing the separate summary attribute array, extending CPUMeter_attributes to include CPU_IOWAIT, and eliminating the update-time switching between detailed vs summary attributes. Also corrects the non-detailed "vir:" output to color/render CPU_METER_STEAL using CPU_STEAL colors.
  • Action.c: Updates the help screen “virtualized” bar to match the new mapping and rendering rules, including correct monochrome vs non-monochrome text/padding behavior.

Code hygiene / change flow

The implementation stays logically clean by splitting responsibilities by file: UI/help text + rendering (Action.c, CPUMeter.c) and platform-specific CPU field mapping (Platform.c per OS), with consistent curItems/zeroing rules across platforms and no exported/public API changes.

Walkthrough

This PR consolidates CPUMeter attributes into a single array (now including CPU_IOWAIT), removes runtime switching between detailed/summary attribute sets, updates actionHelp to render virtualization markers based on CRT_colorScheme (monochrome "/-/-/", others "/"), and aligns platform CPU value assignment (Linux, NetBSD, OpenBSD, PCP) so non-detailed mode zeros IRQ/softIRQ, places steal+guest into STEAL, and adjusts curItems counts accordingly.

Assessment against linked issues

Objective Addressed Explanation
Add description of orange-colored CPU bar to help screen [#1920]

Poem

A compact bar now tells its tale,
Monochrome ticks and colored scale,
Steal and guest in one clear place,
Help explains the orange trace,
Small code, big clarity.


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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e70bc258-53d5-4f74-badd-92edcf46b298

📥 Commits

Reviewing files that changed from the base of the PR and between b7f9df9 and 78d5fc4.

📒 Files selected for processing (6)
  • Action.c
  • CPUMeter.c
  • linux/Platform.c
  • netbsd/Platform.c
  • openbsd/Platform.c
  • pcp/Platform.c
💤 Files with no reviewable changes (1)
  • CPUMeter.c

Comment thread netbsd/Platform.c
Comment thread openbsd/Platform.c Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3607d072-290d-400a-bb4c-78db2a41ee41

📥 Commits

Reviewing files that changed from the base of the PR and between 78d5fc4 and cec7965.

📒 Files selected for processing (6)
  • Action.c
  • CPUMeter.c
  • linux/Platform.c
  • netbsd/Platform.c
  • openbsd/Platform.c
  • pcp/Platform.c
💤 Files with no reviewable changes (1)
  • CPUMeter.c

Comment thread linux/Platform.c
Comment on lines +378 to +380
if (settings->accountGuestInCPUMeter) {
this->curItems = 6;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Drop braces for the single-statement if.

Use brace-less form here to match project control-flow style for trivial single statements.

Suggested patch
-      if (settings->accountGuestInCPUMeter) {
-         this->curItems = 6;
-      }
+      if (settings->accountGuestInCPUMeter)
+         this->curItems = 6;

As per coding guidelines, "Omit braces around simple single statements (return, break, continue, trivial assignments); never put control flow body on same line as condition; if any block in an if/else chain needs braces, all blocks get braces".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (settings->accountGuestInCPUMeter) {
this->curItems = 6;
}
if (settings->accountGuestInCPUMeter)
this->curItems = 6;

Comment thread pcp/Platform.c
Comment on lines +583 to +585
if (settings->accountGuestInCPUMeter) {
this->curItems = 6;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Remove braces around this trivial single-line if.

This block is a single assignment and should follow the repository control-flow style.

Suggested patch
-      if (settings->accountGuestInCPUMeter) {
-         this->curItems = 6;
-      }
+      if (settings->accountGuestInCPUMeter)
+         this->curItems = 6;

As per coding guidelines, "Omit braces around simple single statements (return, break, continue, trivial assignments); never put control flow body on same line as condition; if any block in an if/else chain needs braces, all blocks get braces".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (settings->accountGuestInCPUMeter) {
this->curItems = 6;
}
if (settings->accountGuestInCPUMeter)
this->curItems = 6;

When both the options "Add guest time in CPU meter percentage" and
"Detailed CPU time" are turned off, the CPU meter used to show the
"virtual" CPU time in the bar display as a glitch. Now fix it.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 0f069663-1ccc-44a0-930e-18ec6d4a5b17

📥 Commits

Reviewing files that changed from the base of the PR and between cec7965 and e4584b9.

📒 Files selected for processing (6)
  • Action.c
  • CPUMeter.c
  • linux/Platform.c
  • netbsd/Platform.c
  • openbsd/Platform.c
  • pcp/Platform.c
💤 Files with no reviewable changes (1)
  • CPUMeter.c

Comment thread linux/Platform.c
Comment on lines +373 to +380
this->curItems = 3;

v[CPU_METER_IRQ] = 0.0; // Accounted in 'kernel'
v[CPU_METER_SOFTIRQ] = 0.0; // Accounted in 'kernel'
v[CPU_METER_STEAL] = (cpuData->stealPeriod + cpuData->guestPeriod) / total * 100.0;
if (settings->accountGuestInCPUMeter) {
this->curItems = 6;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Non-detailed virtualized CPU mapping is currently dropped/misreported after moving it to CPU_METER_STEAL.

You now write virtualized time to CPU_METER_STEAL, but default curItems = 3 excludes that slot from sumPositiveValues() and bar rendering, and the non-detailed CPUMeter text path still reads CPU_METER_IRQ for vir:. Result: virtualized time is either hidden or shown as 0.0 in non-detailed mode.

  • linux/Platform.c#L373-L380: include the STEAL slot in non-detailed curItems whenever virtualized time is populated, so totals/bar include it.
  • pcp/Platform.c#L577-L585: apply the same curItems fix so non-detailed totals/bar do not drop STEAL.
  • Also update the non-detailed CPUMeter consumer (vir: source index) to read from the same slot you now populate for virtualized time.

Based on the provided CPUMeter display snippet (CPUMeter.c:152-193), non-detailed vir: still consumes CPU_METER_IRQ.

📍 Affects 2 files
  • linux/Platform.c#L373-L380 (this comment)
  • pcp/Platform.c#L577-L585

The "curItems" member of the CPUMeter will be set to not larger than
necessary. (Note: NetBSD and OpenBSD code currently do not support
"softirq", "steal", "guest" and "iowait" times.)

The meter values of "softirq", "steal", "guest" and "iowait" are now
unconditionally set to 0.0 on update, just to be safe.

No user-visible change in behavior is intended.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
The time displayed is (steal+guest), not just guest CPU time.

Regression from 3d8fa0b

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
Linux and PCP platform code used to write the 'virtualized'
(steal+guest) CPU time into the CPU_METER_IRQ item when the "detailed
CPU time" option is off, which would result in a wrong color painted for
virtual CPU time in bar mode. Write the 'virtualized' CPU time to
CPU_METER_STEAL instead, which is more appropriate for the job.

Also update the "virtualized" CPU meter item in the help screen: (1) The
color is now CPU_STEAL for consistency. (2) In monochrome mode, two
dummy items are displayed before the "virtualized" word so that the
bar meter symbol mapping is correct.

Signed-off-by: Kang-Che Sung <explorer09@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
linux/Platform.c (1)

373-380: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Non-detailed CPU meter undercounts virtualized time due curItems range mismatch. Both implementations now write virtualized CPU to CPU_METER_STEAL (index 5), but default curItems = 3 truncates aggregation/rendering to indices 0..2.

  • linux/Platform.c#L373-L380: set non-detailed curItems high enough to include index 5 whenever CPU_METER_STEAL is populated (typically 6), keeping IRQ/SOFTIRQ as zero-filled placeholders.
  • pcp/Platform.c#L577-L585: apply the same curItems rule so sumPositiveValues() and bar rendering include virtualized CPU in non-detailed mode.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 7464b53e-84f6-4eb8-8551-9489b62c7b63

📥 Commits

Reviewing files that changed from the base of the PR and between 234bcce and b04df08.

📒 Files selected for processing (6)
  • Action.c
  • CPUMeter.c
  • linux/Platform.c
  • netbsd/Platform.c
  • openbsd/Platform.c
  • pcp/Platform.c

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.

Help is missing description of orange-colored CPU bar

2 participants