Пример #1
0
void compute() {
#if PAPI
    int EventCode, retval;
    int EventSet = PAPI_NULL;
    long long PAPI_Counters[MAX_COUNTERS];

    /* Initialize the library */
    retval = PAPI_library_init(PAPI_VER_CURRENT);

    if  (retval != PAPI_VER_CURRENT) {
        fprintf(stderr, "PAPI library init error!\n");
        exit(1); 
    }

    /* PAPI create event */
    if (PAPI_create_eventset(&EventSet) != PAPI_OK) {
        fprintf(stderr, "create event set: %s", PAPI_strerror(retval));
    }

    for (int i = 0; i < NUMCOUNTERS; i++) {
        if ( (retval = PAPI_add_named_event(EventSet, events[i])) != PAPI_OK) {
            fprintf(stderr, "add named event: %s", PAPI_strerror(retval));
        }
    }

    retval = PAPI_start(EventSet);
#endif
    
    float matice1[rozmer][rozmer];
    float matice2[rozmer][rozmer];
    float matice3[rozmer][rozmer];
    //Main multiply code
    int j,k,m;
    for(j = 0; j < rozmer; j++)
    {
        for (k = 0; k < rozmer; k++)
        {
            float temp = 0;
            for (m = 0; m < rozmer; m++)
            {
                temp = temp + matice1[j][m] * matice2[m][k];
            }
            matice3[j][k] = temp;
        }
    }

#if PAPI
    retval = PAPI_stop(EventSet, PAPI_Counters);
    if (retval != PAPI_OK) exit(1);

    for (int j = 0; j < NUMCOUNTERS; j++) {
        printf("%20lld %s\n", PAPI_Counters[j], events[j]);
    //    fprintf(stderr, "%lld\n ", PAPI_Counters[j]);
    }
#endif
}
Пример #2
0
void compute(int *sequence) {
#if PAPI
    int EventCode, retval;
    int EventSet = PAPI_NULL;
    long long PAPI_Counters[MAX_COUNTERS];

    /* Initialize the library */
    retval = PAPI_library_init(PAPI_VER_CURRENT);

    if  (retval != PAPI_VER_CURRENT) {
        fprintf(stderr, "PAPI library init error!\n");
        exit(1); 
    }

    /* PAPI create event */
    if (PAPI_create_eventset(&EventSet) != PAPI_OK) {
        fprintf(stderr, "create event set: %s", PAPI_strerror(retval));
    }

    int i;
    for (i = 0; i < NUMCOUNTERS; i++) {
        if ( (retval = PAPI_add_named_event(EventSet, events[i])) != PAPI_OK) {
            fprintf(stderr, "add named event: %s", PAPI_strerror(retval));
        }
    }

    retval = PAPI_start(EventSet);
#endif
    
    float matice1[SEQUENCE_CNT];
    float matice2[SEQUENCE_CNT];
    int j, idx;
    for(j = 0; j < SEQUENCE_CNT; j++)
    {
        idx = sequence[j];
        matice2[idx] = 13 % (idx + 1);
        matice1[idx] = matice2[idx] * 231;
      //  printf("%d\t", idx);
    }

    //printf("\n");
#if PAPI
    retval = PAPI_stop(EventSet, PAPI_Counters);

    if (retval != PAPI_OK) exit(1);
    int k;
    for (k = 0; k < NUMCOUNTERS; k++) {
        printf("%20lld %s\n", PAPI_Counters[k], events[k]);
    //    fprintf(stderr, "%lld\n ", PAPI_Counters[k]);
    }
#endif
}
Пример #3
0
int
main( int argc, char **argv )
{
	int EventSet = PAPI_NULL;
	long long values0[2],values1[2],values2[2];
	int num_flops = 3000000, retval;
	int mythreshold = 1000000;
	char event_name1[PAPI_MAX_STR_LEN];
        int PAPI_event;
	int cid,numcmp,rapl_cid;
	const PAPI_component_info_t *cmpinfo = NULL;

	/* Set TESTS_QUIET variable */
	tests_quiet( argc, argv );      

	quiet=TESTS_QUIET;

	/* Init PAPI */
	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT ) {
	  test_fail(__FILE__, __LINE__,"PAPI_library_init",retval);
	}

	numcmp = PAPI_num_components();

	for(cid=0; cid<numcmp; cid++) {

	  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
	    test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
	  }

	  if (strstr(cmpinfo->name,"linux-rapl")) {
	    rapl_cid=cid;
	    if (!TESTS_QUIET) printf("Found rapl component at cid %d\n",
				     rapl_cid);
	    if (cmpinfo->num_native_events==0) {
              test_skip(__FILE__,__LINE__,"No rapl events found",0);
	    }
	    break;
	  }
	}

	/* Component not found */
	if (cid==numcmp) {
	  test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
	}


	/* add PAPI_TOT_CYC and PAPI_TOT_INS */
	retval=PAPI_create_eventset(&EventSet);
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
	}

	retval=PAPI_add_event(EventSet,PAPI_TOT_CYC);
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
	}

	retval=PAPI_add_event(EventSet,PAPI_TOT_INS);
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
	}

	/* Add some RAPL events */
	retval=PAPI_create_eventset(&EventSet2);
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
	}

        retval=PAPI_add_named_event(EventSet2,"PACKAGE_ENERGY:PACKAGE0");
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
	}

        retval=PAPI_add_named_event(EventSet2,"PACKAGE_ENERGY:PACKAGE1");
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
	}
	
	PAPI_event=PAPI_TOT_CYC;

	/* arbitrary */
	mythreshold = 2000000;
	if (!TESTS_QUIET) {
	   printf("Using %x for the overflow event, threshold %d\n",
		  PAPI_event,mythreshold);
	}

	/* Start the run calibration run */
	retval = PAPI_start( EventSet );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_start",retval);
	}

	do_ints(num_flops,TESTS_QUIET);
	do_flops( 3000000 );

	/* stop the calibration run */
	retval = PAPI_stop( EventSet, values0 );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
	}


	/* set up overflow handler */
	retval = PAPI_overflow( EventSet,PAPI_event,mythreshold, 0, handler );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
	}

	/* Start overflow run */
	retval = PAPI_start( EventSet );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_start",retval);
	}
	retval = PAPI_start( EventSet2 );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_start",retval);
	}

	do_ints(num_flops,TESTS_QUIET);
	do_flops( num_flops );

	/* stop overflow run */
	retval = PAPI_stop( EventSet, values1 );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
	}

	retval = PAPI_stop( EventSet2, values2 );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
	}

	retval = PAPI_overflow( EventSet, PAPI_event, 0, 0, handler );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
	}

	retval = PAPI_event_code_to_name( PAPI_event, event_name1 );
	if (retval != PAPI_OK) {
	   test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n", retval);
	}

	if (!TESTS_QUIET) {
	   printf("%s: %lld %lld\n",event_name1,values0[0],values1[0]);
	}

	retval = PAPI_event_code_to_name( PAPI_TOT_INS, event_name1 );
	if (retval != PAPI_OK) {
	  test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n",retval);
	}

	if (!TESTS_QUIET) {
	   printf("%s: %lld %lld\n",event_name1,values0[1],values1[1]);
	}

	retval = PAPI_cleanup_eventset( EventSet );
	if ( retval != PAPI_OK ) {
	  test_fail(__FILE__, __LINE__,"PAPI_cleanup_eventset",retval);
	}

	retval = PAPI_destroy_eventset( &EventSet );
	if ( retval != PAPI_OK ) {
	   test_fail(__FILE__, __LINE__,"PAPI_destroy_eventset",retval);
	}

	if (rapl_backward) {
	   test_fail(__FILE__, __LINE__,"RAPL counts went backward!",0);
	}

	test_pass( __FILE__, NULL, 0 );


	return 0;
}
Пример #4
0
int
main( int argc, char **argv )
{
	int retval;
	int num_events;
	long long *values;
	char *success;
	PAPI_event_info_t info;
	int EventSet = PAPI_NULL;
	int i, j, data_type, event;
	int u_format = 0;
	int hex_format = 0;

	tests_quiet( argc, argv );	/* Set TESTS_QUIET variable */

	if ( ( retval =
		   PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT )
		test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );

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

	/* Automatically pass if no events, for run_tests.sh */
	if ((( TESTS_QUIET ) && ( argc == 2)) || ( argc == 1 )) {
		test_pass( __FILE__, NULL, 0 );
	}

	values =
		( long long * ) malloc( sizeof ( long long ) * ( size_t ) argc );
	success = ( char * ) malloc( ( size_t ) argc );

	if ( success == NULL || values == NULL )
		test_fail_exit( __FILE__, __LINE__, "malloc", PAPI_ESYS );

	for ( num_events = 0, i = 1; i < argc; i++ ) {
		if ( !strcmp( argv[i], "-h" ) ) {
			print_help( argv );
			exit( 1 );
		} else if ( !strcmp( argv[i], "-u" ) ) {
			u_format = 1;
		} else if ( !strcmp( argv[i], "-x" ) ) {
			hex_format = 1;
		} else {
			if ( ( retval = PAPI_add_named_event( EventSet, argv[i] ) ) != PAPI_OK ) {
				printf( "Failed adding: %s\nbecause: %s\n", argv[i], 
					PAPI_strerror(retval));
			} else {
				success[num_events++] = i;
				printf( "Successfully added: %s\n", argv[i] );
			}
		}
	}

	/* Automatically pass if no events, for run_tests.sh */
	if ( num_events == 0 ) {
		test_pass( __FILE__, NULL, 0 );
	}


	printf( "\n" );

	do_flops( 1 );
	do_flush(  );

	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
	   test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
	}

	do_flops( NUM_FLOPS );
	do_misses( 1, L1_MISS_BUFFER_SIZE_INTS );

	if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );

	for ( j = 0; j < num_events; j++ ) {
		i = success[j];
		if (! (u_format || hex_format) ) {
			retval = PAPI_event_name_to_code( argv[i], &event );
			if (retval == PAPI_OK) {
				retval = PAPI_get_event_info(event, &info);
				if (retval == PAPI_OK) data_type = info.data_type;
				else data_type = PAPI_DATATYPE_INT64;
			}
			switch (data_type) {
			  case PAPI_DATATYPE_UINT64:
				printf( "%s : \t%llu(u)", argv[i], (unsigned long long)values[j] );
				break;
			  case PAPI_DATATYPE_FP64:
				printf( "%s : \t%0.3f", argv[i], *((double *)(&values[j])) );
				j++;
				break;
			  case PAPI_DATATYPE_BIT64:
				printf( "%s : \t0x%llX", argv[i], values[j] );
				break;
			  case PAPI_DATATYPE_INT64:
			  default:
				printf( "%s : \t%lld", argv[i], values[j] );
				break;
			}
			if (retval == PAPI_OK)  printf( " %s", info.units );
			printf( "\n" );
		}
		if (u_format) printf( "%s : \t%llu(u)\n", argv[i], (unsigned long long)values[j] );
		if (hex_format) printf( "%s : \t0x%llX\n", argv[i], values[j] );
	}

	printf( "\n----------------------------------\n" );
	printf
		( "Verification: Checks for valid event name.\n This utility lets you add events from the command line interface to see if they work.\n" );
	test_pass( __FILE__, NULL, 0 );
	exit( 1 );
}
Пример #5
0
int main (int argc, char **argv)
{
    int retval,cid,rapl_cid=-1,numcmp;
    int EventSet = PAPI_NULL;
    long long values[MAX_EVENTS];
    int i,code,enum_retval;
    const PAPI_component_info_t *cmpinfo = NULL;
    long long start_time,write_start_time,write_end_time,read_start_time,read_end_time;
    char event_name[BUFSIZ];
    union { long long ll; double dbl; } event_value_union;
    static int num_events=0;
    FILE *fileout;
    
    /* PAPI Initialization */
    retval = PAPI_library_init( PAPI_VER_CURRENT );
    if ( retval != PAPI_VER_CURRENT ) {
        fprintf(stderr,"PAPI_library_init failed\n");
	exit(1);
    }
    
    /* Find the libmsr component */
    numcmp = PAPI_num_components();
    for(cid=0; cid<numcmp; cid++) {
	if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
            fprintf(stderr,"PAPI_get_component_info failed\n");
            exit(1);
	}
	if (strstr(cmpinfo->name,"libmsr")) {
            rapl_cid=cid;
            printf("Found libmsr component at cid %d\n", rapl_cid);
            if (cmpinfo->disabled) {
                fprintf(stderr,"No libmsr events found: %s\n", cmpinfo->disabled_reason);
                exit(1);
            }
            break;
	}
    }

    /* Component not found */    
    if (cid==numcmp) {
        fprintf(stderr,"No libmsr component found\n");
        exit(1);
    }
    
    /* Find events in the component */
    code = PAPI_NATIVE_MASK;
    enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
    while ( enum_retval == PAPI_OK ) {
        retval = PAPI_event_code_to_name( code, event_name );
        if ( retval != PAPI_OK ) {
            printf("Error translating %#x\n",code);
            exit(1);
        }
        printf("Found: %s\n",event_name);
        strncpy(events[num_events],event_name,BUFSIZ);
        sprintf(filenames[num_events],"results.%s",event_name);
        num_events++;
        if (num_events==MAX_EVENTS) {
            printf("Too many events! %d\n",num_events);
            exit(1);
        }
        enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
    }
    if (num_events==0) {
        printf("Error!  No libmsr events found!\n");
	exit(1);
    }
    
    /* Open output file */
    char fileoutname[]="libmsr_write_test_output.txt";
    fileout=fopen( fileoutname ,"w" );
    if ( fileout==NULL) { fprintf( stderr,"Could not open %s\n",fileoutname ); exit(1); }

    /* Create EventSet */
    retval = PAPI_create_eventset( &EventSet );
    if (retval != PAPI_OK) {
        fprintf(stderr,"Error creating eventset!\n");
    }
    
    for(i=0;i<num_events;i++) {
        retval = PAPI_add_named_event( EventSet, events[i] );
        if (retval != PAPI_OK) fprintf(stderr,"Error adding event %s\n",events[i]); 
    }
    
    start_time=PAPI_get_real_nsec();
    
    /* Grab the initial values for the events */
    retval = PAPI_start( EventSet);
    if (retval != PAPI_OK) { fprintf(stderr,"PAPI_start() failed\n"); exit(1); }
    /* Initial checking read */
    retval = PAPI_read( EventSet, values);
    if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }

    /* Write a header line */
    fprintf( fileout, "ACTION TIME-STAMP TIME-FOR-UNIT-WORK TIME-OVERHEAD-RW\t" );
    for(i=0; i<num_events; i++) 
        fprintf( fileout, "%s ", events[i]+9 );
    fprintf( fileout, "\n" );

    /* Read the initial values */
    retval = PAPI_read( EventSet, values);
    if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
    fprintf( fileout, "INIT %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
    fprintf( fileout, "%8.3e ", 0.0);
    for(i=0; i<num_events; i++) {
        event_value_union.ll = values[i];
        fprintf( fileout, "%8.3f ", event_value_union.dbl );
    }
    fprintf( fileout, "\n" );

    int rpt=0;
    int limit1base=10;
    int limit2base=10;
    while(rpt++<200) {
        //printf("rpt %d\n", rpt);
        
        if ( rpt % 10 == 0 )  {
            for (i=0; i<num_events; i++) {
                event_value_union.ll = values[i];
                if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=limit1base+(rpt/2);
                else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=1.0;
                else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=limit2base+(rpt/2);
                else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=1.0;
                else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=limit1base+(rpt/2);
                else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=1.0;
                else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=limit2base+(rpt/2);
                else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=1.0;
                else event_value_union.dbl=PAPI_NULL;
                values[i]=event_value_union.ll;
            }

            write_start_time=PAPI_get_real_nsec();
            retval = PAPI_write( EventSet, values );
            write_end_time=PAPI_get_real_nsec();
            if (retval != PAPI_OK) { fprintf(stderr,"PAPI_write() failed\n"); exit(1); }

            fprintf( fileout, "SET  %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
            fprintf( fileout, "%8.3e ", ((double)(write_end_time-write_start_time))/1.0e9 );
            for(i=0; i<num_events; i++) {
                event_value_union.ll = values[i];
                fprintf( fileout, "%8.3f ", event_value_union.dbl );
            }
            fprintf( fileout, "\n" );
        }
        
        /* DO SOME WORK TO USE ENERGY */
        //usleep(100000);
        double work_start_time=PAPI_get_real_nsec();
        ompcpuloadprimes( 100000 );
        double work_time=PAPI_get_real_nsec()-work_start_time;
        //printf("primescount %d\n", primescount);
        
        /* Read and output the values */
        read_start_time=PAPI_get_real_nsec();
        retval = PAPI_read( EventSet, values );
        read_end_time=PAPI_get_real_nsec();
        if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
        fprintf( fileout, "READ %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, work_time/1.0e9 );
        fprintf( fileout, "%8.3e ", ((double)(read_end_time-read_start_time))/1.0e9 );
        for(i=0; i<num_events; i++) {
            event_value_union.ll = values[i];
            fprintf( fileout, "%8.3f ", event_value_union.dbl );
        }
        fprintf( fileout, "\n" );
    }

    retval = PAPI_stop( EventSet, values);
    return 0;
}
int main( int argc, char **argv ) {

   int retval;
   int EventSet = PAPI_NULL;
   int EventSet2 = PAPI_NULL;
   long long values[1],values2[1];
   char *uncore_event=NULL;
   char event_name[BUFSIZ];
   int uncore_cidx=-1;
   const PAPI_component_info_t *info;

   /* Set TESTS_QUIET variable */
   tests_quiet( argc, argv );

   /* Init the PAPI library */
   retval = PAPI_library_init( PAPI_VER_CURRENT );
   if ( retval != PAPI_VER_CURRENT ) {
      test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
   }

   /* Find the uncore PMU */
   uncore_cidx=PAPI_get_component_index("perf_event_uncore");
   if (uncore_cidx<0) {
      test_skip(__FILE__,__LINE__,"perf_event_uncore component not found",0);
   }

   /* Check if component disabled */
   info=PAPI_get_component_info(uncore_cidx);
   if (info->disabled) {
      test_skip(__FILE__,__LINE__,"uncore component disabled",0);
   }

   /* Get a relevant event name */
   uncore_event=get_uncore_event(event_name, BUFSIZ);
   if (uncore_event==NULL) {
      test_skip( __FILE__, __LINE__,
	        "PAPI does not support uncore on this processor", PAPI_ENOSUPP );
   }

   /* Create an eventset */
   retval = PAPI_create_eventset(&EventSet);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   /* Create another eventset */
   retval = PAPI_create_eventset(&EventSet2);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   /* Set a component for the EventSet */
   retval = PAPI_assign_eventset_component(EventSet, uncore_cidx);

   /* we need to set to a certain cpu for uncore to work */

   PAPI_cpu_option_t cpu_opt;

   cpu_opt.eventset=EventSet;
   cpu_opt.cpu_num=0;

   retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
   if (retval != PAPI_OK) {
      test_skip( __FILE__, __LINE__,
		      "this test; trying to PAPI_CPU_ATTACH; need to run as root",
		      retval);
   }

   /* we need to set the granularity to system-wide for uncore to work */

   PAPI_granularity_option_t gran_opt;

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet;
   gran_opt.granularity=PAPI_GRN_SYS;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      test_skip( __FILE__, __LINE__,
		      "this test; trying to set PAPI_GRN_SYS",
		      retval);
   }

   /* we need to set domain to be as inclusive as possible */

   PAPI_domain_option_t domain_opt;

   domain_opt.def_cidx=0;
   domain_opt.eventset=EventSet;
   domain_opt.domain=PAPI_DOM_ALL;

   retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
   if (retval != PAPI_OK) {
      test_skip( __FILE__, __LINE__,
		      "this test; trying to set PAPI_DOM_ALL; need to run as root",
		      retval);
   }

   /* Add our uncore event */
   retval = PAPI_add_named_event(EventSet, uncore_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to use event %s\n", uncore_event);
      }
      test_fail(__FILE__, __LINE__, "adding uncore event ",retval);
   }

   /* Add PAPI_TOT_CYC */
   retval = PAPI_add_named_event(EventSet2, "PAPI_TOT_CYC");
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to add PAPI_TOT_CYC\n");
      }
      test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
   }


   /* Start PAPI */
   retval = PAPI_start( EventSet );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   retval = PAPI_start( EventSet2 );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   /* our work code */
   do_flops( NUM_FLOPS );

   /* Stop PAPI */
   retval = PAPI_stop( EventSet, values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }
   retval = PAPI_stop( EventSet2, values2 );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
      printf("Uncore and regular event test:\n");
      printf("Using uncore event %s\n",uncore_event);
      printf("\t%s: %lld\n",uncore_event,values[0]);
      printf("\t%s: %lld\n","PAPI_TOT_CYC",values2[0]);
   }

   test_pass( __FILE__, NULL, 0 );

   return 0;
}
Пример #7
0
int
main( int argc, char **argv )
{
    int status, retval, num_tests = 1, tmp;
    int EventSet1 = PAPI_NULL;
    long long **values;
    long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
    char event_name[PAPI_MAX_STR_LEN];;
    const PAPI_hw_info_t *hw_info;
    const PAPI_component_info_t *cmpinfo;
    pid_t pid;

    /* Fork before doing anything with the PMU */

    setbuf(stdout,NULL);
    pid = fork(  );
    if ( pid < 0 )
        test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
    if ( pid == 0 )
        exit( wait_for_attach_and_loop(  ) );

    tests_quiet( argc, argv );	/* Set TESTS_QUIET variable */


    /* Master only process below here */

    retval = PAPI_library_init( PAPI_VER_CURRENT );
    if ( retval != PAPI_VER_CURRENT )
        test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );

    if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
        test_fail_exit( __FILE__, __LINE__, "PAPI_get_component_info", 0 );

    if ( cmpinfo->attach == 0 )
        test_skip( __FILE__, __LINE__, "Platform does not support attaching",
                   0 );

    hw_info = PAPI_get_hardware_info(  );
    if ( hw_info == NULL )
        test_fail_exit( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );

    /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
       PAPI_TOT_INS, depending on the availability of the event on the
       platform */
    retval = PAPI_create_eventset(&EventSet1);
    if ( retval != PAPI_OK )
        test_fail_exit( __FILE__, __LINE__, "PAPI_create_eventset", retval );

    /* Here we are testing that this does not cause a fail */

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

    retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
    if ( retval != PAPI_OK )
        test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );

    retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
    if ( retval != PAPI_OK )
        test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );


    strcpy(event_name,"PAPI_FP_INS");

    retval = PAPI_add_named_event(EventSet1, event_name);
    if ( retval == PAPI_ENOEVNT ) {
        strcpy(event_name,"PAPI_TOT_INS");
        retval = PAPI_add_named_event(EventSet1, event_name);
    }

    if ( retval != PAPI_OK ) {
        test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
    }

    values = allocate_test_space( 1, 2);

    elapsed_us = PAPI_get_real_usec(  );

    elapsed_cyc = PAPI_get_real_cyc(  );

    elapsed_virt_us = PAPI_get_virt_usec(  );

    elapsed_virt_cyc = PAPI_get_virt_cyc(  );

    printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
    pid_t  child = wait( &status );
    printf( "Debugger exited wait() with %d\n",child );
    if (WIFSTOPPED( status ))
    {
        printf( "Child has stopped due to signal %d (%s)\n",
                WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
    }
    if (WIFSIGNALED( status ))
    {
        printf( "Child %ld received signal %d (%s)\n",
                (long)child,
                WTERMSIG(status) , strsignal(WTERMSIG( status )) );
    }
    printf("After %d\n",retval);

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

    printf("Continuing\n");
#if defined(__FreeBSD__)
    if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
#else
    if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
#endif
        perror( "ptrace(PTRACE_CONT)" );
        return 1;
    }


    do {
        child = wait( &status );
        printf( "Debugger exited wait() with %d\n", child);
        if (WIFSTOPPED( status ))
        {
            printf( "Child has stopped due to signal %d (%s)\n",
                    WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
        }
        if (WIFSIGNALED( status ))
        {
            printf( "Child %ld received signal %d (%s)\n",
                    (long)child,
                    WTERMSIG(status) , strsignal(WTERMSIG( status )) );
        }
    } while (!WIFEXITED( status ));

    printf("Child exited with value %d\n",WEXITSTATUS(status));
    if (WEXITSTATUS(status) != 0)
        test_fail_exit( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);

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

    elapsed_virt_us = PAPI_get_virt_usec(  ) - elapsed_virt_us;

    elapsed_virt_cyc = PAPI_get_virt_cyc(  ) - elapsed_virt_cyc;

    elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;

    elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;

    retval = PAPI_cleanup_eventset(EventSet1);
    if (retval != PAPI_OK)
        test_fail_exit( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );

    retval = PAPI_destroy_eventset(&EventSet1);
    if (retval != PAPI_OK)
        test_fail_exit( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );

    printf( "Test case: 3rd party attach start, stop.\n" );
    printf( "-----------------------------------------------\n" );
    tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
    printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
    tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
    printf( "Default granularity is: %d (%s)\n", tmp,
            stringify_granularity( tmp ) );
    printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
    printf
    ( "-------------------------------------------------------------------------\n" );

    printf( "Test type    : \t           1\n" );

    printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
    printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
    printf( TAB1, "Real usec    : \t", elapsed_us );
    printf( TAB1, "Real cycles  : \t", elapsed_cyc );
    printf( TAB1, "Virt usec    : \t", elapsed_virt_us );
    printf( TAB1, "Virt cycles  : \t", elapsed_virt_cyc );

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

    printf( "Verification: none\n" );

    test_pass( __FILE__, values, num_tests );
    exit( 1 );
}
Пример #8
0
int main( int argc, char **argv ) {

   int retval,i,j;
   int EventSet[EVENTS_TO_TRY][MAX_PACKAGES];
   long long values[EVENTS_TO_TRY][MAX_PACKAGES];
   char event_name[BUFSIZ];
   char uncore_base[BUFSIZ];
   char uncore_event[BUFSIZ];
   int uncore_cidx=-1;
   int max_cbox=0;
   int core_to_use=0;

   const PAPI_hw_info_t *hwinfo;

   /* Set TESTS_QUIET variable */
   tests_quiet( argc, argv );

   /* Init the PAPI library */
   retval = PAPI_library_init( PAPI_VER_CURRENT );
   if ( retval != PAPI_VER_CURRENT ) {
      test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
   }

   /* Find the uncore PMU */
   uncore_cidx=PAPI_get_component_index("perf_event_uncore");
   if (uncore_cidx<0) {
      test_skip(__FILE__,__LINE__,"perf_event_uncore component not found",0);
   }

   /* Get hardware info */
   hwinfo = PAPI_get_hardware_info();
   if ( hwinfo == NULL ) {
        test_fail(__FILE__,__LINE__,"PAPI_get_hardware_info()",retval);
   }

   /* Get event to use */
   if (hwinfo->vendor == PAPI_VENDOR_INTEL) {

      if ( hwinfo->cpuid_family == 6) {
         switch(hwinfo->cpuid_model) {
           case 45: /* SandyBridge EP */
                    strncpy(event_name,"UNC_C_TOR_OCCUPANCY:ALL",BUFSIZ);
                    strncpy(uncore_base,"snbep_unc_cbo",BUFSIZ);
                    break;
           case 58: /* IvyBridge */
                    strncpy(event_name,"UNC_CBO_CACHE_LOOKUP:STATE_I:ANY_FILTER",BUFSIZ);
                    strncpy(uncore_base,"ivb_unc_cbo",BUFSIZ);
                    break;
           default:
                    test_skip( __FILE__, __LINE__,
	            "We only support IVB and SNB-EP for now", PAPI_ENOSUPP );
        }
      }
      else {
          test_skip( __FILE__, __LINE__,
	            "We only support IVB and SNB-EP for now", PAPI_ENOSUPP );
      }
   }
   else {
      test_skip( __FILE__, __LINE__,
	            "This test only supported Intel chips", PAPI_ENOSUPP );

   }

   if (!TESTS_QUIET) {
      printf("Trying for %d sockets\n",hwinfo->sockets);
      printf("threads %d cores %d ncpus %d\n", hwinfo->threads,hwinfo->cores,
          hwinfo->ncpu);
   }

   for(i=0;i < hwinfo->sockets; i++) {

      /* perf_event provides which to use in "cpumask"    */
      /* but libpfm4 doesn't report this back to us (yet) */
      core_to_use=i*hwinfo->threads*hwinfo->cores;
      if (!TESTS_QUIET) {
         printf("Using core %d for socket %d\n",core_to_use,i);
      }

      for(j=0;j<EVENTS_TO_TRY;j++) {

         /* Create an eventset */
         EventSet[j][i]=PAPI_NULL;
         retval = PAPI_create_eventset(&EventSet[j][i]);
         if (retval != PAPI_OK) {
            test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
         }

         /* Set a component for the EventSet */
         retval = PAPI_assign_eventset_component(EventSet[j][i], uncore_cidx);
         if (retval!=PAPI_OK) {
            test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component",retval);
         }

         /* we need to set to a certain cpu for uncore to work */

         PAPI_cpu_option_t cpu_opt;

         cpu_opt.eventset=EventSet[j][i];
         cpu_opt.cpu_num=core_to_use;

         retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
         if (retval != PAPI_OK) {
            test_skip( __FILE__, __LINE__,
		      "this test; trying to PAPI_CPU_ATTACH; need to run as root",
		      retval);
         }

         /* Default Granularity should work */

         /* Default domain should work */

         /* Add our uncore event */
	 sprintf(uncore_event,"%s%d::%s",uncore_base,j,event_name);
         retval = PAPI_add_named_event(EventSet[j][i], uncore_event);
         if (retval != PAPI_OK) {
            max_cbox=j;
	    break;
         }
	 if (!TESTS_QUIET) printf("Added %s for socket %d\n",uncore_event,i);

     }
   }


   for(i=0;i < hwinfo->sockets; i++) {
      for(j=0;j<max_cbox;j++) {
         if (!TESTS_QUIET) printf("Starting EventSet %d\n",EventSet[j][i]);
         /* Start PAPI */
         retval = PAPI_start( EventSet[j][i] );
         if ( retval != PAPI_OK ) {
	    printf("Error starting socket %d cbox %d\n",i,j);
            test_fail( __FILE__, __LINE__, "PAPI_start", retval );
         }
     }
   }

   /* our work code */
   do_flops( NUM_FLOPS );

   /* Stop PAPI */
   for(i=0;i < hwinfo->sockets; i++) {
      for(j=0;j<max_cbox;j++) {
         retval = PAPI_stop( EventSet[j][i],&values[j][i] );
         if ( retval != PAPI_OK ) {
	    printf("Error stopping socket %d cbox %d\n",i,j);
            test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
         }
     }
   }

   /* Print Results */
   if ( !TESTS_QUIET ) {
      for(i=0;i < hwinfo->sockets; i++) {
         printf("Socket %d\n",i);
         for(j=0;j<max_cbox;j++) {
            printf("\t%s%d::%s %lld\n",uncore_base,j,event_name,values[j][i]);
         }
      }
   }

   PAPI_shutdown();

   test_pass( __FILE__, NULL, 0 );


   return 0;
}
Пример #9
0
int
main( int argc, char **argv )
{
	int num_tests=1;
	int num_events=1;
	int retval;
	int cpu_num = 1;
	int EventSet1 = PAPI_NULL;
	long long **values;
	char event_name[PAPI_MAX_STR_LEN] = "PAPI_TOT_CYC";
	PAPI_option_t opts;

	// user can provide cpu number on which to count events as arg 1
	if (argc > 1) {
		retval = atoi(argv[1]);
		if (retval >= 0) {
			cpu_num = retval;
		}
	}

	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT )
		test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );

	retval = PAPI_create_eventset(&EventSet1);
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );

	// Force event set to be associated with component 0 (perf_events component provides all core events)
	retval = PAPI_assign_eventset_component( EventSet1, 0 );
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval );

	// Attach this event set to cpu 1
	opts.cpu.eventset = EventSet1;
	opts.cpu.cpu_num = cpu_num;

	retval = PAPI_set_opt( PAPI_CPU_ATTACH, &opts );
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_set_opt", retval );

	retval = PAPI_add_named_event(EventSet1, event_name);
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_add_named_event", retval );
	
	// get space for counter values (this needs to do this call because it malloc's space that test_pass and friends free)
	values = allocate_test_space( num_tests, num_events);
	
	retval = PAPI_start( EventSet1 );
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
	
	// do some work
	do_flops(NUM_FLOPS);
	
	retval = PAPI_stop( EventSet1, values[0] );
	if ( retval != PAPI_OK )
		test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
	
	printf ("Event: %s: %8lld on Cpu: %d\n", event_name, values[0][0], cpu_num);
	
	PAPI_shutdown( );
	
	test_pass( __FILE__, values, 1 );
	return PAPI_OK;
}
Пример #10
0
int main( int argc, char **argv ) {


   char *instructions_event=NULL;
   char event_name[BUFSIZ];

   char user_event[BUFSIZ];
   char kernel_event[BUFSIZ];
   char user_kernel_event[BUFSIZ];


   int retval;

   /* Default Domain */
   int EventSet_default = PAPI_NULL;
   int EventSet_default_user = PAPI_NULL;
   int EventSet_default_kernel = PAPI_NULL;
   int EventSet_default_user_kernel = PAPI_NULL;
   long long default_values[1];
   long long default_user_values[1];
   long long default_kernel_values[1];
   long long default_user_kernel_values[1];

   /* User Domain */
   int EventSet_user = PAPI_NULL;
   int EventSet_user_user = PAPI_NULL;
   int EventSet_user_kernel = PAPI_NULL;
   int EventSet_user_user_kernel = PAPI_NULL;
   long long user_values[1];
   long long user_user_values[1];
   long long user_kernel_values[1];
   long long user_user_kernel_values[1];

   /* Kernel Domain */
   int EventSet_kernel = PAPI_NULL;
   int EventSet_kernel_user = PAPI_NULL;
   int EventSet_kernel_kernel = PAPI_NULL;
   int EventSet_kernel_user_kernel = PAPI_NULL;
   long long kernel_values[1];
   long long kernel_user_values[1];
   long long kernel_kernel_values[1];
   long long kernel_user_kernel_values[1];

   /* All Domain */
   int EventSet_all = PAPI_NULL;
   int EventSet_all_user = PAPI_NULL;
   int EventSet_all_kernel = PAPI_NULL;
   int EventSet_all_user_kernel = PAPI_NULL;
   long long all_values[1];
   long long all_user_values[1];
   long long all_kernel_values[1];
   long long all_user_kernel_values[1];

   /* Two Events */
   int EventSet_two = PAPI_NULL;
   long long two_values[2];

   /* Set TESTS_QUIET variable */
   tests_quiet( argc, argv );

   /* Init the PAPI library */
   retval = PAPI_library_init( PAPI_VER_CURRENT );
   if ( retval != PAPI_VER_CURRENT ) {
      test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
   }


   /* Get a relevant event name */
   instructions_event=get_instructions_event(event_name, BUFSIZ);
   if (instructions_event==NULL) {
      test_skip( __FILE__, __LINE__,
                "No instructions event definition for this arch", 
		 PAPI_ENOSUPP );
   }

   sprintf(user_event,"%s:u=1",instructions_event);
   sprintf(kernel_event,"%s:k=1",instructions_event);
   sprintf(user_kernel_event,"%s:u=1:k=1",instructions_event);

   /*********************************/
   /* Two Events                    */
   /*********************************/

   if (!TESTS_QUIET) {
      printf("\tTwo Events in same EventSet\n");
   }

   retval = PAPI_create_eventset(&EventSet_two);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_two, user_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding user event ",retval);
   }
   retval = PAPI_add_named_event(EventSet_two, kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_two );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_two, two_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld, %s count = %lld\n",
	    user_event,two_values[0],
	    kernel_event,two_values[1]);
   }


   /*********************************/
   /* Default Domain, Default Event */
   /*********************************/

   if (!TESTS_QUIET) {
      printf("\tDefault Domain\n");
   }

   retval = PAPI_create_eventset(&EventSet_default);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_default, instructions_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",instructions_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_default );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_default, default_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",instructions_event,default_values[0]);
   }


   /*********************************/
   /* Default Domain, User Event */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_default_user);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_default_user, user_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_default_user );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_default_user, default_user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_event,default_user_values[0]);
   }

   /*********************************/
   /* Default Domain, Kernel Event */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_default_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_default_kernel, kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_default_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_default_kernel, default_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",kernel_event,default_kernel_values[0]);
   }

   /*****************************************/
   /* Default Domain, user and Kernel Event */
   /*****************************************/


   retval = PAPI_create_eventset(&EventSet_default_user_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_default_user_kernel, user_kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_default_user_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_default_user_kernel, default_user_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_kernel_event,default_user_kernel_values[0]);
   }

   /*********************************/
   /* User Domain, Default Event    */
   /*********************************/

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_USER Domain\n");
   }

   retval=PAPI_set_cmp_domain(PAPI_DOM_USER, 0);

   retval = PAPI_create_eventset(&EventSet_user);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_user, instructions_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",instructions_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_user );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_user, user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",instructions_event,user_values[0]);
   }


   /*********************************/
   /* User Domain, User Event       */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_user_user);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_user_user, user_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_user_user );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_user_user, user_user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_event,user_user_values[0]);
   }

   /*********************************/
   /* User Domain, Kernel Event     */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_user_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_user_kernel, kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_user_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_user_kernel, user_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",kernel_event,user_kernel_values[0]);
   }

   /*****************************************/
   /* User Domain, user and Kernel Event    */
   /*****************************************/

   retval = PAPI_create_eventset(&EventSet_user_user_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_user_user_kernel, user_kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_user_user_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_user_user_kernel, user_user_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_kernel_event,user_user_kernel_values[0]);
   }

   /*********************************/
   /* Kernel Domain, Default Event  */
   /*********************************/

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_KERNEL Domain\n");
   }

   retval=PAPI_set_cmp_domain(PAPI_DOM_KERNEL, 0);

   retval = PAPI_create_eventset(&EventSet_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_kernel, instructions_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",instructions_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_kernel, kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",instructions_event,kernel_values[0]);
   }


   /*********************************/
   /* Kernel Domain, User Event     */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_kernel_user);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_kernel_user, user_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_kernel_user );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_kernel_user, kernel_user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_event,kernel_user_values[0]);
   }

   /*********************************/
   /* Kernel Domain, Kernel Event   */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_kernel_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_kernel_kernel, kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_kernel_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_kernel_kernel, kernel_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",kernel_event,kernel_kernel_values[0]);
   }

   /*****************************************/
   /* Kernel Domain, user and Kernel Event  */
   /*****************************************/

   retval = PAPI_create_eventset(&EventSet_kernel_user_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_kernel_user_kernel, user_kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_kernel_user_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_kernel_user_kernel, kernel_user_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_kernel_event,kernel_user_kernel_values[0]);
   }

   /*********************************/
   /* All Domain, Default Event  */
   /*********************************/

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_ALL Domain\n");
   }

   retval=PAPI_set_cmp_domain(PAPI_DOM_ALL, 0);

   retval = PAPI_create_eventset(&EventSet_all);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_all, instructions_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",instructions_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_all );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_all, all_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",instructions_event,all_values[0]);
   }


   /*********************************/
   /* All Domain, User Event        */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_all_user);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_all_user, user_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_all_user );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_all_user, all_user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_event,all_user_values[0]);
   }

   /*********************************/
   /* All Domain, Kernel Event      */
   /*********************************/

   retval = PAPI_create_eventset(&EventSet_all_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_all_kernel, kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_all_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_all_kernel, all_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",kernel_event,all_kernel_values[0]);
   }

   /*****************************************/
   /* All Domain, user and Kernel Event     */
   /*****************************************/

   retval = PAPI_create_eventset(&EventSet_all_user_kernel);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }


   retval = PAPI_add_named_event(EventSet_all_user_kernel, user_kernel_event);
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
	 fprintf(stderr,"Error trying to add %s\n",user_kernel_event);
      }
      test_fail(__FILE__, __LINE__, "adding instructions event ",retval);
   }

   retval = PAPI_start( EventSet_all_user_kernel );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet_all_user_kernel, all_user_kernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
     printf("\t\t%s count = %lld\n",user_kernel_event,all_user_kernel_values[0]);
   }

   /**************/
   /* Validation */
   /**************/

   //TODO

   test_pass( __FILE__, NULL, 0 );

   return 0;
}
Пример #11
0
int main (int argc, char **argv)
{

    int retval,cid,rapl_cid=-1,numcmp;
    int EventSet = PAPI_NULL;
    long long values[MAX_EVENTS];
    int i,code,enum_retval;
    const PAPI_component_info_t *cmpinfo = NULL;
    long long start_time,before_time,after_time;
    double elapsed_time,total_time;
    char event_name[BUFSIZ];

	/* PAPI Initialization */
     retval = PAPI_library_init( PAPI_VER_CURRENT );
     if ( retval != PAPI_VER_CURRENT ) {
        fprintf(stderr,"PAPI_library_init failed\n");
	exit(1);
     }

     numcmp = PAPI_num_components();

     for(cid=0; cid<numcmp; cid++) {

	if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
	   fprintf(stderr,"PAPI_get_component_info failed\n");
	   exit(1);
	}

	if (strstr(cmpinfo->name,"rapl")) {
	   rapl_cid=cid;
	   printf("Found rapl component at cid %d\n", rapl_cid);

           if (cmpinfo->disabled) {
	     fprintf(stderr,"No rapl events found: %s\n",
		     cmpinfo->disabled_reason);
	     exit(1);
           }
	   break;
	}
     }

     /* Component not found */
     if (cid==numcmp) {
        fprintf(stderr,"No rapl component found\n");
        exit(1);
     }

     /* Find Events */
     code = PAPI_NATIVE_MASK;

     enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );

     while ( enum_retval == PAPI_OK ) {

       retval = PAPI_event_code_to_name( code, event_name );
       if ( retval != PAPI_OK ) {
	  printf("Error translating %#x\n",code);
	  exit(1);
       }

       printf("Found: %s\n",event_name);
       strncpy(events[num_events],event_name,BUFSIZ);
       sprintf(filenames[num_events],"results.%s",event_name);
       num_events++;
       
       if (num_events==MAX_EVENTS) {
	  printf("Too many events! %d\n",num_events);
	  exit(1);
       }

       enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );

     }

     if (num_events==0) {
        printf("Error!  No RAPL events found!\n");
	exit(1);
     }

     /* Open output files */
     for(i=0;i<num_events;i++) {
        fff[i]=fopen(filenames[i],"w");
	if (fff[i]==NULL) {
	   fprintf(stderr,"Could not open %s\n",filenames[i]);
	   exit(1);
	}
     }
				   

     /* Create EventSet */
     retval = PAPI_create_eventset( &EventSet );
     if (retval != PAPI_OK) {
        fprintf(stderr,"Error creating eventset!\n");
     }

     for(i=0;i<num_events;i++) {
	
        retval = PAPI_add_named_event( EventSet, events[i] );
        if (retval != PAPI_OK) {
	   fprintf(stderr,"Error adding event %s\n",events[i]);
	}
     }

  

     start_time=PAPI_get_real_nsec();

     while(1) {

        /* Start Counting */
        before_time=PAPI_get_real_nsec();
        retval = PAPI_start( EventSet);
        if (retval != PAPI_OK) {
           fprintf(stderr,"PAPI_start() failed\n");
	   exit(1);
        }


        usleep(100000);

        /* Stop Counting */
        after_time=PAPI_get_real_nsec();
        retval = PAPI_stop( EventSet, values);
        if (retval != PAPI_OK) {
           fprintf(stderr, "PAPI_start() failed\n");
        }

        total_time=((double)(after_time-start_time))/1.0e9;
        elapsed_time=((double)(after_time-before_time))/1.0e9;

        for(i=0;i<num_events;i++) {

	   fprintf(fff[i],"%.4f %.1f (* Average Power for %s *)\n",
		   total_time,
		   ((double)values[i]/1.0e9)/elapsed_time,
		   events[i]);
	   fflush(fff[i]);
        }
     }
		
     return 0;
}
Пример #12
0
int main( int argc, char **argv ) {

   int retval;
   int EventSet1 = PAPI_NULL;
   int EventSet2 = PAPI_NULL;
   int EventSet3 = PAPI_NULL;
   int EventSet4 = PAPI_NULL;
   int EventSet5 = PAPI_NULL;
   int EventSet6 = PAPI_NULL;
   int EventSet7 = PAPI_NULL;
   int EventSet8 = PAPI_NULL;
   int EventSet9 = PAPI_NULL;
   int EventSet10 = PAPI_NULL;
 
   PAPI_domain_option_t domain_opt;
   PAPI_granularity_option_t gran_opt;
   PAPI_cpu_option_t cpu_opt;
   cpu_set_t mask;

   long long dom_user_values[1],dom_userkernel_values[1],dom_all_values[1];
   long long grn_thr_values[1],grn_proc_values[1];
   long long grn_sys_values[1],grn_sys_cpu_values[1];
   long long total_values[1],total_affinity_values[1];
   long long total_all_values[1];

   dom_user_values[0]=0;
   dom_userkernel_values[0]=0;
   dom_all_values[0]=0;
   grn_thr_values[0]=0;
   grn_proc_values[0]=0;
   grn_sys_values[0]=0;
   grn_sys_cpu_values[0]=0;
   total_values[0]=0;
   total_affinity_values[0]=0;
   total_all_values[0]=0;

   /* Set TESTS_QUIET variable */
   tests_quiet( argc, argv );

   /* Init the PAPI library */
   retval = PAPI_library_init( PAPI_VER_CURRENT );
   if ( retval != PAPI_VER_CURRENT ) {
      test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
   }

   /***************************/
   /***************************/
   /* Default, user events    */
   /***************************/
   /***************************/

   if (!TESTS_QUIET) {
      printf("\nTrying PAPI_TOT_CYC with different domains:\n");
   }

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_USER:\t\t\t");
   }

   retval = PAPI_create_eventset(&EventSet1);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_add_named_event(EventSet1, "PAPI_TOT_CYC");
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to add PAPI_TOT_CYC\n");
      }
      test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
   }

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

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet1, dom_user_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
      printf("%lld\n",dom_user_values[0]);
   }


   /***************************/
   /***************************/
   /* User+Kernel events      */
   /***************************/
   /***************************/

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_USER|PAPI_DOM_KERNEL:\t");
   }

   retval = PAPI_create_eventset(&EventSet2);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet2, 0);

   /* we need to set domain to be as inclusive as possible */

   domain_opt.def_cidx=0;
   domain_opt.eventset=EventSet2;
   domain_opt.domain=PAPI_DOM_USER|PAPI_DOM_KERNEL;

   retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
   if (retval != PAPI_OK) {

      if (retval==PAPI_EPERM) {
         test_skip( __FILE__, __LINE__,
		    "this test; trying to set PAPI_DOM_ALL; need to run as root",
		    retval);
      }
      else {
         test_fail(__FILE__, __LINE__, "setting PAPI_DOM_ALL",retval);
      }
   }


   retval = PAPI_add_named_event(EventSet2, "PAPI_TOT_CYC");
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to add PAPI_TOT_CYC\n");
      }
      test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
   }

   retval = PAPI_start( EventSet2 );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet2, dom_userkernel_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
      printf("%lld\n",dom_userkernel_values[0]);
   }

   /***************************/
   /***************************/
   /* DOMAIN_ALL  events      */
   /***************************/
   /***************************/

   if (!TESTS_QUIET) {
      printf("\tPAPI_DOM_ALL:\t\t\t");
   }

   retval = PAPI_create_eventset(&EventSet3);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet3, 0);

   /* we need to set domain to be as inclusive as possible */

   domain_opt.def_cidx=0;
   domain_opt.eventset=EventSet3;
   domain_opt.domain=PAPI_DOM_ALL;

   retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
   if (retval != PAPI_OK) {

      if (retval==PAPI_EPERM) {
         test_skip( __FILE__, __LINE__,
		    "this test; trying to set PAPI_DOM_ALL; need to run as root",
		    retval);
      }
      else {
         test_fail(__FILE__, __LINE__, "setting PAPI_DOM_ALL",retval);
      }
   }


   retval = PAPI_add_named_event(EventSet3, "PAPI_TOT_CYC");
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to add PAPI_TOT_CYC\n");
      }
      test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
   }

   retval = PAPI_start( EventSet3 );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet3, dom_all_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
      printf("%lld\n",dom_all_values[0]);
   }


   /***************************/
   /***************************/
   /* PAPI_GRN_THR  events */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\nTrying different granularities:\n");
   }

   if ( !TESTS_QUIET ) {
      printf("\tPAPI_GRN_THR:\t\t\t");
   }

   retval = PAPI_create_eventset(&EventSet4);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet4, 0);

   /* Set the granularity to system-wide */

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet4;
   gran_opt.granularity=PAPI_GRN_THR;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      test_skip( __FILE__, __LINE__,
		      "this test; trying to set PAPI_GRN_THR",
		      retval);
   }


   retval = PAPI_add_named_event(EventSet4, "PAPI_TOT_CYC");
   if (retval != PAPI_OK) {
      if ( !TESTS_QUIET ) {
         fprintf(stderr,"Error trying to add PAPI_TOT_CYC\n");
      }
      test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
   }

   retval = PAPI_start( EventSet4 );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
   }

   do_flops( NUM_FLOPS );

   retval = PAPI_stop( EventSet4, grn_thr_values );
   if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
   }

   if ( !TESTS_QUIET ) {
      printf("%lld\n",grn_thr_values[0]);
   }


   /***************************/
   /***************************/
   /* PAPI_GRN_PROC  events   */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\tPAPI_GRN_PROC:\t\t\t");
   }

   retval = PAPI_create_eventset(&EventSet5);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet5, 0);

   /* Set the granularity to system-wide */

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet5;
   gran_opt.granularity=PAPI_GRN_PROC;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      if (!TESTS_QUIET) {
         printf("Unable to set PAPI_GRN_PROC\n");
      }
   }
   else {
      retval = PAPI_add_named_event(EventSet5, "PAPI_TOT_CYC");
      if (retval != PAPI_OK) {
         if ( !TESTS_QUIET ) {
            printf("Error trying to add PAPI_TOT_CYC\n");
         }
         test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
      }

      retval = PAPI_start( EventSet5 );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_start", retval );
      }

      do_flops( NUM_FLOPS );

      retval = PAPI_stop( EventSet5, grn_proc_values );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
      }

      if ( !TESTS_QUIET ) {
         printf("%lld\n",grn_proc_values[0]);
      }
   }



   /***************************/
   /***************************/
   /* PAPI_GRN_SYS  events    */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\tPAPI_GRN_SYS:\t\t\t");
   }

   retval = PAPI_create_eventset(&EventSet6);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet6, 0);

   /* Set the granularity to system-wide */

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet6;
   gran_opt.granularity=PAPI_GRN_SYS;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      if (!TESTS_QUIET) {
         printf("Unable to set PAPI_GRN_SYS\n");
      }
   }
   else {
      retval = PAPI_add_named_event(EventSet6, "PAPI_TOT_CYC");
      if (retval != PAPI_OK) {
         if ( !TESTS_QUIET ) {
            printf("Error trying to add PAPI_TOT_CYC\n");
         }
      } else {

         retval = PAPI_start( EventSet6 );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_start", retval );
         }

         do_flops( NUM_FLOPS );

         retval = PAPI_stop( EventSet6, grn_sys_values );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
         }

         if ( !TESTS_QUIET ) {
            printf("GRN_SYS PAPI_TOT_CYC: %lld\n",grn_sys_values[0]);
         }
      }

   }


   /****************************/
   /****************************/
   /* PAPI_GRN_SYS_CPU  events */
   /****************************/
   /****************************/

   if ( !TESTS_QUIET ) {
      printf("\tPAPI_GRN_SYS_CPU:\t\t");
   }

   retval = PAPI_create_eventset(&EventSet7);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet7, 0);

   /* Set the granularity to system-wide */

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet7;
   gran_opt.granularity=PAPI_GRN_SYS_CPU;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      if (!TESTS_QUIET) {
         printf("Unable to set PAPI_GRN_SYS_CPU\n");
      }
   }
   else {
      retval = PAPI_add_named_event(EventSet7, "PAPI_TOT_CYC");
      if (retval != PAPI_OK) {
         if ( !TESTS_QUIET ) {
            printf("Error trying to add PAPI_TOT_CYC\n");
         }
         test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
      }

      retval = PAPI_start( EventSet7 );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_start", retval );
      }

      do_flops( NUM_FLOPS );

      retval = PAPI_stop( EventSet7, grn_sys_cpu_values );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
      }

      if ( !TESTS_QUIET ) {
         printf("%lld\n",grn_sys_cpu_values[0]);
      }
   }


   /***************************/
   /***************************/
   /* SYS and ATTACH  events  */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\nPAPI_GRN_SYS plus CPU attach:\n");
   }

   if ( !TESTS_QUIET ) {
      printf("\tGRN_SYS, DOM_USER, CPU 0 attach:\t");
   }

   retval = PAPI_create_eventset(&EventSet8);
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
   }

   retval = PAPI_assign_eventset_component(EventSet8, 0);

   /* Set the granularity to system-wide */

   gran_opt.def_cidx=0;
   gran_opt.eventset=EventSet8;
   gran_opt.granularity=PAPI_GRN_SYS;

   retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
   if (retval != PAPI_OK) {
      if (!TESTS_QUIET) {
         printf("Unable to set PAPI_GRN_SYS\n");
      }
   }
   else {
      /* we need to set to a certain cpu */

      cpu_opt.eventset=EventSet8;
      cpu_opt.cpu_num=0;

      retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
      if (retval != PAPI_OK) {
	 if (retval==PAPI_EPERM) {
            test_skip( __FILE__, __LINE__,
		    "this test; trying to CPU_ATTACH; need to run as root",
		    retval);
	 }

         test_fail(__FILE__, __LINE__, "PAPI_CPU_ATTACH",retval);
      }

      retval = PAPI_add_named_event(EventSet8, "PAPI_TOT_CYC");
      if (retval != PAPI_OK) {
         if ( !TESTS_QUIET ) {
            printf("Error trying to add PAPI_TOT_CYC\n");
         }
         test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
      }

      retval = PAPI_start( EventSet8 );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_start", retval );
      }

      do_flops( NUM_FLOPS );

      retval = PAPI_stop( EventSet8, total_values );
      if ( retval != PAPI_OK ) {
         test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
      }

      if ( !TESTS_QUIET ) {
         printf("%lld\n",total_values[0]);
      }
   }


   /***************************/
   /***************************/
   /* SYS and ATTACH, bind CPU  events  */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\tGRN_SYS, DOM_USER, CPU 0 affinity:\t");
   }

   /* Set affinity to CPU 0 */
   CPU_ZERO(&mask);
   CPU_SET(0,&mask);
   retval=sched_setaffinity(0, sizeof(mask), &mask);

   if (retval<0) {
     if (!TESTS_QUIET) {
        printf("Setting affinity failed: %s\n",strerror(errno));
     }
   } else {
   
      retval = PAPI_create_eventset(&EventSet9);
      if (retval != PAPI_OK) {
         test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
      }

      retval = PAPI_assign_eventset_component(EventSet9, 0);

      /* Set the granularity to system-wide */

      gran_opt.def_cidx=0;
      gran_opt.eventset=EventSet9;
      gran_opt.granularity=PAPI_GRN_SYS;

      retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
      if (retval != PAPI_OK) {
         if (!TESTS_QUIET) {
            printf("Unable to set PAPI_GRN_SYS\n");
         }
      }
      else {
         /* we need to set to a certain cpu for uncore to work */
      
         cpu_opt.eventset=EventSet9;
         cpu_opt.cpu_num=0;

         retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
         if (retval != PAPI_OK) {
            test_fail(__FILE__, __LINE__, "PAPI_CPU_ATTACH",retval);
         }

         retval = PAPI_add_named_event(EventSet9, "PAPI_TOT_CYC");
         if (retval != PAPI_OK) {
            if ( !TESTS_QUIET ) {
               printf("Error trying to add PAPI_TOT_CYC\n");
            }
            test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
         }

         retval = PAPI_start( EventSet9 );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_start", retval );
         }

         do_flops( NUM_FLOPS );

         retval = PAPI_stop( EventSet9, total_affinity_values );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
         }

         if ( !TESTS_QUIET ) {
            printf("%lld\n",total_affinity_values[0]);
         }
      }
   }

   /***************************/
   /***************************/
   /* SYS and ATTACH, bind CPU  events  */
   /***************************/
   /***************************/

   if ( !TESTS_QUIET ) {
      printf("\tGRN_SYS, DOM_ALL, CPU 0 affinity:\t");
   }



   /* Set affinity to CPU 0 */
   CPU_ZERO(&mask);
   CPU_SET(0,&mask);
   retval=sched_setaffinity(0, sizeof(mask), &mask);

   if (retval<0) {
     if (!TESTS_QUIET) {
        printf("Setting affinity failed: %s\n",strerror(errno));
     }
   } else {
   
      retval = PAPI_create_eventset(&EventSet10);
      if (retval != PAPI_OK) {
         test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
      }

      retval = PAPI_assign_eventset_component(EventSet10, 0);

      /* Set DOM_ALL */
      domain_opt.def_cidx=0;
      domain_opt.eventset=EventSet10;
      domain_opt.domain=PAPI_DOM_ALL;

      retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
      if (retval != PAPI_OK) {

         if (retval==PAPI_EPERM) {
            test_skip( __FILE__, __LINE__,
		    "this test; trying to set PAPI_DOM_ALL; need to run as root",
		    retval);
         }
         else {
            test_fail(__FILE__, __LINE__, "setting PAPI_DOM_ALL",retval);
         }
      }

      /* Set the granularity to system-wide */

      gran_opt.def_cidx=0;
      gran_opt.eventset=EventSet10;
      gran_opt.granularity=PAPI_GRN_SYS;

      retval = PAPI_set_opt(PAPI_GRANUL,(PAPI_option_t*)&gran_opt);
      if (retval != PAPI_OK) {
         if (!TESTS_QUIET) {
            printf("Unable to set PAPI_GRN_SYS\n");
         }
      }
      else {
         /* we need to set to a certain cpu for uncore to work */
      
         cpu_opt.eventset=EventSet10;
         cpu_opt.cpu_num=0;

         retval = PAPI_set_opt(PAPI_CPU_ATTACH,(PAPI_option_t*)&cpu_opt);
         if (retval != PAPI_OK) {
            test_fail(__FILE__, __LINE__, "PAPI_CPU_ATTACH",retval);
         }

         retval = PAPI_add_named_event(EventSet10, "PAPI_TOT_CYC");
         if (retval != PAPI_OK) {
            if ( !TESTS_QUIET ) {
               printf("Error trying to add PAPI_TOT_CYC\n");
            }
            test_fail(__FILE__, __LINE__, "adding PAPI_TOT_CYC ",retval);
         }

         retval = PAPI_start( EventSet10 );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_start", retval );
         }

         do_flops( NUM_FLOPS );

         retval = PAPI_stop( EventSet10, total_all_values );
         if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
         }

         if ( !TESTS_QUIET ) {
            printf("%lld\n",total_all_values[0]);
         }
      }
   }

   /**************/
   /* Validation */
   /**************/

   if ( !TESTS_QUIET ) {
      printf("\n");
   }

   if ( !TESTS_QUIET ) {
      printf("Validating:\n");
      printf("\tDOM_USER|DOM_KERNEL (%lld) > DOM_USER (%lld)\n",
             dom_userkernel_values[0],dom_user_values[0]);
   }
   if (dom_user_values[0] > dom_userkernel_values[0]) {
      test_fail( __FILE__, __LINE__, "DOM_USER too high", 0 );
   }

   if ( !TESTS_QUIET ) {
      printf("\n");
   }

   test_pass( __FILE__, NULL, 0 );

   return 0;
}
Пример #13
0
void init_energy(pwr_ctx_t *ctx) {
    char buf[1024] = { '\0' };
    int ret, num_comp, cid, code = 0;
    const PAPI_component_info_t *comp_info = NULL;
    PAPI_event_info_t evinfo;

    assert(ctx != NULL);
    assert(pwr_is_initialized(ctx, PWR_MODULE_STRUCT));
    assert(!pwr_is_initialized(ctx, PWR_MODULE_ENERGY));

    // Initialize PAPI
    if ( !PAPI_is_initialized() ) {
        ret = PAPI_library_init(PAPI_VER_CURRENT);
        if (PAPI_VER_CURRENT != ret ) {
            PAPI_shutdown();
            if (ctx->err_fd) {
                fprintf(ctx->err_fd, "Unexpected PAPI version\n");
            }
            ctx->error = PWR_ARCH_UNSUPPORTED;
            return;
        }
    }

    // Search PAPI components for RAPL (Intel energy counting) events
    num_comp = PAPI_num_components();

    for (cid = 0; cid < num_comp; ++cid) {
        comp_info = PAPI_get_component_info(cid);
        if (NULL == comp_info) {
            PAPI_shutdown();
            if (ctx->err_fd) {
                fprintf(ctx->err_fd, "Invalid PAPI module found\n");
            }
            ctx->error = PWR_INIT_ERR;
            return;
        }
        if (strstr(comp_info->name, "rapl")) {
            if (0 == comp_info->num_native_events) {
                PAPI_shutdown();
                if (ctx->err_fd) {
                    fprintf(ctx->err_fd, "RAPL counters not available\n");
                }
                ctx->error = PWR_UNAVAILABLE;
                return;
            }
            break;
        }
    }

    if (cid == num_comp) {
        if (ctx->err_fd) {
            fprintf(ctx->err_fd, "Cannot find RAPL module in PAPI\n");
        }
        ctx->error = PWR_UNAVAILABLE;
        return;
    }

    // Create PAPI event set
    ctx->event_set = PAPI_NULL;
    ret = PAPI_create_eventset(&ctx->event_set);
    if (PAPI_OK != ret) {
        PAPI_shutdown();
        if (ctx->err_fd) {
            fprintf(ctx->err_fd, "Failed to create a PAPI event set\n");
        }
        ctx->error = PWR_INIT_ERR;
        return;
    }

    // count how many counters can be added
    ctx->emeas = malloc(sizeof(*ctx->emeas));
    ctx->emeas->nbValues = 0;
    for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) {
        snprintf(buf, 1024, "PACKAGE_ENERGY:PACKAGE%d", i);
        ret = PAPI_query_named_event(buf);
        if (PAPI_OK != ret) {
            break;
        }
        ++ctx->emeas->nbValues;
    }
    for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) {
        snprintf(buf, 1024, "DRAM_ENERGY:PACKAGE%d", i);
        ret = PAPI_query_named_event(buf);
        if (PAPI_OK != ret) {
            break;
        }
        ++ctx->emeas->nbValues;
    }

    ctx->emeas->values = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->values));
    ctx->emeas->units = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->units));
    ctx->emeas->names = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->names));

    // fetch the counter information
    unsigned int cpt_i = 0;
    for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) {
        snprintf(buf, 1024, "PACKAGE_ENERGY:PACKAGE%d", i);
        ret = PAPI_query_named_event(buf);
        if (PAPI_OK != ret) {
            break;
        }

        ret = PAPI_add_named_event(ctx->event_set, buf);
        PAPI_event_name_to_code(buf, &code);
        PAPI_get_event_info(code, &evinfo);
        ctx->emeas->units[cpt_i] = strdup(evinfo.units);
        ctx->emeas->names[cpt_i] = strdup(buf);
        ++cpt_i;
    }

    // same for DRAM
    for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) {
        snprintf(buf, 1024, "DRAM_ENERGY:PACKAGE%d", i);
        ret = PAPI_query_named_event(buf);
        if (PAPI_OK != ret) {
            break;
        }

        ret = PAPI_add_named_event(ctx->event_set, buf);
        PAPI_event_name_to_code(buf, &code);
        PAPI_get_event_info(code, &evinfo);
        ctx->emeas->units[cpt_i] = strdup(evinfo.units);
        ctx->emeas->names[cpt_i] = strdup(buf);
        ++cpt_i;
    }

    ctx->emeas_running = false;
    ctx->error = PWR_OK;
    ctx->module_init |= (1U << PWR_MODULE_ENERGY);

    return;
}