3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # This file contains tests of the memory allocation subsystem
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 sqlite3_reset_auto_extension
19 # This test assumes that no page-cache buffers are installed
20 # by default when a new database connection is opened. As a result, it
21 # will not work with the "memsubsys1" permutation.
23 if {[permutation] == "memsubsys1"} {
28 test_set_config_pagecache 0 0
30 # This procedure constructs a new database in test.db. It fills
31 # this database with many small records (enough to force multiple
32 # rebalance operations in the btree-layer and to require a large
33 # page cache), verifies correct results, then returns.
35 proc build_test_db {testname pragmas} {
37 forcedelete test.db test.db-journal
39 sqlite3_db_config_lookaside db 0 0 0
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);
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}
54 db eval {SELECT count(*) FROM t1}
56 integrity_check $testname.2
59 # Reset all of the highwater marks.
61 proc reset_highwater_marks {} {
62 sqlite3_status SQLITE_STATUS_MEMORY_USED 1
63 sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
64 sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
65 sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
66 sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
67 sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
68 sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
69 sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
70 sqlite3_status SQLITE_STATUS_PARSER_STACK 1
75 # Test 1: Both PAGECACHE and SCRATCH are shut down.
79 sqlite3_config_lookaside 0 0
80 sqlite3_config_pagecache 0 0
83 build_test_db memsubsys1-1 {PRAGMA page_size=1024}
84 do_test memsubsys1-1.3 {
85 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
87 do_test memsubsys1-1.4 {
88 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
90 set max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
93 # Test 2: Activate PAGECACHE with 20 pages
97 sqlite3_config_pagecache [expr 1024+$xtra_size] 20
100 build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0}
102 set MEMORY_MANAGEMENT $sqlite_options(memorymanage)
103 ifcapable 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]
110 do_test memsubsys1-2.4 {
111 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
113 do_test memsubsys1-2.5 {
114 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
117 # Test 3: Activate PAGECACHE with 20 pages but use the wrong page size
118 # so that PAGECACHE is not used.
122 sqlite3_config_pagecache [expr 512+$xtra_size] 20
123 sqlite3_config singlethread
125 reset_highwater_marks
126 build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
127 do_test memsubsys1-3.1.3 {
128 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
130 do_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 what
133 # was requested. System malloc() implementations might (arbitrarily) return
134 # slightly different oversize buffers, which can result in slightly different
135 # PAGECACHE_OVERFLOW sizes between consecutive runs. So we cannot do an
136 # exact comparison. Simply verify that the amount is within 5%.
137 expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05}
139 do_test memsubsys1-3.1.5 {
140 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
144 sqlite3_config_pagecache [expr 2048+$xtra_size] 20
146 reset_highwater_marks
147 build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}
149 do_test memsubsys1-3.2.3 {
150 db eval {PRAGMA page_size}
152 do_test memsubsys1-3.2.4 {
153 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
155 do_test memsubsys1-3.2.5 {
156 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
159 # Test 4: Activate PAGECACHE
163 sqlite3_config_pagecache [expr 1024+$xtra_size] 50
165 reset_highwater_marks
166 build_test_db memsubsys1-4 {PRAGMA page_size=1024}
168 do_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}
172 do_test memsubsys1-4.4 {
173 set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
175 do_test memsubsys1-4.5 {
176 set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
182 sqlite3_config_memstatus 1
183 sqlite3_config_lookaside 100 500
184 sqlite3_config serialized
186 autoinstall_test_functions
188 test_restore_config_pagecache