Skip to content

Commit

Permalink
1.修复原生gpu频率滑动条
Browse files Browse the repository at this point in the history
2.当原生tdp修补失败的时候,插件内显示tdp设置,保证有tdp设置可用
  • Loading branch information
Gawah committed Nov 26, 2023
1 parent 7f2da38 commit a149879
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 114 deletions.
41 changes: 41 additions & 0 deletions backend/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,45 @@ def set_gpuFreqRange(self, value: int, value2:int):
logging.error(e)
return False

def fix_gpuFreqSlider(self):
try:
gpu_file_path=["power_dpm_force_performance_level","pp_od_clk_voltage"]
steamos_priv_path="/usr/bin/steamos-polkit-helpers/steamos-priv-write"
# 读取sh文件内容
with open(steamos_priv_path, 'r') as file:
sh_code = file.read()

for path in gpu_file_path:
# 匹配目标if语句,并检查then部分的代码
if_match = re.search(r'if([\s\S]*?)\[\[([\s\S]*?){}([\s\S]*?)]]([\s\S]*?)then([\s\S]*?)fi'.format(path), sh_code, flags=re.DOTALL)
if if_match:
# 获取then部分的代码
then_code = if_match.group(5)
new_then_code = ''' WRITE_PATH=$(ls /sys/class/drm/*/device/{} | head -n 1)
CommitWrite'''.format(path)

# 如果then部分的代码与目标不同,则将其替换
if then_code.strip() != new_then_code.strip():
new_if_code = re.sub(r'if([\s\S]*?)\[\[([\s\S]*?){}([\s\S]*?)]]([\s\S]*?)then([\s\S]*?)fi'.format(path), r'if\1[[\2{}\3]]\4then\n{}\nfi'.format(path,new_then_code), sh_code)

sh_code = new_if_code
else:
# 没有匹配到目标if语句,在文件能匹配到的最后一个if [[ ]] then fi后面添加代码
last_if_match = re.findall(r'if[\s\S]*?\[\[[\s\S]*?]][\s\S]*?then[\s\S]*?fi', sh_code, flags=re.DOTALL)
add_code='''
if [[ "$WRITE_PATH" == /sys/class/drm/card*/device/{} ]]; then
WRITE_PATH=$(ls /sys/class/drm/*/device/{} | head -n 1)
CommitWrite
fi'''.format(path,path)
# 文件最后一个if,换行后添加
if last_if_match:
sh_code = sh_code.replace(last_if_match[-1], f'{last_if_match[-1]}\n{add_code}')

# 将修改后的代码写回文件
with open(steamos_priv_path, 'w') as file:
file.write(sh_code)
except:
logging.error(e)

gpuManager = GPUManager()
gpuManager.fix_gpuFreqSlider()
13 changes: 7 additions & 6 deletions src/components/cpu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ToggleField,
} from "decky-frontend-lib";
import { useEffect, useState, VFC} from "react";
import { Settings,Backend, PluginManager,ComponentName, UpdateType, GPUMODE} from "../util";
import { Settings,Backend, PluginManager,ComponentName, UpdateType, GPUMODE, Patch} from "../util";
import { localizeStrEnum,localizationManager } from "../i18n";
import {SlowSliderField} from "./SlowSliderField"

Expand Down Expand Up @@ -111,15 +111,15 @@ const CPUNumComponent:VFC = () =>{
);
}

/*

const CPUTDPComponent:VFC = () =>{
const [tdpEnable, setTDPEnable] = useState<boolean>(Settings.appTDPEnable());
const [tdp,setTDP] = useState<number>(Settings.appTDP());
const [disabled,setDisable] = useState<boolean>(!Backend.data.HasRyzenadj()||Settings.appGPUMode()==GPUMODE.AUTO);
const [disabled,setDisable] = useState<boolean>(Settings.appGPUMode()==GPUMODE.AUTO);
const refresh = () => {
setTDPEnable(Settings.appTDPEnable());
setTDP(Settings.appTDP());
setDisable(!Backend.data.HasRyzenadj()||Settings.appGPUMode()==GPUMODE.AUTO);
setDisable(Settings.appGPUMode()==GPUMODE.AUTO);
};
useEffect(() => {
PluginManager.listenUpdateComponent(ComponentName.CPU_TDP,[ComponentName.CPU_TDP,ComponentName.GPU_FREQMODE],(_ComponentName,updateType)=>{
Expand All @@ -136,7 +136,7 @@ const CPUTDPComponent:VFC = () =>{
<PanelSectionRow>
<ToggleField
label={localizationManager.getString(localizeStrEnum.TDP)}
description={Backend.data.HasRyzenadj() ? localizationManager.getString(localizeStrEnum.TDP_DESC) : localizationManager.getString(localizeStrEnum.RYZENADJ_NOT_FOUND)}
description={localizationManager.getString(localizeStrEnum.TDP_DESC)}
checked={tdpEnable}
disabled={disabled}
onChange={(value) => {
Expand All @@ -161,7 +161,7 @@ const CPUTDPComponent:VFC = () =>{
</div>
);
}
*/


export const CPUComponent: VFC = () => {
const [show,setShow] = useState<boolean>(Settings.ensureEnable());
Expand Down Expand Up @@ -189,6 +189,7 @@ export const CPUComponent: VFC = () => {
<CPUBoostComponent/>
<CPUSmtComponent/>
<CPUNumComponent/>
{!PluginManager.isPatchSuccess(Patch.TDPPatch)&& <CPUTDPComponent/>}
</PanelSection>}
</div>
);
Expand Down
19 changes: 8 additions & 11 deletions src/components/gpu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { localizeStrEnum,localizationManager } from "../i18n";
import {SlowSliderField} from "./SlowSliderField"

//GPUFreq模块
/*
const GPUFreqComponent: VFC = () => {
const [gpuFreq, setGPUFreq] = useState<number>(Settings.appGPUFreq());
const refresh = () => {
Expand Down Expand Up @@ -42,6 +43,7 @@ const GPUFreqComponent: VFC = () => {
</PanelSectionRow>
);
};
*/

//GPURange模块
const GPURangeComponent: VFC = () => {
Expand Down Expand Up @@ -174,22 +176,18 @@ const GPUModeComponent: VFC = () => {
return (
<div>
<PanelSectionRow>
<SliderField
<SliderField
label={localizationManager.getString(localizeStrEnum.GPU_FREQMODE)}
value={gpuMode}
step={1}
max={3}
max={2}
min={0}
notchCount={4}
notchCount={3}
notchLabels={
[{
notchIndex: GPUMODE.NOLIMIT,
label: `${localizationManager.getString(localizeStrEnum.UNLIMITED)}`,
value: GPUMODE.NOLIMIT,
}, {
notchIndex: GPUMODE.FIX,
label: `${localizationManager.getString(localizeStrEnum.FIXED_FREQ)}`,
value: GPUMODE.FIX,
notchIndex: GPUMODE.NATIVE,
label: `${localizationManager.getString(localizeStrEnum.NATIVE_FREQ)}`,
value: GPUMODE.NATIVE,
}, {
notchIndex: GPUMODE.RANGE,
label: `${localizationManager.getString(localizeStrEnum.RANGE_FREQ)}`,
Expand All @@ -205,7 +203,6 @@ const GPUModeComponent: VFC = () => {
Settings.setGPUMode(value);
}}
/>
{gpuMode==GPUMODE.FIX&&<GPUFreqComponent/>}
{gpuMode==GPUMODE.RANGE&&<GPURangeComponent/>}
{gpuMode==GPUMODE.AUTO&&<GPUAutoComponent/>}
</PanelSectionRow>
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
"CANCEL":"Cancel",
"CURENT_STAT":"Current status",
"EDIT":"Edit",
"SAVE":"Save"
"SAVE":"Save",
"NATIVE_FREQ":"Native"
}
1 change: 1 addition & 0 deletions src/i18n/localizeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ export enum localizeStrEnum {
CURENT_STAT="CURENT_STAT",
EDIT="EDIT",
SAVE="SAVE",
NATIVE_FREQ="NATIVE_FREQ"
}

3 changes: 2 additions & 1 deletion src/i18n/schinese.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
"CANCEL":"取消",
"CURENT_STAT":"当前状态",
"EDIT":"编辑",
"SAVE":"保存"
"SAVE":"保存",
"NATIVE_FREQ":"原生设置"
}
3 changes: 2 additions & 1 deletion src/i18n/tchinese.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
"CANCEL":"取消",
"CURENT_STAT":"當前狀態",
"EDIT":"編輯",
"SAVE":"儲存"
"SAVE":"儲存",
"NATIVE_FREQ":"本機設定"
}
47 changes: 23 additions & 24 deletions src/util/backend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ServerAPI } from "decky-frontend-lib";
import { APPLYTYPE, FANMODE, GPUMODE } from "./enum";
import { FanControl } from "./pluginMain";
import { APPLYTYPE, FANMODE, GPUMODE, Patch} from "./enum";
import { FanControl, PluginManager} from "./pluginMain";
import { Settings } from "./settings";


Expand Down Expand Up @@ -165,9 +165,12 @@ export class Backend {
this.serverAPI!.callPluginMethod("set_cpuBoost", { "value": cpuBoost });
}



private static applyGPUFreq(freq: number){
public static applyTDP = (tdp:number)=>{
console.log("Applying tdp " + tdp.toString());
this.serverAPI!.callPluginMethod("set_cpuTDP", {"value":tdp});
}

public static applyGPUFreq(freq: number){
console.log("Applying gpuFreq " + freq.toString());
this.serverAPI!.callPluginMethod("set_gpuFreq", {"value":freq});
}
Expand All @@ -190,6 +193,7 @@ export class Backend {
private static applyFanAuto(auto:boolean){
this.serverAPI!.callPluginMethod("set_fanAuto", {"value":auto});
}

private static applyFanPercent(percent:number){
this.serverAPI!.callPluginMethod("set_fanPercent", {"value":percent});
}
Expand All @@ -213,34 +217,29 @@ export class Backend {
const cpuBoost = Settings.appCpuboost();
Backend.applyCpuBoost(cpuBoost);
}
/*
if (applyTarget == APPLYTYPE.SET_ALL || applyTarget == APPLYTYPE.SET_TDP) {
const tdp = Settings.appTDP();
const tdpEnable = Settings.appTDPEnable();
if (tdpEnable) {
Backend.applyTDP(tdp);
}
else {
Backend.applyTDP(Backend.data.getTDPMax());
if(!PluginManager.isPatchSuccess(Patch.TDPPatch)){
const tdp = Settings.appTDP();
const tdpEnable = Settings.appTDPEnable();
if (tdpEnable) {
Backend.applyTDP(tdp);
}
else {
Backend.applyTDP(Backend.data.getTDPMax());
}
}
}
*/
if (applyTarget == APPLYTYPE.SET_ALL || applyTarget == APPLYTYPE.SET_GPUMODE) {
const gpuMode = Settings.appGPUMode();
const gpuFreq = Settings.appGPUFreq();
const gpuAutoMaxFreq = Settings.appGPUAutoMaxFreq();
const gpuAutoMinFreq = Settings.appGPUAutoMinFreq();
const gpuRangeMaxFreq = Settings.appGPURangeMaxFreq();
const gpuRangeMinFreq = Settings.appGPURangeMinFreq();
if (gpuMode == GPUMODE.NOLIMIT) {
Backend.applyGPUAuto(false);
Backend.applyGPUFreq(0);
} else if (gpuMode == GPUMODE.FIX) {
Backend.applyGPUAuto(false);
Backend.applyGPUFreq(gpuFreq);
} else if (gpuMode == GPUMODE.AUTO) {
if (gpuMode == GPUMODE.NATIVE) {
console.log(`原生设置无需处理`)
}else if (gpuMode == GPUMODE.AUTO) {
console.log(`开始自动优化GPU频率`)
//Settings.setTDPEnable(false);
Settings.setTDPEnable(false);
Settings.setCpuboost(false);
Backend.applyGPUAutoRange(gpuAutoMinFreq,gpuAutoMaxFreq);
Backend.applyGPUAuto(true);
Expand Down Expand Up @@ -296,7 +295,7 @@ export class Backend {
Backend.applySmt(true);
Backend.applyCpuNum(Backend.data.getCpuMaxNum());
Backend.applyCpuBoost(true);
//Backend.applyTDP(Backend.data.getTDPMax());
Backend.applyTDP(Backend.data.getTDPMax());
Backend.applyGPUFreq(0);
Backend.applyFanAuto(true);
};
Expand Down
23 changes: 18 additions & 5 deletions src/util/enum.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
export enum GPUMODE{
NOLIMIT=0, //不限制
FIX=1, //固定频率
RANGE=2, //系统调度
AUTO=3, //自动频率
NATIVE=0, //系统原生设置
RANGE=1, //系统调度
AUTO=2, //自动频率
}
export enum FANMODE{
NOCONTROL=0, //不控制
Expand Down Expand Up @@ -59,4 +58,18 @@ export enum PluginState{
INIT="0",
RUN="1",
QUIT="2",
}
}

export enum Patch{
TDPPatch="TDPPatch",
GPUPerformancePatch="GPUPerformancePatch",
}

export enum GPUPerformanceLevel{
DISABLE=1,
ENABLE=2,
}

export enum SettingChangeEvent{
GPUMODE="GPUMODE"
}
Loading

0 comments on commit a149879

Please sign in to comment.