blob: 41bc11526944f8f7ad86ed1acb13ed348d62e4f6 [file] [log] [blame]
drhf7141992008-06-19 00:16:08 +00001# 2008 June 18
2#
3# The author disclaims copyright to this source code. In place of
4# 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 subsystem
13#
drhf7141992008-06-19 00:16:08 +000014
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
drh55b0cf02008-06-19 17:54:33 +000017sqlite3_reset_auto_extension
drhf7141992008-06-19 00:16:08 +000018
drhb2a0f752017-08-28 15:51:35 +000019# This test assumes that no page-cache buffers are installed
dand53850e2010-07-03 19:08:19 +000020# by default when a new database connection is opened. As a result, it
21# will not work with the "memsubsys1" permutation.
22#
23if {[permutation] == "memsubsys1"} {
24 finish_test
25 return
26}
27
dan03bc5252015-07-24 14:17:17 +000028test_set_config_pagecache 0 0
29
drhf7141992008-06-19 00:16:08 +000030# 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.
34#
35proc build_test_db {testname pragmas} {
36 catch {db close}
mistachkinfda06be2011-08-02 00:57:34 +000037 forcedelete test.db test.db-journal
drhf7141992008-06-19 00:16:08 +000038 sqlite3 db test.db
drhe9d1c722008-08-04 20:13:26 +000039 sqlite3_db_config_lookaside db 0 0 0
drhf7141992008-06-19 00:16:08 +000040 db eval $pragmas
41 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 } 8192
56 integrity_check $testname.2
57}
58
drh93ed56d2008-08-12 15:21:11 +000059# Reset all of the highwater marks.
60#
61proc 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
71}
72
drh870c0172011-11-16 18:08:07 +000073set xtra_size 290
danielk19778c0a7912008-08-20 14:49:23 +000074
drhf7141992008-06-19 00:16:08 +000075# Test 1: Both PAGECACHE and SCRATCH are shut down.
76#
77db close
78sqlite3_shutdown
drh633e6d52008-07-28 19:34:53 +000079sqlite3_config_lookaside 0 0
drhc54357c2015-07-07 14:06:18 +000080sqlite3_config_pagecache 0 0
drhf7141992008-06-19 00:16:08 +000081sqlite3_initialize
drh93ed56d2008-08-12 15:21:11 +000082reset_highwater_marks
drhf7141992008-06-19 00:16:08 +000083build_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} 0
87do_test memsubsys1-1.4 {
88 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
89} 0
90set max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
91#show_memstats
92
93# Test 2: Activate PAGECACHE with 20 pages
94#
95db close
96sqlite3_shutdown
danielk19778c0a7912008-08-20 14:49:23 +000097sqlite3_config_pagecache [expr 1024+$xtra_size] 20
drhf7141992008-06-19 00:16:08 +000098sqlite3_initialize
drh93ed56d2008-08-12 15:21:11 +000099reset_highwater_marks
dan3492f4f2013-07-06 18:07:57 +0000100build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0}
drhf7141992008-06-19 00:16:08 +0000101#show_memstats
drh986d3b92011-01-18 16:13:27 +0000102set MEMORY_MANAGEMENT $sqlite_options(memorymanage)
drhb99185f2016-03-18 00:19:48 +0000103ifcapable 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 }
drh6a8ab6d2011-11-09 01:53:25 +0000109}
drhf7141992008-06-19 00:16:08 +0000110do_test memsubsys1-2.4 {
111 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
drh50d1b5f2010-08-27 12:21:06 +0000112} 20
drhf7141992008-06-19 00:16:08 +0000113do_test memsubsys1-2.5 {
114 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
115} 0
116
117# Test 3: Activate PAGECACHE with 20 pages but use the wrong page size
118# so that PAGECACHE is not used.
119#
120db close
121sqlite3_shutdown
danielk19778c0a7912008-08-20 14:49:23 +0000122sqlite3_config_pagecache [expr 512+$xtra_size] 20
drhc54357c2015-07-07 14:06:18 +0000123sqlite3_config singlethread
drhf7141992008-06-19 00:16:08 +0000124sqlite3_initialize
drh93ed56d2008-08-12 15:21:11 +0000125reset_highwater_marks
drhf7141992008-06-19 00:16:08 +0000126build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
drhf7141992008-06-19 00:16:08 +0000127do_test memsubsys1-3.1.3 {
128 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
129} 0
130do_test memsubsys1-3.1.4 {
131 set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
drhdfd15472014-08-19 23:04:49 +0000132 # 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}
138} 1
drhf7141992008-06-19 00:16:08 +0000139do_test memsubsys1-3.1.5 {
140 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
141} 0
142db close
143sqlite3_shutdown
danielk19778c0a7912008-08-20 14:49:23 +0000144sqlite3_config_pagecache [expr 2048+$xtra_size] 20
drhf7141992008-06-19 00:16:08 +0000145sqlite3_initialize
drh93ed56d2008-08-12 15:21:11 +0000146reset_highwater_marks
drhf7141992008-06-19 00:16:08 +0000147build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}
148#show_memstats
149do_test memsubsys1-3.2.3 {
150 db eval {PRAGMA page_size}
151} 2048
152do_test memsubsys1-3.2.4 {
153 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
drh50d1b5f2010-08-27 12:21:06 +0000154} 20
drhf7141992008-06-19 00:16:08 +0000155do_test memsubsys1-3.2.5 {
156 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
157} 0
158
drhb2a0f752017-08-28 15:51:35 +0000159# Test 4: Activate PAGECACHE
drhf7141992008-06-19 00:16:08 +0000160#
161db close
162sqlite3_shutdown
danielk19778c0a7912008-08-20 14:49:23 +0000163sqlite3_config_pagecache [expr 1024+$xtra_size] 50
drhf7141992008-06-19 00:16:08 +0000164sqlite3_initialize
drh93ed56d2008-08-12 15:21:11 +0000165reset_highwater_marks
drhf7141992008-06-19 00:16:08 +0000166build_test_db memsubsys1-4 {PRAGMA page_size=1024}
167#show_memstats
168do_test memsubsys1-4.3 {
169 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
drh50d1b5f2010-08-27 12:21:06 +0000170 expr {$pg_used>=45 && $pg_used<=50}
171} 1
drhf7141992008-06-19 00:16:08 +0000172do_test memsubsys1-4.4 {
173 set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
drh50d1b5f2010-08-27 12:21:06 +0000174} 0
drhf7141992008-06-19 00:16:08 +0000175do_test memsubsys1-4.5 {
176 set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
drhebf1b462008-07-25 13:39:08 +0000177 expr {$maxreq<7000}
drhf7141992008-06-19 00:16:08 +0000178} 1
drh73cd2732009-03-26 12:20:32 +0000179
180db close
181sqlite3_shutdown
182sqlite3_config_memstatus 1
drh633e6d52008-07-28 19:34:53 +0000183sqlite3_config_lookaside 100 500
drhc54357c2015-07-07 14:06:18 +0000184sqlite3_config serialized
drhaf837582008-06-20 00:03:21 +0000185sqlite3_initialize
drh55b0cf02008-06-19 17:54:33 +0000186autoinstall_test_functions
dan03bc5252015-07-24 14:17:17 +0000187
188test_restore_config_pagecache
drhf7141992008-06-19 00:16:08 +0000189finish_test