1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-09-02 12:21:26 +03:00

Add the sqlite3_status64() interface. Make the new interface and the legacy

sqlite3_status() both atomic and threadsafe.  Check threadsafety using
assert()s.

FossilOrigin-Name: 1ce8e8fa4b866aafa12b1da0eb4d02321af9293e
This commit is contained in:
drh
2015-03-23 17:25:18 +00:00
parent 74893a4cdb
commit af89fe66ea
8 changed files with 144 additions and 55 deletions

View File

@@ -21,10 +21,27 @@
*/
typedef struct sqlite3StatType sqlite3StatType;
static SQLITE_WSD struct sqlite3StatType {
int nowValue[10]; /* Current value */
int mxValue[10]; /* Maximum value */
sqlite3_int64 nowValue[10]; /* Current value */
sqlite3_int64 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.
*/
static const char statMutex[] = {
0, /* SQLITE_STATUS_MEMORY_USED */
1, /* SQLITE_STATUS_PAGECACHE_USED */
1, /* SQLITE_STATUS_PAGECACHE_OVERFLOW */
0, /* SQLITE_STATUS_SCRATCH_USED */
0, /* SQLITE_STATUS_SCRATCH_OVERFLOW */
0, /* SQLITE_STATUS_MALLOC_SIZE */
0, /* SQLITE_STATUS_PARSER_STACK */
1, /* SQLITE_STATUS_PAGECACHE_SIZE */
0, /* SQLITE_STATUS_SCRATCH_SIZE */
0, /* SQLITE_STATUS_MALLOC_COUNT */
};
/* The "wsdStat" macro will resolve to the status information
** state vector. If writable static data is unsupported on the target,
@@ -41,33 +58,60 @@ static SQLITE_WSD struct sqlite3StatType {
#endif
/*
** Return the current value of a status parameter.
** Return the current value of a status parameter. The caller must
** be holding the appropriate mutex.
*/
int sqlite3StatusValue(int 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. It is assumed that the
** caller holds appropriate locks.
** 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().
*/
void sqlite3StatusAdd(int op, int N){
void sqlite3StatusUp(int op, int 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){
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;
}
/*
** Set the value of a status to X.
** Set the value of a status to X. The highwater mark is adjusted if
** necessary. The caller must hold the appropriate mutex.
*/
void sqlite3StatusSet(int op, int X){
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] = X;
if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
wsdStat.mxValue[op] = wsdStat.nowValue[op];
@@ -76,26 +120,42 @@ void sqlite3StatusSet(int op, int X){
/*
** Query status information.
**
** This implementation assumes that reading or writing an aligned
** 32-bit integer is an atomic operation. If that assumption is not true,
** then this routine is not threadsafe.
*/
int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
int sqlite3_status64(
int op,
sqlite3_int64 *pCurrent,
sqlite3_int64 *pHighwater,
int resetFlag
){
wsdStatInit;
sqlite3_mutex *pMutex;
if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
return SQLITE_MISUSE_BKPT;
}
#ifdef SQLITE_ENABLE_API_ARMOR
if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
#endif
pMutex = statMutex[op] ? sqlite3Pcache1Mutex() : sqlite3MallocMutex();
sqlite3_mutex_enter(pMutex);
*pCurrent = wsdStat.nowValue[op];
*pHighwater = wsdStat.mxValue[op];
if( resetFlag ){
wsdStat.mxValue[op] = wsdStat.nowValue[op];
}
sqlite3_mutex_leave(pMutex);
return SQLITE_OK;
}
int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
sqlite3_int64 iCur, iHwtr;
int rc;
#ifdef SQLITE_ENABLE_API_ARMOR
if( pCurrent==0 || pHighwater==0 ) return SQLITE_MISUSE_BKPT;
#endif
rc = sqlite3_status64(op, &iCur, &iHwtr, resetFlag);
*pCurrent = (int)iCur;
*pHighwater = (int)iHwtr;
return rc;
}
/*
** Query status information for a single database connection