cpuctl_test03.c revision b6ff02cd9592e40c4b435ff7755cb69572a23f47
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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/* Standard Include Files */ 49#include <unistd.h> 50#include <math.h> 51#include <signal.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <sys/resource.h> 56#include <sys/syscall.h> 57#include <sys/stat.h> 58#include <fcntl.h> 59#include <sys/time.h> 60#include <sys/types.h> 61#include <time.h> 62#include <unistd.h> 63 64#include "../libcontrollers/libcontrollers.h" 65#include "test.h" /* LTP harness APIs*/ 66 67#define TIME_INTERVAL 30 /* Time interval in seconds*/ 68#define NUM_INTERVALS 2 /* How many iterations of TIME_INTERVAL */ 69 70char *TCID = "cpu_controller_test06"; 71int TST_TOTAL = 3; 72pid_t scriptpid; 73char path[] = "/dev/cpuctl"; 74extern void 75cleanup() 76{ 77 kill (scriptpid, SIGUSR1);/* Inform the shell to do cleanup*/ 78 tst_exit (); /* Report exit status*/ 79} 80 81volatile int timer_expired = 0; 82 83int main(int argc, char* argv[]) 84{ 85 86 int test_num; 87 int task_num; 88 int len; 89 int num_cpus; /* Total time = TIME_INTERVAL *num_cpus in the machine */ 90 char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX]; 91 char mysharesfile[FILENAME_MAX], ch; 92 /* Following variables are to capture parameters from script*/ 93 char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p, *task_num_p; 94 pid_t pid; 95 int mygroup_num, /* A number attached with a group*/ 96 fd, /* A descriptor to open a fifo for synchronized start*/ 97 counter =0; /* To take n number of readings*/ 98 double total_cpu_time, /* Accumulated cpu time*/ 99 delta_cpu_time, /* Time the task could run on cpu(s) (in an interval)*/ 100 prev_cpu_time=0; 101 double exp_cpu_time; /* Expected time in % as obtained by shares calculation */ 102 struct rusage cpu_usage; 103 time_t current_time, prev_time, delta_time; 104 unsigned int fmyshares, num_tasks;/* f-> from file. num_tasks is tasks in this group*/ 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)) <= 8) && ((test_num =atoi(test_num_p)) >= 6))) 127 { 128 if ((group_num_p != NULL) && (mygroup_p != NULL) && \ 129 (script_pid_p != NULL) && (num_cpus_p != NULL) && (task_num_p != NULL)) 130 { 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 } 137 else 138 { 139 tst_brkm (TBROK, cleanup, "Invalid other input parameters\n"); 140 } 141 } 142 else 143 { 144 tst_brkm (TBROK, cleanup, "Invalid test number passed\n"); 145 } 146 147 sprintf(mytaskfile, "%s", mygroup); 148 sprintf(mysharesfile, "%s", mygroup); 149 strcat (mytaskfile,"/tasks"); 150 strcat (mysharesfile,"/cpu.shares"); 151 pid = getpid(); 152 write_to_file (mytaskfile, "a", pid); /* Assign the task to it's group*/ 153 154 fd = open ("./myfifo", 0); 155 if (fd == -1) 156 { 157 tst_brkm (TBROK, cleanup, "Could not open fifo for synchronization"); 158 } 159 160 read (fd, &ch, 1); /* To block all tasks here and fire them up at the same time*/ 161 162 /* 163 * We now calculate the expected % cpu time of this task by getting 164 * it's group's shares, the total shares of all the groups and the 165 * number of tasks 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, "Could not copy directory path %s ", path); 173 174 if (scan_shares_files(shares_pointer) != 0) 175 tst_brkm (TBROK, cleanup, "From function scan_shares_files in %s ", fullpath); 176 177 /* return val: -1 in case of function error, else 2 is min share value */ 178 if ((fmyshares = read_shares_file(mysharesfile)) < 2) 179 tst_brkm (TBROK, cleanup, "in reading shares files %s ", mysharesfile); 180 181 if ((read_file (mytaskfile, GET_TASKS, &num_tasks)) < 0) 182 tst_brkm (TBROK, cleanup, "in reading tasks files %s ", mytaskfile); 183 184 exp_cpu_time = (double)(fmyshares * 100) /(total_shares * num_tasks); 185 186 prev_time = time (NULL); /* Note down the time*/ 187 188 while (1) 189 { 190 /* Need to run some cpu intensive task, which also frequently checks the timer value*/ 191 double f = 274.345, mytime; /*just a float number to take sqrt*/ 192 alarm (TIME_INTERVAL); 193 timer_expired = 0; 194 while (!timer_expired) /* Let the task run on cpu for TIME_INTERVAL*/ 195 f = sqrt (f*f); /* Time of this operation should not be high otherwise we can 196 * exceed the TIME_INTERVAL to measure cpu usage 197 */ 198 current_time = time (NULL); 199 delta_time = current_time - prev_time; /* Duration in case its not exact TIME_INTERVAL*/ 200 201 getrusage (0, &cpu_usage); 202 total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 + /* user time*/ 203 cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6) ; /* system time*/ 204 delta_cpu_time = total_cpu_time - prev_cpu_time; 205 206 prev_cpu_time = total_cpu_time; 207 prev_time = current_time; 208 209 /* calculate % cpu time each task gets */ 210 if (delta_time > TIME_INTERVAL) 211 mytime = (delta_cpu_time * 100) / (delta_time * num_cpus); 212 else 213 mytime = (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus); 214 215 fprintf (stdout,"Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(%%) exp:-%6.2f(%%)}\ 216 in %lu (s) INTERVAL\n",mygroup_num, task_num, delta_cpu_time, mytime,\ 217exp_cpu_time, delta_time); 218 219 counter++; 220 221 if (counter >= NUM_INTERVALS) /* Take n sets of readings for each shares value*/ 222 { 223 switch (test_num) 224 { 225 case 6: /* Test 06 */ 226 case 7: /* Test 07 */ 227 case 8: /* Test 08 */ 228 exit (0); /* This task is done with its job*/ 229 break; 230 default: 231 tst_brkm (TBROK, cleanup, "Invalid test number passed\n"); 232 break; 233 234 } /* end switch*/ 235 } /* end if*/ 236 } /* end while*/ 237} /* end main*/ 238