test_start.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. /*
  10. * USAGE:
  11. * ./test_start [prefixes to the program ...]
  12. *
  13. * EXAMPLES:
  14. * ./test_start => native start time
  15. * ./test_start ./libpal.so => graphene start time
  16. */
  17. #define OVERHEAD_TIMES 30000
  18. #define TEST_TIMES 1000
  19. void get_time(char* time_arg, unsigned long overhead) {
  20. struct timeval tv;
  21. gettimeofday(&tv, NULL);
  22. unsigned long long msec = tv.tv_sec * 1000000ULL + tv.tv_usec;
  23. snprintf(time_arg, 30, "%llu", msec + overhead);
  24. }
  25. int main(int argc, char** argv, char** envp) {
  26. char* new_argv[argc + 2];
  27. char time_arg[30];
  28. for (int i = 1; i < argc; i++) {
  29. new_argv[i - 1] = argv[i];
  30. }
  31. new_argv[argc - 1] = "./start";
  32. new_argv[argc] = time_arg;
  33. new_argv[argc + 1] = 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. }
  50. gettimeofday(&tv2, NULL);
  51. unsigned long long msec1 = tv1.tv_sec * 1000000ULL + tv1.tv_usec;
  52. unsigned long long msec2 = tv2.tv_sec * 1000000ULL + tv2.tv_usec;
  53. unsigned long long overhead = (msec2 - msec1) / OVERHEAD_TIMES;
  54. close(pipes[0]);
  55. dup2(pipes[1], 1);
  56. get_time(time_arg, overhead);
  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. if (read(pipes[0], time_arg, 30) < 0)
  64. break;
  65. times[i] = atoll(time_arg);
  66. sum += times[i];
  67. ssum += times[i] * times[i];
  68. close(pipes[0]);
  69. }
  70. int compar(const void* arg1, const void* arg2) {
  71. register unsigned long long a1 = *((unsigned long long*)arg1);
  72. register unsigned long long a2 = *((unsigned long long*)arg2);
  73. return a1 < a2 ? -1 : (a1 == a2 ? 0 : 1);
  74. }
  75. qsort(times, TEST_TIMES, sizeof(unsigned long long), compar);
  76. double median = (TEST_TIMES % 2)
  77. ? (double)times[TEST_TIMES / 2]
  78. : (double)(times[TEST_TIMES / 2 - 1] + times[TEST_TIMES / 2]) / 2;
  79. double mean = (double)sum / TEST_TIMES;
  80. double stddev = sqrt((double)ssum / TEST_TIMES - mean * mean);
  81. double ci = 1.96 * stddev / sqrt((double)TEST_TIMES);
  82. printf("median = %lf, mean = %lf (+/-%lf)\n", median, mean, ci);
  83. return 0;
  84. }