loader.c 8.1 KB

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