Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: b0a47c7f854639c732894e3a5c37b4869a70c6d7 [file] [log] [blame]
drhc4b18b82008-06-19 13:20:011/*
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.
drhc4b18b82008-06-19 13:20:0115*/
16#include "sqliteInt.h"
dand46def72010-07-24 11:28:2817#include "vdbeInt.h"
drhc4b18b82008-06-19 13:20:0118
19/*
20** Variables in which to record status information.
21*/
mistachkin7ef855f2015-10-22 18:06:4022#if SQLITE_PTRSIZE>4
23typedef sqlite3_int64 sqlite3StatValueType;
24#else
25typedef u32 sqlite3StatValueType;
26#endif
drh78f82d12008-09-02 00:52:5227typedef struct sqlite3StatType sqlite3StatType;
28static SQLITE_WSD struct sqlite3StatType {
mistachkin7ef855f2015-10-22 18:06:4029 sqlite3StatValueType nowValue[10]; /* Current value */
30 sqlite3StatValueType mxValue[10]; /* Maximum value */
drh78f82d12008-09-02 00:52:5231} sqlite3Stat = { {0,}, {0,} };
drhc4b18b82008-06-19 13:20:0132
drhaf89fe62015-03-23 17:25:1833/*
34** Elements of sqlite3Stat[] are protected by either the memory allocator
35** mutex, or by the pcache1 mutex. The following array determines which.
36*/
37static 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
drhc4b18b82008-06-19 13:20:0150
drh78f82d12008-09-02 00:52:5251/* 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
drhc4b18b82008-06-19 13:20:0165/*
drhaf89fe62015-03-23 17:25:1866** Return the current value of a status parameter. The caller must
67** be holding the appropriate mutex.
drhc4b18b82008-06-19 13:20:0168*/
drhaf89fe62015-03-23 17:25:1869sqlite3_int64 sqlite3StatusValue(int op){
drh78f82d12008-09-02 00:52:5270 wsdStatInit;
71 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:1872 assert( op>=0 && op<ArraySize(statMutex) );
73 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
74 : sqlite3MallocMutex()) );
drh78f82d12008-09-02 00:52:5275 return wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:0176}
77
78/*
drhaf89fe62015-03-23 17:25:1879** 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().
drhc4b18b82008-06-19 13:20:0188*/
drhaf89fe62015-03-23 17:25:1889void sqlite3StatusUp(int op, int N){
drh78f82d12008-09-02 00:52:5290 wsdStatInit;
91 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:1892 assert( op>=0 && op<ArraySize(statMutex) );
93 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
94 : sqlite3MallocMutex()) );
drh78f82d12008-09-02 00:52:5295 wsdStat.nowValue[op] += N;
96 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
97 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:0198 }
99}
drhaf89fe62015-03-23 17:25:18100void 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}
drhc4b18b82008-06-19 13:20:01109
110/*
drhb02392e2015-10-15 15:28:56111** Adjust the highwater mark if necessary.
112** The caller must hold the appropriate mutex.
drhc4b18b82008-06-19 13:20:01113*/
drhb02392e2015-10-15 15:28:56114void sqlite3StatusHighwater(int op, int X){
mistachkin7ef855f2015-10-22 18:06:40115 sqlite3StatValueType newValue;
drh78f82d12008-09-02 00:52:52116 wsdStatInit;
mistachkin7ef855f2015-10-22 18:06:40117 assert( X>=0 );
118 newValue = (sqlite3StatValueType)X;
drh78f82d12008-09-02 00:52:52119 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:18120 assert( op>=0 && op<ArraySize(statMutex) );
121 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
122 : sqlite3MallocMutex()) );
drhb02392e2015-10-15 15:28:56123 assert( op==SQLITE_STATUS_MALLOC_SIZE
124 || op==SQLITE_STATUS_PAGECACHE_SIZE
drhb02392e2015-10-15 15:28:56125 || op==SQLITE_STATUS_PARSER_STACK );
mistachkin7ef855f2015-10-22 18:06:40126 if( newValue>wsdStat.mxValue[op] ){
127 wsdStat.mxValue[op] = newValue;
drhc4b18b82008-06-19 13:20:01128 }
129}
130
131/*
132** Query status information.
drhc4b18b82008-06-19 13:20:01133*/
drhaf89fe62015-03-23 17:25:18134int sqlite3_status64(
135 int op,
136 sqlite3_int64 *pCurrent,
137 sqlite3_int64 *pHighwater,
138 int resetFlag
139){
drhaf89fe62015-03-23 17:25:18140 sqlite3_mutex *pMutex;
drh24938702015-03-23 19:16:30141 wsdStatInit;
drh78f82d12008-09-02 00:52:52142 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
drh413c3d32010-02-23 20:11:56143 return SQLITE_MISUSE_BKPT;
drhc4b18b82008-06-19 13:20:01144 }
drh9ca95732014-10-24 00:35:58145#ifdef SQLITE_ENABLE_API_ARMOR
146 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
147#endif
drhaf89fe62015-03-23 17:25:18148 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
149 sqlite3_mutex_enter(pMutex);
drh78f82d12008-09-02 00:52:52150 *pCurrent = wsdStat.nowValue[op];
151 *pHighwater = wsdStat.mxValue[op];
drhc4b18b82008-06-19 13:20:01152 if( resetFlag ){
drh78f82d12008-09-02 00:52:52153 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01154 }
drhaf89fe62015-03-23 17:25:18155 sqlite3_mutex_leave(pMutex);
drh47564042015-03-24 18:19:39156 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
drhc4b18b82008-06-19 13:20:01157 return SQLITE_OK;
158}
drhaf89fe62015-03-23 17:25:18159int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
drh38b802d2016-08-02 08:45:26160 sqlite3_int64 iCur = 0, iHwtr = 0;
drhaf89fe62015-03-23 17:25:18161 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);
drh2b4905c2015-03-23 18:52:56166 if( rc==0 ){
167 *pCurrent = (int)iCur;
168 *pHighwater = (int)iHwtr;
169 }
drhaf89fe62015-03-23 17:25:18170 return rc;
171}
drh633e6d52008-07-28 19:34:53172
173/*
drh52fb8e12017-08-29 20:21:12174** Return the number of LookasideSlot elements on the linked list
175*/
176static 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*/
188int sqlite3LookasideUsed(sqlite3 *db, int *pHighwater){
189 u32 nInit = countLookasideSlots(db->lookaside.pInit);
190 u32 nFree = countLookasideSlots(db->lookaside.pFree);
drhcf014f62019-12-31 15:12:34191#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
192 nInit += countLookasideSlots(db->lookaside.pSmallInit);
193 nFree += countLookasideSlots(db->lookaside.pSmallFree);
194#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
drhce250072025-02-21 17:03:22195 assert( db->lookaside.nSlot >= nInit+nFree );
196 if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit);
197 return (int)(db->lookaside.nSlot - (nInit+nFree));
drh52fb8e12017-08-29 20:21:12198}
199
200/*
drh633e6d52008-07-28 19:34:53201** Query status information for a single database connection
202*/
203int sqlite3_db_status(
204 sqlite3 *db, /* The database connection whose status is desired */
205 int op, /* Status verb */
206 int *pCurrent, /* Write current value here */
207 int *pHighwater, /* Write high-water mark here */
208 int resetFlag /* Reset high-water mark if true */
209){
dan2339f062010-07-22 17:55:40210 int rc = SQLITE_OK; /* Return code */
drh9ca95732014-10-24 00:35:58211#ifdef SQLITE_ENABLE_API_ARMOR
212 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
213 return SQLITE_MISUSE_BKPT;
214 }
215#endif
dan2339f062010-07-22 17:55:40216 sqlite3_mutex_enter(db->mutex);
drh633e6d52008-07-28 19:34:53217 switch( op ){
218 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
drh52fb8e12017-08-29 20:21:12219 *pCurrent = sqlite3LookasideUsed(db, pHighwater);
drh633e6d52008-07-28 19:34:53220 if( resetFlag ){
drh52fb8e12017-08-29 20:21:12221 LookasideSlot *p = db->lookaside.pFree;
222 if( p ){
223 while( p->pNext ) p = p->pNext;
224 p->pNext = db->lookaside.pInit;
225 db->lookaside.pInit = db->lookaside.pFree;
226 db->lookaside.pFree = 0;
227 }
drhcf014f62019-12-31 15:12:34228#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
229 p = db->lookaside.pSmallFree;
numist115d6632019-12-12 02:50:07230 if( p ){
231 while( p->pNext ) p = p->pNext;
drhcf014f62019-12-31 15:12:34232 p->pNext = db->lookaside.pSmallInit;
233 db->lookaside.pSmallInit = db->lookaside.pSmallFree;
234 db->lookaside.pSmallFree = 0;
numist115d6632019-12-12 02:50:07235 }
236#endif
drh633e6d52008-07-28 19:34:53237 }
238 break;
239 }
drh63da0892010-03-10 21:42:07240
drh0b12e7f2010-12-20 15:51:58241 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
242 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
243 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
244 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
245 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
246 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
247 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
248 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
249 *pCurrent = 0;
drhce250072025-02-21 17:03:22250 *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
drh0b12e7f2010-12-20 15:51:58251 if( resetFlag ){
252 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
253 }
254 break;
255 }
256
drh63da0892010-03-10 21:42:07257 /*
258 ** Return an approximation for the amount of memory currently used
259 ** by all pagers associated with the given database connection. The
260 ** highwater mark is meaningless and is returned as zero.
261 */
dan9c106082016-07-06 18:12:54262 case SQLITE_DBSTATUS_CACHE_USED_SHARED:
drh63da0892010-03-10 21:42:07263 case SQLITE_DBSTATUS_CACHE_USED: {
264 int totalUsed = 0;
265 int i;
dan2339f062010-07-22 17:55:40266 sqlite3BtreeEnterAll(db);
drh63da0892010-03-10 21:42:07267 for(i=0; i<db->nDb; i++){
268 Btree *pBt = db->aDb[i].pBt;
269 if( pBt ){
270 Pager *pPager = sqlite3BtreePager(pBt);
dan272989b2016-07-06 10:12:02271 int nByte = sqlite3PagerMemUsed(pPager);
dan9c106082016-07-06 18:12:54272 if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
dan272989b2016-07-06 10:12:02273 nByte = nByte / sqlite3BtreeConnectionCount(pBt);
274 }
275 totalUsed += nByte;
drh63da0892010-03-10 21:42:07276 }
277 }
dan2339f062010-07-22 17:55:40278 sqlite3BtreeLeaveAll(db);
drh63da0892010-03-10 21:42:07279 *pCurrent = totalUsed;
280 *pHighwater = 0;
281 break;
282 }
dand46def72010-07-24 11:28:28283
drh643f35e2010-07-26 11:59:40284 /*
285 ** *pCurrent gets an accurate estimate of the amount of memory used
286 ** to store the schema for all databases (main, temp, and any ATTACHed
287 ** databases. *pHighwater is set to zero.
288 */
dand46def72010-07-24 11:28:28289 case SQLITE_DBSTATUS_SCHEMA_USED: {
290 int i; /* Used to iterate through schemas */
291 int nByte = 0; /* Used to accumulate return value */
292
drh21206082011-04-04 18:22:02293 sqlite3BtreeEnterAll(db);
dand46def72010-07-24 11:28:28294 db->pnBytesFreed = &nByte;
drh376860b2022-08-22 15:18:37295 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
296 db->lookaside.pEnd = db->lookaside.pStart;
dand46def72010-07-24 11:28:28297 for(i=0; i<db->nDb; i++){
dan111becf2010-07-26 15:57:01298 Schema *pSchema = db->aDb[i].pSchema;
drh81ba7d12010-07-26 19:09:31299 if( ALWAYS(pSchema!=0) ){
300 HashElem *p;
dand46def72010-07-24 11:28:28301
dan111becf2010-07-26 15:57:01302 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
303 pSchema->tblHash.count
304 + pSchema->trigHash.count
305 + pSchema->idxHash.count
306 + pSchema->fkeyHash.count
307 );
drh039ca6a2015-10-15 16:20:57308 nByte += sqlite3_msize(pSchema->tblHash.ht);
309 nByte += sqlite3_msize(pSchema->trigHash.ht);
310 nByte += sqlite3_msize(pSchema->idxHash.ht);
311 nByte += sqlite3_msize(pSchema->fkeyHash.ht);
dand46def72010-07-24 11:28:28312
313 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
314 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
315 }
316 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
317 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
318 }
dan111becf2010-07-26 15:57:01319 }
dand46def72010-07-24 11:28:28320 }
321 db->pnBytesFreed = 0;
drh376860b2022-08-22 15:18:37322 db->lookaside.pEnd = db->lookaside.pTrueEnd;
drh21206082011-04-04 18:22:02323 sqlite3BtreeLeaveAll(db);
dand46def72010-07-24 11:28:28324
325 *pHighwater = 0;
326 *pCurrent = nByte;
327 break;
328 }
329
drh643f35e2010-07-26 11:59:40330 /*
331 ** *pCurrent gets an accurate estimate of the amount of memory used
332 ** to store all prepared statements.
333 ** *pHighwater is set to zero.
334 */
dand46def72010-07-24 11:28:28335 case SQLITE_DBSTATUS_STMT_USED: {
336 struct Vdbe *pVdbe; /* Used to iterate through VMs */
337 int nByte = 0; /* Used to accumulate return value */
338
339 db->pnBytesFreed = &nByte;
drh376860b2022-08-22 15:18:37340 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd );
341 db->lookaside.pEnd = db->lookaside.pStart;
drhe5928b12022-08-23 20:11:01342 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pVNext){
drh1c848632022-04-04 01:12:11343 sqlite3VdbeDelete(pVdbe);
dand46def72010-07-24 11:28:28344 }
drh376860b2022-08-22 15:18:37345 db->lookaside.pEnd = db->lookaside.pTrueEnd;
dand46def72010-07-24 11:28:28346 db->pnBytesFreed = 0;
347
drh79f7af92014-10-03 16:00:51348 *pHighwater = 0; /* IMP: R-64479-57858 */
dand46def72010-07-24 11:28:28349 *pCurrent = nByte;
350
351 break;
352 }
353
dan58ca31c2011-09-22 14:41:16354 /*
355 ** Set *pCurrent to the total cache hits or misses encountered by all
356 ** pagers the database handle is connected to. *pHighwater is always set
357 ** to zero.
358 */
drhffc78a42018-03-14 14:53:50359 case SQLITE_DBSTATUS_CACHE_SPILL:
360 op = SQLITE_DBSTATUS_CACHE_WRITE+1;
drh08b92082020-08-10 14:18:00361 /* no break */ deliberate_fall_through
dan58ca31c2011-09-22 14:41:16362 case SQLITE_DBSTATUS_CACHE_HIT:
drh9ad3ee42012-03-24 20:06:14363 case SQLITE_DBSTATUS_CACHE_MISS:
364 case SQLITE_DBSTATUS_CACHE_WRITE:{
dan58ca31c2011-09-22 14:41:16365 int i;
drh95cf9582023-12-20 11:34:17366 u64 nRet = 0;
dan58ca31c2011-09-22 14:41:16367 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
drh9ad3ee42012-03-24 20:06:14368 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
dan58ca31c2011-09-22 14:41:16369
370 for(i=0; i<db->nDb; i++){
371 if( db->aDb[i].pBt ){
372 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
373 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
374 }
375 }
drh79f7af92014-10-03 16:00:51376 *pHighwater = 0; /* IMP: R-42420-56072 */
377 /* IMP: R-54100-20147 */
378 /* IMP: R-29431-39229 */
drh95cf9582023-12-20 11:34:17379 *pCurrent = (int)nRet & 0x7fffffff;
dan58ca31c2011-09-22 14:41:16380 break;
381 }
382
drh648e2642013-07-11 15:03:32383 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
384 ** key constraints. Set *pCurrent to zero if all foreign key constraints
385 ** have been satisfied. The *pHighwater is always set to zero.
386 */
387 case SQLITE_DBSTATUS_DEFERRED_FKS: {
drh79f7af92014-10-03 16:00:51388 *pHighwater = 0; /* IMP: R-11967-56545 */
drh648e2642013-07-11 15:03:32389 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
390 break;
391 }
392
drh6480aad2008-08-01 16:31:14393 default: {
dan2339f062010-07-22 17:55:40394 rc = SQLITE_ERROR;
drh6480aad2008-08-01 16:31:14395 }
drh633e6d52008-07-28 19:34:53396 }
dan2339f062010-07-22 17:55:40397 sqlite3_mutex_leave(db->mutex);
398 return rc;
drh633e6d52008-07-28 19:34:53399}