*----------------------------------------------------------------
measuring RT thread and RT thread switching
Programmed by Kazumix 2005.9.2
------------------------------------------------------------------*/
#include <rt.h>
#include <stdio.h>
#include <string.h>
typedef struct RECORDFORMAT {
QWORD p1;
QWORD p2;
QWORD p3;
}LOGSTRUCT ,*LPLOGSTRUCT;
extern QWORD GetPentiumCounter(void);
extern DWORD GetCPUSpeed(void);
extern double CalcTime( QWORD start ,QWORD end );
RTHANDLE hSoftEvent;
RTHANDLE hReturnBackEvent;
RTHANDLE hFinishEvent;
DWORD dwCounter;
LPLOGSTRUCT lpLogData;
#define MAXRECORD 10000
// High priority thread / receiving event
void Thread2 ( void )
{
while(1)
{
WaitForRtSemaphore( hSoftEvent ,1 ,WAIT_FOREVER );
lpLogData[dwCounter].p2 = GetPentiumCounter(); // (2)
ReleaseRtSemaphore( hReturnBackEvent ,1 );
}
}
// Low priority thread / sending event
void Thread1 ( void )
{
RtSleep( 1000 );
lpLogData[dwCounter].p1 = 0; // dummy memory access
lpLogData[dwCounter].p2 = 0; // (for removing memory cache delay time)
while(1)
{
lpLogData[dwCounter].p1 = GetPentiumCounter(); // (1)
ReleaseRtSemaphore( hSoftEvent ,1 );
WaitForRtSemaphore( hReturnBackEvent ,1 ,WAIT_FOREVER );
// lpLogData[dwCounter].p3 = GetPentiumCounter(); // (3)
dwCounter ++;
if( dwCounter > MAXRECORD ){
ReleaseRtSemaphore( hFinishEvent ,1 );
SuspendRtThread( NULL_RTHANDLE );
}
}
}
// the main
main()
{
FILE *fp;
DWORD lp1;
RTHANDLE Th1 ,Th2;
SetRtProcessMaxPriority( 0 ,0 );
SetRtThreadPriority( NULL_RTHANDLE ,2 );
printf("CPU speed is %u Hz\n" , GetCPUSpeed());
// create semaphore objects
hSoftEvent = CreateRtSemaphore( 0 ,128 ,FIFO_QUEUING );
hReturnBackEvent = CreateRtSemaphore( 0 ,128 ,FIFO_QUEUING );
hFinishEvent = CreateRtSemaphore( 0 ,1 ,FIFO_QUEUING );
// logging buffer
lpLogData = AllocateRtMemory( sizeof(LOGSTRUCT) * MAXRECORD );
memset( lpLogData ,0 ,sizeof( sizeof(LOGSTRUCT) * MAXRECORD) );
// log counter
dwCounter = 0;
// create test threads
Th2 = CreateRtThread( 1 ,Thread2 ,4096 ,0 );
Th1 = CreateRtThread( 2 ,Thread1 ,4096 ,0 );
// fait for the finish event
WaitForRtSemaphore( hFinishEvent ,1 ,WAIT_FOREVER );
// save results to CSV format
fp = fopen("c:\\R22RT.csv" ,"wt");
fprintf( fp ,"Count ,Thread switching delay in nano seconds\n");
for( lp1=0 ;lp1<MAXRECORD ;lp1++ ){
fprintf( fp ,"%d ,%.1f\n" ,lp1+1
,CalcTime( lpLogData[lp1].p1 ,lpLogData[lp1].p2 ) // nano seconds
);
}
fclose( fp );
// delete test threads
DeleteRtThread( Th1 );
DeleteRtThread( Th2 );
}