blob: ac32753a227a8803639da23dbb353c766926f27b [file] [log] [blame]
drhc4b18b82008-06-19 13:20:01 +00001/*
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:01 +000015*/
16#include "sqliteInt.h"
dand46def72010-07-24 11:28:28 +000017#include "vdbeInt.h"
drhc4b18b82008-06-19 13:20:01 +000018
19/*
20** Variables in which to record status information.
21*/
drh78f82d12008-09-02 00:52:52 +000022typedef struct sqlite3StatType sqlite3StatType;
23static SQLITE_WSD struct sqlite3StatType {
drh2b4905c2015-03-23 18:52:56 +000024#if SQLITE_PTRSIZE>4
drhaf89fe62015-03-23 17:25:18 +000025 sqlite3_int64 nowValue[10]; /* Current value */
26 sqlite3_int64 mxValue[10]; /* Maximum value */
drh2b4905c2015-03-23 18:52:56 +000027#else
28 u32 nowValue[10]; /* Current value */
29 u32 mxValue[10]; /* Maximum value */
30#endif
drh78f82d12008-09-02 00:52:52 +000031} sqlite3Stat = { {0,}, {0,} };
drhc4b18b82008-06-19 13:20:01 +000032
drhaf89fe62015-03-23 17:25:18 +000033/*
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:01 +000050
drh78f82d12008-09-02 00:52:52 +000051/* 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:01 +000065/*
drhaf89fe62015-03-23 17:25:18 +000066** Return the current value of a status parameter. The caller must
67** be holding the appropriate mutex.
drhc4b18b82008-06-19 13:20:01 +000068*/
drhaf89fe62015-03-23 17:25:18 +000069sqlite3_int64 sqlite3StatusValue(int op){
drh78f82d12008-09-02 00:52:52 +000070 wsdStatInit;
71 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:18 +000072 assert( op>=0 && op<ArraySize(statMutex) );
73 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
74 : sqlite3MallocMutex()) );
drh78f82d12008-09-02 00:52:52 +000075 return wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000076}
77
78/*
drhaf89fe62015-03-23 17:25:18 +000079** 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:01 +000088*/
drhaf89fe62015-03-23 17:25:18 +000089void sqlite3StatusUp(int op, int N){
drh78f82d12008-09-02 00:52:52 +000090 wsdStatInit;
91 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:18 +000092 assert( op>=0 && op<ArraySize(statMutex) );
93 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
94 : sqlite3MallocMutex()) );
drh78f82d12008-09-02 00:52:52 +000095 wsdStat.nowValue[op] += N;
96 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
97 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000098 }
99}
drhaf89fe62015-03-23 17:25:18 +0000100void 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:01 +0000109
110/*
drhaf89fe62015-03-23 17:25:18 +0000111** Set the value of a status to X. The highwater mark is adjusted if
112** necessary. The caller must hold the appropriate mutex.
drhc4b18b82008-06-19 13:20:01 +0000113*/
114void sqlite3StatusSet(int op, int X){
drh78f82d12008-09-02 00:52:52 +0000115 wsdStatInit;
116 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
drhaf89fe62015-03-23 17:25:18 +0000117 assert( op>=0 && op<ArraySize(statMutex) );
118 assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
119 : sqlite3MallocMutex()) );
drh78f82d12008-09-02 00:52:52 +0000120 wsdStat.nowValue[op] = X;
121 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
122 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +0000123 }
124}
125
126/*
127** Query status information.
drhc4b18b82008-06-19 13:20:01 +0000128*/
drhaf89fe62015-03-23 17:25:18 +0000129int sqlite3_status64(
130 int op,
131 sqlite3_int64 *pCurrent,
132 sqlite3_int64 *pHighwater,
133 int resetFlag
134){
drhaf89fe62015-03-23 17:25:18 +0000135 sqlite3_mutex *pMutex;
drh24938702015-03-23 19:16:30 +0000136 wsdStatInit;
drh78f82d12008-09-02 00:52:52 +0000137 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
drh413c3d32010-02-23 20:11:56 +0000138 return SQLITE_MISUSE_BKPT;
drhc4b18b82008-06-19 13:20:01 +0000139 }
drh9ca95732014-10-24 00:35:58 +0000140#ifdef SQLITE_ENABLE_API_ARMOR
141 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
142#endif
drhaf89fe62015-03-23 17:25:18 +0000143 pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
144 sqlite3_mutex_enter(pMutex);
drh78f82d12008-09-02 00:52:52 +0000145 *pCurrent = wsdStat.nowValue[op];
146 *pHighwater = wsdStat.mxValue[op];
drhc4b18b82008-06-19 13:20:01 +0000147 if( resetFlag ){
drh78f82d12008-09-02 00:52:52 +0000148 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +0000149 }
drhaf89fe62015-03-23 17:25:18 +0000150 sqlite3_mutex_leave(pMutex);
drh47564042015-03-24 18:19:39 +0000151 (void)pMutex; /* Prevent warning when SQLITE_THREADSAFE=0 */
drhc4b18b82008-06-19 13:20:01 +0000152 return SQLITE_OK;
153}
drhaf89fe62015-03-23 17:25:18 +0000154int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
155 sqlite3_int64 iCur, iHwtr;
156 int rc;
157#ifdef SQLITE_ENABLE_API_ARMOR
158 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
159#endif
160 rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
drh2b4905c2015-03-23 18:52:56 +0000161 if( rc==0 ){
162 *pCurrent = (int)iCur;
163 *pHighwater = (int)iHwtr;
164 }
drhaf89fe62015-03-23 17:25:18 +0000165 return rc;
166}
drh633e6d52008-07-28 19:34:53 +0000167
168/*
169** Query status information for a single database connection
170*/
171int sqlite3_db_status(
172 sqlite3 *db, /* The database connection whose status is desired */
173 int op, /* Status verb */
174 int *pCurrent, /* Write current value here */
175 int *pHighwater, /* Write high-water mark here */
176 int resetFlag /* Reset high-water mark if true */
177){
dan2339f062010-07-22 17:55:40 +0000178 int rc = SQLITE_OK; /* Return code */
drh9ca95732014-10-24 00:35:58 +0000179#ifdef SQLITE_ENABLE_API_ARMOR
180 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
181 return SQLITE_MISUSE_BKPT;
182 }
183#endif
dan2339f062010-07-22 17:55:40 +0000184 sqlite3_mutex_enter(db->mutex);
drh633e6d52008-07-28 19:34:53 +0000185 switch( op ){
186 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
187 *pCurrent = db->lookaside.nOut;
188 *pHighwater = db->lookaside.mxOut;
189 if( resetFlag ){
190 db->lookaside.mxOut = db->lookaside.nOut;
191 }
192 break;
193 }
drh63da0892010-03-10 21:42:07 +0000194
drh0b12e7f2010-12-20 15:51:58 +0000195 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
196 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
197 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
198 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
199 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
200 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
201 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
202 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
203 *pCurrent = 0;
204 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
205 if( resetFlag ){
206 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
207 }
208 break;
209 }
210
drh63da0892010-03-10 21:42:07 +0000211 /*
212 ** Return an approximation for the amount of memory currently used
213 ** by all pagers associated with the given database connection. The
214 ** highwater mark is meaningless and is returned as zero.
215 */
216 case SQLITE_DBSTATUS_CACHE_USED: {
217 int totalUsed = 0;
218 int i;
dan2339f062010-07-22 17:55:40 +0000219 sqlite3BtreeEnterAll(db);
drh63da0892010-03-10 21:42:07 +0000220 for(i=0; i<db->nDb; i++){
221 Btree *pBt = db->aDb[i].pBt;
222 if( pBt ){
223 Pager *pPager = sqlite3BtreePager(pBt);
224 totalUsed += sqlite3PagerMemUsed(pPager);
225 }
226 }
dan2339f062010-07-22 17:55:40 +0000227 sqlite3BtreeLeaveAll(db);
drh63da0892010-03-10 21:42:07 +0000228 *pCurrent = totalUsed;
229 *pHighwater = 0;
230 break;
231 }
dand46def72010-07-24 11:28:28 +0000232
drh643f35e2010-07-26 11:59:40 +0000233 /*
234 ** *pCurrent gets an accurate estimate of the amount of memory used
235 ** to store the schema for all databases (main, temp, and any ATTACHed
236 ** databases. *pHighwater is set to zero.
237 */
dand46def72010-07-24 11:28:28 +0000238 case SQLITE_DBSTATUS_SCHEMA_USED: {
239 int i; /* Used to iterate through schemas */
240 int nByte = 0; /* Used to accumulate return value */
241
drh21206082011-04-04 18:22:02 +0000242 sqlite3BtreeEnterAll(db);
dand46def72010-07-24 11:28:28 +0000243 db->pnBytesFreed = &nByte;
244 for(i=0; i<db->nDb; i++){
dan111becf2010-07-26 15:57:01 +0000245 Schema *pSchema = db->aDb[i].pSchema;
drh81ba7d12010-07-26 19:09:31 +0000246 if( ALWAYS(pSchema!=0) ){
247 HashElem *p;
dand46def72010-07-24 11:28:28 +0000248
dan111becf2010-07-26 15:57:01 +0000249 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
250 pSchema->tblHash.count
251 + pSchema->trigHash.count
252 + pSchema->idxHash.count
253 + pSchema->fkeyHash.count
254 );
255 nByte += sqlite3MallocSize(pSchema->tblHash.ht);
256 nByte += sqlite3MallocSize(pSchema->trigHash.ht);
257 nByte += sqlite3MallocSize(pSchema->idxHash.ht);
258 nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
dand46def72010-07-24 11:28:28 +0000259
260 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
261 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
262 }
263 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
264 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
265 }
dan111becf2010-07-26 15:57:01 +0000266 }
dand46def72010-07-24 11:28:28 +0000267 }
268 db->pnBytesFreed = 0;
drh21206082011-04-04 18:22:02 +0000269 sqlite3BtreeLeaveAll(db);
dand46def72010-07-24 11:28:28 +0000270
271 *pHighwater = 0;
272 *pCurrent = nByte;
273 break;
274 }
275
drh643f35e2010-07-26 11:59:40 +0000276 /*
277 ** *pCurrent gets an accurate estimate of the amount of memory used
278 ** to store all prepared statements.
279 ** *pHighwater is set to zero.
280 */
dand46def72010-07-24 11:28:28 +0000281 case SQLITE_DBSTATUS_STMT_USED: {
282 struct Vdbe *pVdbe; /* Used to iterate through VMs */
283 int nByte = 0; /* Used to accumulate return value */
284
285 db->pnBytesFreed = &nByte;
286 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
drhcb103b92012-10-26 00:11:23 +0000287 sqlite3VdbeClearObject(db, pVdbe);
mistachkin70cb28f2012-11-06 20:39:11 +0000288 sqlite3DbFree(db, pVdbe);
dand46def72010-07-24 11:28:28 +0000289 }
290 db->pnBytesFreed = 0;
291
drh79f7af92014-10-03 16:00:51 +0000292 *pHighwater = 0; /* IMP: R-64479-57858 */
dand46def72010-07-24 11:28:28 +0000293 *pCurrent = nByte;
294
295 break;
296 }
297
dan58ca31c2011-09-22 14:41:16 +0000298 /*
299 ** Set *pCurrent to the total cache hits or misses encountered by all
300 ** pagers the database handle is connected to. *pHighwater is always set
301 ** to zero.
302 */
303 case SQLITE_DBSTATUS_CACHE_HIT:
drh9ad3ee42012-03-24 20:06:14 +0000304 case SQLITE_DBSTATUS_CACHE_MISS:
305 case SQLITE_DBSTATUS_CACHE_WRITE:{
dan58ca31c2011-09-22 14:41:16 +0000306 int i;
307 int nRet = 0;
308 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
drh9ad3ee42012-03-24 20:06:14 +0000309 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
dan58ca31c2011-09-22 14:41:16 +0000310
311 for(i=0; i<db->nDb; i++){
312 if( db->aDb[i].pBt ){
313 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
314 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
315 }
316 }
drh79f7af92014-10-03 16:00:51 +0000317 *pHighwater = 0; /* IMP: R-42420-56072 */
318 /* IMP: R-54100-20147 */
319 /* IMP: R-29431-39229 */
dan58ca31c2011-09-22 14:41:16 +0000320 *pCurrent = nRet;
321 break;
322 }
323
drh648e2642013-07-11 15:03:32 +0000324 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
325 ** key constraints. Set *pCurrent to zero if all foreign key constraints
326 ** have been satisfied. The *pHighwater is always set to zero.
327 */
328 case SQLITE_DBSTATUS_DEFERRED_FKS: {
drh79f7af92014-10-03 16:00:51 +0000329 *pHighwater = 0; /* IMP: R-11967-56545 */
drh648e2642013-07-11 15:03:32 +0000330 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
331 break;
332 }
333
drh6480aad2008-08-01 16:31:14 +0000334 default: {
dan2339f062010-07-22 17:55:40 +0000335 rc = SQLITE_ERROR;
drh6480aad2008-08-01 16:31:14 +0000336 }
drh633e6d52008-07-28 19:34:53 +0000337 }
dan2339f062010-07-22 17:55:40 +0000338 sqlite3_mutex_leave(db->mutex);
339 return rc;
drh633e6d52008-07-28 19:34:53 +0000340}