Blame src/examples/PAPI_add_remove_events.c

Packit 577717
/******************************************************************************
Packit 577717
 * This is a simple low level function demonstration on using PAPI_add_events *
Packit 577717
 * to add an array of events to a created eventset, we are going to use these *
Packit 577717
 * events to monitor a set of instructions, start the counters, read the      *
Packit 577717
 * counters and then cleanup the eventset when done. In this example we use   *
Packit 577717
 * the presets PAPI_TOT_INS and PAPI_TOT_CYC. PAPI_add_events,PAPI_start,     *
Packit 577717
 * PAPI_stop, PAPI_clean_eventset, PAPI_destroy_eventset and                  *
Packit 577717
 * PAPI_create_eventset all return PAPI_OK(which is 0) when succesful.        *
Packit 577717
 ******************************************************************************/
Packit 577717
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include "papi.h" /* This needs to be included every time you use PAPI */
Packit 577717
Packit 577717
#define NUM_EVENT 2
Packit 577717
#define THRESHOLD 100000
Packit 577717
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
Packit 577717
Packit 577717
Packit 577717
int main(){
Packit 577717
 
Packit 577717
	int i,retval,tmp;
Packit 577717
    int EventSet = PAPI_NULL;
Packit 577717
    /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
Packit 577717
Packit 577717
    int event_codes[NUM_EVENT]={PAPI_TOT_INS,PAPI_TOT_CYC}; 
Packit 577717
    char errstring[PAPI_MAX_STR_LEN];
Packit 577717
    long long values[NUM_EVENT];
Packit 577717
Packit 577717
   /***************************************************************************
Packit 577717
   * This part initializes the library and compares the version number of the *
Packit 577717
   * header file, to the version of the library, if these don't match then it *
Packit 577717
   * is likely that PAPI won't work correctly.If there is an error, retval    *
Packit 577717
   * keeps track of the version number.                                       *
Packit 577717
   ****************************************************************************/
Packit 577717
Packit 577717
    if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
Packit 577717
    {
Packit 577717
		fprintf(stderr, "Error: %s\n", errstring);
Packit 577717
    	exit(1);
Packit 577717
    }
Packit 577717
Packit 577717
   
Packit 577717
  	/* Creating event set   */
Packit 577717
  	if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
Packit 577717
Packit 577717
  	/* Add the array of events PAPI_TOT_INS and PAPI_TOT_CYC to the eventset*/
Packit 577717
	if ((retval=PAPI_add_events(EventSet, event_codes, NUM_EVENT)) != PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
  
Packit 577717
       
Packit 577717
	/* Start counting */
Packit 577717
	if ( (retval=PAPI_start(EventSet)) != PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
     
Packit 577717
    /*** this is where your computation goes *********/
Packit 577717
	for(i=0;i<1000;i++)
Packit 577717
    {
Packit 577717
    	tmp = tmp+i;
Packit 577717
    }  
Packit 577717
Packit 577717
    /* Stop counting, this reads from the counter as well as stop it. */
Packit 577717
    if ( (retval=PAPI_stop(EventSet,values)) != PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
Packit 577717
	printf("\nThe total instructions executed are %lld, total cycles %lld\n",
Packit 577717
            values[0],values[1]);
Packit 577717
Packit 577717
    
Packit 577717
	if ( (retval=PAPI_remove_events(EventSet,event_codes, NUM_EVENT))!=PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
Packit 577717
    /* Free all memory and data structures, EventSet must be empty. */
Packit 577717
    if ( (retval=PAPI_destroy_eventset(&EventSet)) != PAPI_OK)
Packit 577717
		ERROR_RETURN(retval);
Packit 577717
Packit 577717
    /* free the resources used by PAPI */
Packit 577717
    PAPI_shutdown();
Packit 577717
   
Packit 577717
    exit(0);
Packit 577717
}