PAPI  5.0.1.0
PAPI_get_opt.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * This is an example using the low level function PAPI_get_opt to query the *
00003  * option settings of the PAPI library or a specific eventset created by the *
00004  * PAPI_create_eventset function. PAPI_set_opt is used on the otherhand to   *
00005  * set PAPI library or event set options.                                    *
00006  *****************************************************************************/
00007 
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include "papi.h" /* This needs to be included every time you use PAPI */
00012 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
00013 
00014 int poorly_tuned_function()
00015 {
00016    float tmp;
00017    int i;
00018 
00019    for(i=1; i<2000; i++)
00020    {
00021       tmp=(tmp+100)/i;
00022    }
00023    return 0;
00024 }
00025 
00026 int main()
00027 {
00028 
00029    int num, retval, EventSet = PAPI_NULL;
00030    PAPI_option_t options;    
00031    long long values[2];
00032 
00033    /****************************************************************************
00034    *  This part initializes the library and compares the version number of the *
00035    * header file, to the version of the library, if these don't match then it  *
00036    * is likely that PAPI won't work correctly.If there is an error, retval     *
00037    * keeps track of the version number.                                        *
00038    ****************************************************************************/
00039 
00040    if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
00041    {
00042       printf("Library initialization error! \n");
00043       exit(1);
00044    }
00045 
00046    /*PAPI_get_opt returns a negative number if there is an error */
00047 
00048    /* This call returns the maximum available hardware counters */
00049    if((num = PAPI_get_opt(PAPI_MAX_HWCTRS,NULL)) <= 0)
00050       ERROR_RETURN(num);
00051 
00052 
00053    printf("This machine has %d counters.\n",num);
00054 
00055    if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
00056       ERROR_RETURN(retval);
00057 
00058    /* Set the domain of this EventSet to counter user and 
00059       kernel modes for this process.                      */
00060         
00061    memset(&options,0x0,sizeof(options));
00062    
00063    options.domain.eventset = EventSet;
00064    /* Default domain is PAPI_DOM_USER */
00065    options.domain.domain = PAPI_DOM_ALL;
00066    /* this sets the options for the domain */
00067    if ((retval=PAPI_set_opt(PAPI_DOMAIN, &options)) != PAPI_OK)
00068       ERROR_RETURN(retval);
00069    /* Add Total Instructions Executed event to the EventSet */
00070    if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
00071       ERROR_RETURN(retval);
00072 
00073    /* Add Total Cycles Executed event to the EventSet */
00074    if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
00075       ERROR_RETURN(retval);
00076 
00077    /* Start counting */
00078    if((retval=PAPI_start(EventSet)) != PAPI_OK)
00079       ERROR_RETURN(retval);
00080 
00081    poorly_tuned_function();
00082 
00083    /* Stop counting */
00084    if((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
00085       ERROR_RETURN(retval);
00086 
00087    printf(" Total instructions: %lld   Total Cycles: %lld \n", values[0],
00088             values[1]);
00089    
00090    /* clean up */
00091    PAPI_shutdown();
00092 
00093    exit(0);
00094 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines