Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuned ppd adjustments #646

Merged
merged 7 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions tuned-ppd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import os
import dbus
import signal
import argparse
import logging
from dbus.mainloop.glib import DBusGMainLoop
from tuned import exports
from tuned import exports, logs
from tuned.ppd import controller
import tuned.consts as consts

Expand All @@ -16,6 +18,25 @@ def handler_wrapper(_signal_number, _frame):
signal.signal(signal_number, handler_wrapper)

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PPD compatibility daemon.")
parser.add_argument("--debug", "-D", action="store_true", help="log debugging messages")
parser.add_argument(
"--log",
"-l",
nargs="?",
const=consts.PPD_LOG_FILE,
help="log to a file, default is " + consts.PPD_LOG_FILE,
)
args = parser.parse_args()

log = logs.get()

if args.debug:
log.setLevel(logging.DEBUG)

if args.log:
log.switch_to_file(args.log)

if os.geteuid() != 0:
print("Superuser permissions are required to run the daemon.", file=sys.stderr)
sys.exit(1)
Expand All @@ -33,7 +54,7 @@ def handler_wrapper(_signal_number, _frame):

handle_signal(signal.SIGINT, controller.terminate)
handle_signal(signal.SIGTERM, controller.terminate)
handle_signal(signal.SIGHUP, controller.load_config)
handle_signal(signal.SIGHUP, controller.initialize)

dbus_exporter = exports.dbus_with_properties.DBusExporterWithProperties(
consts.PPD_DBUS_BUS, consts.PPD_DBUS_INTERFACE, consts.PPD_DBUS_OBJECT, consts.PPD_NAMESPACE
Expand Down
30 changes: 30 additions & 0 deletions tuned.spec
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ for f in %{_sysconfdir}/tuned/*; do
done
%endif


%post ppd
%systemd_post tuned-ppd.service


%preun
%systemd_preun tuned.service
if [ "$1" == 0 ]; then
Expand All @@ -348,6 +353,10 @@ if [ "$1" == 0 ]; then
fi


%preun ppd
%systemd_preun tuned-ppd.service


%postun
%systemd_postun_with_restart tuned.service

Expand Down Expand Up @@ -387,12 +396,24 @@ if [ "$1" == 0 ]; then
fi


%postun ppd
%systemd_postun_with_restart tuned-ppd.service


%triggerun -- tuned < 2.0-0
# remove ktune from old tuned, now part of tuned
/usr/sbin/service ktune stop &>/dev/null || :
/usr/sbin/chkconfig --del ktune &>/dev/null || :


%triggerun ppd -- power-profiles-daemon
# if swapping power-profiles-daemon for tuned-ppd, check whether it is active
if systemctl is-active --quiet power-profiles-daemon; then
mkdir -p %{_localstatedir}/lib/rpm-state/tuned
touch %{_localstatedir}/lib/rpm-state/tuned/ppd-active
fi


%posttrans
# conditional support for grub2, grub2 is not available on all architectures
# and tuned is noarch package, thus the following hack is needed
Expand All @@ -403,6 +424,15 @@ if [ -d %{_sysconfdir}/grub.d ]; then
fi


%posttrans ppd
# if power-profiles-daemon was active before installing tuned-ppd,
# start tuned-ppd right away
if [ -f %{_localstatedir}/lib/rpm-state/tuned/ppd-active ]; then
systemctl start tuned-ppd
rm -rf %{_localstatedir}/lib/rpm-state/tuned
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will probably trigger rpmlint warning, thus it's better to do rm -f and then rmdir, but this is minor problem that can be ignored/waived or fixed later.

fi


%files
%exclude %{docdir}/README.utils
%exclude %{docdir}/README.scomes
Expand Down
1 change: 1 addition & 0 deletions tuned/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
LOG_FILE_COUNT = 2
LOG_FILE_MAXBYTES = 100*1000
LOG_FILE = "/var/log/tuned/tuned.log"
PPD_LOG_FILE = "/var/log/tuned/tuned-ppd.log"
PID_FILE = "/run/tuned/tuned.pid"
SYSTEM_RELEASE_FILE = "/etc/system-release-cpe"
# prefix for functions plugins
Expand Down
29 changes: 15 additions & 14 deletions tuned/ppd/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DRIVER = "tuned"
NO_TURBO_PATH = "/sys/devices/system/cpu/intel_pstate/no_turbo"
LAP_MODE_PATH = "/sys/bus/platform/devices/thinkpad_acpi/dytc_lapmode"
UNKNOWN_PROFILE = "unknown"

UPOWER_DBUS_NAME = "org.freedesktop.UPower"
UPOWER_DBUS_PATH = "/org/freedesktop/UPower"
Expand Down Expand Up @@ -100,18 +101,15 @@ def __init__(self, bus, tuned_interface):
super(Controller, self).__init__()
self._bus = bus
self._tuned_interface = tuned_interface
self._profile_holds = ProfileHoldManager(self)
self._performance_degraded = PerformanceDegraded.NONE
self._cmd = commands()
self._terminate = threading.Event()
self._on_battery = False
self.load_config()
self.initialize()

def upower_changed(self, interface, changed, invalidated):
properties = dbus.Interface(self.proxy, dbus.PROPERTIES_IFACE)
self._on_battery = bool(properties.Get(UPOWER_DBUS_INTERFACE, "OnBattery"))
log.info("Battery status: " + ("DC (battery)" if self._on_battery else "AC (charging)"))
tuned_profile = self._config.ppd_to_tuned_battery[self._base_profile] if self._on_battery else self._config.ppd_to_tuned[self._base_profile]
log.info("Switching to profile '%s' due to battery %s" % (tuned_profile, self._on_battery))
self._tuned_interface.switch_profile(tuned_profile)

def setup_battery_signaling(self):
Expand All @@ -134,6 +132,17 @@ def _check_performance_degraded(self):
self._performance_degraded = performance_degraded
exports.property_changed("PerformanceDegraded", performance_degraded)

def initialize(self):
self._profile_holds = ProfileHoldManager(self)
self._performance_degraded = PerformanceDegraded.NONE
self._config = PPDConfig(PPD_CONFIG_FILE)
active_profile = self.active_profile()
self._base_profile = active_profile if active_profile != UNKNOWN_PROFILE else self._config.default_profile
self.switch_profile(self._base_profile)
self._on_battery = False
if self._config.battery_detection:
self.setup_battery_signaling()

def run(self):
exports.start()
while not self._cmd.wait(self._terminate, 1):
Expand All @@ -151,14 +160,6 @@ def base_profile(self):
def terminate(self):
self._terminate.set()

def load_config(self):
self._config = PPDConfig(PPD_CONFIG_FILE)
log.debug("Setting base profile to %s" % self._config.default_profile)
self._base_profile = self._config.default_profile
self.switch_profile(self._config.default_profile)
if self._config.battery_detection:
self.setup_battery_signaling()

def switch_profile(self, profile):
if self.active_profile() == profile:
return
Expand All @@ -169,7 +170,7 @@ def switch_profile(self, profile):

def active_profile(self):
tuned_profile = self._tuned_interface.active_profile()
return self._config.tuned_to_ppd.get(tuned_profile, "unknown")
return self._config.tuned_to_ppd.get(tuned_profile, UNKNOWN_PROFILE)

@exports.export("sss", "u")
def HoldProfile(self, profile, reason, app_id, caller):
Expand Down
3 changes: 1 addition & 2 deletions tuned/ppd/tuned-ppd.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ Before=multi-user.target display-manager.target

[Service]
Type=dbus
PIDFile=/run/tuned/tuned-ppd.pid
BusName=net.hadess.PowerProfiles
ExecStart=/usr/sbin/tuned-ppd
ExecStart=/usr/sbin/tuned-ppd -l

[Install]
WantedBy=graphical.target
Loading