1f7141990Sdrh# 2008 June 18 2f7141990Sdrh# 3f7141990Sdrh# The author disclaims copyright to this source code. In place of 4f7141990Sdrh# a legal notice, here is a blessing: 5f7141990Sdrh# 6f7141990Sdrh# May you do good and not evil. 7f7141990Sdrh# May you find forgiveness for yourself and forgive others. 8f7141990Sdrh# May you share freely, never taking more than you give. 9f7141990Sdrh# 10f7141990Sdrh#*********************************************************************** 11f7141990Sdrh# 12f7141990Sdrh# This file contains tests of the memory allocation subsystem 13f7141990Sdrh# 14f7141990Sdrh 15f7141990Sdrhset testdir [file dirname $argv0] 16f7141990Sdrhsource $testdir/tester.tcl 1755b0cf00Sdrhsqlite3_reset_auto_extension 18f7141990Sdrh 19b2a0f75cSdrh# This test assumes that no page-cache buffers are installed 20d53850e5Sdan# by default when a new database connection is opened. As a result, it 21d53850e5Sdan# will not work with the "memsubsys1" permutation. 22d53850e5Sdan# 23d53850e5Sdanif {[permutation] == "memsubsys1"} { 24d53850e5Sdan finish_test 25d53850e5Sdan return 26d53850e5Sdan} 27d53850e5Sdan 2803bc525aSdantest_set_config_pagecache 0 0 2903bc525aSdan 30f7141990Sdrh# This procedure constructs a new database in test.db. It fills 31f7141990Sdrh# this database with many small records (enough to force multiple 32f7141990Sdrh# rebalance operations in the btree-layer and to require a large 33f7141990Sdrh# page cache), verifies correct results, then returns. 34f7141990Sdrh# 35f7141990Sdrhproc build_test_db {testname pragmas} { 36f7141990Sdrh catch {db close} 37fda06befSmistachkin forcedelete test.db test.db-journal 38f7141990Sdrh sqlite3 db test.db 39e9d1c720Sdrh sqlite3_db_config_lookaside db 0 0 0 40f7141990Sdrh db eval $pragmas 41f7141990Sdrh db eval { 42f7141990Sdrh CREATE TABLE t1(x, y); 43f7141990Sdrh CREATE TABLE t2(a, b); 44f7141990Sdrh CREATE INDEX i1 ON t1(x,y); 45f7141990Sdrh INSERT INTO t1 VALUES(1, 100); 46f7141990Sdrh INSERT INTO t1 VALUES(2, 200); 47f7141990Sdrh } 48f7141990Sdrh for {set i 2} {$i<5000} {incr i $i} { 49f7141990Sdrh db eval {INSERT INTO t2 SELECT * FROM t1} 50f7141990Sdrh db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2} 51f7141990Sdrh db eval {DELETE FROM t2} 52f7141990Sdrh } 53f7141990Sdrh do_test $testname.1 { 54f7141990Sdrh db eval {SELECT count(*) FROM t1} 55f7141990Sdrh } 8192 56f7141990Sdrh integrity_check $testname.2 57f7141990Sdrh} 58f7141990Sdrh 5993ed56d9Sdrh# Reset all of the highwater marks. 6093ed56d9Sdrh# 6193ed56d9Sdrhproc reset_highwater_marks {} { 6293ed56d9Sdrh sqlite3_status SQLITE_STATUS_MEMORY_USED 1 6393ed56d9Sdrh sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1 6493ed56d9Sdrh sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1 6593ed56d9Sdrh sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1 6693ed56d9Sdrh sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1 6793ed56d9Sdrh sqlite3_status SQLITE_STATUS_SCRATCH_USED 1 6893ed56d9Sdrh sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1 6993ed56d9Sdrh sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1 7093ed56d9Sdrh sqlite3_status SQLITE_STATUS_PARSER_STACK 1 7193ed56d9Sdrh} 7293ed56d9Sdrh 73870c0178Sdrhset xtra_size 290 748c0a791aSdanielk1977 75f7141990Sdrh# Test 1: Both PAGECACHE and SCRATCH are shut down. 76f7141990Sdrh# 77f7141990Sdrhdb close 78f7141990Sdrhsqlite3_shutdown 79633e6d57Sdrhsqlite3_config_lookaside 0 0 80c54357ccSdrhsqlite3_config_pagecache 0 0 81f7141990Sdrhsqlite3_initialize 8293ed56d9Sdrhreset_highwater_marks 83f7141990Sdrhbuild_test_db memsubsys1-1 {PRAGMA page_size=1024} 84f7141990Sdrhdo_test memsubsys1-1.3 { 85f7141990Sdrh set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] 86f7141990Sdrh} 0 87f7141990Sdrhdo_test memsubsys1-1.4 { 88f7141990Sdrh set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] 89f7141990Sdrh} 0 90f7141990Sdrhset max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] 91f7141990Sdrh#show_memstats 92f7141990Sdrh 93f7141990Sdrh# Test 2: Activate PAGECACHE with 20 pages 94f7141990Sdrh# 95f7141990Sdrhdb close 96f7141990Sdrhsqlite3_shutdown 978c0a791aSdanielk1977sqlite3_config_pagecache [expr 1024+$xtra_size] 20 98f7141990Sdrhsqlite3_initialize 9993ed56d9Sdrhreset_highwater_marks 1003492f4f9Sdanbuild_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0} 101f7141990Sdrh#show_memstats 102986d3b95Sdrhset MEMORY_MANAGEMENT $sqlite_options(memorymanage) 103b99185f2Sdrhifcapable pagecache_overflow_stats { 1046a8ab6d9Sdrh ifcapable !malloc_usable_size { 105f7141990Sdrh do_test memsubsys1-2.3 { 106f7141990Sdrh set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] 107ff07ccadSdan } [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024] 1086a8ab6d9Sdrh } 109b99185f2Sdrh} 110f7141990Sdrhdo_test memsubsys1-2.4 { 111f7141990Sdrh set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] 11250d1b5f3Sdrh} 20 113f7141990Sdrhdo_test memsubsys1-2.5 { 114f7141990Sdrh set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] 115f7141990Sdrh} 0 116f7141990Sdrh 117f7141990Sdrh# Test 3: Activate PAGECACHE with 20 pages but use the wrong page size 118f7141990Sdrh# so that PAGECACHE is not used. 119f7141990Sdrh# 120f7141990Sdrhdb close 121f7141990Sdrhsqlite3_shutdown 1228c0a791aSdanielk1977sqlite3_config_pagecache [expr 512+$xtra_size] 20 123c54357ccSdrhsqlite3_config singlethread 124f7141990Sdrhsqlite3_initialize 12593ed56d9Sdrhreset_highwater_marks 126f7141990Sdrhbuild_test_db memsubsys1-3.1 {PRAGMA page_size=1024} 127f7141990Sdrhdo_test memsubsys1-3.1.3 { 128f7141990Sdrh set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] 129f7141990Sdrh} 0 130f7141990Sdrhdo_test memsubsys1-3.1.4 { 131f7141990Sdrh set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] 132dfd1547bSdrh # Note: The measured PAGECACHE_OVERFLOW is amount malloc() returns, not what 133dfd1547bSdrh # was requested. System malloc() implementations might (arbitrarily) return 134dfd1547bSdrh # slightly different oversize buffers, which can result in slightly different 135dfd1547bSdrh # PAGECACHE_OVERFLOW sizes between consecutive runs. So we cannot do an 136dfd1547bSdrh # exact comparison. Simply verify that the amount is within 5%. 137dfd1547bSdrh expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05} 138dfd1547bSdrh} 1 139f7141990Sdrhdo_test memsubsys1-3.1.5 { 140f7141990Sdrh set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] 141f7141990Sdrh} 0 142f7141990Sdrhdb close 143f7141990Sdrhsqlite3_shutdown 1448c0a791aSdanielk1977sqlite3_config_pagecache [expr 2048+$xtra_size] 20 145f7141990Sdrhsqlite3_initialize 14693ed56d9Sdrhreset_highwater_marks 147f7141990Sdrhbuild_test_db memsubsys1-3.2 {PRAGMA page_size=2048} 148f7141990Sdrh#show_memstats 149f7141990Sdrhdo_test memsubsys1-3.2.3 { 150f7141990Sdrh db eval {PRAGMA page_size} 151f7141990Sdrh} 2048 152f7141990Sdrhdo_test memsubsys1-3.2.4 { 153f7141990Sdrh set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] 15450d1b5f3Sdrh} 20 155f7141990Sdrhdo_test memsubsys1-3.2.5 { 156f7141990Sdrh set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2] 157f7141990Sdrh} 0 158f7141990Sdrh 159b2a0f75cSdrh# Test 4: Activate PAGECACHE 160f7141990Sdrh# 161f7141990Sdrhdb close 162f7141990Sdrhsqlite3_shutdown 1638c0a791aSdanielk1977sqlite3_config_pagecache [expr 1024+$xtra_size] 50 164f7141990Sdrhsqlite3_initialize 16593ed56d9Sdrhreset_highwater_marks 166f7141990Sdrhbuild_test_db memsubsys1-4 {PRAGMA page_size=1024} 167f7141990Sdrh#show_memstats 168f7141990Sdrhdo_test memsubsys1-4.3 { 169f7141990Sdrh set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2] 17050d1b5f3Sdrh expr {$pg_used>=45 && $pg_used<=50} 17150d1b5f3Sdrh} 1 172f7141990Sdrhdo_test memsubsys1-4.4 { 173f7141990Sdrh set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2] 17450d1b5f3Sdrh} 0 175f7141990Sdrhdo_test memsubsys1-4.5 { 176f7141990Sdrh set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2] 177*49d77ee6Sdrh expr {$maxreq<9000} 178f7141990Sdrh} 1 17973cd2730Sdrh 18073cd2730Sdrhdb close 18173cd2730Sdrhsqlite3_shutdown 18273cd2730Sdrhsqlite3_config_memstatus 1 183633e6d57Sdrhsqlite3_config_lookaside 100 500 184c54357ccSdrhsqlite3_config serialized 185af837586Sdrhsqlite3_initialize 18655b0cf00Sdrhautoinstall_test_functions 18703bc525aSdan 18803bc525aSdantest_restore_config_pagecache 189f7141990Sdrhfinish_test 190