Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpupower: add a systemd service to run cpupower

One of the most typical use cases of the 'cpupower' utility works as
follows: run 'cpupower' at boot with the desired command-line options
and then forget about it.

Add a systemd service (disabled by default) that automates this use
case (for environments where the initialization system is 'systemd'),
by running 'cpupower' at boot with the settings read from a default
configuration file.

The systemd service, the associated support script and the
corresponding default configuration file are derived from what is
provided by the Arch Linux package (under "GPL-2.0-or-later" terms),
modernized and enhanced in various ways (the script has also been
checked with 'shellcheck').

Link: https://gitlab.archlinux.org/archlinux/packaging/packages/linux-tools/-/tree/dd2e2a311e05413d0d87a0346ffce8c7e98d6d2b

Signed-off-by: Francesco Poli (wintermute) <invernomuto@paranoici.org>
Reviewed-by: John B. Wyatt IV <jwyatt@redhat.com>
Reviewed-by: John B. Wyatt IV <sageofredondo@gmail.com>
Tested-by: John B. Wyatt IV <jwyatt@redhat.com>
Tested-by: John B. Wyatt IV <sageofredondo@gmail.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Francesco Poli (wintermute) and committed by
Shuah Khan
9c70b779 b4432656

+104
+15
tools/power/cpupower/Makefile
··· 2 2 # Makefile for cpupower 3 3 # 4 4 # Copyright (C) 2005,2006 Dominik Brodowski <linux@dominikbrodowski.net> 5 + # Copyright (C) 2025 Francesco Poli <invernomuto@paranoici.org> 5 6 # 6 7 # Based largely on the Makefile for udev by: 7 8 # ··· 72 71 sbindir ?= /usr/sbin 73 72 mandir ?= /usr/man 74 73 libdir ?= /usr/lib 74 + libexecdir ?= /usr/libexec 75 75 includedir ?= /usr/include 76 76 localedir ?= /usr/share/locale 77 77 docdir ?= /usr/share/doc/packages/cpupower ··· 85 83 INSTALL = /usr/bin/install -c 86 84 INSTALL_PROGRAM = ${INSTALL} 87 85 INSTALL_DATA = ${INSTALL} -m 644 86 + SETPERM_DATA = chmod 644 88 87 #bash completion scripts get sourced and so they should be rw only. 89 88 INSTALL_SCRIPT = ${INSTALL} -m 644 90 89 ··· 305 302 $(INSTALL_PROGRAM) $(OUTPUT)cpupower $(DESTDIR)${bindir} 306 303 $(INSTALL) -d $(DESTDIR)${bash_completion_dir} 307 304 $(INSTALL_SCRIPT) cpupower-completion.sh '$(DESTDIR)${bash_completion_dir}/cpupower' 305 + $(INSTALL) -d $(DESTDIR)${confdir}default 306 + $(INSTALL_DATA) cpupower.default '$(DESTDIR)${confdir}default/cpupower' 307 + $(INSTALL) -d $(DESTDIR)${libexecdir} 308 + $(INSTALL_PROGRAM) cpupower.sh '$(DESTDIR)${libexecdir}/cpupower' 309 + $(INSTALL) -d $(DESTDIR)${libdir}/systemd/system 310 + sed 's|___CDIR___|$(DESTDIR)${confdir}|; s|___LDIR___|$(DESTDIR)${libexecdir}|' cpupower.service.in > '$(DESTDIR)${libdir}/systemd/system/cpupower.service' 311 + $(SETPERM_DATA) '$(DESTDIR)${libdir}/systemd/system/cpupower.service' 312 + if test -d /run/systemd/system; then systemctl daemon-reload; fi 308 313 309 314 install-man: 310 315 $(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1 ··· 347 336 - rm -f $(DESTDIR)${includedir}/cpufreq.h 348 337 - rm -f $(DESTDIR)${includedir}/cpuidle.h 349 338 - rm -f $(DESTDIR)${bindir}/utils/cpupower 339 + - rm -f $(DESTDIR)${confdir}default/cpupower 340 + - rm -f $(DESTDIR)${libexecdir}/cpupower 341 + - rm -f $(DESTDIR)${libdir}/systemd/system/cpupower.service 350 342 - rm -f $(DESTDIR)${mandir}/man1/cpupower.1 351 343 - rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1 352 344 - rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1 ··· 360 346 - for HLANG in $(LANGUAGES); do \ 361 347 rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \ 362 348 done; 349 + - if test -d /run/systemd/system; then systemctl daemon-reload; fi 363 350 364 351 help: 365 352 @echo 'Building targets:'
+19
tools/power/cpupower/README
··· 59 59 ----------------------------------------------------------------------- 60 60 | man pages | /usr/man | 61 61 ----------------------------------------------------------------------- 62 + | systemd service | /usr/lib | 63 + ----------------------------------------------------------------------- 64 + | systemd support script | /usr/libexec | 65 + ----------------------------------------------------------------------- 62 66 63 67 To put it in other words it makes build results available system-wide, 64 68 enabling any user to simply start using it without any additional steps ··· 112 108 | cpufreq-bench_plot.sh | ${DESTDIR}/usr/bin | 113 109 ----------------------------------------------------------------------- 114 110 | man pages | ${DESTDIR}/usr/man | 111 + ----------------------------------------------------------------------- 112 + | systemd service | ${DESTDIR}/usr/lib | 113 + ----------------------------------------------------------------------- 114 + | systemd support script | ${DESTDIR}/usr/libexec | 115 115 ----------------------------------------------------------------------- 116 116 117 117 If you look at the table for the default 'make' output dirs you will ··· 180 172 The issue is that binary cannot find the 'libcpupower' library. So, we 181 173 shall point to the lib dir: 182 174 sudo LD_LIBRARY_PATH=lib64/ ./bin/cpupower 175 + 176 + systemd service 177 + --------------- 178 + 179 + A systemd service is also provided to run the cpupower utility at boot with 180 + settings read from a configuration file. In order to enable this systemd 181 + service, edit '${DESTDIR}/etc/default/cpupower' (uncommenting at least one of 182 + the options, depending on your preferences) and then issue the following 183 + command: 184 + 185 + $ sudo systemctl enable --now cpupower.service 183 186 184 187 185 188 THANKS
+28
tools/power/cpupower/cpupower.default
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + # Copyright (C) 2012, Sébastien Luttringer 3 + # Copyright (C) 2024-2025, Francesco Poli <invernomuto@paranoici.org> 4 + 5 + # Default file for linux-cpupower 6 + 7 + # --- CPU clock frequency --- 8 + 9 + # Define CPU governor 10 + # Valid governors: ondemand, performance, powersave, conservative, userspace 11 + #GOVERNOR='ondemand' 12 + 13 + # Limit frequency range 14 + # Valid suffixes: Hz, kHz (default), MHz, GHz, THz 15 + #MIN_FREQ="2.25GHz" 16 + #MAX_FREQ="3GHz" 17 + 18 + # Specific frequency to be set. 19 + # Requires userspace governor to be available. 20 + # If this option is set, all the previous frequency options are ignored 21 + #FREQ= 22 + 23 + # --- CPU policy --- 24 + 25 + # Sets a register on supported Intel processore which allows software to convey 26 + # its policy for the relative importance of performance versus energy savings to 27 + # the processor. See man CPUPOWER-SET(1) for additional details 28 + #PERF_BIAS=
+16
tools/power/cpupower/cpupower.service.in
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + # Copyright (C) 2012-2020, Sébastien Luttringer 3 + # Copyright (C) 2024, Francesco Poli <invernomuto@paranoici.org> 4 + 5 + [Unit] 6 + Description=Apply cpupower configuration 7 + ConditionVirtualization=!container 8 + 9 + [Service] 10 + Type=oneshot 11 + EnvironmentFile=-___CDIR___default/cpupower 12 + ExecStart=___LDIR___/cpupower 13 + RemainAfterExit=yes 14 + 15 + [Install] 16 + WantedBy=multi-user.target
+26
tools/power/cpupower/cpupower.sh
··· 1 + #!/bin/sh 2 + # SPDX-License-Identifier: GPL-2.0-or-later 3 + # Copyright (C) 2012, Sébastien Luttringer 4 + # Copyright (C) 2024, Francesco Poli <invernomuto@paranoici.org> 5 + 6 + ESTATUS=0 7 + 8 + # apply CPU clock frequency options 9 + if test -n "$FREQ" 10 + then 11 + cpupower frequency-set -f "$FREQ" > /dev/null || ESTATUS=1 12 + elif test -n "${GOVERNOR}${MIN_FREQ}${MAX_FREQ}" 13 + then 14 + cpupower frequency-set \ 15 + ${GOVERNOR:+ -g "$GOVERNOR"} \ 16 + ${MIN_FREQ:+ -d "$MIN_FREQ"} ${MAX_FREQ:+ -u "$MAX_FREQ"} \ 17 + > /dev/null || ESTATUS=1 18 + fi 19 + 20 + # apply CPU policy options 21 + if test -n "$PERF_BIAS" 22 + then 23 + cpupower set -b "$PERF_BIAS" > /dev/null || ESTATUS=1 24 + fi 25 + 26 + exit $ESTATUS