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