PAPI  5.0.1.0
PAPI_overflow.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * This example shows how to use PAPI_overflow to set up an event set to      *
00003 * begin registering overflows.
00004 ******************************************************************************/
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include "papi.h"       /* This needs to be included every time you use PAPI */
00009 #include <pthread.h>
00010 
00011 #define OVER_FMT    "handler(%d ) Overflow at %p! bit=0x%llx \n"
00012 #define THRESHOLD 100000 
00013 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
00014 
00015 int total = 0;  /* we use total to track the amount of overflows that occured */
00016 
00017 /* THis is the handler called by PAPI_overflow*/
00018 void
00019 handler(int EventSet, void *address, long long overflow_vector, void *context)
00020 {
00021    fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
00022    total++;
00023 }
00024 
00025 
00026 int main ()
00027 {
00028    int EventSet = PAPI_NULL;    
00029    /* must be set to null before calling PAPI_create_eventset */
00030 
00031    char errstring[PAPI_MAX_STR_LEN];
00032    long long (values[2])[2];
00033    int retval, i;
00034    double tmp = 0;
00035    int PAPI_event;      /* a place holder for an event preset */
00036    char event_name[PAPI_MAX_STR_LEN];
00037 
00038 
00039    /****************************************************************************
00040    *  This part initializes the library and compares the version number of the *
00041    * header file, to the version of the library, if these don't match then it  *
00042    * is likely that PAPI won't work correctly.If there is an error, retval     *
00043    * keeps track of the version number.                                        *
00044    ****************************************************************************/
00045 
00046    if ((retval = PAPI_library_init (PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
00047    {
00048       printf("Library initialization error! \n");
00049       exit(1);
00050    }
00051 
00052    /* Here we create the eventset */
00053    if ((retval=PAPI_create_eventset (&EventSet)) != PAPI_OK)
00054       ERROR_RETURN(retval);
00055 
00056    PAPI_event = PAPI_TOT_INS;
00057 
00058    /* Here we are querying for the existence of the PAPI presets  */
00059    if (PAPI_query_event (PAPI_TOT_INS) != PAPI_OK)
00060    {
00061       PAPI_event = PAPI_TOT_CYC;
00062 
00063       if ((retval=PAPI_query_event (PAPI_TOT_INS)) != PAPI_OK)
00064          ERROR_RETURN(retval);
00065 
00066       printf ("PAPI_TOT_INS not available on this platform.");
00067       printf (" so subst PAPI_event with PAPI_TOT_CYC !\n\n");
00068 
00069    }
00070 
00071 
00072    /* PAPI_event_code_to_name is used to convert a PAPI preset from 
00073      its integer value to its string name. */
00074    if ((retval = PAPI_event_code_to_name (PAPI_event, event_name)) != PAPI_OK)
00075       ERROR_RETURN(retval);
00076 
00077    /* add event to the event set */
00078    if ((retval = PAPI_add_event (EventSet, PAPI_event)) != PAPI_OK)
00079       ERROR_RETURN(retval);
00080 
00081    /* register overflow and set up threshold */
00082    /* The threshold "THRESHOLD" was set to 100000 */
00083    if ((retval = PAPI_overflow (EventSet, PAPI_event, THRESHOLD, 0,
00084                                handler)) != PAPI_OK)
00085       ERROR_RETURN(retval);
00086 
00087    printf ("Here are the addresses at which overflows occured and overflow vectors \n");
00088   printf ("--------------------------------------------------------------\n");
00089 
00090 
00091   /* Start counting */
00092 
00093    if ( (retval=PAPI_start (EventSet)) != PAPI_OK)
00094       ERROR_RETURN(retval);
00095 
00096    for (i = 0; i < 2000000; i++)
00097    {
00098       tmp = 1.01 + tmp;
00099       tmp++;
00100    }
00101 
00102    /* Stops the counters and reads the counter values into the values array */
00103    if ( (retval=PAPI_stop (EventSet, values[0])) != PAPI_OK)
00104       ERROR_RETURN(retval);
00105 
00106 
00107    printf ("The total no of overflows was %d\n", total);
00108 
00109    /* clear the overflow status */
00110    if ((retval = PAPI_overflow (EventSet, PAPI_event, 0, 0,
00111                                handler)) != PAPI_OK)
00112       ERROR_RETURN(retval);
00113 
00114    /************************************************************************
00115     * PAPI_cleanup_eventset can only be used after the counter has been    *
00116     * stopped then it remove all events in the eventset                    *
00117     ************************************************************************/
00118    if ( (retval=PAPI_cleanup_eventset (EventSet)) != PAPI_OK)
00119       ERROR_RETURN(retval);
00120 
00121    /* Free all memory and data structures, EventSet must be empty. */
00122    if ( (retval=PAPI_destroy_eventset(&EventSet)) != PAPI_OK)
00123       ERROR_RETURN(retval);
00124 
00125    /* free the resources used by PAPI */ 
00126    PAPI_shutdown();
00127 
00128    exit(0);
00129 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines