1/* 2 * Copyright (c) 2004, Bull S.A.. All rights reserved. 3 * Created by: Sebastien Decugis 4 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2 of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 * You should have received a copy of the GNU General Public License along 14 * with this program; if not, write the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 17 */ 18 19/* 20 * The purpose of this file is to provide a monitor process 21 * for the stress/threads/ * / *.c testcases in the OPTS. 22 * 23 * The goal is: 24 * -> if the testcase returns, the monitor returns the error code. 25 * -> after a specified timeout, the monitor let the stress test terminate 26 27 * This allows for the stress tests to be run in an automatic maneer 28 * with a script such as: 29#!/bin/sh 30 31#monitor the system 32vmstat -n 120 180 >monitor.txt 2>&1 & 33 34#run the tests 35for TS in `ls -1 *.c`; 36do <compile $TS>; 37if [ $? -eq 0 ]; 38then <run in background:> 39 helper 6 $TS.exe & 40fi 41done 42 43#wait for the end 44while [ "`ps -e | grep helper`" ]; 45do sleep 30; 46done 47 48*/ 49 50#include <pthread.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <unistd.h> 54#include <signal.h> 55#include <sys/wait.h> 56#include <assert.h> 57#include <time.h> 58 59static pid_t child; 60static int timeout; 61 62/* Note that there could be a race between 63the moment the stress test terminates and 64when the timeout expires. As this is highly 65improbable, we don't care... */ 66 67static void *timer(void *arg) 68{ 69 int ret = 0; 70 71 unsigned remaining = timeout * 3600; 72 do { 73 remaining = sleep(remaining); 74 } while (remaining); 75 ret = kill(child, SIGUSR1); 76 if (ret != 0) { 77 perror("Failed to kill the stress test"); 78 exit(2); 79 } 80 81 return NULL; 82} 83 84int main(int argc, char *argv[]) 85{ 86 int ret; 87 pthread_t th; 88 pid_t chk; 89 int status; 90 char *ts = "[??:??:??]"; 91 struct tm *now; 92 time_t nw; 93 94 /* check args */ 95 if (argc < 3) { 96 printf("\nUsage: \n"); 97 printf(" $ %s n exe arglist\n", argv[0]); 98 printf("\nWhere:\n"); 99 printf(" n is the timeout duration in hours,\n"); 100 printf(" exe is the stress test executable to monitor,\n"); 101 printf 102 (" arglist is the arguments to be passed to executable.\n\n"); 103 return 2; 104 } 105 106 timeout = atoi(argv[1]); 107 if (timeout < 1) { 108 fprintf(stderr, 109 "Invalid timeout value \"%s\". Timeout must be a positive integer.\n", 110 argv[1]); 111 return 2; 112 } 113 114 /* create the timer thread */ 115 ret = pthread_create(&th, NULL, timer, NULL); 116 if (ret != 0) { 117 perror("Failed to create the timeout thread\n"); 118 return 2; 119 } 120 121 /* Create the new process for the stress test */ 122 child = fork(); 123 124 if (child == (pid_t) - 1) { 125 perror("Failed to create a new process"); 126 exit(2); 127 } 128 129 /* The child process executes the test */ 130 if (child == (pid_t) 0) { 131 132 /* Execute the command */ 133 ret = execvp(argv[2], &argv[2]); 134 if (ret == -1) { 135 /* Application was not launched */ 136 perror("Unable to run child application"); 137 return 2; 138 } 139 assert(0); 140 perror("Should not see me"); 141 return 2; 142 } 143 144 /* The parent: */ 145 146 /* wait for the child process to terminate */ 147 chk = waitpid(child, &status, 0); 148 if (chk != child) { 149 perror("Got the wrong process image status"); 150 return 2; 151 } 152 153 /* Cancel the timer thread in case the process returned by itself */ 154 (void)pthread_cancel(th); 155 156 ret = pthread_join(th, NULL); 157 if (ret != 0) { 158 perror("Unable to join the timer thread"); 159 return 2; 160 } 161 162 /* return */ 163 nw = time(NULL); 164 now = localtime(&nw); 165 if (now == NULL) 166 printf(ts); 167 else 168 printf("[%2.2d:%2.2d:%2.2d]\n", now->tm_hour, now->tm_min, 169 now->tm_sec); 170 if (!WIFEXITED(status)) { 171 printf("The stress sample did not exit\n"); 172 if (WIFSIGNALED(status)) { 173 printf("It was killed with signal %i\n", 174 WTERMSIG(status)); 175 } else { 176 printf("and it was not killed...\n"); 177 } 178 exit(1); 179 } 180 if (WEXITSTATUS(status) == 0) { 181 printf("Test %s PASSED\n", argv[2]); 182 } else { 183 printf("Test %s: returned %d\n", argv[2], WEXITSTATUS(status)); 184 } 185 exit(WEXITSTATUS(status)); 186} 187