loader.c 6.4 KB

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