blob: 906ac8983460534091afd25084ec4966468a2fe1 [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
dan9f549d52017-03-29 16:55:23 +000042# The sizes of memory allocations from system malloc() might vary,
43# depending on the memory allocator algorithms used. The following
44# routine is designed to support answers that fall within a range
45# of values while also supplying easy-to-understand "expected" values
46# when errors occur.
47#
48proc value_in_range {target x args} {
49 set v [lindex $args 0]
50 if {$v!=""} {
51 if {$v<$target*$x} {return $v}
52 if {$v>$target/$x} {return $v}
53 }
54 return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]"
55}
56set mrange 0.98 ;# plus or minus 2%
57
dan03bc5252015-07-24 14:17:17 +000058test_set_config_pagecache 0 100
59
drh3aefaba2007-08-12 20:07:58 +000060sqlite3_soft_heap_limit 0
danielk197752622822006-01-09 09:59:49 +000061sqlite3 db test.db
dan9f549d52017-03-29 16:55:23 +000062# db eval {PRAGMA cache_size=1}
danielk197752622822006-01-09 09:59:49 +000063
danielk19770190d1d2005-12-19 14:18:11 +000064do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000065 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk1977468c82b2008-08-27 16:38:56 +000066 # one unused page in a single pager cache. The page cannot be freed, as
67 # it is dirty. So sqlite3_release_memory() returns 0.
danielk19770190d1d2005-12-19 14:18:11 +000068 #
danielk19770190d1d2005-12-19 14:18:11 +000069 execsql {
drh271d8cb2007-04-07 17:44:27 +000070 PRAGMA auto_vacuum=OFF;
danielk19770190d1d2005-12-19 14:18:11 +000071 BEGIN;
72 CREATE TABLE abc(a, b, c);
73 }
danielk1977468c82b2008-08-27 16:38:56 +000074 sqlite3_release_memory
75} {0}
76
77do_test malloc5-1.2 {
78 # Test that the transaction started in the above test is still active.
79 # The lock on the database file should not have been upgraded (this was
80 # not the case before version 3.6.2).
81 #
82 sqlite3 db2 test.db
drh957026a2015-07-16 18:18:19 +000083 execsql {PRAGMA cache_size=2; SELECT * FROM sqlite_master } db2
danielk1977468c82b2008-08-27 16:38:56 +000084} {}
85do_test malloc5-1.3 {
86 # Call [sqlite3_release_memory] when there is exactly one unused page
87 # in the cache belonging to db2.
88 #
drh6aafc292006-01-05 15:50:06 +000089 set ::pgalloc [sqlite3_release_memory]
dan9f549d52017-03-29 16:55:23 +000090 value_in_range 1288 0.75
91} [value_in_range 1288 0.75]
drh9b5d76b2015-03-10 13:50:18 +000092
danielk1977468c82b2008-08-27 16:38:56 +000093do_test malloc5-1.4 {
94 # Commit the transaction and open a new one. Read 1 page into the cache.
95 # Because the page is not dirty, it is eligible for collection even
96 # before the transaction is concluded.
97 #
danielk19770190d1d2005-12-19 14:18:11 +000098 execsql {
99 COMMIT;
100 BEGIN;
101 SELECT * FROM abc;
102 }
drh9b5d76b2015-03-10 13:50:18 +0000103 value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
104} [value_in_range $::pgalloc $::mrange]
danielk1977468c82b2008-08-27 16:38:56 +0000105
danielk19770190d1d2005-12-19 14:18:11 +0000106do_test malloc5-1.5 {
danielk1977468c82b2008-08-27 16:38:56 +0000107 # Conclude the transaction opened in the previous [do_test] block. This
108 # causes another page (page 1) to become eligible for recycling.
109 #
110 execsql { COMMIT }
drh9b5d76b2015-03-10 13:50:18 +0000111 value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
112} [value_in_range $::pgalloc $::mrange]
danielk1977468c82b2008-08-27 16:38:56 +0000113
114do_test malloc5-1.6 {
danielk19770190d1d2005-12-19 14:18:11 +0000115 # Manipulate the cache so that it contains two unused pages. One requires
116 # a journal-sync to free, the other does not.
danielk197724168722007-04-02 05:07:47 +0000117 db2 close
danielk19770190d1d2005-12-19 14:18:11 +0000118 execsql {
danielk1977468c82b2008-08-27 16:38:56 +0000119 BEGIN;
danielk19770190d1d2005-12-19 14:18:11 +0000120 CREATE TABLE def(d, e, f);
dan9f549d52017-03-29 16:55:23 +0000121 SELECT * FROM abc;
danielk19770190d1d2005-12-19 14:18:11 +0000122 }
drh9b5d76b2015-03-10 13:50:18 +0000123 value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500]
124} [value_in_range $::pgalloc $::mrange]
danielk19770190d1d2005-12-19 14:18:11 +0000125do_test malloc5-1.7 {
danielk1977468c82b2008-08-27 16:38:56 +0000126 # Database should not be locked this time.
127 sqlite3 db2 test.db
128 catchsql { SELECT * FROM abc } db2
129} {0 {}}
130do_test malloc5-1.8 {
131 # Try to release another block of memory. This will fail as the only
132 # pages currently in the cache are dirty (page 3) or pinned (page 1).
danielk197724168722007-04-02 05:07:47 +0000133 db2 close
drh6aafc292006-01-05 15:50:06 +0000134 sqlite3_release_memory 500
danielk1977468c82b2008-08-27 16:38:56 +0000135} 0
danielk19770190d1d2005-12-19 14:18:11 +0000136do_test malloc5-1.8 {
danielk1977468c82b2008-08-27 16:38:56 +0000137 # Database is still not locked.
138 #
danielk197724168722007-04-02 05:07:47 +0000139 sqlite3 db2 test.db
danielk1977468c82b2008-08-27 16:38:56 +0000140 catchsql { SELECT * FROM abc } db2
141} {0 {}}
danielk19770190d1d2005-12-19 14:18:11 +0000142do_test malloc5-1.9 {
143 execsql {
144 COMMIT;
145 }
146} {}
147
danielk19770190d1d2005-12-19 14:18:11 +0000148do_test malloc5-2.1 {
149 # Put some data in tables abc and def. Both tables are still wholly
150 # contained within their root pages.
151 execsql {
152 INSERT INTO abc VALUES(1, 2, 3);
153 INSERT INTO abc VALUES(4, 5, 6);
154 INSERT INTO def VALUES(7, 8, 9);
155 INSERT INTO def VALUES(10,11,12);
156 }
157} {}
158do_test malloc5-2.2 {
159 # Load the root-page for table def into the cache. Then query table abc.
160 # Halfway through the query call sqlite3_release_memory(). The goal of this
161 # test is to make sure we don't free pages that are in use (specifically,
162 # the root of table abc).
danielk1977468c82b2008-08-27 16:38:56 +0000163 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +0000164 set nRelease 0
165 execsql {
166 BEGIN;
167 SELECT * FROM def;
168 }
danielk19775591df52005-12-20 09:19:37 +0000169 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000170 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000171 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000172 lappend data $a $b $c
173 }
danielk19775591df52005-12-20 09:19:37 +0000174 execsql {
175 COMMIT;
176 }
dan69a4b382018-06-02 17:00:57 +0000177 value_in_range $::pgalloc $::mrange $nRelease
178} [value_in_range $::pgalloc $::mrange]
179do_test malloc5-2.2.1 {
180 set data
181} {1 2 3 4 5 6}
danielk19770190d1d2005-12-19 14:18:11 +0000182
danielk19775591df52005-12-20 09:19:37 +0000183do_test malloc5-3.1 {
184 # Simple test to show that if two pagers are opened from within this
185 # thread, memory is freed from both when sqlite3_release_memory() is
186 # called.
187 execsql {
188 BEGIN;
189 SELECT * FROM abc;
190 }
191 execsql {
192 SELECT * FROM sqlite_master;
193 BEGIN;
194 SELECT * FROM def;
195 } db2
drh40a3cab2015-06-24 10:46:25 +0000196 value_in_range [expr $::pgalloc*2] 0.99 [sqlite3_release_memory]
197} [value_in_range [expr $::pgalloc * 2] 0.99]
danielk19775591df52005-12-20 09:19:37 +0000198do_test malloc5-3.2 {
199 concat \
200 [execsql {SELECT * FROM abc; COMMIT}] \
201 [execsql {SELECT * FROM def; COMMIT} db2]
202} {1 2 3 4 5 6 7 8 9 10 11 12}
203
204db2 close
drhed138fb2007-08-22 22:04:37 +0000205puts "Highwater mark: [sqlite3_memory_highwater]"
danielk19775591df52005-12-20 09:19:37 +0000206
207# The following two test cases each execute a transaction in which
208# 10000 rows are inserted into table abc. The first test case is used
209# to ensure that more than 1MB of dynamic memory is used to perform
210# the transaction.
211#
212# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
213# and tests to see that this limit is not exceeded at any point during
214# transaction execution.
215#
danielk1977aef0bf62005-12-30 16:28:01 +0000216# Before executing malloc5-4.* we save the value of the current soft heap
217# limit in variable ::soft_limit. The original value is restored after
218# running the tests.
219#
drh6aafc292006-01-05 15:50:06 +0000220set ::soft_limit [sqlite3_soft_heap_limit -1]
drh3a7fb7c2007-08-10 16:41:08 +0000221execsql {PRAGMA cache_size=2000}
danielk19775591df52005-12-20 09:19:37 +0000222do_test malloc5-4.1 {
223 execsql {BEGIN;}
224 execsql {DELETE FROM abc;}
225 for {set i 0} {$i < 10000} {incr i} {
226 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
227 }
228 execsql {COMMIT;}
danc63e8802013-08-21 20:04:54 +0000229 db cache flush
danielk1977468c82b2008-08-27 16:38:56 +0000230 sqlite3_release_memory
231 sqlite3_memory_highwater 1
232 execsql {SELECT * FROM abc}
drhed138fb2007-08-22 22:04:37 +0000233 set nMaxBytes [sqlite3_memory_highwater 1]
234 puts -nonewline " (Highwater mark: $nMaxBytes) "
235 expr $nMaxBytes > 1000000
danielk19775591df52005-12-20 09:19:37 +0000236} {1}
237do_test malloc5-4.2 {
drh957026a2015-07-16 18:18:19 +0000238 db eval {PRAGMA cache_size=1}
danc63e8802013-08-21 20:04:54 +0000239 db cache flush
drh6aafc292006-01-05 15:50:06 +0000240 sqlite3_release_memory
dana7fc2532017-02-07 19:23:51 +0000241 sqlite3_soft_heap_limit 200000
drhed138fb2007-08-22 22:04:37 +0000242 sqlite3_memory_highwater 1
danielk1977468c82b2008-08-27 16:38:56 +0000243 execsql {SELECT * FROM abc}
drhed138fb2007-08-22 22:04:37 +0000244 set nMaxBytes [sqlite3_memory_highwater 1]
245 puts -nonewline " (Highwater mark: $nMaxBytes) "
dana7fc2532017-02-07 19:23:51 +0000246 expr $nMaxBytes <= 210000
danielk19775591df52005-12-20 09:19:37 +0000247} {1}
248do_test malloc5-4.3 {
249 # Check that the content of table abc is at least roughly as expected.
250 execsql {
251 SELECT count(*), sum(a), sum(b) FROM abc;
252 }
danielk1977468c82b2008-08-27 16:38:56 +0000253} [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
danielk19775591df52005-12-20 09:19:37 +0000254
danielk1977aef0bf62005-12-30 16:28:01 +0000255# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000256sqlite3_soft_heap_limit $::soft_limit
danielk197752622822006-01-09 09:59:49 +0000257
danielk1977c551edc2007-04-05 13:12:13 +0000258# Test that there are no problems calling sqlite3_release_memory when
259# there are open in-memory databases.
260#
261# At one point these tests would cause a seg-fault.
262#
263do_test malloc5-5.1 {
264 db close
265 sqlite3 db :memory:
266 execsql {
267 BEGIN;
268 CREATE TABLE abc(a, b, c);
269 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
270 INSERT INTO abc SELECT * FROM abc;
271 INSERT INTO abc SELECT * FROM abc;
272 INSERT INTO abc SELECT * FROM abc;
273 INSERT INTO abc SELECT * FROM abc;
274 INSERT INTO abc SELECT * FROM abc;
275 INSERT INTO abc SELECT * FROM abc;
276 INSERT INTO abc SELECT * FROM abc;
277 }
278 sqlite3_release_memory
279} 0
danielk197784f786f2007-08-28 08:00:17 +0000280do_test malloc5-5.2 {
danielk1977c551edc2007-04-05 13:12:13 +0000281 sqlite3_soft_heap_limit 5000
282 execsql {
283 COMMIT;
284 PRAGMA temp_store = memory;
285 SELECT * FROM abc ORDER BY a;
286 }
287 expr 1
288} {1}
danielk197784f786f2007-08-28 08:00:17 +0000289sqlite3_soft_heap_limit $::soft_limit
290
291#-------------------------------------------------------------------------
292# The following test cases (malloc5-6.*) test the new global LRU list
293# used to determine the pages to recycle when sqlite3_release_memory is
294# called and there is more than one pager open.
295#
296proc nPage {db} {
297 set bt [btree_from_db $db]
298 array set stats [btree_pager_stats $bt]
299 set stats(page)
300}
301db close
mistachkinfda06be2011-08-02 00:57:34 +0000302forcedelete test.db test.db-journal test2.db test2.db-journal
danielk197784f786f2007-08-28 08:00:17 +0000303
304# This block of test-cases (malloc5-6.1.*) prepares two database files
305# for the subsequent tests.
306do_test malloc5-6.1.1 {
307 sqlite3 db test.db
308 execsql {
309 PRAGMA page_size=1024;
drh957026a2015-07-16 18:18:19 +0000310 PRAGMA default_cache_size=2;
danielk1977801880f2008-08-21 15:54:01 +0000311 }
312 execsql {
313 PRAGMA temp_store = memory;
danielk197784f786f2007-08-28 08:00:17 +0000314 BEGIN;
315 CREATE TABLE abc(a PRIMARY KEY, b, c);
316 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
317 INSERT INTO abc
318 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
319 INSERT INTO abc
320 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
321 INSERT INTO abc
322 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
323 INSERT INTO abc
324 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
325 INSERT INTO abc
326 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
327 INSERT INTO abc
328 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
329 COMMIT;
330 }
mistachkinfda06be2011-08-02 00:57:34 +0000331 forcecopy test.db test2.db
danielk197784f786f2007-08-28 08:00:17 +0000332 sqlite3 db2 test2.db
drh957026a2015-07-16 18:18:19 +0000333 db2 eval {PRAGMA cache_size=2}
danielk1977fa18bec2007-09-03 11:04:22 +0000334 list \
335 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
336} {1 1}
danielk197784f786f2007-08-28 08:00:17 +0000337do_test malloc5-6.1.2 {
338 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
drh957026a2015-07-16 18:18:19 +0000339} {2 2}
danielk197784f786f2007-08-28 08:00:17 +0000340
341do_test malloc5-6.2.1 {
danielk1977468c82b2008-08-27 16:38:56 +0000342 execsql {SELECT * FROM abc} db2
danielk197784f786f2007-08-28 08:00:17 +0000343 execsql {SELECT * FROM abc} db
danielk1977801880f2008-08-21 15:54:01 +0000344 expr [nPage db] + [nPage db2]
drh957026a2015-07-16 18:18:19 +0000345} {4}
danielk1977801880f2008-08-21 15:54:01 +0000346
danielk197784f786f2007-08-28 08:00:17 +0000347do_test malloc5-6.2.2 {
348 # If we now try to reclaim some memory, it should come from the db2 cache.
349 sqlite3_release_memory 3000
danielk1977801880f2008-08-21 15:54:01 +0000350 expr [nPage db] + [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000351} {1}
danielk197784f786f2007-08-28 08:00:17 +0000352do_test malloc5-6.2.3 {
353 # Access the db2 cache again, so that all the db2 pages have been used
354 # more recently than all the db pages. Then try to reclaim 3000 bytes.
355 # This time, 3 pages should be pulled from the db cache.
356 execsql { SELECT * FROM abc } db2
357 sqlite3_release_memory 3000
danielk1977801880f2008-08-21 15:54:01 +0000358 expr [nPage db] + [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000359} {0}
danielk197784f786f2007-08-28 08:00:17 +0000360
361do_test malloc5-6.3.1 {
362 # Now open a transaction and update 2 pages in the db2 cache. Then
363 # do a SELECT on the db cache so that all the db pages are more recently
364 # used than the db2 pages. When we try to free memory, SQLite should
365 # free the non-dirty db2 pages, then the db pages, then finally use
366 # sync() to free up the dirty db2 pages. The only page that cannot be
367 # freed is page1 of db2. Because there is an open transaction, the
368 # btree layer holds a reference to page 1 in the db2 cache.
dan9f549d52017-03-29 16:55:23 +0000369 #
370 # UPDATE: No longer. As release_memory() does not cause a sync()
danielk197784f786f2007-08-28 08:00:17 +0000371 execsql {
372 BEGIN;
373 UPDATE abc SET c = randstr(100,100)
374 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
375 } db2
376 execsql { SELECT * FROM abc } db
danielk1977801880f2008-08-21 15:54:01 +0000377 expr [nPage db] + [nPage db2]
drh957026a2015-07-16 18:18:19 +0000378} {4}
danielk197784f786f2007-08-28 08:00:17 +0000379do_test malloc5-6.3.2 {
380 # Try to release 7700 bytes. This should release all the
381 # non-dirty pages held by db2.
dan9d69c5d2012-01-13 10:04:10 +0000382 sqlite3_release_memory [expr 7*1132]
danielk197784f786f2007-08-28 08:00:17 +0000383 list [nPage db] [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000384} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000385do_test malloc5-6.3.3 {
386 # Try to release another 1000 bytes. This should come fromt the db
387 # cache, since all three pages held by db2 are either in-use or diry.
388 sqlite3_release_memory 1000
389 list [nPage db] [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000390} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000391do_test malloc5-6.3.4 {
392 # Now release 9900 more (about 9 pages worth). This should expunge
393 # the rest of the db cache. But the db2 cache remains intact, because
394 # SQLite tries to avoid calling sync().
drh11809362009-04-11 19:09:53 +0000395 if {$::tcl_platform(wordSize)==8} {
dan9d69c5d2012-01-13 10:04:10 +0000396 sqlite3_release_memory 10500
drh11809362009-04-11 19:09:53 +0000397 } else {
398 sqlite3_release_memory 9900
399 }
danielk197784f786f2007-08-28 08:00:17 +0000400 list [nPage db] [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000401} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000402do_test malloc5-6.3.5 {
403 # But if we are really insistent, SQLite will consent to call sync()
danielk1977468c82b2008-08-27 16:38:56 +0000404 # if there is no other option. UPDATE: As of 3.6.2, SQLite will not
405 # call sync() in this scenario. So no further memory can be reclaimed.
danielk197784f786f2007-08-28 08:00:17 +0000406 sqlite3_release_memory 1000
407 list [nPage db] [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000408} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000409do_test malloc5-6.3.6 {
410 # The referenced page (page 1 of the db2 cache) will not be freed no
411 # matter how much memory we ask for:
412 sqlite3_release_memory 31459
413 list [nPage db] [nPage db2]
dan9f549d52017-03-29 16:55:23 +0000414} {0 3}
danielk197784f786f2007-08-28 08:00:17 +0000415
416db2 close
danielk1977c551edc2007-04-05 13:12:13 +0000417
418sqlite3_soft_heap_limit $::soft_limit
dan03bc5252015-07-24 14:17:17 +0000419test_restore_config_pagecache
danielk1977c551edc2007-04-05 13:12:13 +0000420finish_test
danielk197752622822006-01-09 09:59:49 +0000421catch {db close}