blob: f4c77a910959ff01e3273a62ab4c1ca0c4016e5d [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"
17
18/*
19** Variables in which to record status information.
20*/
drh78f82d12008-09-02 00:52:52 +000021typedef struct sqlite3StatType sqlite3StatType;
22static SQLITE_WSD struct sqlite3StatType {
drhe50135e2008-08-05 17:53:22 +000023 int nowValue[9]; /* Current value */
24 int mxValue[9]; /* Maximum value */
drh78f82d12008-09-02 00:52:52 +000025} sqlite3Stat = { {0,}, {0,} };
drhc4b18b82008-06-19 13:20:01 +000026
27
drh78f82d12008-09-02 00:52:52 +000028/* 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
drhc4b18b82008-06-19 13:20:01 +000042/*
drhc4b18b82008-06-19 13:20:01 +000043** Return the current value of a status parameter.
44*/
45int sqlite3StatusValue(int op){
drh78f82d12008-09-02 00:52:52 +000046 wsdStatInit;
47 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
48 return wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000049}
50
51/*
52** Add N to the value of a status record. It is assumed that the
53** caller holds appropriate locks.
54*/
55void sqlite3StatusAdd(int op, int N){
drh78f82d12008-09-02 00:52:52 +000056 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];
drhc4b18b82008-06-19 13:20:01 +000061 }
62}
63
64/*
65** Set the value of a status to X.
66*/
67void sqlite3StatusSet(int op, int X){
drh78f82d12008-09-02 00:52:52 +000068 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];
drhc4b18b82008-06-19 13:20:01 +000073 }
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*/
83int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
drh78f82d12008-09-02 00:52:52 +000084 wsdStatInit;
85 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
drh413c3d32010-02-23 20:11:56 +000086 return SQLITE_MISUSE_BKPT;
drhc4b18b82008-06-19 13:20:01 +000087 }
drh78f82d12008-09-02 00:52:52 +000088 *pCurrent = wsdStat.nowValue[op];
89 *pHighwater = wsdStat.mxValue[op];
drhc4b18b82008-06-19 13:20:01 +000090 if( resetFlag ){
drh78f82d12008-09-02 00:52:52 +000091 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000092 }
93 return SQLITE_OK;
94}
drh633e6d52008-07-28 19:34:53 +000095
96/*
97** Query status information for a single database connection
98*/
99int 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 }
drh6480aad2008-08-01 16:31:14 +0000115 default: {
116 return SQLITE_ERROR;
117 }
drh633e6d52008-07-28 19:34:53 +0000118 }
119 return SQLITE_OK;
120}