Blame src/examples/sprofile.c

Packit 577717
/* This program shows how to use PAPI_sprofil */
Packit 577717
Packit 577717
#include <stdlib.h>
Packit 577717
#include <stdio.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <sys/types.h>
Packit 577717
#include <sys/stat.h>
Packit 577717
#include <fcntl.h>
Packit 577717
#include <string.h>
Packit 577717
Packit 577717
#include "papi.h" /* This needs to be included every time you use PAPI */
Packit 577717
Packit 577717
#define NUM_FLOPS  20000000
Packit 577717
#define NUM_ITERS 100000
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
#if (defined(linux) && defined(__ia64__)) || (defined(_AIX))
Packit 577717
#define DO_FLOPS1 (caddr_t)(*(void **)do_flops1)
Packit 577717
#define DO_FLOPS2 (caddr_t)(*(void **)do_flops2)
Packit 577717
#else
Packit 577717
#define DO_FLOPS1 (caddr_t)(do_flops1)
Packit 577717
#define DO_FLOPS2 (caddr_t)(do_flops2)
Packit 577717
#endif
Packit 577717
Packit 577717
void do_flops2(int);
Packit 577717
volatile double t1 = 0.8, t2 = 0.9;
Packit 577717
void do_flops1(int n)
Packit 577717
{
Packit 577717
   int i;
Packit 577717
   double c = 22222.11;
Packit 577717
Packit 577717
   for (i = 0; i < n; i++)
Packit 577717
      c -= t1 * t2;
Packit 577717
}
Packit 577717
Packit 577717
void do_both(int n)
Packit 577717
{
Packit 577717
   int i;
Packit 577717
   const int flops2 = NUM_FLOPS / n;
Packit 577717
   const int flops1 = NUM_FLOPS / n;
Packit 577717
Packit 577717
   for (i = 0; i < n; i++) 
Packit 577717
   {
Packit 577717
      do_flops1(flops1);
Packit 577717
      do_flops2(flops2);
Packit 577717
   }
Packit 577717
}
Packit 577717
Packit 577717
int main(int argc, char **argv)
Packit 577717
{
Packit 577717
   int i , PAPI_event;
Packit 577717
   int EventSet = PAPI_NULL;
Packit 577717
   unsigned short *profbuf;
Packit 577717
   unsigned short *profbuf2;
Packit 577717
   unsigned short *profbuf3;
Packit 577717
   unsigned long length;
Packit 577717
   caddr_t start, end;
Packit 577717
   long long values[2];
Packit 577717
   const PAPI_exe_info_t *prginfo = NULL;
Packit 577717
   PAPI_sprofil_t sprof[3];
Packit 577717
   int retval;
Packit 577717
Packit 577717
   /* initializaion */
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
   if ((prginfo = PAPI_get_executable_info()) == NULL) 
Packit 577717
      ERROR_RETURN(1);
Packit 577717
Packit 577717
   start = prginfo->address_info.text_start;
Packit 577717
   end = prginfo->address_info.text_end;
Packit 577717
   length = (end - start)/sizeof(unsigned short) * sizeof(unsigned short);
Packit 577717
   printf("start= %p  end =%p \n", start, end);
Packit 577717
Packit 577717
   profbuf = (unsigned short *) malloc(length);
Packit 577717
   if (profbuf == NULL) 
Packit 577717
      ERROR_RETURN(PAPI_ESYS);
Packit 577717
Packit 577717
   memset(profbuf, 0x00, length );
Packit 577717
Packit 577717
   profbuf2 = (unsigned short *) malloc(length);
Packit 577717
   if (profbuf2 == NULL) 
Packit 577717
      ERROR_RETURN(PAPI_ESYS);
Packit 577717
Packit 577717
   memset(profbuf2, 0x00, length );
Packit 577717
Packit 577717
   profbuf3 = (unsigned short *) malloc(1 * sizeof(unsigned short));
Packit 577717
   if (profbuf3 == NULL) 
Packit 577717
      ERROR_RETURN(PAPI_ESYS);
Packit 577717
Packit 577717
   memset(profbuf3, 0x00, 1 * sizeof(unsigned short));
Packit 577717
Packit 577717
   /* First half */
Packit 577717
   sprof[0].pr_base = profbuf;
Packit 577717
   sprof[0].pr_size = length / 2;
Packit 577717
   sprof[0].pr_off = DO_FLOPS2;
Packit 577717
   fprintf(stderr, "do_flops is at %p %lx\n", &do_flops2, strtoul(sprof[0].pr_off,NULL,0));
Packit 577717
Packit 577717
   sprof[0].pr_scale = 65536;  /* constant needed by PAPI_sprofil */
Packit 577717
   /* Second half */
Packit 577717
   sprof[1].pr_base = profbuf2;
Packit 577717
   sprof[1].pr_size = length / 2;
Packit 577717
   sprof[1].pr_off = DO_FLOPS1;
Packit 577717
   fprintf(stderr, "do_flops1 is at %p %lx\n", &do_flops1, strtoul(sprof[1].pr_off,NULL,0));
Packit 577717
   sprof[1].pr_scale = 65536; /* constant needed by PAPI_sprofil */
Packit 577717
Packit 577717
   /* Overflow bin */
Packit 577717
   sprof[2].pr_base = profbuf3;
Packit 577717
   sprof[2].pr_size = 1;
Packit 577717
   sprof[2].pr_off = 0;
Packit 577717
   sprof[2].pr_scale = 0x2;  /* constant needed by PAPI_sprofil */
Packit 577717
Packit 577717
   /* Creating 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_CYC;
Packit 577717
   /* Add Total Instructions Executed to our EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_event)) != 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
   /* set profile flag */
Packit 577717
   if ((retval = PAPI_sprofil(sprof, 3, EventSet, PAPI_event, THRESHOLD,
Packit 577717
                              PAPI_PROFIL_POSIX)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   if ((retval = PAPI_start(EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   do_both(NUM_ITERS);
Packit 577717
Packit 577717
   if ((retval = PAPI_stop(EventSet, values)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* to clear the profile flag before removing the events */
Packit 577717
   if ((retval = PAPI_sprofil(sprof, 3, EventSet, PAPI_event, 0,
Packit 577717
                              PAPI_PROFIL_POSIX)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* free the resources hold by PAPI */
Packit 577717
   PAPI_shutdown();
Packit 577717
Packit 577717
   printf("Test case: PAPI_sprofil()\n");
Packit 577717
   printf("---------Buffer 1--------\n");
Packit 577717
   for (i = 0; i < length / 2; i++) 
Packit 577717
   {
Packit 577717
      if (profbuf[i])
Packit 577717
	printf("%#lx\t%d\n", strtoul(DO_FLOPS2,NULL,0) + 2 * i, profbuf[i]);
Packit 577717
   }
Packit 577717
   printf("---------Buffer 2--------\n");
Packit 577717
   for (i = 0; i < length / 2; i++) 
Packit 577717
   {
Packit 577717
      if (profbuf2[i])
Packit 577717
	printf("%#lx\t%d\n", strtoul(DO_FLOPS1,NULL,0) + 2 * i, profbuf2[i]);
Packit 577717
   }
Packit 577717
   printf("-------------------------\n");
Packit 577717
   printf("%u samples fell outside the regions.\n", *profbuf3);
Packit 577717
Packit 577717
   exit(0);
Packit 577717
}
Packit 577717
Packit 577717
/* Declare a and b to be volatile.
Packit 577717
   This is to try to keep the
Packit 577717
   compiler from optimizing the loop */
Packit 577717
volatile double a = 0.5, b = 2.2;
Packit 577717
void do_flops2(int n)
Packit 577717
{
Packit 577717
   int i;
Packit 577717
   double c = 0.11;
Packit 577717
Packit 577717
   for (i = 0; i < n; i++) 
Packit 577717
      c += a * b;
Packit 577717
}
Packit 577717