Home | History | Annotate | Download | only in cpuctl
      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 
     64 #define TIME_INTERVAL	30	/* Time interval in seconds */
     65 #define NUM_INTERVALS	3	/* How many iterations of TIME_INTERVAL */
     66 
     67 char *TCID = "cpu_controller_test04";
     68 int TST_TOTAL = 1;
     69 pid_t scriptpid;
     70 char path[] = "/dev/cpuctl";
     71 
     72 extern void cleanup(void)
     73 {
     74 	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
     75 	/* Report exit status */
     76 }
     77 
     78 volatile int timer_expired = 0;
     79 
     80 int main(int argc, char *argv[])
     81 {
     82 
     83 	int test_num;
     84 	int task_num;
     85 	int len;
     86 	int num_cpus;
     87 	int migrate = 0;	/* For task migration */
     88 	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
     89 	char mysharesfile[FILENAME_MAX], ch;
     90 	/* Following variables are to capture parameters from script */
     91 	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p;
     92 	char *test_num_p, *task_num_p;
     93 	pid_t pid;
     94 	gid_t mygroup_num;	/* A number attached with a group */
     95 	int fd;			/* to open a fifo to synchronize */
     96 	int counter = 0;	/* To take n number of readings */
     97 	double total_cpu_time;	/* Accumulated cpu time */
     98 	double delta_cpu_time;	/* Time the task could run on cpu(s) */
     99 	double prev_cpu_time = 0;
    100 	double exp_cpu_time;	/* Exp time in % by shares calculation */
    101 
    102 	struct rusage cpu_usage;
    103 	time_t current_time, prev_time, delta_time;
    104 	unsigned int fmyshares, num_tasks;
    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)) == 4) ||
    127 				     ((test_num = atoi(test_num_p)) == 5))) {
    128 		if ((group_num_p != NULL) && (mygroup_p != NULL) &&
    129 		    (script_pid_p != NULL) && (num_cpus_p != NULL) &&
    130 		    (task_num_p != NULL)) {
    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 		} else {
    137 			tst_brkm(TBROK, cleanup,
    138 				 "Invalid other input parameters\n");
    139 		}
    140 	} else {
    141 		tst_brkm(TBROK, cleanup, "Invalid test number passed\n");
    142 	}
    143 
    144 	sprintf(mytaskfile, "%s", mygroup);
    145 	sprintf(mysharesfile, "%s", mygroup);
    146 	strcat(mytaskfile, "/tasks");
    147 	strcat(mysharesfile, "/cpu.shares");
    148 	pid = getpid();
    149 	write_to_file(mytaskfile, "a", pid);	/* Assign task to it's group */
    150 
    151 	fd = open("./myfifo", 0);
    152 	if (fd == -1)
    153 		tst_brkm(TBROK, cleanup,
    154 			 "Could not open fifo for synchronization");
    155 
    156 	read(fd, &ch, 1);	/* Block task here to synchronize */
    157 
    158 	/*
    159 	 * We now calculate the expected % cpu time of this task by getting
    160 	 * it's group's shares, the total shares of all the groups and the
    161 	 * number of tasks in this group.
    162 	 */
    163 	FLAG = 0;
    164 	total_shares = 0;
    165 	shares_pointer = &total_shares;
    166 	len = strlen(path);
    167 	if (!strncpy(fullpath, path, len))
    168 		tst_brkm(TBROK, cleanup,
    169 			 "Could not copy directory path %s ", path);
    170 
    171 	if (scan_shares_files(shares_pointer) != 0)
    172 		tst_brkm(TBROK, cleanup,
    173 			 "From function scan_shares_files in %s ", fullpath);
    174 
    175 	/* return val -1 in case of function error, else 2 is min share value */
    176 	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
    177 		tst_brkm(TBROK, cleanup,
    178 			 "in reading shares files  %s ", mysharesfile);
    179 
    180 	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
    181 		tst_brkm(TBROK, cleanup,
    182 			 "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
    191 		 * frequently checks the timer value
    192 		 */
    193 		double f = 274.345, mytime;	/*just a float number for sqrt */
    194 		alarm(TIME_INTERVAL);
    195 		timer_expired = 0;
    196 		/*
    197 		 * Let the task run on cpu for TIME_INTERVAL. Time of this
    198 		 * operation should not be high otherwise we can exceed the
    199 		 * TIME_INTERVAL to measure cpu usage
    200 		 */
    201 		while (!timer_expired)
    202 			f = sqrt(f * f);
    203 
    204 		current_time = time(NULL);
    205 		/* Duration in case its not exact TIME_INTERVAL */
    206 		delta_time = current_time - prev_time;
    207 
    208 		getrusage(0, &cpu_usage);
    209 		/* total_cpu_time = total user time + total sys time */
    210 		total_cpu_time = (cpu_usage.ru_utime.tv_sec +
    211 				  cpu_usage.ru_utime.tv_usec * 1e-6 +
    212 				  cpu_usage.ru_stime.tv_sec +
    213 				  cpu_usage.ru_stime.tv_usec * 1e-6);
    214 		delta_cpu_time = total_cpu_time - prev_cpu_time;
    215 
    216 		prev_cpu_time = total_cpu_time;
    217 		prev_time = current_time;
    218 
    219 		/* calculate % cpu time each task gets */
    220 		if (delta_time > TIME_INTERVAL)
    221 			mytime = (delta_cpu_time * 100) /
    222 			    (delta_time * num_cpus);
    223 		else
    224 			mytime = (delta_cpu_time * 100) /
    225 			    (TIME_INTERVAL * num_cpus);
    226 
    227 		fprintf(stdout, "Grp:-%3dDEF task-%3d: CPU TIME{calc:-%6.2f(s)"
    228 			"i.e. %6.2f(%%)exp:-%6.2f(%%)} with %u(shares) in %lu"
    229 			" (s)\n", mygroup_num, task_num, delta_cpu_time, mytime,
    230 			exp_cpu_time, fmyshares, delta_time);
    231 
    232 		counter++;
    233 
    234 		if (counter >= NUM_INTERVALS) {
    235 			switch (test_num) {
    236 			case 4:	/* Test04 */
    237 				exit(0);	/* This task is done its job */
    238 				break;
    239 			case 5:	/* Test 05 */
    240 				if (migrate == 0) {
    241 					counter = 0;
    242 					migrate = 1;
    243 				} else {
    244 					exit(0);
    245 				}
    246 				break;
    247 			default:
    248 				tst_brkm(TBROK, cleanup,
    249 					 "Invalid test number passed\n");
    250 				break;
    251 
    252 			}	/* end switch */
    253 		}
    254 	}			/* end while */
    255 }				/* end main */
    256