#include #include #include /* for mmap and MCL_CURRENT */ #include /* for atoi */ #include /* for rdtsc */ #define __USE_GNU #include /* for sched_xxxx */ #define NB_TEST 60000000000ULL #include float get_cpu_clock_speed() { FILE *fp; char buffer[1024]; size_t bytes_read; char *match; float clock_speed; fp = fopen ("/proc/cpuinfo", "r"); bytes_read = fread (buffer, 1, sizeof (buffer), fp); fclose (fp); if (bytes_read == 0 || bytes_read == sizeof (buffer)) return 0; buffer[bytes_read] = '\0'; match = strstr (buffer, "cpu MHz"); if (match == NULL) return 0; sscanf (match, "cpu MHz : %f", &clock_speed); return clock_speed; } int main(int argc,char **argv) { unsigned long long max,dt; float frequency; int cptr; long long i; struct sched_param sched_param; double duree; union { unsigned long long total; struct { unsigned long MSL; unsigned long LSL; }; } tempsdeb,tempsfin,t1,t2,tmax1,tmax2; cpu_set_t new_mask; pid_t p = 0; int ret; if (argc!=2) { fprintf(stderr,"error usage:%s \n",argv[0]); return -1; } CPU_ZERO(&new_mask); CPU_SET(atoi(argv[1]),&new_mask); ret = sched_setaffinity(p,sizeof(cpu_set_t) , &new_mask); rdtsc(tempsdeb.MSL,tempsdeb.LSL); printf(" time begin %Lx \n",tempsdeb.total); sched_param.sched_priority =99; cptr = sched_setscheduler(getpid(), SCHED_FIFO,&sched_param ); cptr = mlockall(MCL_CURRENT); printf("cptr mlockall %d\n",cptr); max=0; rdtsc(t1.MSL,t1.LSL); for (i = 0; i < NB_TEST; i++) { rdtsc(t2.MSL,t2.LSL); dt = t2.total - t1.total; if ( dt > max) { max = dt; tmax1.total = t1.total; tmax2.total = t2.total; } t1.total = t2.total; } rdtsc(tempsfin.MSL,tempsfin.LSL); printf(" time end %Lx \n",tempsfin.total); printf(" time max 1 %Lx \n",tmax1.total); printf(" time max 2 %Lx \n",tmax2.total); frequency = get_cpu_clock_speed(); if (frequency == 0) return -1; duree =((double) (tempsfin.total - tempsdeb.total))/(frequency*1000000); printf("Test duration %f s max detected %.0f µs\n",duree,((double)max)/(frequency)); return 0; }