Home | History | Annotate | Download | only in cpuctl
      1 /******************************************************************************/
      2 /*                                                                            */
      3 /* Copyright (c) International Business Machines  Corp., 2008                 */
      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_latency_check_task.c                                   */
     24 /*                                                                            */
     25 /* Description: This is a c program that runs a task which does frequent sleep*/
     26 /*              on a busy machine and checks if there is any added latency    */
     27 /*              The file is to be used by script                              */
     28 /*                                                                            */
     29 /* Total Tests: 1                                                             */
     30 /*                                                                            */
     31 /* Test Name:   cpu_controller_latency_tests                                  */
     32 /*                                                                            */
     33 /* Test Assertion                                                             */
     34 /*              Please refer to the file cpuctl_testplan.txt                  */
     35 /*                                                                            */
     36 /* Author:      Sudhir Kumar skumar (at) linux.vnet.ibm.com                        */
     37 /*                                                                            */
     38 /* History:                                                                   */
     39 /* Created-     26/11/2008 -Sudhir Kumar <skumar (at) linux.vnet.ibm.com>          */
     40 /*                                                                            */
     41 /******************************************************************************/
     42 
     43 #include <unistd.h>
     44 #include <math.h>
     45 #include <signal.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <sys/time.h>
     49 #include <string.h>
     50 
     51 #include "../libcontrollers/libcontrollers.h"
     52 
     53 /* #define VERBOSE	1 to print verbose output */
     54 
     55 #ifdef VERBOSE
     56 #define verbose(x...) printf(x)
     57 #else
     58 #define verbose(x...) do {} while (0);
     59 #endif
     60 
     61 #define NUM_TIMES 200		/* How many intervals you want to check */
     62 #define INTERVALS 1		/* How many milliseconds interval in iterations */
     63 #define USECONDS  1000		/* microseconds to sleep */
     64 #define info	printf("The results FAIL is just intuitive and not exact" \
     65 		" failure. Please look at cpuctl_testplan.txt in the test directory.\n");
     66 
     67 char *TCID = "cpuctl_latency_tests";
     68 int TST_COUNT = 1;
     69 int TST_TOTAL = 1;
     70 pid_t script_pid;
     71 
     72 int main(int argc, char *argv[])
     73 {
     74 	int count, i = 0, iteration = 0;
     75 	int fail = 0, ALLOWED;
     76 	char mytaskfile[FILENAME_MAX];
     77 	int test_num;
     78 	struct timeval prev_time, cur_time;
     79 	unsigned int actual, actual_s, actual_us, sleeptime;
     80 	unsigned int delta, delta_max = 0;
     81 	pid_t script_pid;
     82 
     83 	if ((argc < 4) || (argc > 5)) {
     84 		printf("Invalid #args received from script. Exiting test..\n");
     85 		exit(1);
     86 	}
     87 
     88 	test_num = atoi(argv[1]);
     89 	script_pid = (pid_t) atoi(argv[2]);
     90 	ALLOWED = atoi(argv[3]);
     91 	if ((test_num < 0) || (script_pid < 0) || (ALLOWED < 0)) {
     92 		printf("Invalid args received from script. Exiting test..\n");
     93 		exit(1);
     94 	}
     95 
     96 	if (test_num == 2) {
     97 		strncpy(mytaskfile, argv[4], FILENAME_MAX);
     98 		strncat(mytaskfile, "/tasks",
     99 			FILENAME_MAX - strlen(mytaskfile) - 1);
    100 		write_to_file(mytaskfile, "a", getpid());
    101 
    102 		/* Give a chance to other tasks too to go to their class */
    103 		sleep(8);
    104 	}
    105 
    106 	printf("TINFO \tThe latency check task started\n");
    107 
    108 	/* Let us start capturing the time now */
    109 	for (count = NUM_TIMES; count >= 0; count -= INTERVALS) {
    110 		if (gettimeofday(&prev_time, NULL) == -1)
    111 			perror("In Iteration no 1 \n");
    112 		/* sleep for specified time */
    113 		sleeptime = count * USECONDS;
    114 		usleep(sleeptime);
    115 
    116 		if (gettimeofday(&cur_time, NULL) == -1)
    117 			perror("In Iteration no 1 \n");
    118 
    119 		/* Get the actual difference */
    120 		actual_s = cur_time.tv_sec - prev_time.tv_sec;
    121 		actual_us = cur_time.tv_usec - prev_time.tv_usec;
    122 		actual = 1e6 * actual_s + actual_us;
    123 		delta = actual - sleeptime;
    124 
    125 		/*  capture the maximum latency observed */
    126 		if (delta >= delta_max) {
    127 			delta_max = delta;
    128 			iteration = i;
    129 		}
    130 
    131 		if (delta > ALLOWED)
    132 			fail = 1;
    133 
    134 		verbose("Iteration %d: Exp(us) =%u, Actual =%u delta = %u\n",
    135 			i++, sleeptime, actual, delta);
    136 	}
    137 
    138 	if (fail) {
    139 		printf("FAIL \tThe Latency test %d failed\n", test_num);
    140 		printf("Max latency observed = %u in Iteration %d\n",
    141 		       delta_max, iteration);
    142 		info;
    143 	} else {
    144 		printf("PASS \tThe Latency test %d passed\n", test_num);
    145 		printf("Max latency observed = %u microsec in Iteration %d\n",
    146 		       delta_max, iteration);
    147 	}
    148 	return fail;
    149 }
    150