xref: /xnu-10002.81.5/doc/cpu_counters.md (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions# CPU Counters
2*5e3eaea3SApple OSS Distributions
3*5e3eaea3SApple OSS DistributionsCPU performance counters are hardware registers that count events of interest to efficient CPU execution.
4*5e3eaea3SApple OSS DistributionsCounters that measure events closely correlated with each CPU's execution pipeline are managed by the Core Performance Monitoring Unit (CPMU).
5*5e3eaea3SApple OSS DistributionsThe CPMU contains both fixed instructions and cycles counters, as well as configurable counters that can be programmed to count any of several hundred possible events.
6*5e3eaea3SApple OSS DistributionsIn addition to the CPMU, the Last Level Cache (LLC) hosts the Uncore Performance Monitoring Unit (UPMU), which measures effects that aren't necessarily correlated to a single CPU.
7*5e3eaea3SApple OSS DistributionsAll counters in the UPMU are configurable.
8*5e3eaea3SApple OSS Distributions
9*5e3eaea3SApple OSS DistributionsCounters are typically used in one of two ways.
10*5e3eaea3SApple OSS DistributionsIn "counting" mode, their counts are periodically queried and tallied up for a duration of interest.
11*5e3eaea3SApple OSS DistributionsIn "sampling" mode, the counters are programmed to generate a Performance Monitor Interrupt (PMI) periodically, during which the currently running code can be sampled, like a time profiler.
12*5e3eaea3SApple OSS Distributions
13*5e3eaea3SApple OSS Distributions## Subsystems
14*5e3eaea3SApple OSS Distributions
15*5e3eaea3SApple OSS DistributionsThere are several subsystems that provide access to CPU counter hardware:
16*5e3eaea3SApple OSS Distributions
17*5e3eaea3SApple OSS Distributions- kpc: The Kernel Performance Counter system is the oldest subsystem and still manages the configurable CPMU counters.
18*5e3eaea3SApple OSS DistributionsIt can use PMIs from these counters to trigger kperf samples and counter values can be recorded in kperf samples.
19*5e3eaea3SApple OSS Distributions
20*5e3eaea3SApple OSS Distributions- Monotonic: The Monotonic system provides access to the fixed CPMU counters with limited support for PMIs.
21*5e3eaea3SApple OSS DistributionsAdditionally, the UPMU is entirely provided by a Monotonic dev node interface.
22*5e3eaea3SApple OSS Distributions
23*5e3eaea3SApple OSS Distributions- cpc: The CPU Performance Counter subsystem provides a policy layer on top of kpc and Monotonic to prevent malicious use of the hardware.
24*5e3eaea3SApple OSS Distributions
25*5e3eaea3SApple OSS DistributionsEventually, cpc will subsume kpc's and Monotonic's roles in the system.
26*5e3eaea3SApple OSS Distributions
27*5e3eaea3SApple OSS Distributions## Integrations
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions- The Recount subsystem makes extensive use of the fixed CPMU counters to attribute CPU resources back to threads and processes.
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions- Microstackshot telemetry is sampled periodically using the CPMU's cycle PMI trigger.
32*5e3eaea3SApple OSS Distributions
33*5e3eaea3SApple OSS Distributions- Stackshot includes cycles and instructions for each thread container in its kcdata.
34*5e3eaea3SApple OSS Distributions
35*5e3eaea3SApple OSS Distributions- The kperf profiling system can trigger samples of thread states and call stacks using CPMU PMIs, allowing it to sample thread states and call stacks.
36*5e3eaea3SApple OSS DistributionsAnd CPU counter values can be sampled by kperf on other triggers, like timers or kdebug events.
37*5e3eaea3SApple OSS Distributions
38*5e3eaea3SApple OSS Distributions## See Also
39*5e3eaea3SApple OSS Distributions
40*5e3eaea3SApple OSS Distributions- `doc/recount.md`
41