blob: f1a54df2e0e3c20c80f18e45f0fd2f99c9208e4a [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.
15**
drh78f82d12008-09-02 00:52:52 +000016** $Id: status.c,v 1.9 2008/09/02 00:52:52 drh Exp $
drhc4b18b82008-06-19 13:20:01 +000017*/
18#include "sqliteInt.h"
19
20/*
21** Variables in which to record status information.
22*/
drh78f82d12008-09-02 00:52:52 +000023typedef struct sqlite3StatType sqlite3StatType;
24static SQLITE_WSD struct sqlite3StatType {
drhe50135e2008-08-05 17:53:22 +000025 int nowValue[9]; /* Current value */
26 int mxValue[9]; /* Maximum value */
drh78f82d12008-09-02 00:52:52 +000027} sqlite3Stat = { {0,}, {0,} };
drhc4b18b82008-06-19 13:20:01 +000028
29
drh78f82d12008-09-02 00:52:52 +000030/* The "wsdStat" macro will resolve to the status information
31** state vector. If writable static data is unsupported on the target,
32** we have to locate the state vector at run-time. In the more common
33** case where writable static data is supported, wsdStat can refer directly
34** to the "sqlite3Stat" state vector declared above.
35*/
36#ifdef SQLITE_OMIT_WSD
37# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
38# define wsdStat x[0]
39#else
40# define wsdStatInit
41# define wsdStat sqlite3Stat
42#endif
43
drhc4b18b82008-06-19 13:20:01 +000044/*
drhc4b18b82008-06-19 13:20:01 +000045** Return the current value of a status parameter.
46*/
47int sqlite3StatusValue(int op){
drh78f82d12008-09-02 00:52:52 +000048 wsdStatInit;
49 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
50 return wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000051}
52
53/*
54** Add N to the value of a status record. It is assumed that the
55** caller holds appropriate locks.
56*/
57void sqlite3StatusAdd(int op, int N){
drh78f82d12008-09-02 00:52:52 +000058 wsdStatInit;
59 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
60 wsdStat.nowValue[op] += N;
61 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
62 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000063 }
64}
65
66/*
67** Set the value of a status to X.
68*/
69void sqlite3StatusSet(int op, int X){
drh78f82d12008-09-02 00:52:52 +000070 wsdStatInit;
71 assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
72 wsdStat.nowValue[op] = X;
73 if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
74 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000075 }
76}
77
78/*
79** Query status information.
80**
81** This implementation assumes that reading or writing an aligned
82** 32-bit integer is an atomic operation. If that assumption is not true,
83** then this routine is not threadsafe.
84*/
85int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
drh78f82d12008-09-02 00:52:52 +000086 wsdStatInit;
87 if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
drhc4b18b82008-06-19 13:20:01 +000088 return SQLITE_MISUSE;
89 }
drh78f82d12008-09-02 00:52:52 +000090 *pCurrent = wsdStat.nowValue[op];
91 *pHighwater = wsdStat.mxValue[op];
drhc4b18b82008-06-19 13:20:01 +000092 if( resetFlag ){
drh78f82d12008-09-02 00:52:52 +000093 wsdStat.mxValue[op] = wsdStat.nowValue[op];
drhc4b18b82008-06-19 13:20:01 +000094 }
95 return SQLITE_OK;
96}
drh633e6d52008-07-28 19:34:53 +000097
98/*
99** Query status information for a single database connection
100*/
101int sqlite3_db_status(
102 sqlite3 *db, /* The database connection whose status is desired */
103 int op, /* Status verb */
104 int *pCurrent, /* Write current value here */
105 int *pHighwater, /* Write high-water mark here */
106 int resetFlag /* Reset high-water mark if true */
107){
108 switch( op ){
109 case SQLITE_DBSTATUS_LOOKASIDE_USED: {
110 *pCurrent = db->lookaside.nOut;
111 *pHighwater = db->lookaside.mxOut;
112 if( resetFlag ){
113 db->lookaside.mxOut = db->lookaside.nOut;
114 }
115 break;
116 }
drh6480aad2008-08-01 16:31:14 +0000117 default: {
118 return SQLITE_ERROR;
119 }
drh633e6d52008-07-28 19:34:53 +0000120 }
121 return SQLITE_OK;
122}