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