-
Notifications
You must be signed in to change notification settings - Fork 13
/
cputemp
executable file
·301 lines (253 loc) · 9 KB
/
cputemp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
# cputemp -- visually monitor CPU temperature
import argparse
from collections import defaultdict
from glob import glob
import math
import os
import signal
import sys
import time
def fround(val):
# Round 0.5 up (Python rounds 0.5 down)
return round(val + 0.1)
def put(s):
print(s, end="", flush=True)
def set_title(text):
print("\033]0;%s\033\\" % text, end="", file=sys.stderr, flush=True)
def set_wname(text):
if os.environ["TERM"].startswith("tmux"):
print("\033k%s\033\\" % text, end="", file=sys.stderr, flush=True)
def get_loadavg():
with open("/proc/loadavg", "r") as fh:
a, b, c, *_ = fh.read().split()
return float(a), float(b), float(c)
def clamp(val, low, high):
return min(max(val, low), high)
def normalize(val, low, high):
return (val - low) / (high - low)
def mix(a, b, t):
return a*(1-t) + b*t
def vec_mix(a, b, t):
assert len(a) == len(b)
return [mix(a[i], b[i], t) for i in range(len(a))]
def vec_gamma_scale(a, gamma):
return [x**gamma for x in a]
def linear_gradient(stops, value, gamma=1.0):
n = 1 / (len(stops) - 1)
ratio = value / n
i = math.floor(ratio)
if i == len(stops) - 1:
return stops[len(stops) - 1]
frac = ratio % 1
stops = [vec_gamma_scale(x, gamma) for x in stops]
result = vec_mix(stops[i], stops[i+1], frac)
return vec_gamma_scale(result, 1/gamma)
def rsuffix(string, old, new):
if string.endswith(old):
return string[:-len(old)] + str(new)
return string
class Sensor:
def __init__(self, dev, subdev):
self.dev = dev
self.subdev = subdev
@property
def devname(self):
with open(f"{self.dev}/name", "r") as fh:
return fh.read().strip()
@property
def label(self):
path = rsuffix(self.subdev, "_input", "_label")
with open(path, "r") as fh:
return fh.read().strip()
def read(self):
with open(self.subdev, "r") as fh:
temp = int(fh.read())
return temp / 1000
@classmethod
def find_hwmon_by_name(cls, name):
for dev in glob("/sys/class/hwmon/hwmon*"):
if os.path.exists("%s/name" % dev):
with open("%s/name" % dev, "r") as fh:
dev_name = fh.read().strip()
if dev_name == "coretemp":
return dev
raise RuntimeError("no %r hwmon device found" % name)
@classmethod
def find_sensor(cls):
dev = cls.find_hwmon_by_name("coretemp")
subdev = None
subdev_pkg = None
subdev_cr0 = None
for path_label in glob("%s/temp*_label" % dev):
path_input = rsuffix(path_label, "_label", "_input")
with open(path_label, "r") as fh:
label = fh.read().strip()
if label == "Package id 0":
subdev_pkg = path_input
elif label == "Core 0":
subdev_cr0 = path_input
if subdev_pkg:
subdev = subdev_pkg
elif subdev_cr0:
subdev = subdev_cr0
else:
raise RuntimeError("no 'coretemp/Core 0' hwmon sensor found")
with open("%s/name" % dev, "r") as fh:
dev_name = fh.read().strip()
with open(rsuffix(subdev, "_input", "_label"), "r") as fh:
subdev_name = fh.read().strip()
return Sensor(dev, subdev)
def print_histogram(data, *, width=5, unit="", barwidth=50):
def buckets(data, width):
res = defaultdict(int)
for k, v in data.items():
lo = k - (k % width)
hi = lo + width
res[lo, hi] += v
return res
total = sum(data.values())
grouped = buckets(data, width)
for (lo, hi), v in sorted(grouped.items()):
percentage = 100 * v / total
width = barwidth * percentage / 100
label = f"[{lo:-2d}, {hi:-2d})"
bar = "=" * round(width)
suffix = f"({v}{unit}, {percentage:.1f}%)"
print(f"{label:9} | {bar} {suffix}")
bar = "-" * barwidth
print(f"{'':9} +-{bar}")
class MeterBar:
def __init__(self, interval=1.0):
# Last values for redraw/overdraw
self.last_ts = 0
self.last_temp = 0
self.last_load = 0
# Statistics
self.interval = interval
self.max_temp = 0
self.time_temps = defaultdict(int)
def _ansicolor_for_temp(self, temp):
low, high = 25, 75
stops = [
(0, 0, 5), # blue
(0, 5, 0), # green
(5, 5, 0), # yellow
(5, 0, 0), # red
]
t = clamp(temp, low, high)
t = normalize(t, low, high)
r, g, b = [round(x) for x in linear_gradient(stops, t, gamma=2.2)]
color = 16 + r*36 + g*6 + b
return f"38;5;{color}"
def _bar(self, width, chars, color):
if width <= 0:
return ""
bar = chars[0] * (width-1) + chars[-1]
if color:
bar = f"\033[{color}m{bar}\033[m"
return bar
def _draw_bar(self, is_current, ts, temp, load):
width = int(temp) - 20
width = clamp(width, 1, 60)
chars = "█" if is_current else "-║"
color = self._ansicolor_for_temp(temp)
put(time.strftime("%T ", time.localtime(ts)))
put(self._bar(width, chars, color))
put(f" {temp:3d}°C")
put(f" \033[2m{load:.2f}\033[m")
print(flush=True)
def overdraw_bar(self):
put("\033[A") # One line up
put("\r\033[K") # Reset position after signal
self._draw_bar(False, self.last_ts, self.last_temp, self.last_load)
def update(self, ts, temp, load):
self._draw_bar(True, ts, temp, load)
# Store values for overdraw
self.last_ts = ts
self.last_temp = temp
self.last_load = load
# Update stats
self.max_temp = max(self.max_temp, temp)
self.time_temps[fround(temp)] += args.interval
def print_stats(self, from_signal=False):
if from_signal:
self.overdraw_bar()
print()
print("Statistics:")
print_histogram(self.time_temps, unit="s")
for _min in [60, 70, 80, 90]:
temp_secs = sum([_time
for (_temp, _time) in self.time_temps.items()
if _temp >= _min])
if temp_secs:
print(f"Spent {temp_secs}s at or above {_min}°C")
max_temp = self.max_temp
max_temp_secs = self.time_temps[max_temp]
print(f"Maximum temperature: {max_temp}°C (for {max_temp_secs}s)")
if from_signal:
print()
self._draw_bar(True, self.last_ts, self.last_temp, self.last_load)
parser = argparse.ArgumentParser()
parser.add_argument("-1", "--once", action="store_true",
help="output current value and exit")
parser.add_argument("-r", "--raw", action="store_true",
help="output only temperature value")
parser.add_argument("-s", "--spark", action="store_true",
help="output through 'spark'")
parser.add_argument("-l", "--load", action="store_true",
help="monitor load average")
parser.add_argument("-W", "--wname", action="store_true",
help="update tmux window name")
parser.add_argument("-n", "--interval", metavar="SECS", type=int, default=1,
help="specify update interval")
args = parser.parse_args()
hostname = os.uname().nodename
sensor = Sensor.find_sensor()
if args.spark:
if args.once:
exit("cputemp: Options --once and --spark are incompatible")
args.raw = True
sys.stdout = os.popen("spark -l 20 -h 70", "w")
if args.raw:
try:
while True:
temp = sensor.read()
load = get_loadavg()[0]
print(f"{load:.2f}" if args.load else f"{temp:.0f}",
flush=True)
set_title(f"{hostname}: {temp:.0f}°C (load {load:.2f})")
if args.wname:
set_wname(f"{load:.2f}" if args.load else f"{temp:.0f}°C")
if args.once:
break
time.sleep(args.interval)
except KeyboardInterrupt:
exit(0)
else:
if args.load:
print("cputemp: Option --load is ignored when not in raw mode",
file=sys.stderr)
print(f"Using sensor {sensor.subdev} ({sensor.devname}:{sensor.label})",
file=sys.stderr)
m = MeterBar(interval=args.interval)
try:
signal.signal(signal.SIGQUIT, lambda s, f: m.print_stats(True))
while True:
if m.last_temp:
m.overdraw_bar()
ts = time.time()
temp = sensor.read()
temp = fround(temp)
load = get_loadavg()[0]
m.update(ts, temp, load)
set_title(f"{hostname}: {temp:.0f}°C (load {load:.2f})")
set_wname(f"{temp:.0f}°C")
if args.once:
break
time.sleep(args.interval)
except RuntimeError as e:
exit(f"error: {e}")
except KeyboardInterrupt:
m.print_stats()
exit(0)