use strict; require "asm/param.ph"; open UPTIME, "</proc/uptime"; my @up = split('\s+', <UPTIME>); close UPTIME; my $pid = shift or die "need pid"; open STAT, "</proc/$pid/stat"; my @stat = split('\s+', <STAT>); close STAT; my $total_time = $stat[13] + $stat[14]; my $include_dead_children = 1; if ($include_dead_children) { $total_time += $stat[15] + $stat[16]; } print $stat[21], "\n"; my $start_time = $stat[21]; my $seconds = $up[0] - $start_time / HZ(); my $pcpu; if($seconds) { $pcpu = ($total_time * 100 / HZ()) / $seconds; } print "start_time $start_time\n"; printf("%%CPU %2.2g\n", $pcpu);
Update: fix to %CPU in printf per zentara.
In reply to Re: Calculating percent cpu in linux?
by gam3
in thread Calculating percent cpu in linux?
by suaveant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |