> I agree with your answer.
> As I make some application tests, I just want the CPU usage of a process in 
> particular during its execution. That's why I made "top" and "prstat" 
> statistics. So, I'm looking for the real CPU usage for this process. As top 
> and prstat didn't give the same value (like the sum for vmstat comparison), I 
> didn't know which one i must use.

prstat uses data from micro-state accounting collected per thread so it is 
quite accurate. I don't know what is used by top. I would definitely suggest 
using prstat. 

You can also monitor CPU usage yourself by reading /proc. I attached a little 
Perl script I use for this purpose that can monitor CPU usage for processes.


#!/usr/bin/perl

# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

# ident "@(#)cpuusage.pl        1.2     07/12/04 SMI"

#
# Print CPU usage of given proces or processes
#
# Alexander Kolbasov <Alexander dot Kolbasov at Sun.Com>
#

use warnings;
use strict;
use Getopt::Std;
use File::Basename;
use Time::HiRes qw(usleep);

# Nanoseconds in one second
use constant {
        NS => 1000000000,
        DELAY => 1,
        COUNT => 60
};

our $fname;

sub time2nsec
{
        my ($sec, $nsec) = @_;
        $sec * NS + $nsec;
}

sub get_cpu_usage
{
        my $handle = shift;

        seek ($handle, 0, 0) or die "can't rewind $fname";
        my $prusage = do { local($/); <$handle> } ;

        my (undef, $sec, $nsec, $usec, $unsec, $ssec, $snsec, $trsec,
                $trnsec, $wsec, $wnsec) =
                 unpack("i x[i] (x[Q])3 (LL)4 (x[Q])5 LL", $prusage);

        my $totime = time2nsec($sec, $nsec);
        my $utime = time2nsec($usec, $unsec);
        my $stime = time2nsec($ssec, $snsec);
        my $trtime = time2nsec($trsec, $trnsec);
        my $wtime = time2nsec($wsec, $wnsec);

        return ($totime, $utime, $stime, $trtime, $wtime);
}

sub usage
{
    print STDERR "usage: ", basename(shift, ".pl"),
        " [-c count] [-d delay] pid\n";
    print STDERR "\n  Monitor specified process and print its CPU usage to 
stdout\n\n";
    print STDERR "\t-c: number of data points to produce (60)\n";
    print STDERR "\t-d: delay between data points (1 sec default)\n";
    print STDERR "\t\tdelay is specified in seconds by default unless it ends 
in 'm'\n";
    print STDERR "\t\tin which case it is specified in microseconds.\n";
    exit 1;
}

our ($opt_c, $opt_h, $opt_d);
usage($0) if !getopts("c:d:h") || $opt_h;

my $iterations = $opt_c || COUNT;
my $delay_time = $opt_d || DELAY;
my $units = 's';

if ($opt_d) {
        $delay_time =~ /(\d+)(\w*)/;
        $delay_time = $1;
        $units = $2 if $2;
}

(usage$0, die "invalid units $units\n") unless $units =~ /^[sm]/;

die "invalid delay\n" unless $delay_time;

$| = 1;

my (%tot_o, %utime_o, %stime_o, %trtime_o, %wtime_o);

my %fh;

foreach my $pid (@ARGV)
{
        $fname = "/proc/$pid/usage";
        open $fh{$pid}, "<$fname" or die "can not open $fname";
        ($tot_o{$pid}, $utime_o{$pid}, $stime_o{$pid}, $trtime_o{$pid},
         $wtime_o{$pid}) = get_cpu_usage ($fh{$pid});
}

while ($iterations--) {
        if ($units =~ /^s/) {
                sleep $delay_time;
        } elsif ($units =~ '^m') {
                usleep ($delay_time);
        }

        my $pct = 0;

        foreach my $pid (@ARGV)
          {
                my ($tot_n, $utime_n, $stime_n, $trtime_n, $wtime_n) =
                  get_cpu_usage ($fh{$pid});

                my $runtime += $utime_n + $stime_n + $trtime_n -
                  $utime_o{$pid} - $stime_o{$pid} - $trtime_o{$pid};

                my $total_time = $tot_n - $tot_o{$pid};
                $pct += $runtime / $total_time;

                ($tot_o{$pid}, $utime_o{$pid}, $stime_o{$pid}, $trtime_o{$pid},
                 $wtime_o{$pid}) = ($tot_n, $utime_n, $stime_n, $trtime_n, 
$wtime_n);
        }

        printf ("%g\n", $pct);
}


foreach my $pid (@ARGV)
{
        my $handle = $fh{$pid};
        close $handle;
}

_______________________________________________
perf-discuss mailing list
perf-discuss@opensolaris.org

Reply via email to