xref: /sqlite-3.40.0/test/malloc5.test (revision ed138fb3)
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.13 2007/08/22 22:04:37 drh 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 malloc 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  expr $nMaxBytes <= 100000
218} {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
226# Restore the soft heap limit.
227sqlite3_soft_heap_limit $::soft_limit
228
229# 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
251do_test malloc5-5.1 {
252  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}
260
261sqlite3_soft_heap_limit $::soft_limit
262finish_test
263catch {db close}
264