Sysfs_Power_Path can now be set in the config file (#48)

This commit is contained in:
erpalma
2018-09-01 10:10:49 +02:00
parent dcfa39acb8
commit 494ea7f595
2 changed files with 20 additions and 10 deletions

View File

@@ -1,5 +1,8 @@
[GENERAL] [GENERAL]
# Enable or disable the script execution
Enabled: True Enabled: True
# SYSFS path for checking if the system is running on AC power
Sysfs_Power_Path = "/sys/class/power_supply/AC*/online"
## Settings to apply while connected to Battery power ## Settings to apply while connected to Battery power
[BATTERY] [BATTERY]

View File

@@ -3,6 +3,7 @@
import argparse import argparse
import configparser import configparser
import dbus import dbus
import glob
import os import os
import psutil import psutil
import struct import struct
@@ -17,7 +18,7 @@ from mmio import MMIO, MMIOError
from multiprocessing import cpu_count from multiprocessing import cpu_count
from threading import Event, Thread from threading import Event, Thread
SYSFS_POWER_PATH = '/sys/class/power_supply/AC/online' DEFAULT_SYSFS_POWER_PATH = '/sys/class/power_supply/AC*/online'
VOLTAGE_PLANES = { VOLTAGE_PLANES = {
'CORE': 0, 'CORE': 0,
@@ -136,9 +137,15 @@ def get_value_for_bits(val, from_bit=0, to_bit=63):
return (val & mask) >> from_bit return (val & mask) >> from_bit
def is_on_battery(): def is_on_battery(config):
with open(SYSFS_POWER_PATH) as f: try:
for path in glob.glob(config.get('Sysfs_Power_Path', 'GENERAL', fallback=DEFAULT_SYSFS_POWER_PATH)):
with open(path) as f:
return not bool(int(f.read())) return not bool(int(f.read()))
except:
pass
print('[E] No valid Sysfs_Power_Path found!')
sys.exit(1)
def get_cpu_platform_info(): def get_cpu_platform_info():
@@ -329,7 +336,7 @@ def power_thread(config, regs, exit_event):
# switch back to sysfs polling # switch back to sysfs polling
if power['method'] == 'polling': if power['method'] == 'polling':
power['source'] = 'BATTERY' if is_on_battery() else 'AC' power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
# set temperature trip point # set temperature trip point
if 'MSR_TEMPERATURE_TARGET' in regs[power['source']]: if 'MSR_TEMPERATURE_TARGET' in regs[power['source']]:
@@ -375,7 +382,7 @@ def power_thread(config, regs, exit_event):
# set full performance mode only when load is greater than this threshold (~ at least 1 core full speed) # set full performance mode only when load is greater than this threshold (~ at least 1 core full speed)
performance_mode = cpu_usage > 100. / (cpu_count() * 1.25) performance_mode = cpu_usage > 100. / (cpu_count() * 1.25)
# check again if we are on AC, since in the meantime we might have switched to BATTERY # check again if we are on AC, since in the meantime we might have switched to BATTERY
if not is_on_battery(): if not is_on_battery(config):
set_hwp('performance' if performance_mode else 'balance_performance') set_hwp('performance' if performance_mode else 'balance_performance')
else: else:
exit_event.wait(wait_t) exit_event.wait(wait_t)
@@ -393,9 +400,9 @@ def main():
parser.add_argument('--config', default='/etc/lenovo_fix.conf', help='override default config file path') parser.add_argument('--config', default='/etc/lenovo_fix.conf', help='override default config file path')
args = parser.parse_args() args = parser.parse_args()
power['source'] = 'BATTERY' if is_on_battery() else 'AC'
config = load_config() config = load_config()
power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
platform_info = get_cpu_platform_info() platform_info = get_cpu_platform_info()
if args.debug: if args.debug:
for key, value in platform_info.items(): for key, value in platform_info.items():