loader.c 7.2 KB

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