Skip to content

Commit

Permalink
refactor: add utils mmodule, add ConfManager
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Jan 10, 2025
1 parent 3276eea commit da77a71
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 60 deletions.
19 changes: 10 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import decky
from settings import SettingsManager

try:
import update
from config import CONFIG_KEY, logger
from conf_manager import confManager
from config import logger
from cpu import cpuManager
from fan import fanManager
from fuse_manager import FuseManager
Expand All @@ -15,16 +15,18 @@

class Plugin:
def __init__(self):
self.fuseManager = FuseManager()
self.fuseManager = None
self.confManager = confManager

async def _migration(self):
decky.logger.info("start _migration")

self.fuseManager = FuseManager()
self.fuseManager.fuse_init()

async def _main(self):
self.settings = SettingsManager(
name="config", settings_directory=decky.DECKY_PLUGIN_SETTINGS_DIR
)
decky.logger.info("start _main")
pass

async def _unload(self):
decky.logger.info("start _unload")
Expand All @@ -33,11 +35,10 @@ async def _unload(self):
logger.info("End PowerControl")

async def get_settings(self):
return self.settings.getSetting(CONFIG_KEY)
return self.confManager.getSettings()

async def set_settings(self, settings):
self.settings.setSetting(CONFIG_KEY, settings)
# logger.info(f"save Settings: {settings}")
self.confManager.setSettings(settings)
return True

async def get_hasRyzenadj(self):
Expand Down
31 changes: 31 additions & 0 deletions py_modules/conf_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import decky
from config import CONFIG_KEY
from settings import SettingsManager


class ConfManager:
def __init__(self):
self.sysSettings = SettingsManager(
name="config", settings_directory=decky.DECKY_PLUGIN_SETTINGS_DIR
)
self.fansSettings = SettingsManager(
name="fans_config",
settings_directory=decky.DECKY_PLUGIN_SETTINGS_DIR,
)

def getSettings(self):
return self.sysSettings.getSetting(CONFIG_KEY)

def setSettings(self, settings):
self.sysSettings.setSetting(CONFIG_KEY, settings)
return True

def getFanSettings(self):
return self.fansSettings.getSetting(CONFIG_KEY)

def setFanSettingsByKey(self, key, value):
self.fansSettings.setSetting(key, value)
return True


confManager = ConfManager()
59 changes: 20 additions & 39 deletions py_modules/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
import traceback
from typing import Dict, List, Optional, Tuple

from config import (
CPU_ID,
CPU_VENDOR,
PRODUCT_NAME,
RYZENADJ_PATH,
SH_PATH,
TDP_LIMIT_CONFIG_CPU,
TDP_LIMIT_CONFIG_PRODUCT,
logger,
)
from config import CPU_VENDOR, RYZENADJ_PATH, SH_PATH, logger
from utils import getMaxTDP


class CPUManager:
Expand Down Expand Up @@ -90,7 +82,7 @@ def get_hasRyzenadj(self) -> bool:
else:
logger.info("get_hasRyzenadj {}".format(False))
return False
except Exception as e:
except Exception:
logger.error("Failed to check ryzenadj tool", exc_info=True)
return False

Expand All @@ -117,7 +109,7 @@ def get_cpuMaxNum(self) -> int:
self.cpu_maxNum = cpu_index
logger.info("get_cpuMaxNum {}".format(self.cpu_maxNum))
return self.cpu_maxNum
except Exception as e:
except Exception:
logger.error("Failed to get max CPU cores", exc_info=True)
return 0

Expand All @@ -127,22 +119,7 @@ def get_tdpMax(self) -> int:
Returns:
int: 最大TDP值(瓦特)
"""
try:
# 根据机器型号或者CPU型号返回tdp最大值
if PRODUCT_NAME in TDP_LIMIT_CONFIG_PRODUCT:
self.cpu_tdpMax = TDP_LIMIT_CONFIG_PRODUCT[PRODUCT_NAME]
else:
for model in TDP_LIMIT_CONFIG_CPU:
if model in CPU_ID:
self.cpu_tdpMax = TDP_LIMIT_CONFIG_CPU[model]
break
else:
self.cpu_tdpMax = 15
logger.info("get_tdpMax {}".format(self.cpu_tdpMax))
return self.cpu_tdpMax
except Exception as e:
logger.error("Failed to get max TDP value", exc_info=True)
return 0
return getMaxTDP()

# 弃用
def get_cpu_AvailableFreq(self) -> List[int]:
Expand Down Expand Up @@ -171,7 +148,7 @@ def get_cpu_AvailableFreq(self) -> List[int]:
f"cpu_avaFreqData={[self.cpu_avaFreq,self.cpu_avaMinFreq,self.cpu_avaMaxFreq]}"
)
return self.cpu_avaFreq
except Exception as e:
except Exception:
logger.error("Failed to get available CPU frequencies", exc_info=True)
return []

Expand Down Expand Up @@ -220,7 +197,7 @@ def __get_intel_rapl_path(self) -> Tuple[str, str]:
elif name == "long_term":
rapl_long = f.replace("_name", "_power_limit_uw")
return rapl_long, rapl_short
except Exception as e:
except Exception:
logger.error("Failed to get Intel RAPL path", exc_info=True)
return "", ""

Expand All @@ -247,7 +224,7 @@ def set_cpuTDP_Intel(self, value: int) -> bool:
file.write(str(tdp))
return True

except Exception as e:
except Exception:
logger.error(f"Failed to set Intel CPU TDP: value={value}", exc_info=True)
return False

Expand Down Expand Up @@ -290,9 +267,11 @@ def set_cpuTDP_AMD(self, value: int) -> bool:

return True
else:
logger.error(f"Failed to set AMD CPU TDP: value less than 3W (value={value})")
logger.error(
f"Failed to set AMD CPU TDP: value less than 3W (value={value})"
)
return False
except Exception as e:
except Exception:
logger.error(f"Failed to set AMD CPU TDP: value={value}", exc_info=True)
return False

Expand Down Expand Up @@ -355,8 +334,10 @@ def set_cpuOnline(self, value: int) -> bool:
else:
self.online_cpu(int(cpu))
return True
except Exception as e:
logger.error(f"Failed to set CPU online status: value={value}", exc_info=True)
except Exception:
logger.error(
f"Failed to set CPU online status: value={value}", exc_info=True
)
return False

def set_enable_All(self) -> bool:
Expand All @@ -376,7 +357,7 @@ def set_enable_All(self) -> bool:
cpu_number = match.group(1)
self.online_cpu(int(cpu_number))
return True
except Exception as e:
except Exception:
logger.error("Failed to enable all CPU cores", exc_info=True)
return False

Expand Down Expand Up @@ -407,7 +388,7 @@ def get_isSupportSMT(self) -> bool:
self.is_support_smt = False
else:
self.is_support_smt = int(stdout) > 1
except Exception as e:
except Exception:
logger.error("Failed to check SMT support", exc_info=True)
self.is_support_smt = False
return self.is_support_smt
Expand All @@ -428,7 +409,7 @@ def set_smt(self, value: bool) -> bool:
logger.debug("set_smt {}".format(value))
self.cpu_smt = value
return True
except Exception as e:
except Exception:
logger.error(f"Failed to set SMT: value={value}", exc_info=True)
return False

Expand Down Expand Up @@ -869,7 +850,7 @@ def set_epp(self, mode: str) -> bool:
success = True

return success
except Exception as e:
except Exception:
logger.error(f"Failed to set EPP mode: mode={mode}", exc_info=True)
return False

Expand Down
14 changes: 5 additions & 9 deletions py_modules/fan.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os

import decky
from conf_manager import confManager
from config import FAN_EC_CONFIG, FAN_HWMON_LIST, PRODUCT_NAME, PRODUCT_VERSION, logger
from ec import EC
from pfuse import umount_fuse_igpu
from settings import SettingsManager


class FanConfig:
Expand Down Expand Up @@ -49,10 +48,7 @@ def __init__(self):

class FanManager:
def __init__(self):
self.settings = SettingsManager(
name="fans_config",
settings_directory=decky.DECKY_PLUGIN_SETTINGS_DIR,
)
self.fansSettings = confManager.fansSettings

self.fan_config_list: list[FanConfig] = [] # 记录每一个风扇的配置
self.cpu_temp_path = "" # CPU温度路径
Expand All @@ -76,7 +72,7 @@ def update_fan_max_value(self, cpu_temp: int):
f"cup_temp {cup_temp}, 风扇{index} 当前转速已达到最大值, 更新最大值: {max_value} -> {current_rpm}"
)
fan_config.pwm_value_max = current_rpm
self.settings.setSetting(f"fan{index}_max", current_rpm)
self.fansSettings.setSetting(f"fan{index}_max", current_rpm)

# 解析处理 HWMON 风扇配置
def __parse_fan_configuration_HWMON(self, name_path_map):
Expand Down Expand Up @@ -178,7 +174,7 @@ def __parse_fan_configuration_HWMON(self, name_path_map):
if "pwm_read_max" in fan_pwm_input
else 0
)
max_value_from_settings = self.settings.getSetting(
max_value_from_settings = self.fansSettings.getSetting(
f"fan{len(self.fan_config_list)}_max"
)
fc.pwm_value_max = (
Expand Down Expand Up @@ -258,7 +254,7 @@ def __parse_fan_configuration_EC(self):
fc.fan_value_max = (
ec_info["rpm_value_max"] if "rpm_value_max" in ec_info else 0
)
max_value_from_settings = self.settings.getSetting(
max_value_from_settings = self.fansSettings.getSetting(
f"fan{len(self.fan_config_list)}_max"
)
logger.info(
Expand Down
25 changes: 23 additions & 2 deletions py_modules/fuse_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
from threading import Event

import decky
from conf_manager import confManager
from config import logger


class FuseManager:
def __init__(self):
def __init__(
self,
max_tdp=30,
):
self.t = None
self.t_sys = None
self.should_exit = None
self.emit = None
self.min_tdp = 3
self.default_tdp = 500
self.max_tdp = 30
self.max_tdp = max_tdp

self.igpu_path = None

Expand Down Expand Up @@ -61,10 +65,26 @@ def fuse_init(self):
start_tdp_client,
umount_fuse_igpu,
)
from utils.tdp import getMaxTDP

# umount igpu
umount_fuse_igpu()

# find igpu
self.igpu_path = find_igpu()

settings = confManager.getSettings()
tdpMax = getMaxTDP()
enableCustomTDPRange = settings.get("enableCustomTDPRange", False)
realTDPMax = (
settings.get("customTDPRangeMax", tdpMax)
if enableCustomTDPRange
else tdpMax
)

logger.info(f">>>> FuseManager tdpMax: {realTDPMax}")
self.max_tdp = realTDPMax

if self.should_exit:
return
self.should_exit = Event()
Expand All @@ -80,3 +100,4 @@ def fuse_init(self):
)
except Exception:
logger.error("Failed to start", exc_info=True)
logger.error("Failed to start", exc_info=True)
4 changes: 3 additions & 1 deletion py_modules/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
INTEL_GPU_MIN_LIMIT,
logger,
)
from gpu_fix import fix_gpuFreqSlider_AMD, fix_gpuFreqSlider_INTEL
from inotify import IN_MODIFY, notify
from utils import fix_gpuFreqSlider_AMD, fix_gpuFreqSlider_INTEL


class GPUAutoFreqManager(threading.Thread):
Expand Down Expand Up @@ -441,3 +441,5 @@ def get_gpuFreqMax(self):

gpuManager = GPUManager()
# gpuManager.fix_gpuFreqSlider()
# gpuManager.fix_gpuFreqSlider()
# gpuManager.fix_gpuFreqSlider()
8 changes: 8 additions & 0 deletions py_modules/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .gpu_fix import fix_gpuFreqSlider_AMD, fix_gpuFreqSlider_INTEL
from .tdp import getMaxTDP

__all__ = [
"fix_gpuFreqSlider_AMD",
"fix_gpuFreqSlider_INTEL",
"getMaxTDP",
]
File renamed without changes.
31 changes: 31 additions & 0 deletions py_modules/utils/tdp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from config import (
CPU_ID,
PRODUCT_NAME,
TDP_LIMIT_CONFIG_CPU,
TDP_LIMIT_CONFIG_PRODUCT,
logger,
)


def getMaxTDP() -> int:
"""获取最大TDP值。
Returns:
int: 最大TDP值(瓦特)
"""
try:
# 根据机器型号或者CPU型号返回tdp最大值
if PRODUCT_NAME in TDP_LIMIT_CONFIG_PRODUCT:
cpu_tdpMax = TDP_LIMIT_CONFIG_PRODUCT[PRODUCT_NAME]
else:
for model in TDP_LIMIT_CONFIG_CPU:
if model in CPU_ID:
cpu_tdpMax = TDP_LIMIT_CONFIG_CPU[model]
break
else:
cpu_tdpMax = 15
logger.info("getMaxTDP {}".format(cpu_tdpMax))
return cpu_tdpMax
except Exception as e:
logger.error("Failed to get max TDP value", exc_info=True)
return 0

0 comments on commit da77a71

Please sign in to comment.