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 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit; 000192 return db->lookaside.nSlot - (nInit+nFree); 000193 } 000194 000195 /* 000196 ** Query status information for a single database connection 000197 */ 000198 int sqlite3_db_status( 000199 sqlite3 *db, /* The database connection whose status is desired */ 000200 int op, /* Status verb */ 000201 int *pCurrent, /* Write current value here */ 000202 int *pHighwater, /* Write high-water mark here */ 000203 int resetFlag /* Reset high-water mark if true */ 000204 ){ 000205 int rc = SQLITE_OK; /* Return code */ 000206 #ifdef SQLITE_ENABLE_API_ARMOR 000207 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ 000208 return SQLITE_MISUSE_BKPT; 000209 } 000210 #endif 000211 sqlite3_mutex_enter(db->mutex); 000212 switch( op ){ 000213 case SQLITE_DBSTATUS_LOOKASIDE_USED: { 000214 *pCurrent = sqlite3LookasideUsed(db, pHighwater); 000215 if( resetFlag ){ 000216 LookasideSlot *p = db->lookaside.pFree; 000217 if( p ){ 000218 while( p->pNext ) p = p->pNext; 000219 p->pNext = db->lookaside.pInit; 000220 db->lookaside.pInit = db->lookaside.pFree; 000221 db->lookaside.pFree = 0; 000222 } 000223 } 000224 break; 000225 } 000226 000227 case SQLITE_DBSTATUS_LOOKASIDE_HIT: 000228 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE: 000229 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: { 000230 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT ); 000231 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); 000232 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); 000233 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); 000234 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); 000235 *pCurrent = 0; 000236 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; 000237 if( resetFlag ){ 000238 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; 000239 } 000240 break; 000241 } 000242 000243 /* 000244 ** Return an approximation for the amount of memory currently used 000245 ** by all pagers associated with the given database connection. The 000246 ** highwater mark is meaningless and is returned as zero. 000247 */ 000248 case SQLITE_DBSTATUS_CACHE_USED_SHARED: 000249 case SQLITE_DBSTATUS_CACHE_USED: { 000250 int totalUsed = 0; 000251 int i; 000252 sqlite3BtreeEnterAll(db); 000253 for(i=0; i<db->nDb; i++){ 000254 Btree *pBt = db->aDb[i].pBt; 000255 if( pBt ){ 000256 Pager *pPager = sqlite3BtreePager(pBt); 000257 int nByte = sqlite3PagerMemUsed(pPager); 000258 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){ 000259 nByte = nByte / sqlite3BtreeConnectionCount(pBt); 000260 } 000261 totalUsed += nByte; 000262 } 000263 } 000264 sqlite3BtreeLeaveAll(db); 000265 *pCurrent = totalUsed; 000266 *pHighwater = 0; 000267 break; 000268 } 000269 000270 /* 000271 ** *pCurrent gets an accurate estimate of the amount of memory used 000272 ** to store the schema for all databases (main, temp, and any ATTACHed 000273 ** databases. *pHighwater is set to zero. 000274 */ 000275 case SQLITE_DBSTATUS_SCHEMA_USED: { 000276 int i; /* Used to iterate through schemas */ 000277 int nByte = 0; /* Used to accumulate return value */ 000278 000279 sqlite3BtreeEnterAll(db); 000280 db->pnBytesFreed = &nByte; 000281 for(i=0; i<db->nDb; i++){ 000282 Schema *pSchema = db->aDb[i].pSchema; 000283 if( ALWAYS(pSchema!=0) ){ 000284 HashElem *p; 000285 000286 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( 000287 pSchema->tblHash.count 000288 + pSchema->trigHash.count 000289 + pSchema->idxHash.count 000290 + pSchema->fkeyHash.count 000291 ); 000292 nByte += sqlite3_msize(pSchema->tblHash.ht); 000293 nByte += sqlite3_msize(pSchema->trigHash.ht); 000294 nByte += sqlite3_msize(pSchema->idxHash.ht); 000295 nByte += sqlite3_msize(pSchema->fkeyHash.ht); 000296 000297 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ 000298 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); 000299 } 000300 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ 000301 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); 000302 } 000303 } 000304 } 000305 db->pnBytesFreed = 0; 000306 sqlite3BtreeLeaveAll(db); 000307 000308 *pHighwater = 0; 000309 *pCurrent = nByte; 000310 break; 000311 } 000312 000313 /* 000314 ** *pCurrent gets an accurate estimate of the amount of memory used 000315 ** to store all prepared statements. 000316 ** *pHighwater is set to zero. 000317 */ 000318 case SQLITE_DBSTATUS_STMT_USED: { 000319 struct Vdbe *pVdbe; /* Used to iterate through VMs */ 000320 int nByte = 0; /* Used to accumulate return value */ 000321 000322 db->pnBytesFreed = &nByte; 000323 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ 000324 sqlite3VdbeClearObject(db, pVdbe); 000325 sqlite3DbFree(db, pVdbe); 000326 } 000327 db->pnBytesFreed = 0; 000328 000329 *pHighwater = 0; /* IMP: R-64479-57858 */ 000330 *pCurrent = nByte; 000331 000332 break; 000333 } 000334 000335 /* 000336 ** Set *pCurrent to the total cache hits or misses encountered by all 000337 ** pagers the database handle is connected to. *pHighwater is always set 000338 ** to zero. 000339 */ 000340 case SQLITE_DBSTATUS_CACHE_SPILL: 000341 op = SQLITE_DBSTATUS_CACHE_WRITE+1; 000342 /* Fall through into the next case */ 000343 case SQLITE_DBSTATUS_CACHE_HIT: 000344 case SQLITE_DBSTATUS_CACHE_MISS: 000345 case SQLITE_DBSTATUS_CACHE_WRITE:{ 000346 int i; 000347 int nRet = 0; 000348 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); 000349 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); 000350 000351 for(i=0; i<db->nDb; i++){ 000352 if( db->aDb[i].pBt ){ 000353 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); 000354 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); 000355 } 000356 } 000357 *pHighwater = 0; /* IMP: R-42420-56072 */ 000358 /* IMP: R-54100-20147 */ 000359 /* IMP: R-29431-39229 */ 000360 *pCurrent = nRet; 000361 break; 000362 } 000363 000364 /* Set *pCurrent to non-zero if there are unresolved deferred foreign 000365 ** key constraints. Set *pCurrent to zero if all foreign key constraints 000366 ** have been satisfied. The *pHighwater is always set to zero. 000367 */ 000368 case SQLITE_DBSTATUS_DEFERRED_FKS: { 000369 *pHighwater = 0; /* IMP: R-11967-56545 */ 000370 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; 000371 break; 000372 } 000373 000374 default: { 000375 rc = SQLITE_ERROR; 000376 } 000377 } 000378 sqlite3_mutex_leave(db->mutex); 000379 return rc; 000380 }