Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

BUG: macOS CPU monitoring calculates incorrect percentages using delta on instantaneous values #3

@noahgift

Description

@noahgift

Summary

The macOS CPU monitoring implementation incorrectly applies delta-based percentage calculation to instantaneous CPU percentages from top, resulting in incorrect CPU usage readings.

Bug Location

src/monitor/collectors/cpu.rs:166-221 (macOS parse_proc_stat implementation)

Root Cause

On macOS, the code:

  1. Parses instantaneous CPU percentages from top -l 1 (e.g., "45.67% user, 3.21% sys, 51.12% idle")
  2. Truncates float to u64, losing decimal precision (45.67 -> 45)
  3. Multiplies by 100 to "scale like Linux jiffies" (45 -> 4500)
  4. Stores these as CpuStats which are meant for cumulative tick counts
  5. calculate_percentage() then computes deltas between two readings

The problem: Linux's /proc/stat gives cumulative CPU tick counts, so delta calculation works. But macOS top gives instantaneous percentages already - computing deltas between two instantaneous percentage readings is mathematically incorrect.

Example of Bug

First reading:  user=45%, idle=50%  -> stored as CpuStats{user: 4500, idle: 5000}
Second reading: user=30%, idle=65%  -> stored as CpuStats{user: 3000, idle: 6500}

Delta calculation:
  total_delta = (3000+6500) - (4500+5000) = 0  (or negative!)
  Result: 0% or NaN instead of ~30%

Expected Behavior

On macOS, the CPU percentage should directly use the instantaneous percentages from top without delta calculation.

Falsification Test (ttop-demo.md Claim 41)

Per specification Section 10.3, Claim 41: "CPU % within ±2% of top"

This claim is currently falsified - the bug causes readings to deviate significantly more than ±2%.

Fix Approach

For macOS:

  1. Store raw percentage directly from top as the CPU usage value
  2. Skip delta calculation since we already have instantaneous percentages
  3. Maintain precision by using f64 throughout

References

  • ttop-demo.md Section 10.3, Claim 41
  • tests/playbooks/macos_collectors.yaml (accuracy_tests)
  • Popperian falsification checklist item 41

Labels

  • bug
  • macos
  • cpu-monitoring
  • falsification-failed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions