loader.c 5.8 KB

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