@@ -260,6 +260,48 @@ checkCounter (int eventcode)
260260 return 1 ;
261261}
262262
263+ static int
264+ get_max_symbol_length ( int initModifier , int iterModifier ) {
265+
266+ int ecode = 0 | PAPI_PRESET_MASK ;
267+ int len , maxLen = 0 ;
268+ PAPI_event_info_t info ;
269+
270+ /* In case of error, return the legacy value. */
271+ if ( PAPI_enum_event ( & ecode , initModifier ) != PAPI_OK ) {
272+ return 13 ;
273+ }
274+
275+ do {
276+ if ( PAPI_get_event_info ( ecode , & info ) == PAPI_OK ) {
277+ len = strlen (info .symbol );
278+ if ( len > maxLen ) {
279+ maxLen = len ;
280+ }
281+ }
282+ } while ( PAPI_enum_event (& ecode , iterModifier ) == PAPI_OK );
283+
284+ return maxLen + 1 ;
285+ }
286+
287+ static int
288+ print_comp_header_flag ( void ) {
289+
290+ int numComps = PAPI_num_components ();
291+ const PAPI_component_info_t * cmpinfo ;
292+ int cid , non_cpu_comps = 0 ;
293+ for ( cid = 0 ; cid < numComps ; cid ++ ) {
294+ cmpinfo = PAPI_get_component_info ( cid );
295+ if ( strcmp (cmpinfo -> name , "perf_event" ) == 0
296+ || strcmp (cmpinfo -> name , "sysdetect" ) == 0
297+ || strcmp (cmpinfo -> name , "No Components Configured. " ) == 0 ) {
298+ continue ;
299+ }
300+ non_cpu_comps ++ ;
301+ }
302+
303+ return non_cpu_comps ;
304+ }
263305
264306/*
265307 Checks whether a preset event is available. If it is available,
@@ -277,19 +319,31 @@ int is_preset_event_available(char *name) {
277319 exit (1 );
278320 }
279321
322+ /* Since some component presets require qualifiers, such as ":device=0", but
323+ * the base preset names do not contain qualifiers, then the qualifier must
324+ * first be stripped in order to find a match. */
325+ char * localname = strdup (name );
326+ char * basename = strtok (localname , ":" );
327+ if ( NULL == basename ) {
328+ basename = name ;
329+ }
330+
280331 /* Iterate over all the available preset events and compare them by names. */
281332 do {
282333 if ( PAPI_get_event_info ( event_code , & info ) == PAPI_OK ) {
283334
284335 if ( info .count ) {
285336 if ( (check_counter && checkCounter (event_code )) || !check_counter ) {
286- if (strcmp (info .symbol , name ) == 0 )
337+ if (strcmp (info .symbol , basename ) == 0 )
287338 return 1 ;
288339 }
289340 }
290341 }
291342 } while (PAPI_enum_event ( & event_code , PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK );
292343
344+ /* Free the temporary, dynamically allocated buffer. */
345+ free (localname );
346+
293347 return 0 ;
294348}
295349
@@ -328,7 +382,7 @@ main( int argc, char **argv )
328382 }
329383 else if ( ( !strstr ( argv [args ], "--" ) && strstr ( argv [args ], "-c" ) ) || strstr (argv [args ], "--check" ) )
330384 {
331- print_avail_only = PAPI_PRESET_ENUM_AVAIL ;
385+ print_avail_only = PAPI_PRESET_ENUM_CPU_AVAIL ;
332386 check_counter = 1 ;
333387 }
334388 else if ( strstr ( argv [args ], "-a" ))
@@ -484,6 +538,11 @@ main( int argc, char **argv )
484538 continue ;
485539 }
486540
541+ /* Get the length of the longest preset symbol. */
542+ int maxSymLen = get_max_symbol_length (PAPI_ENUM_FIRST , PAPI_PRESET_ENUM_CPU );
543+ int frontPad = (maxSymLen - 4 )/2 ; /* 4 == strlen("Name") */
544+ int backPad = maxSymLen - 4 - frontPad ;
545+
487546 // print heading to show which kind of events follow
488547 if (i == 0 ) {
489548 printf ( "================================================================================\n" );
@@ -497,13 +556,21 @@ main( int argc, char **argv )
497556 }
498557
499558 if ( print_tabular ) {
500- printf ( " Name Code " );
559+ int spaceCnt = 0 ;
560+ for ( spaceCnt = 0 ; spaceCnt < frontPad ; ++ spaceCnt ) {
561+ printf (" " );
562+ }
563+ printf ( "Name" );
564+ for ( spaceCnt = 0 ; spaceCnt < backPad ; ++ spaceCnt ) {
565+ printf (" " );
566+ }
567+ printf ( " Code " );
501568 if ( print_avail_only == PAPI_PRESET_ENUM_CPU ) {
502569 printf ( "Avail " );
503570 }
504571 printf ( "Deriv Description (Note)\n" );
505572 } else {
506- printf ( "%-13s %-11s%-8s%-16s\n |Long Description|\n"
573+ printf ( "%-*s %-11s%-8s%-16s\n |Long Description|\n" , maxSymLen ,
507574 " |Developer's Notes|\n |Derived|\n |PostFix|\n"
508575 " Native Code[n]: <hex> |name|\n" ,
509576 "Symbol" , "Event Code" , "Count" , "|Short Description|" );
@@ -517,7 +584,7 @@ main( int argc, char **argv )
517584 if ( info .count ) {
518585 if ( (check_counter && checkCounter (event_code )) || !check_counter )
519586 {
520- printf ( "%-13s %#x %-5s%s" ,
587+ printf ( "%-*s %#x %-5s%s" , maxSymLen ,
521588 info .symbol ,
522589 info .event_code ,
523590 is_derived ( & info ), info .long_descr );
@@ -528,7 +595,7 @@ main( int argc, char **argv )
528595 }
529596 printf ( "\n" );
530597 } else {
531- printf ( "%-13s %#x %-6s%-4s %s" ,
598+ printf ( "%-*s %#x %-6s%-4s %s" , maxSymLen ,
532599 info .symbol ,
533600 info .event_code ,
534601 ( info .count ? "Yes" : "No" ),
@@ -575,13 +642,164 @@ main( int argc, char **argv )
575642 }
576643 }
577644 } while (PAPI_enum_event ( & event_code , print_avail_only ) == PAPI_OK );
645+
646+ /* Repeat the logic for component presets. For consistency, always ASK FOR the first event,
647+ * if there is not one then nothing to process */
648+ if (PAPI_enum_event ( & event_code , PAPI_PRESET_ENUM_FIRST_COMP ) != PAPI_OK ) {
649+ continue ;
650+ }
651+
652+ /* Print heading for component presets. */
653+ if (i == 0 ) {
654+
655+ if ( print_avail_only == PAPI_PRESET_ENUM_CPU ) {
656+ print_avail_only = PAPI_ENUM_EVENTS ;
657+ } else if ( print_avail_only == PAPI_PRESET_ENUM_CPU_AVAIL ) {
658+ print_avail_only = PAPI_PRESET_ENUM_AVAIL ;
659+ }
660+
661+ /* Get the length of the longest component preset symbol. */
662+ int maxCompSymLen = get_max_symbol_length (PAPI_PRESET_ENUM_FIRST_COMP , PAPI_ENUM_EVENTS );
663+ int frontPad = (maxCompSymLen - 4 )/2 ; /* 4 == strlen("Name") */
664+ int backPad = maxCompSymLen - 4 - frontPad ;
665+
666+ printf ( "================================================================================\n" );
667+ printf ( " PAPI Component Preset Events\n" );
668+ printf ( "================================================================================\n" );
669+
670+ int printCompPresets = print_comp_header_flag ();
671+ if ( printCompPresets ) {
672+ if ( print_tabular ) {
673+ int spaceCnt = 0 ;
674+ for ( spaceCnt = 0 ; spaceCnt < frontPad ; ++ spaceCnt ) {
675+ printf (" " );
676+ }
677+ printf ( "Name" );
678+ for ( spaceCnt = 0 ; spaceCnt < backPad ; ++ spaceCnt ) {
679+ printf (" " );
680+ }
681+ printf ( " Code " );
682+ if ( print_avail_only == PAPI_ENUM_EVENTS ) {
683+ printf ( "Avail " );
684+ }
685+ printf ( "Deriv Description (Note)\n" );
686+ } else {
687+ printf ( "%-*s%-11s%-8s%-16s\n |Long Description|\n" , maxCompSymLen ,
688+ " |Developer's Notes|\n |Derived|\n |PostFix|\n"
689+ " Native Code[n]: <hex> |name|\n" ,
690+ "Symbol" , "Event Code" , "Count" , "|Short Description|" );
691+ }
692+ } else {
693+ printf ( "No components compiled in that support PAPI Component Preset Events.\n" );
694+ }
695+
696+ int first_flag = 1 ;
697+ do {
698+ if ( PAPI_get_event_info ( event_code , & info ) == PAPI_OK ) {
699+
700+ /* Skip disabled components */
701+ const PAPI_component_info_t * component = PAPI_get_component_info (info .component_index );
702+ if (component -> disabled && component -> disabled != PAPI_EDELAY_INIT ) {
703+ continue ;
704+ }
705+
706+ if ( !first_flag ) {
707+ printf ( "--------------------------------------------------------------------------------\n" );
708+ }
709+ first_flag = 0 ;
710+
711+ if ( print_tabular ) {
712+ // if this is a user defined event or its a preset and matches the preset event filters, display its information
713+ if ( filter & info .event_type ) {
714+ if ( print_avail_only == PAPI_PRESET_ENUM_AVAIL ) {
715+ if ( info .count ) {
716+ if ( (check_counter && checkCounter (event_code )) || !check_counter ) {
717+ printf ( "%-*s%#x %-5s%s\n" , maxCompSymLen ,
718+ info .symbol ,
719+ info .event_code ,
720+ is_derived ( & info ), info .long_descr );
721+
722+ /* Add event to tally. */
723+ avail_count ++ ;
724+ if ( !strcmp ( is_derived ( & info ), "Yes" ) ) {
725+ deriv_count ++ ;
726+ }
727+
728+ /* List the qualifiers. */
729+ int k ;
730+ for ( k = 0 ; k < info .num_quals ; ++ k ) {
731+ printf (" %s\n %s\n" , info .quals [k ], info .quals_descrs [k ]);
732+ }
733+ }
734+ }
735+ if ( info .note [0 ] ) {
736+ printf ( " (%s)\n" , info .note );
737+ }
738+ } else {
739+ printf ( "%-*s%#x %-6s%-4s %s\n" , maxCompSymLen ,
740+ info .symbol ,
741+ info .event_code ,
742+ ( info .count ? "Yes" : "No" ),
743+ is_derived ( & info ), info .long_descr );
744+ if ( info .note [0 ] ) {
745+ printf ( " (%s)\n" , info .note );
746+ }
747+
748+ /* List the qualifiers. */
749+ int k ;
750+ for ( k = 0 ; k < info .num_quals ; ++ k ) {
751+ printf (" %s\n %s\n" , info .quals [k ], info .quals_descrs [k ]);
752+ }
753+
754+ tot_count ++ ;
755+ if ( info .count ) {
756+ if ((check_counter && checkCounter (event_code )) || !check_counter )
757+ avail_count ++ ;
758+ }
759+ if ( !strcmp ( is_derived ( & info ), "Yes" ) ) {
760+ deriv_count ++ ;
761+ }
762+ }
763+ }
764+ } else {
765+ if ( ( print_avail_only == PAPI_PRESET_ENUM_AVAIL && info .count ) ||
766+ ( print_avail_only == PAPI_ENUM_EVENTS ) )
767+ {
768+ if ((check_counter && checkCounter (event_code )) || !check_counter ) {
769+ printf ( "%s\t%#x\t%d\t|%s|\n |%s|\n"
770+ " |%s|\n |%s|\n |%s|\n" ,
771+ info .symbol , info .event_code , info .count ,
772+ info .short_descr , info .long_descr , info .note ,
773+ info .derived , info .postfix );
774+ for ( j = 0 ; j < ( int ) info .count ; j ++ ) {
775+ printf ( " Native Code[%d]: %#x |%s|\n" , j ,
776+ info .code [j ], info .name [j ] );
777+ }
778+ }
779+ }
780+ tot_count ++ ;
781+ if ( info .count ) {
782+ if ((check_counter && checkCounter (event_code )) || !check_counter )
783+ avail_count ++ ;
784+ }
785+ if ( !strcmp ( is_derived ( & info ), "Yes" ) ) {
786+ deriv_count ++ ;
787+ }
788+ }
789+ }
790+ } while (PAPI_enum_event ( & event_code , print_avail_only ) == PAPI_OK );
791+
792+ printf ( "================================================================================\n" );
793+
794+ }
795+
796+
797+
578798 }
579799 }
580800
581- printf ( "--------------------------------------------------------------------------------\n" );
582-
583801 if ( !print_event_info ) {
584- if ( print_avail_only == PAPI_PRESET_ENUM_CPU_AVAIL ) {
802+ if ( print_avail_only == PAPI_PRESET_ENUM_CPU_AVAIL || print_avail_only == PAPI_PRESET_ENUM_AVAIL ) {
585803 printf ( "Of %d available events, %d " , avail_count , deriv_count );
586804 } else {
587805 printf ( "Of %d possible events, %d are available, of which %d " ,
0 commit comments