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

45
main.py
View file

@ -5,26 +5,34 @@ class Plugin:
SCALING_FREQUENCIES = [1700000, 2400000, 2800000]
# call from main_view.html with setCPUs(onclick_event) or call_plugin_method("set_cpus", count)
async def set_cpus(self, count) -> int:
print("Setting CPUs")
with open("/home/deck/PowerTools.log", "a") as f:
f.write(f"Setting {count} CPUs to active\n")
f.flush()
async def set_cpus(self, count, smt=True) -> int:
# print("Setting CPUs")
if smt:
count = min(int(count), self.CPU_COUNT)
# never touch cpu0, since it's special
for cpu in range(1, count):
f.write(f"Setting CPU {cpu} to online\n")
enable_cpu(cpu)
for cpu in range(count, self.CPU_COUNT):
f.write(f"Setting CPU {cpu} to offline\n")
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:
online_count = 1 # cpu0 is always online
for cpu in range(1, self.CPU_COUNT):
online_count += int(status_cpu(cpu))
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:
write_to_sys("/sys/devices/system/cpu/cpufreq/boost", int(enabled))
@ -40,6 +48,8 @@ class Plugin:
updated = 0
for cpu in range(0, self.CPU_COUNT):
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)
write_to_sys(path, selected_freq)
updated += 1
@ -47,13 +57,14 @@ class Plugin:
async def get_max_boost(self) -> int:
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)
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
with open("/home/deck/PowerTools.log", "w") as f:
f.write(f"Main loop\n")
pass
@ -64,6 +75,9 @@ def cpu_online_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"
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):
with open(path, mode="w") as f:
@ -84,3 +98,12 @@ def disable_cpu(cpu_number: int):
def status_cpu(cpu_number: int) -> bool:
filepath = cpu_online_path(cpu_number)
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,13 +6,17 @@
<script src="/static/library.js"></script>
<script>
// Python functions
function setCPUs(value) {
return call_plugin_method("set_cpus", {"count":value});
function setCPUs(value, smt) {
return call_plugin_method("set_cpus", {"count":value, "smt": smt});
}
function getCPUs() {
return call_plugin_method("get_cpus", {});
}
function getSMT() {
return call_plugin_method("get_smt", {});
}
function setCPUBoost(value) {
return call_plugin_method("set_boost", {"enabled": value});
@ -35,13 +39,14 @@
async function onReady() {
let boostToggle = document.getElementById("boostToggle");
setToggleState(boostToggle, await getCPUBoost());
setToggleState(document.getElementById("smtToggle"), await getSMT());
selectNotch("cpuThreadsNotch", await getCPUs() - 1, 8);
selectNotch("frequencyNotch", await getMaxBoost(), 3);
}
async function setCPUNotch(index) {
const ROOT_ID = "cpuThreadsNotch";
await setCPUs(index);
await setCPUs(index, getToggleState(document.getElementById("smtToggle")));
selectNotch(ROOT_ID, await getCPUs() - 1, 8);
}
@ -64,7 +69,15 @@
let toggle = document.getElementById("boostToggle");
let isActive = getToggleState(toggle);
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) {
@ -93,6 +106,26 @@
<style type="text/css" media="screen"></style>
</head>
<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 -->
<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">
@ -142,11 +175,11 @@
</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="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_FieldLabel_3jMlJ">
CPU Boost
@ -154,7 +187,8 @@
<div class="gamepaddialog_FieldChildren_2rhav">
<div id="boostToggle" tabindex="0" class="gamepaddialog_Toggle_9Ql-o Focusable" onclick="toggleCPUBoost()">
<div class="gamepaddialog_ToggleRail_2bl0i"></div>
<div class="gamepaddialog_ToggleSwitch_1PQpp"></div>
<div class="gamepaddialog_ToggleSwitch_1PQpp"></div>
</div>
</div>
</div>
</div>
@ -190,6 +224,9 @@
</div>
</div>
</div>
<div style="font-size:x-small;">
WARNING: this will change the CPU governor, affecting battery life until reboot.
</div>
</div>
</body>
</html>