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_test03.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 scheduling fairness with respect to number */ 28/* of classes and number of jobs in each class */ 29/* */ 30/* Total Tests: 1 */ 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_test03 */ 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/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 69char *TCID = "cpuctl_test03"; 70int TST_TOTAL = 3; 71pid_t scriptpid; 72char path[] = "/dev/cpuctl"; 73extern void cleanup(void) 74{ 75 kill(scriptpid, SIGUSR1); /* Inform the shell to do cleanup */ 76 tst_exit(); /* Report exit status */ 77} 78 79int main(void) 80{ 81 82 int test_num; 83 int task_num; 84 int len; 85 int num_cpus; /* Total time = TIME_INTERVAL *num_cpus in the machine */ 86 char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX]; 87 char mysharesfile[FILENAME_MAX], ch; 88 /* Following variables are to capture parameters from script */ 89 char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p, 90 *task_num_p; 91 pid_t pid; 92 int mygroup_num, /* A number attached with a group */ 93 fd, /* A descriptor to open a fifo for synchronized start */ 94 counter = 0; /* To take n number of readings */ 95 double total_cpu_time, /* Accumulated cpu time */ 96 delta_cpu_time, /* Time the task could run on cpu(s) (in an interval) */ 97 prev_cpu_time = 0; 98 double exp_cpu_time; /* Expected time in % as obtained by shares calculation */ 99 struct rusage cpu_usage; 100 time_t current_time, prev_time, delta_time; 101 unsigned int fmyshares, num_tasks; /* f-> from file. num_tasks is tasks in this group */ 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) 124 && (((test_num = atoi(test_num_p)) <= 8) 125 && ((test_num = atoi(test_num_p)) >= 6))) { 126 if ((group_num_p != NULL) && (mygroup_p != NULL) 127 && (script_pid_p != NULL) && (num_cpus_p != NULL) 128 && (task_num_p != NULL)) { 129 mygroup_num = atoi(group_num_p); 130 scriptpid = atoi(script_pid_p); 131 num_cpus = atoi(num_cpus_p); 132 task_num = atoi(task_num_p); 133 sprintf(mygroup, "%s", mygroup_p); 134 } else { 135 tst_brkm(TBROK, cleanup, 136 "Invalid other input parameters"); 137 } 138 } else { 139 tst_brkm(TBROK, cleanup, "Invalid test number passed"); 140 } 141 142 sprintf(mytaskfile, "%s", mygroup); 143 sprintf(mysharesfile, "%s", mygroup); 144 strcat(mytaskfile, "/tasks"); 145 strcat(mysharesfile, "/cpu.shares"); 146 pid = getpid(); 147 write_to_file(mytaskfile, "a", pid); /* Assign the task to it's group */ 148 149 fd = SAFE_OPEN(cleanup, "./myfifo", 0); 150 151 read(fd, &ch, 1); /* To block all tasks here and fire them up at the same time */ 152 153 /* 154 * We now calculate the expected % cpu time of this task by getting 155 * it's group's shares, the total shares of all the groups and the 156 * number of tasks in this group. 157 */ 158 FLAG = 0; 159 total_shares = 0; 160 shares_pointer = &total_shares; 161 len = strlen(path); 162 if (!strncpy(fullpath, path, len)) 163 tst_brkm(TBROK, cleanup, "Could not copy directory path %s ", 164 path); 165 166 if (scan_shares_files(shares_pointer) != 0) 167 tst_brkm(TBROK, cleanup, 168 "From function scan_shares_files in %s ", fullpath); 169 170 /* return val: -1 in case of function error, else 2 is min share value */ 171 if ((fmyshares = read_shares_file(mysharesfile)) < 2) 172 tst_brkm(TBROK, cleanup, "in reading shares files %s ", 173 mysharesfile); 174 175 if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0) 176 tst_brkm(TBROK, cleanup, "in reading tasks files %s ", 177 mytaskfile); 178 179 exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks); 180 181 prev_time = time(NULL); /* Note down the time */ 182 183 while (1) { 184 /* Need to run some cpu intensive task, which also frequently checks the timer value */ 185 double f = 274.345, mytime; /*just a float number to take sqrt */ 186 alarm(TIME_INTERVAL); 187 timer_expired = 0; 188 while (!timer_expired) /* Let the task run on cpu for TIME_INTERVAL */ 189 f = sqrt(f * f); /* Time of this operation should not be high otherwise we can 190 * exceed the TIME_INTERVAL to measure cpu usage 191 */ 192 current_time = time(NULL); 193 delta_time = current_time - prev_time; /* Duration in case its not exact TIME_INTERVAL */ 194 195 getrusage(0, &cpu_usage); 196 total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 + /* user time */ 197 cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6); /* system time */ 198 delta_cpu_time = total_cpu_time - prev_cpu_time; 199 200 prev_cpu_time = total_cpu_time; 201 prev_time = current_time; 202 203 /* calculate % cpu time each task gets */ 204 if (delta_time > TIME_INTERVAL) 205 mytime = 206 (delta_cpu_time * 100) / (delta_time * num_cpus); 207 else 208 mytime = 209 (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus); 210 211 fprintf(stdout, "Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(%%) exp:-%6.2f(%%)}\ 212 in %lu (s) INTERVAL\n", mygroup_num, task_num, delta_cpu_time, mytime, 213 exp_cpu_time, delta_time); 214 215 counter++; 216 217 if (counter >= NUM_INTERVALS) { /* Take n sets of readings for each shares value */ 218 switch (test_num) { 219 case 6: /* Test 06 */ 220 case 7: /* Test 07 */ 221 case 8: /* Test 08 */ 222 exit(0); /* This task is done with its job */ 223 break; 224 default: 225 tst_brkm(TBROK, cleanup, 226 "Invalid test number passed"); 227 break; 228 229 } /* end switch */ 230 } /* end if */ 231 } /* end while */ 232} /* end main */ 233