---
title: cpufreq 与 cpuidle
url: https://doc.liz6.com/linux-kernel/14-power-management/01-cpufreq-and-cpuidle
locale: zh
area: linux-kernel
tags:
- linux-kernel
- 电源管理
date: 2026-06-30
modified: 2026-06-30
description: '覆盖: cpufreq governors → CPU idle states (C-states) → cpuidle governor → P-state 驱动 (intel_pstate/amd-pstate) → schedutil → 能效比 内核版本: 2.6 ~ 6.x'
---

# cpufreq 与 cpuidle

> 覆盖: cpufreq governors → CPU idle states (C-states) → cpuidle governor → P-state 驱动 (intel_pstate/amd-pstate) → schedutil → 能效比
> 内核版本: 2.6 ~ 6.x

## cpufreq: 动态频率调节

### Governors (调速器)

```c
// drivers/cpufreq/
// 每个 governor 是一个独立的策略模块:

// performance: 始终最高频率 (P0) → 峰值性能, 零延迟, 高功耗
// powersave:   始终最低频率 → 极限省电, 低性能
// schedutil:   调度器驱动的频率选择 → 当前默认 (5.x+)
// ondemand:    按需调频 (基于 CPU 利用率采样) → 旧默认, 响应滞后
// conservative: 渐进升降频 (类似 ondemand 但更平滑)
```

### schedutil: 调度器驱动

```c
// kernel/sched/cpufreq_schedutil.c
// 频率由 CFS 的 PELT util 信号直接驱动:

// 每次 scheduler tick:
//   util = cpu_util_cfs(rq)  // 当前 CPU 的 CFS 利用率
//   next_freq = 1.25 * util * max_freq / capacity
//   → 设 cpufreq 到 next_freq

// 优点:
//   - 调度器直接知道 CPU 负载 → 不需要独立采样
//   - 响应延迟远低于 ondemand (在调度路径中, 不在独立 timer 中)
//   - 与 EAS (Energy Aware Scheduling) 集成
```

### 驱动: intel_pstate / amd-pstate

```c
// drivers/cpufreq/intel_pstate.c
// intel_pstate: Intel CPU 的 P-state 管理 (Sandy Bridge+)
//   提供: internal governor (powersave / performance)
//   直接写 MSR (IA32_PERF_CTL) → 不需要 ACPI

// drivers/cpufreq/amd-pstate.c
// amd_pstate: AMD CPU 的 P-state 管理 (Zen2+)
//   三种模式: active (CPPC), guided, passive

// 检查当前:
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_driver
```

---

## cpuidle: CPU 空闲状态管理

### C-states

```
每级更深睡眠 → 更省电 + 更长唤醒延迟:

C0: 活动 (运行中)
C1: HLT/MWAIT → 停止执行, ~1μs 唤醒
C1E: 增强 C1 → 降频 + C1
C2: 时钟停止 → ~3μs
C3: Cache flush → ~20μs
C6: 核心断电 → ~50μs
C7: 更大的断电 (IVB+) → ~100μs

CPU 在这些状态间自动转换 (硬件管理)
内核通过 cpuidle 框架选择何时进哪个 C-state
```

### Cpuidle Governor

```c
// drivers/cpuidle/governors/menu.c
// menu governor (默认):
//   基于历史空闲时长预测 → 选 C-state
//   预测因子: 最近的空闲时长, 重复模式检测, next timer event

// drivers/cpuidle/governors/teo.c
// TEO (Timer Events Oriented):
//   更新的 governor → 基于最近 timer events 的间隔选择
//   比 menu 更准确 (尤其是在 bursty workloads)
```

## 能效调度 (EAS)

```
EAS (Energy Aware Scheduling, ARM):
  调度器在选 CPU 时考虑能效:
    → 优先选已经唤醒的 CPU (避免 wake up from deep C-state)
    → 选剩余容量足够的 CPU (避免升频)
    → 选同一 cluster 的 CPU (共享 cache)

  集成了 schedutil + cpuidle → 调度, 频率, C-state 统一决策
```

## 调试

```bash
# cpufreq 信息
cpupower frequency-info
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# C-state 使用统计
cpupower idle-info
cat /sys/devices/system/cpu/cpu0/cpuidle/state*/{name,usage,time,above,below}

# 功耗统计 (Intel RAPL)
cat /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj

# turbostat: 实时频率和 C-state
turbostat --quiet --show PkgWatt,CorWatt,CPU%c1,CPU%c6 1
```

## 参考

- **源码**: `drivers/cpufreq/`, `drivers/cpuidle/`, `kernel/sched/cpufreq_schedutil.c`
- **内核文档**: `Documentation/admin-guide/pm/`

*关键词: cpufreq, schedutil, intel_pstate, C-state, cpuidle, EAS, turbostat, P-state*
