loader.c 7.1 KB

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