loader.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. int main(int argc, char *argv[])
  110. {
  111. int child_exit_status;
  112. char *test_name;
  113. char *test_args;
  114. int status;
  115. char *launcher;
  116. char *launcher_args;
  117. struct sigaction sa;
  118. test_args = NULL;
  119. timeout = 0;
  120. test_name = argv[1];
  121. if (!test_name)
  122. {
  123. fprintf(stderr, "[error] Need name of program to start\n");
  124. exit(EXIT_FAILURE);
  125. }
  126. if (strstr(test_name, "spmv/dw_block_spmv"))
  127. {
  128. test_args = (char *) malloc(150*sizeof(char));
  129. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  130. }
  131. /* get launcher program */
  132. launcher=getenv("STARPU_CHECK_LAUNCHER");
  133. launcher_args=getenv("STARPU_CHECK_LAUNCHER_ARGS");
  134. /* get user-defined iter_max value */
  135. if (getenv("STARPU_TIMEOUT_ENV"))
  136. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  137. if (timeout <= 0)
  138. timeout = DEFAULT_TIMEOUT;
  139. /* set SIGALARM handler */
  140. sa.sa_flags = 0;
  141. sigemptyset(&sa.sa_mask);
  142. sa.sa_handler = test_cleaner;
  143. if (-1 == sigaction(SIGALRM, &sa, NULL))
  144. perror("sigaction");
  145. child_pid = fork();
  146. if (child_pid == 0)
  147. {
  148. // get a new pgid
  149. if (setpgid(0, 0) == -1)
  150. {
  151. perror("setpgid");
  152. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  153. exit(EXIT_FAILURE);
  154. }
  155. if (launcher)
  156. {
  157. /* "Launchers" such as Valgrind need to be inserted
  158. * after the Libtool-generated wrapper scripts, hence
  159. * this special-case. */
  160. const char *top_builddir = getenv ("top_builddir");
  161. if (top_builddir != NULL)
  162. {
  163. char *argv[100];
  164. int i=3;
  165. char libtool[strlen(top_builddir)
  166. + sizeof("libtool") + 1];
  167. strcpy(libtool, top_builddir);
  168. strcat(libtool, "/libtool");
  169. argv[0] = libtool;
  170. argv[1] = "--mode=execute";
  171. argv[2] = launcher;
  172. argv[i] = strtok(launcher_args, " ");
  173. while (argv[i])
  174. {
  175. i++;
  176. argv[i] = strtok(NULL, " ");
  177. }
  178. argv[i] = test_name;
  179. argv[i+1] = NULL;
  180. execvp(*argv, argv);
  181. }
  182. else
  183. {
  184. fprintf(stderr,
  185. "warning: $top_builddir undefined, "
  186. "so $STARPU_CHECK_LAUNCHER ignored\n");
  187. execl(test_name, test_name, test_args, NULL);
  188. }
  189. }
  190. else
  191. execl(test_name, test_name, test_args, NULL);
  192. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  193. exit(EXIT_FAILURE);
  194. }
  195. if (child_pid == -1)
  196. {
  197. fprintf(stderr, "[error] fork. test marked as failed\n");
  198. exit(EXIT_FAILURE);
  199. }
  200. alarm(timeout);
  201. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  202. {
  203. if (WIFEXITED(child_exit_status))
  204. {
  205. status = WEXITSTATUS(child_exit_status);
  206. if (status == EXIT_SUCCESS)
  207. {
  208. alarm(0);
  209. }
  210. else
  211. {
  212. if (status != AUTOTEST_SKIPPED_TEST)
  213. fprintf(stdout, "`%s' exited with return code %d\n",
  214. test_name, status);
  215. return status;
  216. }
  217. }
  218. else if (WIFSIGNALED(child_exit_status))
  219. {
  220. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  221. test_name, WTERMSIG(child_exit_status));
  222. launch_gdb(test_name);
  223. return EXIT_FAILURE;
  224. }
  225. else
  226. {
  227. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  228. test_name);
  229. return EXIT_FAILURE;
  230. }
  231. }
  232. return EXIT_SUCCESS;
  233. }