← Back to Pull requests

system, check: warn when a benchmark ran under a cgroup CPU quota #277

Open
manunicholasjacob opened this pull request with 245 lines diff across 7 files
View on GitHub
manunicholasjacob manunicholasjacob commented on Nov 10, 2026 16:37 UTC
Author

Problem

pyperf system show runs ten operations and pyperf check warns on four statistical conditions, but nothing in either looks at whether the benchmark was throttled while it ran.

A cgroup CPU quota (docker run --cpus, a Kubernetes CPU limit, systemd CPUQuota=) throttles the benchmark, and throttling does not look like noise. A quota that binds for the whole run slows every value by about the same factor, so the mean shifts but the relative spread does not, and the result comes back looking statistically stable while being wrong. The advice check offers on an unstable result (more runs and values, pyperf system tune) does not help, because the machine was not jittery, it was capped. This is the common case on CI runners and in containers. Follows up #274 and reuses the cpu_quota detection from #265.

What this does

  • system show: a new "CPU quota" operation, next to "Power supply", that reports a binding quota (via get_cgroup_cpu_quota()) and, from cgroup v2 cpu.stat, whether the benchmark cgroup was throttled. It reports and advises only, the same shape as the power supply check, because nothing pyperf can tune fixes an externally imposed quota.

  • check: warns when a saved result carries cpu_quota metadata, so a stored result is judged after the fact instead of reading as stable.

  • _collect_metadata.py: adds get_cgroup_cpu_throttling() beside get_cgroup_cpu_quota(). It walks the same cgroup hierarchy and reads nr_throttled / throttled_usec (cgroup v2) or throttled_time (cgroup v1), taking the largest counters across the hierarchy because the throttling is recorded where the limit binds while the root stays at zero. It returns None when the counters cannot be read, so "could not be checked" stays distinct from zero.
  • Scope

    This is the quota-detection piece. The two design questions in #274 are left as follow-ups: capturing the per-run cpu.stat delta around the benchmark (and whether to store throttle counters in metadata), and thermal throttling (core_throttle_count on x86, vcgencmd get_throttled on the Pi), where "could not be checked" has to stay distinct from zero.

    Tests

  • test_metadata.py: cgroup v2 throttled, zero-is-not-None, cgroup v1 nanosecond conversion, and no-cgroup returning None.

  • test_cli.py: check warns on cpu_quota metadata and does not warn without it.

  • test_system.py: the CPU quota operation is silent with no quota, reports a set quota, and reports throttling.
  • Checked on a Raspberry Pi 5

    Under systemd-run --user --scope -p CPUQuota=25%, released pyperf reports "The benchmark seems to be stable" on a run that was throttled for hundreds of milliseconds, and system show says nothing about the quota. With this change check warns that the benchmark ran under a CPU quota, and system show reports the quota and the throttling.

    Diff: 245 lines (+245 / -0) across 7 files Reviewer: vstinner Review duration: 9079m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.45) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 245 lines diff across 7 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 1 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.329
    Real PR Conversation Timeline
    2 items
    vstinner vstinner commented on code on Nov 10, 2026 22:19 UTC
    Code Review
    pyperf/_collect_metadata.py:1
    @@ -422,6 +422,79 @@ def collect_cpu_quota(metadata):
             metadata['cpu_quota'] = '%s CPUs' % format(quota, 'g')
     
     
    +def _read_cgroup_cpu_stat(path):
    +    # Parse a cgroup "cpu.stat" file ("key value" lines) into a dict of ints.
    +    # Return None if the file cannot be read.
    +    stat = {}
    +    found = False
    +    try:
    +        with open_text(path) as fp:
    +            for line in fp:
    +                fields = line.split()
    +                if len(fields) == 2:
    +                    try:
    +                        stat[fields[0]] = int(fields[1])
    +                        found = True
    +                    except ValueError:
    +                        continue
    +    except OSError:
    +        return None
    +    return stat if found else None
    +
    +
    +def get_cgroup_cpu_throttling():

    Please don't duplicate code, there is already get_cgroup_cpu_quota(). Why not reusing it?

    Reviewers
    Assignees
    manunicholasjacob manunicholasjacob
    Labels
    risk: low confidence: 89.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 44.7% (MEDIUM) · Higher = larger blast radius
    44.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.1% · Lower = rushed or shallow review
    89.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.9% · Safe to merge
    4.9%
    Repository