All pastes #1272318 Raw Edit

Someone

public text v1 · immutable
#1272318 ·published 2008-12-01 13:30 UTC
rendered paste body
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>     
#include <iostream>
#include <pthread.h>
#include <sstream>
#include <fstream>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>

using namespace std;
//************************************************************
typedef unsigned long long int UINT64;

UINT64 readTSC(void)
{
   UINT64 ts;

   __asm__ volatile(".byte 0x0f,0x31" : "=A" (ts));
   return ts;
}

UINT64 cyclesElapsed(UINT64 stopTS, UINT64 startTS)
{
   return (stopTS - startTS);
}

double mhz(int sleeptime)
{
	UINT64 startTSC = 0;
	UINT64 stopTSC = 0;
	UINT64 cycleCnt = 0;

   	UINT64 instCnt = 0;
	double clkRate = 0.0;

   	startTSC = readTSC();
   	usleep(sleeptime);
   	stopTSC = readTSC();

   	cycleCnt = cyclesElapsed(stopTSC, startTSC);

   	clkRate = ((double)cycleCnt)/1000000.0;

   	printf("Based on usleep accuracy, CPU clk rate = %lu clks/sec,",
          cycleCnt);
   	printf(" %7.1f Mhz\n", clkRate);

	return clkRate;

}

//************************************************************

void process(struct rusage *p, char *when)
{
	printf("%s\n", when);
	printf("user time used  	           %8d\n", p->ru_utime.tv_usec);
	printf("system time used                   %8d\n", p->ru_stime.tv_usec);
}

unsigned long GetProcessTime()
{

	char procFilename[256] ;
	char buffer[1024] ;

	pid_t pid = getpid() ;
	pid_t tid = syscall(224) ;
	cout<<"Pid == "<<pid<<endl;
	sprintf(procFilename, "/proc/%d/stat",pid) ;
	int fd, num_read ;
	fd = open(procFilename, O_RDONLY, 0);
	num_read = read(fd, buffer, 1023);
	close(fd);
	buffer[num_read] = '\0';
	
	char* ptrUsr = strrchr(buffer, ')') + 1 ;
	for(int i = 3 ; i != 14 ; i++)
	{
		ptrUsr = strchr(ptrUsr+1, ' ') ;
	}

	ptrUsr++;
	long jiffies_user = atol(ptrUsr) ;
	long jiffies_sys = atol(strchr(ptrUsr,' ') + 1) ;

	cout<<"user"<<jiffies_user<<endl;
	cout<<"system"<<jiffies_sys<<endl;

	return jiffies_user + jiffies_sys;
}

unsigned long GetThreadTime()
{

	char procFilename[256] ;
	char buffer[1024] ;

	pid_t pid = getpid() ;
	pid_t tid = syscall(224) ;

	cout<<"Pid == "<<pid<<endl;	
	cout<<"tid == "<<tid<<endl;

	sprintf(procFilename, "/proc/%d/task/%d/stat",pid,tid) ;
	int fd, num_read ;
	fd = open(procFilename, O_RDONLY, 0);
	num_read = read(fd, buffer, 1023);
	close(fd);
	buffer[num_read] = '\0';
	
	char* ptrUsr = strrchr(buffer, ')') + 1 ;
	for(int i = 3 ; i != 14 ; i++)
	{
		ptrUsr = strchr(ptrUsr+1, ' ') ;
	}

	ptrUsr++;
	long jiffies_user = atol(ptrUsr) ;
	long jiffies_sys = atol(strchr(ptrUsr,' ') + 1) ;
	
	cout<<"user"<<jiffies_user<<endl;
	cout<<"system"<<jiffies_sys<<endl;

	return jiffies_user + jiffies_sys;
}

//************************************************************
void *thread_X ( void *ptr )
{
	//pthread_mutex_lock(&g_thread_mutex);
	cout<<"\n\n\n";

	unsigned long t;
	t = GetThreadTime();
	cout<<"Thread X time b4== "<<t<<endl;

	for(int i=0; i<100; i++)
	{
		for(int j=0; j<10; j++)
		{
			//cout<<"X";
		}
	}

	cout<<"ThreadX"<<endl;

	t = GetThreadTime();
	cout<<"Thread X time after== "<<t<<endl;
    //pthread_mutex_unlock(&g_thread_mutex);
	while(1)
	{
	}
}

void *thread_Y ( void *ptr )
{
	cout<<"\n\n\n";

	//pthread_mutex_lock(&g_thread_mutex);

	unsigned long t;
	t = GetThreadTime();
	cout<<"Thread Y time b4== "<<t<<endl;

	for(int i=0; i<100; i++)
	{
		for(int j=0; j<10; j++)
		{
			//cout<<"Y";
		}
	}
	cout<<"ThreadY"<<endl;

	t = GetThreadTime();
	cout<<"Thread Y time after== "<<t<<endl;
    //pthread_mutex_unlock(&g_thread_mutex);
	while(1)
	{
	}
}

void *thread_Z ( void *ptr )
{
	cout<<"\n\n\n";

	//pthread_mutex_lock(&g_thread_mutex);

	unsigned long t;
	t = GetThreadTime();
	cout<<"Thread Z time b4== "<<t<<endl;

	for(int i=0; i<100; i++)
	{
		for(int j=0; j<10; j++)
		{
			//cout<<"Z";
		}
	}

	cout<<"ThreadZ"<<endl;
	t = GetThreadTime();
	cout<<"Thread Z time after== "<<t<<endl;
while(1)
{
}
    //pthread_mutex_unlock(&g_thread_mutex);

}
//************************************************************

pthread_attr_t  g_thread_attr;
pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;

void init()
{
    pthread_attr_init(&g_thread_attr);
    pthread_attr_setdetachstate(&g_thread_attr, PTHREAD_CREATE_DETACHED);
}

void cleanup()
{
    pthread_attr_destroy(&g_thread_attr);

}

int main()
{
	cout<<"\n\n\n";

    init();
	
	double r = mhz(1000000);

    pthread_t threadX, threadY, threadZ;
	
    pthread_create(&threadX, &g_thread_attr, thread_X, NULL);
	pthread_create(&threadY, &g_thread_attr, thread_Y, NULL);
    pthread_create(&threadZ, &g_thread_attr, thread_Z, NULL);

	unsigned long t;
	cout<<"Parent"<<endl;
	t = GetProcessTime();
	cout<<"Parent time after== "<<t<<endl;

	cleanup();
 	cout<<"****** End of Main ******"<<endl;
	while(1)
	{

	}
	return 0;
}
//************************************************************