Commit 7de2029d authored by Daniel P. Berrange's avatar Daniel P. Berrange

Add example of CPU utilization calc

parent 950b75d2
#!/usr/bin/perl
use strict;
use warnings;
use Sys::Virt;
use Time::HiRes qw(time);
my $addr = @ARGV ? shift @ARGV : "";
my $hv = Sys::Virt->new(address => $addr, readonly => 1);
my $interval = @ARGV ? shift @ARGV : 1;
my $iterations = @ARGV ? shift @ARGV : 1;
my $nodeinfo = $hv->get_node_info();
my $ncpus = $nodeinfo->{cpus};
my @cpuTime;
my $then;
for (my $c = 0 ; $c < $ncpus ; $c++) {
printf "CPU %3d ", $c;
}
print "\n";
for (my $i = 0 ; $i < $iterations ; $i++) {
sleep $interval if $i;
my $now = time;
for (my $c = 0 ; $c < $ncpus ; $c++) {
my $info = $hv->get_node_cpu_stats($c);
my $used = $info->{kernel} + $info->{user};
if (exists $cpuTime[$c]) {
my $cpudelta = $used - $cpuTime[$c];
my $timedelta = ($now - $then) * 1000*1000*1000;
my $util = $cpudelta * 100 / $timedelta;
printf "%03.02f%% ", $util;
}
$cpuTime[$c] = $used;
}
print "\n";
$then = $now;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment