Blame src/validation_tests/cycles_validation.c

Packit Service a1973e
/* cycles_validation.c */
Packit Service a1973e
Packit Service a1973e
/* This is based on the old zero.c test */
Packit Service a1973e
Packit Service a1973e
/* It fails on some platforms due to differences in	*/
Packit Service a1973e
/* PAPI_TOT_CYCLES / real_cycles / virt_cycles		*/
Packit Service a1973e
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
Packit Service a1973e
#include "papi.h"
Packit Service a1973e
#include "papi_test.h"
Packit Service a1973e
Packit Service a1973e
#include "testcode.h"
Packit Service a1973e
Packit Service a1973e
#define MAX_CYCLE_ERROR 30
Packit Service a1973e
Packit Service a1973e
#define NUM_EVENTS	2
Packit Service a1973e
Packit Service a1973e
#define NUM_LOOPS	200
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main( int argc, char **argv )
Packit Service a1973e
{
Packit Service a1973e
	int retval, tmp, result, i;
Packit Service a1973e
	int EventSet1 = PAPI_NULL;
Packit Service a1973e
	long long values[NUM_EVENTS];
Packit Service a1973e
	long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
Packit Service a1973e
	double cycles_error;
Packit Service a1973e
	int quiet=0;
Packit Service a1973e
Packit Service a1973e
	/* Set TESTS_QUIET variable */
Packit Service a1973e
	quiet=tests_quiet( argc, argv );
Packit Service a1973e
Packit Service a1973e
	/* Init the PAPI library */
Packit Service a1973e
	retval = PAPI_library_init( PAPI_VER_CURRENT );
Packit Service a1973e
	if ( retval != PAPI_VER_CURRENT ) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Initialize the EventSet */
Packit Service a1973e
	retval=PAPI_create_eventset(&EventSet1);
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Add PAPI_TOT_CYC */
Packit Service a1973e
	retval=PAPI_add_named_event(EventSet1,"PAPI_TOT_CYC");
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		if (!quiet) printf("Trouble adding PAPI_TOT_CYC\n");
Packit Service a1973e
		test_skip( __FILE__, __LINE__, "adding PAPI_TOT_CYC", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Add PAPI_TOT_INS */
Packit Service a1973e
	retval=PAPI_add_named_event(EventSet1,"PAPI_TOT_INS");
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "adding PAPI_TOT_INS", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* warm up the processor to pull it out of idle state */
Packit Service a1973e
	for(i=0;i<100;i++) {
Packit Service a1973e
		result=instructions_million();
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	if (result==CODE_UNIMPLEMENTED) {
Packit Service a1973e
		if (!quiet) printf("Instructions testcode not available\n");
Packit Service a1973e
		test_skip( __FILE__, __LINE__, "No instructions code", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Gather before stats */
Packit Service a1973e
	elapsed_us = PAPI_get_real_usec(  );
Packit Service a1973e
	elapsed_cyc = PAPI_get_real_cyc(  );
Packit Service a1973e
	elapsed_virt_us = PAPI_get_virt_usec(  );
Packit Service a1973e
	elapsed_virt_cyc = PAPI_get_virt_cyc(  );
Packit Service a1973e
Packit Service a1973e
	/* Start PAPI */
Packit Service a1973e
	retval = PAPI_start( EventSet1 );
Packit Service a1973e
	if ( retval != PAPI_OK ) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* our work code */
Packit Service a1973e
	for(i=0;i
Packit Service a1973e
		instructions_million();
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Stop PAPI */
Packit Service a1973e
	retval = PAPI_stop( EventSet1, values );
Packit Service a1973e
	if ( retval != PAPI_OK ) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Calculate total values */
Packit Service a1973e
	elapsed_virt_us = PAPI_get_virt_usec(  ) - elapsed_virt_us;
Packit Service a1973e
	elapsed_virt_cyc = PAPI_get_virt_cyc(  ) - elapsed_virt_cyc;
Packit Service a1973e
	elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
Packit Service a1973e
	elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
Packit Service a1973e
Packit Service a1973e
	/* Shutdown the EventSet */
Packit Service a1973e
	retval = PAPI_remove_named_event( EventSet1, "PAPI_TOT_CYC" );
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_remove_named_event", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	retval = PAPI_remove_named_event( EventSet1, "PAPI_TOT_INS" );
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_remove_named_event", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	retval=PAPI_destroy_eventset( &EventSet1 );
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Print the results */
Packit Service a1973e
	if ( !quiet ) {
Packit Service a1973e
		printf( "Test case 0: start, stop.\n" );
Packit Service a1973e
		printf( "-----------------------------------------------\n" );
Packit Service a1973e
		tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
Packit Service a1973e
		printf( "Default domain is: %d (%s)\n", tmp,
Packit Service a1973e
				stringify_all_domains( tmp ) );
Packit Service a1973e
		tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
Packit Service a1973e
		printf( "Default granularity is: %d (%s)\n", tmp,
Packit Service a1973e
				stringify_granularity( tmp ) );
Packit Service a1973e
		printf( "Using %d iterations 1 million instructions\n", NUM_LOOPS );
Packit Service a1973e
		printf( "-------------------------------------------------------------------------\n" );
Packit Service a1973e
Packit Service a1973e
		printf( "Test type    : \t           1\n" );
Packit Service a1973e
Packit Service a1973e
		/* cycles is first, other event second */
Packit Service a1973e
		printf( "%-12s %12lld\n", "PAPI_TOT_CYC : \t", values[0] );
Packit Service a1973e
		printf( "%-12s %12lld\n", "PAPI_TOT_INS : \t", values[1] );
Packit Service a1973e
Packit Service a1973e
		printf( "%-12s %12lld\n", "Real usec    : \t", elapsed_us );
Packit Service a1973e
		printf( "%-12s %12lld\n", "Real cycles  : \t", elapsed_cyc );
Packit Service a1973e
		printf( "%-12s %12lld\n", "Virt usec    : \t", elapsed_virt_us );
Packit Service a1973e
		printf( "%-12s %12lld\n", "Virt cycles  : \t", elapsed_virt_cyc );
Packit Service a1973e
Packit Service a1973e
		printf( "-------------------------------------------------------------------------\n" );
Packit Service a1973e
Packit Service a1973e
		printf( "Verification: PAPI_TOT_CYC should be roughly real_cycles\n" );
Packit Service a1973e
		printf( "NOTE: Not true if dynamic frequency scaling or turbo boost is enabled.\n" );
Packit Service a1973e
		printf( "Verification: PAPI_TOT_INS should be roughly %d\n", NUM_LOOPS*1000000 );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Check that TOT_CYC and real_cycles roughly match */
Packit Service a1973e
	cycles_error=100.0*((double)values[0] - (double)elapsed_cyc)/((double)elapsed_cyc);
Packit Service a1973e
	if ((cycles_error > MAX_CYCLE_ERROR) || (cycles_error < -MAX_CYCLE_ERROR)) {
Packit Service a1973e
		if (!quiet) printf("PAPI_TOT_CYC Error of %.2f%%\n",cycles_error);
Packit Service a1973e
		test_warn( __FILE__, __LINE__, "Cycles validation", 0 );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* Check that TOT_INS is reasonable */
Packit Service a1973e
	if (abs(values[1] - (1000000*NUM_LOOPS)) > (1000000*NUM_LOOPS)) {
Packit Service a1973e
		printf("%s Error of %.2f%%\n", "PAPI_TOT_INS", (100.0 * (double)(values[1] - (1000000*NUM_LOOPS)))/(1000000*NUM_LOOPS));
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "Instruction validation", 0 );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	test_pass( __FILE__ );
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
}