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