get_time.m.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/wait.h>
  8. #include <sys/time.h>
  9. #include <math.h>
  10. /*
  11. * USAGE:
  12. * ./test_start [prefixes to the program ...]
  13. *
  14. * EXAMPLES:
  15. * ./test_start => native start time
  16. * ./test_start ./libpal.so => graphene start time
  17. */
  18. #define OVERHEAD_TIMES 30000
  19. #define TEST_TIMES 1000
  20. void get_time (char * time_arg, unsigned long overhead)
  21. {
  22. struct timeval tv;
  23. gettimeofday(&tv, NULL);
  24. unsigned long long msec = tv.tv_sec * 1000000ULL + tv.tv_usec;
  25. snprintf(time_arg, 30, "%llu", msec + overhead);
  26. }
  27. int main (int argc, char ** argv, char ** envp)
  28. {
  29. char * new_argv[argc + 1];
  30. char time_arg[30];
  31. for (int i = 1 ; i < argc ; i++)
  32. new_argv[i - 1] = argv[i];
  33. new_argv[argc] = NULL;
  34. unsigned long long times[TEST_TIMES];
  35. unsigned long long sum = 0, ssum = 0;
  36. memset(times, 0, sizeof(times));
  37. for (int i = 1 ; i < TEST_TIMES ; i++) {
  38. int pipes[2];
  39. if (pipe(pipes) < 0)
  40. break;
  41. pid_t pid = fork();
  42. if (pid < 0)
  43. break;
  44. if (!pid) {
  45. struct timeval tv1, tv2;
  46. gettimeofday(&tv1, NULL);
  47. for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
  48. get_time(time_arg, 0);
  49. gettimeofday(&tv2, NULL);
  50. unsigned long long msec1 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
  51. unsigned long long msec2 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
  52. unsigned long long overhead = (msec2 - msec1) / OVERHEAD_TIMES;
  53. get_time(time_arg, overhead);
  54. close(pipes[0]);
  55. write(pipes[1], time_arg, 30);
  56. close(pipes[1]);
  57. execve(new_argv[0], new_argv, envp);
  58. exit(-1);
  59. }
  60. close(pipes[1]);
  61. int status;
  62. waitpid(pid, &status, 0);
  63. struct timeval tv;
  64. gettimeofday(&tv, NULL);
  65. unsigned long long msec = tv.tv_sec * 1000000ULL + tv.tv_usec;
  66. if (read(pipes[0], time_arg, 30) < 0)
  67. break;
  68. times[i] = msec - atoll(time_arg);
  69. sum += times[i];
  70. ssum += times[i] * times[i];
  71. close(pipes[0]);
  72. }
  73. int compar (const void * arg1, const void * arg2)
  74. {
  75. register unsigned long long a1 = *((unsigned long long *) arg1);
  76. register unsigned long long a2 = *((unsigned long long *) arg2);
  77. return a1 < a2 ? -1 : (a1 == a2 ? 0 : 1);
  78. }
  79. qsort(times, TEST_TIMES, sizeof(unsigned long long), compar);
  80. double median =
  81. (TEST_TIMES % 2) ? (double) times[TEST_TIMES / 2] :
  82. (double) (times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
  83. double mean = (double) sum / TEST_TIMES;
  84. double stddev = sqrt((double) ssum / TEST_TIMES - mean * mean);
  85. double ci = 1.96 * stddev / sqrt((double) TEST_TIMES);
  86. printf("median = %lf, mean = %lf (+/-%lf)\n", median, mean, ci);
  87. return 0;
  88. }