|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* This example shows how to use PAPI_library_init, PAPI_create_eventset,
|
|
Packit |
577717 |
* PAPI_add_event, * PAPI_start and PAPI_stop. These 5 functions
|
|
Packit |
577717 |
* will allow a user to do most of the performance information gathering
|
|
Packit |
577717 |
* that they would need. PAPI_read could also be used if you don't want
|
|
Packit |
577717 |
* to stop the EventSet from running but only check the counts.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Also, we will use PAPI_perror for * error information.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* In addition, a new call was created called PAPI_add_env_event
|
|
Packit |
577717 |
* that allows a user to setup environment variable to read
|
|
Packit |
577717 |
* which event should be monitored this allows different events
|
|
Packit |
577717 |
* to be monitored at runtime without recompiling, the syntax
|
|
Packit |
577717 |
* is as follows:
|
|
Packit |
577717 |
* PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
|
|
Packit |
577717 |
* EventSet is the same as in PAPI_add_event
|
|
Packit |
577717 |
* Event is the default event to monitor if the environment variable
|
|
Packit |
577717 |
* does not exist and differs from PAPI_add_event as it is
|
|
Packit |
577717 |
* a pointer.
|
|
Packit |
577717 |
* env_varialbe is the name of the environment variable to look for
|
|
Packit |
577717 |
* the event code, this can be a name, number or hex, for example
|
|
Packit |
577717 |
* PAPI_L1_DCM could be defined in the environment variable as
|
|
Packit |
577717 |
* all of the following: PAPI_L1_DCM, 0x80000000, or -2147483648
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* To use only add_event you would change the calls to
|
|
Packit |
577717 |
* PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
|
|
Packit |
577717 |
* to PAPI_add_event(int *EventSet, int Event);
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* We will also use PAPI_event_code_to_name since the event may have
|
|
Packit |
577717 |
* changed.
|
|
Packit |
577717 |
* Author: Kevin London
|
|
Packit |
577717 |
* email: london@cs.utk.edu
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include "papi.h" /* This needs to be included anytime you use PAPI */
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int main(){
|
|
Packit |
577717 |
int retval,i;
|
|
Packit |
577717 |
int EventSet=PAPI_NULL;
|
|
Packit |
577717 |
int event_code=PAPI_TOT_INS; /* By default monitor total instructions */
|
|
Packit |
577717 |
char errstring[PAPI_MAX_STR_LEN];
|
|
Packit |
577717 |
char event_name[PAPI_MAX_STR_LEN];
|
|
Packit |
577717 |
float a[1000],b[1000],c[1000];
|
|
Packit |
577717 |
long long values;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* This initializes the library and checks the version number of the
|
|
Packit |
577717 |
* header file, to the version of the library, if these don't match
|
|
Packit |
577717 |
* then it is likely that PAPI won't work correctly.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ){
|
|
Packit |
577717 |
/* This call loads up what the error means into errstring
|
|
Packit |
577717 |
* if retval == PAPI_ESYS then it might be beneficial
|
|
Packit |
577717 |
* to call perror as well to see what system call failed
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
PAPI_perror("PAPI_library_init");
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/* Create space for the EventSet */
|
|
Packit |
577717 |
if ( (retval=PAPI_create_eventset( &EventSet ))!=PAPI_OK){
|
|
Packit |
577717 |
PAPI_perror(retval, errstring, PAPI_MAX_STR_LEN);
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* After this call if the environment variable PAPI_EVENT is set,
|
|
Packit |
577717 |
* event_code may contain something different than total instructions.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
if ( (retval=PAPI_add_env_event(&EventSet, &event_code, "PAPI_EVENT"))!=PAPI_OK){
|
|
Packit |
577717 |
PAPI_perror("PAPI_add_env_event");
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/* Now lets start counting */
|
|
Packit |
577717 |
if ( (retval = PAPI_start(EventSet)) != PAPI_OK ){
|
|
Packit |
577717 |
PAPI_perror("PAPI_start");
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* Some work to take up some time, the PAPI_start/PAPI_stop (and/or
|
|
Packit |
577717 |
* PAPI_read) should surround what you want to monitor.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for ( i=0;i<1000;i++){
|
|
Packit |
577717 |
a[i] = b[i]-c[i];
|
|
Packit |
577717 |
c[i] = a[i]*1.2;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( (retval = PAPI_stop(EventSet, &values) ) != PAPI_OK ){
|
|
Packit |
577717 |
PAPI_perror("PAPI_stop");
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( (retval=PAPI_event_code_to_name( event_code, event_name))!=PAPI_OK){
|
|
Packit |
577717 |
PAPI_perror("PAPI_event_code_to_name");
|
|
Packit |
577717 |
exit(-1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Ending values for %s: %lld\n", event_name,values);
|
|
Packit |
577717 |
/* Remove PAPI instrumentation, this is necessary on platforms
|
|
Packit |
577717 |
* that need to release shared memory segments and is always
|
|
Packit |
577717 |
* good practice.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
PAPI_shutdown();
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int PAPI_add_env_event(int *EventSet, int *EventCode, char *env_variable){
|
|
Packit |
577717 |
int real_event=*EventCode;
|
|
Packit |
577717 |
char *eventname;
|
|
Packit |
577717 |
int retval;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( env_variable != NULL ){
|
|
Packit |
577717 |
if ( (eventname=getenv(env_variable)) ) {
|
|
Packit |
577717 |
if ( eventname[0] == 'P' ) { /* Use the PAPI name */
|
|
Packit |
577717 |
retval=PAPI_event_name_to_code(eventname, &real_event );
|
|
Packit |
577717 |
if ( retval != PAPI_OK ) real_event = *EventCode;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
else{
|
|
Packit |
577717 |
if ( strlen(eventname)>1 && eventname[1]=='x')
|
|
Packit |
577717 |
sscanf(eventname, "%#x", &real_event);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
real_event = atoi(eventname);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if ( (retval = PAPI_add_event( *EventSet, real_event))!= PAPI_OK ){
|
|
Packit |
577717 |
if ( real_event != *EventCode ) {
|
|
Packit |
577717 |
if ( (retval = PAPI_add_event( *EventSet, *EventCode)) == PAPI_OK
|
|
Packit |
577717 |
){
|
|
Packit |
577717 |
real_event = *EventCode;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
*EventCode = real_event;
|
|
Packit |
577717 |
return retval;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|