loader.c 7.3 KB

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