File: papi_cost.c

package info (click to toggle)
papi 5.7.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,856 kB
  • sloc: ansic: 93,265; fortran: 3,338; xml: 2,460; makefile: 815; sh: 290
file content (518 lines) | stat: -rw-r--r-- 13,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/** file papi_cost.c
  * @brief papi_cost utility.
  *	@page papi_cost
  * @section  NAME
  *		papi_cost - computes execution time costs for basic PAPI operations.
  *
  *	@section Synopsis
  *		papi_cost [-dhps] [-b bins] [-t threshold]
  *
  *	@section Description
  *		papi_cost is a PAPI utility program that computes the min / max / mean / std. deviation
  *		of execution times for PAPI start/stop pairs and for PAPI reads.
  *		This information provides the basic operating cost to a user's program
  *		for collecting hardware counter data.
  *		Command line options control display capabilities.
  *
  *	@section Options
  *	<ul>
  *		<li>-b < bins > Define the number of bins into which the results are
  *			partitioned for display. The default is 100.
  *		<li>-d	Display a graphical distribution of costs in a vertical histogram.
  *		<li>-h	Display help information about this utility.
  *             <li>-p  Display 25/50/75 perecentile results for making boxplots.
  *		<li>-s	Show the number of iterations in each of the first 10
  *			standard deviations above the mean.
  *		<li>-t < threshold > 	Set the threshold for the number of iterations to
  *			measure costs. The default is 1,000,000.
  *	</ul>
  *
  *	@section Bugs
  *		There are no known bugs in this utility. If you find a bug,
  *		it should be reported to the PAPI Mailing List at <ptools-perfapi@icl.utk.edu>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "papi.h"
#include "cost_utils.h"

/* Search for a derived event of type "type" */
static int
find_derived( int i , char *type)
{
	PAPI_event_info_t info;

	PAPI_enum_event( &i, PAPI_ENUM_FIRST );

	do {
		if ( PAPI_get_event_info( i, &info ) == PAPI_OK ) {

			if ( strcmp( info.derived, type) == 0 ) {
				return i;
			}
		}
	} while ( PAPI_enum_event( &i, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK );

	return PAPI_NULL;
}

/* Slight misnomer, find derived event != DERIVED_POSTFIX */
/* Will look for DERIVED_ADD, if not available will also accept DERIVED_SUB */
static int
find_derived_add( int i )
{
	int ret;

	ret = find_derived( i, "DERIVED_ADD");
	if (ret != PAPI_NULL) {
		return ret;
	}

	return find_derived( i, "DERIVED_SUB");
}

static int
find_derived_postfix( int i )
{
  return ( find_derived ( i, "DERIVED_POSTFIX" ) );
}

static void
print_help( void )
{
	printf( "This is the PAPI cost program.\n" );
	printf( "It computes min / max / mean / std. deviation for PAPI start/stop pairs; for PAPI reads, and for PAPI_accums.  Usage:\n\n" );
	printf( "    cost [options] [parameters]\n" );
	printf( "    cost TESTS_QUIET\n\n" );
	printf( "Options:\n\n" );
	printf( "  -b BINS       set the number of bins for the graphical distribution of costs. Default: 100\n" );
	printf( "  -d            show a graphical distribution of costs\n" );
	printf( "  -h            print this help message\n" );
	printf( "  -p            print 25/50/75th percentile results for making boxplots\n");
	printf( "  -s            show number of iterations above the first 10 std deviations\n" );
	printf( "  -t THRESHOLD  set the threshold for the number of iterations. Default: 1,000,000\n" );
	printf( "\n" );
}


static void
print_stats( int i, long long min, long long max, double average, double std )
{
	char *test[] = {
		"loop latency",
		"PAPI_start/stop (2 counters)",
		"PAPI_read (2 counters)",
		"PAPI_read_ts (2 counters)",
		"PAPI_accum (2 counters)",
		"PAPI_reset (2 counters)",
		"PAPI_read (1 derived_postfix counter)",
		"PAPI_read (1 derived_[add|sub] counter)"
	};

	printf( "\nTotal cost for %s over %d iterations\n",
		test[i], num_iters );
	printf( "min cycles   : %lld\nmax cycles   : %lld\n"
		"mean cycles  : %lf\nstd deviation: %lf\n",
		min, max, average, std );
}

static void
print_std_dev( int *s )
{
	int i;

	printf( "\n" );
	printf( "              --------# Standard Deviations Above the Mean--------\n" );
	printf( "0-------1-------2-------3-------4-------5-------6-------7-------8-------9-----10\n" );
	for ( i = 0; i < 10; i++ )
		printf( "  %d\t", s[i] );
	printf( "\n\n" );
}

static void
print_dist( long long min, long long max, int bins, int *d )
{
	int i, j;
	int step = ( int ) ( max - min ) / bins;

	printf( "\nCost distribution profile\n\n" );
	for ( i = 0; i < bins; i++ ) {
		printf( "%8d:", ( int ) min + ( step * i ) );
		if ( d[i] > 100 ) {
			printf( "**************************** %d counts ****************************",
				  d[i] );
		} else {
			for ( j = 0; j < d[i]; j++ )
				printf( "*" );
		}
		printf( "\n" );
	}
}

static void
print_percentile(long long percent25, long long percent50,
		long long percent75,long long percent99)
{
	printf("25%% cycles   : %lld\n50%% cycles   : %lld\n"
		"75%% cycles   : %lld\n99%% cycles   : %lld\n",
		percent25,percent50,percent75,percent99);
}

static void
do_output( int test_type, long long *array, int bins, int show_std_dev,
		   int show_dist, int show_percentile )
{
	int s[10];
	long long min, max;
	double average, std;
	long long percent25,percent50,percent75,percent99;

	std = do_stats( array, &min, &max, &average );
	print_stats( test_type, min, max, average, std );

	if (show_percentile) {
		do_percentile(array,&percent25,&percent50,&percent75,&percent99);
		print_percentile(percent25,percent50,percent75,percent99);
	}

	if ( show_std_dev ) {
		do_std_dev( array, s, std, average );
		print_std_dev( s );
	}

	if ( show_dist ) {
		int *d;
		d = calloc( bins , sizeof ( int ) );
		do_dist( array, min, max, bins, d );
		print_dist( min, max, bins, d );
		free( d );
	}
}


int
main( int argc, char **argv )
{
	int i, retval, EventSet = PAPI_NULL;
	int retval_start,retval_stop;
	int bins = 100;
	int show_dist = 0, show_std_dev = 0, show_percent = 0;
	long long totcyc, values[2];
	long long *array;
	int event;
	PAPI_event_info_t info;
	int c;

	/* Check command-line arguments */

	while ( (c=getopt(argc, argv, "hb:dpst:") ) != -1) {

		switch(c) {

			case 'h':
				print_help();
				exit(1);
			case 'b':
				bins=atoi(optarg);
				break;
			case 'd':
				show_dist=1;
				break;
			case 'p':
				show_percent=1;
				break;
			case 's':
				show_std_dev=1;
				break;
			case 't':
				num_iters=atoi(optarg);
				break;
			default:
				print_help();
				exit(1);
				break;
		}

	}

	printf( "Cost of execution for PAPI start/stop, read and accum.\n" );
	printf( "This test takes a while. Please be patient...\n" );

	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if (retval != PAPI_VER_CURRENT ) {
		fprintf(stderr,"PAPI_library_init\n");
		exit(retval);
	}
	retval = PAPI_set_debug( PAPI_VERB_ECONT );
	if (retval != PAPI_OK ) {
		fprintf(stderr,"PAPI_set_debug\n");
		exit(retval);
	}
	retval = PAPI_query_event( PAPI_TOT_CYC );
	if (retval != PAPI_OK ) {
		fprintf(stderr,"PAPI_query_event\n");
		exit(retval);
	}
	retval = PAPI_query_event( PAPI_TOT_INS );
	if (retval != PAPI_OK ) {
		fprintf(stderr,"PAPI_query_event\n");
		exit(retval);
	}
	retval = PAPI_create_eventset( &EventSet );
	if (retval != PAPI_OK ) {
		fprintf(stderr,"PAPI_create_eventset\n");
		exit(retval);
	}
	retval = PAPI_add_event( EventSet, PAPI_TOT_CYC );
	if (retval != PAPI_OK ) {
		fprintf(stderr,"PAPI_add_event\n");
		exit(retval);
	}
	retval = PAPI_add_event( EventSet, PAPI_TOT_INS );
	if (retval != PAPI_OK ) {
		retval = PAPI_add_event( EventSet, PAPI_TOT_IIS );
		if (retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_add_event\n");
			exit(retval);
		}
	}

	/* Make sure no errors and warm up */

	totcyc = PAPI_get_real_cyc(  );
	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_start");
		exit(retval);
	}
	if ( ( retval = PAPI_stop( EventSet, NULL ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_stop");
		exit(retval);
	}

	array = ( long long * ) malloc( ( size_t ) num_iters * sizeof ( long long ) );
	if ( array == NULL ) {
		fprintf(stderr,"Error allocating memory for results\n");
		exit(retval);
	}

	/* Determine clock latency */

	printf( "\nPerforming loop latency test...\n" );

	for ( i = 0; i < num_iters; i++ ) {
		totcyc = PAPI_get_real_cyc(  );
		totcyc = PAPI_get_real_cyc(  ) - totcyc;
		array[i] = totcyc;
	}

	do_output( 0, array, bins, show_std_dev, show_dist, show_percent );

	/* Start the start/stop eval */

	printf( "\nPerforming start/stop test...\n" );

	for ( i = 0; i < num_iters; i++ ) {
		totcyc = PAPI_get_real_cyc(  );
		retval_start=PAPI_start( EventSet );
		retval_stop=PAPI_stop( EventSet, values );
		totcyc = PAPI_get_real_cyc(  ) - totcyc;
		array[i] = totcyc;
		if (retval_start || retval_stop) {
		   fprintf(stderr,"PAPI start/stop\n");
			exit(retval_start );
		}
	}

	do_output( 1, array, bins, show_std_dev, show_dist, show_percent );

	/* Start the read eval */
	printf( "\nPerforming read test...\n" );

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_start");
		exit(retval);
	}
	PAPI_read( EventSet, values );

	for ( i = 0; i < num_iters; i++ ) {
		totcyc = PAPI_get_real_cyc(  );
		PAPI_read( EventSet, values );
		totcyc = PAPI_get_real_cyc(  ) - totcyc;
		array[i] = totcyc;
	}
	if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_stop");
		exit(retval);
	}

	do_output( 2, array, bins, show_std_dev, show_dist, show_percent );

	/* Start the read with timestamp eval */
	printf( "\nPerforming read with timestamp test...\n" );

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_start");
		exit(retval);
	}
	PAPI_read_ts( EventSet, values, &totcyc );

	for ( i = 0; i < num_iters; i++ ) {
		PAPI_read_ts( EventSet, values, &array[i] );
	}
	if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_stop");
		exit(retval);
	}

	/* post-process the timing array */
	for ( i = num_iters - 1; i > 0; i-- ) {
		array[i] -= array[i - 1];
	}
	array[0] -= totcyc;

	do_output( 3, array, bins, show_std_dev, show_dist, show_percent );

	/* Start the accum eval */
	printf( "\nPerforming accum test...\n" );

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_start");
		exit(retval);
	}
	PAPI_accum( EventSet, values );

	for ( i = 0; i < num_iters; i++ ) {
		totcyc = PAPI_get_real_cyc(  );
		PAPI_accum( EventSet, values );
		totcyc = PAPI_get_real_cyc(  ) - totcyc;
		array[i] = totcyc;
	}
	if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_stop");
		exit(retval);
	}

	do_output( 4, array, bins, show_std_dev, show_dist, show_percent );

	/* Start the reset eval */
	printf( "\nPerforming reset test...\n" );

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_start");
		exit(retval);
	}

	for ( i = 0; i < num_iters; i++ ) {
		totcyc = PAPI_get_real_cyc(  );
		PAPI_reset( EventSet );
		totcyc = PAPI_get_real_cyc(  ) - totcyc;
		array[i] = totcyc;
	}
	if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK ) {
		fprintf(stderr,"PAPI_stop");
		exit(retval);
	}

	do_output( 5, array, bins, show_std_dev, show_dist, show_percent );

	/* Derived POSTFIX event test */
	PAPI_cleanup_eventset( EventSet );

	event = 0 | PAPI_PRESET_MASK;

	event = find_derived_postfix( event );
	if ( event != PAPI_NULL ) {

		PAPI_get_event_info(event, &info);
		printf( "\nPerforming DERIVED_POSTFIX "
			"PAPI_read(%d counters)  test (%s)...",
			info.count, info.symbol );

		retval = PAPI_add_event( EventSet, event);
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_add_event");
			exit(retval);
		}

		retval = PAPI_start( EventSet );
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_start");
			exit(retval);
		}

		PAPI_read( EventSet, values );

		for ( i = 0; i < num_iters; i++ ) {
			totcyc = PAPI_get_real_cyc(  );
			PAPI_read( EventSet, values );
			totcyc = PAPI_get_real_cyc(  ) - totcyc;
			array[i] = totcyc;
		}

		retval = PAPI_stop( EventSet, values );
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_stop");
			exit(retval);
		}

		do_output( 6, array, bins, show_std_dev, show_dist, show_percent );

	} else {
		printf("\tI was unable to find a DERIVED_POSTFIX preset event "
			"to test on this architecture, skipping.\n");
	}

	/* Find a derived ADD event */
	PAPI_cleanup_eventset( EventSet );

	event = 0 | PAPI_PRESET_MASK;

	event = find_derived_add( event );
	if ( event != PAPI_NULL ) {

		PAPI_get_event_info(event, &info);
		printf( "\nPerforming DERIVED_[ADD|SUB] "
			"PAPI_read(%d counters)  test (%s)...",
			info.count, info.symbol );

		retval = PAPI_add_event( EventSet, event);
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_add_event\n");
			exit(retval);
		}

		retval = PAPI_start( EventSet );
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_start");
			exit(retval);
		}

		PAPI_read( EventSet, values );

		for ( i = 0; i < num_iters; i++ ) {
			totcyc = PAPI_get_real_cyc(  );
			PAPI_read( EventSet, values );
			totcyc = PAPI_get_real_cyc(  ) - totcyc;
			array[i] = totcyc;
	  	}

		retval = PAPI_stop( EventSet, values );
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"PAPI_stop");
			exit(retval);
		}

		do_output( 7, array, bins, show_std_dev, show_dist, show_percent );
	} else {
		printf("\tI was unable to find a suitable DERIVED_[ADD|SUB] "
			"event to test, skipping.\n");
	}

	free( array );

	return 0;
}