loader.c 7.2 KB

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