blob: f18ea26ba925c81269ab397854fc8707ea1f0a6e [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#
danielk1977fa18bec2007-09-03 11:04:22 +000015# $Id: malloc5.test,v 1.15 2007/09/03 11:04:22 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.
drhed138fb2007-08-22 22:04:37 +000028#
29ifcapable !memdebug {
danielk1977aef0bf62005-12-30 16:28:01 +000030 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
31 finish_test
32 return
33}
34
danielk197752622822006-01-09 09:59:49 +000035# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
36ifcapable !memorymanage {
37 finish_test
38 return
39}
40
drh3aefaba2007-08-12 20:07:58 +000041sqlite3_soft_heap_limit 0
danielk197752622822006-01-09 09:59:49 +000042sqlite3 db test.db
43
danielk19770190d1d2005-12-19 14:18:11 +000044do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000045 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk19770190d1d2005-12-19 14:18:11 +000046 # one unused page in a single pager cache. This test case set's the
47 # value of the ::pgalloc variable, which is used in subsequent tests.
48 #
49 # Note: Even though executing this statement on an empty database
50 # modifies 2 pages (the root of sqlite_master and the new root page),
51 # the sqlite_master root (page 1) is never freed because the btree layer
52 # retains a reference to it for the entire transaction.
53 execsql {
drh271d8cb2007-04-07 17:44:27 +000054 PRAGMA auto_vacuum=OFF;
danielk19770190d1d2005-12-19 14:18:11 +000055 BEGIN;
56 CREATE TABLE abc(a, b, c);
57 }
drh6aafc292006-01-05 15:50:06 +000058 set ::pgalloc [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +000059 expr $::pgalloc > 0
60} {1}
61do_test malloc5-1.2 {
62 # Test that the transaction started in the above test is still active.
63 # Because the page freed had been written to, freeing it required a
64 # journal sync and exclusive lock on the database file. Test the file
65 # appears to be locked.
66 sqlite3 db2 test.db
67 catchsql {
68 SELECT * FROM abc;
69 } db2
70} {1 {database is locked}}
71do_test malloc5-1.3 {
drh6aafc292006-01-05 15:50:06 +000072 # Again call [sqlite3_release_memory] when there is exactly one unused page
danielk19770190d1d2005-12-19 14:18:11 +000073 # in the cache. The same amount of memory is required, but no journal-sync
74 # or exclusive lock should be established.
75 execsql {
76 COMMIT;
77 BEGIN;
78 SELECT * FROM abc;
79 }
drh6aafc292006-01-05 15:50:06 +000080 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +000081} $::pgalloc
82do_test malloc5-1.4 {
83 # Database should not be locked this time.
84 catchsql {
85 SELECT * FROM abc;
86 } db2
87} {0 {}}
88do_test malloc5-1.5 {
89 # Manipulate the cache so that it contains two unused pages. One requires
90 # a journal-sync to free, the other does not.
danielk197724168722007-04-02 05:07:47 +000091 db2 close
danielk19770190d1d2005-12-19 14:18:11 +000092 execsql {
93 SELECT * FROM abc;
94 CREATE TABLE def(d, e, f);
95 }
drh6aafc292006-01-05 15:50:06 +000096 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000097} $::pgalloc
98do_test malloc5-1.6 {
99 # Database should not be locked this time. The above test case only
100 # requested 500 bytes of memory, which can be obtained by freeing the page
101 # that does not require an fsync().
danielk197724168722007-04-02 05:07:47 +0000102 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000103 catchsql {
104 SELECT * FROM abc;
105 } db2
106} {0 {}}
107do_test malloc5-1.7 {
108 # Release another 500 bytes of memory. This time we require a sync(),
109 # so the database file will be locked afterwards.
danielk197724168722007-04-02 05:07:47 +0000110 db2 close
drh6aafc292006-01-05 15:50:06 +0000111 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +0000112} $::pgalloc
113do_test malloc5-1.8 {
danielk197724168722007-04-02 05:07:47 +0000114 sqlite3 db2 test.db
danielk19770190d1d2005-12-19 14:18:11 +0000115 catchsql {
116 SELECT * FROM abc;
117 } db2
118} {1 {database is locked}}
119do_test malloc5-1.9 {
120 execsql {
121 COMMIT;
122 }
123} {}
124
danielk19770190d1d2005-12-19 14:18:11 +0000125do_test malloc5-2.1 {
126 # Put some data in tables abc and def. Both tables are still wholly
127 # contained within their root pages.
128 execsql {
129 INSERT INTO abc VALUES(1, 2, 3);
130 INSERT INTO abc VALUES(4, 5, 6);
131 INSERT INTO def VALUES(7, 8, 9);
132 INSERT INTO def VALUES(10,11,12);
133 }
134} {}
135do_test malloc5-2.2 {
136 # Load the root-page for table def into the cache. Then query table abc.
137 # Halfway through the query call sqlite3_release_memory(). The goal of this
138 # test is to make sure we don't free pages that are in use (specifically,
139 # the root of table abc).
140 set nRelease 0
141 execsql {
142 BEGIN;
143 SELECT * FROM def;
144 }
danielk19775591df52005-12-20 09:19:37 +0000145 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000146 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000147 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000148 lappend data $a $b $c
149 }
danielk19775591df52005-12-20 09:19:37 +0000150 execsql {
151 COMMIT;
152 }
danielk19770190d1d2005-12-19 14:18:11 +0000153 list $nRelease $data
154} [list $pgalloc [list 1 2 3 4 5 6]]
155
danielk19775591df52005-12-20 09:19:37 +0000156do_test malloc5-3.1 {
157 # Simple test to show that if two pagers are opened from within this
158 # thread, memory is freed from both when sqlite3_release_memory() is
159 # called.
160 execsql {
161 BEGIN;
162 SELECT * FROM abc;
163 }
164 execsql {
165 SELECT * FROM sqlite_master;
166 BEGIN;
167 SELECT * FROM def;
168 } db2
drh6aafc292006-01-05 15:50:06 +0000169 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000170} [expr $::pgalloc * 2]
171do_test malloc5-3.2 {
172 concat \
173 [execsql {SELECT * FROM abc; COMMIT}] \
174 [execsql {SELECT * FROM def; COMMIT} db2]
175} {1 2 3 4 5 6 7 8 9 10 11 12}
176
177db2 close
drhed138fb2007-08-22 22:04:37 +0000178puts "Highwater mark: [sqlite3_memory_highwater]"
danielk19775591df52005-12-20 09:19:37 +0000179
180# The following two test cases each execute a transaction in which
181# 10000 rows are inserted into table abc. The first test case is used
182# to ensure that more than 1MB of dynamic memory is used to perform
183# the transaction.
184#
185# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
186# and tests to see that this limit is not exceeded at any point during
187# transaction execution.
188#
danielk1977aef0bf62005-12-30 16:28:01 +0000189# Before executing malloc5-4.* we save the value of the current soft heap
190# limit in variable ::soft_limit. The original value is restored after
191# running the tests.
192#
drh6aafc292006-01-05 15:50:06 +0000193set ::soft_limit [sqlite3_soft_heap_limit -1]
drh3a7fb7c2007-08-10 16:41:08 +0000194execsql {PRAGMA cache_size=2000}
danielk19775591df52005-12-20 09:19:37 +0000195do_test malloc5-4.1 {
196 execsql {BEGIN;}
197 execsql {DELETE FROM abc;}
198 for {set i 0} {$i < 10000} {incr i} {
199 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
200 }
201 execsql {COMMIT;}
drhed138fb2007-08-22 22:04:37 +0000202 set nMaxBytes [sqlite3_memory_highwater 1]
203 puts -nonewline " (Highwater mark: $nMaxBytes) "
204 expr $nMaxBytes > 1000000
danielk19775591df52005-12-20 09:19:37 +0000205} {1}
206do_test malloc5-4.2 {
drh6aafc292006-01-05 15:50:06 +0000207 sqlite3_release_memory
drh6aafc292006-01-05 15:50:06 +0000208 sqlite3_soft_heap_limit 100000
drhed138fb2007-08-22 22:04:37 +0000209 sqlite3_memory_highwater 1
danielk19775591df52005-12-20 09:19:37 +0000210 execsql {BEGIN;}
211 for {set i 0} {$i < 10000} {incr i} {
212 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
213 }
214 execsql {COMMIT;}
drhed138fb2007-08-22 22:04:37 +0000215 set nMaxBytes [sqlite3_memory_highwater 1]
216 puts -nonewline " (Highwater mark: $nMaxBytes) "
217 expr $nMaxBytes <= 100000
danielk19775591df52005-12-20 09:19:37 +0000218} {1}
219do_test malloc5-4.3 {
220 # Check that the content of table abc is at least roughly as expected.
221 execsql {
222 SELECT count(*), sum(a), sum(b) FROM abc;
223 }
224} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
225
danielk1977aef0bf62005-12-30 16:28:01 +0000226# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000227sqlite3_soft_heap_limit $::soft_limit
danielk197752622822006-01-09 09:59:49 +0000228
danielk1977c551edc2007-04-05 13:12:13 +0000229# Test that there are no problems calling sqlite3_release_memory when
230# there are open in-memory databases.
231#
232# At one point these tests would cause a seg-fault.
233#
234do_test malloc5-5.1 {
235 db close
236 sqlite3 db :memory:
237 execsql {
238 BEGIN;
239 CREATE TABLE abc(a, b, c);
240 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
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 INSERT INTO abc SELECT * FROM abc;
247 INSERT INTO abc SELECT * FROM abc;
248 }
249 sqlite3_release_memory
250} 0
danielk197784f786f2007-08-28 08:00:17 +0000251do_test malloc5-5.2 {
danielk1977c551edc2007-04-05 13:12:13 +0000252 sqlite3_soft_heap_limit 5000
253 execsql {
254 COMMIT;
255 PRAGMA temp_store = memory;
256 SELECT * FROM abc ORDER BY a;
257 }
258 expr 1
259} {1}
danielk197784f786f2007-08-28 08:00:17 +0000260sqlite3_soft_heap_limit $::soft_limit
261
262#-------------------------------------------------------------------------
263# The following test cases (malloc5-6.*) test the new global LRU list
264# used to determine the pages to recycle when sqlite3_release_memory is
265# called and there is more than one pager open.
266#
267proc nPage {db} {
268 set bt [btree_from_db $db]
269 array set stats [btree_pager_stats $bt]
270 set stats(page)
271}
272db close
273file delete -force test.db test.db-journal test2.db test2.db-journal
274
275# This block of test-cases (malloc5-6.1.*) prepares two database files
276# for the subsequent tests.
277do_test malloc5-6.1.1 {
278 sqlite3 db test.db
279 execsql {
280 PRAGMA page_size=1024;
281 PRAGMA default_cache_size=10;
282 BEGIN;
283 CREATE TABLE abc(a PRIMARY KEY, b, c);
284 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
285 INSERT INTO abc
286 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
287 INSERT INTO abc
288 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
289 INSERT INTO abc
290 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
291 INSERT INTO abc
292 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
293 INSERT INTO abc
294 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
295 INSERT INTO abc
296 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
297 COMMIT;
298 }
299 copy_file test.db test2.db
300 sqlite3 db2 test2.db
danielk1977fa18bec2007-09-03 11:04:22 +0000301 list \
302 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
303} {1 1}
danielk197784f786f2007-08-28 08:00:17 +0000304do_test malloc5-6.1.2 {
305 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
306} {10 10}
307
308do_test malloc5-6.2.1 {
309 execsql { SELECT * FROM abc } db2
310 execsql {SELECT * FROM abc} db
311 list [nPage db] [nPage db2]
312} {10 10}
313do_test malloc5-6.2.2 {
314 # If we now try to reclaim some memory, it should come from the db2 cache.
315 sqlite3_release_memory 3000
316 list [nPage db] [nPage db2]
317} {10 7}
318do_test malloc5-6.2.3 {
319 # Access the db2 cache again, so that all the db2 pages have been used
320 # more recently than all the db pages. Then try to reclaim 3000 bytes.
321 # This time, 3 pages should be pulled from the db cache.
322 execsql { SELECT * FROM abc } db2
323 sqlite3_release_memory 3000
324 list [nPage db] [nPage db2]
325} {7 10}
326
327
328do_test malloc5-6.3.1 {
329 # Now open a transaction and update 2 pages in the db2 cache. Then
330 # do a SELECT on the db cache so that all the db pages are more recently
331 # used than the db2 pages. When we try to free memory, SQLite should
332 # free the non-dirty db2 pages, then the db pages, then finally use
333 # sync() to free up the dirty db2 pages. The only page that cannot be
334 # freed is page1 of db2. Because there is an open transaction, the
335 # btree layer holds a reference to page 1 in the db2 cache.
336 execsql {
337 BEGIN;
338 UPDATE abc SET c = randstr(100,100)
339 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
340 } db2
341 execsql { SELECT * FROM abc } db
342 list [nPage db] [nPage db2]
343} {10 10}
344do_test malloc5-6.3.2 {
345 # Try to release 7700 bytes. This should release all the
346 # non-dirty pages held by db2.
347 sqlite3_release_memory [expr 7*1100]
348 list [nPage db] [nPage db2]
349} {10 3}
350do_test malloc5-6.3.3 {
351 # Try to release another 1000 bytes. This should come fromt the db
352 # cache, since all three pages held by db2 are either in-use or diry.
353 sqlite3_release_memory 1000
354 list [nPage db] [nPage db2]
355} {9 3}
356do_test malloc5-6.3.4 {
357 # Now release 9900 more (about 9 pages worth). This should expunge
358 # the rest of the db cache. But the db2 cache remains intact, because
359 # SQLite tries to avoid calling sync().
360 sqlite3_release_memory 9900
361 list [nPage db] [nPage db2]
362} {0 3}
363do_test malloc5-6.3.5 {
364 # But if we are really insistent, SQLite will consent to call sync()
365 # if there is no other option.
366 sqlite3_release_memory 1000
367 list [nPage db] [nPage db2]
368} {0 2}
369do_test malloc5-6.3.6 {
370 # The referenced page (page 1 of the db2 cache) will not be freed no
371 # matter how much memory we ask for:
372 sqlite3_release_memory 31459
373 list [nPage db] [nPage db2]
374} {0 1}
375
376db2 close
danielk1977c551edc2007-04-05 13:12:13 +0000377
378sqlite3_soft_heap_limit $::soft_limit
379finish_test
danielk197752622822006-01-09 09:59:49 +0000380catch {db close}