Skip to content

devops: show the cpu load and its culprit in the windows tray, the fan gives no hint - #390

Open
landsman wants to merge 8 commits into
mainfrom
devops/windows-cpu-tray
Open

landsman wants to merge 8 commits into
mainfrom
devops/windows-cpu-tray

Conversation

@landsman

@landsman landsman commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

On 2026-12-04 the fan spun up with nothing open that could explain it. The cause
was a Windows Update preview install: DismHost and TiWorker unpacking files,
with Defender (MsMpEng) scanning them as they landed. None of those shows a CPU
figure in Get-Process without elevation, and the fan itself gives no hint which
process is behind it.

cpu-tray.ps1 puts the answer in the tray:

  • The icon is the whole-machine load, counted the way Task Manager counts it:
    grey when quiet, orange from half the alert threshold, red above it.
  • Hover gives the top process, click the top ten. Processes are grouped by
    name, so twenty chrome.exe are one line, and the usual background suspects
    also say what they are doing, e.g. Windows Update installing.
  • A notification comes when the load averages 20% or more for a minute, naming
    the three processes busiest on average over that minute, not the last tick's
    peak. It comes once per busy spell, not again until the load drops below 70% of
    the threshold.

It reads the Process performance counters rather than Get-Process, so protected
processes are covered too, in one call per tick. A tick that keeps failing (an
English counter name on another display language, say) shows the error in the
tooltip and logs it once to %TEMP%\cpu-tray.log. The thresholds are parameters
at the top of the script.

apply.ps1 writes a CPU tray shortcut into the Startup folder on every run, so
a moved checkout gets the new path. It always targets Windows PowerShell 5.1, even
when apply.ps1 ran under pwsh. The icon is pinned to the taskbar through the
same IsPromoted value Settings writes; the script looks for that entry during
its first minute only, since Windows 10 never writes one. To stop it starting,
delete the shortcut; to quit the running one, use Quit in its menu.

Not checked on Windows yet: the pin taking effect live, and the load the tray
itself costs.

@landsman

landsman commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

@claude review it

@claude

claude Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Claude finished @landsman's task in 52s —— View job


Review of #390: CPU tray

I read the diff and the surrounding apply.ps1. I couldn't run any of it, because it needs Windows PowerShell 5.1 with a desktop session. These are findings from reading, not from running.

The design is sound. Reading the Process counters in one ReadCategory call is the right way to see MsMpEng, TiWorker and System. The load math is correct: Idle ticks over delta stamp × cores matches Task Manager. The DestroyIcon handling, the single-instance mutex and the re-arm hysteresis all look right. Both new files are ASCII-only, as apply.ps1 requires.

Things worth fixing

  1. The culprit is a single 2-second snapshot. $script:last = $load keeps only the latest tick's Top. The alert is triggered by a 60 s average but names whatever was busiest in the last 2 s.

    • The hover text will flicker between processes.
    • The notification can name a transient process (WmiPrvSE, dwm) instead of the sustained one (TiWorker).
    • This is the one thing the tool exists to show. Keep per-name percentages for the last $window ticks and rank by their mean, at least for the notification body. The icon number can stay instantaneous.
  2. Set-AlwaysShown retries forever if it never matches. $script:promoted only flips once an entry is found, so it scans NotifyIconSettings and calls Get-ItemProperty on every key every 2 s. That includes Windows 10 and any machine where the InitialTooltip or ExecutablePath differs.

    • It adds constant registry churn, and the README's "about 0.2%" figure won't hold.
    • Cap the retries, for example 30 ticks, or stop after the first success or the first N misses.
    • The match is also loose. Any powershell.exe tray icon whose first tooltip starts with CPU would be promoted. Matching InitialTooltip -eq 'CPU' is tighter, since the script sets $tray.Text = 'CPU' before Visible.
  3. The shortcut hardcodes $PSHOME\powershell.exe. apply.ps1 documents running under powershell, so this is fine today. If someone runs it under pwsh, $PSHOME points at PowerShell 7, which has no powershell.exe, and the shortcut breaks silently. The tray script also relies on -STA and WinForms behaviour that 5.1 defaults to. Use Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' instead.

  4. apply.ps1 is elevated, so the shortcut lands in the Startup folder of whichever account elevated. That is the same user in the normal single-account case. If UAC prompts for a different admin account, the shortcut goes to that account's profile. It's probably fine here, but a one-line comment would record the assumption.

Smaller points

  • Localised counter names. $data['% Processor Time'] and 'ID Process' are English names. On a non-English UI the lookup may return $null and every tick falls into the catch. The README suggests English UI, so this is low risk. If it fails, the tooltip shows the error, which is a reasonable failure mode.
  • Short-lived processes are invisible in Top. A process that started and exited between ticks is skipped (if (-not $old) { continue }), but its CPU is still counted in Total. A burst of short children, such as a build, can show a high icon with a modest top-10. That's inherent to sampling, but worth a sentence in the README.
  • The catch hides real faults. Under $ErrorActionPreference = 'Stop', a persistent failure such as a bad counter lookup is shown only as a truncated tooltip, with no log. A one-line append to $env:TEMP\cpu-tray.log would make "the icon is stuck at 0" debuggable.
  • The catch line builds the message twice. Assign $message once and reuse it.
  • README claims that can't be verified from here.
    • "holds on a fresh machine" depends on Explorer picking up a live IsPromoted write.
    • The "0.2%" figure is only credible if item 2 is fixed.
    • Please confirm both on the T480.

Verdict

No blockers. I'd fix item 1 before merge, because it undermines the purpose of the feature, and item 2 because it's cheap. The rest are optional.
· branch devops/windows-cpu-tray

@landsman

landsman commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

All of it addressed, nothing declined. Nothing here can run PowerShell, so CI's parse check is the only check; behaviour still needs a run on the T480.

  1. The notification now ranks processes by their mean over the alert window (cd59aff). Hover and the icon stay instantaneous.
  2. Set-AlwaysShown is tried for 30 ticks at most and matches InitialTooltip -eq 'CPU' (9f4e390).
  3. The shortcut targets %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe (2642a69).
  4. A comment records that the shortcut lands in the elevating account's Startup folder (2642a69).

Smaller points (3070e5d, d9fb2d7): the catch builds the message once and logs each new error once to %TEMP%\cpu-tray.log. The README now covers the English counter names and short-lived processes. The unmeasured 0.2% figure and "holds on a fresh machine" are gone from the README and the PR body.

@landsman
landsman force-pushed the devops/windows-cpu-tray branch from d9fb2d7 to c1b1d42 Compare December 4, 2026 12:58
@landsman
landsman force-pushed the devops/windows-cpu-tray branch from c1b1d42 to 00bea7a Compare December 4, 2026 13:56
@landsman

landsman commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

@claude review it again

@claude

claude Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Claude finished @landsman's task in 47s —— View job


Second review of #390: CPU tray

All four items from my first review are fixed as you described. I read the diff against origin/main and the full cpu-tray.ps1. I couldn't run any of it, because there is no PowerShell here. These findings come from reading, so the T480 run is still the real test.

Checked and fine

  • Notification ranking: Get-SustainedTop divides each name's sum by $samples.Count, so a tick where a process was absent counts as 0. That is the right mean. $byNameHistory is trimmed to $window in step with $history, so the two always cover the same ticks.
  • Set-AlwaysShown: the 30-try cap and InitialTooltip -eq 'CPU' are correct. $tray.Text = 'CPU' is set before Visible, so the first tooltip does match.
  • Shortcut target and quoting: the target is now the fixed Windows PowerShell 5.1 path and the -File "…" quoting is right. $startup and $shortcut don't collide with anything else in apply.ps1.
  • ASCII: both .ps1 files are ASCII-only (grep -P '[^\x00-\x7F]' found nothing).
  • Log: a new error is logged once, and the message is built once.

Worth a look (small)

  1. An exception in Set-AlwaysShown freezes the icon for up to a minute. It runs before Set-TrayIcon (cpu-tray.ps1:213-216). Under $ErrorActionPreference = 'Stop', a Get-ItemProperty or Set-ItemProperty failure on one key (an ACL, or a key deleted mid-scan) goes to the catch. That tick then never updates the icon or the history. The cause is also only logged once, so it looks like a stuck icon. Fix: give the two registry calls in Set-AlwaysShown -ErrorAction SilentlyContinue. Alternatively move the promote block after Set-TrayIcon. Because promoteTries is decremented first, the damage is bounded to those 30 ticks.

  2. "The way Task Manager counts it" is not quite true. The script uses % Processor Time. Task Manager on Windows 10 1709 and later, and on Windows 11, shows % Processor Utility, which is scaled by the current clock frequency. With turbo boost or throttling the two can differ noticeably. The T480 throttles under load, so the icon and Task Manager may disagree by several points. This doesn't change the diagnosis, since the ranking is the point. But the claim is in the script header (cpu-tray.ps1:7-8), the README and the PR body. I'd reword it to "whole-machine busy time, close to Task Manager's figure". Then you aren't surprised when the two differ.

  3. Nit: the README notification bullet (os/windows/README.md, "naming the three busiest over that minute…") breaks its wrapping. The line is long, with a short one after it. Re-wrap it to match the neighbouring bullets.

Still unverified (from your note)

  • The pin taking effect live.
  • The cost of the tray itself.
  • The English counter names on the T480.
  • The alert wording and the process names Windows Update installing and Defender scanning, on a real TiWorker or MsMpEng spike.

Verdict

No blockers. Item 1 is a one-line fix and worth doing. Items 2 and 3 are wording only.
· branch devops/windows-cpu-tray

…l be at full speed

The load alone does not say whether the cores stepped down, which is the half
that costs battery. Hover now reads "CPU 4% 0.9 GHz", and the menu heads with
the speed against the nominal clock.

It is % Processor Performance times the nominal clock out of the model name,
the arithmetic behind Task Manager's Speed. Measured on the T480 in 2 s
samples: 0.9-1.8 GHz with a session running, 2.3-3.1 GHz with every core busy.
A true idle was not measured, so the readme claims no idle figure.

CurrentClockSpeed from WMI only ever returned 1496 or 1696 there, turbo
included. MaxClockSpeed is the BIOS ceiling, 1896 against a nominal 1700, and
would overstate every figure by 11%.

A display language whose counters are not English has no such counter; that is
caught at start and the tray runs without the figure.
…e shortcut was the only answer

The Startup folder is a folder inside the Start menu, so Windows already has
both halves: Start and type "CPU tray" runs it, and Task Manager -> Startup
apps is the on/off for sign-in. The old answer, delete the shortcut, is undone
by the next apply.ps1 run.

Checked that the toggle is keyed by the shortcut's file name under
StartupApproved\StartupFolder and survives apply.ps1 rewriting the .lnk: set
it disabled, rewrote the shortcut, read 03 00 ... back, restored it.
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.

1 participant