Blame src/ctests/profile.c

Packit Service a1973e
/*
Packit Service a1973e
* File:    profile.c
Packit Service a1973e
* Author:  Philip Mucci
Packit Service a1973e
*          mucci@cs.utk.edu
Packit Service a1973e
* Mods:    Dan Terpstra
Packit Service a1973e
*          terpstra@cs.utk.edu
Packit Service a1973e
* Mods:    Maynard Johnson
Packit Service a1973e
*          maynardj@us.ibm.com
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
/* This file performs the following test:
Packit Service a1973e
	profiling and program info option call
Packit Service a1973e
Packit Service a1973e
   - This tests the SVR4 profiling interface of PAPI. These are counted
Packit Service a1973e
   in the default counting domain and default granularity, depending on
Packit Service a1973e
   the platform. Usually this is the user domain (PAPI_DOM_USER) and
Packit Service a1973e
   thread context (PAPI_GRN_THR).
Packit Service a1973e
Packit Service a1973e
     The Eventset contains:
Packit Service a1973e
     + PAPI_FP_INS (to profile)
Packit Service a1973e
     + PAPI_TOT_CYC
Packit Service a1973e
Packit Service a1973e
   - Set up profile
Packit Service a1973e
   - Start eventset 1
Packit Service a1973e
   - Do both (flops and reads)
Packit Service a1973e
   - Stop eventset 1
Packit Service a1973e
*/
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
#include "prof_utils.h"
Packit Service a1973e
Packit Service a1973e
#include "do_loops.h"
Packit Service a1973e
Packit Service a1973e
#define PROFILE_ALL
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
do_profile( caddr_t start, unsigned long plength, unsigned scale, int thresh,
Packit Service a1973e
			int bucket )
Packit Service a1973e
{
Packit Service a1973e
	int i, retval;
Packit Service a1973e
	unsigned long blength;
Packit Service a1973e
	int num_buckets;
Packit Service a1973e
Packit Service a1973e
	const char *profstr[5] = { "PAPI_PROFIL_POSIX",
Packit Service a1973e
		"PAPI_PROFIL_RANDOM",
Packit Service a1973e
		"PAPI_PROFIL_WEIGHTED",
Packit Service a1973e
		"PAPI_PROFIL_COMPRESS",
Packit Service a1973e
		"PAPI_PROFIL_<all>"
Packit Service a1973e
	};
Packit Service a1973e
Packit Service a1973e
	int profflags[5] = { PAPI_PROFIL_POSIX,
Packit Service a1973e
		PAPI_PROFIL_POSIX | PAPI_PROFIL_RANDOM,
Packit Service a1973e
		PAPI_PROFIL_POSIX | PAPI_PROFIL_WEIGHTED,
Packit Service a1973e
		PAPI_PROFIL_POSIX | PAPI_PROFIL_COMPRESS,
Packit Service a1973e
		PAPI_PROFIL_POSIX | PAPI_PROFIL_WEIGHTED |
Packit Service a1973e
			PAPI_PROFIL_RANDOM | PAPI_PROFIL_COMPRESS
Packit Service a1973e
	};
Packit Service a1973e
Packit Service a1973e
	do_no_profile( TESTS_QUIET );
Packit Service a1973e
	blength = prof_size( plength, scale, bucket, &num_buckets );
Packit Service a1973e
	prof_alloc( 5, blength );
Packit Service a1973e
Packit Service a1973e
	for ( i = 0; i < 5; i++ ) {
Packit Service a1973e
		if ( !TESTS_QUIET ) {
Packit Service a1973e
			printf( "Test type   : \t%s\n", profstr[i] );
Packit Service a1973e
		}
Packit Service a1973e
Packit Service a1973e
#ifndef SWPROFILE
Packit Service a1973e
		if ( ( retval =
Packit Service a1973e
			   PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
Packit Service a1973e
							EventSet, PAPI_event, thresh,
Packit Service a1973e
							profflags[i] | bucket ) ) != PAPI_OK ) {
Packit Service a1973e
		   if (retval==PAPI_ENOSUPP) {
Packit Service a1973e
		      char warning[BUFSIZ];
Packit Service a1973e
Packit Service a1973e
		      sprintf(warning,"PAPI_profil %s not supported",
Packit Service a1973e
			      profstr[i]);
Packit Service a1973e
		      test_warn( __FILE__, __LINE__, warning, 1 );
Packit Service a1973e
		   }
Packit Service a1973e
		   else {
Packit Service a1973e
		      test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
Packit Service a1973e
		   }
Packit Service a1973e
		}
Packit Service a1973e
#else
Packit Service a1973e
		if ( ( retval =
Packit Service a1973e
			   PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
Packit Service a1973e
							EventSet, PAPI_event, thresh,
Packit Service a1973e
							profflags[i] | bucket | PAPI_PROFIL_FORCE_SW ) ) !=
Packit Service a1973e
			 PAPI_OK ) {
Packit Service a1973e
		   test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
Packit Service a1973e
		}
Packit Service a1973e
#endif
Packit Service a1973e
Packit Service a1973e
		if ( retval != PAPI_OK )
Packit Service a1973e
			break;
Packit Service a1973e
Packit Service a1973e
		if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
Packit Service a1973e
			test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit Service a1973e
Packit Service a1973e
		do_flops( getenv( "NUM_FLOPS" ) ? atoi( getenv( "NUM_FLOPS" ) ) :
Packit Service a1973e
				  NUM_FLOPS );
Packit Service a1973e
Packit Service a1973e
		if ( ( retval = PAPI_stop( EventSet, values[1] ) ) != PAPI_OK )
Packit Service a1973e
			test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit Service a1973e
Packit Service a1973e
		if ( !TESTS_QUIET ) {
Packit Service a1973e
			printf( TAB1, event_name, ( values[1] )[0] );
Packit Service a1973e
			printf( TAB1, "PAPI_TOT_CYC", ( values[1] )[1] );
Packit Service a1973e
		}
Packit Service a1973e
		retval = PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
Packit Service a1973e
							EventSet, PAPI_event, 0,
Packit Service a1973e
							profflags[i] );
Packit Service a1973e
		if (retval != PAPI_OK ) {
Packit Service a1973e
			test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
Packit Service a1973e
		}
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	if ( retval == PAPI_OK ) {
Packit Service a1973e
		if (!TESTS_QUIET) prof_head( blength, bucket, num_buckets,
Packit Service a1973e
				   "address\t\t\tflat\trandom\tweight\tcomprs\tall\n" );
Packit Service a1973e
		if (!TESTS_QUIET) prof_out( start, 5, bucket, num_buckets, scale );
Packit Service a1973e
		retval = prof_check( 5, bucket, num_buckets );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	for ( i = 0; i < 5; i++ ) {
Packit Service a1973e
		free( profbuf[i] );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	return retval;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main( int argc, char **argv )
Packit Service a1973e
{
Packit Service a1973e
	int num_tests = 6;
Packit Service a1973e
	long length;
Packit Service a1973e
	int mask;
Packit Service a1973e
	int retval;
Packit Service a1973e
	int mythreshold = THRESHOLD;
Packit Service a1973e
	const PAPI_exe_info_t *prginfo;
Packit Service a1973e
	caddr_t start, end;
Packit Service a1973e
	int quiet;
Packit Service a1973e
Packit Service a1973e
	/* Set TESTS_QUIET variable */
Packit Service a1973e
	quiet = tests_quiet( argc, argv );
Packit Service a1973e
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
        if ( ( prginfo = PAPI_get_executable_info(  ) ) == NULL ) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_get_executable_info", 1 );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	retval = PAPI_query_event(PAPI_TOT_CYC);
Packit Service a1973e
	if (retval!=PAPI_OK) {
Packit Service a1973e
		if (!quiet) printf("No events found\n");
Packit Service a1973e
		test_skip(__FILE__, __LINE__,"No events found",1);
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	mask = prof_events( num_tests );
Packit Service a1973e
Packit Service a1973e
#ifdef PROFILE_ALL
Packit Service a1973e
/* use these lines to profile entire code address space */
Packit Service a1973e
	start = prginfo->address_info.text_start;
Packit Service a1973e
	end = prginfo->address_info.text_end;
Packit Service a1973e
#else
Packit Service a1973e
/* use these lines to profile only do_flops address space */
Packit Service a1973e
	start = ( caddr_t ) do_flops;
Packit Service a1973e
	end = ( caddr_t ) fdo_flops;
Packit Service a1973e
/* Itanium and ppc64 processors return function descriptors instead of function addresses.
Packit Service a1973e
   You must dereference the descriptor to get the address.
Packit Service a1973e
*/
Packit Service a1973e
#if defined(ITANIUM1) || defined(ITANIUM2) || defined(__powerpc64__)
Packit Service a1973e
	start = ( caddr_t ) ( ( ( struct fdesc * ) start )->ip );
Packit Service a1973e
	end = ( caddr_t ) ( ( ( struct fdesc * ) end )->ip );
Packit Service a1973e
#endif
Packit Service a1973e
#endif
Packit Service a1973e
Packit Service a1973e
#if defined(linux)
Packit Service a1973e
	{
Packit Service a1973e
		char *tmp = getenv( "THRESHOLD" );
Packit Service a1973e
		if ( tmp )
Packit Service a1973e
			mythreshold = atoi( tmp );
Packit Service a1973e
	}
Packit Service a1973e
#endif
Packit Service a1973e
Packit Service a1973e
	length = end - start;
Packit Service a1973e
	if ( length < 0 ) {
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "Profile length < 0!", ( int ) length );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	if (!quiet) {
Packit Service a1973e
		prof_print_address( "Test case profile: "
Packit Service a1973e
			"POSIX compatible profiling with hardware counters.\n",
Packit Service a1973e
			prginfo );
Packit Service a1973e
		prof_print_prof_info( start, end, mythreshold, event_name );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	retval = do_profile( start, ( unsigned long ) length, FULL_SCALE,
Packit Service a1973e
				mythreshold, PAPI_PROFIL_BUCKET_16 );
Packit Service a1973e
	if ( retval == PAPI_OK ) {
Packit Service a1973e
		retval = do_profile( start, ( unsigned long ) length,
Packit Service a1973e
						FULL_SCALE, mythreshold,
Packit Service a1973e
						PAPI_PROFIL_BUCKET_32 );
Packit Service a1973e
	}
Packit Service a1973e
	if ( retval == PAPI_OK ) {
Packit Service a1973e
		retval = do_profile( start, ( unsigned long ) length,
Packit Service a1973e
						FULL_SCALE, mythreshold,
Packit Service a1973e
						PAPI_PROFIL_BUCKET_64 );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	remove_test_events( &EventSet, mask );
Packit Service a1973e
Packit Service a1973e
	test_pass( __FILE__ );
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
}
Packit Service a1973e