loader.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2014-2016 Université de Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/config.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/wait.h>
  20. #include <sys/resource.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  27. #include <windows.h>
  28. #else
  29. #include <sys/time.h>
  30. #endif
  31. #define DEFAULT_TIMEOUT 1800
  32. #define AUTOTEST_SKIPPED_TEST 77
  33. static pid_t child_pid = 0;
  34. static int timeout;
  35. #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
  36. static int mygettimeofday(struct timeval *tv, void *tz)
  37. {
  38. if (tv)
  39. {
  40. FILETIME ft;
  41. unsigned long long res;
  42. GetSystemTimeAsFileTime(&ft);
  43. /* 100-nanosecond intervals since January 1, 1601 */
  44. res = ft.dwHighDateTime;
  45. res <<= 32;
  46. res |= ft.dwLowDateTime;
  47. res /= 10;
  48. /* Now we have microseconds */
  49. res -= (((1970-1601)*365) + 89) * 24ULL * 3600ULL * 1000000ULL;
  50. /* Now we are based on epoch */
  51. tv->tv_sec = res / 1000000ULL;
  52. tv->tv_usec = res % 1000000ULL;
  53. }
  54. }
  55. #else
  56. #define mygettimeofday(tv,tz) gettimeofday(tv,tz)
  57. #endif
  58. #ifdef STARPU_GDB_PATH
  59. static int try_launch_gdb(const char *exe, const char *core)
  60. {
  61. # define GDB_ALL_COMMAND "thread apply all bt full"
  62. # define GDB_COMMAND "bt full"
  63. int err;
  64. pid_t pid;
  65. struct stat st;
  66. const char *top_builddir;
  67. char *gdb;
  68. err = stat(core, &st);
  69. if (err != 0)
  70. {
  71. fprintf(stderr, "while looking for core file of %s: %s: %m\n",
  72. exe, core);
  73. return -1;
  74. }
  75. if (!(st.st_mode & S_IFREG))
  76. {
  77. fprintf(stderr, "%s: not a regular file\n", core);
  78. return -1;
  79. }
  80. top_builddir = getenv("top_builddir");
  81. pid = fork();
  82. switch (pid)
  83. {
  84. case 0: /* kid */
  85. if (top_builddir != NULL)
  86. {
  87. /* Run gdb with Libtool. */
  88. gdb = alloca(strlen(top_builddir)
  89. + sizeof("/libtool") + 1);
  90. strcpy(gdb, top_builddir);
  91. strcat(gdb, "/libtool");
  92. err = execl(gdb, "gdb", "--mode=execute",
  93. STARPU_GDB_PATH, "--batch",
  94. "-ex", GDB_COMMAND,
  95. "-ex", GDB_ALL_COMMAND,
  96. exe, core, NULL);
  97. }
  98. else
  99. {
  100. /* Run gdb directly */
  101. gdb = STARPU_GDB_PATH;
  102. err = execl(gdb, "gdb", "--batch",
  103. "-ex", GDB_COMMAND,
  104. "-ex", GDB_ALL_COMMAND,
  105. exe, core, NULL);
  106. }
  107. if (err != 0)
  108. {
  109. fprintf(stderr, "while launching `%s': %m\n", gdb);
  110. exit(EXIT_FAILURE);
  111. }
  112. exit(EXIT_SUCCESS);
  113. break;
  114. case -1:
  115. fprintf(stderr, "fork: %m\n");
  116. return -1;
  117. default: /* parent */
  118. {
  119. pid_t who;
  120. int status;
  121. who = waitpid(pid, &status, 0);
  122. if (who != pid)
  123. fprintf(stderr, "while waiting for gdb "
  124. "process %d: %m\n", pid);
  125. }
  126. }
  127. return 0;
  128. # undef GDB_COMMAND
  129. # undef GDB_ALL_COMMAND
  130. }
  131. #endif /* STARPU_GDB_PATH */
  132. static void launch_gdb(const char *exe)
  133. {
  134. #ifdef STARPU_GDB_PATH
  135. char s[32];
  136. snprintf(s, sizeof(s), "core.%d", child_pid);
  137. if (try_launch_gdb(exe, s) < 0)
  138. try_launch_gdb(exe, "core");
  139. #endif /* STARPU_GDB_PATH */
  140. }
  141. static char *test_name;
  142. static void test_cleaner(int sig)
  143. {
  144. pid_t child_gid;
  145. // send signal to all loader family members
  146. fprintf(stderr, "[error] test %s has been blocked for %d seconds. Mark it as failed\n", test_name, timeout);
  147. child_gid = getpgid(child_pid);
  148. launch_gdb(test_name);
  149. kill(-child_gid, SIGQUIT);
  150. exit(EXIT_FAILURE);
  151. }
  152. static int _decode(char **src, char *motif, const char *value)
  153. {
  154. char *found;
  155. found = strstr(*src, motif);
  156. if (found == NULL) return 0;
  157. char *new_src = calloc(1, strlen(*src)-strlen(motif)+strlen(value)+1);
  158. strncpy(new_src, *src, found - *src);
  159. strcat(new_src, value);
  160. strcat(new_src, found+strlen(motif));
  161. *src = new_src;
  162. return 1;
  163. }
  164. static void decode(char **src, char *motif, const char *value)
  165. {
  166. if (*src)
  167. {
  168. if (strstr(*src, motif) && value == NULL)
  169. {
  170. fprintf(stderr, "error: $%s undefined\n", motif);
  171. exit(EXIT_FAILURE);
  172. }
  173. int d = _decode(src, motif, value);
  174. while (d)
  175. d = _decode(src, motif, value);
  176. }
  177. }
  178. int main(int argc, char *argv[])
  179. {
  180. int child_exit_status;
  181. char *test_args;
  182. int status;
  183. char *launcher;
  184. char *launcher_args;
  185. struct sigaction sa;
  186. int ret;
  187. struct timeval start;
  188. struct timeval end;
  189. double timing;
  190. test_args = NULL;
  191. timeout = 0;
  192. test_name = argv[1];
  193. if (!test_name)
  194. {
  195. fprintf(stderr, "[error] Need name of program to start\n");
  196. exit(EXIT_FAILURE);
  197. }
  198. if (strstr(test_name, "spmv/dw_block_spmv"))
  199. {
  200. test_args = (char *) calloc(150, sizeof(char));
  201. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  202. }
  203. if (strstr(test_name, "starpu_perfmodel_display"))
  204. {
  205. test_args = (char *) calloc(5, sizeof(char));
  206. sprintf(test_args, "-l");
  207. }
  208. if (strstr(test_name, "starpu_perfmodel_plot"))
  209. {
  210. test_args = (char *) calloc(5, sizeof(char));
  211. sprintf(test_args, "-l");
  212. }
  213. /* get launcher program */
  214. launcher=getenv("STARPU_CHECK_LAUNCHER");
  215. launcher_args=getenv("STARPU_CHECK_LAUNCHER_ARGS");
  216. if (launcher_args)
  217. launcher_args=strdup(launcher_args);
  218. /* get user-defined iter_max value */
  219. if (getenv("STARPU_TIMEOUT_ENV"))
  220. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  221. if (timeout <= 0)
  222. timeout = DEFAULT_TIMEOUT;
  223. /* set SIGALARM handler */
  224. sa.sa_flags = 0;
  225. sigemptyset(&sa.sa_mask);
  226. sa.sa_handler = test_cleaner;
  227. if (-1 == sigaction(SIGALRM, &sa, NULL))
  228. perror("sigaction");
  229. child_pid = fork();
  230. if (child_pid == 0)
  231. {
  232. // get a new pgid
  233. if (setpgid(0, 0) == -1)
  234. {
  235. perror("setpgid");
  236. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  237. exit(EXIT_FAILURE);
  238. }
  239. if (launcher)
  240. {
  241. /* "Launchers" such as Valgrind need to be inserted
  242. * after the Libtool-generated wrapper scripts, hence
  243. * this special-case. */
  244. const char *top_builddir = getenv ("top_builddir");
  245. const char *top_srcdir = getenv("top_srcdir");
  246. if (top_builddir != NULL)
  247. {
  248. char *launcher_argv[100];
  249. int i=3;
  250. char libtool[strlen(top_builddir)
  251. + sizeof("libtool") + 1];
  252. strcpy(libtool, top_builddir);
  253. strcat(libtool, "/libtool");
  254. decode(&launcher_args, "@top_srcdir@", top_srcdir);
  255. launcher_argv[0] = libtool;
  256. launcher_argv[1] = "--mode=execute";
  257. launcher_argv[2] = launcher;
  258. launcher_argv[i] = strtok(launcher_args, " ");
  259. while (launcher_argv[i])
  260. {
  261. i++;
  262. launcher_argv[i] = strtok(NULL, " ");
  263. }
  264. launcher_argv[i] = test_name;
  265. launcher_argv[i+1] = test_args;
  266. launcher_argv[i+2] = NULL;
  267. execvp(*launcher_argv, launcher_argv);
  268. }
  269. else
  270. {
  271. fprintf(stderr,
  272. "warning: $top_builddir undefined, "
  273. "so $STARPU_CHECK_LAUNCHER ignored\n");
  274. execl(test_name, test_name, test_args, NULL);
  275. }
  276. }
  277. else
  278. execl(test_name, test_name, test_args, NULL);
  279. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  280. exit(EXIT_FAILURE);
  281. }
  282. if (child_pid == -1)
  283. {
  284. fprintf(stderr, "[error] fork. test marked as failed\n");
  285. exit(EXIT_FAILURE);
  286. }
  287. free(test_args);
  288. ret = EXIT_SUCCESS;
  289. gettimeofday(&start, NULL);
  290. alarm(timeout);
  291. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  292. {
  293. if (WIFEXITED(child_exit_status))
  294. {
  295. status = WEXITSTATUS(child_exit_status);
  296. if (status == EXIT_SUCCESS)
  297. {
  298. alarm(0);
  299. }
  300. else
  301. {
  302. if (status != AUTOTEST_SKIPPED_TEST)
  303. fprintf(stdout, "`%s' exited with return code %d\n",
  304. test_name, status);
  305. ret = status;
  306. }
  307. }
  308. else if (WIFSIGNALED(child_exit_status))
  309. {
  310. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  311. test_name, WTERMSIG(child_exit_status));
  312. launch_gdb(test_name);
  313. ret = EXIT_FAILURE;
  314. }
  315. else
  316. {
  317. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  318. test_name);
  319. ret = EXIT_FAILURE;
  320. }
  321. }
  322. gettimeofday(&end, NULL);
  323. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  324. fprintf(stderr, "#Execution_time_in_seconds %f %s\n", timing/1000000, test_name);
  325. return ret;
  326. }