Exemplo n.º 1
0
void *
Thread( void *arg )
{
	int retval, num_tests = 1, i;
	int EventSet1 = PAPI_NULL, mask1, PAPI_event;
	int num_events1;
	long long **values;
	long long elapsed_us, elapsed_cyc;
	unsigned short *profbuf;
	char event_name[PAPI_MAX_STR_LEN];

	retval = PAPI_register_thread(  );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
	profbuf = ( unsigned short * ) malloc( length * sizeof ( unsigned short ) );
	if ( profbuf == NULL )
		exit( 1 );
	memset( profbuf, 0x00, length * sizeof ( unsigned short ) );

	/* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
	   PAPI_TOT_INS, depends on the availability of the event on the
	   platform */
	EventSet1 =
		add_two_nonderived_events( &num_events1, &PAPI_event, &mask1 );

	values = allocate_test_space( num_tests, num_events1 );

	if ( ( retval =
		   PAPI_event_code_to_name( PAPI_event, event_name ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );

	elapsed_us = PAPI_get_real_usec(  );

	elapsed_cyc = PAPI_get_real_cyc(  );

	retval = PAPI_profil( profbuf, length, my_start, 65536,
						  EventSet1, PAPI_event, THR, PAPI_PROFIL_POSIX );
	if ( retval )
		test_fail( __FILE__, __LINE__, "PAPI_profil", retval );

	if ( ( retval = PAPI_start( EventSet1 ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );

	do_flops( *( int * ) arg );

	if ( ( retval = PAPI_stop( EventSet1, values[0] ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );

	elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;

	elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;

	/* to remove the profile flag */
	retval = PAPI_profil( profbuf, length, my_start, 65536,
						  EventSet1, PAPI_event, 0, PAPI_PROFIL_POSIX );
	if ( retval )
		test_fail( __FILE__, __LINE__, "PAPI_profil", retval );


	remove_test_events( &EventSet1, mask1 );

	if ( !TESTS_QUIET ) {
		if ( mask1 == 0x3 ) {
			printf( "Thread 0x%x PAPI_TOT_INS : \t%lld\n",
					( int ) pthread_self(  ), ( values[0] )[0] );
		} else {
			printf( "Thread 0x%x PAPI_FP_INS : \t%lld\n",
					( int ) pthread_self(  ), ( values[0] )[0] );
		}
		printf( "Thread 0x%x PAPI_TOT_CYC: \t%lld\n", ( int ) pthread_self(  ),
				( values[0] )[1] );
		printf( "Thread 0x%x Real usec   : \t%lld\n", ( int ) pthread_self(  ),
				elapsed_us );
		printf( "Thread 0x%x Real cycles : \t%lld\n", ( int ) pthread_self(  ),
				elapsed_cyc );

		printf( "Test case: PAPI_profil() for pthreads\n" );
		printf( "----Profile buffer for Thread 0x%x---\n",
				( int ) pthread_self(  ) );
		for ( i = 0; i < ( int ) length; i++ ) {
			if ( profbuf[i] )
				printf( "0x%lx\t%d\n", ( unsigned long ) ( my_start + 2 * i ),
						profbuf[i] );
		}
	}
	for ( i = 0; i < ( int ) length; i++ )
		if ( profbuf[i] )
			break;

	if ( i >= ( int ) length )
		test_fail( __FILE__, __LINE__, "No information in buffers", 1 );
	free_test_space( values, num_tests );

	retval = PAPI_unregister_thread(  );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
	return ( NULL );
}
Exemplo n.º 2
0
static int
do_profile( caddr_t start, unsigned long plength, unsigned scale, int thresh,
	    int bucket, unsigned int mask ) {

	int i, retval;
	unsigned long blength;
	int num_buckets,j=0;

	int num_bufs = num_events;
	int event = num_events;

	int events[MAX_TEST_EVENTS];
	char header[BUFSIZ];

	strncpy(header,"address\t\t",BUFSIZ);

	//= "address\t\t\tcyc\tins\tfp_ins\n";

	for(i=0;i<MAX_TEST_EVENTS;i++) {
	  if (mask & test_events[i].mask) {
	    events[j]=test_events[i].event;

	    if (events[j]==PAPI_TOT_CYC) {
	       strncat(header,"\tcyc",BUFSIZ-1);
	    }
	    if (events[j]==PAPI_TOT_INS) {
	       strncat(header,"\tins",BUFSIZ-1);
	    }
	    if (events[j]==PAPI_FP_INS) {
	       strncat(header,"\tfp_ins",BUFSIZ-1);
	    }
	    if (events[j]==PAPI_FP_OPS) {
	       strncat(header,"\tfp_ops",BUFSIZ-1);
	    }
	    if (events[j]==PAPI_L2_TCM) {
	       strncat(header,"\tl2_tcm",BUFSIZ-1);
	    }

	    j++;

	  }
	}

	strncat(header,"\n",BUFSIZ-1);



	blength = prof_size( plength, scale, bucket, &num_buckets );
	prof_alloc( num_bufs, blength );

	if ( !TESTS_QUIET )
		printf( "Overall event counts:\n" );

	for ( i = 0; i < num_events; i++ ) {
		if ( ( retval =
			   PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
							EventSet, events[i], thresh,
							PAPI_PROFIL_POSIX | bucket ) ) != PAPI_OK ) {
	           if (retval == PAPI_EINVAL) {
		      test_warn( __FILE__, __LINE__, "Trying to profile with derived event", 1);
		      num_events=i;
		      break;
		   }
                   else {
		        printf("Failed with event %d 0x%x\n",i,events[i]);
			test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
		   }
		}
	}

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );

	my_main(  );

	if ( ( retval = PAPI_stop( EventSet, values[0] ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );

	if ( !TESTS_QUIET ) {
		printf( TAB1, "PAPI_TOT_CYC:", ( values[0] )[--event] );
		if ( strcmp( hw_info->model_string, "POWER6" ) != 0 ) {
			printf( TAB1, "PAPI_TOT_INS:", ( values[0] )[--event] );
		}
#if defined(__powerpc__)
		printf( TAB1, "PAPI_FP_INS", ( values[0] )[--event] );
#else
		if ( strcmp( hw_info->model_string, "Intel Pentium III" ) != 0 ) {
			printf( TAB1, "PAPI_FP_OPS:", ( values[0] )[--event] );
			printf( TAB1, "PAPI_L2_TCM:", ( values[0] )[--event] );
		}
#endif
	}

	for ( i = 0; i < num_events; i++ ) {
		if ( ( retval =
			   PAPI_profil( profbuf[i], ( unsigned int ) blength, start, scale,
							EventSet, events[i], 0,
							PAPI_PROFIL_POSIX ) ) != PAPI_OK )
			test_fail( __FILE__, __LINE__, "PAPI_profil", retval );
	}

	prof_head( blength, bucket, num_buckets, header );
	prof_out( start, num_events, bucket, num_buckets, scale );
	retval = prof_check( num_bufs, bucket, num_buckets );
	for ( i = 0; i < num_bufs; i++ ) {
		free( profbuf[i] );
	}
	return retval;
}
int main()
{

    unsigned long length;
    caddr_t start, end;
    PAPI_sprofil_t * prof; 
    int EventSet = PAPI_NULL;
    /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
    int PAPI_event,i,tmp = 0;
    char event_name[PAPI_MAX_STR_LEN];
    /*These are going to be used as buffers */
    unsigned short *profbuf;
    long long values[2];
    const PAPI_exe_info_t *prginfo = NULL;


    int retval;
    

   /****************************************************************************
   *  This part initializes the library and compares the version number of the *
   * header file, to the version of the library, if these don't match then it  *
   * is likely that PAPI won't work correctly.If there is an error, retval     *
   * keeps track of the version number.                                        *
   ****************************************************************************/
    
   if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
   {
      printf("Library initialization error! \n");
      exit(1);
   }
    

   if ((prginfo = PAPI_get_executable_info()) == NULL)
   {  
      fprintf(stderr, "Error in get executable information \n");
      exit(1);
   }

   start = prginfo->address_info.text_start;
   end = prginfo->address_info.text_end;
   length = (end - start);

   /* for PAPI_PROFIL_BUCKET_16 and scale = 65536, 
      profile buffer length == program address length.
      Larger bucket sizes would increase the buffer length.
      Smaller scale factors would decrease it.
      Handle with care...
   */
   profbuf = (unsigned short *)malloc(length);
   if (profbuf == NULL)
   {
      fprintf(stderr, "Not enough memory \n");
      exit(1);
   }
   memset(profbuf,0x00,length);

   /* Creating the eventset */
   if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
      ERROR_RETURN(retval);

   PAPI_event = PAPI_TOT_INS;
   /* Add Total Instructions Executed to our EventSet */
   if ( (retval = PAPI_add_event(EventSet, PAPI_event)) != PAPI_OK)
      ERROR_RETURN(retval);

   /* Add Total Cycles Executed to our EventSet */
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
      ERROR_RETURN(retval);

   /* enable the collection of profiling information */
   if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet,
            PAPI_event, THRESHOLD, PAPI_PROFIL_POSIX | PAPI_PROFIL_BUCKET_16)) != PAPI_OK)
      ERROR_RETURN(retval);
   
   /* let's rock and roll */
   if ((retval=PAPI_start(EventSet)) != PAPI_OK)
      ERROR_RETURN(retval);

   code_to_monitor();
  
   if ((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
      ERROR_RETURN(retval);

   /* disable the collection of profiling information by setting threshold
      to 0
   */
   if ((retval = PAPI_profil(profbuf, length, start, 65536, EventSet,
            PAPI_event, 0, PAPI_PROFIL_POSIX)) != PAPI_OK)
      ERROR_RETURN(retval);
   
   printf("-----------------------------------------------------------\n");
   printf("Text start: %p, Text end: %p, \n",   
            prginfo->address_info.text_start,prginfo->address_info.text_end);
   printf("Data start: %p, Data end: %p\n",
            prginfo->address_info.data_start,prginfo->address_info.data_end);
   printf("BSS start : %p, BSS end: %p\n",
            prginfo->address_info.bss_start,prginfo->address_info.bss_end);
    
   printf("------------------------------------------\n");
        
   printf("Test type   : \tPAPI_PROFIL_POSIX\n");
   printf("------------------------------------------\n\n\n");  
   printf("PAPI_profil() hash table.\n");
   printf("address\t\tflat   \n");
   for (i = 0; i < (int) length/2; i++) 
   {
      if (profbuf[i]) 
         printf("0x%lx\t%d \n",
               (unsigned long) start + (unsigned long) (2 * i), profbuf[i]);
   }

   printf("-----------------------------------------\n");

   retval = 0;
   for (i = 0; i < (int) length/2; i++)
      retval = retval || (profbuf[i]); 
   if (retval)
      printf("Test succeeds! \n");
   else
      printf( "No information in buffers\n");
   /* clean up */
   PAPI_shutdown();

   exit(0);
}
Exemplo n.º 4
0
static int do_profile(caddr_t start, unsigned long plength, unsigned scale, int thresh, int bucket) {
   int i, retval;
   unsigned long blength;
   int num_buckets;

   char *profstr[5] = {"PAPI_PROFIL_POSIX",
                        "PAPI_PROFIL_RANDOM",
                        "PAPI_PROFIL_WEIGHTED",
                        "PAPI_PROFIL_COMPRESS",
                        "PAPI_PROFIL_<all>" };

   int profflags[5] = {PAPI_PROFIL_POSIX,
                       PAPI_PROFIL_POSIX | PAPI_PROFIL_RANDOM,
                       PAPI_PROFIL_POSIX | PAPI_PROFIL_WEIGHTED,
                       PAPI_PROFIL_POSIX | PAPI_PROFIL_COMPRESS,
                       PAPI_PROFIL_POSIX | PAPI_PROFIL_WEIGHTED |
                       PAPI_PROFIL_RANDOM | PAPI_PROFIL_COMPRESS };

   do_no_profile();
   blength = prof_size(plength, scale, bucket, &num_buckets);
   prof_alloc(5, blength);

   for (i=0;i<5;i++) {
      if (!TESTS_QUIET)
         printf("Test type   : \t%s\n", profstr[i]);

#ifndef SWPROFILE
      if ((retval = PAPI_profil(profbuf[i], blength, start, scale,
                              EventSet, PAPI_event, thresh,
                              profflags[i] | bucket)) != PAPI_OK) {
         test_fail(__FILE__, __LINE__, "PAPI_profil", retval);
      }
#else
      if ((retval = PAPI_profil(profbuf[i], blength, start, scale,
                              EventSet, PAPI_event, thresh,
                              profflags[i] | bucket | PAPI_PROFIL_FORCE_SW)) != PAPI_OK) {
         test_fail(__FILE__, __LINE__, "PAPI_profil", retval);
      }
#endif
      if ((retval = PAPI_start(EventSet)) != PAPI_OK)
         test_fail(__FILE__, __LINE__, "PAPI_start", retval);

      do_flops(getenv("NUM_FLOPS") ? atoi(getenv("NUM_FLOPS")) : NUM_FLOPS);

      if ((retval = PAPI_stop(EventSet, values[1])) != PAPI_OK)
         test_fail(__FILE__, __LINE__, "PAPI_stop", retval);

      if (!TESTS_QUIET) {
         printf(TAB1, event_name, (values[1])[0]);
         printf(TAB1, "PAPI_TOT_CYC", (values[1])[1]);
      }
      if ((retval = PAPI_profil(profbuf[i], blength, start, scale,
                              EventSet, PAPI_event, 0, profflags[i])) != PAPI_OK)
         test_fail(__FILE__, __LINE__, "PAPI_profil", retval);
   }

   prof_head(blength, bucket, num_buckets, 
      "address\t\t\tflat\trandom\tweight\tcomprs\tall\n");
   prof_out(start, 5, bucket, num_buckets, scale);

   retval = prof_check(5, bucket, num_buckets);

   for (i=0;i<5;i++) {
      free(profbuf[i]);
   }

   return(retval);
}