xref: /xnu-10063.141.1/doc/observability/cpu_counters.md (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions# CPU Counters
2*d8b80295SApple OSS Distributions
3*d8b80295SApple OSS DistributionsThe xnu subsystems that manage CPU performance counters.
4*d8b80295SApple OSS Distributions
5*d8b80295SApple OSS Distributions## Overview
6*d8b80295SApple OSS Distributions
7*d8b80295SApple OSS DistributionsCPU performance counters are hardware registers that count events of interest to efficient CPU execution.
8*d8b80295SApple OSS DistributionsCounters that measure events closely correlated with each CPU's execution pipeline are managed by the Core Performance Monitoring Unit (CPMU).
9*d8b80295SApple 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.
10*d8b80295SApple 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.
11*d8b80295SApple OSS DistributionsAll counters in the UPMU are configurable.
12*d8b80295SApple OSS Distributions
13*d8b80295SApple OSS DistributionsCounters are typically used in one of two ways:
14*d8b80295SApple OSS Distributions
15*d8b80295SApple OSS Distributions1. In "counting" mode, their counts are periodically queried and tallied up for a duration of interest.
16*d8b80295SApple OSS Distributions2. In "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.
17*d8b80295SApple OSS Distributions
18*d8b80295SApple OSS Distributions## Subsystems
19*d8b80295SApple OSS Distributions
20*d8b80295SApple OSS DistributionsThere are several subsystems that provide access to CPU counter hardware:
21*d8b80295SApple OSS Distributions
22*d8b80295SApple OSS Distributions- kpc: The Kernel Performance Counter system is the oldest subsystem and still manages the configurable CPMU counters.
23*d8b80295SApple OSS DistributionsIt can use PMIs from these counters to trigger kperf samples and counter values can be recorded in kperf samples.
24*d8b80295SApple OSS Distributions
25*d8b80295SApple OSS Distributions- Monotonic: The Monotonic system provides access to the fixed CPMU counters with limited support for PMIs.
26*d8b80295SApple OSS DistributionsAdditionally, the UPMU is entirely provided by a Monotonic dev node interface.
27*d8b80295SApple OSS Distributions
28*d8b80295SApple 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.
29*d8b80295SApple OSS Distributions
30*d8b80295SApple OSS DistributionsEventually, cpc will subsume kpc's and Monotonic's roles in the system.
31*d8b80295SApple OSS Distributions
32*d8b80295SApple OSS Distributions## Integrations
33*d8b80295SApple OSS Distributions
34*d8b80295SApple OSS Distributions- The Recount subsystem makes extensive use of the fixed CPMU counters to attribute CPU resources back to threads and processes.
35*d8b80295SApple OSS Distributions
36*d8b80295SApple OSS Distributions- Microstackshot telemetry is sampled periodically using the CPMU's cycle PMI trigger.
37*d8b80295SApple OSS Distributions
38*d8b80295SApple OSS Distributions- Stackshot includes cycles and instructions for each thread container in its kcdata.
39*d8b80295SApple OSS Distributions
40*d8b80295SApple 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.
41*d8b80295SApple OSS DistributionsAnd CPU counter values can be sampled by kperf on other triggers, like timers or kdebug events.
42*d8b80295SApple OSS Distributions
43*d8b80295SApple OSS Distributions## See Also
44*d8b80295SApple OSS Distributions
45*d8b80295SApple OSS Distributions- <doc:recount>
46