Skip to content

Instantly share code, notes, and snippets.

@jhasse
Last active June 17, 2019 16:34
Show Gist options
  • Save jhasse/f6a0a4487a1b19901e8b203a2c57b476 to your computer and use it in GitHub Desktop.
Save jhasse/f6a0a4487a1b19901e8b203a2c57b476 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import subprocess
import time
cputool = '/home/hasse/.local/share/gnome-shell/extensions/cpupower@mko-sl.de/src/cpufreqctl'
turbo_output = subprocess.check_output([cputool, 'turbo', 'get']).decode().strip()
if turbo_output != '0' and turbo_output != '1':
print('Unexpected output: ' + turbo_output)
exit(1)
turbo = turbo_output == '1'
mean_temperature = 80
MHZ_START = 'cpu MHz : '
while True:
with open('/proc/cpuinfo', 'r') as f:
for line in f.readlines():
if line.startswith(MHZ_START):
mhz = float(line[len(MHZ_START):].strip())
break
sensors_output = subprocess.check_output('sensors -j', shell=True)
sensors = json.loads(sensors_output)
mean_temperature *= 0.85
temperature = sensors['coretemp-isa-0000']['Package id 0']['temp1_input']
mean_temperature += temperature * 0.15
print('{}{}°C (current: {}°C) @ {} Mhz\x1b[0m'.format(
'\x1b[1;36m' if turbo else ('\x1b[1;33m' if mhz < 500 else ''),
int(mean_temperature), int(temperature), int(round(mhz)))
)
if turbo:
if mean_temperature > 85:
subprocess.check_call([cputool, 'turbo', '0'])
turbo = False
else:
if mean_temperature < 75:
subprocess.check_call([cputool, 'turbo', '1'])
turbo = True
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment