2008-06-19 04:16:08 +04:00
|
|
|
# 2008 June 18
|
|
|
|
#
|
|
|
|
# The author disclaims copyright to this source code. In place of
|
|
|
|
# a legal notice, here is a blessing:
|
|
|
|
#
|
|
|
|
# May you do good and not evil.
|
|
|
|
# May you find forgiveness for yourself and forgive others.
|
|
|
|
# May you share freely, never taking more than you give.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
|
|
|
# This file contains tests of the memory allocation subsystem
|
|
|
|
#
|
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
2008-06-19 21:54:33 +04:00
|
|
|
sqlite3_reset_auto_extension
|
2008-06-19 04:16:08 +04:00
|
|
|
|
2010-07-03 23:08:19 +04:00
|
|
|
# This test assumes that no page-cache or scratch buffers are installed
|
|
|
|
# by default when a new database connection is opened. As a result, it
|
|
|
|
# will not work with the "memsubsys1" permutation.
|
|
|
|
#
|
|
|
|
if {[permutation] == "memsubsys1"} {
|
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-24 17:17:17 +03:00
|
|
|
test_set_config_pagecache 0 0
|
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
# This procedure constructs a new database in test.db. It fills
|
|
|
|
# this database with many small records (enough to force multiple
|
|
|
|
# rebalance operations in the btree-layer and to require a large
|
|
|
|
# page cache), verifies correct results, then returns.
|
|
|
|
#
|
|
|
|
proc build_test_db {testname pragmas} {
|
|
|
|
catch {db close}
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test.db-journal
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3 db test.db
|
2008-08-05 00:13:26 +04:00
|
|
|
sqlite3_db_config_lookaside db 0 0 0
|
2008-06-19 04:16:08 +04:00
|
|
|
db eval $pragmas
|
|
|
|
db eval {
|
|
|
|
CREATE TABLE t1(x, y);
|
|
|
|
CREATE TABLE t2(a, b);
|
|
|
|
CREATE INDEX i1 ON t1(x,y);
|
|
|
|
INSERT INTO t1 VALUES(1, 100);
|
|
|
|
INSERT INTO t1 VALUES(2, 200);
|
|
|
|
}
|
|
|
|
for {set i 2} {$i<5000} {incr i $i} {
|
|
|
|
db eval {INSERT INTO t2 SELECT * FROM t1}
|
|
|
|
db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
|
|
|
|
db eval {DELETE FROM t2}
|
|
|
|
}
|
|
|
|
do_test $testname.1 {
|
|
|
|
db eval {SELECT count(*) FROM t1}
|
|
|
|
} 8192
|
|
|
|
integrity_check $testname.2
|
|
|
|
}
|
|
|
|
|
2008-08-12 19:21:11 +04:00
|
|
|
# Reset all of the highwater marks.
|
|
|
|
#
|
|
|
|
proc reset_highwater_marks {} {
|
|
|
|
sqlite3_status SQLITE_STATUS_MEMORY_USED 1
|
|
|
|
sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
|
|
|
|
sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
|
|
|
|
sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
|
|
|
|
sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
|
|
|
|
sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
|
|
|
|
sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
|
|
|
|
sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
|
|
|
|
sqlite3_status SQLITE_STATUS_PARSER_STACK 1
|
|
|
|
}
|
|
|
|
|
2011-11-16 22:08:07 +04:00
|
|
|
set xtra_size 290
|
2008-08-20 18:49:23 +04:00
|
|
|
|
2008-06-19 04:16:08 +04:00
|
|
|
# Test 1: Both PAGECACHE and SCRATCH are shut down.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3_config_lookaside 0 0
|
2015-07-07 17:06:18 +03:00
|
|
|
sqlite3_config_pagecache 0 0
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-1 {PRAGMA page_size=1024}
|
|
|
|
do_test memsubsys1-1.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-1.4 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
set max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
|
|
|
|
#show_memstats
|
|
|
|
|
|
|
|
# Test 2: Activate PAGECACHE with 20 pages
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 1024+$xtra_size] 20
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2013-07-06 22:07:57 +04:00
|
|
|
build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0}
|
2008-06-19 04:16:08 +04:00
|
|
|
#show_memstats
|
2011-01-18 19:13:27 +03:00
|
|
|
set MEMORY_MANAGEMENT $sqlite_options(memorymanage)
|
2016-03-18 03:19:48 +03:00
|
|
|
ifcapable pagecache_overflow_stats {
|
|
|
|
ifcapable !malloc_usable_size {
|
|
|
|
do_test memsubsys1-2.3 {
|
|
|
|
set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
|
|
|
|
} [expr ($TEMP_STORE>1 || $MEMORY_MANAGEMENT==0)*1024]
|
|
|
|
}
|
2011-11-09 05:53:25 +04:00
|
|
|
}
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-2.4 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
2010-08-27 16:21:06 +04:00
|
|
|
} 20
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-2.5 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
|
|
|
|
# Test 3: Activate PAGECACHE with 20 pages but use the wrong page size
|
|
|
|
# so that PAGECACHE is not used.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 512+$xtra_size] 20
|
2015-07-07 17:06:18 +03:00
|
|
|
sqlite3_config singlethread
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
|
|
|
|
do_test memsubsys1-3.1.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-3.1.4 {
|
|
|
|
set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
|
2014-08-20 03:04:49 +04:00
|
|
|
# Note: The measured PAGECACHE_OVERFLOW is amount malloc() returns, not what
|
|
|
|
# was requested. System malloc() implementations might (arbitrarily) return
|
|
|
|
# slightly different oversize buffers, which can result in slightly different
|
|
|
|
# PAGECACHE_OVERFLOW sizes between consecutive runs. So we cannot do an
|
|
|
|
# exact comparison. Simply verify that the amount is within 5%.
|
|
|
|
expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05}
|
|
|
|
} 1
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-3.1.5 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 2048+$xtra_size] 20
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}
|
|
|
|
#show_memstats
|
|
|
|
do_test memsubsys1-3.2.3 {
|
|
|
|
db eval {PRAGMA page_size}
|
|
|
|
} 2048
|
|
|
|
do_test memsubsys1-3.2.4 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
2010-08-27 16:21:06 +04:00
|
|
|
} 20
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-3.2.5 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
|
|
|
|
# Test 4: Activate both PAGECACHE and SCRATCH.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 1024+$xtra_size] 50
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_config_scratch 6000 2
|
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-4 {PRAGMA page_size=1024}
|
|
|
|
#show_memstats
|
|
|
|
do_test memsubsys1-4.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
2010-08-27 16:21:06 +04:00
|
|
|
expr {$pg_used>=45 && $pg_used<=50}
|
|
|
|
} 1
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-4.4 {
|
|
|
|
set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
|
2010-08-27 16:21:06 +04:00
|
|
|
} 0
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-4.5 {
|
|
|
|
set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
|
2008-07-25 17:39:08 +04:00
|
|
|
expr {$maxreq<7000}
|
2008-06-19 04:16:08 +04:00
|
|
|
} 1
|
|
|
|
do_test memsubsys1-4.6 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 1
|
|
|
|
|
2014-12-31 23:25:33 +03:00
|
|
|
# Test 5: Activate both PAGECACHE and SCRATCH. But make the page size is
|
2008-06-19 04:16:08 +04:00
|
|
|
# such that the SCRATCH allocations are too small.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
|
2014-12-31 23:25:33 +03:00
|
|
|
sqlite3_config_scratch 4000 2
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-5 {PRAGMA page_size=4096}
|
|
|
|
#show_memstats
|
|
|
|
do_test memsubsys1-5.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
2015-01-01 17:13:45 +03:00
|
|
|
} {/^2[34]$/}
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-5.4 {
|
|
|
|
set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
|
|
|
|
expr {$maxreq>4096}
|
|
|
|
} 1
|
|
|
|
do_test memsubsys1-5.5 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-5.6 {
|
|
|
|
set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
|
|
|
|
expr {$s_ovfl>6000}
|
|
|
|
} 1
|
|
|
|
|
|
|
|
# Test 6: Activate both PAGECACHE and SCRATCH with a 4k page size.
|
|
|
|
# Make it so that SCRATCH is large enough
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
|
2009-04-11 18:46:43 +04:00
|
|
|
sqlite3_config_scratch 25300 1
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-6 {PRAGMA page_size=4096}
|
|
|
|
#show_memstats
|
|
|
|
do_test memsubsys1-6.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
2015-01-01 17:13:45 +03:00
|
|
|
} {/^2[34]$/}
|
2010-08-27 16:21:06 +04:00
|
|
|
#do_test memsubsys1-6.4 {
|
|
|
|
# set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
|
|
|
|
# expr {$maxreq>4096 && $maxreq<=(4096+$xtra_size)}
|
|
|
|
#} 1
|
2008-06-19 04:16:08 +04:00
|
|
|
do_test memsubsys1-6.5 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 1
|
|
|
|
do_test memsubsys1-6.6 {
|
|
|
|
set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
|
|
|
|
} 0
|
|
|
|
|
|
|
|
# Test 7: Activate both PAGECACHE and SCRATCH with a 4k page size.
|
|
|
|
# Set cache_size small so that no PAGECACHE overflow occurs. Verify
|
|
|
|
# that maximum allocation size is small.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2008-08-20 18:49:23 +04:00
|
|
|
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
|
2009-04-11 18:46:43 +04:00
|
|
|
sqlite3_config_scratch 25300 1
|
2008-06-19 04:16:08 +04:00
|
|
|
sqlite3_initialize
|
2008-08-12 19:21:11 +04:00
|
|
|
reset_highwater_marks
|
2008-06-19 04:16:08 +04:00
|
|
|
build_test_db memsubsys1-7 {
|
|
|
|
PRAGMA page_size=4096;
|
2008-08-26 22:05:48 +04:00
|
|
|
PRAGMA cache_size=10;
|
|
|
|
PRAGMA temp_store=memory;
|
2008-06-19 04:16:08 +04:00
|
|
|
}
|
|
|
|
#show_memstats
|
|
|
|
do_test memsubsys1-7.3 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
|
|
|
|
expr {$pg_used<24}
|
|
|
|
} 1
|
|
|
|
do_test memsubsys1-7.4 {
|
|
|
|
set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-7.5 {
|
|
|
|
set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
|
2016-03-14 21:34:42 +03:00
|
|
|
expr {$maxreq<4100 + 4200*[nonzero_reserved_bytes]}
|
2008-06-19 04:16:08 +04:00
|
|
|
} 1
|
|
|
|
do_test memsubsys1-7.6 {
|
|
|
|
set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 1
|
|
|
|
do_test memsubsys1-7.7 {
|
|
|
|
set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
|
|
|
|
} 0
|
|
|
|
|
2009-03-26 15:20:32 +03:00
|
|
|
# Test 8: Disable PAGECACHE. Make available SCRATCH zero. Verify that
|
|
|
|
# the SCRATCH overflow logic works.
|
|
|
|
#
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
|
|
|
sqlite3_config_pagecache 0 0
|
|
|
|
sqlite3_config_scratch 25000 0
|
|
|
|
sqlite3_initialize
|
|
|
|
reset_highwater_marks
|
|
|
|
do_test memsubsys1-8.1 {
|
|
|
|
set pg_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-8.2 {
|
|
|
|
set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
|
|
|
|
} 0
|
|
|
|
do_test memsubsys1-8.3 {
|
|
|
|
sqlite3 db :memory:
|
|
|
|
db eval {
|
|
|
|
CREATE TABLE t1(x);
|
|
|
|
INSERT INTO t1 VALUES(zeroblob(400));
|
|
|
|
INSERT INTO t1 VALUES(zeroblob(400));
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
expr {[lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]>0}
|
|
|
|
} 1
|
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
|
|
|
sqlite3_config_memstatus 0
|
|
|
|
sqlite3_initialize
|
|
|
|
do_test memsubsys1-8.4 {
|
|
|
|
sqlite3 db :memory:
|
|
|
|
db eval {
|
|
|
|
CREATE TABLE t1(x);
|
|
|
|
INSERT INTO t1 VALUES(zeroblob(400));
|
|
|
|
INSERT INTO t1 VALUES(zeroblob(400));
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
INSERT INTO t1 SELECT * FROM t1;
|
|
|
|
SELECT rowid FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
|
|
|
|
|
|
|
|
|
2008-06-20 04:03:21 +04:00
|
|
|
db close
|
|
|
|
sqlite3_shutdown
|
2009-03-26 15:20:32 +03:00
|
|
|
sqlite3_config_memstatus 1
|
2008-06-20 04:03:21 +04:00
|
|
|
sqlite3_config_scratch 0 0
|
2008-07-28 23:34:53 +04:00
|
|
|
sqlite3_config_lookaside 100 500
|
2015-07-07 17:06:18 +03:00
|
|
|
sqlite3_config serialized
|
2008-06-20 04:03:21 +04:00
|
|
|
sqlite3_initialize
|
2008-06-19 21:54:33 +04:00
|
|
|
autoinstall_test_functions
|
2015-07-24 17:17:17 +03:00
|
|
|
|
|
|
|
test_restore_config_pagecache
|
2008-06-19 04:16:08 +04:00
|
|
|
finish_test
|