loader.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. execlp("libtool", "libtool", "--mode=execute", launcher, test_name, test_args, NULL);
  83. else
  84. execlp("libtool", "libtool", "--mode=execute", test_name, test_args, NULL);
  85. fprintf(stderr, "[error] '%s' failed to exec. test marked as failed\n", test_name);
  86. exit(EXIT_FAILURE);
  87. }
  88. if (child_pid == -1)
  89. {
  90. fprintf(stderr, "[error] fork. test marked as failed\n");
  91. exit(EXIT_FAILURE);
  92. }
  93. alarm(timeout);
  94. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  95. {
  96. if (WIFEXITED(child_exit_status))
  97. {
  98. status = WEXITSTATUS(child_exit_status);
  99. if (status == EXIT_SUCCESS)
  100. {
  101. alarm(0);
  102. }
  103. else
  104. {
  105. if (status != AUTOTEST_SKIPPED_TEST)
  106. fprintf(stdout, "`%s' exited with return code %d\n",
  107. test_name, status);
  108. return status;
  109. }
  110. }
  111. else if (WIFSIGNALED(child_exit_status))
  112. {
  113. fprintf(stderr, "[error] `%s' killed with signal %d; test marked as failed\n",
  114. test_name, WTERMSIG(child_exit_status));
  115. return EXIT_FAILURE;
  116. }
  117. else
  118. {
  119. fprintf(stderr, "[error] `%s' did not terminate normally; test marked as failed\n",
  120. test_name);
  121. return EXIT_FAILURE;
  122. }
  123. }
  124. return EXIT_SUCCESS;
  125. }