1 /* 2 ** 2008 June 18 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This module implements the sqlite3_status() interface and related 14 ** functionality. 15 */ 16 #include "sqliteInt.h" 17 #include "vdbeInt.h" 18 19 /* 20 ** Variables in which to record status information. 21 */ 22 typedef struct sqlite3StatType sqlite3StatType; 23 static SQLITE_WSD struct sqlite3StatType { 24 #if SQLITE_PTRSIZE>4 25 sqlite3_int64 nowValue[10]; /* Current value */ 26 sqlite3_int64 mxValue[10]; /* Maximum value */ 27 #else 28 u32 nowValue[10]; /* Current value */ 29 u32 mxValue[10]; /* Maximum value */ 30 #endif 31 } sqlite3Stat = { {0,}, {0,} }; 32 33 /* 34 ** Elements of sqlite3Stat[] are protected by either the memory allocator 35 ** mutex, or by the pcache1 mutex. The following array determines which. 36 */ 37 static const char statMutex[] = { 38 0, /* SQLITE_STATUS_MEMORY_USED */ 39 1, /* SQLITE_STATUS_PAGECACHE_USED */ 40 1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */ 41 0, /* SQLITE_STATUS_SCRATCH_USED */ 42 0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */ 43 0, /* SQLITE_STATUS_MALLOC_SIZE */ 44 0, /* SQLITE_STATUS_PARSER_STACK */ 45 1, /* SQLITE_STATUS_PAGECACHE_SIZE */ 46 0, /* SQLITE_STATUS_SCRATCH_SIZE */ 47 0, /* SQLITE_STATUS_MALLOC_COUNT */ 48 }; 49 50 51 /* The "wsdStat" macro will resolve to the status information 52 ** state vector. If writable static data is unsupported on the target, 53 ** we have to locate the state vector at run-time. In the more common 54 ** case where writable static data is supported, wsdStat can refer directly 55 ** to the "sqlite3Stat" state vector declared above. 56 */ 57 #ifdef SQLITE_OMIT_WSD 58 # define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat) 59 # define wsdStat x[0] 60 #else 61 # define wsdStatInit 62 # define wsdStat sqlite3Stat 63 #endif 64 65 /* 66 ** Return the current value of a status parameter. The caller must 67 ** be holding the appropriate mutex. 68 */ 69 sqlite3_int64 sqlite3StatusValue(int op){ 70 wsdStatInit; 71 assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); 72 assert( op>=0 && op<ArraySize(statMutex) ); 73 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() 74 : sqlite3MallocMutex()) ); 75 return wsdStat.nowValue[op]; 76 } 77 78 /* 79 ** Add N to the value of a status record. The caller must hold the 80 ** appropriate mutex. (Locking is checked by assert()). 81 ** 82 ** The StatusUp() routine can accept positive or negative values for N. 83 ** The value of N is added to the current status value and the high-water 84 ** mark is adjusted if necessary. 85 ** 86 ** The StatusDown() routine lowers the current value by N. The highwater 87 ** mark is unchanged. N must be non-negative for StatusDown(). 88 */ 89 void sqlite3StatusUp(int op, int N){ 90 wsdStatInit; 91 assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); 92 assert( op>=0 && op<ArraySize(statMutex) ); 93 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() 94 : sqlite3MallocMutex()) ); 95 wsdStat.nowValue[op] += N; 96 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ 97 wsdStat.mxValue[op] = wsdStat.nowValue[op]; 98 } 99 } 100 void sqlite3StatusDown(int op, int N){ 101 wsdStatInit; 102 assert( N>=0 ); 103 assert( op>=0 && op<ArraySize(statMutex) ); 104 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() 105 : sqlite3MallocMutex()) ); 106 assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); 107 wsdStat.nowValue[op] -= N; 108 } 109 110 /* 111 ** Set the value of a status to X. The highwater mark is adjusted if 112 ** necessary. The caller must hold the appropriate mutex. 113 */ 114 void sqlite3StatusSet(int op, int X){ 115 wsdStatInit; 116 assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); 117 assert( op>=0 && op<ArraySize(statMutex) ); 118 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() 119 : sqlite3MallocMutex()) ); 120 wsdStat.nowValue[op] = X; 121 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ 122 wsdStat.mxValue[op] = wsdStat.nowValue[op]; 123 } 124 } 125 126 /* 127 ** Query status information. 128 */ 129 int sqlite3_status64( 130 int op, 131 sqlite3_int64 *pCurrent, 132 sqlite3_int64 *pHighwater, 133 int resetFlag 134 ){ 135 wsdStatInit; 136 sqlite3_mutex *pMutex; 137 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ 138 return SQLITE_MISUSE_BKPT; 139 } 140 #ifdef SQLITE_ENABLE_API_ARMOR 141 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; 142 #endif 143 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex(); 144 sqlite3_mutex_enter(pMutex); 145 *pCurrent = wsdStat.nowValue[op]; 146 *pHighwater = wsdStat.mxValue[op]; 147 if( resetFlag ){ 148 wsdStat.mxValue[op] = wsdStat.nowValue[op]; 149 } 150 sqlite3_mutex_leave(pMutex); 151 return SQLITE_OK; 152 } 153 int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ 154 sqlite3_int64 iCur, iHwtr; 155 int rc; 156 #ifdef SQLITE_ENABLE_API_ARMOR 157 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; 158 #endif 159 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag); 160 if( rc==0 ){ 161 *pCurrent = (int)iCur; 162 *pHighwater = (int)iHwtr; 163 } 164 return rc; 165 } 166 167 /* 168 ** Query status information for a single database connection 169 */ 170 int sqlite3_db_status( 171 sqlite3 *db, /* The database connection whose status is desired */ 172 int op, /* Status verb */ 173 int *pCurrent, /* Write current value here */ 174 int *pHighwater, /* Write high-water mark here */ 175 int resetFlag /* Reset high-water mark if true */ 176 ){ 177 int rc = SQLITE_OK; /* Return code */ 178 #ifdef SQLITE_ENABLE_API_ARMOR 179 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){ 180 return SQLITE_MISUSE_BKPT; 181 } 182 #endif 183 sqlite3_mutex_enter(db->mutex); 184 switch( op ){ 185 case SQLITE_DBSTATUS_LOOKASIDE_USED: { 186 *pCurrent = db->lookaside.nOut; 187 *pHighwater = db->lookaside.mxOut; 188 if( resetFlag ){ 189 db->lookaside.mxOut = db->lookaside.nOut; 190 } 191 break; 192 } 193 194 case SQLITE_DBSTATUS_LOOKASIDE_HIT: 195 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE: 196 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: { 197 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT ); 198 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); 199 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); 200 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); 201 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); 202 *pCurrent = 0; 203 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; 204 if( resetFlag ){ 205 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; 206 } 207 break; 208 } 209 210 /* 211 ** Return an approximation for the amount of memory currently used 212 ** by all pagers associated with the given database connection. The 213 ** highwater mark is meaningless and is returned as zero. 214 */ 215 case SQLITE_DBSTATUS_CACHE_USED: { 216 int totalUsed = 0; 217 int i; 218 sqlite3BtreeEnterAll(db); 219 for(i=0; i<db->nDb; i++){ 220 Btree *pBt = db->aDb[i].pBt; 221 if( pBt ){ 222 Pager *pPager = sqlite3BtreePager(pBt); 223 totalUsed += sqlite3PagerMemUsed(pPager); 224 } 225 } 226 sqlite3BtreeLeaveAll(db); 227 *pCurrent = totalUsed; 228 *pHighwater = 0; 229 break; 230 } 231 232 /* 233 ** *pCurrent gets an accurate estimate of the amount of memory used 234 ** to store the schema for all databases (main, temp, and any ATTACHed 235 ** databases. *pHighwater is set to zero. 236 */ 237 case SQLITE_DBSTATUS_SCHEMA_USED: { 238 int i; /* Used to iterate through schemas */ 239 int nByte = 0; /* Used to accumulate return value */ 240 241 sqlite3BtreeEnterAll(db); 242 db->pnBytesFreed = &nByte; 243 for(i=0; i<db->nDb; i++){ 244 Schema *pSchema = db->aDb[i].pSchema; 245 if( ALWAYS(pSchema!=0) ){ 246 HashElem *p; 247 248 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( 249 pSchema->tblHash.count 250 + pSchema->trigHash.count 251 + pSchema->idxHash.count 252 + pSchema->fkeyHash.count 253 ); 254 nByte += sqlite3MallocSize(pSchema->tblHash.ht); 255 nByte += sqlite3MallocSize(pSchema->trigHash.ht); 256 nByte += sqlite3MallocSize(pSchema->idxHash.ht); 257 nByte += sqlite3MallocSize(pSchema->fkeyHash.ht); 258 259 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ 260 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); 261 } 262 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ 263 sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); 264 } 265 } 266 } 267 db->pnBytesFreed = 0; 268 sqlite3BtreeLeaveAll(db); 269 270 *pHighwater = 0; 271 *pCurrent = nByte; 272 break; 273 } 274 275 /* 276 ** *pCurrent gets an accurate estimate of the amount of memory used 277 ** to store all prepared statements. 278 ** *pHighwater is set to zero. 279 */ 280 case SQLITE_DBSTATUS_STMT_USED: { 281 struct Vdbe *pVdbe; /* Used to iterate through VMs */ 282 int nByte = 0; /* Used to accumulate return value */ 283 284 db->pnBytesFreed = &nByte; 285 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ 286 sqlite3VdbeClearObject(db, pVdbe); 287 sqlite3DbFree(db, pVdbe); 288 } 289 db->pnBytesFreed = 0; 290 291 *pHighwater = 0; /* IMP: R-64479-57858 */ 292 *pCurrent = nByte; 293 294 break; 295 } 296 297 /* 298 ** Set *pCurrent to the total cache hits or misses encountered by all 299 ** pagers the database handle is connected to. *pHighwater is always set 300 ** to zero. 301 */ 302 case SQLITE_DBSTATUS_CACHE_HIT: 303 case SQLITE_DBSTATUS_CACHE_MISS: 304 case SQLITE_DBSTATUS_CACHE_WRITE:{ 305 int i; 306 int nRet = 0; 307 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); 308 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); 309 310 for(i=0; i<db->nDb; i++){ 311 if( db->aDb[i].pBt ){ 312 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); 313 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); 314 } 315 } 316 *pHighwater = 0; /* IMP: R-42420-56072 */ 317 /* IMP: R-54100-20147 */ 318 /* IMP: R-29431-39229 */ 319 *pCurrent = nRet; 320 break; 321 } 322 323 /* Set *pCurrent to non-zero if there are unresolved deferred foreign 324 ** key constraints. Set *pCurrent to zero if all foreign key constraints 325 ** have been satisfied. The *pHighwater is always set to zero. 326 */ 327 case SQLITE_DBSTATUS_DEFERRED_FKS: { 328 *pHighwater = 0; /* IMP: R-11967-56545 */ 329 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; 330 break; 331 } 332 333 default: { 334 rc = SQLITE_ERROR; 335 } 336 } 337 sqlite3_mutex_leave(db->mutex); 338 return rc; 339 } 340