perf / fn.py
aka7774's picture
Update fn.py
97b027f verified
raw
history blame
1.7 kB
import os
import json
import csv
import subprocess
import psutil
def load_model():
pass
def run():
# WSL内で取得するとホストの値とは異なるので使い物にならない
try:
# load average per cpu count
la = round((psutil.getloadavg()[0] / psutil.cpu_count()) * 100)
# cpu
cpu = round(psutil.cpu_percent(percpu=False))
# ram
ram = psutil.virtual_memory()
rama = round(ram.available)
ramt = round(ram.total)
ramp = 100 - ram.percent
ramg = round(ram.available / 1073741824)
# vram
gpu = 0
vrama = 0
vramt = 0
vramp = 0
vramg = 0
cmd = 'nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total --format=csv,noheader,nounits'
csv = subprocess.run(cmd, shell=True, capture_output=True, text=True).stdout
lines = csv.split("\n")
for line in lines:
if not line:
continue
r = line.split(',')
gpu += round(float(r[0]))
vrama += round(float(r[1]))
vramt += round(float(r[2]))
vramp += round(vrama / vramt * 100)
vramg += round(float(r[1]) / 1024)
# network
# 1秒ぶんだけ取りたかった
#net = psutil.net_io_counters()
#ni = round((float(net.bytes_recv) / 1048576) * 100)
#no = round((float(net.bytes_sent) / 1048576) * 100)
res = {
'la': la,
'cpu': cpu,
'ram': ramg,
'gpu': gpu,
'vram': vramg,
}
return res
except Exception as e:
print(e)
return {}