Blame src/examples/sprofile.c

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