IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 22, 2006, 3:59:15 PM (20 years ago)
Author:
Paul Price
Message:

Adding memory checking at exit when environment variable PS_ALLOC_CHECK is set to a filename.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psConfigure.c

    r5524 r7646  
    1313 *  @author Robert DeSonia, MHPCC
    1414 *
    15  *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2005-11-16 20:52:23 $
     15 *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-06-23 01:59:15 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
    1919 */
     20#include <stdio.h>
     21#include <stdlib.h>
     22#include <string.h>
     23#include "psMemory.h"
     24#include "psTrace.h"
    2025#include "psString.h"
    2126#include "psTime.h"
     
    2631#include "config.h"
    2732
     33static char *memCheckName = NULL;       // Filename to which to write results of mem check
     34static FILE *memCheckFile = NULL;       // File to which to write results of mem check
     35
    2836char* psLibVersion(void)
    2937{
     
    3139    snprintf(version,80,"%s-v%s",PACKAGE_NAME,PACKAGE_VERSION);
    3240
    33     return(psStringCopy(version));
     41    return psStringCopy(version);
    3442}
     43
     44// Print details of a memory problem to the appropriate file
     45static void memoryProblem(const psMemBlock *ptr, // the pointer to the problematic memory block.
     46                          const char *file, // the file in which the problem originated
     47                          int lineno    // the line number in which the problem originated
     48                         )
     49{
     50    fprintf(memCheckFile,
     51            "Memory corruption detected in memBlock %lu\n"
     52            "\tFile %s, line %d, size %zd\n"
     53            "\tPosts: %p %p %p\n",
     54            ptr->id, file, lineno, ptr->userMemorySize, ptr->startblock, ptr->endblock,
     55            (ptr + 1 + ptr->userMemorySize));
     56}
     57
     58// Check the memory; intended for use on exit, but might be used elsewhere
     59void p_psMemoryCheck(void)
     60{
     61    if (!memCheckName || strlen(memCheckName) == 0) {
     62        return;
     63    }
     64
     65    memCheckFile = fopen(memCheckName, "w"); // File to write leaks to
     66    if (!memCheckFile) {
     67        psError(PS_ERR_IO, true, "Unable to open leaks file, %s\n", memCheckName);
     68        return;
     69    }
     70
     71    int nLeaks = psMemCheckLeaks(0, NULL, memCheckFile, false); // Number of leaks
     72    if (nLeaks > 0) {
     73        psLogMsg(__func__, PS_LOG_WARN, "%d memory leaks found; list written to %s.\n", nLeaks, memCheckName);
     74    } else {
     75        psLogMsg(__func__, PS_LOG_INFO, "No memory leaks found.\n");
     76    }
     77
     78    int nCorrupted;                     // Number of corrupted memory blocks
     79    (void)psMemProblemCallbackSet((psMemProblemCallback)memoryProblem); // Set callback for corruption
     80    nCorrupted = psMemCheckCorruption(false);
     81    if (nCorrupted > 0) {
     82        psError(PS_ERR_UNKNOWN, true, "%d memory blocks corrupted; list written to %s.\n", nCorrupted);
     83    } else {
     84        psLogMsg(__func__, PS_LOG_INFO, "No memory corruption found.\n");
     85    }
     86
     87    fclose(memCheckFile);
     88
     89    return;
     90}
     91
    3592
    3693void psLibInit(const char* timeConfig)
    3794{
    3895    // XXX: Still needs error codes to be set
    39     // XXX: Still needs random number generator initialization
    4096
    41     if(!p_psTimeInit(timeConfig)) {
    42         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    43                 PS_ERRORTEXT_psConfigure_INITIALIZATION_FAILED, "psTime");
    44         return;
     97    if (timeConfig && strlen(timeConfig) > 0) {
     98        if (!p_psTimeInit(timeConfig)) {
     99            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     100                    PS_ERRORTEXT_psConfigure_INITIALIZATION_FAILED, "psTime");
     101            return;
     102        }
    45103    }
    46     if(!p_psEOCInit()) {
     104    if (!p_psEOCInit()) {
    47105        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    48106                PS_ERRORTEXT_psConfigure_INITIALIZATION_FAILED, "psEOC");
    49107        return;
     108    }
     109
     110    // Does the user want memory checking at exit?
     111    memCheckName = getenv("PS_ALLOC_CHECK"); // The value of PS_ALLOC_CHECK
     112    if (memCheckName && strlen(memCheckName) > 0) {
     113        atexit(&p_psMemoryCheck);
    50114    }
    51115}
     
    55119    // Users of persistent memory should free them in this function
    56120
    57     if(!p_psTimeFinalize()) {
    58         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     121    // Stop timers
     122    psTimerStop();
     123
     124    // Free the time tables
     125    if (!p_psTimeFinalize()) {
     126        psError(PS_ERR_UNKNOWN, false,
    59127                PS_ERRORTEXT_psConfigure_FINALIZATION_FAILED, "psTime");
    60128        return;
    61129    }
    62     if(!p_psEOCFinalize()) {
    63         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     130
     131    // Free the precession tables
     132    if (!p_psEOCFinalize()) {
     133        psError(PS_ERR_UNKNOWN, false,
    64134                PS_ERRORTEXT_psConfigure_FINALIZATION_FAILED, "psEOC");
    65135        return;
    66136    }
     137
     138    // Free the trace system
     139    psTraceReset();
     140
     141    // Free the error system
     142    psErrorClear();
     143
    67144}
Note: See TracChangeset for help on using the changeset viewer.