cpuctl_def_task03.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
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 6-8.                          */
29/*                                                                            */
30/* Total Tests: 3                                                             */
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_test 6,7,8                                     */
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/11/2008 -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 cleanup()
75{
76	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
77	/* Report exit status */
78}
79
80volatile int timer_expired = 0;
81
82int main(int argc, char *argv[])
83{
84
85	int test_num, task_num, len, num_cpus;
86	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
87	char mysharesfile[FILENAME_MAX], ch;
88	/* Following variables are to capture parameters from script */
89	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p;
90	char *test_num_p, *task_num_p;
91	pid_t pid;
92	gid_t mygroup_num;	/* A number attached with a group */
93	int fd;			/* to open a fifo to synchronize */
94	int 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) */
97	 prev_cpu_time = 0;
98	double exp_cpu_time;	/* Exp time in % by shares calculation */
99
100	struct rusage cpu_usage;
101	time_t current_time, prev_time, delta_time;
102	unsigned int fmyshares, num_tasks;
103	struct sigaction newaction, oldaction;
104
105	mygroup_num = -1;
106	num_cpus = 0;
107	task_num = 0;
108	test_num = 0;
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	/* Collect the parameters passed by the script */
117	group_num_p = getenv("GROUP_NUM");
118	mygroup_p = getenv("MYGROUP");
119	script_pid_p = getenv("SCRIPT_PID");
120	num_cpus_p = getenv("NUM_CPUS");
121	test_num_p = getenv("TEST_NUM");
122	task_num_p = getenv("TASK_NUM");
123	/* Check if all of them are valid */
124	if ((test_num_p != NULL) && (((test_num = atoi(test_num_p)) <= 8) &&
125				     ((test_num = atoi(test_num_p)) >= 6))) {
126		if ((group_num_p != NULL) && (mygroup_p != NULL) &&
127		    (script_pid_p != NULL) && (num_cpus_p != NULL) &&
128		    (task_num_p != NULL)) {
129			mygroup_num = atoi(group_num_p);
130			scriptpid = atoi(script_pid_p);
131			num_cpus = atoi(num_cpus_p);
132			task_num = atoi(task_num_p);
133			sprintf(mygroup, "%s", mygroup_p);
134		} else {
135			tst_brkm(TBROK, cleanup,
136				 "Invalid other input parameters\n");
137		}
138	} else {
139		tst_brkm(TBROK, cleanup, "Invalid test number passed\n");
140	}
141
142	sprintf(mytaskfile, "%s", mygroup);
143	sprintf(mysharesfile, "%s", mygroup);
144	strcat(mytaskfile, "/tasks");
145	strcat(mysharesfile, "/cpu.shares");
146	pid = getpid();
147	write_to_file(mytaskfile, "a", pid);	/* Assign task to it's group */
148
149	fd = open("./myfifo", 0);
150	if (fd == -1)
151		tst_brkm(TBROK, cleanup, "Could not open fifo synchronize");
152
153	read(fd, &ch, 1);	/* Block task here to synchronize */
154
155	/*
156	 * We need not calculate the expected % cpu time of this task, as
157	 * neither it is required nor it can be predicted as there are idle
158	 * system tasks (and others too) 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,
166			 "Could not copy directory path %s ", path);
167
168	if (scan_shares_files(shares_pointer) != 0)
169		tst_brkm(TBROK, cleanup,
170			 "From function scan_shares_files in %s ", fullpath);
171
172	/* return val -1 in case of function error, else 2 is min share value */
173	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
174		tst_brkm(TBROK, cleanup,
175			 "in reading shares files  %s ", mysharesfile);
176
177	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
178		tst_brkm(TBROK, cleanup,
179			 "in reading tasks files  %s ", mytaskfile);
180
181	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
182
183	prev_time = time(NULL);	/* Note down the time */
184
185	while (1) {
186		/*
187		 * Need to run some cpu intensive task, which also
188		 * frequently checks the timer value
189		 */
190		double f = 274.345, mytime;	/*just a float number for sqrt */
191		alarm(TIME_INTERVAL);
192		timer_expired = 0;
193		/*
194		 * Let the task run on cpu for TIME_INTERVAL. Time of this
195		 * operation should not be high otherwise we can exceed the
196		 * TIME_INTERVAL to measure cpu usage
197		 */
198		while (!timer_expired)
199			f = sqrt(f * f);
200
201		current_time = time(NULL);
202		/* Duration in case its not exact TIME_INTERVAL */
203		delta_time = current_time - prev_time;
204
205		getrusage(0, &cpu_usage);
206		/* total_cpu_time = total user time + total sys time */
207		total_cpu_time = (cpu_usage.ru_utime.tv_sec +
208				  cpu_usage.ru_utime.tv_usec * 1e-6 +
209				  cpu_usage.ru_stime.tv_sec +
210				  cpu_usage.ru_stime.tv_usec * 1e-6);
211		delta_cpu_time = total_cpu_time - prev_cpu_time;
212
213		prev_cpu_time = total_cpu_time;
214		prev_time = current_time;
215
216		/* calculate % cpu time each task gets */
217		if (delta_time > TIME_INTERVAL)
218			mytime = (delta_cpu_time * 100) /
219			    (delta_time * num_cpus);
220		else
221			mytime = (delta_cpu_time * 100) /
222			    (TIME_INTERVAL * num_cpus);
223
224		/* No neeed to print the results */
225		fprintf(stdout, "Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)"
226			" i.e. %6.2f(%%) exp:-%6.2f(%%)} in %lu (s)\n",
227			mygroup_num, task_num, delta_cpu_time, mytime,
228			exp_cpu_time, delta_time);
229
230		counter++;
231
232		if (counter >= NUM_INTERVALS) {
233			switch (test_num) {
234			case 6:	/* Test 06 */
235			case 7:	/* Test 07 */
236			case 8:	/* Test 08 */
237				exit(0);	/* This task is done */
238				break;
239			default:
240				tst_brkm(TBROK, cleanup,
241					 "Invalid test num passed\n");
242				break;
243
244			}	/* end switch */
245		}		/* end if */
246	}			/* end while */
247}				/* end main */
248