Changeset 4828

Show
Ignore:
Timestamp:
09/21/07 14:15:18 (1 year ago)
Author:
khali
Message:

Scan chip features only once at initialization time, rather than on
each update. The chip list and chip features do not change until
sensors_cleanup() is called, so there's no point in doing the
same work over and over again.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/chips.c

    r4827 r4828  
    333333} 
    334334 
    335 /* Note that alarms and beeps are no longer (or not yet) supported */ 
    336 ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip) 
     335static 
     336FeatureDescriptor * generateChipFeatures (const sensors_chip_name *chip) 
    337337{ 
    338338        int nr, count = 1; 
    339339        const sensors_feature_data *sensor; 
    340         ChipDescriptor *descriptor; 
    341340        FeatureDescriptor *features; 
    342341 
     
    349348 
    350349        /* Allocate the memory we need */ 
    351         descriptor = calloc(1, sizeof(ChipDescriptor)); 
    352350        features = calloc(count, sizeof(FeatureDescriptor)); 
    353         if (!descriptor || !features) { 
    354                 free(descriptor); 
    355                 free(features); 
     351        if (!features) 
    356352                return NULL; 
    357         } 
    358         descriptor->features = features; 
    359353 
    360354        /* Fill in the data structures */ 
     
    388382        } 
    389383 
    390         return descriptor; 
    391 
     384        return features; 
     385
     386 
     387ChipDescriptor * knownChips; 
     388 
     389int initKnownChips (void) 
     390
     391  int nr, count = 1; 
     392  const sensors_chip_name *name; 
     393 
     394  /* How many chips do we have? */ 
     395  nr = 0; 
     396  while ((name = sensors_get_detected_chips(NULL, &nr))) 
     397    count++; 
     398 
     399  /* Allocate the memory we need */ 
     400  knownChips = calloc(count, sizeof(ChipDescriptor)); 
     401  if (!knownChips) 
     402    return 1; 
     403 
     404  /* Fill in the data structures */ 
     405  count = 0; 
     406  nr = 0; 
     407  while ((name = sensors_get_detected_chips(NULL, &nr))) { 
     408    knownChips[count].name = name; 
     409    if ((knownChips[count].features = generateChipFeatures(name))) 
     410      count++; 
     411  } 
     412 
     413  return 0; 
     414
     415 
     416void freeKnownChips (void) 
     417
     418  int index0; 
     419 
     420  for (index0 = 0; knownChips[index0].features; index0++) 
     421    free (knownChips[index0].features); 
     422  free (knownChips); 
     423
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/lib.c

    r4693 r4828  
    117117loadLib 
    118118(void) { 
    119   return loadConfig (0); 
     119  int ret; 
     120  ret = loadConfig (0); 
     121  if (!ret) 
     122    ret = initKnownChips (); 
     123  return ret; 
    120124} 
    121125 
     
    123127reloadLib 
    124128(void) { 
    125   return loadConfig (1); 
     129  int ret; 
     130  freeKnownChips (); 
     131  ret = loadConfig (1); 
     132  if (!ret) 
     133    ret = initKnownChips (); 
     134  return ret; 
    126135} 
    127136 
     
    129138unloadLib 
    130139(void) { 
     140  freeKnownChips (); 
    131141  sensors_cleanup (); 
    132142  return 0;   
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/rrd.c

    r4817 r4828  
    142142  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) { 
    143143    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) { 
    144       ChipDescriptor *descriptor; 
    145       descriptor = generateChipDescriptor (chip); 
    146       if (descriptor) { 
     144      int index0, chipindex = -1; 
     145      for (index0 = 0; knownChips[index0].features; ++ index0) 
     146        /* Trick: we compare addresses here. We know it works because both 
     147           pointers were returned by sensors_get_detected_chips(), so they 
     148           refer to libsensors internal structures, which do not move. */ 
     149        if (knownChips[index0].name == chip) { 
     150          chipindex = index0; 
     151          break; 
     152        } 
     153      if (chipindex >= 0) { 
     154        const ChipDescriptor *descriptor = &knownChips[chipindex]; 
    147155        const FeatureDescriptor *features = descriptor->features; 
    148         int index0; 
    149156 
    150157        for (index0 = 0; (ret == 0) && (num < MAX_RRD_SENSORS) && features[index0].format; ++ index0) { 
     
    168175            free (label); 
    169176        } 
    170         free (descriptor->features); 
    171         free (descriptor); 
    172177      } 
    173178    } 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/sense.c

    r4825 r4828  
    169169    ret = setChip (chip); 
    170170  } else { 
    171     ChipDescriptor *descriptor; 
    172     descriptor = generateChipDescriptor (chip); 
    173     if (descriptor) { 
    174       ret = doKnownChip (chip, descriptor, action); 
    175       free (descriptor->features); 
    176       free (descriptor); 
    177     } 
     171    int index0, chipindex = -1; 
     172    for (index0 = 0; knownChips[index0].features; ++ index0) 
     173      /* Trick: we compare addresses here. We know it works because both 
     174         pointers were returned by sensors_get_detected_chips(), so they 
     175         refer to libsensors internal structures, which do not move. */ 
     176      if (knownChips[index0].name == chip) { 
     177        chipindex = index0; 
     178        break; 
     179      } 
     180    if (chipindex >= 0) 
     181      ret = doKnownChip (chip, &knownChips[chipindex], action); 
    178182  } 
    179183  return ret; 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/sensord.h

    r4825 r4828  
    9696 
    9797typedef struct { 
     98  const sensors_chip_name *name; 
    9899  FeatureDescriptor *features; 
    99100} ChipDescriptor; 
    100101 
    101 extern ChipDescriptor * generateChipDescriptor (const sensors_chip_name *chip); 
     102extern ChipDescriptor * knownChips; 
     103extern int initKnownChips (void); 
     104extern void freeKnownChips (void);