Blame src/examples/PAPI_set_domain.c

Packit 577717
/*****************************************************************************
Packit 577717
 * This example shows how to use PAPI_set_domain                             * 
Packit 577717
 *****************************************************************************/
Packit 577717
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include <sys/types.h>
Packit 577717
#include <sys/stat.h>
Packit 577717
#include <fcntl.h>
Packit 577717
Packit 577717
#include "papi.h" /* This needs to be included every time you use PAPI */
Packit 577717
Packit 577717
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
Packit 577717
Packit 577717
int poorly_tuned_function()
Packit 577717
{
Packit 577717
   float tmp;
Packit 577717
   int i;
Packit 577717
Packit 577717
   for(i=1; i<2000; i++)
Packit 577717
   {
Packit 577717
      tmp=(tmp+100)/i;
Packit 577717
   }
Packit 577717
   return 0;
Packit 577717
}
Packit 577717
Packit 577717
int main()
Packit 577717
{
Packit 577717
Packit 577717
   int num, retval, EventSet = PAPI_NULL;
Packit 577717
   long long values[2];
Packit 577717
   PAPI_option_t options;    
Packit 577717
   int fd;
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
   /* Set the domain of this EventSet to counter user mode. The domain
Packit 577717
      will be valid for all the eventset created after this function call 
Packit 577717
      unless you call PAPI_set_domain again */ 
Packit 577717
   if ((retval=PAPI_set_domain(PAPI_DOM_USER)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Instructions Executed event to the EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Cycles Executed event to the EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Start counting */
Packit 577717
   if((retval=PAPI_start(EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   poorly_tuned_function();
Packit 577717
   /* add some system calls */
Packit 577717
   fd = open("/dev/zero", O_RDONLY);
Packit 577717
   if (fd == -1)
Packit 577717
   {
Packit 577717
         perror("open(/dev/zero)");
Packit 577717
         exit(1);
Packit 577717
   }
Packit 577717
   close(fd);
Packit 577717
Packit 577717
Packit 577717
   /* Stop counting */
Packit 577717
   if((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
        
Packit 577717
   printf(" Total instructions: %lld   Total Cycles: %lld \n", values[0],
Packit 577717
            values[1]);
Packit 577717
Packit 577717
   /* Set the domain of this EventSet to counter user and kernel modes */ 
Packit 577717
   if ((retval=PAPI_set_domain(PAPI_DOM_ALL)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   EventSet = PAPI_NULL;
Packit 577717
   if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Instructions Executed to our EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Instructions Executed to our EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
   /* Start counting */
Packit 577717
   if((retval=PAPI_start(EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   poorly_tuned_function();
Packit 577717
   /* add some system calls */
Packit 577717
   fd = open("/dev/zero", O_RDONLY);
Packit 577717
   if (fd == -1)
Packit 577717
   {
Packit 577717
         perror("open(/dev/zero)");
Packit 577717
         exit(1);
Packit 577717
   }
Packit 577717
   close(fd);
Packit 577717
Packit 577717
   /* Stop counting */
Packit 577717
   if((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
        
Packit 577717
   printf(" Total instructions: %lld   Total Cycles: %lld \n", values[0],
Packit 577717
            values[1]);
Packit 577717
Packit 577717
   /* clean up */
Packit 577717
   PAPI_shutdown();
Packit 577717
Packit 577717
   exit(0);
Packit 577717
}