/ Check-in [3e51696c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a malloc size histogram to the debugging malloc implementation in mem2.c. (CVS 4490)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3e51696cb878063e4ebfdcc2a61ba94c9bebdfe3
User & Date: drh 2007-10-15 19:34:32
Context
2007-10-16
19:45
A cosmetic changes to btree.c which (we are told) works around a bug in MSVC++. (CVS 4491) check-in: 329dd014 user: drh tags: trunk
2007-10-15
19:34
Add a malloc size histogram to the debugging malloc implementation in mem2.c. (CVS 4490) check-in: 3e51696c user: drh tags: trunk
07:08
Fix for OMIT_AUTHORIZATION builds. (CVS 4489) check-in: 260711a1 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/mem2.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains the C functions that implement a memory
    13     13   ** allocation subsystem for use by SQLite.  
    14     14   **
    15         -** $Id: mem2.c,v 1.14 2007/10/03 08:46:45 danielk1977 Exp $
           15  +** $Id: mem2.c,v 1.15 2007/10/15 19:34:32 drh Exp $
    16     16   */
    17     17   
    18     18   /*
    19     19   ** This version of the memory allocator is used only if the
    20     20   ** SQLITE_MEMDEBUG macro is defined and SQLITE_OMIT_MEMORY_ALLOCATION
    21     21   ** is not defined.
    22     22   */
................................................................................
    80     80   
    81     81   /*
    82     82   ** Guard words
    83     83   */
    84     84   #define FOREGUARD 0x80F5E153
    85     85   #define REARGUARD 0xE4676B53
    86     86   
           87  +/*
           88  +** Number of malloc size increments to track.
           89  +*/
           90  +#define NCSIZE  500
           91  +
    87     92   /*
    88     93   ** All of the static variables used by this module are collected
    89     94   ** into a single structure named "mem".  This is to keep the
    90     95   ** static variables organized and to reduce namespace pollution
    91     96   ** when this module is combined with other in the amalgamation.
    92     97   */
    93     98   static struct {
................................................................................
   145    150   
   146    151     /* 
   147    152     ** sqlite3MallocDisallow() increments the following counter.
   148    153     ** sqlite3MallocAllow() decrements it.
   149    154     */
   150    155     int disallow; /* Do not allow memory allocation */
   151    156     
          157  +  /*
          158  +  ** Gather statistics on the sizes of memory allocations.
          159  +  ** sizeCnt[i] is the number of allocation attempts of i*4
          160  +  ** bytes.  i==NCSIZE is the number of allocation attempts for
          161  +  ** sizes more than NCSIZE*4 bytes.
          162  +  */
          163  +  int sizeCnt[NCSIZE];
   152    164     
   153    165   } mem;
   154    166   
   155    167   
   156    168   /*
   157    169   ** Enter the mutex mem.mutex. Allocate it if it is not already allocated.
   158    170   */
................................................................................
   268    280     if( nByte>0 ){
   269    281       enterMem();
   270    282       assert( mem.disallow==0 );
   271    283       if( mem.alarmCallback!=0 && mem.nowUsed+nByte>=mem.alarmThreshold ){
   272    284         sqlite3MemsysAlarm(nByte);
   273    285       }
   274    286       nByte = (nByte+3)&~3;
          287  +    if( nByte/8>NCSIZE-1 ){
          288  +      mem.sizeCnt[NCSIZE-1]++;
          289  +    }else{
          290  +      mem.sizeCnt[nByte/8]++;
          291  +    }
   275    292       totalSize = nByte + sizeof(*pHdr) + sizeof(int) +
   276    293                    mem.nBacktrace*sizeof(void*) + mem.nTitle;
   277    294       if( mem.iFail>0 ){
   278    295         if( mem.iFail==1 ){
   279    296           p = 0;
   280    297           mem.iFail = mem.iReset;
   281    298           if( mem.iFailCnt==0 ){
................................................................................
   436    453   ** Open the file indicated and write a log of all unfreed memory 
   437    454   ** allocations into that log.
   438    455   */
   439    456   void sqlite3_memdebug_dump(const char *zFilename){
   440    457     FILE *out;
   441    458     struct MemBlockHdr *pHdr;
   442    459     void **pBt;
          460  +  int i;
   443    461     out = fopen(zFilename, "w");
   444    462     if( out==0 ){
   445    463       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
   446    464                       zFilename);
   447    465       return;
   448    466     }
   449    467     for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
................................................................................
   455    473         fflush(out);
   456    474         pBt = (void**)pHdr;
   457    475         pBt -= pHdr->nBacktraceSlots;
   458    476         backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
   459    477         fprintf(out, "\n");
   460    478       }
   461    479     }
          480  +  fprintf(out, "COUNTS:\n");
          481  +  for(i=0; i<NCSIZE-1; i++){
          482  +    if( mem.sizeCnt[i] ){
          483  +      fprintf(out, "   %3d: %d\n", i*8+8, mem.sizeCnt[i]);
          484  +    }
          485  +  }
          486  +  if( mem.sizeCnt[NCSIZE-1] ){
          487  +    fprintf(out, "  >%3d: %d\n", NCSIZE*8, mem.sizeCnt[NCSIZE-1]);
          488  +  }
   462    489     fclose(out);
   463    490   }
   464    491   
   465    492   /*
   466    493   ** This routine is used to simulate malloc failures.
   467    494   **
   468    495   ** After calling this routine, there will be iFail successful

Changes to test/tester.tcl.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements some common TCL routines used for regression
    12     12   # testing the SQLite library
    13     13   #
    14         -# $Id: tester.tcl,v 1.91 2007/09/01 09:02:54 danielk1977 Exp $
           14  +# $Id: tester.tcl,v 1.92 2007/10/15 19:34:32 drh Exp $
    15     15   
    16     16   
    17     17   set tcl_precision 15
    18     18   set sqlite_pending_byte 0x0010000
    19     19   
    20     20   # 
    21     21   # Check the command-line arguments for a default soft-heap-limit.
................................................................................
   199    199       incr nErr
   200    200       ifcapable memdebug {
   201    201         puts "Writing unfreed memory log to \"./memleak.txt\""
   202    202         sqlite3_memdebug_dump ./memleak.txt
   203    203       }
   204    204     } else {
   205    205       puts "All memory allocations freed - no leaks"
          206  +    ifcapable memdebug {
          207  +      sqlite3_memdebug_dump ./memusage.txt
          208  +    }
   206    209     }
   207    210     puts "Maximum memory usage: [sqlite3_memory_highwater] bytes"
   208    211     foreach f [glob -nocomplain test.db-*-journal] {
   209    212       file delete -force $f
   210    213     }
   211    214     foreach f [glob -nocomplain test.db-mj*] {
   212    215       file delete -force $f