Blame src/examples/PAPI_overflow.c

Packit 577717
/*****************************************************************************
Packit 577717
* This example shows how to use PAPI_overflow to set up an event set to      *
Packit 577717
* begin registering overflows.
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
#include <pthread.h>
Packit 577717
Packit 577717
#define OVER_FMT    "handler(%d ) Overflow at %p! bit=%#llx \n"
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
int total = 0;	/* we use total to track the amount of overflows that occured */
Packit 577717
Packit 577717
/* THis is the handler called by PAPI_overflow*/
Packit 577717
void
Packit 577717
handler(int EventSet, void *address, long long overflow_vector, void *context)
Packit 577717
{
Packit 577717
   fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
Packit 577717
   total++;
Packit 577717
}
Packit 577717
Packit 577717
Packit 577717
int main ()
Packit 577717
{
Packit 577717
   int EventSet = PAPI_NULL;	
Packit 577717
   /* must be set to null before calling PAPI_create_eventset */
Packit 577717
Packit 577717
   char errstring[PAPI_MAX_STR_LEN];
Packit 577717
   long long (values[2])[2];
Packit 577717
   int retval, i;
Packit 577717
   double tmp = 0;
Packit 577717
   int PAPI_event;		/* a place holder for an event preset */
Packit 577717
   char event_name[PAPI_MAX_STR_LEN];
Packit 577717
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
      printf("Library initialization error! \n");
Packit 577717
      exit(1);
Packit 577717
   }
Packit 577717
Packit 577717
   /* Here we create the eventset */
Packit 577717
   if ((retval=PAPI_create_eventset (&EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   PAPI_event = PAPI_TOT_INS;
Packit 577717
Packit 577717
   /* Here we are querying for the existence of the PAPI presets  */
Packit 577717
   if (PAPI_query_event (PAPI_TOT_INS) != PAPI_OK)
Packit 577717
   {
Packit 577717
      PAPI_event = PAPI_TOT_CYC;
Packit 577717
Packit 577717
      if ((retval=PAPI_query_event (PAPI_TOT_INS)) != PAPI_OK)
Packit 577717
         ERROR_RETURN(retval);
Packit 577717
Packit 577717
      printf ("PAPI_TOT_INS not available on this platform.");
Packit 577717
      printf (" so subst PAPI_event with PAPI_TOT_CYC !\n\n");
Packit 577717
Packit 577717
   }
Packit 577717
Packit 577717
Packit 577717
   /* PAPI_event_code_to_name is used to convert a PAPI preset from 
Packit 577717
     its integer value to its string name. */
Packit 577717
   if ((retval = PAPI_event_code_to_name (PAPI_event, event_name)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* add event to the event set */
Packit 577717
   if ((retval = PAPI_add_event (EventSet, PAPI_event)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* register overflow and set up threshold */
Packit 577717
   /* The threshold "THRESHOLD" was set to 100000 */
Packit 577717
   if ((retval = PAPI_overflow (EventSet, PAPI_event, THRESHOLD, 0,
Packit 577717
		                       handler)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   printf ("Here are the addresses at which overflows occured and overflow vectors \n");
Packit 577717
  printf ("--------------------------------------------------------------\n");
Packit 577717
Packit 577717
Packit 577717
  /* Start counting */
Packit 577717
Packit 577717
   if ( (retval=PAPI_start (EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   for (i = 0; i < 2000000; i++)
Packit 577717
   {
Packit 577717
      tmp = 1.01 + tmp;
Packit 577717
      tmp++;
Packit 577717
   }
Packit 577717
Packit 577717
   /* Stops the counters and reads the counter values into the values array */
Packit 577717
   if ( (retval=PAPI_stop (EventSet, values[0])) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
Packit 577717
   printf ("The total no of overflows was %d\n", total);
Packit 577717
Packit 577717
   /* clear the overflow status */
Packit 577717
   if ((retval = PAPI_overflow (EventSet, PAPI_event, 0, 0,
Packit 577717
		                       handler)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /************************************************************************
Packit 577717
    * PAPI_cleanup_eventset can only be used after the counter has been    *
Packit 577717
    * stopped then it remove all events in the eventset                    *
Packit 577717
    ************************************************************************/
Packit 577717
   if ( (retval=PAPI_cleanup_eventset (EventSet)) != 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
}