|
PAPI
5.0.1.0
|
00001 /* 00002 * This example shows how to use PAPI_library_init, PAPI_create_eventset, 00003 * PAPI_add_event, * PAPI_start and PAPI_stop. These 5 functions 00004 * will allow a user to do most of the performance information gathering 00005 * that they would need. PAPI_read could also be used if you don't want 00006 * to stop the EventSet from running but only check the counts. 00007 * 00008 * Also, we will use PAPI_perror for * error information. 00009 * 00010 * In addition, a new call was created called PAPI_add_env_event 00011 * that allows a user to setup environment variable to read 00012 * which event should be monitored this allows different events 00013 * to be monitored at runtime without recompiling, the syntax 00014 * is as follows: 00015 * PAPI_add_env_event(int *EventSet, int *Event, char *env_variable); 00016 * EventSet is the same as in PAPI_add_event 00017 * Event is the default event to monitor if the environment variable 00018 * does not exist and differs from PAPI_add_event as it is 00019 * a pointer. 00020 * env_varialbe is the name of the environment variable to look for 00021 * the event code, this can be a name, number or hex, for example 00022 * PAPI_L1_DCM could be defined in the environment variable as 00023 * all of the following: PAPI_L1_DCM, 0x80000000, or -2147483648 00024 * 00025 * To use only add_event you would change the calls to 00026 * PAPI_add_env_event(int *EventSet, int *Event, char *env_variable); 00027 * to PAPI_add_event(int *EventSet, int Event); 00028 * 00029 * We will also use PAPI_event_code_to_name since the event may have 00030 * changed. 00031 * Author: Kevin London 00032 * email: london@cs.utk.edu 00033 */ 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include "papi.h" /* This needs to be included anytime you use PAPI */ 00037 00038 int PAPI_add_env_event(int *EventSet, int *Event, char *env_variable); 00039 00040 00041 int main(){ 00042 int retval,i; 00043 int EventSet=PAPI_NULL; 00044 int event_code=PAPI_TOT_INS; /* By default monitor total instructions */ 00045 char errstring[PAPI_MAX_STR_LEN]; 00046 char event_name[PAPI_MAX_STR_LEN]; 00047 float a[1000],b[1000],c[1000]; 00048 long long values; 00049 00050 00051 /* This initializes the library and checks the version number of the 00052 * header file, to the version of the library, if these don't match 00053 * then it is likely that PAPI won't work correctly. 00054 */ 00055 if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ){ 00056 /* This call loads up what the error means into errstring 00057 * if retval == PAPI_ESYS then it might be beneficial 00058 * to call perror as well to see what system call failed 00059 */ 00060 PAPI_perror("PAPI_library_init"); 00061 exit(-1); 00062 } 00063 /* Create space for the EventSet */ 00064 if ( (retval=PAPI_create_eventset( &EventSet ))!=PAPI_OK){ 00065 PAPI_perror(retval, errstring, PAPI_MAX_STR_LEN); 00066 exit(-1); 00067 } 00068 00069 /* After this call if the environment variable PAPI_EVENT is set, 00070 * event_code may contain something different than total instructions. 00071 */ 00072 if ( (retval=PAPI_add_env_event(&EventSet, &event_code, "PAPI_EVENT"))!=PAPI_OK){ 00073 PAPI_perror("PAPI_add_env_event"); 00074 exit(-1); 00075 } 00076 /* Now lets start counting */ 00077 if ( (retval = PAPI_start(EventSet)) != PAPI_OK ){ 00078 PAPI_perror("PAPI_start"); 00079 exit(-1); 00080 } 00081 00082 /* Some work to take up some time, the PAPI_start/PAPI_stop (and/or 00083 * PAPI_read) should surround what you want to monitor. 00084 */ 00085 for ( i=0;i<1000;i++){ 00086 a[i] = b[i]-c[i]; 00087 c[i] = a[i]*1.2; 00088 } 00089 00090 if ( (retval = PAPI_stop(EventSet, &values) ) != PAPI_OK ){ 00091 PAPI_perror("PAPI_stop"); 00092 exit(-1); 00093 } 00094 00095 if ( (retval=PAPI_event_code_to_name( event_code, event_name))!=PAPI_OK){ 00096 PAPI_perror("PAPI_event_code_to_name"); 00097 exit(-1); 00098 } 00099 00100 printf("Ending values for %s: %lld\n", event_name,values); 00101 /* Remove PAPI instrumentation, this is necessary on platforms 00102 * that need to release shared memory segments and is always 00103 * good practice. 00104 */ 00105 PAPI_shutdown(); 00106 exit(0); 00107 } 00108 00109 00110 00111 int PAPI_add_env_event(int *EventSet, int *EventCode, char *env_variable){ 00112 int real_event=*EventCode; 00113 char *eventname; 00114 int retval; 00115 00116 if ( env_variable != NULL ){ 00117 if ( (eventname=getenv(env_variable)) ) { 00118 if ( eventname[0] == 'P' ) { /* Use the PAPI name */ 00119 retval=PAPI_event_name_to_code(eventname, &real_event ); 00120 if ( retval != PAPI_OK ) real_event = *EventCode; 00121 } 00122 else{ 00123 if ( strlen(eventname)>1 && eventname[1]=='x') 00124 sscanf(eventname, "%x", &real_event); 00125 else 00126 real_event = atoi(eventname); 00127 } 00128 } 00129 } 00130 if ( (retval = PAPI_add_event( *EventSet, real_event))!= PAPI_OK ){ 00131 if ( real_event != *EventCode ) { 00132 if ( (retval = PAPI_add_event( *EventSet, *EventCode)) == PAPI_OK 00133 ){ 00134 real_event = *EventCode; 00135 } 00136 } 00137 } 00138 *EventCode = real_event; 00139 return retval; 00140 } 00141