• 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_task_def04.c                                           */
24 /*                                                                            */
25 /* Description: This is a c program that runs as a default task in a default  */
26 /*              group to create an ideal scenario. In this default group all  */
27 /*              the system tasks will be running along with this spinning task*/
28 /*              The file is to be used by tests 9-10.                         */
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_test 9,10                                      */
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/11/2008 -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/stat.h>
56 #include <fcntl.h>
57 #include <sys/time.h>
58 #include <sys/types.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 = "cpu_controller_test06";
70 int TST_TOTAL = 2;
71 pid_t scriptpid;
72 char path[] = "/dev/cpuctl";
cleanup()73 extern void cleanup()
74 {
75 	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
76 	/* Report exit status */
77 }
78 
79 volatile int timer_expired = 0;
80 
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 
84 	int test_num;
85 	int task_num;
86 	int len;
87 	int num_cpus;
88 	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
89 	char mysharesfile[FILENAME_MAX], ch;
90 	/* Following variables are to capture parameters from script */
91 	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p;
92 	char *test_num_p, *task_num_p;
93 	gid_t mygroup_num;	/* A number attached with a group */
94 	int fd;			/* to open a fifo to synchronize */
95 	int counter = 0;	/* To take n number of readings */
96 	double total_cpu_time;	/* Accumulated cpu time */
97 	double delta_cpu_time;	/* Time the task could run on cpu(s) */
98 	double prev_cpu_time = 0;
99 	double exp_cpu_time;	/* Exp time in % by shares calculation */
100 
101 	struct rusage cpu_usage;
102 	time_t current_time, prev_time, delta_time;
103 	unsigned int fmyshares, num_tasks;
104 	unsigned int mygroup_shares;
105 	struct sigaction newaction, oldaction;
106 
107 	mygroup_num = -1;
108 	num_cpus = 0;
109 	task_num = 0;
110 	test_num = 0;
111 
112 	/* Signal handling for alarm */
113 	sigemptyset(&newaction.sa_mask);
114 	newaction.sa_handler = signal_handler_alarm;
115 	newaction.sa_flags = 0;
116 	sigaction(SIGALRM, &newaction, &oldaction);
117 
118 	/* Collect the parameters passed by the script */
119 	group_num_p = getenv("GROUP_NUM");
120 	mygroup_p = getenv("MYGROUP");
121 	script_pid_p = getenv("SCRIPT_PID");
122 	num_cpus_p = getenv("NUM_CPUS");
123 	test_num_p = getenv("TEST_NUM");
124 	task_num_p = getenv("TASK_NUM");
125 	/* Check if all of them are valid */
126 	if ((test_num_p != NULL) && (((test_num = atoi(test_num_p)) <= 10) &&
127 				     ((test_num = atoi(test_num_p)) >= 9))) {
128 		if ((group_num_p != NULL) && (mygroup_p != NULL) &&
129 		    (script_pid_p != NULL) && (num_cpus_p != NULL) &&
130 		    (task_num_p != NULL)) {
131 			mygroup_num = atoi(group_num_p);
132 			scriptpid = atoi(script_pid_p);
133 			num_cpus = atoi(num_cpus_p);
134 			task_num = atoi(task_num_p);
135 			sprintf(mygroup, "%s", mygroup_p);
136 		} else {
137 			tst_brkm(TBROK, cleanup,
138 				 "Invalid other input parameters\n");
139 		}
140 	} else {
141 		tst_brkm(TBROK, cleanup, "Invalid test number passed\n");
142 	}
143 
144 	sprintf(mytaskfile, "%s", mygroup);
145 	sprintf(mysharesfile, "%s", mygroup);
146 	strcat(mytaskfile, "/tasks");
147 	strcat(mysharesfile, "/cpu.shares");
148 
149 	write_to_file(mytaskfile, "a", getpid());
150 
151 	/*
152 	 * Let us give the default group 100 shares, as other groups will have
153 	 * a multiple of 100 shares.
154 	 */
155 	mygroup_shares = 100;
156 	write_to_file(mysharesfile, "w", mygroup_shares);
157 
158 	fd = SAFE_OPEN(cleanup, "./myfifo", 0);
159 
160 	read(fd, &ch, 1);	/* Block task here to synchronize */
161 
162 	/*
163 	 * We need not calculate the expected % cpu time of this task, as
164 	 * neither it is required nor it can be predicted as there are idle
165 	 * system tasks (and others too) in this group.
166 	 */
167 	FLAG = 0;
168 	total_shares = 0;
169 	shares_pointer = &total_shares;
170 	len = strlen(path);
171 	if (!strncpy(fullpath, path, len))
172 		tst_brkm(TBROK, cleanup,
173 			 "Could not copy directory path %s ", path);
174 
175 	if (scan_shares_files(shares_pointer) != 0)
176 		tst_brkm(TBROK, cleanup,
177 			 "From function scan_shares_files in %s ", fullpath);
178 
179 	/* return val -1 in case of function error, else 2 is min share value */
180 	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
181 		tst_brkm(TBROK, cleanup,
182 			 "in reading shares files  %s ", mysharesfile);
183 
184 	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
185 		tst_brkm(TBROK, cleanup,
186 			 "in reading tasks files  %s ", mytaskfile);
187 
188 	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
189 
190 	prev_time = time(NULL);	/* Note down the time */
191 
192 	while (1) {
193 		/*
194 		 * Need to run some cpu intensive task, which also
195 		 * frequently checks the timer value
196 		 */
197 		double f = 274.345, mytime;	/*just a float number for sqrt */
198 		alarm(TIME_INTERVAL);
199 		timer_expired = 0;
200 		/*
201 		 * Let the task run on cpu for TIME_INTERVAL. Time of this
202 		 * operation should not be high otherwise we can exceed the
203 		 * TIME_INTERVAL to measure cpu usage
204 		 */
205 		while (!timer_expired)
206 			f = sqrt(f * f);
207 
208 		current_time = time(NULL);
209 		/* Duration in case its not exact TIME_INTERVAL */
210 		delta_time = current_time - prev_time;
211 
212 		getrusage(0, &cpu_usage);
213 		/* total_cpu_time = total user time + total sys time */
214 		total_cpu_time = (cpu_usage.ru_utime.tv_sec +
215 				  cpu_usage.ru_utime.tv_usec * 1e-6 +
216 				  cpu_usage.ru_stime.tv_sec +
217 				  cpu_usage.ru_stime.tv_usec * 1e-6);
218 		delta_cpu_time = total_cpu_time - prev_cpu_time;
219 
220 		prev_cpu_time = total_cpu_time;
221 		prev_time = current_time;
222 
223 		/* calculate % cpu time each task gets */
224 		if (delta_time > TIME_INTERVAL)
225 			mytime = (delta_cpu_time * 100) /
226 			    (delta_time * num_cpus);
227 		else
228 			mytime = (delta_cpu_time * 100) /
229 			    (TIME_INTERVAL * num_cpus);
230 
231 		/* No neeed to print the results */
232 		fprintf(stdout, "Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s) i.e."
233 			"  %6.2f(%%) exp:-%6.2f(%%)} with %3u shares in %lu (s)"
234 			" INTERVAL\n", mygroup_num, task_num, delta_cpu_time,
235 			mytime, exp_cpu_time, fmyshares, delta_time);
236 
237 		counter++;
238 
239 		if (counter >= NUM_INTERVALS) {
240 			switch (test_num) {
241 
242 			case 9:	/* Test 09 */
243 			case 10:	/* Test 10 */
244 				exit(0);	/* This task is done */
245 				break;
246 			default:
247 				tst_brkm(TBROK, cleanup,
248 					 "Invalid test number passed\n");
249 				break;
250 
251 			}	/* end switch */
252 		}		/* end if */
253 	}			/* end while */
254 }				/* end main */
255