/* Set of utilities for writing systems-experimentation programs */ #include #include /* you may need to recalibrate the following constant, used in busywait: */ #define SECONDTIME 25000000 void busywait(double sec) // cpu loops for sec seconds { long i, limit; limit = (long)(sec*SECONDTIME); for (i=0;istart),NULL); } void stoptimer(struct timer * tm) { gettimeofday(&(tm->end),NULL); } long elapsed(struct timer *tm) { long e1, e2; // ms times stoptimer(tm); e1 = (tm->start.tv_sec * 1000) + (tm->start.tv_usec / 1000); e2 = (tm->end.tv_sec * 1000) + (tm->end.tv_usec / 1000); starttimer(tm); return e2-e1; } void realwait(double sec) { int i = 4; struct timer tm; starttimer(&tm); while (elapsed(&tm) < (sec * 1000.0)) { i++; i--; } } void clockwait(double sec) { double elapsed; clock_t start, total; total = (clock_t)(sec*CLOCKS_PER_SEC); start = clock(); while ((clock()-start) < total) {} }