interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / memsubsys1.test

182 lines · 5542 bytes Raw Blame History
1# 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#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17sqlite3_reset_auto_extension
18
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.
22#
23if {[permutation] == "memsubsys1"} {
24 finish_test
25 return
26}
27
28test_set_config_pagecache 0 0
29
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.
34#
35proc build_test_db {testname pragmas} {
36 catch {db close}
37 forcedelete test.db test.db-journal
38 sqlite3 db test.db
39 sqlite3_db_config_lookaside db 0 0 0
40 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
59# 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
73set xtra_size 290
74
75# Test 1: Both PAGECACHE and SCRATCH are shut down.
76#
77db close
78sqlite3_shutdown
79sqlite3_config_lookaside 0 0
80sqlite3_config_pagecache 0 0
81sqlite3_initialize
82reset_highwater_marks
83build_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
97sqlite3_config_pagecache [expr 1024+$xtra_size] 20
98sqlite3_initialize
99reset_highwater_marks
100build_test_db memsubsys1-2 {PRAGMA page_size=1024; PRAGMA mmap_size=0}
101#show_memstats
102set MEMORY_MANAGEMENT $sqlite_options(memorymanage)
103do_test memsubsys1-2.4 {
104 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
105} 20
106do_test memsubsys1-2.5 {
107 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
108} 0
109
110# Test 3: Activate PAGECACHE with 20 pages but use the wrong page size
111# so that PAGECACHE is not used.
112#
113db close
114sqlite3_shutdown
115sqlite3_config_pagecache [expr 512+$xtra_size] 20
116sqlite3_config singlethread
117sqlite3_initialize
118reset_highwater_marks
119build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
120do_test memsubsys1-3.1.3 {
121 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
122} 0
123do_test memsubsys1-3.1.4 {
124 set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
125 # Note: The measured PAGECACHE_OVERFLOW is amount malloc() returns, not what
126 # was requested. System malloc() implementations might (arbitrarily) return
127 # slightly different oversize buffers, which can result in slightly different
128 # PAGECACHE_OVERFLOW sizes between consecutive runs. So we cannot do an
129 # exact comparison. Simply verify that the amount is within 5%.
130 expr {$overflow>=$max_pagecache*0.95 && $overflow<=$max_pagecache*1.05}
131} 1
132do_test memsubsys1-3.1.5 {
133 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
134} 0
135db close
136sqlite3_shutdown
137sqlite3_config_pagecache [expr 2048+$xtra_size] 20
138sqlite3_initialize
139reset_highwater_marks
140build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}
141#show_memstats
142do_test memsubsys1-3.2.3 {
143 db eval {PRAGMA page_size}
144} 2048
145do_test memsubsys1-3.2.4 {
146 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
147} 20
148do_test memsubsys1-3.2.5 {
149 set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
150} 0
151
152# Test 4: Activate PAGECACHE
153#
154db close
155sqlite3_shutdown
156sqlite3_config_pagecache [expr 1024+$xtra_size] 50
157sqlite3_initialize
158reset_highwater_marks
159build_test_db memsubsys1-4 {PRAGMA page_size=1024}
160#show_memstats
161do_test memsubsys1-4.3 {
162 set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
163 expr {$pg_used>=45 && $pg_used<=50}
164} 1
165do_test memsubsys1-4.4 {
166 set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
167} 0
168do_test memsubsys1-4.5 {
169 set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
170 expr {$maxreq<9000}
171} 1
172
173db close
174sqlite3_shutdown
175sqlite3_config_memstatus 1
176sqlite3_config_lookaside 100 500
177sqlite3_config serialized
178sqlite3_initialize
179autoinstall_test_functions
180
181test_restore_config_pagecache
182finish_test