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