Debian Codegraph @

examples/node-cpu.pl

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Sys::Virt;
7 use Time::HiRes qw(time);
8
9 my $addr = @ARGV ? shift @ARGV : "";
10
11 my $hv = Sys::Virt->new(address => $addr, readonly => 1);
12
13 my $interval = @ARGV ? shift @ARGV : 1;
14 my $iterations = @ARGV ? shift @ARGV : 1;
15
16 my $nodeinfo = $hv->get_node_info();
17
18 my $ncpus = $nodeinfo->{cpus};
19
20 my @cpuTime;
21 my $then;
22
23 for (my $c = 0 ; $c < $ncpus ; $c++) {
24 printf "CPU %3d ", $c;
25 }
26 print "\n";
27
28 for (my $i = 0 ; $i < $iterations ; $i++) {
29 sleep $interval if $i;
30
31 my $now = time;
32
33 for (my $c = 0 ; $c < $ncpus ; $c++) {
34 my $info = $hv->get_node_cpu_stats($c);
35
36 my $used = $info->{kernel} + $info->{user};
37 if (exists $cpuTime[$c]) {
38 my $cpudelta = $used - $cpuTime[$c];
39 my $timedelta = ($now - $then) * 1000*1000*1000;
40 my $util = $cpudelta * 100 / $timedelta;
41
42 printf "%03.02f%% ", $util;
43 }
44 $cpuTime[$c] = $used;
45 }
46 print "\n";
47
48 $then = $now;
49 }
50
51 my ($totcpus, $onlinemask, $nonline) = $hv->get_node_cpu_map();
52
53 printf "CPUs total %d, online %d\n", $totcpus, $nonline;
54
55 my @bits = split(//, unpack("b*", $onlinemask));
56 for (my $i = 0 ; $i < $totcpus ; $i++) {
57 printf " %d: %d\n", $i, $bits[$i];
58 }