Calculate %Cpu


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calculate %Cpu
# 1  
Old 03-02-2012
Calculate %Cpu

Hi.

For an homework, I have to simulate some part of the PS function. By reading the doc I've been able to find most of it but I don't how I can get the %cpu for each PID.

I've read on Internet that I have to do:

(utime(t)-utim(t-1))/(cputime(t)-cputime(t-1))

where cputime is the sum of int in the file /proc/stat for the line cpu.
and utime is find in /proc/pid/stat but it didn't work.

How can i get it?

PS: Sorry, for my english.
# 2  
Old 03-03-2012
Please post homework in the homework forum.

Please mention what Operating System and version you have and what Shell or programming language you are using.

If your Operating System has /proc (mine does not) please post representative sample data, a description of the calculation and an example of the output you would expect.


Mathematically your calculation looks unsound.

Percentage CPU time for a PID over elapsed time "t" would be:
Code:
((("CPU time used now by PID") - ("CPU time t seconds ago by PID")) x 100) / ("t seconds")

This assumes that one PID can only use one CPU and assumes that CPU time on your computer is in units of seconds (I don't know anything about your computer or the contents of /proc).
# 3  
Old 03-03-2012
Thanks, for the answer, it works better this way.

And sorry for the wrong section, haven't see the Homework section.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Is it possible to combine multiple CPU to act as a single CPU on the same server?

We have a single threaded application which is restricted by CPU usage even though there are multiple CPUs on the server, hence leading to significant performance issues. Is it possible to merge / combine multiple CPUs at OS level so it appear as a single CPU for the application? (6 Replies)
Discussion started by: Dissa
6 Replies

2. Shell Programming and Scripting

Shell script to calculate the max cpu usage from the main script

Hi All, I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide which cpu number (column 2) has maximum value (column 6). Since the output is displayed/updated every seconds, it's very... (1 Reply)
Discussion started by: Optimus81
1 Replies

3. Linux

How to calculate total CPU usage from SAR report?

Hi, I want to calculate the total cpu usage from the sar report. Say for example, Linux 2.6.24-21-generic (blade10) 09/10/2012 04:54:36 PM CPU %user %nice %system %iowait %steal %idle 04:54:37 PM all 0.00 0.00 0.00 0.00 ... (1 Reply)
Discussion started by: rohitmd
1 Replies

4. AIX

How to calculate AIX CPU utilization using lparstat command

Could you please explain about calculate CPU utilization of aix server using lparstat command? Here below i have provided example output from aix test server. System configuration: type=Shared mode=Uncapped smt=On lcpu=4 mem=4096 psize=63 ent=0.50 %user %sys %wait %idle physc %entc ... (1 Reply)
Discussion started by: maruthu
1 Replies

5. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

6. Solaris

cpu-shares vs cpu-cap in solaris

Can anyone tell me difference between cpu-shares vs cpu-cap in solaris & how FSS will work with cpu-caps ? (9 Replies)
Discussion started by: fugitive
9 Replies

7. Solaris

Multi CPU Solaris system shows 100% CPU usage.

Hello Friends, On one of my Solaris 10 box, CPU usage shows 100% using "sar", "vmstat". However, it has 4 CPUs and prstat and glance are not showing enough processes to justify high CPU utilization. ========================================================================= $ prstat -a ... (4 Replies)
Discussion started by: mahive
4 Replies

8. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux ..to use these values in shell script to kill processes exceeding cpu utilization.ps (pcpu) command does not give exact values..top does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

9. Shell Programming and Scripting

How to calculate Memory and CPU Usage on AIX

I have written a script that will calculate CPU usage and Memory usage of a particular process on AIX Unix. I know the PID and I am using the command -- # for CPU Usage MSG_CPU_USG=`ps uax | grep ${PID} | awk {'print $3'}` #for Memory Usage MSG_MEM_USG=`ps uax | grep ${PID} | awk {'print... (0 Replies)
Discussion started by: asutoshch
0 Replies

10. HP-UX

how to calculate CPU time under HP-UX

Hi, I am loking for a c++ function that calculate CPU time under HP-UX Thank you (1 Reply)
Discussion started by: limame
1 Replies
Login or Register to Ask a Question