-
Notifications
You must be signed in to change notification settings - Fork 116
/
hotdog.py
executable file
·112 lines (93 loc) · 2.81 KB
/
hotdog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# hotdog: watch the temperature, and when it gets near critical,
# suspend any CPU-intensive process.
import subprocess
import json
import re
import psutil
from pprint import pprint
import sys
import time
def fetch_temps():
temps = []
sensors = psutil.sensors_temperatures()
for thing in sensors:
for piece in sensors[thing]:
tcurrent = piece.current
tmax = piece.high
tcrit = piece.critical
if not tcurrent or (not tmax and not tcrit):
continue
if not tmax:
tmax = tcrit
elif not tcrit:
tcrit = tmax
temps.append((thing, tcurrent, tmax, tcrit))
print(temps)
return temps
# import random
def overtemp(temps):
"""Are any of the temperatures excessive?
"""
# if random.randint(0, 5) == 0:
# return True
for quad in temps:
if quad[1] > quad[2]:
return True
return False
def hoglist(delay=3):
"""Return a list of processes using a nonzero CPU percentage
during the interval specified by delay (seconds),
sorted so the biggest hog is first.
"""
proccesses = list(psutil.process_iter())
for proc in proccesses:
proc.cpu_percent(None) # non-blocking; throw away first bogus value
print("Sleeping ...")
sys.stdout.flush()
time.sleep(delay)
print()
procs = []
for proc in proccesses:
# Even on simple things like name(), psutil may fail with NoSuchProcess
try:
percent = proc.cpu_percent(None)
if percent:
procs.append((proc.name(), percent, proc))
except psutil._exceptions.NoSuchProcess:
continue
procs.sort(key=lambda x: x[1], reverse=True)
return procs
def slowdown(proc):
print("\07\07Suspending process %d, '%s'" % (proc.pid, proc.name()))
proc.suspend()
def check_and_slow(verbose=True):
immune = [ 'Xorg', 'kworker', 'kthread', 'openbox', 'watchdog' ]
temps = fetch_temps()
if verbose:
print("Temps")
for quad in temps:
print("%15s: %f (%f max, %f crit)" % quad)
if overtemp(temps):
print("Yikes! Overtemp!")
hogs = hoglist()
if verbose:
print("Procs")
for p in hogs:
print("%20s: %5.2f" % (p[0], p[1]))
# Slow down anything over 80%;
# if none, then slow down the single biggest disk hog.
if hogs[0][1] > .8:
for h in hogs:
if h[1] > .9:
slowdown(h[2])
else:
break
else:
slowdown(hogs[0][2])
elif verbose:
print("Not overtemp")
if __name__ == '__main__':
while True:
check_and_slow()
time.sleep(3)