cpuctl_test04.c revision d3ba25b3d046e18320734e03dbcfe42b089115f8
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_test04.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 ability of the cpu controller to provide */ 28/* fairness for share values (absolute). */ 29/* */ 30/* Total Tests: 2 */ 31/* */ 32/* Test 09: Heavy stress test with nice value change */ 33/* Test 10: Heavy stress test (effect of heavy group on light group) */ 34/* */ 35/* Test Name: cpu_controller_test04 */ 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- 20/12/2007 -Sudhir Kumar <skumar@linux.vnet.ibm.com> */ 44/* */ 45/******************************************************************************/ 46 47/* Standard Include Files */ 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/time.h> 57#include <sys/types.h> 58#include <time.h> 59#include <unistd.h> 60 61#include "../libcontrollers/libcontrollers.h" 62#include "test.h" /* LTP harness APIs*/ 63 64#define TIME_INTERVAL 200 /* Time interval in seconds*/ 65#define NUM_INTERVALS 2 /* How many iterations of TIME_INTERVAL */ 66 67extern int Tst_count; 68char *TCID = "cpu_controller_test06"; 69int TST_TOTAL = 2; 70pid_t scriptpid; 71char path[] = "/dev/cpuctl"; 72 73extern void 74cleanup() 75{ 76 kill (scriptpid, SIGUSR1);/* Inform the shell to do cleanup*/ 77 tst_exit (); /* Report exit status*/ 78} 79 80int timer_expired = 0; 81 82int main(int argc, char* argv[]) 83{ 84 85 int test_num, task_num, len, num_cpus; /* Total time = TIME_INTERVAL *num_cpus in the machine */ 86 char mygroup[64], mytaskfile[64], mysharesfile[64], ch; 87 /* Following variables are to capture parameters from script*/ 88 char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p, *task_num_p; 89 int mygroup_num, /* A number attached with a group*/ 90 fd, /* A descriptor to open a fifo for synchronized start*/ 91 counter =0; /* To take n number of readings*/ 92 double total_cpu_time, /* Accumulated cpu time*/ 93 delta_cpu_time, /* Time the task could run on cpu(s) (in an interval)*/ 94 prev_cpu_time=0; 95 double exp_cpu_time; /* Expected time in % as obtained by shares calculation */ 96 struct rusage cpu_usage; 97 unsigned long int mygroup_shares; 98 time_t current_time, prev_time, delta_time; 99 unsigned int fmyshares, num_tasks;/* f-> from file. num_tasks is tasks in this group*/ 100 struct sigaction newaction, oldaction; 101 /* Signal handling for alarm*/ 102 sigemptyset (&newaction.sa_mask); 103 newaction.sa_handler = signal_handler_alarm; 104 newaction.sa_flags=0; 105 sigaction (SIGALRM, &newaction, &oldaction); 106 107 /* Collect the parameters passed by the script */ 108 group_num_p = getenv("GROUP_NUM"); 109 mygroup_p = getenv("MYGROUP"); 110 script_pid_p = getenv("SCRIPT_PID"); 111 num_cpus_p = getenv("NUM_CPUS"); 112 test_num_p = getenv("TEST_NUM"); 113 task_num_p = getenv("TASK_NUM"); 114 /* Check if all of them are valid */ 115 if ((test_num_p != NULL) && (((test_num =atoi(test_num_p)) <= 10) && ((test_num =atoi(test_num_p)) >= 9))) 116 { 117 if ((group_num_p != NULL) && (mygroup_p != NULL) && \ 118 (script_pid_p != NULL) && (num_cpus_p != NULL) && (task_num_p != NULL)) 119 { 120 mygroup_num = atoi(group_num_p); 121 scriptpid = atoi(script_pid_p); 122 num_cpus = atoi (num_cpus_p); 123 task_num = atoi (task_num_p); 124 sprintf(mygroup,"%s", mygroup_p); 125 } 126 else 127 { 128 tst_brkm (TBROK, cleanup, "Invalid other input parameters\n"); 129 } 130 } 131 else 132 { 133 tst_brkm (TBROK, cleanup, "Invalid test number passed\n"); 134 } 135 136 mygroup_shares = mygroup_num + 1; /* Min shares value currently is 2 */ 137 sprintf(mytaskfile, "%s", mygroup); 138 strcat (mytaskfile,"/tasks"); 139 sprintf(mysharesfile, "%s", mygroup); 140 strcat (mysharesfile,"/cpu.shares"); 141 /* Need some technique so as only 1 task per grp writes to shares file */ 142 if (test_num == 9) 143 write_to_file (mysharesfile, "w", mygroup_shares); /* Write the shares of the group*/ 144 145 write_to_file (mytaskfile, "a", getpid()); /* Assign the task to it's group*/ 146 147 fd = open ("./myfifo", 0); 148 if (fd == -1) 149 { 150 tst_brkm (TBROK, cleanup, "Could not open fifo for synchronization"); 151 } 152 153 read (fd, &ch, 1); /* To block all tasks here and fire them up at the same time*/ 154 155 /* 156 * We now calculate the expected % cpu time of this task by getting 157 * it's group's shares, the total shares of all the groups and the 158 * number of tasks in this group. 159 */ 160 FLAG = 0; 161 total_shares = 0; 162 shares_pointer = &total_shares; 163 len = strlen (path); 164 if (!strncpy (fullpath, path, len)) 165 tst_brkm (TBROK, cleanup, "Could not copy directory path %s ", path); 166 167 if (scan_shares_files() != 0) 168 tst_brkm (TBROK, cleanup, "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 ", mysharesfile); 173 174 if ((read_file (mytaskfile, GET_TASKS, &num_tasks)) < 0) 175 tst_brkm (TBROK, cleanup, "in reading tasks files %s ", mytaskfile); 176 177 exp_cpu_time = (double)(fmyshares * 100) /(total_shares * num_tasks); 178 179 prev_time = time (NULL); /* Note down the time*/ 180 181 while (1) 182 { 183 /* Need to run some cpu intensive task, which also frequently checks the timer value*/ 184 double f = 274.345, mytime; /*just a float number to take sqrt*/ 185 alarm (TIME_INTERVAL); 186 timer_expired = 0; 187 while (!timer_expired) /* Let the task run on cpu for TIME_INTERVAL*/ 188 f = sqrt (f*f); /* Time of this operation should not be high otherwise we can 189 * exceed the TIME_INTERVAL to measure cpu usage 190 */ 191 current_time = time (NULL); 192 delta_time = current_time - prev_time; /* Duration in case its not exact TIME_INTERVAL*/ 193 194 getrusage (0, &cpu_usage); 195 total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 + /* user time*/ 196 cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6) ; /* system time*/ 197 delta_cpu_time = total_cpu_time - prev_cpu_time; 198 199 prev_cpu_time = total_cpu_time; 200 prev_time = current_time; 201 202 /* calculate % cpu time each task gets */ 203 if (delta_time > TIME_INTERVAL) 204 mytime = (delta_cpu_time * 100) / (delta_time * num_cpus); 205 else 206 mytime = (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus); 207 208 fprintf (stdout,"Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(\%) exp:-%6.2f(\%)}\ 209with %3lu shares in %lu (s) INTERVAL\n",mygroup_num, task_num, delta_cpu_time, mytime,\ 210exp_cpu_time, fmyshares, delta_time); 211 212 counter++; 213 214 if (counter >= NUM_INTERVALS) /* Take n sets of readings for each shares value*/ 215 { 216 switch (test_num) 217 { 218 case 9: /* Test 09 */ 219 exit (0); /* This task is done with its job*/ 220 break; 221 case 10: /* Test 10 */ 222 exit (0); /* This task is done with its job*/ 223 break; 224 default: 225 tst_brkm (TBROK, cleanup, "Invalid test number passed\n"); 226 break; 227 228 } /* end switch*/ 229 } /* end if*/ 230 } /* end while*/ 231} /* end main*/ 232 233