Skip to content

Commit

Permalink
Add debug output of current cache_size when DEBUG_DATABASE is set
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Nov 17, 2022
1 parent fc821e8 commit 4ed7397
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/database/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ sqlite3* _dbopen(bool create, const char *func, const int line, const char *file
return NULL;
}

// Print database cache size when DEBUG_DATABASE is set
if(config.debug & DEBUG_DATABASE)
print_cache_size(db);

return db;
}

Expand Down Expand Up @@ -634,3 +638,38 @@ const char *get_sqlite3_version(void)
{
return sqlite3_libversion();
}

// Get current cache_size of the specified database connection
static int get_cache_size(sqlite3 *db)
{
// Prepare query
sqlite3_stmt *pragma_stmt = NULL;
int rc = sqlite3_prepare_v2(db, "PRAGMA cache_size", -1, &pragma_stmt, NULL);
if(rc != SQLITE_OK){
logg("get_cache_size() - SQL error prepare: %s", sqlite3_errstr(rc));
return DB_FAILED;
}

// Perform query
rc = sqlite3_step(pragma_stmt);
if(rc != SQLITE_ROW){
logg("get_cache_size() - SQL error step: %s", sqlite3_errstr(rc));
return DB_FAILED;
}

// Get result when there was no error
const int result = sqlite3_column_int(pragma_stmt, 0);

// Finalize prepared statement and return result
sqlite3_finalize(pragma_stmt);
return result;
}

void print_cache_size(sqlite3 *db)
{
const int cs = get_cache_size(db);
if(cs < 0)
logg("Gravity database cache size is %.1f MiB", -1.0*cs/1024);
else
logg("Gravity database cache size is %d", cs);
}
1 change: 1 addition & 0 deletions src/database/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void SQLite3LogCallback(void *pArg, int iErrCode, const char *zMsg);
long int get_max_query_ID(sqlite3 *db);
bool db_update_counters(sqlite3 *db, const int total, const int blocked);
const char *get_sqlite3_version(void);
void print_cache_size(sqlite3 *db);

extern long int lastdbindex;
extern bool DBdeleteoldqueries;
Expand Down
6 changes: 6 additions & 0 deletions src/database/gravity-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "../datastructure.h"
// reset_aliasclient()
#include "aliasclients.h"
// print_cache_size()
#include "common.h"

// Definition of struct regexData
#include "../regex_r.h"
Expand Down Expand Up @@ -140,6 +142,10 @@ bool gravityDB_open(void)
return false;
}

// Print database cache size when DEBUG_DATABASE is set
if(config.debug & DEBUG_DATABASE)
print_cache_size(gravity_db);

// Prepare audit statement
if(config.debug & DEBUG_DATABASE)
logg("gravityDB_open(): Preparing audit query");
Expand Down

0 comments on commit 4ed7397

Please sign in to comment.