1# CPU frequency and idle states 2 3This data source is available on Linux and Android (Since P). 4It records changes in the CPU power management scheme through the 5Linux kernel ftrace infrastructure. 6It involves three aspects: 7 8#### Frequency scaling 9 10Records changes in the frequency of a CPU. An event is emitted every time the 11scaling governor scales the CPU frequency up or down. 12 13On most Android devices the frequency scaling is per-cluster (group of 14big/little cores) so it's not unusual to see groups of four CPUs changing 15frequency at the same time. 16 17#### idle states 18 19When no threads are eligible to be executed (e.g. they are all in sleep states) 20the kernel sets the CPU into an idle state, turning off some of the circuitry 21to reduce idle power usage. Most modern CPUs have more than one idle state: 22deeper idle states use less power but also require more time to resume from. 23 24Note that idle transitions are relatively fast and cheap, a CPU can enter and 25leave idle states hundreds of times in a second. 26Idle-ness must not be confused with full device suspend, which is a stronger and 27more invasive power saving state (See below). CPUs can be idle even when the 28screen is on and the device looks operational. 29 30The details about how many idle states are available and their semantic is 31highly CPU/SoC specific. At the trace level, the idle state 0 means not-idle, 32values greater than 0 represent increasingly deeper power saving states 33(e.g., single core idle -> full package idle). 34 35Note that most Android devices won't enter idle states as long as the USB 36cable is plugged in (the USB driver stack holds wakelocks). It is not unusual 37to see only one idle state in traces collected through USB. 38 39On most SoCs the frequency has little value when the CPU is idle, as the CPU is 40typically clock-gated in idle states. In those cases the frequency in the trace 41happens to be the last frequency the CPU was running at before becoming idle. 42 43Known issues: 44 45* The event is emitted only when the frequency changes. This might 46 not happen for long periods of times. In short traces 47 it's possible that some CPU might not report any event, showing a gap on the 48 left-hand side of the trace, or none at all. Perfetto doesn't currently record 49 the initial cpu frequency when the trace is started. 50 51* Currently the UI doesn't render the cpufreq track if idle states (see below) 52 are not captured. This is a UI-only bug, data is recorded and query-able 53 through trace processor even if not displayed. 54 55### UI 56 57In the UI, CPU frequency and idle-ness are shown on the same track. The height 58of the track represents the frequency, the coloring represents the idle 59state (colored: not-idle, gray: idle). Hovering or clicking a point in the 60track will reveal both the frequency and the idle state: 61 62![](/docs/images/cpu-frequency.png "CPU frequency and idle states in the UI") 63 64### SQL 65 66At the SQL level, both frequency and idle states are modeled as counters, 67Note that the cpuidle value 0xffffffff (4294967295) means _back to not-idle_. 68 69```sql 70select ts, t.name, cpu, value from counter as c 71left join cpu_counter_track as t on c.track_id = t.id 72where t.name = 'cpuidle' or t.name = 'cpufreq' 73``` 74 75ts | name | cpu | value 76---|------|------|------ 77261187013242350 | cpuidle | 1 | 0 78261187013246204 | cpuidle | 1 | 4294967295 79261187013317818 | cpuidle | 1 | 0 80261187013333027 | cpuidle | 0 | 0 81261187013338287 | cpufreq | 0 | 1036800 82261187013357922 | cpufreq | 1 | 1036800 83261187013410735 | cpuidle | 1 | 4294967295 84261187013451152 | cpuidle | 0 | 4294967295 85261187013665683 | cpuidle | 1 | 0 86261187013845058 | cpufreq | 0 | 1900800 87 88### TraceConfig 89 90```protobuf 91data_sources: { 92 config { 93 name: "linux.ftrace" 94 ftrace_config { 95 ftrace_events: "power/cpu_frequency" 96 ftrace_events: "power/cpu_idle" 97 ftrace_events: "power/suspend_resume" 98 } 99 } 100} 101``` 102 103### Full-device suspend 104 105Full device suspend happens when a laptop is put in "sleep" mode (e.g. by 106closing the lid) or when a smartphone display is turned off for enough time. 107 108When the device is suspended, most of the hardware units are turned off entering 109the highest power-saving state possible (other than full shutdown). 110 111Note that most Android devices don't suspend immediately after dimming the 112display but tend to do so if the display is forced off through the power button. 113The details are highly device/manufacturer/kernel specific. 114 115Known issues: 116 117* The UI doesn't display clearly the suspended state. When an Android device 118 suspends it looks like as if all CPUs are running the kmigration thread and 119 one CPU is running the power HAL. 120