blob: 4c2eabb66192673b1d77742dc1fc257697e5e8fc [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 {
drheafc43b2010-07-26 18:43:40 +000024 int nowValue[10]; /* Current value */
25 int mxValue[10]; /* Maximum value */
drh78f82d12008-09-02 00:52:52 +000026} sqlite3Stat = { {0,}, {0,} };
drhc4b18b82008-06-19 13:20:01 +000027
28
drh78f82d12008-09-02 00:52:52 +000029/* The "wsdStat" macro will resolve to the status information
30** state vector. If writable static data is unsupported on the target,
31** we have to locate the state vector at run-time. In the more common
32** case where writable static data is supported, wsdStat can refer directly
33** to the "sqlite3Stat" state vector declared above.
34*/
35#ifdef SQLITE_OMIT_WSD
36# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
37# define wsdStat x[0]
38#else
39# define wsdStatInit
40# define wsdStat sqlite3Stat
41#endif
42
drhc4b18b82008-06-19 13:20:01 +000043/*
drhc4b18b82008-06-19 13:20:01 +000044** Return the current value of a status parameter.
45*/
46int sqlite3StatusValue(int op){
drh78f82d12008-09-02 00:52:52 +000047 wsdStatInit;
48 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
49 return wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000050}
51
52/*
53** Add N to the value of a status record. It is assumed that the
54** caller holds appropriate locks.
55*/
56void sqlite3StatusAdd(int op, int N){
drh78f82d12008-09-02 00:52:52 +000057 wsdStatInit;
58 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
59 wsdStat.nowValue[op] += N;
60 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
61 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000062 }
63}
64
65/*
66** Set the value of a status to X.
67*/
68void sqlite3StatusSet(int op, int X){
drh78f82d12008-09-02 00:52:52 +000069 wsdStatInit;
70 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
71 wsdStat.nowValue[op] = X;
72 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
73 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000074 }
75}
76
77/*
78** Query status information.
79**
80** This implementation assumes that reading or writing an aligned
81** 32-bit integer is an atomic operation. If that assumption is not true,
82** then this routine is not threadsafe.
83*/
84int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
drh78f82d12008-09-02 00:52:52 +000085 wsdStatInit;
86 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
drh413c3d32010-02-23 20:11:56 +000087 return SQLITE_MISUSE_BKPT;
drhc4b18b82008-06-19 13:20:01 +000088 }
drh9ca95732014-10-24 00:35:58 +000089#ifdef SQLITE_ENABLE_API_ARMOR
90 if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
91#endif
drh78f82d12008-09-02 00:52:52 +000092 *pCurrent = wsdStat.nowValue[op];
93 *pHighwater = wsdStat.mxValue[op];
drhc4b18b82008-06-19 13:20:01 +000094 if( resetFlag ){
drh78f82d12008-09-02 00:52:52 +000095 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000096 }
97 return SQLITE_OK;
98}
drh633e6d52008-07-28 19:34:53 +000099
100/*
101** Query status information for a single database connection
102*/
103int sqlite3_db_status(
104 sqlite3 *db, /* The database connection whose status is desired */
105 int op, /* Status verb */
106 int *pCurrent, /* Write current value here */
107 int *pHighwater, /* Write high-water mark here */
108 int resetFlag /* Reset high-water mark if true */
109){
dan2339f062010-07-22 17:55:40 +0000110 int rc = SQLITE_OK; /* Return code */
drh9ca95732014-10-24 00:35:58 +0000111#ifdef SQLITE_ENABLE_API_ARMOR
112 if( !sqlite3SafetyCheckOk(db) || pCurrent==0|| pHighwater==0 ){
113 return SQLITE_MISUSE_BKPT;
114 }
115#endif
dan2339f062010-07-22 17:55:40 +0000116 sqlite3_mutex_enter(db->mutex);
drh633e6d52008-07-28 19:34:53 +0000117 switch( op ){
118 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
119 *pCurrent = db->lookaside.nOut;
120 *pHighwater = db->lookaside.mxOut;
121 if( resetFlag ){
122 db->lookaside.mxOut = db->lookaside.nOut;
123 }
124 break;
125 }
drh63da0892010-03-10 21:42:07 +0000126
drh0b12e7f2010-12-20 15:51:58 +0000127 case SQLITE_DBSTATUS_LOOKASIDE_HIT:
128 case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
129 case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
130 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
131 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
132 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
133 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
134 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
135 *pCurrent = 0;
136 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
137 if( resetFlag ){
138 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
139 }
140 break;
141 }
142
drh63da0892010-03-10 21:42:07 +0000143 /*
144 ** Return an approximation for the amount of memory currently used
145 ** by all pagers associated with the given database connection. The
146 ** highwater mark is meaningless and is returned as zero.
147 */
148 case SQLITE_DBSTATUS_CACHE_USED: {
149 int totalUsed = 0;
150 int i;
dan2339f062010-07-22 17:55:40 +0000151 sqlite3BtreeEnterAll(db);
drh63da0892010-03-10 21:42:07 +0000152 for(i=0; i<db->nDb; i++){
153 Btree *pBt = db->aDb[i].pBt;
154 if( pBt ){
155 Pager *pPager = sqlite3BtreePager(pBt);
156 totalUsed += sqlite3PagerMemUsed(pPager);
157 }
158 }
dan2339f062010-07-22 17:55:40 +0000159 sqlite3BtreeLeaveAll(db);
drh63da0892010-03-10 21:42:07 +0000160 *pCurrent = totalUsed;
161 *pHighwater = 0;
162 break;
163 }
dand46def72010-07-24 11:28:28 +0000164
drh643f35e2010-07-26 11:59:40 +0000165 /*
166 ** *pCurrent gets an accurate estimate of the amount of memory used
167 ** to store the schema for all databases (main, temp, and any ATTACHed
168 ** databases. *pHighwater is set to zero.
169 */
dand46def72010-07-24 11:28:28 +0000170 case SQLITE_DBSTATUS_SCHEMA_USED: {
171 int i; /* Used to iterate through schemas */
172 int nByte = 0; /* Used to accumulate return value */
173
drh21206082011-04-04 18:22:02 +0000174 sqlite3BtreeEnterAll(db);
dand46def72010-07-24 11:28:28 +0000175 db->pnBytesFreed = &nByte;
176 for(i=0; i<db->nDb; i++){
dan111becf2010-07-26 15:57:01 +0000177 Schema *pSchema = db->aDb[i].pSchema;
drh81ba7d12010-07-26 19:09:31 +0000178 if( ALWAYS(pSchema!=0) ){
179 HashElem *p;
dand46def72010-07-24 11:28:28 +0000180
dan111becf2010-07-26 15:57:01 +0000181 nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
182 pSchema->tblHash.count
183 + pSchema->trigHash.count
184 + pSchema->idxHash.count
185 + pSchema->fkeyHash.count
186 );
187 nByte += sqlite3MallocSize(pSchema->tblHash.ht);
188 nByte += sqlite3MallocSize(pSchema->trigHash.ht);
189 nByte += sqlite3MallocSize(pSchema->idxHash.ht);
190 nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
dand46def72010-07-24 11:28:28 +0000191
192 for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
193 sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
194 }
195 for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
196 sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
197 }
dan111becf2010-07-26 15:57:01 +0000198 }
dand46def72010-07-24 11:28:28 +0000199 }
200 db->pnBytesFreed = 0;
drh21206082011-04-04 18:22:02 +0000201 sqlite3BtreeLeaveAll(db);
dand46def72010-07-24 11:28:28 +0000202
203 *pHighwater = 0;
204 *pCurrent = nByte;
205 break;
206 }
207
drh643f35e2010-07-26 11:59:40 +0000208 /*
209 ** *pCurrent gets an accurate estimate of the amount of memory used
210 ** to store all prepared statements.
211 ** *pHighwater is set to zero.
212 */
dand46def72010-07-24 11:28:28 +0000213 case SQLITE_DBSTATUS_STMT_USED: {
214 struct Vdbe *pVdbe; /* Used to iterate through VMs */
215 int nByte = 0; /* Used to accumulate return value */
216
217 db->pnBytesFreed = &nByte;
218 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
drhcb103b92012-10-26 00:11:23 +0000219 sqlite3VdbeClearObject(db, pVdbe);
mistachkin70cb28f2012-11-06 20:39:11 +0000220 sqlite3DbFree(db, pVdbe);
dand46def72010-07-24 11:28:28 +0000221 }
222 db->pnBytesFreed = 0;
223
drh79f7af92014-10-03 16:00:51 +0000224 *pHighwater = 0; /* IMP: R-64479-57858 */
dand46def72010-07-24 11:28:28 +0000225 *pCurrent = nByte;
226
227 break;
228 }
229
dan58ca31c2011-09-22 14:41:16 +0000230 /*
231 ** Set *pCurrent to the total cache hits or misses encountered by all
232 ** pagers the database handle is connected to. *pHighwater is always set
233 ** to zero.
234 */
235 case SQLITE_DBSTATUS_CACHE_HIT:
drh9ad3ee42012-03-24 20:06:14 +0000236 case SQLITE_DBSTATUS_CACHE_MISS:
237 case SQLITE_DBSTATUS_CACHE_WRITE:{
dan58ca31c2011-09-22 14:41:16 +0000238 int i;
239 int nRet = 0;
240 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
drh9ad3ee42012-03-24 20:06:14 +0000241 assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
dan58ca31c2011-09-22 14:41:16 +0000242
243 for(i=0; i<db->nDb; i++){
244 if( db->aDb[i].pBt ){
245 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
246 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
247 }
248 }
drh79f7af92014-10-03 16:00:51 +0000249 *pHighwater = 0; /* IMP: R-42420-56072 */
250 /* IMP: R-54100-20147 */
251 /* IMP: R-29431-39229 */
dan58ca31c2011-09-22 14:41:16 +0000252 *pCurrent = nRet;
253 break;
254 }
255
drh648e2642013-07-11 15:03:32 +0000256 /* Set *pCurrent to non-zero if there are unresolved deferred foreign
257 ** key constraints. Set *pCurrent to zero if all foreign key constraints
258 ** have been satisfied. The *pHighwater is always set to zero.
259 */
260 case SQLITE_DBSTATUS_DEFERRED_FKS: {
drh79f7af92014-10-03 16:00:51 +0000261 *pHighwater = 0; /* IMP: R-11967-56545 */
drh648e2642013-07-11 15:03:32 +0000262 *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
263 break;
264 }
265
drh6480aad2008-08-01 16:31:14 +0000266 default: {
dan2339f062010-07-22 17:55:40 +0000267 rc = SQLITE_ERROR;
drh6480aad2008-08-01 16:31:14 +0000268 }
drh633e6d52008-07-28 19:34:53 +0000269 }
dan2339f062010-07-22 17:55:40 +0000270 sqlite3_mutex_leave(db->mutex);
271 return rc;
drh633e6d52008-07-28 19:34:53 +0000272}