• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) International Business Machines  Corp., 2007                 */
4 /*                                                                            */
5 /* This program is free software;  you can redistribute it and/or modify      */
6 /* it under the terms of the GNU General Public License as published by       */
7 /* the Free Software Foundation; either version 2 of the License, or          */
8 /* (at your option) any later version.                                        */
9 /*                                                                            */
10 /* This program is distributed in the hope that it will be useful,            */
11 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13 /* the GNU General Public License for more details.                           */
14 /*                                                                            */
15 /* You should have received a copy of the GNU General Public License          */
16 /* along with this program;  if not, write to the Free Software               */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
18 /*                                                                            */
19 /******************************************************************************/
20 
21 /******************************************************************************/
22 /*                                                                            */
23 /* File:        cpuctl_test04.c                                               */
24 /*                                                                            */
25 /* Description: This is a c program that tests the cpucontroller fairness of  */
26 /*              scheduling the tasks according to their group shares. This    */
27 /*              testcase tests the ability of the cpu controller to provide   */
28 /*              fairness for share values (absolute).                         */
29 /*                                                                            */
30 /* Total Tests: 2                                                             */
31 /*                                                                            */
32 /* Test 09:     Heavy stress test with nice value change                      */
33 /* Test 10:     Heavy stress test (effect of heavy group on light group)      */
34 /*                                                                            */
35 /* Test Name:   cpu_controller_test04                                         */
36 /*                                                                            */
37 /* Test Assertion                                                             */
38 /*              Please refer to the file cpuctl_testplan.txt                  */
39 /*                                                                            */
40 /* Author:      Sudhir Kumar skumar@linux.vnet.ibm.com                        */
41 /*                                                                            */
42 /* History:                                                                   */
43 /* Created-     20/12/2007 -Sudhir Kumar <skumar@linux.vnet.ibm.com>          */
44 /*                                                                            */
45 /******************************************************************************/
46 
47 #include <unistd.h>
48 #include <math.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sys/resource.h>
54 #include <sys/syscall.h>
55 #include <sys/time.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <time.h>
60 #include <unistd.h>
61 
62 #include "../libcontrollers/libcontrollers.h"
63 #include "test.h"		/* LTP harness APIs */
64 #include "safe_macros.h"
65 
66 #define TIME_INTERVAL	100	/* Time interval in seconds */
67 #define NUM_INTERVALS	2	/* How many iterations of TIME_INTERVAL */
68 
69 char *TCID = "cpuctl_test04";
70 int TST_TOTAL = 2;
71 pid_t scriptpid;
72 char path[] = "/dev/cpuctl";
73 
cleanup(void)74 extern void cleanup(void)
75 {
76 	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
77 	tst_exit();		/* Report exit status */
78 }
79 
main(void)80 int main(void)
81 {
82 
83 	int test_num;
84 	int task_num;
85 	int len;
86 	int num_cpus;		/* Total time = TIME_INTERVAL *num_cpus in the machine */
87 	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
88 	char mysharesfile[FILENAME_MAX], ch;
89 	/* Following variables are to capture parameters from script */
90 	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p,
91 	    *task_num_p;
92 	gid_t mygroup_num;	/* A number attached with a group */
93 	int fd;			/* A descriptor to open a fifo for synchronized start */
94 	int counter = 0;	/* To take n number of readings */
95 	double total_cpu_time,	/* Accumulated cpu time */
96 	 delta_cpu_time,	/* Time the task could run on cpu(s) (in an interval) */
97 	 prev_cpu_time = 0;
98 	double exp_cpu_time;	/* Expected time in % as obtained by shares calculation */
99 	struct rusage cpu_usage;
100 	unsigned long int mygroup_shares;
101 	time_t current_time, prev_time, delta_time;
102 	unsigned int fmyshares, num_tasks;	/* f-> from file. num_tasks is tasks in this group */
103 	struct sigaction newaction, oldaction;
104 
105 	mygroup_num = -1;
106 	num_cpus = 0;
107 	task_num = 0;
108 	test_num = 0;
109 
110 	/* Signal handling for alarm */
111 	sigemptyset(&newaction.sa_mask);
112 	newaction.sa_handler = signal_handler_alarm;
113 	newaction.sa_flags = 0;
114 	sigaction(SIGALRM, &newaction, &oldaction);
115 
116 	/* Collect the parameters passed by the script */
117 	group_num_p = getenv("GROUP_NUM");
118 	mygroup_p = getenv("MYGROUP");
119 	script_pid_p = getenv("SCRIPT_PID");
120 	num_cpus_p = getenv("NUM_CPUS");
121 	test_num_p = getenv("TEST_NUM");
122 	task_num_p = getenv("TASK_NUM");
123 	/* Check if all of them are valid */
124 	if ((test_num_p != NULL)
125 	    && (((test_num = atoi(test_num_p)) <= 10)
126 		&& ((test_num = atoi(test_num_p)) >= 9))) {
127 		if ((group_num_p != NULL) && (mygroup_p != NULL)
128 		    && (script_pid_p != NULL) && (num_cpus_p != NULL)
129 		    && (task_num_p != NULL)) {
130 			mygroup_num = atoi(group_num_p);
131 			scriptpid = atoi(script_pid_p);
132 			num_cpus = atoi(num_cpus_p);
133 			task_num = atoi(task_num_p);
134 			sprintf(mygroup, "%s", mygroup_p);
135 		} else {
136 			tst_brkm(TBROK, cleanup,
137 				 "Invalid other input parameters");
138 		}
139 	} else {
140 		tst_brkm(TBROK, cleanup, "Invalid test number passed");
141 	}
142 
143 	/*
144 	 * Let us give the default group 100 shares, as other groups will have
145 	 * a multiple of 100 shares.
146 	 * WARN: For large num of groups this may hit MAX_SHARES
147 	 */
148 	mygroup_shares = mygroup_num * 100;
149 
150 	sprintf(mytaskfile, "%s", mygroup);
151 	strcat(mytaskfile, "/tasks");
152 	sprintf(mysharesfile, "%s", mygroup);
153 	strcat(mysharesfile, "/cpu.shares");
154 	/* Need some technique so as only 1 task per grp writes to shares file */
155 	if (test_num == 9)
156 		write_to_file(mysharesfile, "w", mygroup_shares);	/* Write the shares of the group */
157 
158 	write_to_file(mytaskfile, "a", getpid());	/* Assign the task to it's group */
159 
160 	fd = SAFE_OPEN(cleanup, "./myfifo", 0);
161 
162 	read(fd, &ch, 1);	/* To block all tasks here and fire them up at the same time */
163 
164 	/*
165 	 * We now calculate the expected % cpu time of this task by getting
166 	 * it's group's shares, the total shares of all the groups and the
167 	 * number of tasks in this group.
168 	 */
169 	FLAG = 0;
170 	total_shares = 0;
171 	shares_pointer = &total_shares;
172 	len = strlen(path);
173 	if (!strncpy(fullpath, path, len))
174 		tst_brkm(TBROK, cleanup, "Could not copy directory path %s ",
175 			 path);
176 
177 	if (scan_shares_files(shares_pointer) != 0)
178 		tst_brkm(TBROK, cleanup,
179 			 "From function scan_shares_files in %s ", fullpath);
180 
181 	/* return val: -1 in case of function error, else 2 is min share value */
182 	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
183 		tst_brkm(TBROK, cleanup, "in reading shares files  %s ",
184 			 mysharesfile);
185 
186 	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
187 		tst_brkm(TBROK, cleanup, "in reading tasks files  %s ",
188 			 mytaskfile);
189 
190 	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
191 
192 	prev_time = time(NULL);	/* Note down the time */
193 
194 	while (1) {
195 		/* Need to run some cpu intensive task, which also frequently checks the timer value */
196 		double f = 274.345, mytime;	/*just a float number to take sqrt */
197 		alarm(TIME_INTERVAL);
198 		timer_expired = 0;
199 		while (!timer_expired)	/* Let the task run on cpu for TIME_INTERVAL */
200 			f = sqrt(f * f);	/* Time of this operation should not be high otherwise we can
201 						 * exceed the TIME_INTERVAL to measure cpu usage
202 						 */
203 		current_time = time(NULL);
204 		delta_time = current_time - prev_time;	/* Duration in case its not exact TIME_INTERVAL */
205 
206 		getrusage(0, &cpu_usage);
207 		total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 +	/* user time */
208 				  cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6);	/* system time */
209 		delta_cpu_time = total_cpu_time - prev_cpu_time;
210 
211 		prev_cpu_time = total_cpu_time;
212 		prev_time = current_time;
213 
214 		/* calculate % cpu time each task gets */
215 		if (delta_time > TIME_INTERVAL)
216 			mytime =
217 			    (delta_cpu_time * 100) / (delta_time * num_cpus);
218 		else
219 			mytime =
220 			    (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus);
221 
222 		fprintf(stdout, "Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(%%) exp:-%6.2f(%%)}\
223 with %3u shares in %lu (s) INTERVAL\n", mygroup_num, task_num, delta_cpu_time,
224 			mytime, exp_cpu_time, fmyshares, delta_time);
225 
226 		counter++;
227 
228 		if (counter >= NUM_INTERVALS) {	/* Take n sets of readings for each shares value */
229 			switch (test_num) {
230 			case 9:	/* Test 09 */
231 				exit(0);	/* This task is done with its job */
232 				break;
233 			case 10:	/* Test 10 */
234 				exit(0);	/* This task is done with its job */
235 				break;
236 			default:
237 				tst_brkm(TBROK, cleanup,
238 					 "Invalid test number passed");
239 				break;
240 
241 			}	/* end switch */
242 		}		/* end if */
243 	}			/* end while */
244 }				/* end main */
245