Blame src/ctests/attach_cpu.c

Packit Service a1973e
/*
Packit Service a1973e
 * This test case creates an event set and attaches it to a cpu.  This causes only activity
Packit Service a1973e
 * on that cpu to get counted.	The test case then starts the event set does a little work and
Packit Service a1973e
 * then stops the event set.  It then prints out the event, count and cpu number which was used
Packit Service a1973e
 * during the test case.
Packit Service a1973e
 *
Packit Service a1973e
 * Since this test case does not try to force its own execution to the cpu which it is using to
Packit Service a1973e
 * count events, it is fairly normal to get zero counts printed at the end of the test.	 But every
Packit Service a1973e
 * now and then it will count the cpu where the test case is running and then the counts will be non-zero.
Packit Service a1973e
 *
Packit Service a1973e
 * The test case allows the user to specify which cpu should be counted by providing an argument to the
Packit Service a1973e
 * test case (ie: ./attach_cpu 3).  Sometimes by trying different cpu numbers with the test case, you
Packit Service a1973e
 * can find the cpu used to run the test (because counts will look like cycle counts).
Packit Service a1973e
 *
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
Packit Service a1973e
#include "do_loops.h"
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main( int argc, char **argv )
Packit Service a1973e
{
Packit Service a1973e
	int num_tests=1;
Packit Service a1973e
	int num_events=1;
Packit Service a1973e
	int retval;
Packit Service a1973e
	int cpu_num = 1;
Packit Service a1973e
	int EventSet1 = PAPI_NULL;
Packit Service a1973e
	long long **values;
Packit Service a1973e
	char event_name[PAPI_MAX_STR_LEN] = "PAPI_TOT_CYC";
Packit Service a1973e
	PAPI_option_t opts;
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
	// user can provide cpu number on which to count events as arg 1
Packit Service a1973e
	if (argc > 1) {
Packit Service a1973e
		retval = atoi(argv[1]);
Packit Service a1973e
		if (retval >= 0) {
Packit Service a1973e
			cpu_num = retval;
Packit Service a1973e
		}
Packit Service a1973e
	}
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
	retval = PAPI_create_eventset(&EventSet1);
Packit Service a1973e
	if ( retval != PAPI_OK )
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_attach", retval );
Packit Service a1973e
Packit Service a1973e
	// Force event set to be associated with component 0 (perf_events component provides all core events)
Packit Service a1973e
	retval = PAPI_assign_eventset_component( EventSet1, 0 );
Packit Service a1973e
	if ( retval != PAPI_OK )
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval );
Packit Service a1973e
Packit Service a1973e
	// Attach this event set to cpu 1
Packit Service a1973e
	opts.cpu.eventset = EventSet1;
Packit Service a1973e
	opts.cpu.cpu_num = cpu_num;
Packit Service a1973e
Packit Service a1973e
	retval = PAPI_set_opt( PAPI_CPU_ATTACH, &opts );
Packit Service a1973e
	if ( retval != PAPI_OK )
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_set_opt", retval );
Packit Service a1973e
Packit Service a1973e
	retval = PAPI_add_named_event(EventSet1, event_name);
Packit Service a1973e
	if ( retval != PAPI_OK ) {
Packit Service a1973e
		if (!quiet) printf("Trouble adding event %s\n",event_name);
Packit Service a1973e
		test_skip( __FILE__, __LINE__, "PAPI_add_named_event", retval );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	// get space for counter values (this needs to do this call because it malloc's space that test_pass and friends free)
Packit Service a1973e
	values = allocate_test_space( num_tests, num_events);
Packit Service a1973e
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
	// do some work
Packit Service a1973e
	do_flops(NUM_FLOPS);
Packit Service a1973e
Packit Service a1973e
	retval = PAPI_stop( EventSet1, values[0] );
Packit Service a1973e
	if ( retval != PAPI_OK )
Packit Service a1973e
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit Service a1973e
Packit Service a1973e
	if (!quiet) printf ("Event: %s: %8lld on Cpu: %d\n", event_name, values[0][0], cpu_num);
Packit Service a1973e
Packit Service a1973e
	PAPI_shutdown( );
Packit Service a1973e
Packit Service a1973e
	test_pass( __FILE__ );
Packit Service a1973e
Packit Service a1973e
	return 0;
Packit Service a1973e
Packit Service a1973e
}