test_start.m.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 + 2];
  30. char time_arg[30];
  31. for (int i = 1 ; i < argc ; i++)
  32. new_argv[i - 1] = argv[i];
  33. new_argv[argc - 1] = "./start";
  34. new_argv[argc] = time_arg;
  35. new_argv[argc + 1] = NULL;
  36. unsigned long long times[TEST_TIMES];
  37. unsigned long long sum = 0, ssum = 0;
  38. memset(times, 0, sizeof(times));
  39. for (int i = 1 ; i < TEST_TIMES ; i++) {
  40. int pipes[2];
  41. if (pipe(pipes) < 0)
  42. break;
  43. pid_t pid = fork();
  44. if (pid < 0)
  45. break;
  46. if (!pid) {
  47. struct timeval tv1, tv2;
  48. gettimeofday(&tv1, NULL);
  49. for (int j = 0 ; j < OVERHEAD_TIMES ; j++)
  50. get_time(time_arg, 0);
  51. gettimeofday(&tv2, NULL);
  52. unsigned long long msec1 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
  53. unsigned long long msec2 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
  54. unsigned long long overhead = (msec2 - msec1) / OVERHEAD_TIMES;
  55. close(pipes[0]);
  56. dup2(pipes[1], 1);
  57. get_time(time_arg, overhead);
  58. execve(new_argv[0], new_argv, envp);
  59. exit(-1);
  60. }
  61. close(pipes[1]);
  62. int status;
  63. waitpid(pid, &status, 0);
  64. if (read(pipes[0], time_arg, 30) < 0)
  65. break;
  66. times[i] = atoll(time_arg);
  67. sum += times[i];
  68. ssum += times[i] * times[i];
  69. close(pipes[0]);
  70. }
  71. int compar (const void * arg1, const void * arg2)
  72. {
  73. register unsigned long long a1 = *((unsigned long long *) arg1);
  74. register unsigned long long a2 = *((unsigned long long *) arg2);
  75. return a1 < a2 ? -1 : (a1 == a2 ? 0 : 1);
  76. }
  77. qsort(times, TEST_TIMES, sizeof(unsigned long long), compar);
  78. double median =
  79. (TEST_TIMES % 2) ? (double) times[TEST_TIMES / 2] :
  80. (double) (times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
  81. double mean = (double) sum / TEST_TIMES;
  82. double stddev = sqrt((double) ssum / TEST_TIMES - mean * mean);
  83. double ci = 1.96 * stddev / sqrt((double) TEST_TIMES);
  84. printf("median = %lf, mean = %lf (+/-%lf)\n", median, mean, ci);
  85. return 0;
  86. }