• 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_test01.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: 3                                                             */
31 /*                                                                            */
32 /* Test 01:     Tests if fairness persists among different runs               */
33 /* Test 02:     Tests fairness with respect to absolute share values          */
34 /* Test 03:     Granularity test with respect to shares values                */
35 /*                                                                            */
36 /* Test Name:   cpu_controller_test01                                         */
37 /*                                                                            */
38 /* Test Assertion                                                             */
39 /*              Please refer to the file cpuctl_testplan.txt                  */
40 /*                                                                            */
41 /* Author:      Sudhir Kumar skumar@linux.vnet.ibm.com                        */
42 /*                                                                            */
43 /* History:                                                                   */
44 /* Created-     20/12/2007 -Sudhir Kumar <skumar@linux.vnet.ibm.com>          */
45 /*                                                                            */
46 /******************************************************************************/
47 
48 #include <unistd.h>
49 #include <math.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/resource.h>
55 #include <sys/syscall.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 #include <time.h>
61 #include <unistd.h>
62 
63 #include "../libcontrollers/libcontrollers.h"
64 #include "test.h"		/* LTP harness APIs */
65 
66 #define TIME_INTERVAL	30	/* Time interval in seconds */
67 #define NUM_INTERVALS	3	/* How many iterations of TIME_INTERVAL */
68 #define NUM_SETS	4	/* How many share values (with same ratio) */
69 #define MULTIPLIER   	10	/* decides the rate at which share value gets multiplied */
70 #define GRANULARITY    5	/* % value by which shares of a group changes */
71 char *TCID = "cpuctl_test01";
72 int TST_TOTAL = 1;
73 pid_t scriptpid;
74 char path[] = "/dev/cpuctl";
cleanup()75 extern void cleanup()
76 {
77 	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
78 	tst_exit();		/* Report exit status */
79 }
80 
81 volatile int timer_expired = 0;
82 
main(int argc,char * argv[])83 int main(int argc, char *argv[])
84 {
85 
86 	int num_cpus;
87 	int test_num;
88 	int len;		/* Total time = TIME_INTERVAL *num_cpus in the machine */
89 	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
90 	char mysharesfile[FILENAME_MAX], ch;
91 	pid_t pid;
92 	gid_t my_group_num;	/* A number attached with a group */
93 	int fd;			/* A descriptor to open a fifo for synchronized start */
94 	int first_counter = 0;	/* To take n number of readings */
95 	int second_counter = 0;	/* To track number of times the base value of shares has been changed */
96 	double total_cpu_time,	/* Accumulated cpu time */
97 	 delta_cpu_time,	/* Time the task could run on cpu(s) (in an interval) */
98 	 prev_cpu_time = 0;
99 	double exp_cpu_time;	/* Expected time in % as obtained by shares calculation */
100 	struct rusage cpu_usage;
101 	time_t current_time, prev_time, delta_time;
102 	unsigned long int myshares = 2, baseshares = 1000;	/* Simply the base value to start with */
103 	unsigned int fmyshares, num_tasks;	/* f-> from file. num_tasks is tasks in this group */
104 	struct sigaction newaction, oldaction;
105 
106 	my_group_num = -1;
107 	num_cpus = 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 	/* Check if all parameters passed are correct */
117 	if ((argc < 5) || ((my_group_num = atoi(argv[1])) <= 0)
118 	    || ((scriptpid = atoi(argv[3])) <= 0)
119 	    || ((num_cpus = atoi(argv[4])) <= 0)
120 	    || (test_num = atoi(argv[5])) <= 0) {
121 		tst_brkm(TBROK, cleanup, "Invalid input parameters\n");
122 	}
123 
124 	if (test_num == 1)	/* Test 01 & Test 02 */
125 		myshares *= my_group_num;
126 	else if (test_num == 3)	/* Test 03 */
127 		myshares = baseshares;
128 	else {
129 		tst_brkm(TBROK, cleanup,
130 			 "Wrong Test number passed. Exiting Test...\n");
131 	}
132 
133 	sprintf(mygroup, "%s", argv[2]);
134 	sprintf(mytaskfile, "%s", mygroup);
135 	sprintf(mysharesfile, "%s", mygroup);
136 	strcat(mytaskfile, "/tasks");
137 	strcat(mysharesfile, "/cpu.shares");
138 	pid = getpid();
139 	write_to_file(mytaskfile, "a", pid);	/* Assign the task to it's group */
140 	write_to_file(mysharesfile, "w", myshares);
141 
142 	fd = open("./myfifo", 0);
143 	if (fd == -1) {
144 		tst_brkm(TBROK, cleanup,
145 			 "Could not open fifo for synchronization");
146 	}
147 
148 	fprintf(stdout, "\ntask-%d SHARES=%lu\n", my_group_num, myshares);
149 	read(fd, &ch, 1);	/* To block all tasks here and fire them up at the same time */
150 
151 	/*
152 	 * We now calculate the expected % cpu time of this task by getting
153 	 * it's group's shares, the total shares of all the groups and the
154 	 * number of tasks in this group.
155 	 */
156 	FLAG = 0;
157 	total_shares = 0;
158 	shares_pointer = &total_shares;
159 	len = strlen(path);
160 	if (!strncpy(fullpath, path, len))
161 		tst_brkm(TBROK, cleanup, "Could not copy directory path %s ",
162 			 path);
163 
164 	if (scan_shares_files(shares_pointer) != 0)
165 		tst_brkm(TBROK, cleanup,
166 			 "From function scan_shares_files in %s ", fullpath);
167 
168 	/* return val: -1 in case of function error, else 2 is min share value */
169 	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
170 		tst_brkm(TBROK, cleanup, "in reading shares files  %s ",
171 			 mysharesfile);
172 
173 	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
174 		tst_brkm(TBROK, cleanup, "in reading tasks files  %s ",
175 			 mytaskfile);
176 
177 	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
178 
179 	prev_time = time(NULL);	/* Note down the time */
180 
181 	while (1) {
182 		/* Need to run some cpu intensive task, which also frequently checks the timer value */
183 		double f = 274.345, mytime;	/*just a float number to take sqrt */
184 		alarm(TIME_INTERVAL);
185 		timer_expired = 0;
186 		while (!timer_expired)	/* Let the task run on cpu for TIME_INTERVAL */
187 			f = sqrt(f * f);	/* Time of this operation should not be high otherwise we can
188 						 * exceed the TIME_INTERVAL to measure cpu usage
189 						 */
190 		current_time = time(NULL);
191 		delta_time = current_time - prev_time;	/* Duration in case its not exact TIME_INTERVAL */
192 
193 		getrusage(0, &cpu_usage);
194 		total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 +	/*user */
195 				  cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6);	/*sys */
196 		delta_cpu_time = total_cpu_time - prev_cpu_time;
197 
198 		prev_cpu_time = total_cpu_time;
199 		prev_time = current_time;
200 
201 		/* calculate % cpu time each task gets */
202 		if (delta_time > TIME_INTERVAL)
203 			mytime =
204 			    (delta_cpu_time * 100) / (delta_time * num_cpus);
205 		else
206 			mytime =
207 			    (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus);
208 
209 		fprintf(stdout, "task-%d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(%%) exp:-%6.2f(%%)}\
210 with %lu(shares) in %lu (s) INTERVAL\n", my_group_num, delta_cpu_time, mytime,
211 			exp_cpu_time, myshares, delta_time);
212 		first_counter++;
213 
214 		if (first_counter >= NUM_INTERVALS) {	/* Take n sets of readings for each shares value */
215 			first_counter = 0;
216 			second_counter++;
217 			if (second_counter >= NUM_SETS)
218 				exit(0);	/* This task is done with its job */
219 
220 			/* Change share values depending on the test_num */
221 			if (test_num == 1) {
222 				/* Keep same ratio but change values */
223 				myshares = MULTIPLIER * myshares;
224 			} else {
225 				/* Increase for odd task and decrease for even task */
226 				if (my_group_num % 2)
227 					myshares +=
228 					    baseshares * GRANULARITY / 100;
229 				else
230 					myshares -=
231 					    baseshares * GRANULARITY / 100;
232 			}
233 			write_to_file(mysharesfile, "w", myshares);
234 			if (test_num == 3) {
235 				/*
236 				 * Read the shares file and again calculate the cpu fraction
237 				 * No need to read tasks file as we do not migrate tasks
238 				 * No need to scan all shares file as total shares are const
239 				 */
240 				if ((fmyshares =
241 				     read_shares_file(mysharesfile)) < 2)
242 					tst_brkm(TBROK, cleanup,
243 						 "in reading shares files  %s ",
244 						 mysharesfile);
245 				exp_cpu_time =
246 				    (double)(fmyshares * 100) / (total_shares *
247 								 num_tasks);
248 			}
249 
250 			fprintf(stdout, "\ntask-%d SHARES=%lu\n", my_group_num,
251 				myshares);
252 		}		/* end if */
253 	}			/* end while */
254 }				/* end main */
255