How to get %CPU Utilization for performance testing using /proc in Solaris 5.6

  • Follow


Hi All,

I have a problem. I have to measure the performance of a network layer
in Solaris 5.6. For this my layer is run for a long time and i have to
find out that at what %CPU utilization it was running.

I wrote a small program to find %CPU of any process. In that i take
input PID of a dummy heavy [hogger] process. Then i read the file
/proc/PID/psinfo and i store the whole structure of psinfo_t in ps1.
After one second, i again read the same structure into ps2. Now i
calculate the total CPU time used by this process during the last one
second as:


uses1=(ps2.pr_time.tv_sec-ps1.pr_time.tv_sec)+(ps2.pr_ctime.tv_sec-ps1.pr_ctime.tv_sec);


uses2=1.0*((ps2.pr_time.tv_nsec-ps1.pr_time.tv_nsec)+(ps2.pr_ctime.tv_nsec-

      ps1.pr_ctime.tv_nsec) )/1000000000;

total = uses1+use2 ;

So, the %CPU utilization for the last second was total * 100;

I tried to match this results with 'top' command [ for 1 second
interval ], but %CPU utilization reported by both [ my program and
'top' ] are quite different. [ e.g. My program reports 97% while 'top'
reports 45%].

The same program [ with little modification, such as reading from
/proc/PID/stat and the file is in ascii format], i wrote for Linux and
the results are almost accurate.

Can anyone please help me out.
Also, how 'top' reports %CPU utilization in Solaris.

Bye

0
Reply anil.padia (1) 3/30/2006 7:37:56 AM

Anil <anil.padia@gmail.com> wrote:

> I tried to match this results with 'top' command [ for 1 second
> interval ], but %CPU utilization reported by both [ my program and
> 'top' ] are quite different. [ e.g. My program reports 97% while 'top'
> reports 45%].

How many processors do you have?  

The figure returned by 'top' (and 'ps') is sort of a weighted average
over several seconds.  It will read too low for a recently started
process.  It should reach close to the steady-state value in about a
minute or so.

> The same program [ with little modification, such as reading from
> /proc/PID/stat and the file is in ascii format], i wrote for Linux and
> the results are almost accurate.

> Can anyone please help me out.
> Also, how 'top' reports %CPU utilization in Solaris.

It and ps are just reading a struct associated with the process that is
maintained by the kernel.  

-- 
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >
0
Reply Darren 3/30/2006 6:53:12 PM


Thanks for responding.

It is a Uniprocessor machine.
I suppose  the strategy as how to calculate %CPU by 'top' differs in
Linux and Solaris, because as i stated, my program is running fine in
Linux [ i.e. I think in Linux, 'top' reports the average usage in the
last second ].
I also think, in Solaris the figure returned by 'top' (and 'ps') is
sort of a weighted average over several seconds [ I don't know how many
seconds, but if you can tell me that, it would be a great help] because
the %CPU reading for my hogger program [ when i start it ] increases
very slowly in Solaris, where as in Linux, it increases
instantaneously.

I also went through the code of 'ps' for Solaris. It just reads
pr_pctcpu filed of psinfo_t structure of /proc/PID/psinfo file and
reports it as      %CPU [ when 'ps' is executed with -o pcpu, it
reports %CPU ], 'top' should also have been using the same technique.
Then kernel should have been responsible for calculating and then
setting the pr_pctcpu field.

Then, does my strategy to calculate %CPU [ suppose my performance test
is run for delta T, then the average CPU utilization was        (total
/ delta T ) * 100 ], is wrong in Solaris ???

Bye

Anil Padia

0
Reply Anil 3/31/2006 4:35:45 AM

Anil <anil.padia@gmail.com> wrote:
> I also think, in Solaris the figure returned by 'top' (and 'ps') is
> sort of a weighted average over several seconds [ I don't know how many
> seconds, but if you can tell me that, it would be a great help]

"sort of" is correct.  It's not really an average in the normal sense.
It's a logarithmic permutation of the current figure.  For instance if
it were a 60 second average, then a CPU hog would start at 0, and over
the course of a minute would proceed linearly to 100.  Instead it jumps
rapidly at first and then takes quite a bit longer to tail toward 100.  

For a steady-state process, the figure will be pretty close after about
a minute.

If you want to see something faster, use 'prstat -m'.  It'll show you
the instant CPU figure, and not the field kept by the kernel.

> because
> the %CPU reading for my hogger program [ when i start it ] increases
> very slowly in Solaris, where as in Linux, it increases
> instantaneously.

'psrstat -m'.

> I also went through the code of 'ps' for Solaris. It just reads
> pr_pctcpu filed of psinfo_t structure of /proc/PID/psinfo file and
> reports it as      %CPU [ when 'ps' is executed with -o pcpu, it
> reports %CPU ], 'top' should also have been using the same technique.
> Then kernel should have been responsible for calculating and then
> setting the pr_pctcpu field.

Yes.

> Then, does my strategy to calculate %CPU [ suppose my performance test
> is run for delta T, then the average CPU utilization was        (total
> / delta T ) * 100 ], is wrong in Solaris ???

Wrong how?  It may not be what top reports, but then top (on solaris) is
not really reporting an average.

Do you want an average?

-- 
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >
0
Reply Darren 3/31/2006 6:24:09 AM

I really require the avearge  %CPU utilization over the time for which
the performance testing was run. From the discussion, i can draw the
conclusion that techniques used by 'top' of Solaris and Linux to report
the figures are different and that what Linux's 'top' reports is
nothing but the average utilization of the last second.

My Final Qn for this topic: Do you also approve my technique of getting
average %CPU utilization [ both for Linux and Solaris] which is
calculated as below:
Assuming the test is run for time T
 Average %CPU = ( total / T ) * 100

Anil Padia

0
Reply Anil 3/31/2006 7:53:21 AM

Anil <anil.padia@gmail.com> wrote:

> My Final Qn for this topic: Do you also approve my technique of getting
> average %CPU utilization [ both for Linux and Solaris] which is
> calculated as below:
> Assuming the test is run for time T
>  Average %CPU = ( total / T ) * 100

Seems fine to me.  You get to use whatever you want.

Does it agree with 'prstat -m' output over short intervals?

-- 
Darren Dunham                                           ddunham@taos.com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >
0
Reply Darren 3/31/2006 7:08:16 PM

5 Replies
695 Views

(page loaded in 0.107 seconds)

Similiar Articles:













7/21/2012 4:32:16 AM


Reply: