loader.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_ALL_COMMAND "thread apply all bt full"
  36. # define GDB_COMMAND "bt full"
  37. int err;
  38. pid_t pid;
  39. struct stat st;
  40. const char *top_builddir;
  41. char *gdb;
  42. err = stat(CORE_FILE, &st);
  43. if (err != 0)
  44. {
  45. fprintf(stderr, "while looking for core file of %s: %s: %m\n",
  46. exe, CORE_FILE);
  47. return;
  48. }
  49. if (!(st.st_mode & S_IFREG))
  50. {
  51. fprintf(stderr, CORE_FILE ": not a regular file\n");
  52. return;
  53. }
  54. top_builddir = getenv("top_builddir");
  55. pid = fork();
  56. switch (pid)
  57. {
  58. case 0: /* kid */
  59. if (top_builddir != NULL)
  60. {
  61. /* Run gdb with Libtool. */
  62. gdb = alloca(strlen(top_builddir)
  63. + sizeof("libtool") + 1);
  64. strcpy(gdb, top_builddir);
  65. strcat(gdb, "/libtool");
  66. err = execl(gdb, "gdb", "--mode=execute",
  67. STARPU_GDB_PATH, "--batch",
  68. "-ex", GDB_COMMAND,
  69. "-ex", GDB_ALL_COMMAND,
  70. exe, CORE_FILE, NULL);
  71. }
  72. else
  73. {
  74. /* Run gdb directly */
  75. gdb = STARPU_GDB_PATH;
  76. err = execl(gdb, "gdb", "--batch",
  77. "-ex", GDB_COMMAND,
  78. "-ex", GDB_ALL_COMMAND,
  79. exe, CORE_FILE, NULL);
  80. }
  81. if (err != 0)
  82. {
  83. fprintf(stderr, "while launching `%s': %m\n", gdb);
  84. exit(EXIT_FAILURE);
  85. }
  86. exit(EXIT_SUCCESS);
  87. break;
  88. case -1:
  89. fprintf(stderr, "fork: %m\n");
  90. return;
  91. default: /* parent */
  92. {
  93. pid_t who;
  94. int status;
  95. who = waitpid(pid, &status, 0);
  96. if (who != pid)
  97. fprintf(stderr, "while waiting for gdb "
  98. "process %d: %m\n", pid);
  99. }
  100. }
  101. # undef GDB_COMMAND
  102. # undef GDB_ALL_COMMAND
  103. # undef CORE_FILE
  104. #endif /* STARPU_GDB_PATH */
  105. }
  106. static char *test_name;
  107. static void test_cleaner(int sig)
  108. {
  109. pid_t child_gid;
  110. // send signal to all loader family members
  111. fprintf(stderr, "[error] test %s has been blocked for %d seconds. Mark it as failed\n", test_name, timeout);
  112. child_gid = getpgid(child_pid);
  113. launch_gdb(test_name);
  114. kill(-child_gid, SIGQUIT);
  115. exit(EXIT_FAILURE);
  116. }
  117. static void decode(char **src, char *motif, const char *value)
  118. {
  119. if (*src)
  120. {
  121. char *y = strstr(*src, motif);
  122. if (y && value == NULL)
  123. {
  124. fprintf(stderr, "error: $%s undefined\n", motif);
  125. exit(EXIT_FAILURE);
  126. }
  127. while (y)
  128. {
  129. char *neo = malloc((strlen(*src)-strlen(motif)+strlen(value)) * sizeof(char));
  130. char *to = neo;
  131. to = strncpy(to, *src, strlen(*src)-strlen(y)); to += strlen(*src)-strlen(y);
  132. to = strcpy(to, value); to += strlen(value);
  133. strcpy(to, y+strlen(motif));
  134. *src = strdup(neo);
  135. y = strstr(*src, motif);
  136. }
  137. }
  138. }
  139. int main(int argc, char *argv[])
  140. {
  141. int child_exit_status;
  142. char *test_args;
  143. int status;
  144. char *launcher;
  145. char *launcher_args;
  146. struct sigaction sa;
  147. int ret;
  148. struct timeval start;
  149. struct timeval end;
  150. double timing;
  151. test_args = NULL;
  152. timeout = 0;
  153. test_name = argv[1];
  154. if (!test_name)
  155. {
  156. fprintf(stderr, "[error] Need name of program to start\n");
  157. exit(EXIT_FAILURE);
  158. }
  159. if (strstr(test_name, "spmv/dw_block_spmv"))
  160. {
  161. test_args = (char *) malloc(150*sizeof(char));
  162. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  163. }
  164. if (strstr(test_name, "starpu_perfmodel_display"))
  165. {
  166. test_args = (char *) malloc(5*sizeof(char));
  167. sprintf(test_args, "-l");
  168. }
  169. if (strstr(test_name, "starpu_perfmodel_plot"))
  170. {
  171. test_args = (char *) malloc(5*sizeof(char));
  172. sprintf(test_args, "-l");
  173. }
  174. /* get launcher program */
  175. launcher=getenv("STARPU_CHECK_LAUNCHER");
  176. launcher_args=getenv("STARPU_CHECK_LAUNCHER_ARGS");
  177. /* get user-defined iter_max value */
  178. if (getenv("STARPU_TIMEOUT_ENV"))
  179. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  180. if (timeout <= 0)
  181. timeout = DEFAULT_TIMEOUT;
  182. /* set SIGALARM handler */
  183. sa.sa_flags = 0;
  184. sigemptyset(&sa.sa_mask);
  185. sa.sa_handler = test_cleaner;
  186. if (-1 == sigaction(SIGALRM, &sa, NULL))
  187. perror("sigaction");
  188. child_pid = fork();
  189. if (child_pid == 0)
  190. {
  191. // get a new pgid
  192. if (setpgid(0, 0) == -1)
  193. {
  194. perror("setpgid");
  195. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  196. exit(EXIT_FAILURE);
  197. }
  198. if (launcher)
  199. {
  200. /* "Launchers" such as Valgrind need to be inserted
  201. * after the Libtool-generated wrapper scripts, hence
  202. * this special-case. */
  203. const char *top_builddir = getenv ("top_builddir");
  204. const char *top_srcdir = getenv("top_srcdir");
  205. if (top_builddir != NULL)
  206. {
  207. char *launcher_argv[100];
  208. int i=3;
  209. char libtool[strlen(top_builddir)
  210. + sizeof("libtool") + 1];
  211. strcpy(libtool, top_builddir);
  212. strcat(libtool, "/libtool");
  213. decode(&launcher_args, "@top_srcdir@", top_srcdir);
  214. launcher_argv[0] = libtool;
  215. launcher_argv[1] = "--mode=execute";
  216. launcher_argv[2] = launcher;
  217. launcher_argv[i] = strtok(launcher_args, " ");
  218. while (launcher_argv[i])
  219. {
  220. i++;
  221. launcher_argv[i] = strtok(NULL, " ");
  222. }
  223. launcher_argv[i] = test_name;
  224. launcher_argv[i+1] = test_args;
  225. launcher_argv[i+2] = NULL;
  226. execvp(*launcher_argv, launcher_argv);
  227. }
  228. else
  229. {
  230. fprintf(stderr,
  231. "warning: $top_builddir undefined, "
  232. "so $STARPU_CHECK_LAUNCHER ignored\n");
  233. execl(test_name, test_name, test_args, NULL);
  234. }
  235. }
  236. else
  237. execl(test_name, test_name, test_args, NULL);
  238. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  239. exit(EXIT_FAILURE);
  240. }
  241. if (child_pid == -1)
  242. {
  243. fprintf(stderr, "[error] fork. test marked as failed\n");
  244. exit(EXIT_FAILURE);
  245. }
  246. ret = EXIT_SUCCESS;
  247. gettimeofday(&start, NULL);
  248. alarm(timeout);
  249. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  250. {
  251. if (WIFEXITED(child_exit_status))
  252. {
  253. status = WEXITSTATUS(child_exit_status);
  254. if (status == EXIT_SUCCESS)
  255. {
  256. alarm(0);
  257. }
  258. else
  259. {
  260. if (status != AUTOTEST_SKIPPED_TEST)
  261. fprintf(stdout, "`%s' exited with return code %d\n",
  262. test_name, status);
  263. ret = status;
  264. }
  265. }
  266. else if (WIFSIGNALED(child_exit_status))
  267. {
  268. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  269. test_name, WTERMSIG(child_exit_status));
  270. launch_gdb(test_name);
  271. ret = EXIT_FAILURE;
  272. }
  273. else
  274. {
  275. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  276. test_name);
  277. ret = EXIT_FAILURE;
  278. }
  279. }
  280. gettimeofday(&end, NULL);
  281. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  282. fprintf(stderr, "#Execution_time_in_seconds %f %s\n", timing/1000000, test_name);
  283. return ret;
  284. }