get_time.c 3.0 KB

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