perfstat_thread interfaces

The perfstat_thread interface returns a set of structures of type perfstat_thread_t, which is defined in the libperfstat.h file.

The field of the perfstat_thread_t structure includes the following:
Table 1. perfstat_thread_t fields
Item Description
Pid The process ID of the thread.
Tid The kernel ID of the thread.
Cpuid The processor on which the thread is bound.
ucpu_time The user mode CPU time in milliseconds.
scpu_time The system mode CPU time in milliseconds.
The following is an example of code for the perfstat_thread_t API:
#include <libperfstat.h>

void main()
{
	perfstat_thread_t *threadt;
	perfstat_id_t id;
	int i,rc,thread_count;

	/* Get the count of threads */
	thread_count = perfstat_thread(NULL, NULL,sizeof(perfstat_thread_t),0);

	/* check for error */
	if(thread_count <= 0)
	{
		perror("Error in perfstat_thread");
		exit(-1) ;
	}
	printf("Number of Threads = %d\n",thread_count);

	/* Allocate enough memory */
	threadt = (perfstat_thread_t *)calloc(thread_count,sizeof(perfstat_thread_t));
	if(threadt == NULL)
	{
		perror("Memory Allocation Error");
		exit(-1) ;
	}

	strcpy(id.name,"");
	rc = perfstat_thread(&id,threadt,sizeof(perfstat_thread_t),thread_count);
	if(rc <= 0)
	{
		free(threadt);
		perror("Error in perfstat_thread");
		exit(-1) ;
	}

	printf("\n =======Thread Related metrics =======\n");
	for(i=0 ; i<thread_count ;i++)
	{
		printf("Process ID =                     %u\n",threadt[i].pid);
		printf("Thread ID =                       %u\n",threadt[i].tid);
		printf("\nCPU Related Statistics \n");
		printf("User Mode CPU time =               %f ms\n",threadt[i].ucpu_time);
		printf("System Mode CPU time =             %f ms\n",threadt[i].scpu_time);
		printf("Processor to which the thread is bound = %d\n", threadt[i].cpuid);
		printf("=====================================\n");
		printf("\n\n");
	}
	free(threadt);
}
The program displays an output that is similar to the following example output:
Process ID = 6553744
Thread ID  = 12345

CPU Related Statistics
User Mode CPU time = 714000.000000 ms
System Mode CPU time = 3000.000000 ms
Processor to which the thread is bound = 1