loader.c 7.0 KB

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