|
| 1 | +--- |
| 2 | +title: "CPU Frequency - MSR - core" |
| 3 | +description: "Documentation of CpuFrequencyMsrCoreProvider for the Green Metrics Tool" |
| 4 | +date: 2022-06-01T08:49:15+00:00 |
| 5 | +draft: false |
| 6 | +images: [] |
| 7 | +weight: 120 |
| 8 | +--- |
| 9 | + |
| 10 | +### What it does |
| 11 | + |
| 12 | +This metric provider measures the actual CPU frequency for every core on **Intel CPUs** by reading |
| 13 | +the hardware APERF and MPERF performance counters via the Linux MSR (Model Specific Register) interface. |
| 14 | + |
| 15 | +### Classname |
| 16 | + |
| 17 | +- `CpuFrequencyMsrCoreProvider` |
| 18 | + |
| 19 | +### Metric Name |
| 20 | + |
| 21 | +- `cpu_frequency_msr_core` |
| 22 | + |
| 23 | +### Input Parameters |
| 24 | + |
| 25 | +- args |
| 26 | + - `-i`: interval in milliseconds |
| 27 | + - `-f`: base CPU frequency in GHz (default: 2.4) |
| 28 | + - `-c`: check/test mode — validates that MSR counters are incrementing and then exits |
| 29 | + |
| 30 | +By default the measurement interval is 1000 ms. |
| 31 | + |
| 32 | +```bash |
| 33 | +./metric-provider-binary -i 100 -f 2.4 |
| 34 | +``` |
| 35 | + |
| 36 | +### Output |
| 37 | + |
| 38 | +This metric provider prints to *Stdout* a continuous stream of data. The format of the data is as follows: |
| 39 | + |
| 40 | +`TIMESTAMP READING CORE` |
| 41 | + |
| 42 | +Where: |
| 43 | + |
| 44 | +- `TIMESTAMP`: Unix timestamp, in microseconds |
| 45 | +- `READING`: The current effective frequency of the core in *Hz* |
| 46 | +- `CORE`: The logical CPU index as reported by the kernel |
| 47 | + |
| 48 | +Any errors are printed to Stderr. |
| 49 | + |
| 50 | +### How it works |
| 51 | + |
| 52 | +The provider is a compiled C binary that uses the x86 APERF and MPERF hardware counters to |
| 53 | +compute the effective CPU frequency per core. |
| 54 | + |
| 55 | +- **APERF** (Actual Performance register, MSR `0xE8`) increments at the actual clock rate |
| 56 | +- **MPERF** (Maximum Performance register, MSR `0xE7`) increments at the base (nominal) clock rate |
| 57 | + |
| 58 | +For each sampling interval the provider reads both counter deltas and derives the effective frequency: |
| 59 | + |
| 60 | +```C |
| 61 | +freq_hz = base_ghz * (APERF_delta / MPERF_delta) * 1e9 |
| 62 | +``` |
| 63 | + |
| 64 | +This gives the true average clock speed across the interval, including the effect of turbo boost |
| 65 | +and frequency throttling, rather than the OS governor's *requested* frequency. |
| 66 | + |
| 67 | +Core discovery is performed at startup by enumerating |
| 68 | +`/sys/devices/system/cpu/cpuX/topology/core_id` entries. The MSRs are then accessed via |
| 69 | +`/dev/cpu/<n>/msr`. |
| 70 | + |
| 71 | +Because the binary needs to open `/dev/cpu/*/msr` (which is restricted to root), the compiled |
| 72 | +binary is installed with the setuid-root bit set. |
| 73 | + |
| 74 | +### Caveats |
| 75 | + |
| 76 | +- The `-f` base frequency must match the nominal (non-turbo) base clock of the CPU being measured. |
| 77 | + An incorrect value will scale all reported frequencies proportionally. |
| 78 | +- If you have very many cores you will generate a lot of data which is tricky to interpret, as |
| 79 | + Linux constantly migrates processes between cores and measurements are not directly comparable |
| 80 | + core-to-core. |
| 81 | +- For most users the per-core detail will be noise rather than actionable data. We recommend |
| 82 | + looking at the aggregate or only enabling this provider when debugging frequency-related behaviour. |
| 83 | +- This provider only works on **Intel CPUs**. The APERF/MPERF MSRs are Intel-specific and are not |
| 84 | + available on AMD or ARM processors. |
| 85 | +- It cannot be used on most VMs or in containers where MSR access is blocked. |
| 86 | + |
| 87 | +### Troubleshooting |
| 88 | + |
| 89 | +The provider requires read access to `/dev/cpu/*/msr`. Common failure modes: |
| 90 | + |
| 91 | +- **`open msr: Permission denied`** — the setuid bit is not set on the binary. Re-run `make` from |
| 92 | + the provider directory (requires sudo). |
| 93 | +- **`CPU N no MSR support`** — the `msr` kernel module is not loaded. Run `sudo modprobe msr`. |
| 94 | +- **`MSR test failed (no counter progression)`** — the APERF/MPERF counters are not incrementing, |
| 95 | + which typically indicates a VM or container environment that does not expose hardware counters. |
| 96 | +- On VMs and many cloud instances MSR access is not available. Use a different frequency provider |
| 97 | + in those environments. |
0 commit comments