Take a look at the following program - it will give you the
percent of time over each second that the specified process (or
a particular thread in that process) was running.
Note that using pid 1, thread gives you the amount of time that
cpu is idle, and it was the shelf cpu monitor uses.
#include <stdlib.h>
#include <stdio.h>
#include <sys/neutrino.h>
#include <inttypes.h>
int main( int argc, char *argv )
{
int id;
pid_t pid = 0; / this process /
pthread_t tid = 0; / all threads */
uint64_t start_time[2], end_time[2];
double percent;
if ( argc > 1 )
pid = atoi(argv[1]);
if ( argc > 2 )
tid = atoi(argv[2]);
id = ClockId( pid, tid );
while(1) {
ClockTime( CLOCK_REALTIME, NULL, &start_time[0] );
ClockTime( id, NULL, &start_time[1] );
sleep(1);
ClockTime( CLOCK_REALTIME, NULL, &end_time[0] );
ClockTime( id, NULL, &end_time[1] );
end_time[0] -= start_time[0];
end_time[1] -= start_time[1];
percent = end_time[1];
percent /= end_time[0];
percent *= 100.0;
printf(“PID %d, TID %d, %lld/%lld ticks %%%d\n”,
pid, tid, end_time[1], end_time[0], (int)percent );
}
return 0;
}
William Morris <william@bangel.demon.co.uk> wrote:
How can I get an accurate idea of process loads?
If I use ‘spin’ the numbers reported are often wrong.
But what about ‘sin threads’ and ‘sin cpu’? Is sin cpu accurate? Is sin
threads accurate if the threads are long-lived (eg for a given thread
number, if that thread is not continually begin killed and restarted)?
I have a process with a thread which is normally in the INTR state.
sin threads indicates that it takes about 50% of cpu time, but I am told by
the author that is impossible. Why should the value reported be wrong and
what is the correct way to measure such things?
Thanks for any help
William Morris
–
cburgess@qnx.com