Add SMT toggle

This commit is contained in:
NGnius (Graham) 2022-04-21 21:01:48 -04:00
parent dc625f023e
commit 265989ec87
3 changed files with 78 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 119 KiB

After

Width:  |  Height:  |  Size: 154 KiB

43
main.py
View file

@ -5,20 +5,25 @@ class Plugin:
SCALING_FREQUENCIES = [1700000, 2400000, 2800000] SCALING_FREQUENCIES = [1700000, 2400000, 2800000]
# call from main_view.html with setCPUs(onclick_event) or call_plugin_method("set_cpus", count) # call from main_view.html with setCPUs(onclick_event) or call_plugin_method("set_cpus", count)
async def set_cpus(self, count) -> int: async def set_cpus(self, count, smt=True) -> int:
print("Setting CPUs") # print("Setting CPUs")
with open("/home/deck/PowerTools.log", "a") as f: if smt:
f.write(f"Setting {count} CPUs to active\n")
f.flush()
count = min(int(count), self.CPU_COUNT) count = min(int(count), self.CPU_COUNT)
# never touch cpu0, since it's special # never touch cpu0, since it's special
for cpu in range(1, count): for cpu in range(1, count):
f.write(f"Setting CPU {cpu} to online\n")
enable_cpu(cpu) enable_cpu(cpu)
for cpu in range(count, self.CPU_COUNT): for cpu in range(count, self.CPU_COUNT):
f.write(f"Setting CPU {cpu} to offline\n")
disable_cpu(cpu) disable_cpu(cpu)
return self.CPU_COUNT return self.CPU_COUNT
else:
count = min(int(count), self.CPU_COUNT / 2)
for cpu in range(1, self.CPU_COUNT, 2):
disable_cpu(cpu)
for cpu in range(2, self.CPU_COUNT, 2):
if (cpu / 2) + 1 > count:
disable_cpu(cpu)
else:
enable_cpu(cpu)
async def get_cpus(self) -> int: async def get_cpus(self) -> int:
online_count = 1 # cpu0 is always online online_count = 1 # cpu0 is always online
@ -26,6 +31,9 @@ class Plugin:
online_count += int(status_cpu(cpu)) online_count += int(status_cpu(cpu))
return online_count return online_count
async def get_smt(self) -> bool:
return status_cpu(1) == status_cpu(2) and status_cpu(3) == status_cpu(4)
async def set_boost(self, enabled: bool) -> bool: async def set_boost(self, enabled: bool) -> bool:
write_to_sys("/sys/devices/system/cpu/cpufreq/boost", int(enabled)) write_to_sys("/sys/devices/system/cpu/cpufreq/boost", int(enabled))
return True return True
@ -40,6 +48,8 @@ class Plugin:
updated = 0 updated = 0
for cpu in range(0, self.CPU_COUNT): for cpu in range(0, self.CPU_COUNT):
if cpu == 0 or status_cpu(cpu): if cpu == 0 or status_cpu(cpu):
if read_scaling_governor_cpu(cpu) != "userspace":
write_scaling_governor_cpu(cpu, "userspace")
path = cpu_freq_scaling_path(cpu) path = cpu_freq_scaling_path(cpu)
write_to_sys(path, selected_freq) write_to_sys(path, selected_freq)
updated += 1 updated += 1
@ -47,13 +57,14 @@ class Plugin:
async def get_max_boost(self) -> int: async def get_max_boost(self) -> int:
path = cpu_freq_scaling_path(0) path = cpu_freq_scaling_path(0)
freq = int(read_from_sys(path, amount=-1).strip()) freq_maybe = read_from_sys(path, amount=-1).strip()
if freq_maybe is None or len(freq_maybe) == 0 or freq_maybe == "<unsupported>":
return len(self.SCALING_FREQUENCIES) - 1
freq = int(freq_maybe)
return self.SCALING_FREQUENCIES.index(freq) return self.SCALING_FREQUENCIES.index(freq)
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded # Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self): async def _main(self):
with open("/home/deck/PowerTools.log", "w") as f:
f.write(f"Main loop\n")
pass pass
@ -65,6 +76,9 @@ def cpu_online_path(cpu_number: int) -> str:
def cpu_freq_scaling_path(cpu_number: int) -> str: def cpu_freq_scaling_path(cpu_number: int) -> str:
return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_setspeed" return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_setspeed"
def cpu_governor_scaling_path(cpu_number: int) -> str:
return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_governor"
def write_to_sys(path, value: int): def write_to_sys(path, value: int):
with open(path, mode="w") as f: with open(path, mode="w") as f:
f.write(str(value)) f.write(str(value))
@ -84,3 +98,12 @@ def disable_cpu(cpu_number: int):
def status_cpu(cpu_number: int) -> bool: def status_cpu(cpu_number: int) -> bool:
filepath = cpu_online_path(cpu_number) filepath = cpu_online_path(cpu_number)
return read_from_sys(filepath) == "1" return read_from_sys(filepath) == "1"
def read_scaling_governor_cpu(cpu_number: int) -> str:
filepath = cpu_governor_scaling_path(cpu_number)
return read_from_sys(filepath, amount=-1).trim()
def write_scaling_governor_cpu(cpu_number: int, governor: str):
filepath = cpu_governor_scaling_path(cpu_number)
with open(filepath, mode="w") as f:
f.write(governor)

View file

@ -6,14 +6,18 @@
<script src="/static/library.js"></script> <script src="/static/library.js"></script>
<script> <script>
// Python functions // Python functions
function setCPUs(value) { function setCPUs(value, smt) {
return call_plugin_method("set_cpus", {"count":value}); return call_plugin_method("set_cpus", {"count":value, "smt": smt});
} }
function getCPUs() { function getCPUs() {
return call_plugin_method("get_cpus", {}); return call_plugin_method("get_cpus", {});
} }
function getSMT() {
return call_plugin_method("get_smt", {});
}
function setCPUBoost(value) { function setCPUBoost(value) {
return call_plugin_method("set_boost", {"enabled": value}); return call_plugin_method("set_boost", {"enabled": value});
} }
@ -35,13 +39,14 @@
async function onReady() { async function onReady() {
let boostToggle = document.getElementById("boostToggle"); let boostToggle = document.getElementById("boostToggle");
setToggleState(boostToggle, await getCPUBoost()); setToggleState(boostToggle, await getCPUBoost());
setToggleState(document.getElementById("smtToggle"), await getSMT());
selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8); selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8);
selectNotch("frequencyNotch", await getMaxBoost(), 3); selectNotch("frequencyNotch", await getMaxBoost(), 3);
} }
async function setCPUNotch(index) { async function setCPUNotch(index) {
const ROOT_ID = "cpuThreadsNotch"; const ROOT_ID = "cpuThreadsNotch";
await setCPUs(index); await setCPUs(index, getToggleState(document.getElementById("smtToggle")));
selectNotch(ROOT_ID, await getCPUs() - 1, 8); selectNotch(ROOT_ID, await getCPUs() - 1, 8);
} }
@ -64,7 +69,15 @@
let toggle = document.getElementById("boostToggle"); let toggle = document.getElementById("boostToggle");
let isActive = getToggleState(toggle); let isActive = getToggleState(toggle);
await setCPUBoost(!isActive); await setCPUBoost(!isActive);
setToggleState(toggle, await getCPUBoost()); setToggleState(toggle, !isActive);
}
async function toggleCPUSMT() {
let toggle = document.getElementById("smtToggle");
let isActive = getToggleState(toggle);
await setCPUs(await getCPUs(), !isActive);
setToggleState(toggle, !isActive);
selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8);
} }
async function setBoostNotch(index) { async function setBoostNotch(index) {
@ -93,6 +106,26 @@
<style type="text/css" media="screen"></style> <style type="text/css" media="screen"></style>
</head> </head>
<body onload="onReady()"> <body onload="onReady()">
<!-- SMT toggle switch, roughly copied from https://github.com/SteamDeckHomebrew/ExtraSettingsPlugin/blob/main/main_view.html -->
<div class="quickaccessmenu_TabGroupPanel_1QO7b Panel Focusable">
<div class="quickaccesscontrols_PanelSectionRow_26R5w">
<div class="quickaccesscontrols_PanelSectionRow_26R5w">
<div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable" style="--indent-level:0;">
<div class="gamepaddialog_FieldLabelRow_2VcTl">
<div class="gamepaddialog_FieldLabel_3jMlJ">
CPU SMT
</div>
<div class="gamepaddialog_FieldChildren_2rhav">
<div id="smtToggle" tabindex="0" class="gamepaddialog_Toggle_9Ql-o Focusable" onclick="toggleCPUSMT()">
<div class="gamepaddialog_ToggleRail_2bl0i"></div>
<div class="gamepaddialog_ToggleSwitch_1PQpp"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- CPUs selector --> <!-- CPUs selector -->
<div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_WithChildrenBelow_37xzV gamepaddialog_InlineWrapShiftsChildrenBelow_3LCXh gamepaddialog_WithBottomSeparator_3YKpU gamepaddialog_ChildrenWidthFixed_ljcbL gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable"> <div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_WithChildrenBelow_37xzV gamepaddialog_InlineWrapShiftsChildrenBelow_3LCXh gamepaddialog_WithBottomSeparator_3YKpU gamepaddialog_ChildrenWidthFixed_ljcbL gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable">
<div class="gamepaddialog_FieldLabelRow_2VcTl"> <div class="gamepaddialog_FieldLabelRow_2VcTl">
@ -142,11 +175,11 @@
</div> </div>
</div> </div>
<!-- Toggle switch, roughly copied from https://github.com/SteamDeckHomebrew/ExtraSettingsPlugin/blob/main/main_view.html --> <!-- CPU Boost toggle switch, roughly copied from https://github.com/SteamDeckHomebrew/ExtraSettingsPlugin/blob/main/main_view.html -->
<div class="quickaccessmenu_TabGroupPanel_1QO7b Panel Focusable"> <div class="quickaccessmenu_TabGroupPanel_1QO7b Panel Focusable">
<div class="quickaccesscontrols_PanelSectionRow_26R5w"> <div class="quickaccesscontrols_PanelSectionRow_26R5w">
<div class="quickaccesscontrols_PanelSectionRow_26R5w"> <div class="quickaccesscontrols_PanelSectionRow_26R5w">
<div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm gamepaddialog_WithBottomSeparator_3YKpU Panel Focusable" style="--indent-level:0;"> <div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable" style="--indent-level:0;">
<div class="gamepaddialog_FieldLabelRow_2VcTl"> <div class="gamepaddialog_FieldLabelRow_2VcTl">
<div class="gamepaddialog_FieldLabel_3jMlJ"> <div class="gamepaddialog_FieldLabel_3jMlJ">
CPU Boost CPU Boost
@ -161,6 +194,7 @@
</div> </div>
</div> </div>
</div> </div>
</div>
<!-- Frequency selector --> <!-- Frequency selector -->
<div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_WithChildrenBelow_37xzV gamepaddialog_InlineWrapShiftsChildrenBelow_3LCXh gamepaddialog_WithBottomSeparator_3YKpU gamepaddialog_ChildrenWidthFixed_ljcbL gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable"> <div class="gamepaddialog_Field_eKmEX gamepaddialog_WithFirstRow_2bDqk gamepaddialog_WithChildrenBelow_37xzV gamepaddialog_InlineWrapShiftsChildrenBelow_3LCXh gamepaddialog_WithBottomSeparator_3YKpU gamepaddialog_ChildrenWidthFixed_ljcbL gamepaddialog_ExtraPaddingOnChildrenBelow_3nLNL gamepaddialog_StandardPadding_xIITX gamepaddialog_HighlightOnFocus_2HFrm Panel Focusable">
<div class="gamepaddialog_FieldLabelRow_2VcTl"> <div class="gamepaddialog_FieldLabelRow_2VcTl">
@ -190,6 +224,9 @@
</div> </div>
</div> </div>
</div> </div>
<div style="font-size:x-small;">
WARNING: this will change the CPU governor, affecting battery life until reboot.
</div>
</div> </div>
</body> </body>
</html> </html>