void matrix(int taintcount) { x = allocMatrix(dimension, TAINT_CAMERA, taintcount); y = allocMatrix(dimension, 0, 0); o = allocMatrix(dimension, 0, 0); int i, j; /* init matrix */ for (i = 0; i < dimension; i++){ for(j = 0; j < dimension; j++){ x[i][j] = i + j; y[i][j] = i + j; o[i][j] = 0; } } // double start, end, delta; struct timespec start, end; if (emu) { // emu_set_protect(true); emu_mprotect_mem(true); emu_reset_stats(); time_ns(&start); EMU_MARKER_START; } else { time_ns(&start); } for (i = 0; i < runs; i++) { // M(matrixLoop()); matrixLoop(); } if (emu) { EMU_MARKER_STOP; time_ns(&end); emu_unprotect_mem(); emu_dump_stats(); /* emu_dump_taintmaps(); */ emu_dump_taintpages(); } else { time_ns(&end); } printf("cycles = %lld\n", ns_to_cycles(diff_ns(&start, &end)) / runs); /* free matrix */ /* dealloc(x, dimension); */ /* dealloc(y, dimension); */ /* dealloc(o, dimension); */ freeMatrix(x, dimension); freeMatrix(y, dimension); freeMatrix(o, dimension); }
int main(void) { const int iterations = 500000; struct timespec ts; const int shm_id = shmget(IPC_PRIVATE, sizeof (int), IPC_CREAT | 0666); const pid_t other = fork(); int* futex = shmat(shm_id, NULL, 0); *futex = 0xA; if (other == 0) { for (int i = 0; i < iterations; i++) { sched_yield(); while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xA, NULL, NULL, 42)) { // retry sched_yield(); } *futex = 0xB; while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) { // retry sched_yield(); } } return 0; } const long long unsigned start_ns = time_ns(&ts); for (int i = 0; i < iterations; i++) { *futex = 0xA; while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) { // retry sched_yield(); } sched_yield(); while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xB, NULL, NULL, 42)) { // retry sched_yield(); } } const long long unsigned delta = time_ns(&ts) - start_ns; const int nswitches = iterations << 2; printf("%i process context switches in %lluns (%.1fns/ctxsw)\n", nswitches, delta, (delta / (float) nswitches)); wait(futex); return 0; }
int main(void) { const int iterations = 10000000; struct timespec ts; long long unsigned start_ns; char c; int fd; fd = open( "/dev/zero", O_RDWR); start_ns = time_ns(&ts); for (int i = 0; i < iterations; i++) { if (syscall(SYS_write, fd, &c, 1) <0) { exit(2); } } const long long unsigned delta = time_ns(&ts) - start_ns; printf("%i write system calls in %lluns (%.1fns/syscall)\n", iterations, delta, (delta / (float) iterations)); return 0; }
int main(void) { struct sched_param param; param.sched_priority = 1; if (sched_setscheduler(getpid(), SCHED_FIFO, ¶m)) fprintf(stderr, "sched_setscheduler(): %s\n", strerror(errno)); struct timespec ts; pthread_t thd; if (pthread_create(&thd, NULL, thread, NULL)) { return 1; } long long unsigned start_ns = time_ns(&ts); for (int i = 0; i < iterations; i++) sched_yield(); long long unsigned delta = time_ns(&ts) - start_ns; const int nswitches = iterations << 2; printf("%i thread context switches in %lluns (%.1fns/ctxsw)\n", nswitches, delta, (delta / (float) nswitches)); return 0; }
int main(int argc, char** argv) { if(argc!=2){ printf("Usage: ./atimectxsw <NumProcs>"); return 0; } const int numProcs=atoi(argv[1]); const int iterations = 5000000; //Timer struct timespec ts; long long unsigned start_ns=0; //Set CPU affinity, pin to a single core cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(7, &mask); if(sched_setaffinity(0, sizeof(mask), &mask)){ perror("Sched_Affinity not set"); exit(1); } //Set the scheduler to Round Robin struct sched_param param; //60 is higher than certain IRQs param.sched_priority=60; if(sched_setscheduler(getpid(), SCHED_RR, ¶m)) fprintf(stderr, "sched_setscheduler(): %s\n", strerror(errno)); //Sanity check counter //int switches=0; //Attach shared memory for atomic counters const int shm_id = shmget(IPC_PRIVATE, 3*sizeof(int), IPC_CREAT | 0666); int* shmPTR = shmat(shm_id, NULL, 0); shmPTR[ITER_INDX]=0;//number of iterations done shmPTR[NUMPROC_INDX]=0;//number of procs gathered shmPTR[SIG_INDX]=0;//parent signal to children //Spawn procs pid_t pid=getpid(); for(int i=0; i<numProcs-1; i++){ pid = fork(); if(pid<0) exit(1); else if(pid==0) break; } //gather all procs at the starting line __sync_fetch_and_add(&shmPTR[NUMPROC_INDX], 1); if(pid==0){ //children wait for parent's signal while(shmPTR[SIG_INDX]!=1) sched_yield(); } else{ //parent waits for children to gather at starting line while(shmPTR[NUMPROC_INDX]!=numProcs) sched_yield(); //Everybody here? start timer start_ns = time_ns(&ts); //signal the children __sync_fetch_and_add(&shmPTR[SIG_INDX], 1); } //Increment and yield to context switch onto next proc while(shmPTR[ITER_INDX]<iterations){ __sync_fetch_and_add(&shmPTR[ITER_INDX], 1); //switches++; sched_yield(); } //Print out number of switches done per process, should be equal //printf("Pid:%d Switches:%d\n",getpid(),switches); //Children terminate if(pid==0) return 0; //Stop timer const long long unsigned delta = time_ns(&ts) - start_ns; //Cleanup while(wait(NULL)>=0); printf("Switches=%d, Procs=%d\n", shmPTR[0], shmPTR[1]); printf("%i context switches in %lluns (%.1fns/syscall)\n", iterations, delta, (delta / (float) iterations)); return 0; }
int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "usage: %s <size of working set in 4K pages>\n", *argv); return 1; } const long ws_pages = strtol(argv[1], NULL, 10); if (ws_pages < 0) { fprintf(stderr, "Invalid usage: working set size must be positive\n"); return 1; } const int iterations = get_iterations(ws_pages); struct timespec ts; long long unsigned memset_time = 0; if (ws_pages) { void* buf = malloc(ws_pages * 4096); memset_time = time_ns(&ts); for (int i = 0; i < iterations; i++) { memset(buf, i, ws_pages * 4096); } memset_time = time_ns(&ts) - memset_time; printf("%i memset on %4liK in %10lluns (%.1fns/page)\n", iterations, ws_pages * 4, memset_time, (memset_time / ((float) ws_pages * iterations))); free(buf); } const int shm_id = shmget(IPC_PRIVATE, (ws_pages + 1) * 4096, IPC_CREAT | 0666); const pid_t other = fork(); int* futex = shmat(shm_id, NULL, 0); void* ws = ((char *) futex) + 4096; *futex = 0xA; if (other == 0) { for (int i = 0; i < iterations; i++) { sched_yield(); while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xA, NULL, NULL, 42)) { // retry sched_yield(); } *futex = 0xB; if (ws_pages) { memset(ws, i, ws_pages * 4096); } while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) { // retry sched_yield(); } } return 0; } const long long unsigned start_ns = time_ns(&ts); for (int i = 0; i < iterations; i++) { *futex = 0xA; if (ws_pages) { memset(ws, i, ws_pages * 4096); } while (!syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 42)) { // retry sched_yield(); } sched_yield(); while (syscall(SYS_futex, futex, FUTEX_WAIT, 0xB, NULL, NULL, 42)) { // retry sched_yield(); } } const long long unsigned delta = time_ns(&ts) - start_ns - memset_time * 2; const int nswitches = iterations * 4; printf("%i process context switches (wss:%4liK) in %12lluns (%.1fns/ctxsw)\n", nswitches, ws_pages * 4, delta, (delta / (float) nswitches)); wait(futex); return 0; }