To find CPU Usage


 
Thread Tools Search this Thread
Top Forums Programming To find CPU Usage
# 1  
Old 01-19-2006
To find CPU Usage

I am programatically trying to find CPU usage for a particular process. I am writing a C program for this. I am not sure if my approach is good at all. I first of all find the PID using getpid() method. Then I call top -f <filename> and then parse to reach the PID row. I then try to read the 10th field namely %CPU. But this is very tedious and likely to have many bugs in parsing process.
I had to read from file since top command does not terminate automatically. Can anyone, please suggest some better way to do?
Thanks a lot in advance.
# 2  
Old 01-19-2006
Finding cpu usage of another process is generally a hardware dependent kind of thing.
What hardware and OS are you using?

Plus, most of the tools available for system information usually work better in a shell script. C is normally not the best choice.
# 3  
Old 01-19-2006
use unix shell,it is too diffcult to using c program

i give a shell script under aix,as follow
usage:nproc.sh -m -p
nproc.sh

#!/bin/ksh
ShowUsage ( )
{
echo "===================================="
echo "nproc.sh"
echo "\t[-h help]"
echo "\t[-m memory statics]"
echo "\t[-p cpu statics]"
echo "\t[-t internal seconds]"
echo "\t[-n display count]"
echo "===================================="
exit 0;
}

#define FILED sort
PID_FIELD=1
TTY_FIELD=2
STAT_FIELD=3
TIME_FIELD=4
SIZE_FIELD=5
RSS_FIELD=6
CPU_FIELD=7
MEM_FIELD=8
USER_FIELD=9
COMMAND_FIELD=10
TMPFILE=`date '+%Y%m%d%H%M%S'`
STATFILE="$TMPFILE"
TMPFILE="$TMPFILE.log"

#define input tocken
HELP_TOCKEN="-h"
MEMORY_TOCKEN="-m"
CPU_TOCKEN="-p"
INTERTIME_TOCKEN="-t"
COUNT_TOCKEN="-n"

#set init value
typeset -i I
typeset -i K
typeset -i SORT_FILED
typeset -i INTERTIME_SECOND
typeset -i CPU_FLAG
typeset -i MEM_FLAG

I=0
K=$#
DISPLAY_COUNT=10
SORT_FILED=$RSS_FIELD-1
INTERTIME_SECOND=2
CPU_FLAG=0
MEM_FLAG=0

cat /dev/null >/tmp/$STATFILE.mem
cat /dev/null >/tmp/$STATFILE.cpu

while [ $I -lt $K ]
do
#echo $1
if [ "$1" = "$HELP_TOCKEN" ]; then
ShowUsage
exit 1
fi

if [ "$1" = "$CPU_TOCKEN" ]; then
#SORT_FILED=$CPU_FIELD-1
CPU_FLAG=1
fi

if [ "$1" = "$MEMORY_TOCKEN" ]; then
#SORT_FILED=$CPU_FIELD-1
MEM_FLAG=1
fi

if [ "$1" = "$COUNT_TOCKEN" ]; then
DISPLAY_COUNT=$2
shift 1
I=$I+1
fi

if [ "$1" = "$INTERTIME_TOCKEN" ]; then
INTERTIME_SECOND=$2
shift 1
I=$I+1
fi

shift 1
I=$I+1
done


while [ 1 ]
do
ps -Ao pid,tty,stat,time,vsz=VSZ,rss=RSS,pcpu,pmem,comm,user|awk '{
PID=$1
TTY=$2
STAT=$3
if (length($4) > 8) {
TIME=substr( $4 ,length($4)-8+1,8 )
}
else{
TIME=$4
}

SIZE=$5
RSS=$6
CPU=$7
MEM=$8
COMMAND=$9
USER=$10


TTY="-"
#printf "%-8ld %s %s %05s %-7ld %-07s %-07s %-6s %-8.2f %-8ld%-6.2f%-5.2f %s\n",PID,TTY,S
TAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND
#printf "%-8ld %s %s %-8s %-7ld %-7.2f %-7.2f %-6s %-8.2f %-8ld%-6.2f%-6.2f %s %s\n",PID,
TTY,STAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND,USER
printf "%-8ld %s %s %-10s %-7.2f %-7.2f %-6.2f %-6.2f %-10s %s\n",PID,TTY,STAT,TIME,SIZE
/1024,RSS/1024,CPU,MEM,USER,COMMAND
#print PID,"\t",STAT,TIME,PGIN,SIZE/1024,RSS/1024,LIM,TSIZ/1024,TRS,CPU,MEM,COMMAND


}' >$TMPFILE

clear
echo ""
echo "=============================================================================================== ==="
echo "SIZE:virtual size of the data section of the process"
echo "RSS:The real-memory (resident set) size of the process"
#echo "TSIZ:The size of text (shared-program) image"
#echo "LIM: The soft limit on memory used"
#echo "TRS:The size of resident-set (real memory) of text"
#echo "=============================================================================================== =="
echo "PID\tTTY STAT TIME SIZE(MB) RSS(MB) %CPU %MEM USER COMMAND"

if [ $MEM_FLAG -eq 1 ]; then
SORT_FILED=$RSS_FIELD-1
sort -n -r +$SORT_FILED $TMPFILE >$STATFILE.mem
fi

if [ $CPU_FLAG -eq 1 ]; then
SORT_FILED=$CPU_FIELD-1
sort -n -r +$SORT_FILED $TMPFILE >$STATFILE.cpu
fi

if [ $MEM_FLAG -eq 1 ]; then
echo "=============================================MEMORY=STAT======================================= =="
SORT_FILED=$RSS_FIELD-1
head -n $DISPLAY_COUNT $STATFILE.mem
fi

if [ $CPU_FLAG -eq 1 ]; then
echo "=============================================CPU===STAT======================================== =="
head -n $DISPLAY_COUNT $STATFILE.cpu
fi
echo "=============================================================================================== =="
rm -f $TMPFILE
rm -f $STATFILE.cpu
rm -f $STATFILE.mem
sleep $INTERTIME_SECOND
done
# 4  
Old 01-20-2006
Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

How to find the usage of Individual cpu core in FreeBsd server?

Hi, If anyone know Please answer me the following question : How to find the usage of individual core cpu usage in FreeBsd server? What command is used for that? Thanks, Maruthu (2 Replies)
Discussion started by: maruthu
2 Replies

2. UNIX for Advanced & Expert Users

How to find out memory & cpu usage of a process

Hi, By using time command we can determine the execution time of a process or command. bash-2.04$ time ls -l total 0 -rw-r--r-- 1 admin tac 0 Oct 6 04:46 file1 -rw-r--r-- 1 admin tac 0 Oct 6 04:46 file2 real 0m0.002s user 0m0.000s sys 0m0.001s... (5 Replies)
Discussion started by: learn more
5 Replies

3. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

4. 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

5. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

6. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

7. Solaris

Find any spike in CPU usage a hour ago

Folks, I need to find out from the system logs ( or elsewhere ) if the CPU utilization reached say 90% an hour ago from now. Is there a way out. (2 Replies)
Discussion started by: kavera
2 Replies

8. Programming

CPU usage and memory usage

Please tell me solaris functions/api for getting following information 1- Function that tells how much memory used by current process 2- Function that tells how much memory used by all running processes 3- Function that tells how much CPU is used by current process 4- Function that tells how... (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

9. Shell Programming and Scripting

Getting CPU Usage

For analysis, I need to gather CPU usage from say 1:00 PM to 2:00 PM every 10 seconds on a given day. I heard this can be done with awk, but my UNIX is kind of rusty. (4 Replies)
Discussion started by: WG1
4 Replies

10. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies
Login or Register to Ask a Question