test_start_pthread_m.m.c 2.9 KB

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