xref: /sqlite-3.40.0/test/malloc5.test (revision cd7274ce)
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# $Id: malloc5.test,v 1.17 2007/10/03 09:43:55 danielk1977 Exp $
16
17#---------------------------------------------------------------------------
18# NOTES ON EXPECTED BEHAVIOUR
19#
20#---------------------------------------------------------------------------
21
22
23set testdir [file dirname $argv0]
24source $testdir/tester.tcl
25db close
26
27# Only run these tests if memory debugging is turned on.
28#
29ifcapable !memdebug {
30   puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
31   finish_test
32   return
33}
34
35# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
36ifcapable !memorymanage {
37   finish_test
38   return
39}
40
41sqlite3_soft_heap_limit 0
42sqlite3 db test.db
43
44do_test malloc5-1.1 {
45  # Simplest possible test. Call sqlite3_release_memory when there is exactly
46  # 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 {
54    PRAGMA auto_vacuum=OFF;
55    BEGIN;
56    CREATE TABLE abc(a, b, c);
57  }
58  set ::pgalloc [sqlite3_release_memory]
59  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 {
72  # Again call [sqlite3_release_memory] when there is exactly one unused page
73  # 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  }
80  sqlite3_release_memory
81} $::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.
91  db2 close
92  execsql {
93    SELECT * FROM abc;
94    CREATE TABLE def(d, e, f);
95  }
96  sqlite3_release_memory 500
97} $::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().
102  sqlite3 db2 test.db
103  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.
110  db2 close
111  sqlite3_release_memory 500
112} $::pgalloc
113do_test malloc5-1.8 {
114  sqlite3 db2 test.db
115  catchsql {
116    SELECT * FROM abc;
117  } db2
118} {1 {database is locked}}
119do_test malloc5-1.9 {
120  execsql {
121    COMMIT;
122  }
123} {}
124
125do_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  }
145  set data [list]
146  db eval {SELECT * FROM abc} {
147    incr nRelease [sqlite3_release_memory]
148    lappend data $a $b $c
149  }
150  execsql {
151    COMMIT;
152  }
153  list $nRelease $data
154} [list $pgalloc [list 1 2 3 4 5 6]]
155
156do_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
169  sqlite3_release_memory
170} [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
178puts "Highwater mark: [sqlite3_memory_highwater]"
179
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#
189# 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#
193set ::soft_limit [sqlite3_soft_heap_limit -1]
194execsql {PRAGMA cache_size=2000}
195do_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;}
202  set nMaxBytes [sqlite3_memory_highwater 1]
203  puts -nonewline " (Highwater mark: $nMaxBytes) "
204  expr $nMaxBytes > 1000000
205} {1}
206do_test malloc5-4.2 {
207  sqlite3_release_memory
208  sqlite3_soft_heap_limit 100000
209  sqlite3_memory_highwater 1
210  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;}
215  set nMaxBytes [sqlite3_memory_highwater 1]
216  puts -nonewline " (Highwater mark: $nMaxBytes) "
217
218  # We used to test ($nMaxBytes<100000), because the soft-heap-limit is
219  # 100000 bytes. But if an allocation that will exceed the
220  # soft-heap-limit is requested from within the only pager instance in
221  # the system, then there is no way to free memory and the limit has to
222  # be exceeded. An exception is memory allocated to store actual page
223  # data (the code contains a special case for this).
224  #
225  # This is not a problem because all allocations apart from those
226  # used to store cached page data are both small and transient.
227  #
228  # Summary: the actual high-water mark for memory usage may be slightly
229  # higher than the soft-heap-limit. The specific allocations that cause
230  # the problem are the calls to sqlite3_malloc() inserted into selected
231  # sqlite3OsXXX() functions in test builds.
232  #
233  expr $nMaxBytes <= 100100
234} {1}
235do_test malloc5-4.3 {
236  # Check that the content of table abc is at least roughly as expected.
237  execsql {
238    SELECT count(*), sum(a), sum(b) FROM abc;
239  }
240} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
241
242# Restore the soft heap limit.
243sqlite3_soft_heap_limit $::soft_limit
244
245# Test that there are no problems calling sqlite3_release_memory when
246# there are open in-memory databases.
247#
248# At one point these tests would cause a seg-fault.
249#
250do_test malloc5-5.1 {
251  db close
252  sqlite3 db :memory:
253  execsql {
254    BEGIN;
255    CREATE TABLE abc(a, b, c);
256    INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
257    INSERT INTO abc SELECT * FROM abc;
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  }
265  sqlite3_release_memory
266} 0
267do_test malloc5-5.2 {
268  sqlite3_soft_heap_limit 5000
269  execsql {
270    COMMIT;
271    PRAGMA temp_store = memory;
272    SELECT * FROM abc ORDER BY a;
273  }
274  expr 1
275} {1}
276sqlite3_soft_heap_limit $::soft_limit
277
278#-------------------------------------------------------------------------
279# The following test cases (malloc5-6.*) test the new global LRU list
280# used to determine the pages to recycle when sqlite3_release_memory is
281# called and there is more than one pager open.
282#
283proc nPage {db} {
284  set bt [btree_from_db $db]
285  array set stats [btree_pager_stats $bt]
286  set stats(page)
287}
288db close
289file delete -force test.db test.db-journal test2.db test2.db-journal
290
291# This block of test-cases (malloc5-6.1.*) prepares two database files
292# for the subsequent tests.
293do_test malloc5-6.1.1 {
294  sqlite3 db test.db
295  execsql {
296    PRAGMA page_size=1024;
297    PRAGMA default_cache_size=10;
298    BEGIN;
299    CREATE TABLE abc(a PRIMARY KEY, b, c);
300    INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
301    INSERT INTO abc
302        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
303    INSERT INTO abc
304        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
305    INSERT INTO abc
306        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
307    INSERT INTO abc
308        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
309    INSERT INTO abc
310        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
311    INSERT INTO abc
312        SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
313    COMMIT;
314  }
315  copy_file test.db test2.db
316  sqlite3 db2 test2.db
317  list \
318    [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
319} {1 1}
320do_test malloc5-6.1.2 {
321  list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
322} {10 10}
323
324do_test malloc5-6.2.1 {
325  execsql { SELECT * FROM abc } db2
326  execsql {SELECT * FROM abc} db
327  list [nPage db] [nPage db2]
328} {10 10}
329do_test malloc5-6.2.2 {
330  # If we now try to reclaim some memory, it should come from the db2 cache.
331  sqlite3_release_memory 3000
332  list [nPage db] [nPage db2]
333} {10 7}
334do_test malloc5-6.2.3 {
335  # Access the db2 cache again, so that all the db2 pages have been used
336  # more recently than all the db pages. Then try to reclaim 3000 bytes.
337  # This time, 3 pages should be pulled from the db cache.
338  execsql { SELECT * FROM abc } db2
339  sqlite3_release_memory 3000
340  list [nPage db] [nPage db2]
341} {7 10}
342
343
344do_test malloc5-6.3.1 {
345  # Now open a transaction and update 2 pages in the db2 cache. Then
346  # do a SELECT on the db cache so that all the db pages are more recently
347  # used than the db2 pages. When we try to free memory, SQLite should
348  # free the non-dirty db2 pages, then the db pages, then finally use
349  # sync() to free up the dirty db2 pages. The only page that cannot be
350  # freed is page1 of db2. Because there is an open transaction, the
351  # btree layer holds a reference to page 1 in the db2 cache.
352  execsql {
353    BEGIN;
354    UPDATE abc SET c = randstr(100,100)
355    WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
356  } db2
357  execsql { SELECT * FROM abc } db
358  list [nPage db] [nPage db2]
359} {10 10}
360do_test malloc5-6.3.2 {
361  # Try to release 7700 bytes. This should release all the
362  # non-dirty pages held by db2.
363  sqlite3_release_memory [expr 7*1100]
364  list [nPage db] [nPage db2]
365} {10 3}
366do_test malloc5-6.3.3 {
367  # Try to release another 1000 bytes. This should come fromt the db
368  # cache, since all three pages held by db2 are either in-use or diry.
369  sqlite3_release_memory 1000
370  list [nPage db] [nPage db2]
371} {9 3}
372do_test malloc5-6.3.4 {
373  # Now release 9900 more (about 9 pages worth). This should expunge
374  # the rest of the db cache. But the db2 cache remains intact, because
375  # SQLite tries to avoid calling sync().
376  sqlite3_release_memory 9900
377  list [nPage db] [nPage db2]
378} {0 3}
379do_test malloc5-6.3.5 {
380  # But if we are really insistent, SQLite will consent to call sync()
381  # if there is no other option.
382  sqlite3_release_memory 1000
383  list [nPage db] [nPage db2]
384} {0 2}
385do_test malloc5-6.3.6 {
386  # The referenced page (page 1 of the db2 cache) will not be freed no
387  # matter how much memory we ask for:
388  sqlite3_release_memory 31459
389  list [nPage db] [nPage db2]
390} {0 1}
391
392db2 close
393
394sqlite3_soft_heap_limit $::soft_limit
395finish_test
396catch {db close}
397