loader.c 7.0 KB

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