Skip to content

Instantly share code, notes, and snippets.

@rhizoome
Last active May 20, 2016 12:26
Show Gist options
  • Save rhizoome/efd43362ad259c14ee1c18ce51efb5c9 to your computer and use it in GitHub Desktop.
Save rhizoome/efd43362ad259c14ee1c18ce51efb5c9 to your computer and use it in GitHub Desktop.
Powertop daemon (collects udev and suspend events and calls powertop --auto-tune 15s later max all 120s)
[Unit]
Description=Powertop daemon
After=sysinit.target
[Service]
Type=simple
ExecStart=/usr/local/bin/powertop.py
KillSignal=SIGINT
[Install]
WantedBy=sysinit.target
[Unit]
Description=Powertop trigger
After=suspend.target
After=hibernate.target
After=hybrid-sleep.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c "echo x > /var/run/powertop.fifo"
[Install]
WantedBy=multi-user.target
WantedBy=suspend.target
WantedBy=hibernate.target
WantedBy=hybrid-sleep.target
ACTION=="add", RUN+="/usr/local/bin/powertrigger.sh"
# If neeeded this disables power-save for all mouse devices and their parent pci devices
def fix_mouse():
mouse_devs = []
mouse_devs.extend(glob.glob(
"/sys/class/input/mouse*/power/control"
))
mouse_devs.extend(glob.glob(
"/sys/class/input/mouse*/device/device/power/control"
))
for dev in mouse_devs:
print("echo on > %s" % dev)
with open(dev, "w") as f:
f.write("on")
pci_devs = glob.glob("/dev/input/mouse*")
for dev in pci_devs:
info = subprocess.check_output([
"udevadm",
"info",
dev
])
for line in info.split(os.linesep):
if line.startswith("P: /devices/pci"):
pci_dev = line.split("/")[3]
dev = "/sys/bus/pci/devices/%s/power/control" % pci_dev
print("echo on > %s" % dev)
with open(dev, "w") as f:
f.write("on")
continue
#!/usr/bin/env python
import datetime
import os
import time
import threading
import sys
import subprocess
os.environ["TERM"] = "dumb"
FIFO = "/var/run/powertop.fifo"
try:
os.unlink(FIFO)
§except OSError:
pass
os.mkfifo(FIFO)
before = datetime.datetime.min
def run():
time.sleep(15)
try:
print(subprocess.check_output([
"/usr/sbin/powertop",
"--auto-tune",
], stderr=subprocess.STDOUT))
except subprocess.CalledProcessError as e:
print(e.output)
sys.stdout.flush()
try:
while True:
with open(FIFO, "r") as f:
for line in f.readlines():
force = line.strip() == "x"
now = datetime.datetime.now()
if now - before > datetime.timedelta(minutes=2) or force:
if force:
print("forced")
threading.Thread(target=run).start()
before = now
sys.stdout.flush()
finally:
os.unlink(FIFO)
os.system("touch %s" % FIFO)
sys.exit(0)
#!/usr/bin/env bash
echo . > /var/run/powertop.fifo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment