000001  /*
000002  ** 2008 June 18
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** This module implements the sqlite3_status() interface and related
000014  ** functionality.
000015  */
000016  #include "sqliteInt.h"
000017  #include "vdbeInt.h"
000018  
000019  /*
000020  ** Variables in which to record status information.
000021  */
000022  #if SQLITE_PTRSIZE>4
000023  typedef sqlite3_int64 sqlite3StatValueType;
000024  #else
000025  typedef u32 sqlite3StatValueType;
000026  #endif
000027  typedef struct sqlite3StatType sqlite3StatType;
000028  static SQLITE_WSD struct sqlite3StatType {
000029    sqlite3StatValueType nowValue[10];  /* Current value */
000030    sqlite3StatValueType mxValue[10];   /* Maximum value */
000031  } sqlite3Stat = { {0,}, {0,} };
000032  
000033  /*
000034  ** Elements of sqlite3Stat[] are protected by either the memory allocator
000035  ** mutex, or by the pcache1 mutex.  The following array determines which.
000036  */
000037  static const char statMutex[] = {
000038    0,  /* SQLITE_STATUS_MEMORY_USED */
000039    1,  /* SQLITE_STATUS_PAGECACHE_USED */
000040    1,  /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
000041    0,  /* SQLITE_STATUS_SCRATCH_USED */
000042    0,  /* SQLITE_STATUS_SCRATCH_OVERFLOW */
000043    0,  /* SQLITE_STATUS_MALLOC_SIZE */
000044    0,  /* SQLITE_STATUS_PARSER_STACK */
000045    1,  /* SQLITE_STATUS_PAGECACHE_SIZE */
000046    0,  /* SQLITE_STATUS_SCRATCH_SIZE */
000047    0,  /* SQLITE_STATUS_MALLOC_COUNT */
000048  };
000049  
000050  
000051  /* The "wsdStat" macro will resolve to the status information
000052  ** state vector.  If writable static data is unsupported on the target,
000053  ** we have to locate the state vector at run-time.  In the more common
000054  ** case where writable static data is supported, wsdStat can refer directly
000055  ** to the "sqlite3Stat" state vector declared above.
000056  */
000057  #ifdef SQLITE_OMIT_WSD
000058  # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
000059  # define wsdStat x[0]
000060  #else
000061  # define wsdStatInit
000062  # define wsdStat sqlite3Stat
000063  #endif
000064  
000065  /*
000066  ** Return the current value of a status parameter.  The caller must
000067  ** be holding the appropriate mutex.
000068  */
000069  sqlite3_int64 sqlite3StatusValue(int op){
000070    wsdStatInit;
000071    assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
000072    assert( op>=0 && op<ArraySize(statMutex) );
000073    assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
000074                                             : sqlite3MallocMutex()) );
000075    return wsdStat.nowValue[op];
000076  }
000077  
000078  /*
000079  ** Add N to the value of a status record.  The caller must hold the
000080  ** appropriate mutex.  (Locking is checked by assert()).
000081  **
000082  ** The StatusUp() routine can accept positive or negative values for N.
000083  ** The value of N is added to the current status value and the high-water
000084  ** mark is adjusted if necessary.
000085  **
000086  ** The StatusDown() routine lowers the current value by N.  The highwater
000087  ** mark is unchanged.  N must be non-negative for StatusDown().
000088  */
000089  void sqlite3StatusUp(int op, int N){
000090    wsdStatInit;
000091    assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
000092    assert( op>=0 && op<ArraySize(statMutex) );
000093    assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
000094                                             : sqlite3MallocMutex()) );
000095    wsdStat.nowValue[op] += N;
000096    if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
000097      wsdStat.mxValue[op] = wsdStat.nowValue[op];
000098    }
000099  }
000100  void sqlite3StatusDown(int op, int N){
000101    wsdStatInit;
000102    assert( N>=0 );
000103    assert( op>=0 && op<ArraySize(statMutex) );
000104    assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
000105                                             : sqlite3MallocMutex()) );
000106    assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
000107    wsdStat.nowValue[op] -= N;
000108  }
000109  
000110  /*
000111  ** Adjust the highwater mark if necessary.
000112  ** The caller must hold the appropriate mutex.
000113  */
000114  void sqlite3StatusHighwater(int op, int X){
000115    sqlite3StatValueType newValue;
000116    wsdStatInit;
000117    assert( X>=0 );
000118    newValue = (sqlite3StatValueType)X;
000119    assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
000120    assert( op>=0 && op<ArraySize(statMutex) );
000121    assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
000122                                             : sqlite3MallocMutex()) );
000123    assert( op==SQLITE_STATUS_MALLOC_SIZE
000124            || op==SQLITE_STATUS_PAGECACHE_SIZE
000125            || op==SQLITE_STATUS_PARSER_STACK );
000126    if( newValue>wsdStat.mxValue[op] ){
000127      wsdStat.mxValue[op] = newValue;
000128    }
000129  }
000130  
000131  /*
000132  ** Query status information.
000133  */
000134  int sqlite3_status64(
000135    int op,
000136    sqlite3_int64 *pCurrent,
000137    sqlite3_int64 *pHighwater,
000138    int resetFlag
000139  ){
000140    sqlite3_mutex *pMutex;
000141    wsdStatInit;
000142    if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
000143      return SQLITE_MISUSE_BKPT;
000144    }
000145  #ifdef SQLITE_ENABLE_API_ARMOR
000146    if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
000147  #endif
000148    pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
000149    sqlite3_mutex_enter(pMutex);
000150    *pCurrent = wsdStat.nowValue[op];
000151    *pHighwater = wsdStat.mxValue[op];
000152    if( resetFlag ){
000153      wsdStat.mxValue[op] = wsdStat.nowValue[op];
000154    }
000155    sqlite3_mutex_leave(pMutex);
000156    (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
000157    return SQLITE_OK;
000158  }
000159  int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
000160    sqlite3_int64 iCur = 0, iHwtr = 0;
000161    int rc;
000162  #ifdef SQLITE_ENABLE_API_ARMOR
000163    if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
000164  #endif
000165    rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
000166    if( rc==0 ){
000167      *pCurrent = (int)iCur;
000168      *pHighwater = (int)iHwtr;
000169    }
000170    return rc;
000171  }
000172  
000173  /*
000174  ** Return the number of LookasideSlot elements on the linked list
000175  */
000176  static u32 countLookasideSlots(LookasideSlot *p){
000177    u32 cnt = 0;
000178    while( p ){
000179      p = p->pNext;
000180      cnt++;
000181    }
000182    return cnt;
000183  }
000184  
000185  /*
000186  ** Count the number of slots of lookaside memory that are outstanding
000187  */
000188  int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
000189    u32 nInit = countLookasideSlots(db->lookaside.pInit);
000190    u32 nFree = countLookasideSlots(db->lookaside.pFree);
000191  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000192    nInit += countLookasideSlots(db->lookaside.pSmallInit);
000193    nFree += countLookasideSlots(db->lookaside.pSmallFree);
000194  #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
000195    if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
000196    return db->lookaside.nSlot - (nInit+nFree);
000197  }
000198  
000199  /*
000200  ** Query status information for a single database connection
000201  */
000202  int sqlite3_db_status(
000203    sqlite3 *db,          /* The database connection whose status is desired */
000204    int op,               /* Status verb */
000205    int *pCurrent,        /* Write current value here */
000206    int *pHighwater,      /* Write high-water mark here */
000207    int resetFlag         /* Reset high-water mark if true */
000208  ){
000209    int rc = SQLITE_OK;   /* Return code */
000210  #ifdef SQLITE_ENABLE_API_ARMOR
000211    if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
000212      return SQLITE_MISUSE_BKPT;
000213    }
000214  #endif
000215    sqlite3_mutex_enter(db->mutex);
000216    switch( op ){
000217      case SQLITE_DBSTATUS_LOOKASIDE_USED: {
000218        *pCurrent = sqlite3LookasideUsed(db, pHighwater);
000219        if( resetFlag ){
000220          LookasideSlot *p = db->lookaside.pFree;
000221          if( p ){
000222            while( p->pNext ) p = p->pNext;
000223            p->pNext = db->lookaside.pInit;
000224            db->lookaside.pInit = db->lookaside.pFree;
000225            db->lookaside.pFree = 0;
000226          }
000227  #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
000228          p = db->lookaside.pSmallFree;
000229          if( p ){
000230            while( p->pNext ) p = p->pNext;
000231            p->pNext = db->lookaside.pSmallInit;
000232            db->lookaside.pSmallInit = db->lookaside.pSmallFree;
000233            db->lookaside.pSmallFree = 0;
000234          }
000235  #endif
000236        }
000237        break;
000238      }
000239  
000240      case SQLITE_DBSTATUS_LOOKASIDE_HIT:
000241      case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
000242      case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
000243        testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
000244        testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
000245        testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
000246        assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
000247        assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
000248        *pCurrent = 0;
000249        *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
000250        if( resetFlag ){
000251          db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
000252        }
000253        break;
000254      }
000255  
000256      /* 
000257      ** Return an approximation for the amount of memory currently used
000258      ** by all pagers associated with the given database connection.  The
000259      ** highwater mark is meaningless and is returned as zero.
000260      */
000261      case SQLITE_DBSTATUS_CACHE_USED_SHARED:
000262      case SQLITE_DBSTATUS_CACHE_USED: {
000263        int totalUsed = 0;
000264        int i;
000265        sqlite3BtreeEnterAll(db);
000266        for(i=0; i<db->nDb; i++){
000267          Btree *pBt = db->aDb[i].pBt;
000268          if( pBt ){
000269            Pager *pPager = sqlite3BtreePager(pBt);
000270            int nByte = sqlite3PagerMemUsed(pPager);
000271            if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
000272              nByte = nByte / sqlite3BtreeConnectionCount(pBt);
000273            }
000274            totalUsed += nByte;
000275          }
000276        }
000277        sqlite3BtreeLeaveAll(db);
000278        *pCurrent = totalUsed;
000279        *pHighwater = 0;
000280        break;
000281      }
000282  
000283      /*
000284      ** *pCurrent gets an accurate estimate of the amount of memory used
000285      ** to store the schema for all databases (main, temp, and any ATTACHed
000286      ** databases.  *pHighwater is set to zero.
000287      */
000288      case SQLITE_DBSTATUS_SCHEMA_USED: {
000289        int i;                      /* Used to iterate through schemas */
000290        int nByte = 0;              /* Used to accumulate return value */
000291  
000292        sqlite3BtreeEnterAll(db);
000293        db->pnBytesFreed = &nByte;
000294        assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
000295        db->lookaside.pEnd = db->lookaside.pStart;
000296        for(i=0; i<db->nDb; i++){
000297          Schema *pSchema = db->aDb[i].pSchema;
000298          if( ALWAYS(pSchema!=0) ){
000299            HashElem *p;
000300  
000301            nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
000302                pSchema->tblHash.count 
000303              + pSchema->trigHash.count
000304              + pSchema->idxHash.count
000305              + pSchema->fkeyHash.count
000306            );
000307            nByte += sqlite3_msize(pSchema->tblHash.ht);
000308            nByte += sqlite3_msize(pSchema->trigHash.ht);
000309            nByte += sqlite3_msize(pSchema->idxHash.ht);
000310            nByte += sqlite3_msize(pSchema->fkeyHash.ht);
000311  
000312            for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
000313              sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
000314            }
000315            for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
000316              sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
000317            }
000318          }
000319        }
000320        db->pnBytesFreed = 0;
000321        db->lookaside.pEnd = db->lookaside.pTrueEnd;
000322        sqlite3BtreeLeaveAll(db);
000323  
000324        *pHighwater = 0;
000325        *pCurrent = nByte;
000326        break;
000327      }
000328  
000329      /*
000330      ** *pCurrent gets an accurate estimate of the amount of memory used
000331      ** to store all prepared statements.
000332      ** *pHighwater is set to zero.
000333      */
000334      case SQLITE_DBSTATUS_STMT_USED: {
000335        struct Vdbe *pVdbe;         /* Used to iterate through VMs */
000336        int nByte = 0;              /* Used to accumulate return value */
000337  
000338        db->pnBytesFreed = &nByte;
000339        assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
000340        db->lookaside.pEnd = db->lookaside.pStart;
000341        for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
000342          sqlite3VdbeDelete(pVdbe);
000343        }
000344        db->lookaside.pEnd = db->lookaside.pTrueEnd;
000345        db->pnBytesFreed = 0;
000346  
000347        *pHighwater = 0;  /* IMP: R-64479-57858 */
000348        *pCurrent = nByte;
000349  
000350        break;
000351      }
000352  
000353      /*
000354      ** Set *pCurrent to the total cache hits or misses encountered by all
000355      ** pagers the database handle is connected to. *pHighwater is always set 
000356      ** to zero.
000357      */
000358      case SQLITE_DBSTATUS_CACHE_SPILL:
000359        op = SQLITE_DBSTATUS_CACHE_WRITE+1;
000360        /* no break */ deliberate_fall_through
000361      case SQLITE_DBSTATUS_CACHE_HIT:
000362      case SQLITE_DBSTATUS_CACHE_MISS:
000363      case SQLITE_DBSTATUS_CACHE_WRITE:{
000364        int i;
000365        int nRet = 0;
000366        assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
000367        assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
000368  
000369        for(i=0; i<db->nDb; i++){
000370          if( db->aDb[i].pBt ){
000371            Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
000372            sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
000373          }
000374        }
000375        *pHighwater = 0; /* IMP: R-42420-56072 */
000376                         /* IMP: R-54100-20147 */
000377                         /* IMP: R-29431-39229 */
000378        *pCurrent = nRet;
000379        break;
000380      }
000381  
000382      /* Set *pCurrent to non-zero if there are unresolved deferred foreign
000383      ** key constraints.  Set *pCurrent to zero if all foreign key constraints
000384      ** have been satisfied.  The *pHighwater is always set to zero.
000385      */
000386      case SQLITE_DBSTATUS_DEFERRED_FKS: {
000387        *pHighwater = 0;  /* IMP: R-11967-56545 */
000388        *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
000389        break;
000390      }
000391  
000392      default: {
000393        rc = SQLITE_ERROR;
000394      }
000395    }
000396    sqlite3_mutex_leave(db->mutex);
000397    return rc;
000398  }