File size: 2,948 Bytes
400839c |
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 |
#!/usr/bin/env python
from __future__ import annotations
import collections
import gradio as gr
import nvitop
import pandas as pd
import plotly.express as px
import psutil
class SystemMonitor:
MAX_SIZE = 61
def __init__(self):
self.devices = nvitop.Device.all()
self.cpu_memory_usage = collections.deque(
[0 for _ in range(self.MAX_SIZE)], maxlen=self.MAX_SIZE)
self.cpu_memory_usage_str = ''
self.gpu_memory_usage = collections.deque(
[0 for _ in range(self.MAX_SIZE)], maxlen=self.MAX_SIZE)
self.gpu_util = collections.deque([0 for _ in range(self.MAX_SIZE)],
maxlen=self.MAX_SIZE)
self.gpu_memory_usage_str = ''
self.gpu_util_str = ''
def update(self) -> None:
self.update_cpu()
self.update_gpu()
def update_cpu(self) -> None:
memory = psutil.virtual_memory()
self.cpu_memory_usage.append(memory.percent)
self.cpu_memory_usage_str = f'{memory.used / 1024**3:0.2f}GiB / {memory.total / 1024**3:0.2f}GiB ({memory.percent}%)'
def update_gpu(self) -> None:
if not self.devices:
return
device = self.devices[0]
self.gpu_memory_usage.append(device.memory_percent())
self.gpu_util.append(device.gpu_utilization())
self.gpu_memory_usage_str = f'{device.memory_usage()} ({device.memory_percent()}%)'
self.gpu_util_str = f'{device.gpu_utilization()}%'
def get_json(self) -> dict[str, str]:
return {
'CPU memory usage': self.cpu_memory_usage_str,
'GPU memory usage': self.gpu_memory_usage_str,
'GPU Util': self.gpu_util_str,
}
def get_graph_data(self) -> dict[str, list[int | float]]:
return {
'index': list(range(-self.MAX_SIZE + 1, 1)),
'CPU memory usage': self.cpu_memory_usage,
'GPU memory usage': self.gpu_memory_usage,
'GPU Util': self.gpu_util,
}
def get_graph(self):
df = pd.DataFrame(self.get_graph_data())
return px.line(df,
x='index',
y=[
'CPU memory usage',
'GPU memory usage',
'GPU Util',
],
range_y=[-5,
105]).update_layout(xaxis_title='Time',
yaxis_title='Percentage')
def create_monitor_demo() -> gr.Blocks:
monitor = SystemMonitor()
with gr.Blocks() as demo:
gr.JSON(value=monitor.update, every=1, visible=False)
gr.JSON(value=monitor.get_json, show_label=False, every=1)
gr.Plot(value=monitor.get_graph, show_label=False, every=1)
return demo
if __name__ == '__main__':
demo = create_monitor_demo()
demo.queue(api_open=False).launch()
|