Skip to content

Commit ab2fc3d

Browse files
wjlroeflavorjones
authored andcommitted
Add SQLite3.status to call sqlite3_status
This function is useful for understanding the runtime performance of sqlite. This is a very thin layer on top of the sqlite3 library's function, so it allows full usage of it from Ruby.
1 parent 92e5c16 commit ab2fc3d

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

ext/sqlite3/sqlite3.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ threadsafe_p(VALUE UNUSED(klass))
8585
return INT2NUM(sqlite3_threadsafe());
8686
}
8787

88+
static VALUE
89+
status_p(VALUE UNUSED(klass), VALUE opArg, VALUE resetFlagArg)
90+
{
91+
int op = NUM2INT(opArg);
92+
bool resetFlag = TYPE(resetFlagArg) == T_TRUE;
93+
94+
int pCurrent = 0;
95+
int pHighwater = 0;
96+
sqlite3_status(op, &pCurrent, &pHighwater, resetFlag);
97+
98+
VALUE hash = rb_hash_new();
99+
rb_hash_aset(hash, ID2SYM(rb_intern("current")), INT2FIX(pCurrent));
100+
rb_hash_aset(hash, ID2SYM(rb_intern("highwater")), INT2FIX(pHighwater));
101+
return hash;
102+
}
103+
88104
void
89105
init_sqlite3_constants(void)
90106
{
@@ -164,6 +180,7 @@ Init_sqlite3_native(void)
164180
rb_define_singleton_method(mSqlite3, "sqlcipher?", using_sqlcipher, 0);
165181
rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
166182
rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0);
183+
rb_define_singleton_method(mSqlite3, "status", status_p, 2);
167184
rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));
168185
rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));
169186
rb_define_const(mSqlite3, "SQLITE_LOADED_VERSION", rb_str_new2(sqlite3_libversion()));

lib/sqlite3/constants.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,18 @@ module ErrorCode
116116
# sqlite_step() has finished executing
117117
DONE = 101
118118
end
119+
120+
module Status
121+
MEMORY_USED = 0 # This parameter is the current amount of memory checked out using sqlite3_malloc(), either directly or indirectly. The figure includes calls made to sqlite3_malloc() by the application and internal memory usage by the SQLite library. Auxiliary page-cache memory controlled by SQLITE_CONFIG_PAGECACHE is not included in this parameter. The amount returned is the sum of the allocation sizes as reported by the xSize method in sqlite3_mem_methods.
122+
PAGECACHE_USED = 1 # This parameter returns the number of pages used out of the pagecache memory allocator that was configured using SQLITE_CONFIG_PAGECACHE. The value returned is in pages, not in bytes.
123+
PAGECACHE_OVERFLOW = 2 # This parameter returns the number of bytes of page cache allocation which could not be satisfied by the SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc(). The returned value includes allocations that overflowed because they where too large (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations that overflowed because no space was left in the page cache.
124+
SCRATCH_USED = 3 # NOT USED
125+
SCRATCH_OVERFLOW = 4 # NOT USED
126+
MALLOC_SIZE = 5 # This parameter records the largest memory allocation request handed to sqlite3_malloc() or sqlite3_realloc() (or their internal equivalents). Only the value returned in the *pHighwater parameter to sqlite3_status() is of interest. The value written into the *pCurrent parameter is undefined.
127+
PARSER_STACK = 6 # The *pHighwater parameter records the deepest parser stack. The *pCurrent value is undefined. The *pHighwater value is only meaningful if SQLite is compiled with YYTRACKMAXSTACKDEPTH.
128+
PAGECACHE_SIZE = 7 # This parameter records the largest memory allocation request handed to the pagecache memory allocator. Only the value returned in the *pHighwater parameter to sqlite3_status() is of interest. The value written into the *pCurrent parameter is undefined.
129+
SCRATCH_SIZE = 8 # NOT USED
130+
MALLOC_COUNT = 9 # This parameter records the number of separate memory allocations currently checked out.
131+
end
119132
end
120133
end

test/test_sqlite3.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ def test_threadsafe?
2121
def test_compiled_version_and_loaded_version
2222
assert_equal(SQLite3::SQLITE_VERSION, SQLite3::SQLITE_LOADED_VERSION)
2323
end
24+
25+
def test_status
26+
status = SQLite3.status(SQLite3::Constants::Status::MEMORY_USED, false)
27+
assert_not_nil(status.fetch(:current))
28+
assert_not_nil(status.fetch(:highwater))
29+
end
2430
end
2531
end

0 commit comments

Comments
 (0)