Bug 497341

Summary: Individual Core Usage incorrectly shows 100% CPU usage, disagreeing with htop and System Monitor
Product: [Frameworks and Libraries] ksystemstats Reporter: Andrew Kelley <andrew>
Component: GeneralAssignee: Plasma Bugs List <plasma-bugs>
Status: ASSIGNED ---    
Severity: major CC: ahiemstra, kde, nate, shu72
Priority: NOR    
Version: 6.2.4   
Target Milestone: ---   
Platform: NixOS   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: screenshot
screenshot with htop and system monitor

Description Andrew Kelley 2024-12-11 23:01:21 UTC
Created attachment 176536 [details]
screenshot

SUMMARY

Individual Core Usage shows wrong data sometimes, incorrectly marking applications as 100% CPU usage when they are idle. Please audit its logic to make sure it matches System Monitor and htop.

This was brought up here and incorrectly dismissed: https://discuss.kde.org/t/individual-core-usage-doesnt-match-top-by-a-lot/9232

STEPS TO REPRODUCE and OBSERVED RESULT

1. Wait for the public release of Ghostty, scheduled for December 31, 2024, or ask Mitchell Hashimoto for a private beta invitation.
2. Follow the steps in the README of that project to build from source.
3. Run the ghostty binary and observe 2 apparently pegged CPU cores per open window.
4. Run htop and notice ghostty actually is idle.
5. Run System Monitor and notice ghostty is actually idle.

EXPECTED RESULT

No bars shown in the Individual Core Usage widget when the app is idling.

SOFTWARE/OS VERSIONS

Operating System: NixOS 24.11
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.8.0
Qt Version: 6.8.0
Kernel Version: 6.12.3 (64-bit)
Graphics Platform: Wayland
Processors: 32 × AMD Ryzen 9 7950X 16-Core Processor
Memory: 62.0 GiB of RAM
Graphics Processor: AMD Radeon RX 7900 XTX

ADDITIONAL INFORMATION

I observed this also in Plasma 5, before upgrading to Plasma 6.2.
Comment 1 Nate Graham 2024-12-12 18:48:43 UTC
The widget doesn't measure apps but rather than whole system. Apps can still be consuming CPU even when they're idle, too.

Can you attach a new screenshot that shows the output of htop and System Monitor disagreeing with what the widget is saying?
Comment 2 Andrew Kelley 2024-12-12 22:49:02 UTC
Created attachment 176567 [details]
screenshot with htop and system monitor

Here is the requested screenshot.
Comment 3 Andrew Kelley 2024-12-12 22:52:53 UTC
To be clear when I use the word "idle" here, I mean "using 0% CPU".
Comment 4 Andrew Kelley 2024-12-26 22:12:41 UTC
Updated steps to reproduce now that ghostty is released as free and open source software:

First, ensure x11 or wayland, libadwaita, pkg-config, and gtk4 development system libraries are installed.

Build from source:

```
wget https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz
tar xf zig-linux-x86_64-0.13.0.tar.xz
git clone https://github.com/ghostty-org/ghostty/
cd ghostty
git checkout v1.0.0
../zig-linux-x86_64-0.13.0/zig build
./zig-out/bin/ghostty
```

Observe 2 apparently pegged CPU cores per open window.
Run htop and notice ghostty actually is idle.
Run System Monitor and notice ghostty is actually idle.
Comment 5 Steven H. 2024-12-27 09:27:30 UTC
For what it's worth I can confirm this happening on my system too:

Operating System: openSUSE Tumbleweed 20241224
KDE Plasma Version: 6.2.4
KDE Frameworks Version: 6.9.0
Qt Version: 6.8.1
Kernel Version: 6.12.6-1-default (64-bit)
Graphics Platform: Wayland
Processors: 12 × AMD Ryzen 5 5600X 6-Core Processor
Memory: 15.5 GiB of RAM
Graphics Processor: AMD Radeon RX 6600 XT
Manufacturer: Micro-Star International Co., Ltd.
Product Name: MS-7C56
System Version: 6.0

Another screenshot that illustrates the problem: https://ibb.co/bzdkbvS 

On the dashboard the CPU usage is constantly at around 17 percent while Ghostty is running (and that is mirrored when using the applet "Individual Core Usage" which shows 2 of 12 cores constantly busy with Ghostty). But the actual list of processes (or htop) show that Ghostty is basically idle at zero percent cpu usage. 

This is the first time I've seen this happen though. No other software than Ghostty leads to this false reporting of cpu usage.
Comment 6 Arjen Hiemstra 2025-01-23 15:29:07 UTC
The CPU usage values are computed by reading `/proc/stat` which contains the amount of time the different cores have been in different states. We divide these values into three groups: system, user and wait and calculate the percentages of these. The total CPU usage percentage is then these three added together.

Now, one of the values in "wait" is "iowait" which is the amount of time spent idle but while some process is waiting on IO. Now, I don't know what GhostTTY is doing but the end result is that it's causing almost 100% iowait on a core. This then results in the total usage sensor reporting 100% usage.

While we should probably remove iowait from the CPU usage accounting (as it is not actually doing anything), there *also* seems to be a problem in GhosTTY, other processes don't cause this much iowait.

Note that this is actually also visible in top, the "%wa" statistic at the top shows ~16% for my machine when ghostty is running.
Comment 7 Bug Janitor Service 2025-01-23 15:40:19 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/ksystemstats/-/merge_requests/104