Home | History | Annotate | Download | only in sched_stress
      1 /*
      2  *   Copyright (c) International Business Machines  Corp., 2001
      3  *
      4  *   This program is free software;  you can redistribute it and/or modify
      5  *   it under the terms of the GNU General Public License as published by
      6  *   the Free Software Foundation; either version 2 of the License, or
      7  *   (at your option) any later version.
      8  *
      9  *   This program is distributed in the hope that it will be useful,
     10  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     12  *   the GNU General Public License for more details.
     13  *
     14  *   You should have received a copy of the GNU General Public License
     15  *   along with this program;  if not, write to the Free Software
     16  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     17  */
     18 
     19  /* FUNCTIONS: Scheduler Test Suite */
     20 
     21 /*---------------------------------------------------------------------+
     22 |                               sched_tc0                              |
     23 | ==================================================================== |
     24 |                                                                      |
     25 | Description:  Creates long-term disk I/O bound process               |
     26 |                                                                      |
     27 | Algorithm:    o  Set process priority                                |
     28 |               o  Open a large file and repeatedly read from it       |
     29 |                  for a specified time duration                       |
     30 |                                                                      |
     31 | To compile:   cc -o sched_tc0 sched_tc0.c -L. -lpsc                  |
     32 |                                                                      |
     33 | Usage:        sched_tc0 [-t sec] [-p priority] [-v] [-d]             |
     34 |                                                                      |
     35 | Last update:   Ver. 1.3, 4/10/94 23:04:59                           |
     36 |                                                                      |
     37 | Change Activity                                                      |
     38 |                                                                      |
     39 |   Version  Date    Name  Reason                                      |
     40 |    0.1     050689  CTU   Initial version                             |
     41 |    0.2     010402  Manoj Iyer Ported to Linux			       |
     42 |                                                                      |
     43 +---------------------------------------------------------------------*/
     44 
     45 #include   <stdlib.h>
     46 #include <sys/time.h>
     47 #include <sys/resource.h>
     48 #include   "sched.h"
     49 
     50 /*
     51  * Defines:
     52  *
     53  * USAGE: usage statement
     54  *
     55  * DEFAULT_PRIORITY_TYPE: default priority
     56  *
     57  * DEFAULT_EXECUTION_TIME: default execution time (in seconds)
     58  *
     59  */
     60 #define DEFAULT_PRIORITY_TYPE	"variable"
     61 #define DEFAULT_EXECUTION_TIME	1800
     62 #define USAGE "Usage:  %s  [-p priority] [-t sec] [-v] [-d]      \n" \
     63               "        -t sec      execution time (default 1800 sec)    \n" \
     64               "        -p priority priority (default variable)          \n" \
     65               "        -v          verbose                              \n" \
     66               "        -d          enable debugging messages            \n"
     67 
     68 /*
     69  * Function prototypes:
     70  *
     71  * process_file: reads data file
     72  *
     73  * parse_args: parse command line arguments
     74  */
     75 void process_file(char *);
     76 void parse_args(int, char **);
     77 
     78 /*
     79  * Global variables:
     80  *
     81  * verbose: enable normal messages
     82  *
     83  * debug: enable debugging messages
     84  *
     85  * execution_time: testcase execution time (hours)
     86  *
     87  * priority: process type (fixed priority, variable priority)
     88  */
     89 int verbose = 0;
     90 int debug = 0;
     91 long execution_time = DEFAULT_EXECUTION_TIME;
     92 char *priority = DEFAULT_PRIORITY_TYPE;
     93 
     94 /*---------------------------------------------------------------------+
     95 |                                 main                                 |
     96 | ==================================================================== |
     97 |                                                                      |
     98 | Function:  ...                                                       |
     99 |                                                                      |
    100 +---------------------------------------------------------------------*/
    101 int main(int argc, char **argv)
    102 {
    103 	char *filename = NULL;
    104 	long start_time;	/* time at start of testcase */
    105 	int i;
    106 
    107 	if ((filename = getenv("KERNEL")) == NULL) {
    108 		errno = ENODATA;
    109 		sys_error("environment variable KERNEL not set", __FILE__,
    110 			  __LINE__);
    111 	}
    112 	/*
    113 	 * Process command line arguments...
    114 	 */
    115 
    116 	parse_args(argc, argv);
    117 	if (verbose)
    118 		printf("%s: Scheduler TestSuite program\n\n", *argv);
    119 	if (debug) {
    120 		printf("\tpriority:       %s\n", priority);
    121 		printf("\texecution_time: %ld (sec)\n", execution_time);
    122 	}
    123 
    124 	/*
    125 	 * Adjust the priority of this process if the real time flag is set
    126 	 */
    127 	if (!strcmp(priority, "fixed")) {
    128 #ifndef __linux__
    129 		if (setpri(0, DEFAULT_PRIORITY) < 0)
    130 			sys_error("setpri failed", __FILE__, __LINE__);
    131 #else
    132 		if (setpriority(PRIO_PROCESS, 0, 0) < 0)
    133 			sys_error("setpri failed", __FILE__, __LINE__);
    134 #endif
    135 	}
    136 
    137 	/*
    138 	 * Continuously read through file as time permits...
    139 	 */
    140 	i = 0;
    141 	start_time = time(NULL);
    142 
    143 	if (debug)
    144 		printf("\n");
    145 	while ((time(NULL) - start_time) < execution_time) {
    146 		if (debug) {
    147 			printf("\r\tprocessing file [%d], time left: %ld",
    148 			       i++,
    149 			       execution_time - (time(NULL) - start_time));
    150 			fflush(stdout);
    151 		}
    152 		process_file(filename);
    153 	}
    154 	if (debug)
    155 		printf("\n");
    156 
    157 	/*
    158 	 * Exit with success!
    159 	 */
    160 	if (verbose)
    161 		printf("\nsuccessful!\n");
    162 	return (0);
    163 }
    164 
    165 /*---------------------------------------------------------------------+
    166 |                            process_file ()                           |
    167 | ==================================================================== |
    168 |                                                                      |
    169 | Function:  Opens a file, reads from it until it encounters an        |
    170 |            end-of-file and then closes the file..                    |
    171 |                                                                      |
    172 +---------------------------------------------------------------------*/
    173 void process_file(char *filename)
    174 {
    175 	char record[100];	/* holds each record of the file read */
    176 	FILE *datafile;		/* file pointer to the open file */
    177 
    178 	/*
    179 	 * Try and open the datafile
    180 	 */
    181 	if ((datafile = fopen(filename, "r")) == NULL)
    182 		sys_error("fopen failed", __FILE__, __LINE__);
    183 
    184 	/*
    185 	 * Read the first record of the datafile, then read until end-of-file
    186 	 */
    187 	while (fgets(record, 80, datafile)) {
    188 		if (feof(datafile))
    189 			break;
    190 	}
    191 
    192 	/*
    193 	 * Close the datafile
    194 	 */
    195 	if (fclose(datafile))
    196 		sys_error("fclose failed", __FILE__, __LINE__);
    197 }
    198 
    199 /*---------------------------------------------------------------------+
    200 |                             parse_args ()                            |
    201 | ==================================================================== |
    202 |                                                                      |
    203 | Function:  Parse the command line arguments & initialize global      |
    204 |            variables.                                                |
    205 |                                                                      |
    206 | Updates:   (command line options)                                    |
    207 |                                                                      |
    208 |            [-p] priority: "fixed" or "variable"                      |
    209 |            [-t] n:        execution time in hours                    |
    210 |            [-v]           verbose                                    |
    211 |            [-d]           enable debugging messages                  |
    212 |                                                                      |
    213 +---------------------------------------------------------------------*/
    214 void parse_args(int argc, char **argv)
    215 {
    216 	int opt;
    217 	int pflg = 0, tflg = 0;
    218 	int errflag = 0;
    219 	char *program_name = *argv;
    220 	extern char *optarg;	/* Command line option */
    221 
    222 	if (argc < 2) {
    223 		fprintf(stderr, USAGE, program_name);
    224 		exit(0);
    225 	}
    226 
    227 	/*
    228 	 * Parse command line options.
    229 	 */
    230 	while ((opt = getopt(argc, argv, "p:t:vd")) != EOF) {
    231 		switch (opt) {
    232 		case 'p':	/* process type */
    233 			pflg++;
    234 			priority = optarg;
    235 			break;
    236 		case 't':	/* time (hours) */
    237 			tflg++;
    238 			execution_time = atof(optarg);
    239 			break;
    240 		case 'v':	/* verbose */
    241 			verbose++;
    242 			break;
    243 		case 'd':	/* enable debugging messages */
    244 			verbose++;
    245 			debug++;
    246 			break;
    247 		default:
    248 			errflag++;
    249 			break;
    250 		}
    251 	}
    252 
    253 	/*
    254 	 * Check percentage, execution time and process slots...
    255 	 */
    256 	if (pflg) {
    257 		if (strcmp(priority, "fixed") && strcmp(priority, "variable"))
    258 			errflag++;
    259 	}
    260 	if (tflg) {
    261 		if (execution_time < 0.0 || execution_time > 360000)
    262 			errflag++;
    263 	}
    264 	if (errflag) {
    265 		fprintf(stderr, USAGE, program_name);
    266 		exit(2);
    267 	}
    268 }
    269