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 ** $Id: status.c,v 1.5 2008/07/28 19:34:54 drh Exp $ 17 */ 18 #include "sqliteInt.h" 19 20 /* 21 ** Variables in which to record status information. 22 */ 23 static struct { 24 int nowValue[7]; /* Current value */ 25 int mxValue[7]; /* Maximum value */ 26 } sqlite3Stat; 27 28 29 /* 30 ** Reset the status records. This routine is called by 31 ** sqlite3_initialize(). 32 */ 33 void sqlite3StatusReset(void){ 34 memset(&sqlite3Stat, 0, sizeof(sqlite3Stat)); 35 } 36 37 /* 38 ** Return the current value of a status parameter. 39 */ 40 int sqlite3StatusValue(int op){ 41 assert( op>=0 && op<ArraySize(sqlite3Stat.nowValue) ); 42 return sqlite3Stat.nowValue[op]; 43 } 44 45 /* 46 ** Add N to the value of a status record. It is assumed that the 47 ** caller holds appropriate locks. 48 */ 49 void sqlite3StatusAdd(int op, int N){ 50 assert( op>=0 && op<ArraySize(sqlite3Stat.nowValue) ); 51 sqlite3Stat.nowValue[op] += N; 52 if( sqlite3Stat.nowValue[op]>sqlite3Stat.mxValue[op] ){ 53 sqlite3Stat.mxValue[op] = sqlite3Stat.nowValue[op]; 54 } 55 } 56 57 /* 58 ** Set the value of a status to X. 59 */ 60 void sqlite3StatusSet(int op, int X){ 61 assert( op>=0 && op<ArraySize(sqlite3Stat.nowValue) ); 62 sqlite3Stat.nowValue[op] = X; 63 if( sqlite3Stat.nowValue[op]>sqlite3Stat.mxValue[op] ){ 64 sqlite3Stat.mxValue[op] = sqlite3Stat.nowValue[op]; 65 } 66 } 67 68 /* 69 ** Query status information. 70 ** 71 ** This implementation assumes that reading or writing an aligned 72 ** 32-bit integer is an atomic operation. If that assumption is not true, 73 ** then this routine is not threadsafe. 74 */ 75 int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){ 76 if( op<0 || op>=ArraySize(sqlite3Stat.nowValue) ){ 77 return SQLITE_MISUSE; 78 } 79 *pCurrent = sqlite3Stat.nowValue[op]; 80 *pHighwater = sqlite3Stat.mxValue[op]; 81 if( resetFlag ){ 82 sqlite3Stat.mxValue[op] = sqlite3Stat.nowValue[op]; 83 } 84 return SQLITE_OK; 85 } 86 87 /* 88 ** Query status information for a single database connection 89 */ 90 int sqlite3_db_status( 91 sqlite3 *db, /* The database connection whose status is desired */ 92 int op, /* Status verb */ 93 int *pCurrent, /* Write current value here */ 94 int *pHighwater, /* Write high-water mark here */ 95 int resetFlag /* Reset high-water mark if true */ 96 ){ 97 switch( op ){ 98 case SQLITE_DBSTATUS_LOOKASIDE_USED: { 99 *pCurrent = db->lookaside.nOut; 100 *pHighwater = db->lookaside.mxOut; 101 if( resetFlag ){ 102 db->lookaside.mxOut = db->lookaside.nOut; 103 } 104 break; 105 } 106 } 107 return SQLITE_OK; 108 } 109