File: cpustat

package info (click to toggle)
mgm 1.1.cvs.20020221-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,724 kB
  • ctags: 33
  • sloc: perl: 768; makefile: 32
file content (137 lines) | stat: -rw-r--r-- 3,478 bytes parent folder | download | duplicates (2)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*-Perl-*-

# instances allowed: one 

# (single instance modules would be silly to use more than one of
# anyway, so we use package local storage.  This is faster and places
# less artificial load on the machine than doing everything through
# the object hash)

package MGMmodule::cpustat;
use vars qw($numcpus $statcpus $xpath $graph @load @total @prevload 
	    @prevtotal $widget $lastmod);

# class init
sub module_init{
    my$this=shift;
    my$xclass=$this->{"xclass"};

    # how many CPUs? Get this from /proc/cpuinfo then /proc/stat.
    # /proc/stat only sees multiple CPUs as of late 2.1, but we
    # at least want to mark the single bar as SMP under 2.0

    $numcpus=0;
    $statcpus=0;
    if(open(PROC,"</proc/cpuinfo")){
	while(<PROC>){
	    if(m/processor\s*:\s*(\d*)/){
		$numcpus=$1+1 if ($numcpus<$1+1);
	    }
	}
	close PROC;
	# now check /proc/stat to see how many cpus it reports
	$this->read_proc;
    }else{
	$this->{"toplevel"}->optionAdd("$xclass.active",0,21);
    }
    $this->{"toplevel"}->optionAdd("$xclass.order",     1,21);      
    $this;
}

# instance init
sub module_instance{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    return undef if(defined($xpath));
    $xpath=$this->{"xpath"};

    ($minx,$miny)=MGM::Graph::calcxysize($this,100,'%',$statcpus);

    if($numcpus>1 && $statcpus==1){
	$toplevel->optionAdd("$xpath.bar.0.label", "cpus 0-".($numcpus-1),22);
    }else{
	for(my$i=0;$i<$numcpus;$i++){
	    $toplevel->optionAdd("$xpath.bar.$i.label", "cpu$i",22);
	}
    }

    $toplevel->optionAdd("$xpath.scalerefresh", 500,21); # faster
    $toplevel->optionAdd("$xpath.scalewidadj", 100*$statcpus,21);
    $toplevel->optionAdd("$xpath.scalelenadj", 100,21); 

    $toplevel->optionAdd("$xpath.minx",     $minx,21);      
    $toplevel->optionAdd("$xpath.miny",     $miny,21);      

    $this;
}

# instance widget build
sub module_run{
    my($this)=@_;
    $graph=MGM::Graph->new($this,num=>$statcpus,fixed=>'1',
			   prompt=>"%",rangecurrent=>100,rangesetting=>'100');
    $lastmod=-1;
    $widget=$graph->{"widget"};        # must return widget
}

sub module_update{ 
    my$this=shift;
    
    if($lastmod!=$MGMmodule::helperST::lastmod){
	# don't update unless the helper has
	$lastmod=$MGMmodule::helperST::lastmod;

	$this->read_proc;
	
	if(defined(@prevload)){
	    my@vals;
	    
	    if($statcpus==1){
		my$tot=$total[0]-$prevtotal[0];
		if($tot>0){
		    $vals[0]=100*($load[0]-$prevload[0])/$tot;
		    $vals[0]=100 if $vals[0]>100;
		}else{
		    $vals[0]=0;
		}
	    }else{
		for(my$i=0;$i<$numcpus;$i++){
		    my$use=$i+1;
		    my$tot=$total[$use]-$prevtotal[$use];
		    if($tot>0){
			$vals[$i]=100*($load[$use]-$prevload[$use])/$tot;
			$vals[$i]=100 if $vals[$i]>100;
		    }else{
			$vals[$i]=0;
		    }
		}
	    }
	    $graph->set(@vals);
	}
    
	@prevload=@load;
	@prevtotal=@total;
    }
}
    
sub read_proc{
    # now uses the 00helper to save on opens 
    
    # in late 2.1+ kernels, each CPU has an entry in /proc/stat along with
    # a common entry.  Earlier, only the common entry.
    
    # collective cpu entry
    $MGMmodule::helperST::proc{"cpu"}=~m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
    $load[0]=$1+$2+$3;
    $total[0]=$load[0]+$4;
    
    for(my$i=0;defined(my$line=$MGMmodule::helperST::proc{"cpu$i"});$i++){
	$line=~m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
	# per-cpu entry
	$total[$i+1]=($load[$i+1]=$1+$2+$3)+$4;
	$statcpus=$i+1 if($i+1>$statcpus);
    }	
}

bless {};