src/components/pcp/linux-pcp.c
//-----------------------------------------------------------------------------
// We cache all domains we read; to save the round-trip cost to the daemon;
// on Saturn Test 8ms (too much delay invoked over thousands of events). 
// Use inst = -1 to free all malloced memory. 
// WARNING: realloc() cachedDomains[] can relocate the entire cache in memory,
// INVALIDATING ALL POINTERS into it, causing segfaults or anomolous results.
//-----------------------------------------------------------------------------
We cache all domains we read; to save the round-trip cost to the daemon;
on Saturn Test 8ms (too much delay invoked over thousands of events).
Use inst = -1 to free all malloced memory.
WARNING: realloc() cachedDomains[] can relocate the entire cache in memory,
INVALIDATING ALL POINTERS into it, causing segfaults or anomolous results.
We cache all domains we read ; to save the round - trip cost to the daemon ; on Saturn Test 8ms ( too much delay invoked over thousands of events ) . Use inst = - 1 to free all malloced memory . WARNING : realloc () cachedDomains [] can relocate the entire cache in memory INVALIDATING ALL POINTERS into it causing segfaults or anomolous results .
static char *cachedGetInDom(pmInDom indom, int inst) 
{
   static int domains=0;                                                
   static _pcp_domain_cache_t* cachedDomains = NULL;                    
   int i, domIdx;
   if (inst == -1) {                                                    
      if (cachedDomains == NULL) return(NULL);                          
      for (i=0; i<domains; i++) {                                       
         free(cachedDomains[i].instances);                              
         free(cachedDomains[i].names);                                  
      }
      free(cachedDomains);                                              
      domains = 0;                                                      
      cachedDomains = NULL;                                             
      return(NULL);                                                     
   } 
   for (i=0; i<domains; i++) {
      if (indom == cachedDomains[i].domainId) break;                    
   } 
   domIdx = i;                                                          
   if (i == domains) {                                                  
      domains++;                                                        
      if (domains == 1) {                                               
         cachedDomains = malloc(sizeof(_pcp_domain_cache_t));           
      } else {                                                          
         cachedDomains = realloc(cachedDomains, 
                        domains*sizeof(_pcp_domain_cache_t));           
      }
      if (cachedDomains == NULL) {                                      
         fprintf(stderr, "%s:%i:%s malloc/realloc denied for "
                 "cachedDomains; size=%i.\n", 
                 __FILE__, __LINE__, __func__, domains);
         exit(-1);
      } 
      cachedDomains[domIdx].domainId = indom;                           
      cachedDomains[domIdx].numInstances = pcp_pmGetInDom(indom, 
         &cachedDomains[domIdx].instances,                              
         &cachedDomains[domIdx].names);                                 
      for (i=0; i<cachedDomains[domIdx].numInstances; i++) {            
         if (cachedDomains[domIdx].names[i] == NULL || 
             strlen(cachedDomains[domIdx].names[i]) == 0 || 
             strlen(cachedDomains[domIdx].names[i]) >= PAPI_MAX_STR_LEN) {
            fprintf(stderr, "%s:%i:%s ERROR: cachedGetInDom: domain=%u, domIdx=%i, name idx %i invalid string.\n",
               __FILE__, __LINE__, FUNC, indom, domIdx, i);
            exit(-1);
         } 
      }           
   } 
   for (i=0; i < cachedDomains[domIdx].numInstances; i++) {             
      if (cachedDomains[domIdx].instances[i] == inst)                   If found,
        return cachedDomains[domIdx].names[i];                          .. return matching name.
   } 
   fprintf(stderr, "%s:%i:%s ERROR: cachedGetInDom: domain=%u, domIdx=%i, numInstances=%i, failed to find inst=%i.\n",
      __FILE__, __LINE__, FUNC, indom, domIdx, cachedDomains[domIdx].numInstances, inst);
   exit(-1);                                                            
   return NULL;                                                         
}
static char * cachedGetInDom ( pmInDom indom , int inst ) { static int domains = 0 ; static _pcp_domain_cache_t * cachedDomains = NULL ; int i , domIdx ; if ( inst == -1 ) { if ( cachedDomains == NULL ) return ( NULL ) ; for ( i = 0 ; i < domains ; i ++ ) { free ( cachedDomains [ i ] . instances ) ; free ( cachedDomains [ i ] . names ) ; } free ( cachedDomains ) ; domains = 0 ; cachedDomains = NULL ; return ( NULL ) ; } for ( i = 0 ; i < domains ; i ++ ) { if ( indom == cachedDomains [ i ] . domainId ) break ; } domIdx = i ; if ( i == domains ) { domains ++ ; if ( domains == 1 ) { cachedDomains = malloc ( sizeof ( _pcp_domain_cache_t ) ) ; } else { cachedDomains = realloc ( cachedDomains , domains * sizeof ( _pcp_domain_cache_t ) ) ; } if ( cachedDomains == NULL ) { fprintf ( stderr , " " " \n " , __FILE__ , __LINE__ , __func__ , domains ) ; exit ( -1 ) ; } cachedDomains [ domIdx ] . domainId = indom ; cachedDomains [ domIdx ] . numInstances = pcp_pmGetInDom ( indom , & cachedDomains [ domIdx ] . instances , & cachedDomains [ domIdx ] . names ) ; for ( i = 0 ; i < cachedDomains [ domIdx ] . numInstances ; i ++ ) { if ( cachedDomains [ domIdx ] . names [ i ] == NULL || strlen ( cachedDomains [ domIdx ] . names [ i ] ) == 0 || strlen ( cachedDomains [ domIdx ] . names [ i ] ) >= PAPI_MAX_STR_LEN ) { fprintf ( stderr , " \n " , __FILE__ , __LINE__ , FUNC , indom , domIdx , i ) ; exit ( -1 ) ; } } } for ( i = 0 ; i < cachedDomains [ domIdx ] . numInstances ; i ++ ) { if ( cachedDomains [ domIdx ] . instances [ i ] == inst ) return cachedDomains [ domIdx ] . names [ i ] ; } fprintf ( stderr , " \n " , __FILE__ , __LINE__ , FUNC , indom , domIdx , cachedDomains [ domIdx ] . numInstances , inst ) ; exit ( -1 ) ; return NULL ; }
We cache all domains we read; to save the round-trip cost to the daemon;
on Saturn Test 8ms (too much delay invoked over thousands of events).
We cache all domains we read ; to save the round - trip cost to the daemon ; on Saturn Test 8ms ( too much delay invoked over thousands of events ) .
// None cached to begin. // Empty pointer. // If we are shutting down, // exit if we never built an array. // for every one cached, // .. free malloced memory. // .. free malloced memory. // discard our table. // reset. // never free twice. // exit. // end if shutting down. // Check if we have it already. // Exit loop if found. // The domain index. // If not found; make a new one and read it. // .. add one to count. // for first domain, // ..malloc. // for subsequent domains, // realloc, retain first. // If we failed malloc/realloc,  // end if realloc failed. // .. The domain we are getting. // .. store pointer lists in struct too. // ..  // DEBUG, vet the strings. // end if domain string is nonsense. // end if we need to cache a new domain. // We got the domain index, Now we can try to look up the  // instance name. // look through all instances.  // .. If found, // .. .. return matching name. // end search for inst. // Cannot continue; should not have happened. // Code cannot be reached. Couldn't find it.