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 (at) linux.vnet.ibm.com */ 41 /* */ 42 /* History: */ 43 /* Created- 18/11/2008 -Sudhir Kumar <skumar (at) 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 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 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 int migrate = 0; /* For task migration */ 89 char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX]; 90 char mysharesfile[FILENAME_MAX], ch; 91 /* Following variables are to capture parameters from script */ 92 char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p; 93 char *test_num_p, *task_num_p; 94 pid_t pid; 95 gid_t mygroup_num; /* A number attached with a group */ 96 int fd; /* to open a fifo to synchronize */ 97 int counter = 0; /* To take n number of readings */ 98 double total_cpu_time; /* Accumulated cpu time */ 99 double delta_cpu_time; /* Time the task could run on cpu(s) */ 100 double prev_cpu_time = 0; 101 double exp_cpu_time; /* Exp time in % by shares calculation */ 102 103 struct rusage cpu_usage; 104 time_t current_time, prev_time, delta_time; 105 unsigned int fmyshares, num_tasks; 106 struct sigaction newaction, oldaction; 107 108 mygroup_num = -1; 109 num_cpus = 0; 110 task_num = 0; 111 test_num = 0; 112 113 /* Signal handling for alarm */ 114 sigemptyset(&newaction.sa_mask); 115 newaction.sa_handler = signal_handler_alarm; 116 newaction.sa_flags = 0; 117 sigaction(SIGALRM, &newaction, &oldaction); 118 119 /* Collect the parameters passed by the script */ 120 group_num_p = getenv("GROUP_NUM"); 121 mygroup_p = getenv("MYGROUP"); 122 script_pid_p = getenv("SCRIPT_PID"); 123 num_cpus_p = getenv("NUM_CPUS"); 124 test_num_p = getenv("TEST_NUM"); 125 task_num_p = getenv("TASK_NUM"); 126 /* Check if all of them are valid */ 127 if ((test_num_p != NULL) && (((test_num = atoi(test_num_p)) == 4) || 128 ((test_num = atoi(test_num_p)) == 5))) { 129 if ((group_num_p != NULL) && (mygroup_p != NULL) && 130 (script_pid_p != NULL) && (num_cpus_p != NULL) && 131 (task_num_p != NULL)) { 132 mygroup_num = atoi(group_num_p); 133 scriptpid = atoi(script_pid_p); 134 num_cpus = atoi(num_cpus_p); 135 task_num = atoi(task_num_p); 136 sprintf(mygroup, "%s", mygroup_p); 137 } else { 138 tst_brkm(TBROK, cleanup, 139 "Invalid other input parameters\n"); 140 } 141 } else { 142 tst_brkm(TBROK, cleanup, "Invalid test number passed\n"); 143 } 144 145 sprintf(mytaskfile, "%s", mygroup); 146 sprintf(mysharesfile, "%s", mygroup); 147 strcat(mytaskfile, "/tasks"); 148 strcat(mysharesfile, "/cpu.shares"); 149 pid = getpid(); 150 write_to_file(mytaskfile, "a", pid); /* Assign task to it's group */ 151 152 fd = SAFE_OPEN(cleanup, "./myfifo", 0); 153 154 read(fd, &ch, 1); /* Block task here to synchronize */ 155 156 /* 157 * We now calculate the expected % cpu time of this task by getting 158 * it's group's shares, the total shares of all the groups and the 159 * number of tasks in this group. 160 */ 161 FLAG = 0; 162 total_shares = 0; 163 shares_pointer = &total_shares; 164 len = strlen(path); 165 if (!strncpy(fullpath, path, len)) 166 tst_brkm(TBROK, cleanup, 167 "Could not copy directory path %s ", path); 168 169 if (scan_shares_files(shares_pointer) != 0) 170 tst_brkm(TBROK, cleanup, 171 "From function scan_shares_files in %s ", fullpath); 172 173 /* return val -1 in case of function error, else 2 is min share value */ 174 if ((fmyshares = read_shares_file(mysharesfile)) < 2) 175 tst_brkm(TBROK, cleanup, 176 "in reading shares files %s ", mysharesfile); 177 178 if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0) 179 tst_brkm(TBROK, cleanup, 180 "in reading tasks files %s ", mytaskfile); 181 182 exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks); 183 184 prev_time = time(NULL); /* Note down the time */ 185 186 while (1) { 187 /* 188 * Need to run some cpu intensive task, which also 189 * frequently checks the timer value 190 */ 191 double f = 274.345, mytime; /*just a float number for sqrt */ 192 alarm(TIME_INTERVAL); 193 timer_expired = 0; 194 /* 195 * Let the task run on cpu for TIME_INTERVAL. Time of this 196 * operation should not be high otherwise we can exceed the 197 * TIME_INTERVAL to measure cpu usage 198 */ 199 while (!timer_expired) 200 f = sqrt(f * f); 201 202 current_time = time(NULL); 203 /* Duration in case its not exact TIME_INTERVAL */ 204 delta_time = current_time - prev_time; 205 206 getrusage(0, &cpu_usage); 207 /* total_cpu_time = total user time + total sys time */ 208 total_cpu_time = (cpu_usage.ru_utime.tv_sec + 209 cpu_usage.ru_utime.tv_usec * 1e-6 + 210 cpu_usage.ru_stime.tv_sec + 211 cpu_usage.ru_stime.tv_usec * 1e-6); 212 delta_cpu_time = total_cpu_time - prev_cpu_time; 213 214 prev_cpu_time = total_cpu_time; 215 prev_time = current_time; 216 217 /* calculate % cpu time each task gets */ 218 if (delta_time > TIME_INTERVAL) 219 mytime = (delta_cpu_time * 100) / 220 (delta_time * num_cpus); 221 else 222 mytime = (delta_cpu_time * 100) / 223 (TIME_INTERVAL * num_cpus); 224 225 fprintf(stdout, "Grp:-%3dDEF task-%3d: CPU TIME{calc:-%6.2f(s)" 226 "i.e. %6.2f(%%)exp:-%6.2f(%%)} with %u(shares) in %lu" 227 " (s)\n", mygroup_num, task_num, delta_cpu_time, mytime, 228 exp_cpu_time, fmyshares, delta_time); 229 230 counter++; 231 232 if (counter >= NUM_INTERVALS) { 233 switch (test_num) { 234 case 4: /* Test04 */ 235 exit(0); /* This task is done its job */ 236 break; 237 case 5: /* Test 05 */ 238 if (migrate == 0) { 239 counter = 0; 240 migrate = 1; 241 } else { 242 exit(0); 243 } 244 break; 245 default: 246 tst_brkm(TBROK, cleanup, 247 "Invalid test number passed\n"); 248 break; 249 250 } /* end switch */ 251 } 252 } /* end while */ 253 } /* end main */ 254