Skip to content

Commit

Permalink
add power utils
Browse files Browse the repository at this point in the history
  • Loading branch information
aarron-lee committed Jan 22, 2024
1 parent 398e05d commit 4886699
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions py_modules/power_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import glob
import decky_plugin
import subprocess
from enum import Enum

# ENERGY_PERFORMANCE_PREFERENCE_PATH
EPP_DEVICES = glob.glob('/sys/devices/system/cpu/cpu*/cpufreq/energy_performance_preference')
EPP_OPTION_PATHS = glob.glob('/sys/devices/system/cpu/cpu*/cpufreq/energy_performance_available_preferences')
EPP_PATH ='/sys/devices/system/cpu/cpu*/cpufreq/energy_performance_preference'

POWER_GOVERNOR_PATH = '/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor'
POWER_GOVERNOR_DEVICES = glob.glob(POWER_GOVERNOR_PATH)

class PowerGovernorOptions(Enum):
PERFORMANCE = 'performance'
POWER_SAVE = 'powersave'
BALANCED = 'schedutil'

def set_power_governor(governor_option):
try:
option = PowerGovernorOptions(governor_option).value

if len(POWER_GOVERNOR_DEVICES) > 0:
return execute_bash_command(option, POWER_GOVERNOR_PATH)
except Exception as e:
decky_plugin.logger.error(f'{__name__} error setting power governor {e}')

def get_available_epp_options():
try:
if len(EPP_OPTION_PATHS) > 0:
path = EPP_OPTION_PATHS[0]
with open(path, 'r') as file:
available_options = file.read().strip().split(' ')
file.close()
return available_options
except Exception as e:
decky_plugin.logger.error(f'{__name__} error getting epp options {e}')

return []

def set_epp(epp_option):
try:
if epp_option not in get_available_epp_options():
return
if len(EPP_DEVICES) > 0:
return execute_bash_command(epp_option, EPP_PATH)

except Exception as e:
decky_plugin.logger.error(f'{__name__} error setting epp {e}')

def execute_bash_command(command, path):
cmd = f"echo '{command}' | tee {path}"
result = subprocess.run(cmd, shell=True, check=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result

0 comments on commit 4886699

Please sign in to comment.