Blame src/ctests/attach_cpu.c

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