in reply to Calculating percent cpu in linux?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Calculating percent cpu in linux?
by zentara (Cardinal) on May 03, 2005 at 14:55 UTC | |
|
Re^2: Calculating percent cpu in linux?
by suaveant (Parson) on May 04, 2005 at 14:59 UTC |