perfstat_thread_util interface
The perfstat_thread_util
interface returns
a set of structures of type perfstat_thread_t
, which
is defined in the libperfstat.h file.
The following is an example of code for the
perfstat_thread_util
API:#include <libperfstat.h>
#define PERIOD 5
void main()
{
perfstat_thread_t *cur, *prev;
perfstat_rawdata_t buf;
perfstat_thread_t *thread_util;
perfstat_id_t id;
int cur_thread_count,prev_thread_count;
int i,rc;
prev_thread_count = perfstat_thread(NULL, NULL,sizeof(perfstat_thread_t),0);
if(prev_thread_count <= 0)
{
perror("Error in perfstat_thread");
exit(-1) ;
}
prev = (perfstat_thread_t *)calloc(prev_thread_count,sizeof(perfstat_thread_t));
if(prev == NULL)
{
perror("Memory Allocation Error");
exit(-1) ;
}
strcpy(id.name,"");
prev_thread_count = perfstat_thread(&id,prev,sizeof(perfstat_thread_t),prev_thread_count);
if(prev_thread_count <= 0)
{
free(prev);
perror("Error in perfstat_thread");
exit(-1) ;
}
sleep(PERIOD);
cur_thread_count = perfstat_thread(NULL, NULL,sizeof(perfstat_thread_t),0);
if(cur_thread_count <= 0)
{
free(prev);
perror("Error in perfstat_thread");
exit(-1) ;
}
cur = (perfstat_thread_t *)calloc(cur_thread_count,sizeof(perfstat_thread_t));
thread_util = (perfstat_thread_t *)calloc(cur_thread_count,sizeof(perfstat_thread_t));
if(cur == NULL || thread_util == NULL)
{
free(prev);
perror("Memory Allocation Error");
exit(-1) ;
}
cur_thread_count = perfstat_thread(&id,cur,sizeof(perfstat_thread_t),cur_thread_count);
if(cur_thread_count <= 0)
{
free(prev);
free(cur);
free(thread_util);
perror("Error in perfstat_thread");
exit(-1) ;
}
bzero(&buf, sizeof(perfstat_rawdata_t));
buf.type = UTIL_PROCESS;
buf.curstat = cur;
buf.prevstat = prev;
buf.sizeof_data = sizeof(perfstat_thread_t);
buf.cur_elems = cur_thread_count;
buf.prev_elems = prev_thread_count;
/* Calculate Thread Utilization. This returns the number of thread_util structures that are filled */
rc = perfstat_thread_util(&buf,thread_util,sizeof(perfstat_thread_t),cur_thread_count);
if(rc <= 0)
{
free(prev);
free(cur);
free(thread_util);
perror("Error in perfstat_thread_util");
exit(-1);
}
printf("\n =======Thread Related Utilization Metrics =======\n");
for(i=0 ; i<rc ;i++)
{
printf("Process ID = %u\n",thread_util[i].pid);
printf("Thread ID = %u\n",thread_util[i].tid);
printf("User Mode CPU time = %f \n",thread_util[i].ucpu_time);
printf("System Mode CPU time = %f \n",thread_util[i].scpu_time);
printf(" Bound CPU Id = %d\n", thread_util[i].cpuid);
printf("=====================================\n");
printf("\n\n");
}
free(prev);
free(cur);
free(thread_util);
}
The program displays an output
that is similar to the following example output:
Process ID = 6160532
Thread ID = 123456
User Mode CPU time = 21.824531
System Mode CPU time = 0.000000
Bound CPU Id = 1