bsdtestharness.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_APACHE_LICENSE_HEADER_START@
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @APPLE_APACHE_LICENSE_HEADER_END@
  19. */
  20. #include <dispatch/dispatch.h>
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
  25. #include <spawn.h>
  26. #include <sys/resource.h>
  27. #include <sys/time.h>
  28. #include <sys/wait.h>
  29. #include <unistd.h>
  30. #elif defined(_WIN32)
  31. #include <generic_win_port.h>
  32. #include <Psapi.h>
  33. #include <Windows.h>
  34. #endif
  35. #include <signal.h>
  36. #ifdef __APPLE__
  37. #include <mach/clock_types.h>
  38. #include <mach-o/arch.h>
  39. #endif
  40. #include <bsdtests.h>
  41. #if !defined(_WIN32)
  42. extern char **environ;
  43. #endif
  44. int
  45. main(int argc, char *argv[])
  46. {
  47. int res;
  48. pid_t pid = 0;
  49. if (argc < 2) {
  50. fprintf(stderr, "usage: %s [...]\n", argv[0]);
  51. exit(1);
  52. }
  53. #ifdef HAVE_POSIX_SPAWNP
  54. short spawnflags = 0;
  55. #ifdef __APPLE__
  56. spawnflags |= POSIX_SPAWN_START_SUSPENDED;
  57. #if TARGET_OS_EMBEDDED
  58. spawnflags |= POSIX_SPAWN_SETEXEC;
  59. #endif
  60. #endif
  61. posix_spawnattr_t attr;
  62. res = posix_spawnattr_init(&attr);
  63. assert(res == 0);
  64. res = posix_spawnattr_setflags(&attr, spawnflags);
  65. assert(res == 0);
  66. #endif
  67. #ifdef __APPLE__
  68. char *arch = getenv("BSDTEST_ARCH");
  69. if (arch) {
  70. const NXArchInfo *ai = NXGetArchInfoFromName(arch);
  71. if (ai) {
  72. res = posix_spawnattr_setbinpref_np(&attr, 1, (cpu_type_t*)&ai->cputype, NULL);
  73. assert(res == 0);
  74. }
  75. }
  76. #endif
  77. int i;
  78. char** newargv = calloc((size_t)argc, sizeof(void*));
  79. for (i = 1; i < argc; ++i) {
  80. newargv[i-1] = argv[i];
  81. }
  82. newargv[i-1] = NULL;
  83. struct timeval tv_start;
  84. gettimeofday(&tv_start, NULL);
  85. #ifdef HAVE_POSIX_SPAWNP
  86. #ifdef __APPLE__
  87. if (spawnflags & POSIX_SPAWN_SETEXEC) {
  88. pid = fork();
  89. }
  90. #endif
  91. if (!pid) {
  92. res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
  93. if (res) {
  94. errno = res;
  95. perror(newargv[0]);
  96. exit(EXIT_FAILURE);
  97. }
  98. }
  99. #elif defined(__unix__)
  100. (void)res;
  101. pid = fork();
  102. if (pid == -1) {
  103. perror("fork");
  104. exit(EXIT_FAILURE);
  105. } else if (pid == 0) {
  106. // Child process
  107. if (execve(newargv[0], newargv, environ) == -1) {
  108. perror(newargv[0]);
  109. _Exit(EXIT_FAILURE);
  110. }
  111. }
  112. #elif defined(_WIN32)
  113. (void)res;
  114. WCHAR *cmdline = argv_to_command_line(newargv);
  115. if (!cmdline) {
  116. fprintf(stderr, "argv_to_command_line() failed\n");
  117. exit(EXIT_FAILURE);
  118. }
  119. STARTUPINFOW si = {.cb = sizeof(si)};
  120. PROCESS_INFORMATION pi;
  121. BOOL created = CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
  122. DWORD error = GetLastError();
  123. free(cmdline);
  124. if (!created) {
  125. print_winapi_error("CreateProcessW", error);
  126. exit(EXIT_FAILURE);
  127. }
  128. pid = (pid_t)pi.dwProcessId;
  129. #else
  130. #error "bsdtestharness not implemented on this platform"
  131. #endif
  132. //fprintf(stderr, "pid = %d\n", pid);
  133. assert(pid > 0);
  134. #if defined(__linux__)
  135. int status;
  136. struct rusage usage;
  137. struct timeval tv_stop, tv_wall;
  138. int res2 = wait4(pid, &status, 0, &usage);
  139. (void)res2;
  140. gettimeofday(&tv_stop, NULL);
  141. tv_wall.tv_sec = tv_stop.tv_sec - tv_start.tv_sec;
  142. tv_wall.tv_sec -= (tv_stop.tv_usec < tv_start.tv_usec);
  143. tv_wall.tv_usec = labs(tv_stop.tv_usec - tv_start.tv_usec);
  144. assert(res2 != -1);
  145. test_long("Process exited", (WIFEXITED(status) && WEXITSTATUS(status) && WEXITSTATUS(status) != 0xff) || WIFSIGNALED(status), 0);
  146. printf("[PERF]\twall time: %ld.%06ld\n", tv_wall.tv_sec, tv_wall.tv_usec);
  147. printf("[PERF]\tuser time: %ld.%06ld\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
  148. printf("[PERF]\tsystem time: %ld.%06ld\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
  149. printf("[PERF]\tmax resident set size: %ld\n", usage.ru_maxrss);
  150. printf("[PERF]\tpage faults: %ld\n", usage.ru_majflt);
  151. printf("[PERF]\tswaps: %ld\n", usage.ru_nswap);
  152. printf("[PERF]\tvoluntary context switches: %ld\n", usage.ru_nvcsw);
  153. printf("[PERF]\tinvoluntary context switches: %ld\n", usage.ru_nivcsw);
  154. exit((WIFEXITED(status) && WEXITSTATUS(status)) || WIFSIGNALED(status));
  155. #elif defined(_WIN32)
  156. if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_OBJECT_0) {
  157. print_winapi_error("WaitForSingleObject", GetLastError());
  158. exit(EXIT_FAILURE);
  159. }
  160. struct timeval tv_stop, tv_wall;
  161. gettimeofday(&tv_stop, NULL);
  162. tv_wall.tv_sec = tv_stop.tv_sec - tv_start.tv_sec;
  163. tv_wall.tv_sec -= (tv_stop.tv_usec < tv_start.tv_usec);
  164. tv_wall.tv_usec = labs(tv_stop.tv_usec - tv_start.tv_usec);
  165. DWORD status;
  166. if (!GetExitCodeProcess(pi.hProcess, &status)) {
  167. print_winapi_error("GetExitCodeProcess", GetLastError());
  168. exit(EXIT_FAILURE);
  169. }
  170. FILETIME create_time, exit_time, kernel_time, user_time;
  171. if (!GetProcessTimes(pi.hProcess, &create_time, &exit_time, &kernel_time, &user_time)) {
  172. print_winapi_error("GetProcessTimes", GetLastError());
  173. exit(EXIT_FAILURE);
  174. }
  175. struct timeval utime, stime;
  176. filetime_to_timeval(&utime, &user_time);
  177. filetime_to_timeval(&stime, &kernel_time);
  178. PROCESS_MEMORY_COUNTERS counters;
  179. if (!GetProcessMemoryInfo(pi.hProcess, &counters, sizeof(counters))) {
  180. print_winapi_error("GetProcessMemoryInfo", GetLastError());
  181. exit(EXIT_FAILURE);
  182. }
  183. test_long("Process exited", status == 0 || status == 0xff, 1);
  184. printf("[PERF]\twall time: %ld.%06ld\n", tv_wall.tv_sec, tv_wall.tv_usec);
  185. printf("[PERF]\tuser time: %ld.%06ld\n", utime.tv_sec, utime.tv_usec);
  186. printf("[PERF]\tsystem time: %ld.%06ld\n", stime.tv_sec, stime.tv_usec);
  187. printf("[PERF]\tmax working set size: %zu\n", counters.PeakWorkingSetSize);
  188. printf("[PERF]\tpage faults: %lu\n", counters.PageFaultCount);
  189. exit(status ? EXIT_FAILURE : EXIT_SUCCESS);
  190. #else
  191. dispatch_queue_t main_q = dispatch_get_main_queue();
  192. dispatch_source_t tmp_ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q);
  193. assert(tmp_ds);
  194. dispatch_source_set_event_handler(tmp_ds, ^{
  195. int status;
  196. struct rusage usage;
  197. struct timeval tv_stop, tv_wall;
  198. gettimeofday(&tv_stop, NULL);
  199. tv_wall.tv_sec = tv_stop.tv_sec - tv_start.tv_sec;
  200. tv_wall.tv_sec -= (tv_stop.tv_usec < tv_start.tv_usec);
  201. tv_wall.tv_usec = abs(tv_stop.tv_usec - tv_start.tv_usec);
  202. int res2 = wait4(pid, &status, 0, &usage);
  203. assert(res2 != -1);
  204. test_long("Process exited", (WIFEXITED(status) && WEXITSTATUS(status) && WEXITSTATUS(status) != 0xff) || WIFSIGNALED(status), 0);
  205. printf("[PERF]\twall time: %ld.%06d\n", tv_wall.tv_sec, tv_wall.tv_usec);
  206. printf("[PERF]\tuser time: %ld.%06d\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
  207. printf("[PERF]\tsystem time: %ld.%06d\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
  208. printf("[PERF]\tmax resident set size: %ld\n", usage.ru_maxrss);
  209. printf("[PERF]\tpage faults: %ld\n", usage.ru_majflt);
  210. printf("[PERF]\tswaps: %ld\n", usage.ru_nswap);
  211. printf("[PERF]\tvoluntary context switches: %ld\n", usage.ru_nvcsw);
  212. printf("[PERF]\tinvoluntary context switches: %ld\n", usage.ru_nivcsw);
  213. exit((WIFEXITED(status) && WEXITSTATUS(status)) || WIFSIGNALED(status));
  214. });
  215. dispatch_resume(tmp_ds);
  216. uint64_t to = 0;
  217. char *tos = getenv("BSDTEST_TIMEOUT");
  218. if (tos) {
  219. to = strtoul(tos, NULL, 0);
  220. to *= NSEC_PER_SEC;
  221. }
  222. if (!to) {
  223. #if TARGET_OS_EMBEDDED
  224. to = 180LL * NSEC_PER_SEC;
  225. #else
  226. to = 90LL * NSEC_PER_SEC;
  227. #endif
  228. }
  229. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, to), main_q, ^{
  230. kill(pid, SIGKILL);
  231. fprintf(stderr, "Terminating unresponsive process (%0.1lfs)\n", (double)to / NSEC_PER_SEC);
  232. });
  233. signal(SIGINT, SIG_IGN);
  234. tmp_ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, main_q);
  235. assert(tmp_ds);
  236. dispatch_source_set_event_handler(tmp_ds, ^{
  237. fprintf(stderr, "Terminating process due to signal\n");
  238. kill(pid, SIGKILL);
  239. });
  240. dispatch_resume(tmp_ds);
  241. if (spawnflags & POSIX_SPAWN_SETEXEC) {
  242. usleep(USEC_PER_SEC/10);
  243. }
  244. kill(pid, SIGCONT);
  245. dispatch_main();
  246. #endif
  247. return 0;
  248. }