add cpu usage
This commit is contained in:
@ -2,6 +2,7 @@ import subprocess
|
||||
from owrx.config import PropertyManager
|
||||
import threading
|
||||
import csdr
|
||||
import time
|
||||
|
||||
class RtlNmuxSource(object):
|
||||
def __init__(self):
|
||||
@ -157,3 +158,62 @@ class DspManager(object):
|
||||
|
||||
def setProperty(self, prop, value):
|
||||
self.localProps.getProperty(prop).setValue(value)
|
||||
|
||||
class CpuUsageThread(threading.Thread):
|
||||
sharedInstance = None
|
||||
@staticmethod
|
||||
def getSharedInstance():
|
||||
if CpuUsageThread.sharedInstance is None:
|
||||
CpuUsageThread.sharedInstance = CpuUsageThread()
|
||||
CpuUsageThread.sharedInstance.start()
|
||||
return CpuUsageThread.sharedInstance
|
||||
|
||||
def __init__(self):
|
||||
self.clients = []
|
||||
self.doRun = True
|
||||
self.last_worktime = 0
|
||||
self.last_idletime = 0
|
||||
super().__init__()
|
||||
|
||||
def run(self):
|
||||
while self.doRun:
|
||||
time.sleep(3)
|
||||
try:
|
||||
cpu_usage = self.get_cpu_usage()
|
||||
except:
|
||||
cpu_usage = 0
|
||||
for c in self.clients:
|
||||
c.write_cpu_usage(cpu_usage)
|
||||
print("cpu usage thread shut down")
|
||||
|
||||
def get_cpu_usage(self):
|
||||
try:
|
||||
f = open("/proc/stat","r")
|
||||
except:
|
||||
return 0 #Workaround, possibly we're on a Mac
|
||||
line = ""
|
||||
while not "cpu " in line: line=f.readline()
|
||||
f.close()
|
||||
spl = line.split(" ")
|
||||
worktime = int(spl[2]) + int(spl[3]) + int(spl[4])
|
||||
idletime = int(spl[5])
|
||||
dworktime = (worktime - self.last_worktime)
|
||||
didletime = (idletime - self.last_idletime)
|
||||
rate = float(dworktime) / (didletime+dworktime)
|
||||
self.last_worktime = worktime
|
||||
self.last_idletime = idletime
|
||||
if (self.last_worktime==0): return 0
|
||||
return rate
|
||||
|
||||
def add_client(self, c):
|
||||
self.clients.append(c)
|
||||
|
||||
def remove_client(self, c):
|
||||
self.clients.remove(c)
|
||||
if not self.clients:
|
||||
self.shutdown()
|
||||
|
||||
def shutdown(self):
|
||||
print("shutting down cpu usage thread")
|
||||
CpuUsageThread.sharedInstance = None
|
||||
self.doRun = False
|
||||
|
Reference in New Issue
Block a user