loader.c 7.5 KB

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