Add basic CPU count selection functionality
This commit is contained in:
parent
de7e356ea4
commit
c93f2a8569
3 changed files with 118 additions and 14 deletions
60
main.py
60
main.py
|
@ -1,12 +1,58 @@
|
||||||
class Plugin:
|
import time
|
||||||
# A normal method. It can be called from JavaScript using call_plugin_function("method_1", argument1, argument2)
|
|
||||||
async def method_1(self, *args):
|
|
||||||
pass
|
|
||||||
|
|
||||||
# A normal method. It can be called from JavaScript using call_plugin_function("method_2", argument1, argument2)
|
class Plugin:
|
||||||
async def method_2(self, *args):
|
CPU_COUNT = 8
|
||||||
pass
|
|
||||||
|
# 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()
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
|
|
||||||
|
# these are stateless (well, the state is not saved internally) functions, so there's no need for these to be called like a class method
|
||||||
|
|
||||||
|
def cpu_online_path(cpu_number: int) -> str:
|
||||||
|
return f"/sys/devices/system/cpu/cpu{cpu_number}/online"
|
||||||
|
|
||||||
|
def write_to_sys(path, value: int):
|
||||||
|
with open(path, mode="w") as f:
|
||||||
|
f.write(str(value))
|
||||||
|
|
||||||
|
def read_from_sys(path):
|
||||||
|
with open(path, mode="r") as f:
|
||||||
|
return f.read(1)
|
||||||
|
|
||||||
|
def enable_cpu(cpu_number: int):
|
||||||
|
filepath = cpu_online_path(cpu_number)
|
||||||
|
write_to_sys(filepath, 1)
|
||||||
|
|
||||||
|
def disable_cpu(cpu_number: int):
|
||||||
|
filepath = cpu_online_path(cpu_number)
|
||||||
|
write_to_sys(filepath, 0)
|
||||||
|
|
||||||
|
def status_cpu(cpu_number: int) -> bool:
|
||||||
|
filepath = cpu_online_path(cpu_number)
|
||||||
|
return read_from_sys(filepath) == "1"
|
||||||
|
|
|
@ -4,8 +4,66 @@
|
||||||
<link rel="stylesheet" href="/steam_resource/css/39.css">
|
<link rel="stylesheet" href="/steam_resource/css/39.css">
|
||||||
<link rel="stylesheet" href="/steam_resource/css/library.css">
|
<link rel="stylesheet" href="/steam_resource/css/library.css">
|
||||||
<script src="/static/library.js"></script>
|
<script src="/static/library.js"></script>
|
||||||
|
<script>
|
||||||
|
// Python functions
|
||||||
|
function setCPUs(value) {
|
||||||
|
return call_plugin_method("set_cpus", {"count":value});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCPUs() {
|
||||||
|
return call_plugin_method("get_cpus", {});
|
||||||
|
}
|
||||||
|
|
||||||
|
// other logic
|
||||||
|
|
||||||
|
async function onSetCPUs() {
|
||||||
|
let target = document.getElementById("cpu_threads");
|
||||||
|
let ok = await setCPUs(target.value);
|
||||||
|
target.value = await getCPUs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onReady() {
|
||||||
|
document.getElementById("cpu_threads").value = await getCPUs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function decrementCPUs() {
|
||||||
|
let target = document.getElementById("cpu_threads");
|
||||||
|
if (target.value >= 2) {
|
||||||
|
target.value -= 1;
|
||||||
|
}
|
||||||
|
await onSetCPUs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function incrementCPUs() {
|
||||||
|
let target = document.getElementById("cpu_threads");
|
||||||
|
if (target.value <= 7) {
|
||||||
|
target.value += 1;
|
||||||
|
}
|
||||||
|
await onSetCPUs();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resetCPUs() {
|
||||||
|
let target = document.getElementById("cpu_threads");
|
||||||
|
target.value = 8;
|
||||||
|
await onSetCPUs();
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
<style type="text/css" media="screen">
|
||||||
|
input::-webkit-outer-spin-button,
|
||||||
|
input::-webkit-inner-spin-button {
|
||||||
|
-webkit-appearance: none;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body onload="onReady()">
|
||||||
<h2>Hello World</h2>
|
<div>
|
||||||
|
<label for="cpu_threads">Online CPUs</label>
|
||||||
|
<input type="number" id="cpu_threads" name="cpu_threads" min="1" max="8">
|
||||||
|
<button type="button" onclick="incrementCPUs()">/\</button>
|
||||||
|
<button type="button" onclick="decrementCPUs()">\/</button>
|
||||||
|
<button type="button" onclick="resetCPUs()">Reset</button>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "Plugin name",
|
"name": "Power Tools",
|
||||||
"author": "Plugin author",
|
"author": "NGnius",
|
||||||
"main_view_html": "main_view.html",
|
"main_view_html": "main_view.html",
|
||||||
"tile_view_html": "",
|
"tile_view_html": "",
|
||||||
"flags": ["hot_reload", "root"]
|
"flags": ["root", "_debug"]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue