add cpu usage

This commit is contained in:
Jakob Ketterl 2019-05-05 17:34:40 +02:00
parent 854ac6d5f1
commit 0da62dad82
4 changed files with 70 additions and 3 deletions

View File

@ -24,7 +24,6 @@
<title>OpenWebRX | Open Source SDR Web App for Everyone!</title>
<!-- <script type="text/javascript">
//Global variables
var audio_buffering_fill_to=%[AUDIO_BUFSIZE];
var server_enable_digimodes=%[DIGIMODES_ENABLE];
var mathbox_waterfall_frequency_resolution=%[MATHBOX_WATERFALL_FRES];
var mathbox_waterfall_history_length=%[MATHBOX_WATERFALL_THIST];

View File

@ -1185,8 +1185,12 @@ function on_ws_recv(evt)
case "smeter":
setSmeterAbsoluteValue(json.value);
break;
case "cpuusage":
var server_cpu_usage = json.value;
progressbar_set(e("openwebrx-bar-server-cpu"),server_cpu_usage/100,"Server CPU [" + server_cpu_usage + "%]",server_cpu_usage>85);
break;
default:
console.warn('received message of unknown type', json);
console.warn('received message of unknown type: ' + json.type);
}
} catch (e) {
// don't lose exception

View File

@ -1,7 +1,7 @@
import mimetypes
from owrx.websocket import WebSocketConnection
from owrx.config import PropertyManager
from owrx.source import SpectrumThread, DspManager
from owrx.source import SpectrumThread, DspManager, CpuUsageThread
import json
class Controller(object):
@ -55,6 +55,8 @@ class SpectrumForwarder(object):
self.conn.send(bytes([0x02]) + data)
def write_s_meter_level(self, level):
self.conn.send({"type":"smeter","value":level})
def write_cpu_usage(self, usage):
self.conn.send({"type":"cpuusage","value":usage})
class WebSocketMessageHandler(object):
def __init__(self):
@ -78,6 +80,7 @@ class WebSocketMessageHandler(object):
self.forwarder = SpectrumForwarder(conn)
SpectrumThread.getSharedInstance().add_client(self.forwarder)
CpuUsageThread.getSharedInstance().add_client(self.forwarder)
self.dsp = DspManager(self.forwarder)
@ -101,6 +104,7 @@ class WebSocketMessageHandler(object):
def handleClose(self, conn):
if self.forwarder:
SpectrumThread.getSharedInstance().remove_client(self.forwarder)
CpuUsageThread.getSharedInstance().remove_client(self.forwarder)
if self.dsp:
self.dsp.stop()

View File

@ -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