blob: a5e45dd33aedecec46d866cc082c3eacce33426d [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**
drh633e6d52008-07-28 19:34:53 +000016** $Id: status.c,v 1.5 2008/07/28 19:34:54 drh Exp $
drhc4b18b82008-06-19 13:20:01 +000017*/
18#include "sqliteInt.h"
19
20/*
21** Variables in which to record status information.
22*/
23static struct {
drhec424a52008-07-25 15:39:03 +000024 int nowValue[7]; /* Current value */
25 int mxValue[7]; /* Maximum value */
drhc4b18b82008-06-19 13:20:01 +000026} sqlite3Stat;
27
28
29/*
30** Reset the status records. This routine is called by
31** sqlite3_initialize().
32*/
33void sqlite3StatusReset(void){
34 memset(&sqlite3Stat, 0, sizeof(sqlite3Stat));
35}
36
37/*
38** Return the current value of a status parameter.
39*/
40int 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*/
49void 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*/
60void 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*/
75int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
drhc4b18b82008-06-19 13:20:01 +000076 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}
drh633e6d52008-07-28 19:34:53 +000086
87/*
88** Query status information for a single database connection
89*/
90int 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}