blob: e62ba12e90e52e590575eb48559e5b0078f33abf [file] [log] [blame]
danielk19770190d1d2005-12-19 14:18:11 +00001# 2005 November 30
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#
danielk1977aef0bf62005-12-30 16:28:01 +000012# This file contains test cases focused on the two memory-management APIs,
13# sqlite3_soft_heap_limit() and sqlite3_release_memory().
14#
danielk197752622822006-01-09 09:59:49 +000015# $Id: malloc5.test,v 1.5 2006/01/09 09:59:49 danielk1977 Exp $
danielk19770190d1d2005-12-19 14:18:11 +000016
17#---------------------------------------------------------------------------
18# NOTES ON EXPECTED BEHAVIOUR
19#
20#---------------------------------------------------------------------------
21
danielk197752622822006-01-09 09:59:49 +000022
danielk19770190d1d2005-12-19 14:18:11 +000023set testdir [file dirname $argv0]
24source $testdir/tester.tcl
danielk197752622822006-01-09 09:59:49 +000025db close
danielk19770190d1d2005-12-19 14:18:11 +000026
danielk1977aef0bf62005-12-30 16:28:01 +000027# Only run these tests if memory debugging is turned on.
28if {[info command sqlite_malloc_stat]==""} {
29 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
30 finish_test
31 return
32}
33
danielk197752622822006-01-09 09:59:49 +000034# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
35ifcapable !memorymanage {
36 finish_test
37 return
38}
39
40sqlite3_enable_memory_management 1
41sqlite3 db test.db
42
danielk19770190d1d2005-12-19 14:18:11 +000043do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000044 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk19770190d1d2005-12-19 14:18:11 +000045 # one unused page in a single pager cache. This test case set's the
46 # value of the ::pgalloc variable, which is used in subsequent tests.
47 #
48 # Note: Even though executing this statement on an empty database
49 # modifies 2 pages (the root of sqlite_master and the new root page),
50 # the sqlite_master root (page 1) is never freed because the btree layer
51 # retains a reference to it for the entire transaction.
52 execsql {
53 BEGIN;
54 CREATE TABLE abc(a, b, c);
55 }
drh6aafc292006-01-05 15:50:06 +000056 set ::pgalloc [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +000057 expr $::pgalloc > 0
58} {1}
59do_test malloc5-1.2 {
60 # Test that the transaction started in the above test is still active.
61 # Because the page freed had been written to, freeing it required a
62 # journal sync and exclusive lock on the database file. Test the file
63 # appears to be locked.
64 sqlite3 db2 test.db
65 catchsql {
66 SELECT * FROM abc;
67 } db2
68} {1 {database is locked}}
69do_test malloc5-1.3 {
drh6aafc292006-01-05 15:50:06 +000070 # Again call [sqlite3_release_memory] when there is exactly one unused page
danielk19770190d1d2005-12-19 14:18:11 +000071 # in the cache. The same amount of memory is required, but no journal-sync
72 # or exclusive lock should be established.
73 execsql {
74 COMMIT;
75 BEGIN;
76 SELECT * FROM abc;
77 }
drh6aafc292006-01-05 15:50:06 +000078 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +000079} $::pgalloc
80do_test malloc5-1.4 {
81 # Database should not be locked this time.
82 catchsql {
83 SELECT * FROM abc;
84 } db2
85} {0 {}}
86do_test malloc5-1.5 {
87 # Manipulate the cache so that it contains two unused pages. One requires
88 # a journal-sync to free, the other does not.
89 execsql {
90 SELECT * FROM abc;
91 CREATE TABLE def(d, e, f);
92 }
drh6aafc292006-01-05 15:50:06 +000093 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000094} $::pgalloc
95do_test malloc5-1.6 {
96 # Database should not be locked this time. The above test case only
97 # requested 500 bytes of memory, which can be obtained by freeing the page
98 # that does not require an fsync().
99 catchsql {
100 SELECT * FROM abc;
101 } db2
102} {0 {}}
103do_test malloc5-1.7 {
104 # Release another 500 bytes of memory. This time we require a sync(),
105 # so the database file will be locked afterwards.
drh6aafc292006-01-05 15:50:06 +0000106 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +0000107} $::pgalloc
108do_test malloc5-1.8 {
109 catchsql {
110 SELECT * FROM abc;
111 } db2
112} {1 {database is locked}}
113do_test malloc5-1.9 {
114 execsql {
115 COMMIT;
116 }
117} {}
118
danielk19770190d1d2005-12-19 14:18:11 +0000119do_test malloc5-2.1 {
120 # Put some data in tables abc and def. Both tables are still wholly
121 # contained within their root pages.
122 execsql {
123 INSERT INTO abc VALUES(1, 2, 3);
124 INSERT INTO abc VALUES(4, 5, 6);
125 INSERT INTO def VALUES(7, 8, 9);
126 INSERT INTO def VALUES(10,11,12);
127 }
128} {}
129do_test malloc5-2.2 {
130 # Load the root-page for table def into the cache. Then query table abc.
131 # Halfway through the query call sqlite3_release_memory(). The goal of this
132 # test is to make sure we don't free pages that are in use (specifically,
133 # the root of table abc).
134 set nRelease 0
135 execsql {
136 BEGIN;
137 SELECT * FROM def;
138 }
danielk19775591df52005-12-20 09:19:37 +0000139 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000140 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000141 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000142 lappend data $a $b $c
143 }
danielk19775591df52005-12-20 09:19:37 +0000144 execsql {
145 COMMIT;
146 }
danielk19770190d1d2005-12-19 14:18:11 +0000147 list $nRelease $data
148} [list $pgalloc [list 1 2 3 4 5 6]]
149
danielk19775591df52005-12-20 09:19:37 +0000150do_test malloc5-3.1 {
151 # Simple test to show that if two pagers are opened from within this
152 # thread, memory is freed from both when sqlite3_release_memory() is
153 # called.
154 execsql {
155 BEGIN;
156 SELECT * FROM abc;
157 }
158 execsql {
159 SELECT * FROM sqlite_master;
160 BEGIN;
161 SELECT * FROM def;
162 } db2
drh6aafc292006-01-05 15:50:06 +0000163 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000164} [expr $::pgalloc * 2]
165do_test malloc5-3.2 {
166 concat \
167 [execsql {SELECT * FROM abc; COMMIT}] \
168 [execsql {SELECT * FROM def; COMMIT} db2]
169} {1 2 3 4 5 6 7 8 9 10 11 12}
170
171db2 close
172sqlite_malloc_outstanding -clearmaxbytes
173
174# The following two test cases each execute a transaction in which
175# 10000 rows are inserted into table abc. The first test case is used
176# to ensure that more than 1MB of dynamic memory is used to perform
177# the transaction.
178#
179# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
180# and tests to see that this limit is not exceeded at any point during
181# transaction execution.
182#
danielk1977aef0bf62005-12-30 16:28:01 +0000183# Before executing malloc5-4.* we save the value of the current soft heap
184# limit in variable ::soft_limit. The original value is restored after
185# running the tests.
186#
drh6aafc292006-01-05 15:50:06 +0000187set ::soft_limit [sqlite3_soft_heap_limit -1]
danielk19775591df52005-12-20 09:19:37 +0000188do_test malloc5-4.1 {
189 execsql {BEGIN;}
190 execsql {DELETE FROM abc;}
191 for {set i 0} {$i < 10000} {incr i} {
192 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
193 }
194 execsql {COMMIT;}
195 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
196 expr $::nMaxBytes > 1000000
197} {1}
198do_test malloc5-4.2 {
drh6aafc292006-01-05 15:50:06 +0000199 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000200 sqlite_malloc_outstanding -clearmaxbytes
drh6aafc292006-01-05 15:50:06 +0000201 sqlite3_soft_heap_limit 100000
danielk19775591df52005-12-20 09:19:37 +0000202 execsql {BEGIN;}
203 for {set i 0} {$i < 10000} {incr i} {
204 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
205 }
206 execsql {COMMIT;}
207 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
208 expr $::nMaxBytes <= 100000
209} {1}
210do_test malloc5-4.3 {
211 # Check that the content of table abc is at least roughly as expected.
212 execsql {
213 SELECT count(*), sum(a), sum(b) FROM abc;
214 }
215} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
216
danielk1977aef0bf62005-12-30 16:28:01 +0000217# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000218sqlite3_soft_heap_limit $::soft_limit
danielk19770190d1d2005-12-19 14:18:11 +0000219finish_test
danielk197752622822006-01-09 09:59:49 +0000220
221catch {db close}
222sqlite3_enable_memory_management 0
223