blob: 22f9237df4c8d15daec25218585f69f2585ca51c [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#
drheee4c8c2008-02-18 22:24:57 +000015# $Id: malloc5.test,v 1.18 2008/02/18 22:24:58 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
drheee4c8c2008-02-18 22:24:57 +000025source $testdir/malloc_common.tcl
danielk197752622822006-01-09 09:59:49 +000026db close
danielk19770190d1d2005-12-19 14:18:11 +000027
danielk1977aef0bf62005-12-30 16:28:01 +000028# Only run these tests if memory debugging is turned on.
drhed138fb2007-08-22 22:04:37 +000029#
drheee4c8c2008-02-18 22:24:57 +000030if {!$MEMDEBUG} {
drh5a3032b2007-09-03 16:12:09 +000031 puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
danielk1977aef0bf62005-12-30 16:28:01 +000032 finish_test
33 return
34}
35
danielk197752622822006-01-09 09:59:49 +000036# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
37ifcapable !memorymanage {
38 finish_test
39 return
40}
41
drh3aefaba2007-08-12 20:07:58 +000042sqlite3_soft_heap_limit 0
danielk197752622822006-01-09 09:59:49 +000043sqlite3 db test.db
44
danielk19770190d1d2005-12-19 14:18:11 +000045do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000046 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk19770190d1d2005-12-19 14:18:11 +000047 # one unused page in a single pager cache. This test case set's the
48 # value of the ::pgalloc variable, which is used in subsequent tests.
49 #
50 # Note: Even though executing this statement on an empty database
51 # modifies 2 pages (the root of sqlite_master and the new root page),
52 # the sqlite_master root (page 1) is never freed because the btree layer
53 # retains a reference to it for the entire transaction.
54 execsql {
drh271d8cb2007-04-07 17:44:27 +000055 PRAGMA auto_vacuum=OFF;
danielk19770190d1d2005-12-19 14:18:11 +000056 BEGIN;
57 CREATE TABLE abc(a, b, c);
58 }
drh6aafc292006-01-05 15:50:06 +000059 set ::pgalloc [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +000060 expr $::pgalloc > 0
61} {1}
62do_test malloc5-1.2 {
63 # Test that the transaction started in the above test is still active.
64 # Because the page freed had been written to, freeing it required a
65 # journal sync and exclusive lock on the database file. Test the file
66 # appears to be locked.
67 sqlite3 db2 test.db
68 catchsql {
69 SELECT * FROM abc;
70 } db2
71} {1 {database is locked}}
72do_test malloc5-1.3 {
drh6aafc292006-01-05 15:50:06 +000073 # Again call [sqlite3_release_memory] when there is exactly one unused page
danielk19770190d1d2005-12-19 14:18:11 +000074 # in the cache. The same amount of memory is required, but no journal-sync
75 # or exclusive lock should be established.
76 execsql {
77 COMMIT;
78 BEGIN;
79 SELECT * FROM abc;
80 }
drh6aafc292006-01-05 15:50:06 +000081 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +000082} $::pgalloc
83do_test malloc5-1.4 {
84 # Database should not be locked this time.
85 catchsql {
86 SELECT * FROM abc;
87 } db2
88} {0 {}}
89do_test malloc5-1.5 {
90 # Manipulate the cache so that it contains two unused pages. One requires
91 # a journal-sync to free, the other does not.
danielk197724168722007-04-02 05:07:47 +000092 db2 close
danielk19770190d1d2005-12-19 14:18:11 +000093 execsql {
94 SELECT * FROM abc;
95 CREATE TABLE def(d, e, f);
96 }
drh6aafc292006-01-05 15:50:06 +000097 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000098} $::pgalloc
99do_test malloc5-1.6 {
100 # Database should not be locked this time. The above test case only
101 # requested 500 bytes of memory, which can be obtained by freeing the page
102 # that does not require an fsync().
danielk197724168722007-04-02 05:07:47 +0000103 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000104 catchsql {
105 SELECT * FROM abc;
106 } db2
107} {0 {}}
108do_test malloc5-1.7 {
109 # Release another 500 bytes of memory. This time we require a sync(),
110 # so the database file will be locked afterwards.
danielk197724168722007-04-02 05:07:47 +0000111 db2 close
drh6aafc292006-01-05 15:50:06 +0000112 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +0000113} $::pgalloc
114do_test malloc5-1.8 {
danielk197724168722007-04-02 05:07:47 +0000115 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000116 catchsql {
117 SELECT * FROM abc;
118 } db2
119} {1 {database is locked}}
120do_test malloc5-1.9 {
121 execsql {
122 COMMIT;
123 }
124} {}
125
danielk19770190d1d2005-12-19 14:18:11 +0000126do_test malloc5-2.1 {
127 # Put some data in tables abc and def. Both tables are still wholly
128 # contained within their root pages.
129 execsql {
130 INSERT INTO abc VALUES(1, 2, 3);
131 INSERT INTO abc VALUES(4, 5, 6);
132 INSERT INTO def VALUES(7, 8, 9);
133 INSERT INTO def VALUES(10,11,12);
134 }
135} {}
136do_test malloc5-2.2 {
137 # Load the root-page for table def into the cache. Then query table abc.
138 # Halfway through the query call sqlite3_release_memory(). The goal of this
139 # test is to make sure we don't free pages that are in use (specifically,
140 # the root of table abc).
141 set nRelease 0
142 execsql {
143 BEGIN;
144 SELECT * FROM def;
145 }
danielk19775591df52005-12-20 09:19:37 +0000146 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000147 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000148 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000149 lappend data $a $b $c
150 }
danielk19775591df52005-12-20 09:19:37 +0000151 execsql {
152 COMMIT;
153 }
danielk19770190d1d2005-12-19 14:18:11 +0000154 list $nRelease $data
155} [list $pgalloc [list 1 2 3 4 5 6]]
156
danielk19775591df52005-12-20 09:19:37 +0000157do_test malloc5-3.1 {
158 # Simple test to show that if two pagers are opened from within this
159 # thread, memory is freed from both when sqlite3_release_memory() is
160 # called.
161 execsql {
162 BEGIN;
163 SELECT * FROM abc;
164 }
165 execsql {
166 SELECT * FROM sqlite_master;
167 BEGIN;
168 SELECT * FROM def;
169 } db2
drh6aafc292006-01-05 15:50:06 +0000170 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000171} [expr $::pgalloc * 2]
172do_test malloc5-3.2 {
173 concat \
174 [execsql {SELECT * FROM abc; COMMIT}] \
175 [execsql {SELECT * FROM def; COMMIT} db2]
176} {1 2 3 4 5 6 7 8 9 10 11 12}
177
178db2 close
drhed138fb2007-08-22 22:04:37 +0000179puts "Highwater mark: [sqlite3_memory_highwater]"
danielk19775591df52005-12-20 09:19:37 +0000180
181# The following two test cases each execute a transaction in which
182# 10000 rows are inserted into table abc. The first test case is used
183# to ensure that more than 1MB of dynamic memory is used to perform
184# the transaction.
185#
186# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
187# and tests to see that this limit is not exceeded at any point during
188# transaction execution.
189#
danielk1977aef0bf62005-12-30 16:28:01 +0000190# Before executing malloc5-4.* we save the value of the current soft heap
191# limit in variable ::soft_limit. The original value is restored after
192# running the tests.
193#
drh6aafc292006-01-05 15:50:06 +0000194set ::soft_limit [sqlite3_soft_heap_limit -1]
drh3a7fb7c2007-08-10 16:41:08 +0000195execsql {PRAGMA cache_size=2000}
danielk19775591df52005-12-20 09:19:37 +0000196do_test malloc5-4.1 {
197 execsql {BEGIN;}
198 execsql {DELETE FROM abc;}
199 for {set i 0} {$i < 10000} {incr i} {
200 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
201 }
202 execsql {COMMIT;}
drhed138fb2007-08-22 22:04:37 +0000203 set nMaxBytes [sqlite3_memory_highwater 1]
204 puts -nonewline " (Highwater mark: $nMaxBytes) "
205 expr $nMaxBytes > 1000000
danielk19775591df52005-12-20 09:19:37 +0000206} {1}
207do_test malloc5-4.2 {
drh6aafc292006-01-05 15:50:06 +0000208 sqlite3_release_memory
drh6aafc292006-01-05 15:50:06 +0000209 sqlite3_soft_heap_limit 100000
drhed138fb2007-08-22 22:04:37 +0000210 sqlite3_memory_highwater 1
danielk19775591df52005-12-20 09:19:37 +0000211 execsql {BEGIN;}
212 for {set i 0} {$i < 10000} {incr i} {
213 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
214 }
215 execsql {COMMIT;}
drhed138fb2007-08-22 22:04:37 +0000216 set nMaxBytes [sqlite3_memory_highwater 1]
217 puts -nonewline " (Highwater mark: $nMaxBytes) "
danielk19778da60212007-10-03 09:43:54 +0000218
219 # We used to test ($nMaxBytes<100000), because the soft-heap-limit is
220 # 100000 bytes. But if an allocation that will exceed the
221 # soft-heap-limit is requested from within the only pager instance in
222 # the system, then there is no way to free memory and the limit has to
223 # be exceeded. An exception is memory allocated to store actual page
224 # data (the code contains a special case for this).
225 #
226 # This is not a problem because all allocations apart from those
227 # used to store cached page data are both small and transient.
228 #
229 # Summary: the actual high-water mark for memory usage may be slightly
230 # higher than the soft-heap-limit. The specific allocations that cause
231 # the problem are the calls to sqlite3_malloc() inserted into selected
232 # sqlite3OsXXX() functions in test builds.
233 #
234 expr $nMaxBytes <= 100100
danielk19775591df52005-12-20 09:19:37 +0000235} {1}
236do_test malloc5-4.3 {
237 # Check that the content of table abc is at least roughly as expected.
238 execsql {
239 SELECT count(*), sum(a), sum(b) FROM abc;
240 }
241} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
242
danielk1977aef0bf62005-12-30 16:28:01 +0000243# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000244sqlite3_soft_heap_limit $::soft_limit
danielk197752622822006-01-09 09:59:49 +0000245
danielk1977c551edc2007-04-05 13:12:13 +0000246# Test that there are no problems calling sqlite3_release_memory when
247# there are open in-memory databases.
248#
249# At one point these tests would cause a seg-fault.
250#
251do_test malloc5-5.1 {
252 db close
253 sqlite3 db :memory:
254 execsql {
255 BEGIN;
256 CREATE TABLE abc(a, b, c);
257 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
258 INSERT INTO abc SELECT * FROM abc;
259 INSERT INTO abc SELECT * FROM abc;
260 INSERT INTO abc SELECT * FROM abc;
261 INSERT INTO abc SELECT * FROM abc;
262 INSERT INTO abc SELECT * FROM abc;
263 INSERT INTO abc SELECT * FROM abc;
264 INSERT INTO abc SELECT * FROM abc;
265 }
266 sqlite3_release_memory
267} 0
danielk197784f786f2007-08-28 08:00:17 +0000268do_test malloc5-5.2 {
danielk1977c551edc2007-04-05 13:12:13 +0000269 sqlite3_soft_heap_limit 5000
270 execsql {
271 COMMIT;
272 PRAGMA temp_store = memory;
273 SELECT * FROM abc ORDER BY a;
274 }
275 expr 1
276} {1}
danielk197784f786f2007-08-28 08:00:17 +0000277sqlite3_soft_heap_limit $::soft_limit
278
279#-------------------------------------------------------------------------
280# The following test cases (malloc5-6.*) test the new global LRU list
281# used to determine the pages to recycle when sqlite3_release_memory is
282# called and there is more than one pager open.
283#
284proc nPage {db} {
285 set bt [btree_from_db $db]
286 array set stats [btree_pager_stats $bt]
287 set stats(page)
288}
289db close
290file delete -force test.db test.db-journal test2.db test2.db-journal
291
292# This block of test-cases (malloc5-6.1.*) prepares two database files
293# for the subsequent tests.
294do_test malloc5-6.1.1 {
295 sqlite3 db test.db
296 execsql {
297 PRAGMA page_size=1024;
298 PRAGMA default_cache_size=10;
299 BEGIN;
300 CREATE TABLE abc(a PRIMARY KEY, b, c);
301 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
302 INSERT INTO abc
303 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
304 INSERT INTO abc
305 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
306 INSERT INTO abc
307 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
308 INSERT INTO abc
309 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
310 INSERT INTO abc
311 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
312 INSERT INTO abc
313 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
314 COMMIT;
315 }
316 copy_file test.db test2.db
317 sqlite3 db2 test2.db
danielk1977fa18bec2007-09-03 11:04:22 +0000318 list \
319 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
320} {1 1}
danielk197784f786f2007-08-28 08:00:17 +0000321do_test malloc5-6.1.2 {
322 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
323} {10 10}
324
325do_test malloc5-6.2.1 {
326 execsql { SELECT * FROM abc } db2
327 execsql {SELECT * FROM abc} db
328 list [nPage db] [nPage db2]
329} {10 10}
330do_test malloc5-6.2.2 {
331 # If we now try to reclaim some memory, it should come from the db2 cache.
332 sqlite3_release_memory 3000
333 list [nPage db] [nPage db2]
334} {10 7}
335do_test malloc5-6.2.3 {
336 # Access the db2 cache again, so that all the db2 pages have been used
337 # more recently than all the db pages. Then try to reclaim 3000 bytes.
338 # This time, 3 pages should be pulled from the db cache.
339 execsql { SELECT * FROM abc } db2
340 sqlite3_release_memory 3000
341 list [nPage db] [nPage db2]
342} {7 10}
343
344
345do_test malloc5-6.3.1 {
346 # Now open a transaction and update 2 pages in the db2 cache. Then
347 # do a SELECT on the db cache so that all the db pages are more recently
348 # used than the db2 pages. When we try to free memory, SQLite should
349 # free the non-dirty db2 pages, then the db pages, then finally use
350 # sync() to free up the dirty db2 pages. The only page that cannot be
351 # freed is page1 of db2. Because there is an open transaction, the
352 # btree layer holds a reference to page 1 in the db2 cache.
353 execsql {
354 BEGIN;
355 UPDATE abc SET c = randstr(100,100)
356 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
357 } db2
358 execsql { SELECT * FROM abc } db
359 list [nPage db] [nPage db2]
360} {10 10}
361do_test malloc5-6.3.2 {
362 # Try to release 7700 bytes. This should release all the
363 # non-dirty pages held by db2.
364 sqlite3_release_memory [expr 7*1100]
365 list [nPage db] [nPage db2]
366} {10 3}
367do_test malloc5-6.3.3 {
368 # Try to release another 1000 bytes. This should come fromt the db
369 # cache, since all three pages held by db2 are either in-use or diry.
370 sqlite3_release_memory 1000
371 list [nPage db] [nPage db2]
372} {9 3}
373do_test malloc5-6.3.4 {
374 # Now release 9900 more (about 9 pages worth). This should expunge
375 # the rest of the db cache. But the db2 cache remains intact, because
376 # SQLite tries to avoid calling sync().
377 sqlite3_release_memory 9900
378 list [nPage db] [nPage db2]
379} {0 3}
380do_test malloc5-6.3.5 {
381 # But if we are really insistent, SQLite will consent to call sync()
382 # if there is no other option.
383 sqlite3_release_memory 1000
384 list [nPage db] [nPage db2]
385} {0 2}
386do_test malloc5-6.3.6 {
387 # The referenced page (page 1 of the db2 cache) will not be freed no
388 # matter how much memory we ask for:
389 sqlite3_release_memory 31459
390 list [nPage db] [nPage db2]
391} {0 1}
392
393db2 close
danielk1977c551edc2007-04-05 13:12:13 +0000394
395sqlite3_soft_heap_limit $::soft_limit
396finish_test
danielk197752622822006-01-09 09:59:49 +0000397catch {db close}