File: node-cpu.pl

package info (click to toggle)
libsys-virt-perl 11.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,088 kB
  • sloc: perl: 2,187; sh: 12; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,193 bytes parent folder | download | duplicates (7)
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
#!/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;
}

my ($totcpus, $onlinemask, $nonline) = $hv->get_node_cpu_map();

printf "CPUs total %d, online %d\n", $totcpus, $nonline;

my @bits = split(//, unpack("b*", $onlinemask));
for (my $i = 0 ; $i < $totcpus ; $i++) {
    printf "  %d: %d\n", $i, $bits[$i];
}