Blame src/components/perf_event_uncore/tests/perf_event_amd_northbridge.c

Packit 577717
/*
Packit 577717
 * This file tests uncore events on AMD fam15h Northbridge machines
Packit 577717
 *  The Linux perf_event developers introduced fam15h Northbridge
Packit 577717
 *   support in Linux 3.9 with an interfae similar to fam10h
Packit 577717
 *   where the events were part of the core CPU
Packit 577717
 *  They broke the ABI with Linux 3.10 and made fam15h NB a separate
Packit 577717
 *   PMU, like the Intel uncore support.
Packit 577717
 */
Packit 577717
Packit 577717
#include <stdio.h>
Packit 577717
#include <string.h>
Packit 577717
Packit 577717
#include <sys/utsname.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 main( int argc, char **argv ) {
Packit 577717
Packit 577717
	int retval;
Packit 577717
	int EventSet = PAPI_NULL;
Packit 577717
	long long values[1];
Packit 577717
	char event_name[BUFSIZ];
Packit 577717
	int uncore_cidx=-1;
Packit 577717
	const PAPI_hw_info_t *hwinfo;
Packit 577717
	int quiet;
Packit 577717
	struct utsname uname_info;
Packit 577717
Packit 577717
	/* Set TESTS_QUIET variable */
Packit 577717
	quiet = tests_quiet( argc, argv );
Packit 577717
Packit 577717
	uname(&uname_info);
Packit 577717
	if (!quiet) printf("Found Linux %s\n",uname_info.release);
Packit 577717
Packit 577717
	/* Init the PAPI library */
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
Packit 577717
	/* Check for AMD machine */
Packit 577717
	hwinfo = PAPI_get_hardware_info();
Packit 577717
	if ( hwinfo == NULL ) {
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
Packit 577717
	}
Packit 577717
Packit 577717
	if (hwinfo->vendor != PAPI_VENDOR_AMD) {
Packit 577717
		if (!quiet) printf("Test only for AMD machines\n");
Packit 577717
		test_skip(__FILE__,__LINE__,"Test only for AMD processor",0);
Packit 577717
	}
Packit 577717
Packit 577717
	if ( hwinfo->cpuid_family != 21) {
Packit 577717
		if (!quiet) printf("Test only for fam15h AMD machines\n");
Packit 577717
		test_skip(__FILE__,__LINE__,"Test only for fam15h AMD processor",0);
Packit 577717
	}
Packit 577717
Packit 577717
	if (!strcmp(uname_info.release,"3.9")) {
Packit 577717
Packit 577717
		if (!quiet) printf("Detected 3.9 kernel, using perf_event\n");
Packit 577717
Packit 577717
		/* For kernel 3.9 use regular CPU component */
Packit 577717
Packit 577717
		/* Find the uncore PMU */
Packit 577717
		uncore_cidx=PAPI_get_component_index("perf_event");
Packit 577717
		if (uncore_cidx<0) {
Packit 577717
			test_skip(__FILE__,__LINE__,"perf_event component not found",0);
Packit 577717
		}
Packit 577717
Packit 577717
		/* Get a relevant event name */
Packit 577717
		strncpy(event_name,"DRAM_ACCESSES:ALL", BUFSIZ);
Packit 577717
	}
Packit 577717
	else {
Packit 577717
Packit 577717
		/* 3.10 and later */
Packit 577717
Packit 577717
		if (!quiet) {
Packit 577717
			printf("Detected > 3.9 kernel, using perf_event_uncore\n");
Packit 577717
		}
Packit 577717
Packit 577717
		/* Find the uncore PMU */
Packit 577717
		uncore_cidx=PAPI_get_component_index("perf_event_uncore");
Packit 577717
		if (uncore_cidx<0) {
Packit 577717
			test_skip(__FILE__,__LINE__,"perf_event_uncore component not found",0);
Packit 577717
		}
Packit 577717
Packit 577717
		/* Get a relevant event name */
Packit 577717
		/* This might change once libpfm4 gets new fam15h NB support */
Packit 577717
		strncpy(event_name,"DRAM_ACCESSES:ALL", BUFSIZ);
Packit 577717
	}
Packit 577717
Packit 577717
	/* Create an eventset */
Packit 577717
	retval = PAPI_create_eventset(&EventSet);
Packit 577717
	if (retval != PAPI_OK) {
Packit 577717
		test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
Packit 577717
	}
Packit 577717
Packit 577717
	/* Set a component for the EventSet */
Packit 577717
	retval = PAPI_assign_eventset_component(EventSet, uncore_cidx);
Packit 577717
Packit 577717
	/* we need to set to a certain cpu for uncore to work */
Packit 577717
Packit 577717
	PAPI_cpu_option_t cpu_opt;
Packit 577717
Packit 577717
	cpu_opt.eventset=EventSet;
Packit 577717
	cpu_opt.cpu_num=0;
Packit 577717
Packit 577717
	retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
Packit 577717
	if (retval != PAPI_OK) {
Packit 577717
		test_skip( __FILE__, __LINE__,
Packit 577717
			"this test; trying to PAPI_CPU_ATTACH; need to run as root",
Packit 577717
			retval);
Packit 577717
	}
Packit 577717
Packit 577717
	/* we need to set the granularity to system-wide for uncore to work */
Packit 577717
Packit 577717
	PAPI_granularity_option_t gran_opt;
Packit 577717
Packit 577717
	gran_opt.def_cidx=0;
Packit 577717
	gran_opt.eventset=EventSet;
Packit 577717
	gran_opt.granularity=PAPI_GRN_SYS;
Packit 577717
Packit 577717
	retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
Packit 577717
	if (retval != PAPI_OK) {
Packit 577717
		test_skip( __FILE__, __LINE__,
Packit 577717
			"this test; trying to set PAPI_GRN_SYS",
Packit 577717
			retval);
Packit 577717
	}
Packit 577717
Packit 577717
	/* we need to set domain to be as inclusive as possible */
Packit 577717
Packit 577717
	PAPI_domain_option_t domain_opt;
Packit 577717
Packit 577717
	domain_opt.def_cidx=0;
Packit 577717
	domain_opt.eventset=EventSet;
Packit 577717
	domain_opt.domain=PAPI_DOM_ALL;
Packit 577717
Packit 577717
	retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
Packit 577717
	if (retval != PAPI_OK) {
Packit 577717
		test_skip( __FILE__, __LINE__,
Packit 577717
			"this test; trying to set PAPI_DOM_ALL; need to run as root",
Packit 577717
			retval);
Packit 577717
	}
Packit 577717
Packit 577717
	/* Add our uncore event */
Packit 577717
	retval = PAPI_add_named_event(EventSet, event_name);
Packit 577717
	if (retval != PAPI_OK) {
Packit 577717
		if ( !quiet ) {
Packit 577717
			fprintf(stderr,"Error trying to use event %s\n", event_name);
Packit 577717
		}
Packit 577717
		test_fail(__FILE__, __LINE__, "adding uncore event",retval);
Packit 577717
	}
Packit 577717
Packit 577717
Packit 577717
	/* Start PAPI */
Packit 577717
	retval = PAPI_start( EventSet );
Packit 577717
	if ( retval != PAPI_OK ) {
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit 577717
	}
Packit 577717
Packit 577717
	/* our work code */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	/* Stop PAPI */
Packit 577717
	retval = PAPI_stop( EventSet, values );
Packit 577717
	if ( retval != PAPI_OK ) {
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit 577717
	}
Packit 577717
Packit 577717
	if ( !quiet ) {
Packit 577717
		printf("AMD fam15h Northbridge test:\n");
Packit 577717
		printf("Using event %s\n",event_name);
Packit 577717
		printf("\t%s: %lld\n",event_name,values[0]);
Packit 577717
	}
Packit 577717
Packit 577717
	test_pass( __FILE__ );
Packit 577717
Packit 577717
	return 0;
Packit 577717
}