blob: c4148a042ba507b99440e0f328f7682e41cf7854 [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#
drh3a7fb7c2007-08-10 16:41:08 +000015# $Id: malloc5.test,v 1.11 2007/08/10 16:41:09 drh 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
danielk197752622822006-01-09 09:59:49 +000040sqlite3 db test.db
41
danielk19770190d1d2005-12-19 14:18:11 +000042do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000043 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk19770190d1d2005-12-19 14:18:11 +000044 # one unused page in a single pager cache. This test case set's the
45 # value of the ::pgalloc variable, which is used in subsequent tests.
46 #
47 # Note: Even though executing this statement on an empty database
48 # modifies 2 pages (the root of sqlite_master and the new root page),
49 # the sqlite_master root (page 1) is never freed because the btree layer
50 # retains a reference to it for the entire transaction.
51 execsql {
drh271d8cb2007-04-07 17:44:27 +000052 PRAGMA auto_vacuum=OFF;
danielk19770190d1d2005-12-19 14:18:11 +000053 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.
danielk197724168722007-04-02 05:07:47 +000089 db2 close
danielk19770190d1d2005-12-19 14:18:11 +000090 execsql {
91 SELECT * FROM abc;
92 CREATE TABLE def(d, e, f);
93 }
drh6aafc292006-01-05 15:50:06 +000094 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000095} $::pgalloc
96do_test malloc5-1.6 {
97 # Database should not be locked this time. The above test case only
98 # requested 500 bytes of memory, which can be obtained by freeing the page
99 # that does not require an fsync().
danielk197724168722007-04-02 05:07:47 +0000100 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000101 catchsql {
102 SELECT * FROM abc;
103 } db2
104} {0 {}}
105do_test malloc5-1.7 {
106 # Release another 500 bytes of memory. This time we require a sync(),
107 # so the database file will be locked afterwards.
danielk197724168722007-04-02 05:07:47 +0000108 db2 close
drh6aafc292006-01-05 15:50:06 +0000109 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +0000110} $::pgalloc
111do_test malloc5-1.8 {
danielk197724168722007-04-02 05:07:47 +0000112 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000113 catchsql {
114 SELECT * FROM abc;
115 } db2
116} {1 {database is locked}}
117do_test malloc5-1.9 {
118 execsql {
119 COMMIT;
120 }
121} {}
122
danielk19770190d1d2005-12-19 14:18:11 +0000123do_test malloc5-2.1 {
124 # Put some data in tables abc and def. Both tables are still wholly
125 # contained within their root pages.
126 execsql {
127 INSERT INTO abc VALUES(1, 2, 3);
128 INSERT INTO abc VALUES(4, 5, 6);
129 INSERT INTO def VALUES(7, 8, 9);
130 INSERT INTO def VALUES(10,11,12);
131 }
132} {}
133do_test malloc5-2.2 {
134 # Load the root-page for table def into the cache. Then query table abc.
135 # Halfway through the query call sqlite3_release_memory(). The goal of this
136 # test is to make sure we don't free pages that are in use (specifically,
137 # the root of table abc).
138 set nRelease 0
139 execsql {
140 BEGIN;
141 SELECT * FROM def;
142 }
danielk19775591df52005-12-20 09:19:37 +0000143 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000144 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000145 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000146 lappend data $a $b $c
147 }
danielk19775591df52005-12-20 09:19:37 +0000148 execsql {
149 COMMIT;
150 }
danielk19770190d1d2005-12-19 14:18:11 +0000151 list $nRelease $data
152} [list $pgalloc [list 1 2 3 4 5 6]]
153
danielk19775591df52005-12-20 09:19:37 +0000154do_test malloc5-3.1 {
155 # Simple test to show that if two pagers are opened from within this
156 # thread, memory is freed from both when sqlite3_release_memory() is
157 # called.
158 execsql {
159 BEGIN;
160 SELECT * FROM abc;
161 }
162 execsql {
163 SELECT * FROM sqlite_master;
164 BEGIN;
165 SELECT * FROM def;
166 } db2
drh6aafc292006-01-05 15:50:06 +0000167 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000168} [expr $::pgalloc * 2]
169do_test malloc5-3.2 {
170 concat \
171 [execsql {SELECT * FROM abc; COMMIT}] \
172 [execsql {SELECT * FROM def; COMMIT} db2]
173} {1 2 3 4 5 6 7 8 9 10 11 12}
174
175db2 close
176sqlite_malloc_outstanding -clearmaxbytes
177
178# The following two test cases each execute a transaction in which
179# 10000 rows are inserted into table abc. The first test case is used
180# to ensure that more than 1MB of dynamic memory is used to perform
181# the transaction.
182#
183# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
184# and tests to see that this limit is not exceeded at any point during
185# transaction execution.
186#
danielk1977aef0bf62005-12-30 16:28:01 +0000187# Before executing malloc5-4.* we save the value of the current soft heap
188# limit in variable ::soft_limit. The original value is restored after
189# running the tests.
190#
drh6aafc292006-01-05 15:50:06 +0000191set ::soft_limit [sqlite3_soft_heap_limit -1]
drh3a7fb7c2007-08-10 16:41:08 +0000192execsql {PRAGMA cache_size=2000}
danielk19775591df52005-12-20 09:19:37 +0000193do_test malloc5-4.1 {
194 execsql {BEGIN;}
195 execsql {DELETE FROM abc;}
196 for {set i 0} {$i < 10000} {incr i} {
197 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
198 }
199 execsql {COMMIT;}
200 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
danielk1977ed429312006-01-19 08:43:31 +0000201 if {$::nMaxBytes==""} {set ::nMaxBytes 1000001}
danielk19775591df52005-12-20 09:19:37 +0000202 expr $::nMaxBytes > 1000000
203} {1}
204do_test malloc5-4.2 {
drh6aafc292006-01-05 15:50:06 +0000205 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000206 sqlite_malloc_outstanding -clearmaxbytes
drh6aafc292006-01-05 15:50:06 +0000207 sqlite3_soft_heap_limit 100000
danielk19775591df52005-12-20 09:19:37 +0000208 execsql {BEGIN;}
209 for {set i 0} {$i < 10000} {incr i} {
210 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
211 }
212 execsql {COMMIT;}
213 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
danielk1977ed429312006-01-19 08:43:31 +0000214 if {$::nMaxBytes==""} {set ::nMaxBytes 0}
danielk19775591df52005-12-20 09:19:37 +0000215 expr $::nMaxBytes <= 100000
216} {1}
217do_test malloc5-4.3 {
218 # Check that the content of table abc is at least roughly as expected.
219 execsql {
220 SELECT count(*), sum(a), sum(b) FROM abc;
221 }
222} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
223
danielk1977aef0bf62005-12-30 16:28:01 +0000224# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000225sqlite3_soft_heap_limit $::soft_limit
danielk197752622822006-01-09 09:59:49 +0000226
danielk1977c551edc2007-04-05 13:12:13 +0000227# Test that there are no problems calling sqlite3_release_memory when
228# there are open in-memory databases.
229#
230# At one point these tests would cause a seg-fault.
231#
232do_test malloc5-5.1 {
233 db close
234 sqlite3 db :memory:
235 execsql {
236 BEGIN;
237 CREATE TABLE abc(a, b, c);
238 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
239 INSERT INTO abc SELECT * FROM abc;
240 INSERT INTO abc SELECT * FROM abc;
241 INSERT INTO abc SELECT * FROM abc;
242 INSERT INTO abc SELECT * FROM abc;
243 INSERT INTO abc SELECT * FROM abc;
244 INSERT INTO abc SELECT * FROM abc;
245 INSERT INTO abc SELECT * FROM abc;
246 }
247 sqlite3_release_memory
248} 0
249do_test malloc5-5.1 {
250 sqlite3_soft_heap_limit 5000
251 execsql {
252 COMMIT;
253 PRAGMA temp_store = memory;
254 SELECT * FROM abc ORDER BY a;
255 }
256 expr 1
257} {1}
258
259sqlite3_soft_heap_limit $::soft_limit
260finish_test
danielk197752622822006-01-09 09:59:49 +0000261catch {db close}