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_def01.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 1-3.                          */
     29 /*                                                                            */
     30 /* Total Tests: 1                                                             */
     31 /*                                                                            */
     32 /* Test Name:   cpu_controller_tests                                          */
     33 /*                                                                            */
     34 /* Test Assertion                                                             */
     35 /*              Please refer to the file cpuctl_testplan.txt                  */
     36 /*                                                                            */
     37 /* Author:      Sudhir Kumar skumar (at) linux.vnet.ibm.com                        */
     38 /*                                                                            */
     39 /* History:                                                                   */
     40 /* Created-     14/11/2008 -Sudhir Kumar <skumar (at) linux.vnet.ibm.com>          */
     41 /*                                                                            */
     42 /******************************************************************************/
     43 
     44 #include <unistd.h>
     45 #include <math.h>
     46 #include <signal.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <sys/resource.h>
     51 #include <sys/syscall.h>
     52 #include <sys/time.h>
     53 #include <sys/types.h>
     54 #include <sys/stat.h>
     55 #include <fcntl.h>
     56 #include <time.h>
     57 
     58 #include "../libcontrollers/libcontrollers.h"
     59 #include "test.h"		/* LTP harness APIs */
     60 
     61 #ifdef DEBUG
     62 #define dbg(x...)	printf(x);
     63 #else
     64 #define dbg(x...)	do {}	while (0)
     65 #endif
     66 
     67 #define TIME_INTERVAL	30	/* Time interval in seconds */
     68 #define NUM_INTERVALS	3	/* How many iterations of TIME_INTERVAL */
     69 #define NUM_SETS	4	/* How many share values (with same ratio) */
     70 #define MULTIPLIER   	10	/* Rate at which share value gets multiplied */
     71 
     72 char *TCID = "cpu_controller_tests";
     73 int TST_TOTAL = 1;
     74 pid_t scriptpid;
     75 char path[FILENAME_MAX] = "/dev/cpuctl";
     76 
     77 extern void cleanup(void)
     78 {
     79 	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
     80 	/* Report exit status */
     81 }
     82 
     83 volatile int timer_expired = 0;
     84 
     85 int main(int argc, char *argv[])
     86 {
     87 
     88 	int num_cpus, test_num, len;	/* Total time = TIME_INTERVAL*num_cpus */
     89 	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
     90 	char mysharesfile[FILENAME_MAX], ch;
     91 	pid_t pid;
     92 	int my_group_num;	/* A number attached with a group */
     93 	int fd;			/* To open a fifo for synchronization */
     94 	int first_counter = 0;	/* To take n number of readings */
     95 	int second_counter = 0;	/* no of times shares have changed */
     96 	double total_cpu_time;	/* Accumulated cpu time */
     97 	double delta_cpu_time;	/* Time the task could run on cpu(s) */
     98 	double prev_cpu_time = 0;
     99 	double exp_cpu_time;	/* Exp time in % by shares calculation */
    100 	struct rusage cpu_usage;
    101 	time_t current_time, prev_time, delta_time;
    102 	unsigned long int myshares = 2, baseshares = 1000;
    103 	unsigned int fmyshares, num_tasks;
    104 	struct sigaction newaction, oldaction;
    105 
    106 	num_cpus = 0;
    107 	test_num = 0;
    108 	my_group_num = -1;
    109 
    110 	/* Signal handling for alarm */
    111 	sigemptyset(&newaction.sa_mask);
    112 	newaction.sa_handler = signal_handler_alarm;
    113 	newaction.sa_flags = 0;
    114 	sigaction(SIGALRM, &newaction, &oldaction);
    115 
    116 	/* Check if all parameters passed are correct */
    117 	if ((argc < 5) || ((my_group_num = atoi(argv[1])) <= 0) ||
    118 	    ((scriptpid = atoi(argv[3])) <= 0) ||
    119 	    ((num_cpus = atoi(argv[4])) <= 0) ||
    120 	    (test_num = atoi(argv[5])) <= 0)
    121 		tst_brkm(TBROK, cleanup, "Invalid input parameters\n");
    122 
    123 	if (test_num == 1)
    124 		myshares *= my_group_num;
    125 	else if (test_num == 3)
    126 		myshares = baseshares;
    127 	else
    128 		tst_brkm(TBROK, cleanup, "Wrong Test num passed. Exiting.\n");
    129 
    130 	sprintf(mygroup, "%s", argv[2]);
    131 	sprintf(mytaskfile, "%s", mygroup);
    132 	sprintf(mysharesfile, "%s", mygroup);
    133 	strcat(mytaskfile, "/tasks");
    134 	strcat(mysharesfile, "/cpu.shares");
    135 	pid = getpid();
    136 	write_to_file(mytaskfile, "a", pid);	/* Assign task to it's group */
    137 	write_to_file(mysharesfile, "w", myshares);
    138 	dbg("Default task's initial shares = %u", myshares);
    139 
    140 	fd = open("./myfifo", 0);
    141 	if (fd == -1)
    142 		tst_brkm(TBROK, cleanup, "Could not open fifo to synchronizae");
    143 
    144 	read(fd, &ch, 1);	/* To fire all the tasks up at the same time */
    145 
    146 	/*
    147 	 * We need not calculate the expected % cpu time of this task, as
    148 	 * neither it is required nor it can be predicted as there are idle
    149 	 * system tasks (and others too) in this group.
    150 	 */
    151 	FLAG = 0;
    152 	total_shares = 0;
    153 	shares_pointer = &total_shares;
    154 	len = strlen(path);
    155 	if (!strncpy(fullpath, path, len))
    156 		tst_brkm(TBROK, cleanup,
    157 			 "Could not copy directory path %s ", path);
    158 
    159 	if (scan_shares_files(shares_pointer) != 0)
    160 		tst_brkm(TBROK, cleanup,
    161 			 "From function scan_shares_files in %s ", fullpath);
    162 
    163 	/* return val -1 in case of function error, else 2 is min share value */
    164 	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
    165 		tst_brkm(TBROK, cleanup,
    166 			 "in reading shares files  %s ", mysharesfile);
    167 
    168 	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
    169 		tst_brkm(TBROK, cleanup,
    170 			 "in reading tasks files  %s ", mytaskfile);
    171 
    172 	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
    173 
    174 	prev_time = time(NULL);	/* Note down the time */
    175 
    176 	while (1) {
    177 		/*
    178 		 * Need to run some cpu intensive task, which also
    179 		 * frequently checks the timer value
    180 		 */
    181 		double f = 274.345, mytime;	/*just a float number for sqrt */
    182 		alarm(TIME_INTERVAL);
    183 		timer_expired = 0;
    184 		/*
    185 		 * Let the task run on cpu for TIME_INTERVAL. Time of this
    186 		 * operation should not be high otherwise we can exceed the
    187 		 * TIME_INTERVAL to measure cpu usage
    188 		 */
    189 		while (!timer_expired)
    190 			f = sqrt(f * f);
    191 
    192 		current_time = time(NULL);
    193 		/* Duration in case its not exact TIME_INTERVAL */
    194 		delta_time = current_time - prev_time;
    195 
    196 		getrusage(0, &cpu_usage);
    197 		/* total_cpu_time = total user time + total sys time */
    198 		total_cpu_time = (cpu_usage.ru_utime.tv_sec +
    199 				  cpu_usage.ru_utime.tv_usec * 1e-6 +
    200 				  cpu_usage.ru_stime.tv_sec +
    201 				  cpu_usage.ru_stime.tv_usec * 1e-6);
    202 		delta_cpu_time = total_cpu_time - prev_cpu_time;
    203 
    204 		prev_cpu_time = total_cpu_time;
    205 		prev_time = current_time;
    206 
    207 		/* calculate % cpu time each task gets */
    208 		if (delta_time > TIME_INTERVAL)
    209 			mytime = (delta_cpu_time * 100) /
    210 			    (delta_time * num_cpus);
    211 		else
    212 			mytime = (delta_cpu_time * 100) /
    213 			    (TIME_INTERVAL * num_cpus);
    214 
    215 		/*
    216 		 * Lets print the results. The exp cpu time calculated may not
    217 		 * be correct due to running system tasks at the moment
    218 		 */
    219 		fprintf(stdout, "DEF TASK:CPU TIME{calc:-%6.2f(s)"
    220 			" i.e. %6.2f(%%) exp:-%6.2f(%%)} with %lu(shares)"
    221 			" in %lu (s) INTERVAL\n", delta_cpu_time, mytime,
    222 			exp_cpu_time, myshares, delta_time);
    223 		first_counter++;
    224 
    225 		/* Take n sets of readings for each shares value */
    226 		if (first_counter >= NUM_INTERVALS) {
    227 			first_counter = 0;
    228 			second_counter++;
    229 			if (second_counter >= NUM_SETS)
    230 				exit(0);	/* This task is done */
    231 
    232 			/* Keep same ratio but change values */
    233 			if (test_num == 1) {
    234 				myshares = MULTIPLIER * myshares;
    235 				write_to_file(mysharesfile, "w", myshares);
    236 			}
    237 			/* No need to change shares for def task for test 3 */
    238 
    239 		}		/* end if */
    240 	}			/* end while */
    241 }				/* end main */
    242