blob: c046499261cf77dd33d4e8e15c7761c1a43222c7 [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#
danielk1977468c82b2008-08-27 16:38:56 +000015# Prior to version 3.6.2, calling sqlite3_release_memory() or exceeding
16# the configured soft heap limit could cause sqlite to upgrade database
17# locks and flush dirty pages to the file system. As of 3.6.2, this is
18# no longer the case. In version 3.6.2, sqlite3_release_memory() only
19# reclaims clean pages. This test file has been updated accordingly.
danielk19770190d1d2005-12-19 14:18:11 +000020#
drh11809362009-04-11 19:09:53 +000021# $Id: malloc5.test,v 1.22 2009/04/11 19:09:54 drh Exp $
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
danielk1977468c82b2008-08-27 16:38:56 +000047 # one unused page in a single pager cache. The page cannot be freed, as
48 # it is dirty. So sqlite3_release_memory() returns 0.
danielk19770190d1d2005-12-19 14:18:11 +000049 #
danielk19770190d1d2005-12-19 14:18:11 +000050 execsql {
drh271d8cb2007-04-07 17:44:27 +000051 PRAGMA auto_vacuum=OFF;
danielk19770190d1d2005-12-19 14:18:11 +000052 BEGIN;
53 CREATE TABLE abc(a, b, c);
54 }
danielk1977468c82b2008-08-27 16:38:56 +000055 sqlite3_release_memory
56} {0}
57
58do_test malloc5-1.2 {
59 # Test that the transaction started in the above test is still active.
60 # The lock on the database file should not have been upgraded (this was
61 # not the case before version 3.6.2).
62 #
63 sqlite3 db2 test.db
64 execsql { SELECT * FROM sqlite_master } db2
65} {}
66do_test malloc5-1.3 {
67 # Call [sqlite3_release_memory] when there is exactly one unused page
68 # in the cache belonging to db2.
69 #
drh6aafc292006-01-05 15:50:06 +000070 set ::pgalloc [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +000071 expr $::pgalloc > 0
72} {1}
danielk1977468c82b2008-08-27 16:38:56 +000073
drh9b5d76b2015-03-10 13:50:18 +000074# The sizes of memory allocations from system malloc() might vary,
75# depending on the memory allocator algorithms used. The following
76# routine is designed to support answers that fall within a range
77# of values while also supplying easy-to-understand "expected" values
78# when errors occur.
79#
80proc value_in_range {target x args} {
81 set v [lindex $args 0]
82 if {$v!=""} {
83 if {$v<$target*$x} {return $v}
84 if {$v>$target/$x} {return $v}
85 }
86 return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]"
87}
88set mrange 0.98 ;# plus or minus 2%
89
90
danielk1977468c82b2008-08-27 16:38:56 +000091do_test malloc5-1.4 {
92 # Commit the transaction and open a new one. Read 1 page into the cache.
93 # Because the page is not dirty, it is eligible for collection even
94 # before the transaction is concluded.
95 #
danielk19770190d1d2005-12-19 14:18:11 +000096 execsql {
97 COMMIT;
98 BEGIN;
99 SELECT * FROM abc;
100 }
drh9b5d76b2015-03-10 13:50:18 +0000101 value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
102} [value_in_range $::pgalloc $::mrange]
danielk1977468c82b2008-08-27 16:38:56 +0000103
danielk19770190d1d2005-12-19 14:18:11 +0000104do_test malloc5-1.5 {
danielk1977468c82b2008-08-27 16:38:56 +0000105 # Conclude the transaction opened in the previous [do_test] block. This
106 # causes another page (page 1) to become eligible for recycling.
107 #
108 execsql { COMMIT }
drh9b5d76b2015-03-10 13:50:18 +0000109 value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
110} [value_in_range $::pgalloc $::mrange]
danielk1977468c82b2008-08-27 16:38:56 +0000111
112do_test malloc5-1.6 {
danielk19770190d1d2005-12-19 14:18:11 +0000113 # Manipulate the cache so that it contains two unused pages. One requires
114 # a journal-sync to free, the other does not.
danielk197724168722007-04-02 05:07:47 +0000115 db2 close
danielk19770190d1d2005-12-19 14:18:11 +0000116 execsql {
danielk1977468c82b2008-08-27 16:38:56 +0000117 BEGIN;
danielk19770190d1d2005-12-19 14:18:11 +0000118 SELECT * FROM abc;
119 CREATE TABLE def(d, e, f);
120 }
drh9b5d76b2015-03-10 13:50:18 +0000121 value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500]
122} [value_in_range $::pgalloc $::mrange]
danielk1977468c82b2008-08-27 16:38:56 +0000123
danielk19770190d1d2005-12-19 14:18:11 +0000124do_test malloc5-1.7 {
danielk1977468c82b2008-08-27 16:38:56 +0000125 # Database should not be locked this time.
126 sqlite3 db2 test.db
127 catchsql { SELECT * FROM abc } db2
128} {0 {}}
129do_test malloc5-1.8 {
130 # Try to release another block of memory. This will fail as the only
131 # pages currently in the cache are dirty (page 3) or pinned (page 1).
danielk197724168722007-04-02 05:07:47 +0000132 db2 close
drh6aafc292006-01-05 15:50:06 +0000133 sqlite3_release_memory 500
danielk1977468c82b2008-08-27 16:38:56 +0000134} 0
danielk19770190d1d2005-12-19 14:18:11 +0000135do_test malloc5-1.8 {
danielk1977468c82b2008-08-27 16:38:56 +0000136 # Database is still not locked.
137 #
danielk197724168722007-04-02 05:07:47 +0000138 sqlite3 db2 test.db
danielk1977468c82b2008-08-27 16:38:56 +0000139 catchsql { SELECT * FROM abc } db2
140} {0 {}}
danielk19770190d1d2005-12-19 14:18:11 +0000141do_test malloc5-1.9 {
142 execsql {
143 COMMIT;
144 }
145} {}
146
danielk19770190d1d2005-12-19 14:18:11 +0000147do_test malloc5-2.1 {
148 # Put some data in tables abc and def. Both tables are still wholly
149 # contained within their root pages.
150 execsql {
151 INSERT INTO abc VALUES(1, 2, 3);
152 INSERT INTO abc VALUES(4, 5, 6);
153 INSERT INTO def VALUES(7, 8, 9);
154 INSERT INTO def VALUES(10,11,12);
155 }
156} {}
157do_test malloc5-2.2 {
158 # Load the root-page for table def into the cache. Then query table abc.
159 # Halfway through the query call sqlite3_release_memory(). The goal of this
160 # test is to make sure we don't free pages that are in use (specifically,
161 # the root of table abc).
danielk1977468c82b2008-08-27 16:38:56 +0000162 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +0000163 set nRelease 0
164 execsql {
165 BEGIN;
166 SELECT * FROM def;
167 }
danielk19775591df52005-12-20 09:19:37 +0000168 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000169 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000170 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000171 lappend data $a $b $c
172 }
danielk19775591df52005-12-20 09:19:37 +0000173 execsql {
174 COMMIT;
175 }
danielk19770190d1d2005-12-19 14:18:11 +0000176 list $nRelease $data
177} [list $pgalloc [list 1 2 3 4 5 6]]
178
danielk19775591df52005-12-20 09:19:37 +0000179do_test malloc5-3.1 {
180 # Simple test to show that if two pagers are opened from within this
181 # thread, memory is freed from both when sqlite3_release_memory() is
182 # called.
183 execsql {
184 BEGIN;
185 SELECT * FROM abc;
186 }
187 execsql {
188 SELECT * FROM sqlite_master;
189 BEGIN;
190 SELECT * FROM def;
191 } db2
drh6aafc292006-01-05 15:50:06 +0000192 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000193} [expr $::pgalloc * 2]
194do_test malloc5-3.2 {
195 concat \
196 [execsql {SELECT * FROM abc; COMMIT}] \
197 [execsql {SELECT * FROM def; COMMIT} db2]
198} {1 2 3 4 5 6 7 8 9 10 11 12}
199
200db2 close
drhed138fb2007-08-22 22:04:37 +0000201puts "Highwater mark: [sqlite3_memory_highwater]"
danielk19775591df52005-12-20 09:19:37 +0000202
203# The following two test cases each execute a transaction in which
204# 10000 rows are inserted into table abc. The first test case is used
205# to ensure that more than 1MB of dynamic memory is used to perform
206# the transaction.
207#
208# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
209# and tests to see that this limit is not exceeded at any point during
210# transaction execution.
211#
danielk1977aef0bf62005-12-30 16:28:01 +0000212# Before executing malloc5-4.* we save the value of the current soft heap
213# limit in variable ::soft_limit. The original value is restored after
214# running the tests.
215#
drh6aafc292006-01-05 15:50:06 +0000216set ::soft_limit [sqlite3_soft_heap_limit -1]
drh3a7fb7c2007-08-10 16:41:08 +0000217execsql {PRAGMA cache_size=2000}
danielk19775591df52005-12-20 09:19:37 +0000218do_test malloc5-4.1 {
219 execsql {BEGIN;}
220 execsql {DELETE FROM abc;}
221 for {set i 0} {$i < 10000} {incr i} {
222 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
223 }
224 execsql {COMMIT;}
danc63e8802013-08-21 20:04:54 +0000225 db cache flush
danielk1977468c82b2008-08-27 16:38:56 +0000226 sqlite3_release_memory
227 sqlite3_memory_highwater 1
228 execsql {SELECT * FROM abc}
drhed138fb2007-08-22 22:04:37 +0000229 set nMaxBytes [sqlite3_memory_highwater 1]
230 puts -nonewline " (Highwater mark: $nMaxBytes) "
231 expr $nMaxBytes > 1000000
danielk19775591df52005-12-20 09:19:37 +0000232} {1}
233do_test malloc5-4.2 {
danc63e8802013-08-21 20:04:54 +0000234 db cache flush
drh6aafc292006-01-05 15:50:06 +0000235 sqlite3_release_memory
danielk1977468c82b2008-08-27 16:38:56 +0000236 sqlite3_soft_heap_limit 100000
drhed138fb2007-08-22 22:04:37 +0000237 sqlite3_memory_highwater 1
danielk1977468c82b2008-08-27 16:38:56 +0000238 execsql {SELECT * FROM abc}
drhed138fb2007-08-22 22:04:37 +0000239 set nMaxBytes [sqlite3_memory_highwater 1]
240 puts -nonewline " (Highwater mark: $nMaxBytes) "
drh385e52f2011-10-08 21:39:11 +0000241 expr $nMaxBytes <= 110000
danielk19775591df52005-12-20 09:19:37 +0000242} {1}
243do_test malloc5-4.3 {
244 # Check that the content of table abc is at least roughly as expected.
245 execsql {
246 SELECT count(*), sum(a), sum(b) FROM abc;
247 }
danielk1977468c82b2008-08-27 16:38:56 +0000248} [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
danielk19775591df52005-12-20 09:19:37 +0000249
danielk1977aef0bf62005-12-30 16:28:01 +0000250# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000251sqlite3_soft_heap_limit $::soft_limit
danielk197752622822006-01-09 09:59:49 +0000252
danielk1977c551edc2007-04-05 13:12:13 +0000253# Test that there are no problems calling sqlite3_release_memory when
254# there are open in-memory databases.
255#
256# At one point these tests would cause a seg-fault.
257#
258do_test malloc5-5.1 {
259 db close
260 sqlite3 db :memory:
261 execsql {
262 BEGIN;
263 CREATE TABLE abc(a, b, c);
264 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
265 INSERT INTO abc SELECT * FROM abc;
266 INSERT INTO abc SELECT * FROM abc;
267 INSERT INTO abc SELECT * FROM abc;
268 INSERT INTO abc SELECT * FROM abc;
269 INSERT INTO abc SELECT * FROM abc;
270 INSERT INTO abc SELECT * FROM abc;
271 INSERT INTO abc SELECT * FROM abc;
272 }
273 sqlite3_release_memory
274} 0
danielk197784f786f2007-08-28 08:00:17 +0000275do_test malloc5-5.2 {
danielk1977c551edc2007-04-05 13:12:13 +0000276 sqlite3_soft_heap_limit 5000
277 execsql {
278 COMMIT;
279 PRAGMA temp_store = memory;
280 SELECT * FROM abc ORDER BY a;
281 }
282 expr 1
283} {1}
danielk197784f786f2007-08-28 08:00:17 +0000284sqlite3_soft_heap_limit $::soft_limit
285
286#-------------------------------------------------------------------------
287# The following test cases (malloc5-6.*) test the new global LRU list
288# used to determine the pages to recycle when sqlite3_release_memory is
289# called and there is more than one pager open.
290#
291proc nPage {db} {
292 set bt [btree_from_db $db]
293 array set stats [btree_pager_stats $bt]
294 set stats(page)
295}
296db close
mistachkinfda06be2011-08-02 00:57:34 +0000297forcedelete test.db test.db-journal test2.db test2.db-journal
danielk197784f786f2007-08-28 08:00:17 +0000298
299# This block of test-cases (malloc5-6.1.*) prepares two database files
300# for the subsequent tests.
301do_test malloc5-6.1.1 {
302 sqlite3 db test.db
303 execsql {
304 PRAGMA page_size=1024;
305 PRAGMA default_cache_size=10;
danielk1977801880f2008-08-21 15:54:01 +0000306 }
307 execsql {
308 PRAGMA temp_store = memory;
danielk197784f786f2007-08-28 08:00:17 +0000309 BEGIN;
310 CREATE TABLE abc(a PRIMARY KEY, b, c);
311 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
312 INSERT INTO abc
313 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
314 INSERT INTO abc
315 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
316 INSERT INTO abc
317 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
318 INSERT INTO abc
319 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
320 INSERT INTO abc
321 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
322 INSERT INTO abc
323 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
324 COMMIT;
325 }
mistachkinfda06be2011-08-02 00:57:34 +0000326 forcecopy test.db test2.db
danielk197784f786f2007-08-28 08:00:17 +0000327 sqlite3 db2 test2.db
danielk1977fa18bec2007-09-03 11:04:22 +0000328 list \
329 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
330} {1 1}
danielk197784f786f2007-08-28 08:00:17 +0000331do_test malloc5-6.1.2 {
332 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
333} {10 10}
334
335do_test malloc5-6.2.1 {
danielk1977468c82b2008-08-27 16:38:56 +0000336 execsql {SELECT * FROM abc} db2
danielk197784f786f2007-08-28 08:00:17 +0000337 execsql {SELECT * FROM abc} db
danielk1977801880f2008-08-21 15:54:01 +0000338 expr [nPage db] + [nPage db2]
339} {20}
340
danielk197784f786f2007-08-28 08:00:17 +0000341do_test malloc5-6.2.2 {
342 # If we now try to reclaim some memory, it should come from the db2 cache.
343 sqlite3_release_memory 3000
danielk1977801880f2008-08-21 15:54:01 +0000344 expr [nPage db] + [nPage db2]
345} {17}
danielk197784f786f2007-08-28 08:00:17 +0000346do_test malloc5-6.2.3 {
347 # Access the db2 cache again, so that all the db2 pages have been used
348 # more recently than all the db pages. Then try to reclaim 3000 bytes.
349 # This time, 3 pages should be pulled from the db cache.
350 execsql { SELECT * FROM abc } db2
351 sqlite3_release_memory 3000
danielk1977801880f2008-08-21 15:54:01 +0000352 expr [nPage db] + [nPage db2]
353} {17}
danielk197784f786f2007-08-28 08:00:17 +0000354
355do_test malloc5-6.3.1 {
356 # Now open a transaction and update 2 pages in the db2 cache. Then
357 # do a SELECT on the db cache so that all the db pages are more recently
358 # used than the db2 pages. When we try to free memory, SQLite should
359 # free the non-dirty db2 pages, then the db pages, then finally use
360 # sync() to free up the dirty db2 pages. The only page that cannot be
361 # freed is page1 of db2. Because there is an open transaction, the
362 # btree layer holds a reference to page 1 in the db2 cache.
363 execsql {
364 BEGIN;
365 UPDATE abc SET c = randstr(100,100)
366 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
367 } db2
368 execsql { SELECT * FROM abc } db
danielk1977801880f2008-08-21 15:54:01 +0000369 expr [nPage db] + [nPage db2]
370} {20}
danielk197784f786f2007-08-28 08:00:17 +0000371do_test malloc5-6.3.2 {
372 # Try to release 7700 bytes. This should release all the
373 # non-dirty pages held by db2.
dan9d69c5d2012-01-13 10:04:10 +0000374 sqlite3_release_memory [expr 7*1132]
danielk197784f786f2007-08-28 08:00:17 +0000375 list [nPage db] [nPage db2]
376} {10 3}
377do_test malloc5-6.3.3 {
378 # Try to release another 1000 bytes. This should come fromt the db
379 # cache, since all three pages held by db2 are either in-use or diry.
380 sqlite3_release_memory 1000
381 list [nPage db] [nPage db2]
382} {9 3}
383do_test malloc5-6.3.4 {
384 # Now release 9900 more (about 9 pages worth). This should expunge
385 # the rest of the db cache. But the db2 cache remains intact, because
386 # SQLite tries to avoid calling sync().
drh11809362009-04-11 19:09:53 +0000387 if {$::tcl_platform(wordSize)==8} {
dan9d69c5d2012-01-13 10:04:10 +0000388 sqlite3_release_memory 10500
drh11809362009-04-11 19:09:53 +0000389 } else {
390 sqlite3_release_memory 9900
391 }
danielk197784f786f2007-08-28 08:00:17 +0000392 list [nPage db] [nPage db2]
393} {0 3}
394do_test malloc5-6.3.5 {
395 # But if we are really insistent, SQLite will consent to call sync()
danielk1977468c82b2008-08-27 16:38:56 +0000396 # if there is no other option. UPDATE: As of 3.6.2, SQLite will not
397 # call sync() in this scenario. So no further memory can be reclaimed.
danielk197784f786f2007-08-28 08:00:17 +0000398 sqlite3_release_memory 1000
399 list [nPage db] [nPage db2]
danielk1977468c82b2008-08-27 16:38:56 +0000400} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000401do_test malloc5-6.3.6 {
402 # The referenced page (page 1 of the db2 cache) will not be freed no
403 # matter how much memory we ask for:
404 sqlite3_release_memory 31459
405 list [nPage db] [nPage db2]
danielk1977468c82b2008-08-27 16:38:56 +0000406} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000407
408db2 close
danielk1977c551edc2007-04-05 13:12:13 +0000409
410sqlite3_soft_heap_limit $::soft_limit
411finish_test
danielk197752622822006-01-09 09:59:49 +0000412catch {db close}