xref: /sqlite-3.40.0/test/malloc5.test (revision cc285c5a)
1# 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#
12# This file contains test cases focused on the two memory-management APIs,
13# sqlite3_soft_heap_limit() and sqlite3_release_memory().
14#
15# 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.
20#
21# $Id: malloc5.test,v 1.22 2009/04/11 19:09:54 drh Exp $
22
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25source $testdir/malloc_common.tcl
26db close
27
28# Only run these tests if memory debugging is turned on.
29#
30if {!$MEMDEBUG} {
31   puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
32   finish_test
33   return
34}
35
36# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
37ifcapable !memorymanage {
38   finish_test
39   return
40}
41
42sqlite3_soft_heap_limit 0
43sqlite3 db test.db
44
45do_test malloc5-1.1 {
46  # Simplest possible test. Call sqlite3_release_memory when there is exactly
47  # one unused page in a single pager cache. The page cannot be freed, as
48  # it is dirty. So sqlite3_release_memory() returns 0.
49  #
50  execsql {
51    PRAGMA auto_vacuum=OFF;
52    BEGIN;
53    CREATE TABLE abc(a, b, c);
54  }
55  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  #
70  set ::pgalloc [sqlite3_release_memory]
71  expr $::pgalloc > 0
72} {1}
73
74# 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
91do_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  #
96  execsql {
97    COMMIT;
98    BEGIN;
99    SELECT * FROM abc;
100  }
101  value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
102} [value_in_range $::pgalloc $::mrange]
103
104do_test malloc5-1.5 {
105  # 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 }
109  value_in_range $::pgalloc $::mrange [sqlite3_release_memory]
110} [value_in_range $::pgalloc $::mrange]
111
112do_test malloc5-1.6 {
113  # Manipulate the cache so that it contains two unused pages. One requires
114  # a journal-sync to free, the other does not.
115  db2 close
116  execsql {
117    BEGIN;
118    SELECT * FROM abc;
119    CREATE TABLE def(d, e, f);
120  }
121  value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500]
122} [value_in_range $::pgalloc $::mrange]
123
124do_test malloc5-1.7 {
125  # 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).
132  db2 close
133  sqlite3_release_memory 500
134} 0
135do_test malloc5-1.8 {
136  # Database is still not locked.
137  #
138  sqlite3 db2 test.db
139  catchsql { SELECT * FROM abc } db2
140} {0 {}}
141do_test malloc5-1.9 {
142  execsql {
143    COMMIT;
144  }
145} {}
146
147do_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).
162  sqlite3_release_memory
163  set nRelease 0
164  execsql {
165    BEGIN;
166    SELECT * FROM def;
167  }
168  set data [list]
169  db eval {SELECT * FROM abc} {
170    incr nRelease [sqlite3_release_memory]
171    lappend data $a $b $c
172  }
173  execsql {
174    COMMIT;
175  }
176  list $nRelease $data
177} [list $pgalloc [list 1 2 3 4 5 6]]
178
179do_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
192  sqlite3_release_memory
193} [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
201puts "Highwater mark: [sqlite3_memory_highwater]"
202
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#
212# 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#
216set ::soft_limit [sqlite3_soft_heap_limit -1]
217execsql {PRAGMA cache_size=2000}
218do_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;}
225  db cache flush
226  sqlite3_release_memory
227  sqlite3_memory_highwater 1
228  execsql {SELECT * FROM abc}
229  set nMaxBytes [sqlite3_memory_highwater 1]
230  puts -nonewline " (Highwater mark: $nMaxBytes) "
231  expr $nMaxBytes > 1000000
232} {1}
233do_test malloc5-4.2 {
234  db cache flush
235  sqlite3_release_memory
236  sqlite3_soft_heap_limit 100000
237  sqlite3_memory_highwater 1
238  execsql {SELECT * FROM abc}
239  set nMaxBytes [sqlite3_memory_highwater 1]
240  puts -nonewline " (Highwater mark: $nMaxBytes) "
241  expr $nMaxBytes <= 110000
242} {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  }
248} [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
249
250# Restore the soft heap limit.
251sqlite3_soft_heap_limit $::soft_limit
252
253# 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
275do_test malloc5-5.2 {
276  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}
284sqlite3_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
297forcedelete test.db test.db-journal test2.db test2.db-journal
298
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;
306  }
307  execsql {
308    PRAGMA temp_store = memory;
309    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  }
326  forcecopy test.db test2.db
327  sqlite3 db2 test2.db
328  list \
329    [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
330} {1 1}
331do_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 {
336  execsql {SELECT * FROM abc} db2
337  execsql {SELECT * FROM abc} db
338  expr [nPage db] + [nPage db2]
339} {20}
340
341do_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
344  expr [nPage db] + [nPage db2]
345} {17}
346do_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
352  expr [nPage db] + [nPage db2]
353} {17}
354
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
369  expr [nPage db] + [nPage db2]
370} {20}
371do_test malloc5-6.3.2 {
372  # Try to release 7700 bytes. This should release all the
373  # non-dirty pages held by db2.
374  sqlite3_release_memory [expr 7*1132]
375  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().
387  if {$::tcl_platform(wordSize)==8} {
388    sqlite3_release_memory 10500
389  } else {
390    sqlite3_release_memory 9900
391  }
392  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()
396  # 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.
398  sqlite3_release_memory 1000
399  list [nPage db] [nPage db2]
400} {0 3}
401do_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]
406} {0 3}
407
408db2 close
409
410sqlite3_soft_heap_limit $::soft_limit
411finish_test
412catch {db close}
413