loader.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <sys/wait.h>
  17. #include <sys/resource.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #include <common/config.h>
  24. #define DEFAULT_TIMEOUT 600
  25. #define AUTOTEST_SKIPPED_TEST 77
  26. static pid_t child_pid = 0;
  27. static int timeout;
  28. static void test_cleaner(int sig)
  29. {
  30. pid_t child_gid;
  31. // send signal to all loader family members
  32. fprintf(stderr, "[error] test is blocked since %d seconds. Mark it as failed\n", timeout);
  33. child_gid = getpgid(child_pid);
  34. kill(-child_gid, SIGKILL);
  35. exit(EXIT_FAILURE);
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. int child_exit_status;
  40. char *test_name;
  41. char *test_args;
  42. int status;
  43. char *launcher;
  44. char *launcher_args;
  45. struct sigaction sa;
  46. test_args = NULL;
  47. timeout = 0;
  48. test_name = argv[1];
  49. if (!test_name)
  50. {
  51. fprintf(stderr, "[error] Need name of program to start\n");
  52. exit(EXIT_FAILURE);
  53. }
  54. if (strstr(test_name, "spmv/dw_block_spmv"))
  55. {
  56. test_args = (char *) malloc(150*sizeof(char));
  57. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  58. }
  59. /* get launcher program */
  60. launcher=getenv("STARPU_CHECK_LAUNCHER");
  61. launcher_args=getenv("STARPU_CHECK_LAUNCHER_ARGS");
  62. /* get user-defined iter_max value */
  63. if (getenv("STARPU_TIMEOUT_ENV"))
  64. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  65. if (timeout <= 0)
  66. timeout = DEFAULT_TIMEOUT;
  67. /* set SIGALARM handler */
  68. sa.sa_flags = 0;
  69. sigemptyset(&sa.sa_mask);
  70. sa.sa_handler = test_cleaner;
  71. if (-1 == sigaction(SIGALRM, &sa, NULL))
  72. perror("sigaction");
  73. child_pid = fork();
  74. if (child_pid == 0)
  75. {
  76. // get a new pgid
  77. if (setpgid(0, 0) == -1)
  78. {
  79. perror("setpgid");
  80. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. if (launcher)
  84. {
  85. /* "Launchers" such as Valgrind need to be inserted
  86. * after the Libtool-generated wrapper scripts, hence
  87. * this special-case. */
  88. const char *top_builddir = getenv ("top_builddir");
  89. if (top_builddir != NULL)
  90. {
  91. char *argv[100];
  92. int i=3;
  93. char libtool[strlen(top_builddir)
  94. + sizeof("libtool") + 1];
  95. strcpy(libtool, top_builddir);
  96. strcat(libtool, "/libtool");
  97. argv[0] = libtool;
  98. argv[1] = "--mode=execute";
  99. argv[2] = launcher;
  100. argv[i] = strtok(launcher_args, " ");
  101. while (argv[i])
  102. {
  103. i++;
  104. argv[i] = strtok(NULL, " ");
  105. }
  106. argv[i] = test_name;
  107. argv[i+1] = NULL;
  108. execvp(*argv, argv);
  109. }
  110. else
  111. {
  112. fprintf(stderr,
  113. "warning: $top_builddir undefined, "
  114. "so $STARPU_CHECK_LAUNCHER ignored\n");
  115. execl(test_name, test_name, test_args, NULL);
  116. }
  117. }
  118. else
  119. execl(test_name, test_name, test_args, NULL);
  120. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  121. exit(EXIT_FAILURE);
  122. }
  123. if (child_pid == -1)
  124. {
  125. fprintf(stderr, "[error] fork. test marked as failed\n");
  126. exit(EXIT_FAILURE);
  127. }
  128. alarm(timeout);
  129. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  130. {
  131. if (WIFEXITED(child_exit_status))
  132. {
  133. status = WEXITSTATUS(child_exit_status);
  134. if (status == EXIT_SUCCESS)
  135. {
  136. alarm(0);
  137. }
  138. else
  139. {
  140. if (status != AUTOTEST_SKIPPED_TEST)
  141. fprintf(stdout, "`%s' exited with return code %d\n",
  142. test_name, status);
  143. return status;
  144. }
  145. }
  146. else if (WIFSIGNALED(child_exit_status))
  147. {
  148. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  149. test_name, WTERMSIG(child_exit_status));
  150. return EXIT_FAILURE;
  151. }
  152. else
  153. {
  154. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  155. test_name);
  156. return EXIT_FAILURE;
  157. }
  158. }
  159. return EXIT_SUCCESS;
  160. }