loader.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2014-2015 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. static void launch_gdb(const char *exe)
  59. {
  60. #ifdef STARPU_GDB_PATH
  61. # define CORE_FILE "core"
  62. # define GDB_ALL_COMMAND "thread apply all bt full"
  63. # define GDB_COMMAND "bt full"
  64. int err;
  65. pid_t pid;
  66. struct stat st;
  67. const char *top_builddir;
  68. char *gdb;
  69. err = stat(CORE_FILE, &st);
  70. if (err != 0)
  71. {
  72. fprintf(stderr, "while looking for core file of %s: %s: %m\n",
  73. exe, CORE_FILE);
  74. return;
  75. }
  76. if (!(st.st_mode & S_IFREG))
  77. {
  78. fprintf(stderr, CORE_FILE ": not a regular file\n");
  79. return;
  80. }
  81. top_builddir = getenv("top_builddir");
  82. pid = fork();
  83. switch (pid)
  84. {
  85. case 0: /* kid */
  86. if (top_builddir != NULL)
  87. {
  88. /* Run gdb with Libtool. */
  89. gdb = alloca(strlen(top_builddir)
  90. + sizeof("/libtool") + 1);
  91. strcpy(gdb, top_builddir);
  92. strcat(gdb, "/libtool");
  93. err = execl(gdb, "gdb", "--mode=execute",
  94. STARPU_GDB_PATH, "--batch",
  95. "-ex", GDB_COMMAND,
  96. "-ex", GDB_ALL_COMMAND,
  97. exe, CORE_FILE, NULL);
  98. }
  99. else
  100. {
  101. /* Run gdb directly */
  102. gdb = STARPU_GDB_PATH;
  103. err = execl(gdb, "gdb", "--batch",
  104. "-ex", GDB_COMMAND,
  105. "-ex", GDB_ALL_COMMAND,
  106. exe, CORE_FILE, NULL);
  107. }
  108. if (err != 0)
  109. {
  110. fprintf(stderr, "while launching `%s': %m\n", gdb);
  111. exit(EXIT_FAILURE);
  112. }
  113. exit(EXIT_SUCCESS);
  114. break;
  115. case -1:
  116. fprintf(stderr, "fork: %m\n");
  117. return;
  118. default: /* parent */
  119. {
  120. pid_t who;
  121. int status;
  122. who = waitpid(pid, &status, 0);
  123. if (who != pid)
  124. fprintf(stderr, "while waiting for gdb "
  125. "process %d: %m\n", pid);
  126. }
  127. }
  128. # undef GDB_COMMAND
  129. # undef GDB_ALL_COMMAND
  130. # undef CORE_FILE
  131. #endif /* STARPU_GDB_PATH */
  132. }
  133. static char *test_name;
  134. static void test_cleaner(int sig)
  135. {
  136. pid_t child_gid;
  137. // send signal to all loader family members
  138. fprintf(stderr, "[error] test %s has been blocked for %d seconds. Mark it as failed\n", test_name, timeout);
  139. child_gid = getpgid(child_pid);
  140. launch_gdb(test_name);
  141. kill(-child_gid, SIGQUIT);
  142. exit(EXIT_FAILURE);
  143. }
  144. static int _decode(char **src, char *motif, const char *value)
  145. {
  146. char *found;
  147. found = strstr(*src, motif);
  148. if (found == NULL) return 0;
  149. char *new_src = calloc(1, strlen(*src)-strlen(motif)+strlen(value)+1);
  150. strncpy(new_src, *src, found - *src);
  151. strcat(new_src, value);
  152. strcat(new_src, found+strlen(motif));
  153. *src = new_src;
  154. return 1;
  155. }
  156. static void decode(char **src, char *motif, const char *value)
  157. {
  158. if (*src)
  159. {
  160. if (strstr(*src, motif) && value == NULL)
  161. {
  162. fprintf(stderr, "error: $%s undefined\n", motif);
  163. exit(EXIT_FAILURE);
  164. }
  165. int d = _decode(src, motif, value);
  166. while (d)
  167. d = _decode(src, motif, value);
  168. }
  169. }
  170. int main(int argc, char *argv[])
  171. {
  172. int child_exit_status;
  173. char *test_args;
  174. int status;
  175. char *launcher;
  176. char *launcher_args;
  177. struct sigaction sa;
  178. int ret;
  179. struct timeval start;
  180. struct timeval end;
  181. double timing;
  182. test_args = NULL;
  183. timeout = 0;
  184. test_name = argv[1];
  185. if (!test_name)
  186. {
  187. fprintf(stderr, "[error] Need name of program to start\n");
  188. exit(EXIT_FAILURE);
  189. }
  190. if (strstr(test_name, "spmv/dw_block_spmv"))
  191. {
  192. test_args = (char *) calloc(150, sizeof(char));
  193. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  194. }
  195. if (strstr(test_name, "starpu_perfmodel_display"))
  196. {
  197. test_args = (char *) calloc(5, sizeof(char));
  198. sprintf(test_args, "-l");
  199. }
  200. if (strstr(test_name, "starpu_perfmodel_plot"))
  201. {
  202. test_args = (char *) calloc(5, sizeof(char));
  203. sprintf(test_args, "-l");
  204. }
  205. /* get launcher program */
  206. launcher=getenv("STARPU_CHECK_LAUNCHER");
  207. launcher_args=getenv("STARPU_CHECK_LAUNCHER_ARGS");
  208. if (launcher_args)
  209. launcher_args=strdup(launcher_args);
  210. /* get user-defined iter_max value */
  211. if (getenv("STARPU_TIMEOUT_ENV"))
  212. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  213. if (timeout <= 0)
  214. timeout = DEFAULT_TIMEOUT;
  215. /* set SIGALARM handler */
  216. sa.sa_flags = 0;
  217. sigemptyset(&sa.sa_mask);
  218. sa.sa_handler = test_cleaner;
  219. if (-1 == sigaction(SIGALRM, &sa, NULL))
  220. perror("sigaction");
  221. child_pid = fork();
  222. if (child_pid == 0)
  223. {
  224. // get a new pgid
  225. if (setpgid(0, 0) == -1)
  226. {
  227. perror("setpgid");
  228. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  229. exit(EXIT_FAILURE);
  230. }
  231. if (launcher)
  232. {
  233. /* "Launchers" such as Valgrind need to be inserted
  234. * after the Libtool-generated wrapper scripts, hence
  235. * this special-case. */
  236. const char *top_builddir = getenv ("top_builddir");
  237. const char *top_srcdir = getenv("top_srcdir");
  238. if (top_builddir != NULL)
  239. {
  240. char *launcher_argv[100];
  241. int i=3;
  242. char libtool[strlen(top_builddir)
  243. + sizeof("libtool") + 1];
  244. strcpy(libtool, top_builddir);
  245. strcat(libtool, "/libtool");
  246. decode(&launcher_args, "@top_srcdir@", top_srcdir);
  247. launcher_argv[0] = libtool;
  248. launcher_argv[1] = "--mode=execute";
  249. launcher_argv[2] = launcher;
  250. launcher_argv[i] = strtok(launcher_args, " ");
  251. while (launcher_argv[i])
  252. {
  253. i++;
  254. launcher_argv[i] = strtok(NULL, " ");
  255. }
  256. launcher_argv[i] = test_name;
  257. launcher_argv[i+1] = test_args;
  258. launcher_argv[i+2] = NULL;
  259. execvp(*launcher_argv, launcher_argv);
  260. }
  261. else
  262. {
  263. fprintf(stderr,
  264. "warning: $top_builddir undefined, "
  265. "so $STARPU_CHECK_LAUNCHER ignored\n");
  266. execl(test_name, test_name, test_args, NULL);
  267. }
  268. }
  269. else
  270. execl(test_name, test_name, test_args, NULL);
  271. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  272. exit(EXIT_FAILURE);
  273. }
  274. if (child_pid == -1)
  275. {
  276. fprintf(stderr, "[error] fork. test marked as failed\n");
  277. exit(EXIT_FAILURE);
  278. }
  279. ret = EXIT_SUCCESS;
  280. gettimeofday(&start, NULL);
  281. alarm(timeout);
  282. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  283. {
  284. if (WIFEXITED(child_exit_status))
  285. {
  286. status = WEXITSTATUS(child_exit_status);
  287. if (status == EXIT_SUCCESS)
  288. {
  289. alarm(0);
  290. }
  291. else
  292. {
  293. if (status != AUTOTEST_SKIPPED_TEST)
  294. fprintf(stdout, "`%s' exited with return code %d\n",
  295. test_name, status);
  296. ret = status;
  297. }
  298. }
  299. else if (WIFSIGNALED(child_exit_status))
  300. {
  301. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  302. test_name, WTERMSIG(child_exit_status));
  303. launch_gdb(test_name);
  304. ret = EXIT_FAILURE;
  305. }
  306. else
  307. {
  308. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  309. test_name);
  310. ret = EXIT_FAILURE;
  311. }
  312. }
  313. gettimeofday(&end, NULL);
  314. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  315. fprintf(stderr, "#Execution_time_in_seconds %f %s\n", timing/1000000, test_name);
  316. return ret;
  317. }