xref: /sqlite-3.40.0/src/status.c (revision 6695f47e)
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 
18 /*
19 ** Variables in which to record status information.
20 */
21 typedef struct sqlite3StatType sqlite3StatType;
22 static SQLITE_WSD struct sqlite3StatType {
23   int nowValue[9];         /* Current value */
24   int mxValue[9];          /* Maximum value */
25 } sqlite3Stat = { {0,}, {0,} };
26 
27 
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 
42 /*
43 ** Return the current value of a status parameter.
44 */
45 int sqlite3StatusValue(int op){
46   wsdStatInit;
47   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
48   return wsdStat.nowValue[op];
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){
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];
61   }
62 }
63 
64 /*
65 ** Set the value of a status to X.
66 */
67 void sqlite3StatusSet(int op, int X){
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];
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){
84   wsdStatInit;
85   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
86     return SQLITE_MISUSE;
87   }
88   *pCurrent = wsdStat.nowValue[op];
89   *pHighwater = wsdStat.mxValue[op];
90   if( resetFlag ){
91     wsdStat.mxValue[op] = wsdStat.nowValue[op];
92   }
93   return SQLITE_OK;
94 }
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     }
115     default: {
116       return SQLITE_ERROR;
117     }
118   }
119   return SQLITE_OK;
120 }
121