sqlite实现sqlite3_status接口

本文详细介绍了SQLite3数据库管理系统中的状态跟踪模块实现。该模块通过维护当前值和最大值记录数据库运行过程中的关键状态参数,如内存使用情况、页缓存状态等。文章还深入解析了如何在保证线程安全的情况下更新这些状态参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值