9418d8
diff -up papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c.orig papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c
9418d8
--- papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c.orig	2013-08-06 12:12:20.000000000 -0400
9418d8
+++ papi-5.2.0/src/components/perf_event/pe_libpfm4_events.c	2017-06-15 22:41:58.784904523 -0400
9418d8
@@ -236,14 +236,15 @@ static int find_next_no_aliases(int code
9418d8
         current_pmu++;
9418d8
         SUBDBG("Incrementing PMU: %#x\n",current_pmu);
9418d8
 
9418d8
+        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
+        ret = pfm_get_pmu_info(current_pmu, &pinfo);
9418d8
+
9418d8
 	/* Off the end, so done iterating */
9418d8
-        if (current_pmu>PFM_PMU_MAX) {
9418d8
+        if (ret==PFM_ERR_INVAL) {
9418d8
            return PFM_ERR_NOTFOUND;
9418d8
         }
9418d8
  
9418d8
-        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
-        pfm_get_pmu_info(current_pmu, &pinfo);
9418d8
-        if (pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
9418d8
+        if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
9418d8
      }
9418d8
 
9418d8
      current_event=pinfo.first_event;
9418d8
@@ -533,12 +534,21 @@ get_event_first_active(int pmu_type)
9418d8
 
9418d8
   pmu_idx=0;
9418d8
 
9418d8
-  while(pmu_idx
9418d8
+	/* We loop forever here and exit if pfm_get_pmu_info() fails.	*/
9418d8
+	/* Before we only went up to PFM_PMU_MAX but this is set at	*/
9418d8
+	/* compile time and might not reflect the number of PMUs if	*/
9418d8
+	/* PAPI is dynamically linked against libpfm4.			*/
9418d8
+
9418d8
+  while(1) {
9418d8
 
9418d8
     /* clear the PMU structure (required by libpfm4) */
9418d8
     memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
     ret=pfm_get_pmu_info(pmu_idx, &pinfo);
9418d8
 
9418d8
+    if (ret==PFM_ERR_INVAL) {
9418d8
+	    break;
9418d8
+    }
9418d8
+
9418d8
     if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) {
9418d8
 
9418d8
       pidx=pinfo.first_event;
9418d8
@@ -1372,14 +1382,23 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
9418d8
    retval=pfm_get_pmu_info(0, &(event_table->default_pmu));
9418d8
    
9418d8
    SUBDBG("Detected pmus:\n");
9418d8
-   for(i=0;i
9418d8
+   i=0;
9418d8
+   while(1) {
9418d8
       memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
       retval=pfm_get_pmu_info(i, &pinfo);
9418d8
-      if (retval!=PFM_SUCCESS) {
9418d8
-	 continue;
9418d8
+
9418d8
+      /* We're done if we hit an invalid PMU entry			*/
9418d8
+      /* We can't check against PFM_PMU_MAX				*/
9418d8
+      /* as that might not match if libpfm4 is dynamically linked	*/
9418d8
+
9418d8
+      if (retval==PFM_ERR_INVAL) {
9418d8
+	      break;
9418d8
       }
9418d8
 
9418d8
-      if (pmu_is_present_and_right_type(&pinfo,pmu_type)) {
9418d8
+      if ((retval==PFM_SUCCESS) && (pinfo.name != NULL) &&
9418d8
+	(pmu_is_present_and_right_type(&pinfo,pmu_type))) {
9418d8
+
9418d8
+
9418d8
 	 SUBDBG("\t%d %s %s %d\n",i,pinfo.name,pinfo.desc,pinfo.type);
9418d8
 
9418d8
          detected_pmus++;
9418d8
@@ -1402,6 +1421,7 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
9418d8
 	       found_default=1;
9418d8
 	 }
9418d8
       }
9418d8
+	i++;
9418d8
    }
9418d8
    SUBDBG("%d native events detected on %d pmus\n",ncnt,detected_pmus);
9418d8
 
9418d8
@@ -1421,7 +1441,7 @@ _pe_libpfm4_init(papi_vector_t *my_vecto
9418d8
                                    event_table->default_pmu.num_fixed_cntrs;
9418d8
 
9418d8
    SUBDBG( "num_counters: %d\n", my_vector->cmp_info.num_cntrs );
9418d8
-   
9418d8
+
9418d8
    /* Setup presets, only if Component 0 */
9418d8
    if (cidx==0) {
9418d8
       retval = _papi_load_preset_table( (char *)event_table->default_pmu.name, 
9418d8
diff -up papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c.orig papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c
9418d8
--- papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c.orig	2013-08-06 12:12:20.000000000 -0400
9418d8
+++ papi-5.2.0/src/components/perf_event_uncore/peu_libpfm4_events.c	2017-06-15 22:50:08.700238377 -0400
9418d8
@@ -238,14 +238,15 @@ static int find_next_no_aliases(int code
9418d8
         current_pmu++;
9418d8
         SUBDBG("Incrementing PMU: %#x\n",current_pmu);
9418d8
 
9418d8
+        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
+        ret=pfm_get_pmu_info(current_pmu, &pinfo);
9418d8
+
9418d8
 	/* Off the end, so done iterating */
9418d8
-        if (current_pmu>PFM_PMU_MAX) {
9418d8
+        if (ret==PFM_ERR_INVAL) {
9418d8
            return PFM_ERR_NOTFOUND;
9418d8
         }
9418d8
  
9418d8
-        memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
-        pfm_get_pmu_info(current_pmu, &pinfo);
9418d8
-        if (pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
9418d8
+        if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) break;
9418d8
      }
9418d8
 
9418d8
      current_event=pinfo.first_event;
9418d8
@@ -531,12 +532,20 @@ get_event_first_active(int pmu_type)
9418d8
 
9418d8
   pmu_idx=0;
9418d8
 
9418d8
-  while(pmu_idx
9418d8
+  /* We loop forever here and exit if pfm_get_pmu_info() fails.	*/
9418d8
+  /* Before we only went up to PFM_PMU_MAX but this is set at	*/
9418d8
+  /* compile time and might not reflect the number of PMUs if	*/
9418d8
+  /* PAPI is dynamically linked against libpfm4.		*/
9418d8
+  while(1) {
9418d8
 
9418d8
     /* clear the PMU structure (required by libpfm4) */
9418d8
     memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
     ret=pfm_get_pmu_info(pmu_idx, &pinfo);
9418d8
 
9418d8
+    if (ret==PFM_ERR_INVAL) {
9418d8
+	    break;
9418d8
+    }
9418d8
+
9418d8
     if ((ret==PFM_SUCCESS) && pmu_is_present_and_right_type(&pinfo,pmu_type)) {
9418d8
 
9418d8
       pidx=pinfo.first_event;
9418d8
@@ -1356,14 +1365,21 @@ _peu_libpfm4_init(papi_vector_t *my_vect
9418d8
    my_vector->cmp_info.num_cntrs=0;
9418d8
 
9418d8
    SUBDBG("Detected pmus:\n");
9418d8
-   for(i=0;i
9418d8
+   i=0;
9418d8
+   while(1) {
9418d8
       memset(&pinfo,0,sizeof(pfm_pmu_info_t));
9418d8
       retval=pfm_get_pmu_info(i, &pinfo);
9418d8
-      if (retval!=PFM_SUCCESS) {
9418d8
-	 continue;
9418d8
+
9418d8
+      /* We're done if we hit an invalid PMU entry                    */
9418d8
+      /* We can't check against PFM_PMU_MAX                           */
9418d8
+      /* as that might not match if libpfm4 is dynamically linked     */
9418d8
+
9418d8
+      if (retval==PFM_ERR_INVAL) {
9418d8
+	      break;
9418d8
       }
9418d8
 
9418d8
-      if (pmu_is_present_and_right_type(&pinfo,pmu_type)) {
9418d8
+      if ((retval==PFM_SUCCESS) && (pinfo.name != NULL) &&
9418d8
+	  (pmu_is_present_and_right_type(&pinfo,pmu_type))) {
9418d8
 	 SUBDBG("\t%d %s %s %d\n",i,pinfo.name,pinfo.desc,pinfo.type);
9418d8
 
9418d8
          detected_pmus++;
9418d8
@@ -1372,6 +1388,7 @@ _peu_libpfm4_init(papi_vector_t *my_vect
9418d8
          my_vector->cmp_info.num_cntrs += pinfo.num_cntrs+
9418d8
                                    pinfo.num_fixed_cntrs;
9418d8
       }
9418d8
+	i++;
9418d8
    }
9418d8
    SUBDBG("%d native events detected on %d pmus\n",ncnt,detected_pmus);
9418d8