xref: /sqlite-3.40.0/src/status.c (revision b94f2ecc)
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(wsdStat.nowValue) );
104 #if !defined(SQLITE_DISABLE_INTRINSIC) \
105     && (GCC_VERSION>=4004000 || CLANG_VERSION>=3000000)
106   (void)__sync_fetch_and_sub(&wsdStat.nowValue[op], N);
107 #else
108   assert( op>=0 && op<ArraySize(statMutex) );
109   assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
110                                            : sqlite3MallocMutex()) );
111   wsdStat.nowValue[op] -= N;
112 #endif
113 }
114 
115 /*
116 ** Adjust the highwater mark if necessary.
117 ** The caller must hold the appropriate mutex.
118 */
119 void sqlite3StatusHighwater(int op, int X){
120   sqlite3StatValueType newValue;
121   wsdStatInit;
122   assert( X>=0 );
123   newValue = (sqlite3StatValueType)X;
124   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
125   assert( op>=0 && op<ArraySize(statMutex) );
126   assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
127                                            : sqlite3MallocMutex()) );
128   assert( op==SQLITE_STATUS_MALLOC_SIZE
129           || op==SQLITE_STATUS_PAGECACHE_SIZE
130           || op==SQLITE_STATUS_SCRATCH_SIZE
131           || op==SQLITE_STATUS_PARSER_STACK );
132   if( newValue>wsdStat.mxValue[op] ){
133     wsdStat.mxValue[op] = newValue;
134   }
135 }
136 
137 /*
138 ** Query status information.
139 */
140 int sqlite3_status64(
141   int op,
142   sqlite3_int64 *pCurrent,
143   sqlite3_int64 *pHighwater,
144   int resetFlag
145 ){
146   sqlite3_mutex *pMutex;
147   wsdStatInit;
148   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
149     return SQLITE_MISUSE_BKPT;
150   }
151 #ifdef SQLITE_ENABLE_API_ARMOR
152   if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
153 #endif
154   pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
155   sqlite3_mutex_enter(pMutex);
156   *pCurrent = wsdStat.nowValue[op];
157   *pHighwater = wsdStat.mxValue[op];
158   if( resetFlag ){
159     wsdStat.mxValue[op] = wsdStat.nowValue[op];
160   }
161   sqlite3_mutex_leave(pMutex);
162   (void)pMutex;  /* Prevent warning when SQLITE_THREADSAFE=0 */
163   return SQLITE_OK;
164 }
165 int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
166   sqlite3_int64 iCur = 0, iHwtr = 0;
167   int rc;
168 #ifdef SQLITE_ENABLE_API_ARMOR
169   if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
170 #endif
171   rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
172   if( rc==0 ){
173     *pCurrent = (int)iCur;
174     *pHighwater = (int)iHwtr;
175   }
176   return rc;
177 }
178 
179 /*
180 ** Query status information for a single database connection
181 */
182 int sqlite3_db_status(
183   sqlite3 *db,          /* The database connection whose status is desired */
184   int op,               /* Status verb */
185   int *pCurrent,        /* Write current value here */
186   int *pHighwater,      /* Write high-water mark here */
187   int resetFlag         /* Reset high-water mark if true */
188 ){
189   int rc = SQLITE_OK;   /* Return code */
190 #ifdef SQLITE_ENABLE_API_ARMOR
191   if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
192     return SQLITE_MISUSE_BKPT;
193   }
194 #endif
195   sqlite3_mutex_enter(db->mutex);
196   switch( op ){
197     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
198       *pCurrent = db->lookaside.nOut;
199       *pHighwater = db->lookaside.mxOut;
200       if( resetFlag ){
201         db->lookaside.mxOut = db->lookaside.nOut;
202       }
203       break;
204     }
205 
206     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
207     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
208     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
209       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
210       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
211       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
212       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
213       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
214       *pCurrent = 0;
215       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
216       if( resetFlag ){
217         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
218       }
219       break;
220     }
221 
222     /*
223     ** Return an approximation for the amount of memory currently used
224     ** by all pagers associated with the given database connection.  The
225     ** highwater mark is meaningless and is returned as zero.
226     */
227     case SQLITE_DBSTATUS_CACHE_USED_SHARED:
228     case SQLITE_DBSTATUS_CACHE_USED: {
229       int totalUsed = 0;
230       int i;
231       sqlite3BtreeEnterAll(db);
232       for(i=0; i<db->nDb; i++){
233         Btree *pBt = db->aDb[i].pBt;
234         if( pBt ){
235           Pager *pPager = sqlite3BtreePager(pBt);
236           int nByte = sqlite3PagerMemUsed(pPager);
237           if( op==SQLITE_DBSTATUS_CACHE_USED_SHARED ){
238             nByte = nByte / sqlite3BtreeConnectionCount(pBt);
239           }
240           totalUsed += nByte;
241         }
242       }
243       sqlite3BtreeLeaveAll(db);
244       *pCurrent = totalUsed;
245       *pHighwater = 0;
246       break;
247     }
248 
249     /*
250     ** *pCurrent gets an accurate estimate of the amount of memory used
251     ** to store the schema for all databases (main, temp, and any ATTACHed
252     ** databases.  *pHighwater is set to zero.
253     */
254     case SQLITE_DBSTATUS_SCHEMA_USED: {
255       int i;                      /* Used to iterate through schemas */
256       int nByte = 0;              /* Used to accumulate return value */
257 
258       sqlite3BtreeEnterAll(db);
259       db->pnBytesFreed = &nByte;
260       for(i=0; i<db->nDb; i++){
261         Schema *pSchema = db->aDb[i].pSchema;
262         if( ALWAYS(pSchema!=0) ){
263           HashElem *p;
264 
265           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
266               pSchema->tblHash.count
267             + pSchema->trigHash.count
268             + pSchema->idxHash.count
269             + pSchema->fkeyHash.count
270           );
271           nByte += sqlite3_msize(pSchema->tblHash.ht);
272           nByte += sqlite3_msize(pSchema->trigHash.ht);
273           nByte += sqlite3_msize(pSchema->idxHash.ht);
274           nByte += sqlite3_msize(pSchema->fkeyHash.ht);
275 
276           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
277             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
278           }
279           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
280             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
281           }
282         }
283       }
284       db->pnBytesFreed = 0;
285       sqlite3BtreeLeaveAll(db);
286 
287       *pHighwater = 0;
288       *pCurrent = nByte;
289       break;
290     }
291 
292     /*
293     ** *pCurrent gets an accurate estimate of the amount of memory used
294     ** to store all prepared statements.
295     ** *pHighwater is set to zero.
296     */
297     case SQLITE_DBSTATUS_STMT_USED: {
298       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
299       int nByte = 0;              /* Used to accumulate return value */
300 
301       db->pnBytesFreed = &nByte;
302       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
303         sqlite3VdbeClearObject(db, pVdbe);
304         sqlite3DbFree(db, pVdbe);
305       }
306       db->pnBytesFreed = 0;
307 
308       *pHighwater = 0;  /* IMP: R-64479-57858 */
309       *pCurrent = nByte;
310 
311       break;
312     }
313 
314     /*
315     ** Set *pCurrent to the total cache hits or misses encountered by all
316     ** pagers the database handle is connected to. *pHighwater is always set
317     ** to zero.
318     */
319     case SQLITE_DBSTATUS_CACHE_HIT:
320     case SQLITE_DBSTATUS_CACHE_MISS:
321     case SQLITE_DBSTATUS_CACHE_WRITE:{
322       int i;
323       int nRet = 0;
324       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
325       assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
326 
327       for(i=0; i<db->nDb; i++){
328         if( db->aDb[i].pBt ){
329           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
330           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
331         }
332       }
333       *pHighwater = 0; /* IMP: R-42420-56072 */
334                        /* IMP: R-54100-20147 */
335                        /* IMP: R-29431-39229 */
336       *pCurrent = nRet;
337       break;
338     }
339 
340     /* Set *pCurrent to non-zero if there are unresolved deferred foreign
341     ** key constraints.  Set *pCurrent to zero if all foreign key constraints
342     ** have been satisfied.  The *pHighwater is always set to zero.
343     */
344     case SQLITE_DBSTATUS_DEFERRED_FKS: {
345       *pHighwater = 0;  /* IMP: R-11967-56545 */
346       *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
347       break;
348     }
349 
350     default: {
351       rc = SQLITE_ERROR;
352     }
353   }
354   sqlite3_mutex_leave(db->mutex);
355   return rc;
356 }
357