loader.c 7.4 KB

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