cpuctl_test02.c revision 8e843c6a701cc4de95ca6cc501ba5ee901be3a5a
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_test02.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 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 sudhirkumarmalik@in.ibm.com                      */
41/*                                                                            */
42/* History:                                                                   */
43/* Created-     20/12/2007 -Sudhir Kumar sudhirkumarmalik@in.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	60	/* Time interval in seconds*/
65#define NUM_INTERVALS	3       /* How many iterations of TIME_INTERVAL */
66
67extern int Tst_count;
68char *TCID = "cpu_controller_test04";
69int TST_TOTAL = 1;
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 migrate_task ();
81int timer_expired = 0;
82
83int main(int argc, char* argv[])
84{
85
86	int test_num, task_num, len, num_cpus;	/* num_cpus to calculate cpu time in %*/
87	int migrate=0;			/* For task migration*/
88	char mygroup[32], mytaskfile[32], mysharesfile[32], ch;
89	/* Following variables are to capture parameters from script*/
90	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p, *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
100	struct rusage cpu_usage;
101	time_t current_time, prev_time, delta_time;
102        unsigned int fmyshares, num_tasks;/* f-> from file. num_tasks is tasks in this group*/
103	struct sigaction newaction, oldaction;
104	/* Signal handling for alarm*/
105	sigemptyset (&newaction.sa_mask);
106	newaction.sa_handler = signal_handler_alarm;
107	newaction.sa_flags=0;
108	sigaction (SIGALRM, &newaction, &oldaction);
109
110	/* Collect the parameters passed by the script */
111	group_num_p	= getenv("GROUP_NUM");
112	mygroup_p	= getenv("MYGROUP");
113	script_pid_p 	= getenv("SCRIPT_PID");
114	num_cpus_p 	= getenv("NUM_CPUS");
115	test_num_p 	= getenv("TEST_NUM");
116	task_num_p 	= getenv("TASK_NUM");
117	/* Check if all of them are valid */
118	if ((test_num_p != NULL) && (((test_num = atoi(test_num_p)) == 4) || ((test_num =atoi(test_num_p)) == 5)))
119	{
120		if ((group_num_p != NULL) && (mygroup_p != NULL) && \
121			(script_pid_p != NULL) && (num_cpus_p != NULL) && (task_num_p != NULL))
122		{
123			mygroup_num	 = atoi(group_num_p);
124			scriptpid	 = atoi(script_pid_p);
125			num_cpus	 = atoi (num_cpus_p);
126			task_num	 = atoi (task_num_p);
127			sprintf(mygroup,"%s", mygroup_p);
128		}
129		else
130		{
131			tst_brkm (TBROK, cleanup, "Invalid other input parameters\n");
132		}
133	}
134	else
135	{
136		tst_brkm (TBROK, cleanup, "Invalid test number passed\n");
137	}
138
139	sprintf(mytaskfile, "%s", mygroup);
140	sprintf(mysharesfile, "%s", mygroup);
141	strcat (mytaskfile,"/tasks");
142	strcat (mysharesfile,"/cpu.shares");
143	pid = getpid();
144	write_to_file (mytaskfile, "a", pid);    /* Assign the task to it's group*/
145
146	fd = open ("./myfifo", 0);
147	if (fd == -1)
148	{
149		tst_brkm (TBROK, cleanup, "Could not open fifo for synchronization");
150	}
151
152	read (fd, &ch, 1);	         /* To block all tasks here and fire them up at the same time*/
153
154	/*
155	 * We now calculate the expected % cpu time of this task by getting
156	 * it's group's shares, the total shares of all the groups and the
157	 * number of tasks in this group.
158	 */
159	FLAG = 0;
160	total_shares = 0;
161	shares_pointer = &total_shares;
162	len = strlen (path);
163	if (!strncpy (fullpath, path, len))
164		tst_brkm (TBROK, cleanup, "Could not copy directory path %s ", path);
165
166	if (scan_shares_files() != 0)
167		tst_brkm (TBROK, cleanup, "From function scan_shares_files in %s ", fullpath);
168
169	/* return val: -1 in case of function error, else 2 is min share value */
170	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
171		tst_brkm (TBROK, cleanup, "in reading shares files  %s ", mysharesfile);
172
173	if ((read_file (mytaskfile, GET_TASKS, &num_tasks)) < 0)
174		tst_brkm (TBROK, cleanup, "in reading tasks files  %s ", mytaskfile);
175
176	exp_cpu_time = (double)(fmyshares * 100) /(total_shares * num_tasks);
177
178	prev_time = time (NULL);	 /* Note down the time*/
179
180	while (1)
181	{
182		/* Need to run some cpu intensive task, which also frequently checks the timer value*/
183		double f = 274.345, mytime;	/*just a float number to take sqrt*/
184		alarm (TIME_INTERVAL);
185		timer_expired = 0;
186		while (!timer_expired)	/* Let the task run on cpu for TIME_INTERVAL*/
187			f = sqrt (f*f); /* Time of this operation should not be high otherwise we can
188					 * exceed the TIME_INTERVAL to measure cpu usage
189					 */
190		current_time = time (NULL);
191		delta_time = current_time - prev_time;	/* Duration in case its not exact TIME_INTERVAL*/
192
193		getrusage (0, &cpu_usage);
194		total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 + /* user time*/
195				cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6) ;  /* system time*/
196		delta_cpu_time = total_cpu_time - prev_cpu_time;
197
198		prev_cpu_time = total_cpu_time;
199		prev_time = current_time;
200
201		/* calculate % cpu time each task gets */
202		if (delta_time > TIME_INTERVAL)
203			mytime =  (delta_cpu_time * 100) / (delta_time * num_cpus);
204		else
205			mytime =  (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus);
206
207                fprintf (stdout,"Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(\%) exp:-%6.2f(\%)}\
208with %lu(shares) in %lu (s) INTERVAL\n",mygroup_num, task_num, delta_cpu_time, mytime,\
209exp_cpu_time, fmyshares, delta_time);
210
211		counter++;
212
213		if (counter >= NUM_INTERVALS)	 /* Take n sets of readings for each shares value*/
214		{
215		switch (test_num)
216			{
217			case 4:			/* Test04 */
218				exit (0);	/* This task is done with its job*/
219				break;
220			case 5:			/* Test 05 */
221				if (migrate == 0)
222				{
223					counter = 0;
224					fprintf (stdout, "FIRST RUN COMPLETED FOR TASK %d\n", task_num);
225					migrate = 1;
226				}
227				else
228				{
229					fprintf (stdout, "SECOND RUN COMPLETED FOR TASK %d\n", task_num);
230					exit (0);
231				}
232				break;
233			default:
234				tst_brkm (TBROK, cleanup, "Invalid test number passed\n");
235				break;
236
237			}	/* end switch*/
238		}
239		if ((migrate == 1) && (counter == 0))
240		{
241			if (task_num == 1)
242			{
243				if (migrate_task() != 0)
244					tst_brkm (TFAIL, cleanup, "Could not migrate task 1 ");
245				else
246					fprintf (stdout, "TASK 1 MIGRATED FROM GROUP 1 TO GROUP 2\n");
247				strcpy (mytaskfile, "/dev/cpuctl/group_2/tasks");
248			}
249			/*
250			 * Read the shares files and again calculate the cpu fraction
251			 * In test 2(case 4) we need to read tasks file as we migrate task
252			 * Q?? How to ensure other tasks do not read before task 1 migration
253			 */
254			if ((read_file (mytaskfile, GET_TASKS, &num_tasks)) < 0)
255				tst_brkm (TBROK, cleanup, "in reading tasks files  %s ", mytaskfile);
256			exp_cpu_time = (double)(fmyshares * 100) /(total_shares * num_tasks);
257		}
258        }	/* end while*/
259}	/* end main*/
260
261int migrate_task ()
262{
263	char target[32] = "/dev/cpuctl/group_2/tasks";/* Hard coding..Will try dynamic*/
264	pid_t pid = getpid();
265	return (write_to_file (target, "a", pid));
266}
267