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: dd2e2a311e

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>
This commit is contained in:
Francesco Poli (wintermute) 2025-04-25 17:07:31 +02:00 committed by Shuah Khan
parent b4432656b3
commit 9c70b779ad
5 changed files with 104 additions and 0 deletions

View File

@ -2,6 +2,7 @@
# Makefile for cpupower # Makefile for cpupower
# #
# Copyright (C) 2005,2006 Dominik Brodowski <linux@dominikbrodowski.net> # Copyright (C) 2005,2006 Dominik Brodowski <linux@dominikbrodowski.net>
# Copyright (C) 2025 Francesco Poli <invernomuto@paranoici.org>
# #
# Based largely on the Makefile for udev by: # Based largely on the Makefile for udev by:
# #
@ -71,6 +72,7 @@ bindir ?= /usr/bin
sbindir ?= /usr/sbin sbindir ?= /usr/sbin
mandir ?= /usr/man mandir ?= /usr/man
libdir ?= /usr/lib libdir ?= /usr/lib
libexecdir ?= /usr/libexec
includedir ?= /usr/include includedir ?= /usr/include
localedir ?= /usr/share/locale localedir ?= /usr/share/locale
docdir ?= /usr/share/doc/packages/cpupower docdir ?= /usr/share/doc/packages/cpupower
@ -83,6 +85,7 @@ CP = cp -fpR
INSTALL = /usr/bin/install -c INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL} INSTALL_PROGRAM = ${INSTALL}
INSTALL_DATA = ${INSTALL} -m 644 INSTALL_DATA = ${INSTALL} -m 644
SETPERM_DATA = chmod 644
#bash completion scripts get sourced and so they should be rw only. #bash completion scripts get sourced and so they should be rw only.
INSTALL_SCRIPT = ${INSTALL} -m 644 INSTALL_SCRIPT = ${INSTALL} -m 644
@ -302,6 +305,14 @@ install-tools: $(OUTPUT)cpupower
$(INSTALL_PROGRAM) $(OUTPUT)cpupower $(DESTDIR)${bindir} $(INSTALL_PROGRAM) $(OUTPUT)cpupower $(DESTDIR)${bindir}
$(INSTALL) -d $(DESTDIR)${bash_completion_dir} $(INSTALL) -d $(DESTDIR)${bash_completion_dir}
$(INSTALL_SCRIPT) cpupower-completion.sh '$(DESTDIR)${bash_completion_dir}/cpupower' $(INSTALL_SCRIPT) cpupower-completion.sh '$(DESTDIR)${bash_completion_dir}/cpupower'
$(INSTALL) -d $(DESTDIR)${confdir}default
$(INSTALL_DATA) cpupower.default '$(DESTDIR)${confdir}default/cpupower'
$(INSTALL) -d $(DESTDIR)${libexecdir}
$(INSTALL_PROGRAM) cpupower.sh '$(DESTDIR)${libexecdir}/cpupower'
$(INSTALL) -d $(DESTDIR)${libdir}/systemd/system
sed 's|___CDIR___|$(DESTDIR)${confdir}|; s|___LDIR___|$(DESTDIR)${libexecdir}|' cpupower.service.in > '$(DESTDIR)${libdir}/systemd/system/cpupower.service'
$(SETPERM_DATA) '$(DESTDIR)${libdir}/systemd/system/cpupower.service'
if test -d /run/systemd/system; then systemctl daemon-reload; fi
install-man: install-man:
$(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1 $(INSTALL_DATA) -D man/cpupower.1 $(DESTDIR)${mandir}/man1/cpupower.1
@ -336,6 +347,9 @@ uninstall:
- rm -f $(DESTDIR)${includedir}/cpufreq.h - rm -f $(DESTDIR)${includedir}/cpufreq.h
- rm -f $(DESTDIR)${includedir}/cpuidle.h - rm -f $(DESTDIR)${includedir}/cpuidle.h
- rm -f $(DESTDIR)${bindir}/utils/cpupower - rm -f $(DESTDIR)${bindir}/utils/cpupower
- rm -f $(DESTDIR)${confdir}default/cpupower
- rm -f $(DESTDIR)${libexecdir}/cpupower
- rm -f $(DESTDIR)${libdir}/systemd/system/cpupower.service
- rm -f $(DESTDIR)${mandir}/man1/cpupower.1 - rm -f $(DESTDIR)${mandir}/man1/cpupower.1
- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1 - rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-set.1
- rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1 - rm -f $(DESTDIR)${mandir}/man1/cpupower-frequency-info.1
@ -346,6 +360,7 @@ uninstall:
- for HLANG in $(LANGUAGES); do \ - for HLANG in $(LANGUAGES); do \
rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \ rm -f $(DESTDIR)${localedir}/$$HLANG/LC_MESSAGES/cpupower.mo; \
done; done;
- if test -d /run/systemd/system; then systemctl daemon-reload; fi
help: help:
@echo 'Building targets:' @echo 'Building targets:'

View File

@ -59,6 +59,10 @@ $ sudo make install
----------------------------------------------------------------------- -----------------------------------------------------------------------
| man pages | /usr/man | | man pages | /usr/man |
----------------------------------------------------------------------- -----------------------------------------------------------------------
| systemd service | /usr/lib |
-----------------------------------------------------------------------
| systemd support script | /usr/libexec |
-----------------------------------------------------------------------
To put it in other words it makes build results available system-wide, To put it in other words it makes build results available system-wide,
enabling any user to simply start using it without any additional steps enabling any user to simply start using it without any additional steps
@ -109,6 +113,10 @@ The files will be installed to the following dirs:
----------------------------------------------------------------------- -----------------------------------------------------------------------
| man pages | ${DESTDIR}/usr/man | | man pages | ${DESTDIR}/usr/man |
----------------------------------------------------------------------- -----------------------------------------------------------------------
| systemd service | ${DESTDIR}/usr/lib |
-----------------------------------------------------------------------
| systemd support script | ${DESTDIR}/usr/libexec |
-----------------------------------------------------------------------
If you look at the table for the default 'make' output dirs you will If you look at the table for the default 'make' output dirs you will
notice that the only difference with the non-default case is the notice that the only difference with the non-default case is the
@ -173,6 +181,17 @@ The issue is that binary cannot find the 'libcpupower' library. So, we
shall point to the lib dir: shall point to the lib dir:
sudo LD_LIBRARY_PATH=lib64/ ./bin/cpupower sudo LD_LIBRARY_PATH=lib64/ ./bin/cpupower
systemd service
---------------
A systemd service is also provided to run the cpupower utility at boot with
settings read from a configuration file. In order to enable this systemd
service, edit '${DESTDIR}/etc/default/cpupower' (uncommenting at least one of
the options, depending on your preferences) and then issue the following
command:
$ sudo systemctl enable --now cpupower.service
THANKS THANKS
------ ------

View File

@ -0,0 +1,28 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2012, Sébastien Luttringer
# Copyright (C) 2024-2025, Francesco Poli <invernomuto@paranoici.org>
# Default file for linux-cpupower
# --- CPU clock frequency ---
# Define CPU governor
# Valid governors: ondemand, performance, powersave, conservative, userspace
#GOVERNOR='ondemand'
# Limit frequency range
# Valid suffixes: Hz, kHz (default), MHz, GHz, THz
#MIN_FREQ="2.25GHz"
#MAX_FREQ="3GHz"
# Specific frequency to be set.
# Requires userspace governor to be available.
# If this option is set, all the previous frequency options are ignored
#FREQ=
# --- CPU policy ---
# Sets a register on supported Intel processore which allows software to convey
# its policy for the relative importance of performance versus energy savings to
# the processor. See man CPUPOWER-SET(1) for additional details
#PERF_BIAS=

View File

@ -0,0 +1,16 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2012-2020, Sébastien Luttringer
# Copyright (C) 2024, Francesco Poli <invernomuto@paranoici.org>
[Unit]
Description=Apply cpupower configuration
ConditionVirtualization=!container
[Service]
Type=oneshot
EnvironmentFile=-___CDIR___default/cpupower
ExecStart=___LDIR___/cpupower
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,26 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2012, Sébastien Luttringer
# Copyright (C) 2024, Francesco Poli <invernomuto@paranoici.org>
ESTATUS=0
# apply CPU clock frequency options
if test -n "$FREQ"
then
cpupower frequency-set -f "$FREQ" > /dev/null || ESTATUS=1
elif test -n "${GOVERNOR}${MIN_FREQ}${MAX_FREQ}"
then
cpupower frequency-set \
${GOVERNOR:+ -g "$GOVERNOR"} \
${MIN_FREQ:+ -d "$MIN_FREQ"} ${MAX_FREQ:+ -u "$MAX_FREQ"} \
> /dev/null || ESTATUS=1
fi
# apply CPU policy options
if test -n "$PERF_BIAS"
then
cpupower set -b "$PERF_BIAS" > /dev/null || ESTATUS=1
fi
exit $ESTATUS