drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 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. |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 15 | */ |
| 16 | #include "sqliteInt.h" |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 17 | #include "vdbeInt.h" |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | ** Variables in which to record status information. |
| 21 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 22 | typedef struct sqlite3StatType sqlite3StatType; |
| 23 | static SQLITE_WSD struct sqlite3StatType { |
drh | eafc43b | 2010-07-26 18:43:40 +0000 | [diff] [blame] | 24 | int nowValue[10]; /* Current value */ |
| 25 | int mxValue[10]; /* Maximum value */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 26 | } sqlite3Stat = { {0,}, {0,} }; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 27 | |
| 28 | |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 29 | /* 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 | |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 43 | /* |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 44 | ** Return the current value of a status parameter. |
| 45 | */ |
| 46 | int sqlite3StatusValue(int op){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 47 | wsdStatInit; |
| 48 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 49 | return wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* |
| 53 | ** Add N to the value of a status record. It is assumed that the |
| 54 | ** caller holds appropriate locks. |
| 55 | */ |
| 56 | void sqlite3StatusAdd(int op, int N){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 57 | 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]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | ** Set the value of a status to X. |
| 67 | */ |
| 68 | void sqlite3StatusSet(int op, int X){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 69 | 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]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 74 | } |
| 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 | */ |
| 84 | int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 85 | wsdStatInit; |
| 86 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 87 | return SQLITE_MISUSE_BKPT; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 88 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 89 | *pCurrent = wsdStat.nowValue[op]; |
| 90 | *pHighwater = wsdStat.mxValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 91 | if( resetFlag ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 92 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 93 | } |
| 94 | return SQLITE_OK; |
| 95 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | ** Query status information for a single database connection |
| 99 | */ |
| 100 | int sqlite3_db_status( |
| 101 | sqlite3 *db, /* The database connection whose status is desired */ |
| 102 | int op, /* Status verb */ |
| 103 | int *pCurrent, /* Write current value here */ |
| 104 | int *pHighwater, /* Write high-water mark here */ |
| 105 | int resetFlag /* Reset high-water mark if true */ |
| 106 | ){ |
dan | 2339f06 | 2010-07-22 17:55:40 +0000 | [diff] [blame] | 107 | int rc = SQLITE_OK; /* Return code */ |
| 108 | sqlite3_mutex_enter(db->mutex); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 109 | switch( op ){ |
| 110 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 111 | *pCurrent = db->lookaside.nOut; |
| 112 | *pHighwater = db->lookaside.mxOut; |
| 113 | if( resetFlag ){ |
| 114 | db->lookaside.mxOut = db->lookaside.nOut; |
| 115 | } |
| 116 | break; |
| 117 | } |
drh | 63da089 | 2010-03-10 21:42:07 +0000 | [diff] [blame] | 118 | |
drh | 0b12e7f | 2010-12-20 15:51:58 +0000 | [diff] [blame] | 119 | case SQLITE_DBSTATUS_LOOKASIDE_HIT: |
| 120 | case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE: |
| 121 | case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: { |
| 122 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT ); |
| 123 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE ); |
| 124 | testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL ); |
| 125 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 ); |
| 126 | assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 ); |
| 127 | *pCurrent = 0; |
| 128 | *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT]; |
| 129 | if( resetFlag ){ |
| 130 | db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | |
drh | 63da089 | 2010-03-10 21:42:07 +0000 | [diff] [blame] | 135 | /* |
| 136 | ** Return an approximation for the amount of memory currently used |
| 137 | ** by all pagers associated with the given database connection. The |
| 138 | ** highwater mark is meaningless and is returned as zero. |
| 139 | */ |
| 140 | case SQLITE_DBSTATUS_CACHE_USED: { |
| 141 | int totalUsed = 0; |
| 142 | int i; |
dan | 2339f06 | 2010-07-22 17:55:40 +0000 | [diff] [blame] | 143 | sqlite3BtreeEnterAll(db); |
drh | 63da089 | 2010-03-10 21:42:07 +0000 | [diff] [blame] | 144 | for(i=0; i<db->nDb; i++){ |
| 145 | Btree *pBt = db->aDb[i].pBt; |
| 146 | if( pBt ){ |
| 147 | Pager *pPager = sqlite3BtreePager(pBt); |
| 148 | totalUsed += sqlite3PagerMemUsed(pPager); |
| 149 | } |
| 150 | } |
dan | 2339f06 | 2010-07-22 17:55:40 +0000 | [diff] [blame] | 151 | sqlite3BtreeLeaveAll(db); |
drh | 63da089 | 2010-03-10 21:42:07 +0000 | [diff] [blame] | 152 | *pCurrent = totalUsed; |
| 153 | *pHighwater = 0; |
| 154 | break; |
| 155 | } |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 156 | |
drh | 643f35e | 2010-07-26 11:59:40 +0000 | [diff] [blame] | 157 | /* |
| 158 | ** *pCurrent gets an accurate estimate of the amount of memory used |
| 159 | ** to store the schema for all databases (main, temp, and any ATTACHed |
| 160 | ** databases. *pHighwater is set to zero. |
| 161 | */ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 162 | case SQLITE_DBSTATUS_SCHEMA_USED: { |
| 163 | int i; /* Used to iterate through schemas */ |
| 164 | int nByte = 0; /* Used to accumulate return value */ |
| 165 | |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 166 | sqlite3BtreeEnterAll(db); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 167 | db->pnBytesFreed = &nByte; |
| 168 | for(i=0; i<db->nDb; i++){ |
dan | 111becf | 2010-07-26 15:57:01 +0000 | [diff] [blame] | 169 | Schema *pSchema = db->aDb[i].pSchema; |
drh | 81ba7d1 | 2010-07-26 19:09:31 +0000 | [diff] [blame] | 170 | if( ALWAYS(pSchema!=0) ){ |
| 171 | HashElem *p; |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 172 | |
dan | 111becf | 2010-07-26 15:57:01 +0000 | [diff] [blame] | 173 | nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * ( |
| 174 | pSchema->tblHash.count |
| 175 | + pSchema->trigHash.count |
| 176 | + pSchema->idxHash.count |
| 177 | + pSchema->fkeyHash.count |
| 178 | ); |
| 179 | nByte += sqlite3MallocSize(pSchema->tblHash.ht); |
| 180 | nByte += sqlite3MallocSize(pSchema->trigHash.ht); |
| 181 | nByte += sqlite3MallocSize(pSchema->idxHash.ht); |
| 182 | nByte += sqlite3MallocSize(pSchema->fkeyHash.ht); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 183 | |
| 184 | for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){ |
| 185 | sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p)); |
| 186 | } |
| 187 | for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){ |
| 188 | sqlite3DeleteTable(db, (Table *)sqliteHashData(p)); |
| 189 | } |
dan | 111becf | 2010-07-26 15:57:01 +0000 | [diff] [blame] | 190 | } |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 191 | } |
| 192 | db->pnBytesFreed = 0; |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 193 | sqlite3BtreeLeaveAll(db); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 194 | |
| 195 | *pHighwater = 0; |
| 196 | *pCurrent = nByte; |
| 197 | break; |
| 198 | } |
| 199 | |
drh | 643f35e | 2010-07-26 11:59:40 +0000 | [diff] [blame] | 200 | /* |
| 201 | ** *pCurrent gets an accurate estimate of the amount of memory used |
| 202 | ** to store all prepared statements. |
| 203 | ** *pHighwater is set to zero. |
| 204 | */ |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 205 | case SQLITE_DBSTATUS_STMT_USED: { |
| 206 | struct Vdbe *pVdbe; /* Used to iterate through VMs */ |
| 207 | int nByte = 0; /* Used to accumulate return value */ |
| 208 | |
| 209 | db->pnBytesFreed = &nByte; |
| 210 | for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){ |
drh | cb103b9 | 2012-10-26 00:11:23 +0000 | [diff] [blame] | 211 | sqlite3VdbeClearObject(db, pVdbe); |
mistachkin | 70cb28f | 2012-11-06 20:39:11 +0000 | [diff] [blame] | 212 | sqlite3DbFree(db, pVdbe); |
dan | d46def7 | 2010-07-24 11:28:28 +0000 | [diff] [blame] | 213 | } |
| 214 | db->pnBytesFreed = 0; |
| 215 | |
| 216 | *pHighwater = 0; |
| 217 | *pCurrent = nByte; |
| 218 | |
| 219 | break; |
| 220 | } |
| 221 | |
dan | 58ca31c | 2011-09-22 14:41:16 +0000 | [diff] [blame] | 222 | /* |
| 223 | ** Set *pCurrent to the total cache hits or misses encountered by all |
| 224 | ** pagers the database handle is connected to. *pHighwater is always set |
| 225 | ** to zero. |
| 226 | */ |
| 227 | case SQLITE_DBSTATUS_CACHE_HIT: |
drh | 9ad3ee4 | 2012-03-24 20:06:14 +0000 | [diff] [blame] | 228 | case SQLITE_DBSTATUS_CACHE_MISS: |
| 229 | case SQLITE_DBSTATUS_CACHE_WRITE:{ |
dan | 58ca31c | 2011-09-22 14:41:16 +0000 | [diff] [blame] | 230 | int i; |
| 231 | int nRet = 0; |
| 232 | assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 ); |
drh | 9ad3ee4 | 2012-03-24 20:06:14 +0000 | [diff] [blame] | 233 | assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 ); |
dan | 58ca31c | 2011-09-22 14:41:16 +0000 | [diff] [blame] | 234 | |
| 235 | for(i=0; i<db->nDb; i++){ |
| 236 | if( db->aDb[i].pBt ){ |
| 237 | Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt); |
| 238 | sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet); |
| 239 | } |
| 240 | } |
| 241 | *pHighwater = 0; |
| 242 | *pCurrent = nRet; |
| 243 | break; |
| 244 | } |
| 245 | |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 246 | default: { |
dan | 2339f06 | 2010-07-22 17:55:40 +0000 | [diff] [blame] | 247 | rc = SQLITE_ERROR; |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 248 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 249 | } |
dan | 2339f06 | 2010-07-22 17:55:40 +0000 | [diff] [blame] | 250 | sqlite3_mutex_leave(db->mutex); |
| 251 | return rc; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 252 | } |