sl@0: # 2008 June 18 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file contains tests of the memory allocation subsystem. sl@0: # sl@0: # $Id: memsubsys2.test,v 1.2 2008/08/12 15:21:12 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sqlite3_reset_auto_extension sl@0: sl@0: # This procedure constructs a new database in test.db. It fills sl@0: # this database with many small records (enough to force multiple sl@0: # rebalance operations in the btree-layer and to require a large sl@0: # page cache), verifies correct results, then returns. sl@0: # sl@0: proc build_test_db {testname pragmas} { sl@0: catch {db close} sl@0: file delete -force test.db test.db-journal sl@0: sqlite3 db test.db sl@0: db eval $pragmas sl@0: db eval { sl@0: CREATE TABLE t1(x, y); sl@0: CREATE TABLE t2(a, b); sl@0: CREATE INDEX i1 ON t1(x,y); sl@0: INSERT INTO t1 VALUES(1, 100); sl@0: INSERT INTO t1 VALUES(2, 200); sl@0: } sl@0: for {set i 2} {$i<5000} {incr i $i} { sl@0: db eval {INSERT INTO t2 SELECT * FROM t1} sl@0: db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2} sl@0: db eval {DELETE FROM t2} sl@0: } sl@0: do_test $testname.1 { sl@0: db eval {SELECT count(*) FROM t1} sl@0: } 8192 sl@0: integrity_check $testname.2 sl@0: } sl@0: sl@0: # Reset all of the highwater marks. sl@0: # sl@0: proc reset_highwater_marks {} { sl@0: sqlite3_status SQLITE_STATUS_MEMORY_USED 1 sl@0: sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1 sl@0: sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1 sl@0: sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1 sl@0: sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1 sl@0: sqlite3_status SQLITE_STATUS_SCRATCH_USED 1 sl@0: sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1 sl@0: sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1 sl@0: sqlite3_status SQLITE_STATUS_PARSER_STACK 1 sl@0: } sl@0: sl@0: # Test 1: Verify that calling sqlite3_malloc(0) returns a NULL sl@0: # pointer. sl@0: # sl@0: set highwater [sqlite3_memory_highwater 0] sl@0: do_test memsubsys2-1.1 { sl@0: sqlite3_malloc 0 sl@0: } {0} sl@0: do_test memsubsys2-1.2 { sl@0: sqlite3_memory_highwater 0 sl@0: } $highwater sl@0: sl@0: sl@0: # Test 2: Verify that the highwater mark increases after a large sl@0: # allocation. sl@0: # sl@0: sqlite3_memory_highwater 1 sl@0: set highwater [sqlite3_memory_highwater 0] sl@0: do_test memsubsys2-2.1 { sl@0: sqlite3_free [set x [sqlite3_malloc 100000]] sl@0: expr {$x!="0"} sl@0: } {1} sl@0: do_test memsubsys2-2.2 { sl@0: expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+$highwater} sl@0: } {1} sl@0: sl@0: # Test 3: Verify that turning of memstatus disables the statistics sl@0: # tracking. sl@0: # sl@0: db close sl@0: sqlite3_shutdown sl@0: sqlite3_config_memstatus 0 sl@0: sqlite3_initialize sl@0: reset_highwater_marks sl@0: set highwater [sqlite3_memory_highwater 0] sl@0: do_test memsubsys2-3.1 { sl@0: set highwater sl@0: } {0} sl@0: do_test memsubsys2-3.2 { sl@0: sqlite3_malloc 0 sl@0: } {0} sl@0: do_test memsubsys2-3.3 { sl@0: sqlite3_memory_highwater 0 sl@0: } {0} sl@0: do_test memsubsys2-3.4 { sl@0: sqlite3_memory_used sl@0: } {0} sl@0: do_test memsubsys2-3.5 { sl@0: set ::allocation [sqlite3_malloc 100000] sl@0: expr {$::allocation!="0"} sl@0: } {1} sl@0: do_test memsubsys2-3.6 { sl@0: sqlite3_memory_highwater 0 sl@0: } {0} sl@0: do_test memsubsys2-3.7 { sl@0: sqlite3_memory_used sl@0: } {0} sl@0: do_test memsubsys2-3.8 { sl@0: sqlite3_free $::allocation sl@0: } {} sl@0: do_test memsubsys2-3.9 { sl@0: sqlite3_free 0 sl@0: } {} sl@0: sl@0: sl@0: # Test 4: Verify that turning on memstatus reenables the statistics sl@0: # tracking. sl@0: # sl@0: sqlite3_shutdown sl@0: sqlite3_config_memstatus 1 sl@0: sqlite3_initialize sl@0: reset_highwater_marks sl@0: set highwater [sqlite3_memory_highwater 0] sl@0: do_test memsubsys2-4.1 { sl@0: set highwater sl@0: } {0} sl@0: do_test memsubsys2-4.2 { sl@0: sqlite3_malloc 0 sl@0: } {0} sl@0: do_test memsubsys2-4.3 { sl@0: sqlite3_memory_highwater 0 sl@0: } {0} sl@0: do_test memsubsys2-4.4 { sl@0: sqlite3_memory_used sl@0: } {0} sl@0: do_test memsubsys2-4.5 { sl@0: set ::allocation [sqlite3_malloc 100000] sl@0: expr {$::allocation!="0"} sl@0: } {1} sl@0: do_test memsubsys2-4.6 { sl@0: expr {[sqlite3_memory_highwater 0]>=100000} sl@0: } {1} sl@0: do_test memsubsys2-4.7 { sl@0: expr {[sqlite3_memory_used]>=100000} sl@0: } {1} sl@0: do_test memsubsys2-4.8 { sl@0: sqlite3_free $::allocation sl@0: } {} sl@0: do_test memsubsys2-4.9 { sl@0: sqlite3_free 0 sl@0: } {} sl@0: do_test memsubsys2-4.10 { sl@0: expr {[sqlite3_memory_highwater 0]>=100000} sl@0: } {1} sl@0: do_test memsubsys2-4.11 { sl@0: sqlite3_memory_used sl@0: } {0} sl@0: sl@0: sl@0: sl@0: sl@0: autoinstall_test_functions sl@0: finish_test