Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - how to check the cpu usage of every core: (2 Items)
   
how to check the cpu usage of every core  
I have installed a multi-core QNX OS on Intel Allagash board, there is 4 cpu cores in this system. It can display 4 
cores in the system monitor. Now I do benchmarking on Allagash board with QNX OS, I need to know the cpu usage of every 
core when I running a special application, but I don't know how to get the every core usage, it can get the total cpu 
usage when using hogs command, but I need to usage of each core. How can I get the cpu usage of every core?  Thanks!
Re: how to check the cpu usage of every core  
> I have installed a multi-core QNX OS on Intel Allagash board, there is 4 cpu 
> cores in this system. It can display 4 cores in the system monitor. Now I do 
> benchmarking on Allagash board with QNX OS, I need to know the cpu usage of 
> every core when I running a special application, but I don't know how to get 
> the every core usage, it can get the total cpu usage when using hogs command, 
> but I need to usage of each core. How can I get the cpu usage of every core?  
> Thanks!

You can use Your IDE45 and get a kernel trace in wide mode. 
then You can get infos like in attached picture.

or try this one:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <devctl.h>
#include <err.h>
#include <errno.h>
#include <libc.h>
#include <inttypes.h>
#include <atomic.h>
#include <pthread.h>
#include <sys/debug.h>
#include <sys/iofunc.h>
#include <sys/neutrino.h>
#include <sys/procfs.h>
#include <sys/resmgr.h>
#include <sys/syspage.h>
#include <sys/time.h>
#include <sys/types.h>

#define MAX_CPUS 32
/* CPU */
static double		Loads[ MAX_CPUS ];
static _uint64		LastSutime[ MAX_CPUS ];
static _uint64		LastNsec[ MAX_CPUS ];
static int			ProcFd = -1;
static int			NumCpus = 0;
/* Actual time in nsec */
static _uint64 CurTimeNs( void )
{
	struct timeval	tval;
	_uint64			sec, usec;
	gettimeofday( &tval, NULL );
	sec = tval.tv_sec;
	usec = tval.tv_usec;
	return ( ( sec * 1000000 ) + usec ) * 1000;
}
/* Initialize the CPU-measurement */
void init_cpus( void )
{
	debug_thread_t		debug_data;
	int						i;
	/* get number of CPUs */
	NumCpus = _syspage_ptr->num_cpu;
	memset( &debug_data, 0, sizeof debug_data );
	ProcFd = open( "/proc/1/as", O_RDONLY );
	for ( i = 0; i < NumCpus; i++ )
	{
debug_data.tid = i + 1;
devctl( ProcFd, DCMD_PROC_TIDSTATUS, &debug_data, sizeof debug_data, NULL );
		LastSutime[i] = debug_data.sutime;
		LastNsec[i] = CurTimeNs();
	}
}
/* Number of CPUs */
int num_cpus( void )
{
	return NumCpus;
}
/* Sample all CPUs  */
void sample_cpus( void )
{
	debug_thread_t	debug_data;
	_uint64			cur, dsu, dt;
	int					i;
	memset( &debug_data, 0, sizeof debug_data );
	for ( i = 0; i < NumCpus; i++ )
	{
		
		/* 
		The trick, is:
		The su-time of the idle-treads tells us how much CPU-time was
		/not/ consumed. Having this and knowing the indeed passed by 
		time we can calculate the CPU-Load in percent. 
		The idle.thread /i/+1 belongs belongs to CPU /i/. 
		*/
		debug_data.tid = i + 1;
		devctl( ProcFd, DCMD_PROC_TIDSTATUS, &debug_data, sizeof debug_data, NULL );
	      cur = CurTimeNs(); 
		dsu = debug_data.sutime - LastSutime[i];
		dt  = cur - LastNsec[i];
		/* calculate load */
		Loads[i] = 100.0 - ( ( dsu * 100.0 ) / dt );
		/* balance rounding errors */
		if ( Loads[i] < 0 )
			Loads[i] = 0;
		else if ( Loads[i] > 100 )
			Loads[i] = 100;
		/*  Save old values for the next pass */
		LastNsec[i] = cur;
		LastSutime[i] = debug_data.sutime;
	}
}

int get_cpu( int n )
{
	return Loads[ n % MAX_CPUS ];
}
int main()
{
	int		i;
	init_cpus();
	for ( ; ; )
	{
		sample_cpus();
		printf( "\f" );
		for ( i = 0; i < num_cpus(); i++ )
			printf( "CPU #%d: %d%%\n", i+1, get_cpu( i ) );
		delay( 100 );
	}

	return EXIT_SUCCESS;
}


Hope this helps.
Jeevan



Attachment: Image multcpuusage.jpg 71.56 KB