Skip to content

Instantly share code, notes, and snippets.

@alculquicondor
Last active October 2, 2017 03:22
Show Gist options
  • Save alculquicondor/c5a2378c2a836ffd9d82dfaf72dc6716 to your computer and use it in GitHub Desktop.
Save alculquicondor/c5a2378c2a836ffd9d82dfaf72dc6716 to your computer and use it in GitHub Desktop.
import libvirt
import signal
import sys
import time
stats = []
def signal_handler(signal, frame):
for stat in stats:
print(",".join("%.2f" % x for x in stat))
sys.exit(0)
conn = libvirt.open('qemu:///system')
dom = conn.lookupByName("archlinux")
signal.signal(signal.SIGINT, signal_handler)
start_clock_time = clock_time = time.time()
host_stats = dom.getCPUStats(0)
total_stats = dom.getCPUStats(1)[0]
while True:
n_host_stats = dom.getCPUStats(0)
n_total_stats = dom.getCPUStats(1)[0]
n_clock_time = time.time()
elapsed_time = n_clock_time - clock_time
stat = [
n_clock_time - start_clock_time,
min((n_total_stats['cpu_time'] - total_stats['cpu_time']) / elapsed_time * 1e-7, 200)
]
for i in range(len(host_stats)):
stat.append(min((n_host_stats[i]['cpu_time'] - host_stats[i]['cpu_time']) / elapsed_time * 1e-7, 100))
stats.append(stat)
host_stats = n_host_stats
total_stats = n_total_stats
clock_time = n_clock_time
time.sleep(2e-1)
set xlabel 'seconds'
set ylabel 'CPU usage'
set grid
set term png giant
set output 'graph.png'
set datafile separator ','
plot 'stats.csv' using 1:2 title 'VM' with lines,\
'stats.csv' using 1:3 title 'CPU0' with lines,\
'stats.csv' using 1:4 title 'CPU1' with lines,\
'stats.csv' using 1:5 title 'CPU2' with lines,\
'stats.csv' using 1:6 title 'CPU3' with lines,\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment