loader.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. struct sigaction sa;
  45. test_args = NULL;
  46. timeout = 0;
  47. test_name = argv[1];
  48. if (!test_name)
  49. {
  50. fprintf(stderr, "[error] Need name of program to start\n");
  51. exit(EXIT_FAILURE);
  52. }
  53. if (strstr(test_name, "spmv/dw_block_spmv"))
  54. {
  55. test_args = (char *) malloc(150*sizeof(char));
  56. sprintf(test_args, "%s/examples/spmv/matrix_market/examples/fidapm05.mtx", STARPU_SRC_DIR);
  57. }
  58. /* get launcher program */
  59. launcher=getenv("STARPU_CHECK_LAUNCHER");
  60. /* get user-defined iter_max value */
  61. if (getenv("STARPU_TIMEOUT_ENV"))
  62. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  63. if (timeout <= 0)
  64. timeout = DEFAULT_TIMEOUT;
  65. /* set SIGALARM handler */
  66. sa.sa_flags = 0;
  67. sigemptyset(&sa.sa_mask);
  68. sa.sa_handler = test_cleaner;
  69. if (-1 == sigaction(SIGALRM, &sa, NULL))
  70. perror("sigaction");
  71. child_pid = fork();
  72. if (child_pid == 0)
  73. {
  74. // get a new pgid
  75. if (setpgid(0, 0) == -1)
  76. {
  77. perror("setpgid");
  78. fprintf(stderr, "[error] setpgid. Mark test as failed\n");
  79. exit(EXIT_FAILURE);
  80. }
  81. if (launcher)
  82. {
  83. /* "Launchers" such as Valgrind need to be inserted
  84. * after the Libtool-generated wrapper scripts, hence
  85. * this special-case. */
  86. const char *top_builddir = getenv ("top_builddir");
  87. if (top_builddir != NULL)
  88. {
  89. char libtool[strlen(top_builddir)
  90. + sizeof("libtool") + 1];
  91. strcpy(libtool, top_builddir);
  92. strcat(libtool, "/libtool");
  93. execl(libtool, test_name, "--mode=execute",
  94. launcher, test_name, test_args, NULL);
  95. }
  96. else
  97. {
  98. fprintf(stderr,
  99. "warning: $top_builddir undefined, "
  100. "so $STARPU_CHECK_LAUNCHER ignored\n");
  101. execl(test_name, test_name, test_args, NULL);
  102. }
  103. }
  104. else
  105. execl(test_name, test_name, test_args, NULL);
  106. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  107. exit(EXIT_FAILURE);
  108. }
  109. if (child_pid == -1)
  110. {
  111. fprintf(stderr, "[error] fork. test marked as failed\n");
  112. exit(EXIT_FAILURE);
  113. }
  114. alarm(timeout);
  115. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  116. {
  117. if (WIFEXITED(child_exit_status))
  118. {
  119. status = WEXITSTATUS(child_exit_status);
  120. if (status == EXIT_SUCCESS)
  121. {
  122. alarm(0);
  123. }
  124. else
  125. {
  126. if (status != AUTOTEST_SKIPPED_TEST)
  127. fprintf(stdout, "`%s' exited with return code %d\n",
  128. test_name, status);
  129. return status;
  130. }
  131. }
  132. else if (WIFSIGNALED(child_exit_status))
  133. {
  134. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  135. test_name, WTERMSIG(child_exit_status));
  136. return EXIT_FAILURE;
  137. }
  138. else
  139. {
  140. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  141. test_name);
  142. return EXIT_FAILURE;
  143. }
  144. }
  145. return EXIT_SUCCESS;
  146. }