• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /*---------------------------------------------------------------------+
23 |                               sched_tc3                              |
24 | ==================================================================== |
25 |                                                                      |
26 | Description:  Creates short-term disk I/O bound process              |
27 |                                                                      |
28 | Algorithm:    o  Set process priority                                |
29 |               o  Continuously multiply matrices together until       |
30 |                  interrupted.                                        |
31 |                                                                      |
32 | To compile:   cc -o sched_tc3 sched_tc3.c -L. -lpsc                  |
33 |                                                                      |
34 | Usage:        sched_tc3 [-t sec] [-p priority] [-v] [-d]             |
35 |                                                                      |
36 | Last update:   Ver. 1.3, 4/10/94 23:05:01                           |
37 |                                                                      |
38 | Change Activity                                                      |
39 |                                                                      |
40 |   Version  Date    Name  Reason                                      |
41 |    0.1     050689  CTU   Initial version                             |
42 |    0.2     010402  Manoj Iyer Ported to Linux			       |
43 |                                                                      |
44 +---------------------------------------------------------------------*/
45 
46 #include <stdlib.h>
47 #include   <signal.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 #include   "sched.h"
51 
52 /*
53  * Defines:
54  *
55  * USAGE: usage statement
56  *
57  * DEFAULT_PRIORITY_TYPE: default priority
58  *
59  * DEFAULT_EXECUTION_TIME: default execution time (in seconds)
60  *
61  * DEFAULT_MATRIX_SIZE: default size of matrix
62  *
63  */
64 #define DEFAULT_PRIORITY_TYPE	"variable"
65 #define DEFAULT_EXECUTION_TIME	1800
66 #define MATRIX_SIZE		100
67 #define USAGE "Usage:  %s  [-p priority] [-v] [-d]               \n" \
68               "        -p priority priority (default variable)          \n" \
69               "        -v          verbose                              \n" \
70               "        -d          enable debugging messages            \n"
71 
72 /*
73  * Function prototypes:
74  *
75  * process_file: reads data file
76  *
77  * parse_args: parse command line arguments
78  */
79 void parse_args(int, char **);
80 void multiply_matrices();
81 void signal_handler();
82 
83 /*
84  * Global variables:
85  *
86  * verbose: enable normal messages
87  *
88  * debug: enable debugging messages
89  *
90  * signaled: set upon receiving SIGUSER1 signal
91  *
92  * priority: process type (fixed priority, variable priority)
93  */
94 int verbose = 0;
95 int debug = 0;
96 int signaled = 0;
97 char *priority = DEFAULT_PRIORITY_TYPE;
98 
99 /*---------------------------------------------------------------------+
100 |                                 main                                 |
101 | ==================================================================== |
102 |                                                                      |
103 | Function:  ...                                                       |
104 |                                                                      |
105 +---------------------------------------------------------------------*/
main(int argc,char ** argv)106 int main(int argc, char **argv)
107 {
108 	long start_time;	/* time at start of testcase */
109 	int i;
110 
111 	/*
112 	 * Setup signal handler & setup alarm so we do not loop forever...
113 	 */
114 	signal(SIGUSR1, signal_handler);
115 	signal(SIGALRM, signal_handler);
116 	alarm(600);		/* wait 10 minutes before aborting */
117 
118 	/*
119 	 * Process command line arguments...
120 	 */
121 	parse_args(argc, argv);
122 	if (verbose)
123 		printf("%s: Scheduler TestSuite program\n\n", *argv);
124 	if (debug) {
125 		printf("\tpriority:       %s\n", priority);
126 	}
127 
128 	/*
129 	 * Adjust the priority of this process if the real time flag is set
130 	 */
131 	if (!strcmp(priority, "fixed")) {
132 #ifndef __linux__
133 		if (setpri(0, DEFAULT_PRIORITY) < 0)
134 			sys_error("setpri failed", __FILE__, __LINE__);
135 #else
136 		if (setpriority(PRIO_PROCESS, 0, 0) < 0)
137 			sys_error("setpri failed", __FILE__, __LINE__);
138 #endif
139 	}
140 
141 	/*
142 	 * Continuously multiply matrix as time permits...
143 	 */
144 	i = 0;
145 	start_time = time(NULL);
146 
147 	/*
148 	 * Continuously read through file until interrupted...
149 	 */
150 	if (debug)
151 		printf("\n");
152 	while (!signaled) {
153 		if (debug) {
154 			printf("\r\tmultiplying matrix [%d]", i++);
155 			fflush(stdout);
156 		}
157 		multiply_matrices();
158 	}
159 	if (debug)
160 		printf("\n");
161 
162 	/*
163 	 * Exit with success!
164 	 */
165 	if (verbose)
166 		printf("\nsuccessful!\n");
167 	return (0);
168 }
169 
170 /*---------------------------------------------------------------------+
171 |                         multiply_matricies ()                        |
172 | ==================================================================== |
173 |                                                                      |
174 | Function:  Randomly assigns two matrices values and then multiplies  |
175 |            them together.                                            |
176 |                                                                      |
177 +---------------------------------------------------------------------*/
multiply_matrices()178 void multiply_matrices()
179 {
180 	int i, j, k;		/* various indeces to access the arrays */
181 	float matrix_1[MATRIX_SIZE][MATRIX_SIZE];
182 	float matrix_2[MATRIX_SIZE][MATRIX_SIZE];
183 	float matrix_3[MATRIX_SIZE][MATRIX_SIZE];
184 
185 	/* first, fill the two matrices to be multiplied with random values */
186 
187 	for (i = 0; i < MATRIX_SIZE; i++) {
188 		for (j = 0; j < MATRIX_SIZE; j++) {
189 			matrix_1[i][j] = (float)(rand() % 100);
190 			matrix_2[i][j] = (float)(rand() % 100);
191 		}
192 	}
193 
194 	/*
195 	 * Now multiply the two matrices
196 	 */
197 	for (i = 0; i < MATRIX_SIZE; i++) {
198 		for (j = 0; j < MATRIX_SIZE; j++) {
199 			matrix_3[i][j] = 0.0;	/* clear the element first */
200 			for (k = 0; k < MATRIX_SIZE; k++)
201 				matrix_3[i][j] +=
202 				    matrix_1[i][k] * matrix_2[k][j];
203 		}
204 	}
205 }
206 
207 /*---------------------------------------------------------------------+
208 |                           signal_handler ()                          |
209 | ==================================================================== |
210 |                                                                      |
211 | Function:  ...                                                       |
212 |                                                                      |
213 +---------------------------------------------------------------------*/
signal_handler(int signal)214 void signal_handler(int signal)
215 {
216 	if (signal == SIGUSR1) {
217 		signaled++;
218 		if (debug)
219 			printf("\n\t<< caught SIGUSR1 interrupt>>\n");
220 	} else if (signal == SIGALRM) {
221 		error("Failed to receive SIGUSR1 signal before timeout!",
222 		      __FILE__, __LINE__);
223 	} else
224 		error("received unexpected signal", __FILE__, __LINE__);
225 }
226 
227 /*---------------------------------------------------------------------+
228 |                             parse_args ()                            |
229 | ==================================================================== |
230 |                                                                      |
231 | Function:  Parse the command line arguments & initialize global      |
232 |            variables.                                                |
233 |                                                                      |
234 | Updates:   (command line options)                                    |
235 |                                                                      |
236 |            [-p] priority: "fixed" or "variable"                      |
237 |            [-t] n:        execution time in hours                    |
238 |            [-v]           verbose                                    |
239 |            [-d]           enable debugging messages                  |
240 |                                                                      |
241 +---------------------------------------------------------------------*/
parse_args(int argc,char ** argv)242 void parse_args(int argc, char **argv)
243 {
244 	int opt;
245 	int pflg = 0;
246 	int errflag = 0;
247 	char *program_name = *argv;
248 	extern char *optarg;	/* Command line option */
249 
250 	/*
251 	 * Parse command line options.
252 	 */
253 	if (argc < 2) {
254 		fprintf(stderr, USAGE, program_name);
255 		exit(0);
256 	}
257 
258 	while ((opt = getopt(argc, argv, "p:vd")) != EOF) {
259 		switch (opt) {
260 		case 'p':	/* process type */
261 			pflg++;
262 			priority = optarg;
263 			break;
264 		case 'v':	/* verbose */
265 			verbose++;
266 			break;
267 		case 'd':	/* enable debugging messages */
268 			verbose++;
269 			debug++;
270 			break;
271 		default:
272 			errflag++;
273 			break;
274 		}
275 	}
276 
277 	/*
278 	 * Check percentage, execution time and process slots...
279 	 */
280 	if (pflg) {
281 		if (strcmp(priority, "fixed") && strcmp(priority, "variable"))
282 			errflag++;
283 	}
284 	if (errflag) {
285 		fprintf(stderr, USAGE, program_name);
286 		exit(2);
287 	}
288 }
289