Skip to content

Commit

Permalink
tuned-ppd: Use inotify to check for performance degradation
Browse files Browse the repository at this point in the history
Instead of actively polling for changes in the relevant files,
set up inotify handlers which are invoked when the files change.
  • Loading branch information
zacikpa committed Dec 3, 2024
1 parent bbb7062 commit 63961a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions tuned.spec
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ BuildRequires: %{_py}-mock
BuildRequires: %{_py}-pyudev
Requires: %{_py}-pyudev
Requires: %{_py}-linux-procfs, %{_py}-perf
Requires: %{_py}-inotify
%if %{without python3}
Requires: %{_py}-schedutils
%endif
Expand Down
44 changes: 42 additions & 2 deletions tuned/ppd/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tuned.consts import PPD_CONFIG_FILE, PPD_BASE_PROFILE_FILE, PPD_API_COMPATIBILITY
from tuned.ppd.config import PPDConfig, PPD_PERFORMANCE, PPD_POWER_SAVER
from enum import StrEnum
import pyinotify
import threading
import dbus
import os
Expand All @@ -27,6 +28,21 @@ class PerformanceDegraded(StrEnum):
HIGH_OPERATING_TEMPERATURE = "high-operating-temperature"


class PerformanceDegradedEventHandler(pyinotify.ProcessEvent):
"""
Event handler for checking performance degradation.
"""
def __init__(self, controller, path):
super(PerformanceDegradedEventHandler, self).__init__()
self._controller = controller
self._path = path

def process_IN_MODIFY(self, event):
if event.pathname != self._path:
return
self._controller.check_performance_degraded()


class ProfileHold(object):
"""
Class holding information about a single profile hold,
Expand Down Expand Up @@ -149,6 +165,11 @@ def __init__(self, bus, tuned_interface):
self._terminate = threading.Event()
self._battery_handler = None
self._on_battery = False
self._watch_manager = pyinotify.WatchManager()
self._notifier = pyinotify.ThreadedNotifier(self._watch_manager)
self._inotify_watches = {}
self._no_turbo_supported = os.path.isfile(NO_TURBO_PATH)
self._lap_mode_supported = os.path.isfile(LAP_MODE_PATH)
self._tuned_interface.connect_to_signal("profile_changed", self._tuned_profile_changed)
self.initialize()

Expand Down Expand Up @@ -198,7 +219,21 @@ def _setup_battery_signaling(self):
except dbus.exceptions.DBusException as error:
log.debug(error)

def _check_performance_degraded(self):
def _setup_inotify(self):
"""
Sets up inotify file watches.
"""
self._watch_manager.rm_watch(list(self._inotify_watches.values()))
if self._no_turbo_supported:
self._inotify_watches |= self._watch_manager.add_watch(path=os.path.dirname(NO_TURBO_PATH),
mask=pyinotify.IN_MODIFY,
proc_fun=PerformanceDegradedEventHandler(NO_TURBO_PATH, self))
if self._lap_mode_supported:
self._inotify_watches |= self._watch_manager.add_watch(path=os.path.dirname(LAP_MODE_PATH),
mask=pyinotify.IN_MODIFY,
proc_fun=PerformanceDegradedEventHandler(LAP_MODE_PATH, self))

def check_performance_degraded(self):
"""
Checks the current performance degradation status and sends a signal if it changed.
"""
Expand Down Expand Up @@ -244,19 +279,24 @@ def initialize(self):
self._active_profile = None
self._profile_holds = ProfileHoldManager(self)
self._performance_degraded = PerformanceDegraded.NONE
self.check_performance_degraded()
self._config = PPDConfig(PPD_CONFIG_FILE, self._tuned_interface)
self._setup_battery_signaling()
self._base_profile = self._load_base_profile() or self._config.default_profile
self.switch_profile(self._base_profile)
self._save_base_profile(self._base_profile)
self._setup_inotify()

def run(self):
"""
Exports the DBus interface and runs the main daemon loop.
"""
exports.start()
self._notifier.start()
while not self._cmd.wait(self._terminate, 1):
self._check_performance_degraded()
pass
self._watch_manager.rm_watch(list(self._inotify_watches.values()))
self._notifier.stop()
exports.stop()

@property
Expand Down

0 comments on commit 63961a4

Please sign in to comment.