/* ** (c) 2004 Herbert Poetzl ** ** somewhat ispired by itimer_test.c ** by Olivier Poitrey ** ** V0.1 basic sleep operations ** */ #include #include #include #include #include #include #include #define VERSION "V0.1" enum _opt_type { TYPE_SLEEP = 0, TYPE_USLEEP, TYPE_PAUSE, TYPE_POLL, TYPE_SELECT, }; static char opt_type = TYPE_SLEEP; static int interval = 0; static int number = -1; static char *cmd_name = NULL; #define MAX_IVAL 5 #define MIN_IVAL 1 #define TOMS(x) ((x)*1000) #define TOUS(x) ((x)*1000*1000) #define USTOS(x) (((x)/1000)/1000) #define USREM(x) ((x)%(1000*1000)) #define OPTIONS "hipsuI:N:" void do_nothing(int signum) { } int main(int argc, char *argv[]) { extern int optind; extern char *optarg; struct timeval start_time, end_time; struct sigaction action; char c, errflg = 0; int count = 0; long adjust; cmd_name = argv[0]; while ((c = getopt(argc, argv, OPTIONS)) != EOF) { switch (c) { case 'h': fprintf(stderr, "This is %s " VERSION "\n" "options are:\n" "-h print this help message\n" "-i use pause() and itimer\n" "-p use poll() for waiting\n" "-s use select() for waiting\n" "-u use usleep() for waiting\n" "-I interval (0 = random)\n" "-N number of runs\n", cmd_name); exit(0); break; case 'i': opt_type = TYPE_PAUSE; break; case 'p': opt_type = TYPE_POLL; break; case 's': opt_type = TYPE_SELECT; break; case 'u': opt_type = TYPE_USLEEP; break; case 'I': interval = atol(optarg); break; case 'N': number = atol(optarg); break; case '?': default: errflg++; break; } } if (errflg) { fprintf(stderr, "Usage: %s -" OPTIONS "\n" "%s -h for help.\n", cmd_name, cmd_name); exit(2); } gettimeofday(&start_time, NULL); for (count=100; count>0; count--) gettimeofday(&end_time, NULL); adjust = TOUS(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec); adjust /= 100; action.sa_handler = do_nothing; action.sa_flags = 0; sigaction(SIGALRM, &action, NULL); while (number--) { struct itimerval period; long secs = interval; long delta, sign; if (!interval) secs = (random() % (MAX_IVAL - MIN_IVAL)) + MIN_IVAL; printf("%4d: [%3ld] ...", count, secs); period.it_interval.tv_sec = 0; period.it_interval.tv_usec = 0; period.it_value.tv_sec = secs; period.it_value.tv_usec = 0; gettimeofday(&start_time, NULL); switch (opt_type) { case TYPE_SLEEP: sleep(secs); break; case TYPE_USLEEP: usleep(TOUS(secs)); break; case TYPE_PAUSE: setitimer(ITIMER_REAL, &period, NULL); pause(); break; case TYPE_SELECT: select(0, NULL, NULL, NULL, &period.it_value); break; case TYPE_POLL: poll(NULL, 0, TOMS(secs)); break; } gettimeofday(&end_time, NULL); delta = TOUS(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec) - TOUS(secs) - adjust; sign = (delta >= 0) ? 1 : -1; delta *= sign; printf("%4ld.%06ld s %7.3f %%\n", sign*USTOS(delta), USREM(delta), (delta*100.0/TOUS(secs))); count++; } exit((errflg)?1:0); }