cpumon_light reimplementation to avoid fork
[debian6500.git] / master / root / bin / cpumon_light
CommitLineData
8d6c9e9b 1#!/usr/bin/perl
4aba78da 2
8d6c9e9b 3use strict;
4aba78da 4
8d6c9e9b
TPVT
5my @cpu_stat_t0 = proc_stat();
6sleep 1;
7my @cpu_stat_t1 = proc_stat();
8my $total = 0;
9for (my $cpu_i = 0; $cpu_i < (scalar @cpu_stat_t0); $cpu_i++) {
10 my @stat_t0 = split /\s+/, $cpu_stat_t0[$cpu_i];
11 my @stat_t1 = split /\s+/, $cpu_stat_t1[$cpu_i];
12 my ($t0_idle, $t1_idle) = ($stat_t0[3], $stat_t1[3]);
13 my ($t0_total, $t1_total) = (0, 0);
14 $t0_total += $_ foreach @stat_t0;
15 $t1_total += $_ foreach @stat_t1;
16
17 my $load = ($t1_total - $t0_total == 0) ? 100:
18 int((100 * (($t1_total - $t0_total) - ($t1_idle - $t0_idle))/($t1_total - $t0_total)) + 0.5);
19 print " +" unless ($cpu_i == 0);
20 printf "% 5.1f%% ", $load;
21 $total += $load;
22}
23printf " = % 6.1f%%\n", $total;
24
25sub proc_stat() {
26 return grep {s/^cpu\d+\s//} read_file('/proc/stat');
4aba78da 27}
8d6c9e9b
TPVT
28
29sub read_file($) {
30 my ($file) = @_;
31 open FILE, "<$file";
32 my @content = <FILE>;
33 close FILE;
34 return @content;
35}
36