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" |
| 17 | |
| 18 | /* |
| 19 | ** Variables in which to record status information. |
| 20 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 21 | typedef struct sqlite3StatType sqlite3StatType; |
| 22 | static SQLITE_WSD struct sqlite3StatType { |
drh | e50135e | 2008-08-05 17:53:22 +0000 | [diff] [blame] | 23 | int nowValue[9]; /* Current value */ |
| 24 | int mxValue[9]; /* Maximum value */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 25 | } sqlite3Stat = { {0,}, {0,} }; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 26 | |
| 27 | |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 28 | /* The "wsdStat" macro will resolve to the status information |
| 29 | ** state vector. If writable static data is unsupported on the target, |
| 30 | ** we have to locate the state vector at run-time. In the more common |
| 31 | ** case where writable static data is supported, wsdStat can refer directly |
| 32 | ** to the "sqlite3Stat" state vector declared above. |
| 33 | */ |
| 34 | #ifdef SQLITE_OMIT_WSD |
| 35 | # define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat) |
| 36 | # define wsdStat x[0] |
| 37 | #else |
| 38 | # define wsdStatInit |
| 39 | # define wsdStat sqlite3Stat |
| 40 | #endif |
| 41 | |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 42 | /* |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 43 | ** Return the current value of a status parameter. |
| 44 | */ |
| 45 | int sqlite3StatusValue(int op){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 46 | wsdStatInit; |
| 47 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 48 | return wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* |
| 52 | ** Add N to the value of a status record. It is assumed that the |
| 53 | ** caller holds appropriate locks. |
| 54 | */ |
| 55 | void sqlite3StatusAdd(int op, int N){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 56 | wsdStatInit; |
| 57 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 58 | wsdStat.nowValue[op] += N; |
| 59 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 60 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | ** Set the value of a status to X. |
| 66 | */ |
| 67 | void sqlite3StatusSet(int op, int X){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 68 | wsdStatInit; |
| 69 | assert( op>=0 && op<ArraySize(wsdStat.nowValue) ); |
| 70 | wsdStat.nowValue[op] = X; |
| 71 | if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){ |
| 72 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | ** Query status information. |
| 78 | ** |
| 79 | ** This implementation assumes that reading or writing an aligned |
| 80 | ** 32-bit integer is an atomic operation. If that assumption is not true, |
| 81 | ** then this routine is not threadsafe. |
| 82 | */ |
| 83 | int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 84 | wsdStatInit; |
| 85 | if( op<0 || op>=ArraySize(wsdStat.nowValue) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame^] | 86 | return SQLITE_MISUSE_BKPT; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 87 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 88 | *pCurrent = wsdStat.nowValue[op]; |
| 89 | *pHighwater = wsdStat.mxValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 90 | if( resetFlag ){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 91 | wsdStat.mxValue[op] = wsdStat.nowValue[op]; |
drh | c4b18b8 | 2008-06-19 13:20:01 +0000 | [diff] [blame] | 92 | } |
| 93 | return SQLITE_OK; |
| 94 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | ** Query status information for a single database connection |
| 98 | */ |
| 99 | int sqlite3_db_status( |
| 100 | sqlite3 *db, /* The database connection whose status is desired */ |
| 101 | int op, /* Status verb */ |
| 102 | int *pCurrent, /* Write current value here */ |
| 103 | int *pHighwater, /* Write high-water mark here */ |
| 104 | int resetFlag /* Reset high-water mark if true */ |
| 105 | ){ |
| 106 | switch( op ){ |
| 107 | case SQLITE_DBSTATUS_LOOKASIDE_USED: { |
| 108 | *pCurrent = db->lookaside.nOut; |
| 109 | *pHighwater = db->lookaside.mxOut; |
| 110 | if( resetFlag ){ |
| 111 | db->lookaside.mxOut = db->lookaside.nOut; |
| 112 | } |
| 113 | break; |
| 114 | } |
drh | 6480aad | 2008-08-01 16:31:14 +0000 | [diff] [blame] | 115 | default: { |
| 116 | return SQLITE_ERROR; |
| 117 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 118 | } |
| 119 | return SQLITE_OK; |
| 120 | } |