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 4-5. */
29 /* */
30 /* Total Tests: 2 */
31 /* */
32 /* Test 04: Nice value effect on group scheduling */
33 /* Test 05: Task migration test */
34 /* */
35 /* Test Name: cpu_controller_test02 */
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- 18/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/time.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <time.h>
60
61 #include "../libcontrollers/libcontrollers.h"
62 #include "test.h" /* LTP harness APIs */
63 #include "safe_macros.h"
64
65 #define TIME_INTERVAL 30 /* Time interval in seconds */
66 #define NUM_INTERVALS 3 /* How many iterations of TIME_INTERVAL */
67
68 char *TCID = "cpu_controller_test04";
69 int TST_TOTAL = 1;
70 pid_t scriptpid;
71 char path[] = "/dev/cpuctl";
72
cleanup(void)73 extern void cleanup(void)
74 {
75 kill(scriptpid, SIGUSR1); /* Inform the shell to do cleanup */
76 /* Report exit status */
77 }
78
main(void)79 int main(void)
80 {
81
82 int test_num;
83 int task_num;
84 int len;
85 int num_cpus;
86 int migrate = 0; /* For task migration */
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;
91 char *test_num_p, *task_num_p;
92 pid_t pid;
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 struct sigaction newaction, oldaction;
105
106 mygroup_num = -1;
107 num_cpus = 0;
108 task_num = 0;
109 test_num = 0;
110
111 /* Signal handling for alarm */
112 sigemptyset(&newaction.sa_mask);
113 newaction.sa_handler = signal_handler_alarm;
114 newaction.sa_flags = 0;
115 sigaction(SIGALRM, &newaction, &oldaction);
116
117 /* Collect the parameters passed by the script */
118 group_num_p = getenv("GROUP_NUM");
119 mygroup_p = getenv("MYGROUP");
120 script_pid_p = getenv("SCRIPT_PID");
121 num_cpus_p = getenv("NUM_CPUS");
122 test_num_p = getenv("TEST_NUM");
123 task_num_p = getenv("TASK_NUM");
124 /* Check if all of them are valid */
125 if ((test_num_p != NULL) && (((test_num = atoi(test_num_p)) == 4) ||
126 ((test_num = atoi(test_num_p)) == 5))) {
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 sprintf(mytaskfile, "%s", mygroup);
144 sprintf(mysharesfile, "%s", mygroup);
145 strcat(mytaskfile, "/tasks");
146 strcat(mysharesfile, "/cpu.shares");
147 pid = getpid();
148 write_to_file(mytaskfile, "a", pid); /* Assign task to it's group */
149
150 fd = SAFE_OPEN(cleanup, "./myfifo", 0);
151
152 read(fd, &ch, 1); /* Block task here to synchronize */
153
154 /*
155 * We now calculate the expected % cpu time of this task by getting
156 * it's group's shares, the total shares of all the groups and the
157 * number of tasks in this group.
158 */
159 FLAG = 0;
160 total_shares = 0;
161 shares_pointer = &total_shares;
162 len = strlen(path);
163 if (!strncpy(fullpath, path, len))
164 tst_brkm(TBROK, cleanup,
165 "Could not copy directory path %s ", path);
166
167 if (scan_shares_files(shares_pointer) != 0)
168 tst_brkm(TBROK, cleanup,
169 "From function scan_shares_files in %s ", fullpath);
170
171 /* return val -1 in case of function error, else 2 is min share value */
172 if ((fmyshares = read_shares_file(mysharesfile)) < 2)
173 tst_brkm(TBROK, cleanup,
174 "in reading shares files %s ", mysharesfile);
175
176 if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
177 tst_brkm(TBROK, cleanup,
178 "in reading tasks files %s ", mytaskfile);
179
180 exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
181
182 prev_time = time(NULL); /* Note down the time */
183
184 while (1) {
185 /*
186 * Need to run some cpu intensive task, which also
187 * frequently checks the timer value
188 */
189 double f = 274.345, mytime; /*just a float number for sqrt */
190 alarm(TIME_INTERVAL);
191 timer_expired = 0;
192 /*
193 * Let the task run on cpu for TIME_INTERVAL. Time of this
194 * operation should not be high otherwise we can exceed the
195 * TIME_INTERVAL to measure cpu usage
196 */
197 while (!timer_expired)
198 f = sqrt(f * f);
199
200 current_time = time(NULL);
201 /* Duration in case its not exact TIME_INTERVAL */
202 delta_time = current_time - prev_time;
203
204 getrusage(0, &cpu_usage);
205 /* total_cpu_time = total user time + total sys time */
206 total_cpu_time = (cpu_usage.ru_utime.tv_sec +
207 cpu_usage.ru_utime.tv_usec * 1e-6 +
208 cpu_usage.ru_stime.tv_sec +
209 cpu_usage.ru_stime.tv_usec * 1e-6);
210 delta_cpu_time = total_cpu_time - prev_cpu_time;
211
212 prev_cpu_time = total_cpu_time;
213 prev_time = current_time;
214
215 /* calculate % cpu time each task gets */
216 if (delta_time > TIME_INTERVAL)
217 mytime = (delta_cpu_time * 100) /
218 (delta_time * num_cpus);
219 else
220 mytime = (delta_cpu_time * 100) /
221 (TIME_INTERVAL * num_cpus);
222
223 fprintf(stdout, "Grp:-%3dDEF task-%3d: CPU TIME{calc:-%6.2f(s)"
224 "i.e. %6.2f(%%)exp:-%6.2f(%%)} with %u(shares) in %lu"
225 " (s)\n", mygroup_num, task_num, delta_cpu_time, mytime,
226 exp_cpu_time, fmyshares, delta_time);
227
228 counter++;
229
230 if (counter >= NUM_INTERVALS) {
231 switch (test_num) {
232 case 4: /* Test04 */
233 exit(0); /* This task is done its job */
234 break;
235 case 5: /* Test 05 */
236 if (migrate == 0) {
237 counter = 0;
238 migrate = 1;
239 } else {
240 exit(0);
241 }
242 break;
243 default:
244 tst_brkm(TBROK, cleanup,
245 "Invalid test number passed");
246 break;
247
248 } /* end switch */
249 }
250 } /* end while */
251 } /* end main */
252