sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | #if SQLITE_PTRSIZE>4 |
| 23 | typedef sqlite3_int64 sqlite3StatValueType; |
| 24 | #else |
| 25 | typedef u32 sqlite3StatValueType; |
| 26 | #endif |
| 27 | typedef struct sqlite3StatType sqlite3StatType; |
| 28 | static SQLITE_WSD struct sqlite3StatType { |
| 29 | sqlite3StatValueType nowValue[10]; /* Current value */ |
| 30 | sqlite3StatValueType mxValue[10]; /* Maximum value */ |
| 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 | ** Adjust the highwater mark if necessary. |
| 112 | ** The caller must hold the appropriate mutex. |
| 113 | */ |
| 114 | void sqlite3StatusHighwater(int op, int X){ |
| 115 | sqlite3StatValueType newValue; |
| 116 | wsdStatInit; |
| 117 | assert( X>=0 ); |
| 118 | newValue = (sqlite3StatValueType)X; |
| 119 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 120 | assert( op>=0 && op<ArraySize(statMutex) ); |
| 121 | assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex() |
| 122 | : sqlite3MallocMutex()) ); |
| 123 | assert( op==SQLITE_STATUS_MALLOC_SIZE |
| 124 | || op==SQLITE_STATUS_PAGECACHE_SIZE |
| 125 | || op==SQLITE_STATUS_PARSER_STACK ); |
| 126 | if( newValue>wsdStat.mxValue[op] ){ |
| 127 | wsdStat.mxValue[op] = newValue; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | ** Query status information. |
| 133 | */ |
| 134 | int sqlite3_status64( |
| 135 | int op, |
| 136 | sqlite3_int64 *pCurrent, |
| 137 | sqlite3_int64 *pHighwater, |
| 138 | int resetFlag |
| 139 | ){ |
| 140 | sqlite3_mutex *pMutex; |
| 141 | wsdStatInit; |
| 142 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
| 143 | return SQLITE_MISUSE_BKPT; |
| 144 | } |
| 145 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 146 | if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 147 | #endif |
| 148 | pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex(); |
| 149 | sqlite3_mutex_enter(pMutex); |
| 150 | *pCurrent = wsdStat.nowValue[op]; |
| 151 | *pHighwater = wsdStat.mxValue[op]; |
| 152 | if( resetFlag ){ |
| 153 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
| 154 | } |
| 155 | sqlite3_mutex_leave(pMutex); |
| 156 | (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */ |
| 157 | return SQLITE_OK; |
| 158 | } |
| 159 | int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
| 160 | sqlite3_int64 iCur = 0, iHwtr = 0; |
| 161 | int rc; |
| 162 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 163 | if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT; |
| 164 | #endif |
| 165 | rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag); |
| 166 | if( rc==0 ){ |
| 167 | *pCurrent = (int)iCur; |
| 168 | *pHighwater = (int)iHwtr; |
| 169 | } |
| 170 | return rc; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Return the number of LookasideSlot elements on the linked list |
| 175 | */ |
| 176 | static u32 countLookasideSlots(LookasideSlot *p){ |
| 177 | u32 cnt = 0; |
| 178 | while( p ){ |
| 179 | p = p->pNext; |
| 180 | cnt++; |
| 181 | } |
| 182 | return cnt; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | ** Count the number of slots of lookaside memory that are outstanding |
| 187 | */ |
| 188 | int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){ |
| 189 | u32 nInit = countLookasideSlots(db->lookaside.pInit); |
| 190 | u32 nFree = countLookasideSlots(db->lookaside.pFree); |
| 191 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 192 | nInit += countLookasideSlots(db->lookaside.pSmallInit); |
| 193 | nFree += countLookasideSlots(db->lookaside.pSmallFree); |
| 194 | #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */ |
| 195 | assert( db->lookaside.nSlot >= nInit+nFree ); |
| 196 | if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit); |
| 197 | return (int)(db->lookaside.nSlot - (nInit+nFree)); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | ** Query status information for a single database connection |
| 202 | */ |
| 203 | int sqlite3_db_status64( |
| 204 | sqlite3 *db, /* The database connection whose status is desired */ |
| 205 | int op, /* Status verb */ |
| 206 | sqlite3_int64 *pCurrent, /* Write current value here */ |
| 207 | sqlite3_int64 *pHighwtr, /* Write high-water mark here */ |
| 208 | int resetFlag /* Reset high-water mark if true */ |
| 209 | ){ |
| 210 | int rc = SQLITE_OK; /* Return code */ |
| 211 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 212 | if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){ |
| 213 | return SQLITE_MISUSE_BKPT; |
| 214 | } |
| 215 | #endif |
| 216 | sqlite3_mutex_enter(db->mutex); |
| 217 | switch( op ){ |
| 218 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 219 | int H = 0; |
| 220 | *pCurrent = sqlite3LookasideUsed(db, &H); |
| 221 | *pHighwtr = H; |
| 222 | if( resetFlag ){ |
| 223 | LookasideSlot *p = db->lookaside.pFree; |
| 224 | if( p ){ |
| 225 | while( p->pNext ) p = p->pNext; |
| 226 | p->pNext = db->lookaside.pInit; |
| 227 | db->lookaside.pInit = db->lookaside.pFree; |
| 228 | db->lookaside.pFree = 0; |
| 229 | } |
| 230 | #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE |
| 231 | p = db->lookaside.pSmallFree; |
| 232 | if( p ){ |
| 233 | while( p->pNext ) p = p->pNext; |
| 234 | p->pNext = db->lookaside.pSmallInit; |
| 235 | db->lookaside.pSmallInit = db->lookaside.pSmallFree; |
| 236 | db->lookaside.pSmallFree = 0; |
| 237 | } |
| 238 | #endif |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | case SQLITE_DBSTATUS_LOOKASIDE_HIT: |
| 244 | case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE: |
| 245 | case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: { |
| 246 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT ); |
| 247 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); |
| 248 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); |
| 249 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); |
| 250 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); |
| 251 | *pCurrent = 0; |
| 252 | *pHighwtr = db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT]; |
| 253 | if( resetFlag ){ |
| 254 | db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; |
| 255 | } |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | ** Return an approximation for the amount of memory currently used |
| 261 | ** by all pagers associated with the given database connection. The |
| 262 | ** highwater mark is meaningless and is returned as zero. |
| 263 | */ |
| 264 | case SQLITE_DBSTATUS_CACHE_USED_SHARED: |
| 265 | case SQLITE_DBSTATUS_CACHE_USED: { |
| 266 | sqlite3_int64 totalUsed = 0; |
| 267 | int i; |
| 268 | sqlite3BtreeEnterAll(db); |
| 269 | for(i=0; i<db->nDb; i++){ |
| 270 | Btree *pBt = db->aDb[i].pBt; |
| 271 | if( pBt ){ |
| 272 | Pager *pPager = sqlite3BtreePager(pBt); |
| 273 | int nByte = sqlite3PagerMemUsed(pPager); |
| 274 | if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){ |
| 275 | nByte = nByte / sqlite3BtreeConnectionCount(pBt); |
| 276 | } |
| 277 | totalUsed += nByte; |
| 278 | } |
| 279 | } |
| 280 | sqlite3BtreeLeaveAll(db); |
| 281 | *pCurrent = totalUsed; |
| 282 | *pHighwtr = 0; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | ** *pCurrent gets an accurate estimate of the amount of memory used |
| 288 | ** to store the schema for all databases (main, temp, and any ATTACHed |
| 289 | ** databases. *pHighwtr is set to zero. |
| 290 | */ |
| 291 | case SQLITE_DBSTATUS_SCHEMA_USED: { |
| 292 | int i; /* Used to iterate through schemas */ |
| 293 | int nByte = 0; /* Used to accumulate return value */ |
| 294 | |
| 295 | sqlite3BtreeEnterAll(db); |
| 296 | db->pnBytesFreed = &nByte; |
| 297 | assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 298 | db->lookaside.pEnd = db->lookaside.pStart; |
| 299 | for(i=0; i<db->nDb; i++){ |
| 300 | Schema *pSchema = db->aDb[i].pSchema; |
| 301 | if( ALWAYS(pSchema!=0) ){ |
| 302 | HashElem *p; |
| 303 | |
| 304 | nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( |
| 305 | pSchema->tblHash.count |
| 306 | + pSchema->trigHash.count |
| 307 | + pSchema->idxHash.count |
| 308 | + pSchema->fkeyHash.count |
| 309 | ); |
| 310 | nByte += sqlite3_msize(pSchema->tblHash.ht); |
| 311 | nByte += sqlite3_msize(pSchema->trigHash.ht); |
| 312 | nByte += sqlite3_msize(pSchema->idxHash.ht); |
| 313 | nByte += sqlite3_msize(pSchema->fkeyHash.ht); |
| 314 | |
| 315 | for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ |
| 316 | sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); |
| 317 | } |
| 318 | for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 319 | sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | db->pnBytesFreed = 0; |
| 324 | db->lookaside.pEnd = db->lookaside.pTrueEnd; |
| 325 | sqlite3BtreeLeaveAll(db); |
| 326 | |
| 327 | *pHighwtr = 0; |
| 328 | *pCurrent = nByte; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | ** *pCurrent gets an accurate estimate of the amount of memory used |
| 334 | ** to store all prepared statements. |
| 335 | ** *pHighwtr is set to zero. |
| 336 | */ |
| 337 | case SQLITE_DBSTATUS_STMT_USED: { |
| 338 | struct Vdbe *pVdbe; /* Used to iterate through VMs */ |
| 339 | int nByte = 0; /* Used to accumulate return value */ |
| 340 | |
| 341 | db->pnBytesFreed = &nByte; |
| 342 | assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); |
| 343 | db->lookaside.pEnd = db->lookaside.pStart; |
| 344 | for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){ |
| 345 | sqlite3VdbeDelete(pVdbe); |
| 346 | } |
| 347 | db->lookaside.pEnd = db->lookaside.pTrueEnd; |
| 348 | db->pnBytesFreed = 0; |
| 349 | |
| 350 | *pHighwtr = 0; /* IMP: R-64479-57858 */ |
| 351 | *pCurrent = nByte; |
| 352 | |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | ** Set *pCurrent to the total cache hits or misses encountered by all |
| 358 | ** pagers the database handle is connected to. *pHighwtr is always set |
| 359 | ** to zero. |
| 360 | */ |
| 361 | case SQLITE_DBSTATUS_CACHE_SPILL: |
| 362 | op = SQLITE_DBSTATUS_CACHE_WRITE+1; |
| 363 | /* no break */ deliberate_fall_through |
| 364 | case SQLITE_DBSTATUS_CACHE_HIT: |
| 365 | case SQLITE_DBSTATUS_CACHE_MISS: |
| 366 | case SQLITE_DBSTATUS_CACHE_WRITE:{ |
| 367 | int i; |
| 368 | u64 nRet = 0; |
| 369 | assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); |
| 370 | assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); |
| 371 | |
| 372 | for(i=0; i<db->nDb; i++){ |
| 373 | if( db->aDb[i].pBt ){ |
| 374 | Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); |
| 375 | sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); |
| 376 | } |
| 377 | } |
| 378 | *pHighwtr = 0; /* IMP: R-42420-56072 */ |
| 379 | /* IMP: R-54100-20147 */ |
| 380 | /* IMP: R-29431-39229 */ |
| 381 | *pCurrent = nRet; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | /* Set *pCurrent to the number of bytes that the db database connection |
| 386 | ** has spilled to the filesystem in temporary files that could have been |
| 387 | ** stored in memory, had sufficient memory been available. |
| 388 | ** The *pHighwater is always set to zero. |
| 389 | */ |
| 390 | case SQLITE_DBSTATUS_TEMPBUF_SPILL: { |
| 391 | u64 nRet = 0; |
| 392 | if( db->aDb[1].pBt ){ |
| 393 | Pager *pPager = sqlite3BtreePager(db->aDb[1].pBt); |
| 394 | sqlite3PagerCacheStat(pPager, SQLITE_DBSTATUS_CACHE_WRITE, |
| 395 | resetFlag, &nRet); |
| 396 | nRet *= sqlite3BtreeGetPageSize(db->aDb[1].pBt); |
| 397 | } |
| 398 | nRet += db->nSpill; |
| 399 | if( resetFlag ) db->nSpill = 0; |
| 400 | *pHighwtr = 0; |
| 401 | *pCurrent = nRet; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | /* Set *pCurrent to non-zero if there are unresolved deferred foreign |
| 406 | ** key constraints. Set *pCurrent to zero if all foreign key constraints |
| 407 | ** have been satisfied. The *pHighwtr is always set to zero. |
| 408 | */ |
| 409 | case SQLITE_DBSTATUS_DEFERRED_FKS: { |
| 410 | *pHighwtr = 0; /* IMP: R-11967-56545 */ |
| 411 | *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0; |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | default: { |
| 416 | rc = SQLITE_ERROR; |
| 417 | } |
| 418 | } |
| 419 | sqlite3_mutex_leave(db->mutex); |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | ** 32-bit variant of sqlite3_db_status64() |
| 425 | */ |
| 426 | int sqlite3_db_status( |
| 427 | sqlite3 *db, /* The database connection whose status is desired */ |
| 428 | int op, /* Status verb */ |
| 429 | int *pCurrent, /* Write current value here */ |
| 430 | int *pHighwtr, /* Write high-water mark here */ |
| 431 | int resetFlag /* Reset high-water mark if true */ |
| 432 | ){ |
| 433 | sqlite3_int64 C = 0, H = 0; |
| 434 | int rc; |
| 435 | #ifdef SQLITE_ENABLE_API_ARMOR |
| 436 | if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwtr==0 ){ |
| 437 | return SQLITE_MISUSE_BKPT; |
| 438 | } |
| 439 | #endif |
| 440 | rc = sqlite3_db_status64(db, op, &C, &H, resetFlag); |
| 441 | if( rc==0 ){ |
| 442 | *pCurrent = C & 0x7fffffff; |
| 443 | *pHighwtr = H & 0x7fffffff; |
| 444 | } |
| 445 | return rc; |
| 446 | } |