IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10884


Ignore:
Timestamp:
Jan 2, 2007, 5:36:38 PM (19 years ago)
Author:
jhoblitt
Message:

completely remove memblock recycling (PS_MEM_USE_RECYCLE)

Location:
branches/jch-memory/psLib/src/sys
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/jch-memory/psLib/src/sys/psMemory.c

    r10547 r10884  
    88*  @author Robert Lupton, Princeton University
    99*
    10 *  @version $Revision: 1.88 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2006-12-08 11:35:54 $
     10*  @version $Revision: 1.88.2.1 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2007-01-03 03:36:38 $
    1212*
    1313*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    5151static bool memory_is_persistent = false;
    5252
    53 #ifdef PS_MEM_USE_RECYCLE               // Only use recycling if this is set
    54 #define N_RECYCLE_BINS 14               // number of recycle bins
    55 #define MAX_RECYCLE 100                 // Maximum number permitted in a recycle bin
    56 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for recycle bins
    57 static const psS32 recycleBinSize[N_RECYCLE_BINS] = // Size of each bin
    58     {
    59         8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE
    60     };
    61 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops)
    62 static psS32 recycleBinNums[N_RECYCLE_BINS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Number in each bin
    63 static psMemBlock* recycleMemBlockList[N_RECYCLE_BINS] = // Contents of the bins
    64     { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
    65 #endif // #ifdef PS_MEM_USE_RECYCLE
    66 
    6753#ifdef PS_MEM_DEBUG
    6854static psMemBlock* deadBlockList;       // a place to put dead memBlocks in debug mode.
     
    7965static psPtr memExhaustedCallbackDefault(size_t size)
    8066{
    81     #if PS_MEM_USE_RECYCLE
    82     psPtr ptr = NULL;
    83     if (safeThreads) {
    84         pthread_mutex_lock(&recycleMemBlockListMutex);
    85     }
    86     // Attempt to free up everything I can find so I can alloc my ptr
    87     int bin = N_RECYCLE_BINS - 1;       // Recycle bin
    88 
    89     while (bin >= 0 && ptr == NULL) {
    90         while (recycleMemBlockList[bin] != NULL && ptr == NULL) {
    91             psMemBlock *old = recycleMemBlockList[bin];
    92             recycleMemBlockList[bin] = recycleMemBlockList[bin]->nextBlock;
    93             free(old);
    94             ptr = malloc(size);
    95         }
    96         bin--;
    97     }
    98 
    99     if (safeThreads) {
    100         pthread_mutex_unlock(&recycleMemBlockListMutex);
    101     }
    102     return ptr;
    103     #else  // #ifdef PS_MEM_USE_RECYCLE
    104 
    10567    return NULL;
    106     #endif // #ifdef PS_MEM_USE_RECYCLE
    10768}
    10869
     
    364325}
    365326
    366 #ifdef PS_MEM_USE_RECYCLE
    367 // Return the appropriate recycle bin number
    368 static int getRecycleBin(size_t size)
    369 {
    370     if (size >= P_PS_LARGE_BLOCK_SIZE) {
    371         return N_RECYCLE_BINS;
    372     }
    373     int binNum = 0;                     // Recycle bin number
    374     while (size > recycleBinSize[binNum]) {
    375         binNum++;
    376     }
    377     return binNum;
    378 }
    379 
    380 // Push a pointer onto the recycle bin
    381 static void recyclePush(psMemBlock *mb, // Pointer to push
    382                         int bin         // Recycling bin
    383                        )
    384 {
    385     assert(mb);
    386     assert(bin < N_RECYCLE_BINS);
    387 
    388     mb->previousBlock = NULL;
    389 
    390     if (safeThreads) {
    391         pthread_mutex_lock(&recycleMemBlockListMutex);
    392     }
    393 
    394     mb->nextBlock = recycleMemBlockList[bin];
    395     if (recycleMemBlockList[bin] != NULL) {
    396         recycleMemBlockList[bin]->previousBlock = mb;
    397     }
    398     recycleMemBlockList[bin] = mb;
    399     recycleBinNums[bin]++;
    400 
    401     if (safeThreads) {
    402         pthread_mutex_unlock(&recycleMemBlockListMutex);
    403     }
    404 
    405     return;
    406 }
    407 
    408 // Pop a pointer from the recycle bin
    409 static psMemBlock *recyclePop(int bin   // Recycling bin
    410                              )
    411 {
    412     if (bin >= N_RECYCLE_BINS) {
    413         return NULL;
    414     }
    415 
    416     if (safeThreads) {
    417         pthread_mutex_lock(&recycleMemBlockListMutex);
    418     }
    419 
    420     psMemBlock *mb = recycleMemBlockList[bin]; // Pointer popped from recycle bin
    421 
    422     if (mb) {
    423         // We pull it off the list
    424         recycleMemBlockList[bin] = mb->nextBlock;
    425         if (recycleMemBlockList[bin] != NULL) {
    426             recycleMemBlockList[bin]->previousBlock = NULL;
    427         }
    428         recycleBinNums[bin]--;
    429     }
    430 
    431     if (safeThreads) {
    432         pthread_mutex_unlock(&recycleMemBlockListMutex);
    433     }
    434 
    435     return mb;
    436 }
    437 #endif // #ifdef PS_MEM_USE_RECYCLE
    438 
    439327/*
    440328 * Actually allocate memory
     
    446334
    447335    psMemBlock *ptr = NULL;
    448 
    449     #ifdef PS_MEM_USE_RECYCLE
    450     // Are we in one of the recycle bins?
    451     int bin = getRecycleBin(size);
    452     if (bin < N_RECYCLE_BINS) {
    453         size = recycleBinSize[bin];     // round-up size to next sized bin.
    454         ptr = recyclePop(bin);          // grab out of the recycle bin
    455     }
    456     #endif // #ifdef PS_MEM_USE_RECYCLE
    457336
    458337    if (ptr == NULL) {
     
    531410    }
    532411
    533     #ifdef PS_MEM_USE_RECYCLE
    534     // Ensure the size matches that of the recycle bin
    535     int bin = getRecycleBin(size);
    536     if (bin < N_RECYCLE_BINS) {
    537         size = recycleBinSize[bin];     // round-up size to next sized bin.
    538     }
    539     #endif // #ifdef PS_MEM_USE_RECYCLE
    540 
    541412    if (size == ptr->userMemorySize) {
    542413        // Nothing to do
     
    831702        if (safeThreads) {
    832703            pthread_mutex_unlock(&memBlockListMutex);
    833         }
    834 
    835         #ifdef PS_MEM_USE_RECYCLE
    836         // do we recycle?
    837         int bin = getRecycleBin(ptr->userMemorySize);
    838         if (bin < N_RECYCLE_BINS && recycleBinNums[bin] < MAX_RECYCLE) {
    839             ptr->refCounter = 0;
    840             recyclePush(ptr, bin);
    841         } else
    842             #endif  // #ifdef PS_MEM_USE_RECYCLE
    843             {
    844                 // memory is larger than I want to recycle.
    845                 #ifdef PS_MEM_DEBUG
    846                 (void)p_psRealloc(vptr, 0, file, lineno);
    847                 ptr->previousBlock = NULL;
    848                 ptr->nextBlock = deadBlockList;
    849                 if (deadBlockList != NULL) {
    850                 deadBlockList->previous = ptr;
    851             }
    852         deadBlockList = ptr;
    853         #else // #ifdef PS_MEM_DEBUG
    854 
    855         if (safeThreads) {
    856             pthread_mutex_destroy(&ptr->refCounterMutex);
    857             }
    858             free(ptr);
    859             #endif // #else - #ifdef PS_MEM_DEBUG
    860 
    861704        }
    862705
     
    12211064size_t psMemStats(const bool print, // print details as they're found?
    12221065                  size_t *allocated, // memory that's currently allocated (but not persistent)
    1223                   size_t *persistent, // persistent memory that's currently allocated
    1224                   size_t *freelist) // memory that's waiting to be recycled
     1066                  size_t *persistent) // persistent memory that's currently allocated
    12251067{
    12261068    const size_t overhead = sizeof(psMemBlock) + sizeof(psPtr); // overhead on each allocation
     
    12641106        printf("Persistent %6s  %10zd %8zd\n", "", persist, npersist);
    12651107    }
    1266     /*
    1267      * Count the memory on the free list
    1268      */
    1269     size_t freelist_s;
    1270     if (freelist == NULL) {
    1271         freelist = &freelist_s;
    1272     }
    1273 
    1274     *freelist = 0;
    1275     int nblock_tot = 0;
    1276     #ifdef PS_MEM_USE_RECYCLE
    1277 
    1278     for (int i = 0; recycleBinSize[i] < P_PS_LARGE_BLOCK_SIZE; i++) {
    1279         size_t bin_contents = 0;
    1280         size_t nblock = 0;
    1281         for (const psMemBlock *ptr = recycleMemBlockList[i]; ptr != NULL; ptr = ptr->nextBlock) {
    1282             nblock++;
    1283             bin_contents += ptr->userMemorySize + overhead;
    1284         }
    1285         *freelist += bin_contents;
    1286         nblock_tot += nblock;
    1287 
    1288         if (print) {
    1289             printf("Freelist   %6d  %10d %8d\n", recycleBinSize[i], bin_contents, nblock);
    1290         }
    1291     }
    1292     #endif // #ifdef PS_MEM_USE_RECYCLE
    1293 
    1294     if (print) {
    1295         printf("Freelist   %6s  %10zd %8d\n", "", *freelist, nblock_tot);
    1296     }
    1297 
    12981108
    12991109    if (safeThreads) {
     
    13011111    }
    13021112
    1303     return *freelist + *allocated + *persistent;
    1304 }
     1113    return *allocated + *persistent;
     1114}
  • branches/jch-memory/psLib/src/sys/psMemory.h

    r9538 r10884  
    1212 *  @ingroup MemoryManagement
    1313 *
    14  *  @version $Revision: 1.61 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-10-13 21:13:48 $
     14 *  @version $Revision: 1.61.2.1 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2007-01-03 03:36:38 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    522522/** return statistics on memory usage
    523523 *
    524  * @return the total amount of memory owned by psLib; if non-NULL also provide a breakdown
    525  * into recyclable, allocated, and allocated-and-persistent
     524 * @return the total amount of memory owned by psLib; if non-NULL also provide
     525 * a breakdown into allocated and allocated-and-persistent
    526526 */
    527527size_t psMemStats(const bool print, ///< print details as they're found?
    528528                  size_t *allocated, ///< memory that's currently allocated (but not persistent)
    529                   size_t *persistent, ///< persistent memory that's currently allocated
    530                   size_t *freelist); ///< memory that's waiting to be recycled
     529                  size_t *persistent); ///< persistent memory that's currently allocated
    531530
    532531//@} End of Memory Management Functions
Note: See TracChangeset for help on using the changeset viewer.