This module implements the sqlite3_status() interface and related
functionality.
*/
#include "sqliteInt.h"
#include "vdbeInt.h"
/*
Variables in which to record status information.
*/
#if SQLITE_PTRSIZE>4
typedef sqlite3_int64 sqlite3StatValueType;
#else
typedef u32 sqlite3StatValueType;
#endif
typedef struct sqlite3StatType sqlite3StatType;
static SQLITE_WSD struct sqlite3StatType {
sqlite3StatValueType nowValue[10]; /* Current value */
sqlite3StatValueType mxValue[10]; /* Maximum value */
} sqlite3Stat = { {0,}, {0,} };
/*
Elements of sqlite3Stat[] are protected by either the memory allocator
mutex, or by the pcache1 mutex. The following array determines which.
*/
sqlite3Stat数组中的元素要么被内存分配器中的互斥变量保护,要么被pcache1中的互斥变量保护。值为1的被pcache1中的互斥变量保护,值为0的被内存分配器中的互斥变量保护
static const char statMutex[] = {
0, /* SQLITE_STATUS_MEMORY_USED */ memory_used
1, /* SQLITE_STATUS_PAGECACHE_USED */ pagecache_used
1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */ pagecache_overflow
0, /* SQLITE_STATUS_SCRATCH_USED */ scratch_used
0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */ scratch_overflow
0, /* SQLITE_STATUS_MALLOC_SIZE */ malloc_size
0, /* SQLITE_STATUS_PARSER_STACK */ parser_stack
1, /* SQLITE_STATUS_PAGECACHE_SIZE */ pagecache_size
0, /* SQLITE_STATUS_SCRATCH_SIZE */ scratch_size
0, /* SQLITE_STATUS_MALLOC_COUNT */ malloc_count
};
/* The "wsdStat" macro will resolve to the status information
state vector. If writable static data is unsupported on the target,
we have to locate the state vector at run-time. In the more common
case where writable static data is supported, wsdStat can refer directly
to the "sqlite3Stat" state vector declared above.
*/
wsdStat宏会处理状态数组的状态信息。如果WSD不被支持,那么就需要在运行时分配堆栈。在一般情况下WSD是被支持的,这时可以直接通过sqlite3Stat来访问上面的状态数组
#ifdef SQLITE_OMIT_WSD
# define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
# define wsdStat x[0]
#else
# define wsdStatInit
# define wsdStat sqlite3Stat
#endif
/*
Return the current value of a status parameter. The caller must
be holding the appropriate mutex.
*/
返回参数op代表的那个参数的状态值。判断的时候需要首先获得对应的互斥变量
sqlite3_int64 sqlite3StatusValue(int op){
wsdStatInit;
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
assert( op>=0 && op<ArraySize(statMutex) );
assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
: sqlite3MallocMutex()) );
return wsdStat.nowValue[op];
}
/*
Add N to the value of a status record. The caller must hold the
appropriate mutex. (Locking is checked by assert()).
The StatusUp() routine can accept positive or negative values for N.
The value of N is added to the current status value and the high-water
mark is adjusted if necessary.
The StatusDown() routine lowers the current value by N. The highwater
mark is unchanged. N must be non-negative for StatusDown().
*/
给状态值+N,调用者需要获得对应的互斥变量(在assert中检查锁)。
void sqlite3StatusUp(int op, int N){//N可正可负
wsdStatInit;
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
assert( op>=0 && op<ArraySize(statMutex) );
assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
: sqlite3MallocMutex()) );
wsdStat.nowValue[op] += N;
if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
wsdStat.mxValue[op] = wsdStat.nowValue[op];//更新最大值
}
}
void sqlite3StatusDown(int op, int N){//N必须>=0
wsdStatInit;
assert( N>=0 );
assert( op>=0 && op<ArraySize(statMutex) );
assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
: sqlite3MallocMutex()) );
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
wsdStat.nowValue[op] -= N;
}
/*
Adjust the highwater mark if necessary.
The caller must hold the appropriate mutex.
*/
如果需要,就对highwater mark进行调整
void sqlite3StatusHighwater(int op, int X){
sqlite3StatValueType newValue;
wsdStatInit;
assert( X>=0 );
newValue = (sqlite3StatValueType)X;
assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
assert( op>=0 && op<ArraySize(statMutex) );
assert( sqlite3_mutex_held(statMutex[op] ? sqlite3Pcache1Mutex()
: sqlite3MallocMutex()) );
assert( op==SQLITE_STATUS_MALLOC_SIZE
|| op==SQLITE_STATUS_PAGECACHE_SIZE
|| op==SQLITE_STATUS_PARSER_STACK );
if( newValue>wsdStat.mxValue[op] ){
wsdStat.mxValue[op] = newValue;//调整highwater mark
}
}