Skip to content

About

Intel RAPL package power limits and power measurement for FreeBSD

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

intel_rapl

FreeBSD MidnightBSD Licence Version

Read and set the Intel CPU package power envelope on FreeBSD.

Why

FreeBSD can change CPU frequency through cpufreq and powerd, but it has no interface to the processor's power budget. On a thermally healthy machine that does not matter. On a laptop whose heatsink is clogged or whose thermal paste has dried out, it matters a great deal: frequency capping limits the top of the range whether or not there is headroom, while RAPL lets the processor manage its own frequency inside a power envelope you choose.

It is also the only way on FreeBSD to answer a simple question — how many watts is this CPU actually drawing right now — which turns out to be the fastest way to tell a cooling problem from a configuration one.

What it measures, on real hardware

Dell XPS 13 9343, Core i7-5600U (Broadwell-U), 15 W TDP, FreeBSD 15.1-RELEASE-p3, with a heatsink overdue for cleaning:

idle              3.5 - 6.4 W      52 C
full load        17.9 - 20.8 W     97-100 C, throttled to 500 MHz
after stopping         2.2 W

Sustained draw of 18-21 W against a 15 W design point, which is what a degraded thermal path looks like from the software side. Without this driver the same machine only reports "hot and slow".

Tested hardware

Every figure in this README was read from this machine, not estimated:

Model Dell XPS 13 9343
BIOS A20
CPU Intel Core i7-5600U (Broadwell-U)
Package TDP 15 W, from MSR_PKG_POWER_INFO
System FreeBSD 15.1-RELEASE-p3, amd64
Limit register locked by firmware

Reported by users

Read from other people's machines and passed on as reported:

CPU Intel Core Ultra 5 245K (desktop)
Package TDP 125 W, from MSR_PKG_POWER_INFO
System MidnightBSD 4.0.9, amd64
Limit register not locked
Result PL1, PL2 and package power read correctly (#1)

MidnightBSD carries the module in mports. On this processor the PL2 window is shorter than a second, so pl2_window_sec reads 0 — use pl2_window_ms for it. The report covers reading only, not writing a limit.

Reports from other hardware are welcome, particularly from machines where the limit register is not locked — the write path has necessarily been tested only against a refusal here.

Scope

Package domain only, and within it only what is needed to cap sustained power and observe the result. DRAM, PP0 and PP1 domains, per-core energy accounting and the PCI-mapped DPTF interface are deliberately left out: none of them help keep a laptop from cooking itself, and each adds hardware variation to test.

The driver is a plain kernel module rather than a newbus device. RAPL is a CPU feature reached through MSRs, so attaching to the Processor Thermal Subsystem PCI function would add a dependency without adding capability.

Requirements

  • FreeBSD 13 or later, amd64; MidnightBSD 4.x is reported to work
  • Intel CPU, Sandy Bridge or newer
  • kernel sources in /usr/src to build the module

Build and load

make build
sudo make install      # into /boot/modules
sudo make load
make status

Loading at boot

Add to /boot/loader.conf:

intel_rapl_load="YES"

/etc/rc.conf works too, and is the usual home for a module with nothing to do early in boot:

kld_list="intel_rapl"

kld_list holds every module loaded that way, so append to it rather than replacing whatever is already there.

Prefer loader.conf if you want any hw.intel_rapl setting to survive a reboot through /etc/sysctl.conf. rcorder puts /etc/rc.d/sysctl first and /etc/rc.d/kld twenty-five places later:

$ rcorder /etc/rc.d/* | grep -nE '/(sysctl|kld)$'
1:/etc/rc.d/sysctl
26:/etc/rc.d/kld

A limit written to sysctl.conf is therefore applied while a module loaded from kld_list does not yet exist, and fails with unknown oid. The loader has the module in memory before any startup script runs.

Check that it took:

$ kldstat | grep intel_rapl
$ dmesg | grep intel_rapl
intel_rapl: package TDP 15 W, accepting 5-30 W, register locked by firmware

register locked by firmware means MSR_PKG_POWER_LIMIT has its lock bit set and the limits are read-only until the next power cycle, so there is nothing to persist beyond the module itself. hw.intel_rapl.locked reports the same.

Use

# what the hardware reports
sysctl hw.intel_rapl

hw.intel_rapl.tdp_watts: 15
hw.intel_rapl.hw_max_watts: 0
hw.intel_rapl.locked: 1
hw.intel_rapl.pl1_watts: 15
hw.intel_rapl.pl2_watts: 25
hw.intel_rapl.power_mw: 18761
sysctl meaning
tdp_watts package thermal design power, read-only
hw_max_watts highest limit the hardware advertises; 0 when firmware leaves the field empty
locked firmware sealed the limit register — writes will fail
pl1_watts sustained power limit, read/write
pl2_watts short-term burst limit, read/write
pl1_window_sec interval the sustained limit is averaged over, read/write
pl2_window_sec interval the burst limit is averaged over, read/write
pl1_window_ms the same interval as pl1_window_sec, in milliseconds
pl2_window_ms the same interval as pl2_window_sec, in milliseconds
min_allowed_watts lowest limit this driver will accept, read/write
max_allowed_watts highest it will accept; 0 disables the check
power_mw actual package power since the previous read

The averaging window matters as much as the limit

pl1_watts alone does not determine temperature - the same figure averaged over one second and over thirty behaves very differently. The XPS ships with a 28 second window:

sysctl hw.intel_rapl.pl1_window_sec
hw.intel_rapl.pl1_window_sec: 28

A long window lets the package run well above its limit for tens of seconds before anything intervenes, which is exactly the burst behaviour a laptop wants and exactly the wrong thing when the heatsink cannot keep up. Shortening it makes the limit bite sooner.

Each window is exposed twice, in seconds and in milliseconds, and both names read and write the same register field. Seconds suit PL1. A burst window is usually a few milliseconds, which pl2_window_sec can only show as 0 and can only set to a second or more, so use pl2_window_ms there:

sysctl hw.intel_rapl.pl2_window_ms
hw.intel_rapl.pl2_window_ms: 2

The hardware stores the window as a coarse mantissa and exponent, so a write lands on the nearest representable value and reads back as that. Below about 4 ms neighbouring values share a millisecond figure.

Bounds

min_allowed_watts and max_allowed_watts are the driver's own guard rails, not the hardware's. They are seeded from MSR_PKG_POWER_INFO where it reports a range and fall back to 5 W and twice TDP where it does not - which is the common case: on the XPS only the TDP field is populated, so trusting the hardware alone would leave no ceiling at all.

Both are writable, so an operator who knows their hardware can widen them.

Lowering the sustained limit:

sysctl hw.intel_rapl.pl1_watts=10

power_mw is a rate, not a total: it covers the interval since the previous read of that same sysctl. Read it twice a few seconds apart to get a figure that means anything. Reads closer together than 200 ms return the previous result rather than dividing by a near-zero interval.

When writes fail

sysctl: hw.intel_rapl.pl1_watts=10: Operation not permitted

Many vendors lock MSR_PKG_POWER_LIMIT in firmware, after which the register is read-only until the next reset. The XPS above does exactly this, so on that machine the driver can measure but not intervene.

Writes are always verified by reading the register back, because a locked register accepts a write and silently discards it. Reporting EPERM is more useful than reporting a success that did not happen.

There is no way around the lock from the OS. If the firmware offers a configurable TDP setting, that is where to change it.

Safety

  • limits below 5 W are refused: a package starved that far looks broken rather than slow, and undoing it needs a working shell
  • limits above what the hardware advertises are refused
  • the limit register is updated read-modify-write, so the time window, enable, clamp and lock bits are preserved

Licence

BSD-2-Clause. See LICENSE.

About

Intel RAPL package power limits and power measurement for FreeBSD

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages