Home | History | Annotate | Download | only in pi_perf
      1 /******************************************************************************
      2  *
      3  *   Copyright  International Business Machines  Corp., 2007, 2008, 2009
      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  * NAME
     20  *     pi_perf.c
     21  *
     22  * DESCRIPTION
     23  *     Create a scenario with one high, one low and several
     24  *     medium priority threads. Low priority thread holds a PI lock, high
     25  *     priority thread later tries to grab it. The test measures the maximum
     26  *     amount of time the high priority thread has to wait before it gets
     27  *     the lock. This time should be bound by the duration for which low
     28  *     priority thread holds the lock
     29  *
     30  * USAGE:
     31  *     Use run_auto.sh script in current directory to build and run test.
     32  *     Use "-j" to enable jvm simulator.
     33  *
     34  * AUTHOR
     35  *     Author: Sripathi Kodi <sripathik (at) in.ibm.com>
     36  *
     37  * HISTORY
     38  *     2007-Nov-20:    Initial version by Sripathi Kodi <sripathik (at) in.ibm.com>
     39  *     2009-Jul-03:    Pass criteria corrected by Sripathi Kodi
     40  *						      <sripathik (at) in.ibm.com>
     41  *
     42  *****************************************************************************/
     43 
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <limits.h>
     47 #include <unistd.h>
     48 #include <string.h>
     49 #include <math.h>
     50 #include <librttest.h>
     51 #include <libstats.h>
     52 
     53 #define LOWPRIO 30
     54 #define HIGHPRIO 40
     55 #define BUSYPRIO 35
     56 
     57 #define DEF_LOW_WORK_MS 6
     58 #define DEF_HIGH_WORK_MS 1
     59 #define DEF_BUSY_WORK_MS 6
     60 #define DEF_ITERATIONS 100
     61 
     62 #define HIST_BUCKETS 100
     63 #define THRESHOLD 200		/* microseconds */
     64 
     65 pthread_barrier_t bar1, bar2;
     66 pthread_mutex_t lock;
     67 
     68 static int end = 0;
     69 
     70 static unsigned int iterations = DEF_ITERATIONS;
     71 static unsigned int low_work_time = DEF_LOW_WORK_MS;
     72 static unsigned int high_work_time = DEF_HIGH_WORK_MS;
     73 static unsigned int busy_work_time;
     74 static int num_busy = -1;
     75 
     76 nsec_t low_unlock, max_pi_delay;
     77 
     78 stats_container_t low_dat, cpu_delay_dat;
     79 stats_container_t cpu_delay_hist;
     80 stats_quantiles_t cpu_delay_quantiles;
     81 stats_record_t rec;
     82 
     83 void usage(void)
     84 {
     85 	rt_help();
     86 	printf("pi_perf_test specific options:\n");
     87 	printf
     88 	    ("  -nNUMBER   Number of busy threads. Default = number of cpus\n");
     89 	printf("  -iNUMBER   Number of iterations. Default = %d\n",
     90 	       DEF_ITERATIONS);
     91 	printf("  -tPERIOD   Duration of work. Number of ms.\n");
     92 }
     93 
     94 int parse_args(int c, char *v)
     95 {
     96 	int handled = 1;
     97 	switch (c) {
     98 	case 'h':
     99 		usage();
    100 		exit(0);
    101 	case 'i':
    102 		iterations = atoi(v);
    103 		break;
    104 	case 'n':
    105 		num_busy = atoi(v);
    106 		break;
    107 	case 'w':
    108 		low_work_time = atoi(v);
    109 		break;
    110 	default:
    111 		handled = 0;
    112 		break;
    113 	}
    114 	return handled;
    115 }
    116 
    117 void *busy_thread(void *arg)
    118 {
    119 	struct thread *thr = (struct thread *)arg;
    120 
    121 	printf("Busy %ld started\n", (long)thr->arg);
    122 
    123 	while (!end) {
    124 		/* Wait for all threads to reach barrier wait */
    125 		pthread_barrier_wait(&bar1);
    126 		busy_work_ms(busy_work_time);
    127 		/* Wait for all threads to finish this iteration */
    128 		pthread_barrier_wait(&bar2);
    129 	}
    130 	return NULL;
    131 }
    132 
    133 void *low_prio_thread(void *arg)
    134 {
    135 	nsec_t low_start, low_hold;
    136 	unsigned int i;
    137 
    138 	stats_container_init(&low_dat, iterations);
    139 
    140 	printf("Low prio thread started\n");
    141 
    142 	for (i = 0; i < iterations; i++) {
    143 		pthread_mutex_lock(&lock);
    144 		/* Wait for all threads to reach barrier wait.
    145 		   Since we already own the mutex, high prio
    146 		   thread will boost our priority.
    147 		 */
    148 		pthread_barrier_wait(&bar1);
    149 
    150 		low_start = rt_gettime();
    151 		busy_work_ms(low_work_time);
    152 		low_unlock = rt_gettime();
    153 		low_hold = low_unlock - low_start;
    154 
    155 		pthread_mutex_unlock(&lock);
    156 
    157 		rec.x = i;
    158 		rec.y = low_hold / NS_PER_US;
    159 		stats_container_append(&low_dat, rec);
    160 
    161 		if (i == iterations - 1)
    162 			end = 1;
    163 
    164 		/* Wait for all threads to finish this iteration */
    165 		pthread_barrier_wait(&bar2);
    166 	}
    167 
    168 	return NULL;
    169 }
    170 
    171 void *high_prio_thread(void *arg)
    172 {
    173 	nsec_t high_start, high_end, high_get_lock;
    174 	unsigned int i;
    175 
    176 	stats_container_init(&cpu_delay_dat, iterations);
    177 	stats_container_init(&cpu_delay_hist, HIST_BUCKETS);
    178 	stats_quantiles_init(&cpu_delay_quantiles, (int)log10(iterations));
    179 
    180 	printf("High prio thread started\n");
    181 
    182 	for (i = 0; i < iterations; i++) {
    183 		/* Wait for all threads to reach barrier wait. When
    184 		   woken up, low prio thread will own the mutex
    185 		 */
    186 		pthread_barrier_wait(&bar1);
    187 
    188 		high_start = rt_gettime();
    189 		pthread_mutex_lock(&lock);
    190 		high_end = rt_gettime();
    191 		high_get_lock = high_end - low_unlock;
    192 
    193 		busy_work_ms(high_work_time);
    194 		pthread_mutex_unlock(&lock);
    195 
    196 		rec.x = i;
    197 		rec.y = high_get_lock / NS_PER_US;
    198 		stats_container_append(&cpu_delay_dat, rec);
    199 
    200 		/* Wait for all threads to finish this iteration */
    201 		pthread_barrier_wait(&bar2);
    202 	}
    203 
    204 	stats_hist(&cpu_delay_hist, &cpu_delay_dat);
    205 	stats_container_save("samples", "pi_perf Latency Scatter Plot",
    206 			     "Iteration", "Latency (us)", &cpu_delay_dat,
    207 			     "points");
    208 	stats_container_save("hist", "pi_perf Latency Histogram",
    209 			     "Latency (us)", "Samples", &cpu_delay_hist,
    210 			     "steps");
    211 
    212 	printf
    213 	    ("Time taken for high prio thread to get the lock once released by low prio thread\n");
    214 	printf("Min delay = %ld us\n", stats_min(&cpu_delay_dat));
    215 	printf("Max delay = %ld us\n", stats_max(&cpu_delay_dat));
    216 	printf("Average delay = %4.2f us\n", stats_avg(&cpu_delay_dat));
    217 	printf("Standard Deviation = %4.2f us\n", stats_stddev(&cpu_delay_dat));
    218 	printf("Quantiles:\n");
    219 	stats_quantiles_calc(&cpu_delay_dat, &cpu_delay_quantiles);
    220 	stats_quantiles_print(&cpu_delay_quantiles);
    221 
    222 	max_pi_delay = stats_max(&cpu_delay_dat);
    223 
    224 	return NULL;
    225 }
    226 
    227 int main(int argc, char *argv[])
    228 {
    229 	long i;
    230 	int ret;
    231 	setup();
    232 
    233 	pass_criteria = THRESHOLD;
    234 	rt_init("hi:n:w:", parse_args, argc, argv);
    235 
    236 	if (iterations < 100) {
    237 		printf("Number of iterations cannot be less than 100\n");
    238 		exit(1);
    239 	}
    240 
    241 	busy_work_time = low_work_time;
    242 	if (num_busy == -1) {
    243 		/* Number of busy threads = No. of CPUs */
    244 		num_busy = sysconf(_SC_NPROCESSORS_ONLN);
    245 	}
    246 
    247 	if ((ret = pthread_barrier_init(&bar1, NULL, (num_busy + 2)))) {
    248 		printf("pthread_barrier_init failed: %s\n", strerror(ret));
    249 		exit(ret);
    250 	}
    251 	if ((ret = pthread_barrier_init(&bar2, NULL, (num_busy + 2)))) {
    252 		printf("pthread_barrier_init failed: %s\n", strerror(ret));
    253 		exit(ret);
    254 	}
    255 
    256 	init_pi_mutex(&lock);
    257 
    258 	if ((ret = create_fifo_thread(low_prio_thread, NULL, LOWPRIO)) < 0)
    259 		exit(ret);
    260 	if ((ret =
    261 	     create_fifo_thread(high_prio_thread, NULL, HIGHPRIO)) < 0)
    262 		exit(ret);
    263 
    264 	for (i = 0; i < num_busy; i++) {
    265 		if ((ret =
    266 		     create_fifo_thread(busy_thread, (void *)i, BUSYPRIO)) < 0)
    267 			exit(ret);
    268 	}
    269 
    270 	join_threads();
    271 	printf("Criteria: High prio lock wait time < "
    272 	       "(Low prio lock held time + %d us)\n", (int)pass_criteria);
    273 
    274 	ret = 0;
    275 	if (max_pi_delay > pass_criteria)
    276 		ret = 1;
    277 
    278 	printf("Result: %s\n", ret ? "FAIL" : "PASS");
    279 	return ret;
    280 }
    281