cpuctl_test04.c revision 354ebb48db8e66a853a58379a4808d5dcd1ceac3
13c744274b453bf857361a94a6950422215c529a9subrata_modak/******************************************************************************/
23c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
33c744274b453bf857361a94a6950422215c529a9subrata_modak/* Copyright (c) International Business Machines  Corp., 2007                 */
43c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
53c744274b453bf857361a94a6950422215c529a9subrata_modak/* This program is free software;  you can redistribute it and/or modify      */
63c744274b453bf857361a94a6950422215c529a9subrata_modak/* it under the terms of the GNU General Public License as published by       */
73c744274b453bf857361a94a6950422215c529a9subrata_modak/* the Free Software Foundation; either version 2 of the License, or          */
83c744274b453bf857361a94a6950422215c529a9subrata_modak/* (at your option) any later version.                                        */
93c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
103c744274b453bf857361a94a6950422215c529a9subrata_modak/* This program is distributed in the hope that it will be useful,            */
113c744274b453bf857361a94a6950422215c529a9subrata_modak/* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
123c744274b453bf857361a94a6950422215c529a9subrata_modak/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
133c744274b453bf857361a94a6950422215c529a9subrata_modak/* the GNU General Public License for more details.                           */
143c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
153c744274b453bf857361a94a6950422215c529a9subrata_modak/* You should have received a copy of the GNU General Public License          */
163c744274b453bf857361a94a6950422215c529a9subrata_modak/* along with this program;  if not, write to the Free Software               */
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
183c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
193c744274b453bf857361a94a6950422215c529a9subrata_modak/******************************************************************************/
203c744274b453bf857361a94a6950422215c529a9subrata_modak
213c744274b453bf857361a94a6950422215c529a9subrata_modak/******************************************************************************/
223c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
233c744274b453bf857361a94a6950422215c529a9subrata_modak/* File:        cpuctl_test04.c                                               */
243c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
253c744274b453bf857361a94a6950422215c529a9subrata_modak/* Description: This is a c program that tests the cpucontroller fairness of  */
263c744274b453bf857361a94a6950422215c529a9subrata_modak/*              scheduling the tasks according to their group shares. This    */
273c744274b453bf857361a94a6950422215c529a9subrata_modak/*              testcase tests the ability of the cpu controller to provide   */
283c744274b453bf857361a94a6950422215c529a9subrata_modak/*              fairness for share values (absolute).                         */
293c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
3072d13509bbe99a677e7b764d4f2f39e2b03b2f81subrata_modak/* Total Tests: 2                                                             */
313c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
328e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak/* Test 09:     Heavy stress test with nice value change                      */
338e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak/* Test 10:     Heavy stress test (effect of heavy group on light group)      */
348e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak/*                                                                            */
353c744274b453bf857361a94a6950422215c529a9subrata_modak/* Test Name:   cpu_controller_test04                                         */
363c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
373c744274b453bf857361a94a6950422215c529a9subrata_modak/* Test Assertion                                                             */
383c744274b453bf857361a94a6950422215c529a9subrata_modak/*              Please refer to the file cpuctl_testplan.txt                  */
393c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
40d3ba25b3d046e18320734e03dbcfe42b089115f8subrata_modak/* Author:      Sudhir Kumar skumar@linux.vnet.ibm.com                        */
413c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
423c744274b453bf857361a94a6950422215c529a9subrata_modak/* History:                                                                   */
43d3ba25b3d046e18320734e03dbcfe42b089115f8subrata_modak/* Created-     20/12/2007 -Sudhir Kumar <skumar@linux.vnet.ibm.com>          */
443c744274b453bf857361a94a6950422215c529a9subrata_modak/*                                                                            */
453c744274b453bf857361a94a6950422215c529a9subrata_modak/******************************************************************************/
463c744274b453bf857361a94a6950422215c529a9subrata_modak
473c744274b453bf857361a94a6950422215c529a9subrata_modak/* Standard Include Files */
483c744274b453bf857361a94a6950422215c529a9subrata_modak#include <unistd.h>
493c744274b453bf857361a94a6950422215c529a9subrata_modak#include <math.h>
503c744274b453bf857361a94a6950422215c529a9subrata_modak#include <signal.h>
513c744274b453bf857361a94a6950422215c529a9subrata_modak#include <stdio.h>
523c744274b453bf857361a94a6950422215c529a9subrata_modak#include <stdlib.h>
533c744274b453bf857361a94a6950422215c529a9subrata_modak#include <string.h>
543c744274b453bf857361a94a6950422215c529a9subrata_modak#include <sys/resource.h>
553c744274b453bf857361a94a6950422215c529a9subrata_modak#include <sys/syscall.h>
563c744274b453bf857361a94a6950422215c529a9subrata_modak#include <sys/time.h>
573c744274b453bf857361a94a6950422215c529a9subrata_modak#include <sys/types.h>
58b15aafd4f07a07e790ac504343cf8d9bd77963afsubrata_modak#include <sys/stat.h>
59b15aafd4f07a07e790ac504343cf8d9bd77963afsubrata_modak#include <fcntl.h>
603c744274b453bf857361a94a6950422215c529a9subrata_modak#include <time.h>
613c744274b453bf857361a94a6950422215c529a9subrata_modak#include <unistd.h>
623c744274b453bf857361a94a6950422215c529a9subrata_modak
63287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak#include "../libcontrollers/libcontrollers.h"
64354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao#include "test.h"		/* LTP harness APIs */
653c744274b453bf857361a94a6950422215c529a9subrata_modak
66354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao#define TIME_INTERVAL	100	/* Time interval in seconds */
67354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao#define NUM_INTERVALS	2	/* How many iterations of TIME_INTERVAL */
683c744274b453bf857361a94a6950422215c529a9subrata_modak
693c744274b453bf857361a94a6950422215c529a9subrata_modakchar *TCID = "cpu_controller_test06";
7072d13509bbe99a677e7b764d4f2f39e2b03b2f81subrata_modakint TST_TOTAL = 2;
713c744274b453bf857361a94a6950422215c529a9subrata_modakpid_t scriptpid;
72287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modakchar path[] = "/dev/cpuctl";
73287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
74354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoextern void cleanup()
753c744274b453bf857361a94a6950422215c529a9subrata_modak{
76354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	kill(scriptpid, SIGUSR1);	/* Inform the shell to do cleanup */
77354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	tst_exit();		/* Report exit status */
783c744274b453bf857361a94a6950422215c529a9subrata_modak}
793c744274b453bf857361a94a6950422215c529a9subrata_modak
80575839ea989d07d4efa8816256469da86c2dab5csubrata_modakvolatile int timer_expired = 0;
813c744274b453bf857361a94a6950422215c529a9subrata_modak
82354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint main(int argc, char *argv[])
833c744274b453bf857361a94a6950422215c529a9subrata_modak{
843c744274b453bf857361a94a6950422215c529a9subrata_modak
85b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	int test_num;
86b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	int task_num;
87b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	int len;
88354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int num_cpus;		/* Total time = TIME_INTERVAL *num_cpus in the machine */
8937aed20773b801a903517a7ef896176d0d93a4efsubrata_modak	char mygroup[FILENAME_MAX], mytaskfile[FILENAME_MAX];
9037aed20773b801a903517a7ef896176d0d93a4efsubrata_modak	char mysharesfile[FILENAME_MAX], ch;
91354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	/* Following variables are to capture parameters from script */
92354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char *group_num_p, *mygroup_p, *script_pid_p, *num_cpus_p, *test_num_p,
93354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	    *task_num_p;
94354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	gid_t mygroup_num;	/* A number attached with a group */
95354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int fd;			/* A descriptor to open a fifo for synchronized start */
96354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int counter = 0;	/* To take n number of readings */
97354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	double total_cpu_time,	/* Accumulated cpu time */
98354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 delta_cpu_time,	/* Time the task could run on cpu(s) (in an interval) */
99354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 prev_cpu_time = 0;
100354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	double exp_cpu_time;	/* Expected time in % as obtained by shares calculation */
1013c744274b453bf857361a94a6950422215c529a9subrata_modak	struct rusage cpu_usage;
1023c744274b453bf857361a94a6950422215c529a9subrata_modak	unsigned long int mygroup_shares;
1033c744274b453bf857361a94a6950422215c529a9subrata_modak	time_t current_time, prev_time, delta_time;
104354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	unsigned int fmyshares, num_tasks;	/* f-> from file. num_tasks is tasks in this group */
1053c744274b453bf857361a94a6950422215c529a9subrata_modak	struct sigaction newaction, oldaction;
106b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper
107b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	mygroup_num = -1;
108b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	num_cpus = 0;
109b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	task_num = 0;
110b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper	test_num = 0;
111b6ff02cd9592e40c4b435ff7755cb69572a23f47Garrett Cooper
112354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	/* Signal handling for alarm */
113354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	sigemptyset(&newaction.sa_mask);
1143c744274b453bf857361a94a6950422215c529a9subrata_modak	newaction.sa_handler = signal_handler_alarm;
115354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	newaction.sa_flags = 0;
116354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	sigaction(SIGALRM, &newaction, &oldaction);
1173c744274b453bf857361a94a6950422215c529a9subrata_modak
1183c744274b453bf857361a94a6950422215c529a9subrata_modak	/* Collect the parameters passed by the script */
119354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	group_num_p = getenv("GROUP_NUM");
120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	mygroup_p = getenv("MYGROUP");
121354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	script_pid_p = getenv("SCRIPT_PID");
122354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	num_cpus_p = getenv("NUM_CPUS");
123354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	test_num_p = getenv("TEST_NUM");
124354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	task_num_p = getenv("TASK_NUM");
1253c744274b453bf857361a94a6950422215c529a9subrata_modak	/* Check if all of them are valid */
126354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((test_num_p != NULL)
127354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	    && (((test_num = atoi(test_num_p)) <= 10)
128354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		&& ((test_num = atoi(test_num_p)) >= 9))) {
129354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if ((group_num_p != NULL) && (mygroup_p != NULL)
130354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    && (script_pid_p != NULL) && (num_cpus_p != NULL)
131354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    && (task_num_p != NULL)) {
132354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			mygroup_num = atoi(group_num_p);
133354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			scriptpid = atoi(script_pid_p);
134354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			num_cpus = atoi(num_cpus_p);
135354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			task_num = atoi(task_num_p);
136354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			sprintf(mygroup, "%s", mygroup_p);
137354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
138354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_brkm(TBROK, cleanup,
139354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 "Invalid other input parameters\n");
1403c744274b453bf857361a94a6950422215c529a9subrata_modak		}
141354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	} else {
142354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup, "Invalid test number passed\n");
1433c744274b453bf857361a94a6950422215c529a9subrata_modak	}
1443c744274b453bf857361a94a6950422215c529a9subrata_modak
145402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	/*
146402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	 * Let us give the default group 100 shares, as other groups will have
147402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	 * a multiple of 100 shares.
148402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	 * WARN: For large num of groups this may hit MAX_SHARES
149402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	 */
150402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak	mygroup_shares = mygroup_num * 100;
151402fe7bdddfd10440b6290c2d7609ebcda03ffdbsubrata_modak
1523c744274b453bf857361a94a6950422215c529a9subrata_modak	sprintf(mytaskfile, "%s", mygroup);
153354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	strcat(mytaskfile, "/tasks");
1543c744274b453bf857361a94a6950422215c529a9subrata_modak	sprintf(mysharesfile, "%s", mygroup);
155354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	strcat(mysharesfile, "/cpu.shares");
1563c744274b453bf857361a94a6950422215c529a9subrata_modak	/* Need some technique so as only 1 task per grp writes to shares file */
1578e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak	if (test_num == 9)
158354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		write_to_file(mysharesfile, "w", mygroup_shares);	/* Write the shares of the group */
15972d13509bbe99a677e7b764d4f2f39e2b03b2f81subrata_modak
160354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	write_to_file(mytaskfile, "a", getpid());	/* Assign the task to it's group */
1613c744274b453bf857361a94a6950422215c529a9subrata_modak
162354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	fd = open("./myfifo", 0);
163354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (fd == -1) {
164354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup,
165354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "Could not open fifo for synchronization");
1663c744274b453bf857361a94a6950422215c529a9subrata_modak	}
1673c744274b453bf857361a94a6950422215c529a9subrata_modak
168354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	read(fd, &ch, 1);	/* To block all tasks here and fire them up at the same time */
169287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
170287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	/*
171287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	 * We now calculate the expected % cpu time of this task by getting
172287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	 * it's group's shares, the total shares of all the groups and the
173287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	 * number of tasks in this group.
174287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	 */
175287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	FLAG = 0;
176287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	total_shares = 0;
177287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	shares_pointer = &total_shares;
178354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	len = strlen(path);
179354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (!strncpy(fullpath, path, len))
180354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup, "Could not copy directory path %s ",
181354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 path);
182287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
183bd1f58065370eddeea275a238b0a439451c8b70esubrata_modak	if (scan_shares_files(shares_pointer) != 0)
184354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup,
185354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "From function scan_shares_files in %s ", fullpath);
186287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
187287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	/* return val: -1 in case of function error, else 2 is min share value */
188287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak	if ((fmyshares = read_shares_file(mysharesfile)) < 2)
189354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup, "in reading shares files  %s ",
190354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 mysharesfile);
191287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
192354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((read_file(mytaskfile, GET_TASKS, &num_tasks)) < 0)
193354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup, "in reading tasks files  %s ",
194354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 mytaskfile);
195287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
196354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	exp_cpu_time = (double)(fmyshares * 100) / (total_shares * num_tasks);
197287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
198354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	prev_time = time(NULL);	/* Note down the time */
1993c744274b453bf857361a94a6950422215c529a9subrata_modak
200354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	while (1) {
201354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		/* Need to run some cpu intensive task, which also frequently checks the timer value */
202354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		double f = 274.345, mytime;	/*just a float number to take sqrt */
203354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		alarm(TIME_INTERVAL);
2043c744274b453bf857361a94a6950422215c529a9subrata_modak		timer_expired = 0;
205354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		while (!timer_expired)	/* Let the task run on cpu for TIME_INTERVAL */
206354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			f = sqrt(f * f);	/* Time of this operation should not be high otherwise we can
207354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 * exceed the TIME_INTERVAL to measure cpu usage
208354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						 */
209354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		current_time = time(NULL);
210354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		delta_time = current_time - prev_time;	/* Duration in case its not exact TIME_INTERVAL */
211354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao
212354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		getrusage(0, &cpu_usage);
213354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		total_cpu_time = (cpu_usage.ru_utime.tv_sec + cpu_usage.ru_utime.tv_usec * 1e-6 +	/* user time */
214354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				  cpu_usage.ru_stime.tv_sec + cpu_usage.ru_stime.tv_usec * 1e-6);	/* system time */
2153c744274b453bf857361a94a6950422215c529a9subrata_modak		delta_cpu_time = total_cpu_time - prev_cpu_time;
2163c744274b453bf857361a94a6950422215c529a9subrata_modak
2173c744274b453bf857361a94a6950422215c529a9subrata_modak		prev_cpu_time = total_cpu_time;
2183c744274b453bf857361a94a6950422215c529a9subrata_modak		prev_time = current_time;
2198e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak
2208e843c6a701cc4de95ca6cc501ba5ee901be3a5asubrata_modak		/* calculate % cpu time each task gets */
2213c744274b453bf857361a94a6950422215c529a9subrata_modak		if (delta_time > TIME_INTERVAL)
222354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			mytime =
223354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    (delta_cpu_time * 100) / (delta_time * num_cpus);
2243c744274b453bf857361a94a6950422215c529a9subrata_modak		else
225354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			mytime =
226354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    (delta_cpu_time * 100) / (TIME_INTERVAL * num_cpus);
2273c744274b453bf857361a94a6950422215c529a9subrata_modak
228354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stdout, "Grp:-%3d task-%3d:CPU TIME{calc:-%6.2f(s)i.e. %6.2f(%%) exp:-%6.2f(%%)}\
229354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaowith %3u shares in %lu (s) INTERVAL\n", mygroup_num, task_num, delta_cpu_time,
230354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			mytime, exp_cpu_time, fmyshares, delta_time);
231287d18bf1010633ce7ce49daabb14f90bc7d997asubrata_modak
2323c744274b453bf857361a94a6950422215c529a9subrata_modak		counter++;
2333c744274b453bf857361a94a6950422215c529a9subrata_modak
234354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (counter >= NUM_INTERVALS) {	/* Take n sets of readings for each shares value */
235354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			switch (test_num) {
236354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 9:	/* Test 09 */
237354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				exit(0);	/* This task is done with its job */
2383c744274b453bf857361a94a6950422215c529a9subrata_modak				break;
239354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 10:	/* Test 10 */
240354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				exit(0);	/* This task is done with its job */
24172d13509bbe99a677e7b764d4f2f39e2b03b2f81subrata_modak				break;
2423c744274b453bf857361a94a6950422215c529a9subrata_modak			default:
243354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				tst_brkm(TBROK, cleanup,
244354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					 "Invalid test number passed\n");
2453c744274b453bf857361a94a6950422215c529a9subrata_modak				break;
2463c744274b453bf857361a94a6950422215c529a9subrata_modak
247354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}	/* end switch */
248354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		}		/* end if */
249354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}			/* end while */
250354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao}				/* end main */
251