forked from DMOJ/judge-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysinfo.py
49 lines (38 loc) · 1.4 KB
/
sysinfo.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
import os
from multiprocessing import cpu_count
_cpu_count = cpu_count()
if hasattr(os, 'getloadavg'):
def load_fair():
try:
load = os.getloadavg()[0] / _cpu_count
except OSError: # as of May 2016, Windows' Linux subsystem throws OSError on getloadavg
load = -1
return 'load', load
else: # pragma: no cover
from dmoj.utils.winperfmon import PerformanceCounter
from threading import Thread
from collections import deque
from time import sleep
class SystemLoadThread(Thread):
def __init__(self):
super(SystemLoadThread, self).__init__()
self.daemon = True
self.samples = deque(maxlen=10)
self.load = 0.5
self.counter = PerformanceCounter(r'\System\Processor Queue Length', r'\Processor(_Total)\% Processor Time')
def run(self):
while True:
pql, pt = self.counter.query()
self.samples.append(pql)
if pt >= 100:
self.load = max(sum(self.samples) / len(self.samples) / _cpu_count, pt / 100.)
else:
self.load = pt / 100.
sleep(1)
_load_thread = SystemLoadThread()
_load_thread.start()
def load_fair():
return 'load', _load_thread.load
def cpu_count():
return 'cpu-count', _cpu_count
report_callbacks = [load_fair, cpu_count]