proc_stats: add notify_proc_stat_update websocket notifcation
Report CPU temp in addition to moonraker stats in this notification and the proc_stat request. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
5321a8a558
commit
f705f1145a
|
@ -14,6 +14,7 @@ from tornado.locks import Lock
|
||||||
|
|
||||||
VC_GEN_CMD_FILE = "/usr/bin/vcgencmd"
|
VC_GEN_CMD_FILE = "/usr/bin/vcgencmd"
|
||||||
STATM_FILE_PATH = "/proc/self/smaps_rollup"
|
STATM_FILE_PATH = "/proc/self/smaps_rollup"
|
||||||
|
TEMPERATURE_PATH = "/sys/class/thermal/thermal_zone0/temp"
|
||||||
STAT_UPDATE_TIME_MS = 1000
|
STAT_UPDATE_TIME_MS = 1000
|
||||||
REPORT_QUEUE_SIZE = 30
|
REPORT_QUEUE_SIZE = 30
|
||||||
THROTTLE_CHECK_INTERVAL = 10
|
THROTTLE_CHECK_INTERVAL = 10
|
||||||
|
@ -45,11 +46,13 @@ class ProcStats:
|
||||||
else:
|
else:
|
||||||
logging.info("Unable to find 'vcgencmd', throttle checking "
|
logging.info("Unable to find 'vcgencmd', throttle checking "
|
||||||
"disabled")
|
"disabled")
|
||||||
|
self.temp_file = pathlib.Path(TEMPERATURE_PATH)
|
||||||
self.smaps = pathlib.Path(STATM_FILE_PATH)
|
self.smaps = pathlib.Path(STATM_FILE_PATH)
|
||||||
self.server.register_endpoint(
|
self.server.register_endpoint(
|
||||||
"/machine/proc_stats", ["GET"], self._handle_stat_request)
|
"/machine/proc_stats", ["GET"], self._handle_stat_request)
|
||||||
self.server.register_event_handler(
|
self.server.register_event_handler(
|
||||||
"server:klippy_shutdown", self._handle_shutdown)
|
"server:klippy_shutdown", self._handle_shutdown)
|
||||||
|
self.server.register_notification("proc_stats:proc_stat_update")
|
||||||
self.proc_stat_queue = deque(maxlen=30)
|
self.proc_stat_queue = deque(maxlen=30)
|
||||||
self.last_update_time = time.time()
|
self.last_update_time = time.time()
|
||||||
self.last_proc_time = time.process_time()
|
self.last_proc_time = time.process_time()
|
||||||
|
@ -64,13 +67,15 @@ class ProcStats:
|
||||||
ts = await self._check_throttled_state()
|
ts = await self._check_throttled_state()
|
||||||
return {
|
return {
|
||||||
'moonraker_stats': list(self.proc_stat_queue),
|
'moonraker_stats': list(self.proc_stat_queue),
|
||||||
'throttled_state': ts
|
'throttled_state': ts,
|
||||||
|
'cpu_temp': self._get_cpu_temperature()
|
||||||
}
|
}
|
||||||
|
|
||||||
async def _handle_shutdown(self):
|
async def _handle_shutdown(self):
|
||||||
msg = "\nMoonraker System Usage Statistics:"
|
msg = "\nMoonraker System Usage Statistics:"
|
||||||
for stats in self.proc_stat_queue:
|
for stats in self.proc_stat_queue:
|
||||||
msg += f"\n{self._format_stats(stats)}"
|
msg += f"\n{self._format_stats(stats)}"
|
||||||
|
msg += f"\nCPU Temperature: {self._get_cpu_temperature()}"
|
||||||
logging.info(msg)
|
logging.info(msg)
|
||||||
if self.vcgencmd is not None:
|
if self.vcgencmd is not None:
|
||||||
ts = await self._check_throttled_state()
|
ts = await self._check_throttled_state()
|
||||||
|
@ -82,11 +87,17 @@ class ProcStats:
|
||||||
time_diff = update_time - self.last_update_time
|
time_diff = update_time - self.last_update_time
|
||||||
usage = round((proc_time - self.last_proc_time) / time_diff * 100, 2)
|
usage = round((proc_time - self.last_proc_time) / time_diff * 100, 2)
|
||||||
mem, mem_units = self._get_memory_usage()
|
mem, mem_units = self._get_memory_usage()
|
||||||
self.proc_stat_queue.append({
|
cpu_temp = self._get_cpu_temperature()
|
||||||
|
result = {
|
||||||
"time": update_time,
|
"time": update_time,
|
||||||
"cpu_usage": usage,
|
"cpu_usage": usage,
|
||||||
"memory": mem,
|
"memory": mem,
|
||||||
"mem_units": mem_units
|
"mem_units": mem_units,
|
||||||
|
}
|
||||||
|
self.proc_stat_queue.append(result)
|
||||||
|
self.server.send_event("proc_stats:proc_stat_update", {
|
||||||
|
'moonraker_stats': result,
|
||||||
|
'cpu_temp': cpu_temp
|
||||||
})
|
})
|
||||||
self.last_update_time = update_time
|
self.last_update_time = update_time
|
||||||
self.last_proc_time = proc_time
|
self.last_proc_time = proc_time
|
||||||
|
@ -130,6 +141,17 @@ class ProcStats:
|
||||||
return None, None
|
return None, None
|
||||||
return mem, units
|
return mem, units
|
||||||
|
|
||||||
|
def _get_cpu_temperature(self):
|
||||||
|
temp = None
|
||||||
|
if self.temp_file.exists():
|
||||||
|
try:
|
||||||
|
temp = int(self.temp_file.read_text().strip())
|
||||||
|
temp = temp / 1000.
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
return temp
|
||||||
|
|
||||||
|
|
||||||
def _format_stats(self, stats):
|
def _format_stats(self, stats):
|
||||||
return f"System Time: {stats['time']:2f}, " \
|
return f"System Time: {stats['time']:2f}, " \
|
||||||
f"Usage: {stats['cpu_usage']}%, " \
|
f"Usage: {stats['cpu_usage']}%, " \
|
||||||
|
|
Loading…
Reference in New Issue