9cead1
commit 861ff65d099c2b958321ca659786c7eb8a9a1130
9cead1
Author: William Cohen <wcohen@redhat.com>
9cead1
Date:   Wed Nov 15 23:47:16 2017 -0500
9cead1
9cead1
    Avoid statically limiting the number of lmsensor events allowed
9cead1
    
9cead1
    Some high-end server machines provide more events than the 512 entries
9cead1
    limit imposed by the LM_SENSORS_MAX_COUNTERS define in the lmsensor
9cead1
    component (observed 577 entries on one machine).  When this limit was
9cead1
    exceeded the lmsensor component would write beyond the array bounds
9cead1
    causing ctests/all_native_events to crash.  Modified the lmsensor code
9cead1
    to dynamically allocate the required space for all the available
9cead1
    lmsensor entries on the machine. This allows ctests/all_native_events
9cead1
    to run to completion.
9cead1
9cead1
diff --git a/src/components/lmsensors/linux-lmsensors.c b/src/components/lmsensors/linux-lmsensors.c
9cead1
index c9d8530d..07be89fd 100644
9cead1
--- a/src/components/lmsensors/linux-lmsensors.c
9cead1
+++ b/src/components/lmsensors/linux-lmsensors.c
9cead1
@@ -43,8 +43,6 @@
9cead1
 
9cead1
 /*************************  DEFINES SECTION  ***********************************
9cead1
  *******************************************************************************/
9cead1
-/* this number assumes that there will never be more events than indicated */
9cead1
-#define LM_SENSORS_MAX_COUNTERS 512
9cead1
 // time in usecs
9cead1
 #define LM_SENSORS_REFRESHTIME 200000
9cead1
 
9cead1
@@ -82,7 +80,6 @@ typedef struct _lmsensors_reg_alloc
9cead1
 
9cead1
 typedef struct _lmsensors_control_state
9cead1
 {
9cead1
-	long_long counts[LM_SENSORS_MAX_COUNTERS];	// used for caching
9cead1
 	long_long lastupdate;
9cead1
 } _lmsensors_control_state_t;
9cead1
 
9cead1
@@ -100,6 +97,7 @@ typedef struct _lmsensors_context
9cead1
 static _lmsensors_native_event_entry_t *lm_sensors_native_table;
9cead1
 /* number of events in the table*/
9cead1
 static int num_events = 0;
9cead1
+long_long *cached_counts = NULL;	// used for caching readings
9cead1
 
9cead1
 
9cead1
 
9cead1
@@ -304,6 +302,9 @@ _lmsensors_init_component( int cidx )
9cead1
     num_events = detectSensors(  );
9cead1
     SUBDBG("Found %d sensors\n",num_events);
9cead1
 
9cead1
+    _lmsensors_vector.cmp_info.num_mpx_cntrs = num_events;
9cead1
+    _lmsensors_vector.cmp_info.num_cntrs = num_events;
9cead1
+
9cead1
     if ( ( lm_sensors_native_table =
9cead1
 	   calloc( num_events, sizeof ( _lmsensors_native_event_entry_t )))
9cead1
 				   == NULL ) {
9cead1
@@ -312,6 +313,14 @@ _lmsensors_init_component( int cidx )
9cead1
        return PAPI_ENOMEM;
9cead1
     }
9cead1
 
9cead1
+    cached_counts = (long long*) calloc(num_events, sizeof(long long));
9cead1
+
9cead1
+    if (cached_counts == NULL) {
9cead1
+        strncpy(_lmsensors_vector.cmp_info.disabled_reason,
9cead1
+               "Could not malloc room",PAPI_MAX_STR_LEN);
9cead1
+	return PAPI_ENOMEM;
9cead1
+    }
9cead1
+
9cead1
     if ( ( unsigned ) num_events != createNativeEvents(  ) ) {
9cead1
        strncpy(_lmsensors_vector.cmp_info.disabled_reason,
9cead1
 	      "LM_SENSOR number mismatch",PAPI_MAX_STR_LEN);
9cead1
@@ -413,8 +422,7 @@ _lmsensors_init_control_state( hwd_control_state_t *ctl )
9cead1
 	int i;
9cead1
 
9cead1
 	for ( i = 0; i < num_events; i++ )
9cead1
-		( ( _lmsensors_control_state_t * ) ctl )->counts[i] =
9cead1
-			getEventValue( i );
9cead1
+		cached_counts[i] = getEventValue( i );
9cead1
 
9cead1
 	( ( _lmsensors_control_state_t * ) ctl )->lastupdate =
9cead1
 		PAPI_get_real_usec(  );
9cead1
@@ -465,12 +473,12 @@ _lmsensors_read( hwd_context_t *ctx, hwd_control_state_t *ctl,
9cead1
     if ( start - control->lastupdate > 200000 ) {	// cache refresh
9cead1
        
9cead1
        for ( i = 0; i < num_events; i++ ) {
9cead1
-	   control->counts[i] = getEventValue( i );
9cead1
+	   cached_counts[i] = getEventValue( i );
9cead1
        }
9cead1
        control->lastupdate = PAPI_get_real_usec(  );
9cead1
     }
9cead1
 
9cead1
-    *events = control->counts;
9cead1
+    *events = cached_counts;
9cead1
     return PAPI_OK;
9cead1
 }
9cead1
 
9cead1
@@ -478,6 +486,8 @@ _lmsensors_read( hwd_context_t *ctx, hwd_control_state_t *ctl,
9cead1
 int
9cead1
 _lmsensors_shutdown_component( void )
9cead1
 {
9cead1
+	if (cached_counts)
9cead1
+		free(cached_counts);
9cead1
 
9cead1
 	/* Call the libsensors cleaning function before leaving */
9cead1
 	sensors_cleanup(  );
9cead1
@@ -627,8 +637,8 @@ papi_vector_t _lmsensors_vector = {
9cead1
 	.short_name = "lmsensors",
9cead1
 	.version = "5.0",
9cead1
 	.description = "Linux LMsensor statistics",
9cead1
-	.num_mpx_cntrs = LM_SENSORS_MAX_COUNTERS,
9cead1
-	.num_cntrs = LM_SENSORS_MAX_COUNTERS,
9cead1
+	.num_mpx_cntrs = 0,
9cead1
+	.num_cntrs = 0,
9cead1
 	.default_domain = PAPI_DOM_USER,
9cead1
 	//.available_domains = PAPI_DOM_USER,
9cead1
 	.default_granularity = PAPI_GRN_THR,