AryaWu/sqlite
0
1# 2008 June 182#3# The author disclaims copyright to this source code. In place of4# a legal notice, here is a blessing:5#6# May you do good and not evil.7# May you find forgiveness for yourself and forgive others.8# May you share freely, never taking more than you give.9#10#***********************************************************************11#12# This file contains tests of the memory allocation subsystem13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17sqlite3_reset_auto_extension18 19# This test assumes that no page-cache buffers are installed20# by default when a new database connection is opened. As a result, it21# will not work with the "memsubsys1" permutation.22#23if {[permutation] == "memsubsys1"} {24 finish_test25 return26}27 28test_set_config_pagecache 0 029 30# This procedure constructs a new database in test.db. It fills31# this database with many small records (enough to force multiple32# rebalance operations in the btree-layer and to require a large33# page cache), verifies correct results, then returns.34#35proc build_test_db {testname pragmas} {36 catch {db close}37 forcedelete test.db test.db-journal38 sqlite3 db test.db39 sqlite3_db_config_lookaside db 0 0 040 db eval $pragmas41 db eval {42 CREATE TABLE t1(x, y);43 CREATE TABLE t2(a, b);44 CREATE INDEX i1 ON t1(x,y);45 INSERT INTO t1 VALUES(1, 100);46 INSERT INTO t1 VALUES(2, 200);47 }48 for {set i 2} {$i<5000} {incr i $i} {49 db eval {INSERT INTO t2 SELECT * FROM t1}50 db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}51 db eval {DELETE FROM t2}52 }53 do_test $testname.1 {54 db eval {SELECT count(*) FROM t1}55 } 819256 integrity_check $testname.257}58 59# Reset all of the highwater marks.60#61proc reset_highwater_marks {} {62 sqlite3_status SQLITE_STATUS_MEMORY_USED 163 sqlite3_status SQLITE_STATUS_MALLOC_SIZE 164 sqlite3_status SQLITE_STATUS_PAGECACHE_USED 165 sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 166 sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 167 sqlite3_status SQLITE_STATUS_SCRATCH_USED 168 sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 169 sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 170 sqlite3_status SQLITE_STATUS_PARSER_STACK 171}72 73set xtra_size 29074 75# Test 1: Both PAGECACHE and SCRATCH are shut down.76#77db close78sqlite3_shutdown79sqlite3_config_lookaside 0 080sqlite3_config_pagecache 0 081sqlite3_initialize82reset_highwater_marks83build_test_db memsubsys1-1 {PRAGMA page_size=1024}84do_test memsubsys1-1.3 {85 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]86} 087do_test memsubsys1-1.4 {88 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]89} 090set max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]91#show_memstats92 93# Test 2: Activate PAGECACHE with 20 pages94#95db close96sqlite3_shutdown97sqlite3_config_pagecache [expr 1024+$xtra_size] 2098sqlite3_initialize99reset_highwater_marks100build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0}101#show_memstats102set MEMORY_MANAGEMENT $sqlite_options(memorymanage)103ifcapable pagecache_overflow_stats {104 ifcapable !malloc_usable_size {105 do_test memsubsys1-2.3 {106 set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]107 } [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024]108 }109}110do_test memsubsys1-2.4 {111 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]112} 20113do_test memsubsys1-2.5 {114 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]115} 0116 117# Test 3: Activate PAGECACHE with 20 pages but use the wrong page size118# so that PAGECACHE is not used.119#120db close121sqlite3_shutdown122sqlite3_config_pagecache [expr 512+$xtra_size] 20123sqlite3_config singlethread124sqlite3_initialize125reset_highwater_marks126build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}127do_test memsubsys1-3.1.3 {128 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]129} 0130do_test memsubsys1-3.1.4 {131 set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]132 # Note: The measured PAGECACHE_OVERFLOW is amount malloc() returns, not what133 # was requested. System malloc() implementations might (arbitrarily) return134 # slightly different oversize buffers, which can result in slightly different135 # PAGECACHE_OVERFLOW sizes between consecutive runs. So we cannot do an136 # exact comparison. Simply verify that the amount is within 5%.137 expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05}138} 1139do_test memsubsys1-3.1.5 {140 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]141} 0142db close143sqlite3_shutdown144sqlite3_config_pagecache [expr 2048+$xtra_size] 20145sqlite3_initialize146reset_highwater_marks147build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}148#show_memstats149do_test memsubsys1-3.2.3 {150 db eval {PRAGMA page_size}151} 2048152do_test memsubsys1-3.2.4 {153 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]154} 20155do_test memsubsys1-3.2.5 {156 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]157} 0158 159# Test 4: Activate PAGECACHE160#161db close162sqlite3_shutdown163sqlite3_config_pagecache [expr 1024+$xtra_size] 50164sqlite3_initialize165reset_highwater_marks166build_test_db memsubsys1-4 {PRAGMA page_size=1024}167#show_memstats168do_test memsubsys1-4.3 {169 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]170 expr {$pg_used>=45 && $pg_used<=50}171} 1172do_test memsubsys1-4.4 {173 set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]174} 0175do_test memsubsys1-4.5 {176 set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]177 expr {$maxreq<9000}178} 1179 180db close181sqlite3_shutdown182sqlite3_config_memstatus 1183sqlite3_config_lookaside 100 500184sqlite3_config serialized185sqlite3_initialize186autoinstall_test_functions187 188test_restore_config_pagecache189finish_test190 