blob: 2b38a160ed27d168cea21e65b1e34d6504d32379 [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#
drh6aafc292006-01-05 15:50:06 +000015# $Id: malloc5.test,v 1.4 2006/01/05 15:50:07 drh Exp $
danielk19770190d1d2005-12-19 14:18:11 +000016
17#---------------------------------------------------------------------------
18# NOTES ON EXPECTED BEHAVIOUR
19#
20#---------------------------------------------------------------------------
21
22set testdir [file dirname $argv0]
23source $testdir/tester.tcl
24
danielk1977aef0bf62005-12-30 16:28:01 +000025# Only run these tests if memory debugging is turned on.
26if {[info command sqlite_malloc_stat]==""} {
27 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
28 finish_test
29 return
30}
31
danielk19770190d1d2005-12-19 14:18:11 +000032do_test malloc5-1.1 {
drh6aafc292006-01-05 15:50:06 +000033 # Simplest possible test. Call sqlite3_release_memory when there is exactly
danielk19770190d1d2005-12-19 14:18:11 +000034 # one unused page in a single pager cache. This test case set's the
35 # value of the ::pgalloc variable, which is used in subsequent tests.
36 #
37 # Note: Even though executing this statement on an empty database
38 # modifies 2 pages (the root of sqlite_master and the new root page),
39 # the sqlite_master root (page 1) is never freed because the btree layer
40 # retains a reference to it for the entire transaction.
41 execsql {
42 BEGIN;
43 CREATE TABLE abc(a, b, c);
44 }
drh6aafc292006-01-05 15:50:06 +000045 set ::pgalloc [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +000046 expr $::pgalloc > 0
47} {1}
48do_test malloc5-1.2 {
49 # Test that the transaction started in the above test is still active.
50 # Because the page freed had been written to, freeing it required a
51 # journal sync and exclusive lock on the database file. Test the file
52 # appears to be locked.
53 sqlite3 db2 test.db
54 catchsql {
55 SELECT * FROM abc;
56 } db2
57} {1 {database is locked}}
58do_test malloc5-1.3 {
drh6aafc292006-01-05 15:50:06 +000059 # Again call [sqlite3_release_memory] when there is exactly one unused page
danielk19770190d1d2005-12-19 14:18:11 +000060 # in the cache. The same amount of memory is required, but no journal-sync
61 # or exclusive lock should be established.
62 execsql {
63 COMMIT;
64 BEGIN;
65 SELECT * FROM abc;
66 }
drh6aafc292006-01-05 15:50:06 +000067 sqlite3_release_memory
danielk19770190d1d2005-12-19 14:18:11 +000068} $::pgalloc
69do_test malloc5-1.4 {
70 # Database should not be locked this time.
71 catchsql {
72 SELECT * FROM abc;
73 } db2
74} {0 {}}
75do_test malloc5-1.5 {
76 # Manipulate the cache so that it contains two unused pages. One requires
77 # a journal-sync to free, the other does not.
78 execsql {
79 SELECT * FROM abc;
80 CREATE TABLE def(d, e, f);
81 }
drh6aafc292006-01-05 15:50:06 +000082 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000083} $::pgalloc
84do_test malloc5-1.6 {
85 # Database should not be locked this time. The above test case only
86 # requested 500 bytes of memory, which can be obtained by freeing the page
87 # that does not require an fsync().
88 catchsql {
89 SELECT * FROM abc;
90 } db2
91} {0 {}}
92do_test malloc5-1.7 {
93 # Release another 500 bytes of memory. This time we require a sync(),
94 # so the database file will be locked afterwards.
drh6aafc292006-01-05 15:50:06 +000095 sqlite3_release_memory 500
danielk19770190d1d2005-12-19 14:18:11 +000096} $::pgalloc
97do_test malloc5-1.8 {
98 catchsql {
99 SELECT * FROM abc;
100 } db2
101} {1 {database is locked}}
102do_test malloc5-1.9 {
103 execsql {
104 COMMIT;
105 }
106} {}
107
danielk19770190d1d2005-12-19 14:18:11 +0000108do_test malloc5-2.1 {
109 # Put some data in tables abc and def. Both tables are still wholly
110 # contained within their root pages.
111 execsql {
112 INSERT INTO abc VALUES(1, 2, 3);
113 INSERT INTO abc VALUES(4, 5, 6);
114 INSERT INTO def VALUES(7, 8, 9);
115 INSERT INTO def VALUES(10,11,12);
116 }
117} {}
118do_test malloc5-2.2 {
119 # Load the root-page for table def into the cache. Then query table abc.
120 # Halfway through the query call sqlite3_release_memory(). The goal of this
121 # test is to make sure we don't free pages that are in use (specifically,
122 # the root of table abc).
123 set nRelease 0
124 execsql {
125 BEGIN;
126 SELECT * FROM def;
127 }
danielk19775591df52005-12-20 09:19:37 +0000128 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000129 db eval {SELECT * FROM abc} {
drh6aafc292006-01-05 15:50:06 +0000130 incr nRelease [sqlite3_release_memory]
danielk19770190d1d2005-12-19 14:18:11 +0000131 lappend data $a $b $c
132 }
danielk19775591df52005-12-20 09:19:37 +0000133 execsql {
134 COMMIT;
135 }
danielk19770190d1d2005-12-19 14:18:11 +0000136 list $nRelease $data
137} [list $pgalloc [list 1 2 3 4 5 6]]
138
danielk19775591df52005-12-20 09:19:37 +0000139do_test malloc5-3.1 {
140 # Simple test to show that if two pagers are opened from within this
141 # thread, memory is freed from both when sqlite3_release_memory() is
142 # called.
143 execsql {
144 BEGIN;
145 SELECT * FROM abc;
146 }
147 execsql {
148 SELECT * FROM sqlite_master;
149 BEGIN;
150 SELECT * FROM def;
151 } db2
drh6aafc292006-01-05 15:50:06 +0000152 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000153} [expr $::pgalloc * 2]
154do_test malloc5-3.2 {
155 concat \
156 [execsql {SELECT * FROM abc; COMMIT}] \
157 [execsql {SELECT * FROM def; COMMIT} db2]
158} {1 2 3 4 5 6 7 8 9 10 11 12}
159
160db2 close
161sqlite_malloc_outstanding -clearmaxbytes
162
163# The following two test cases each execute a transaction in which
164# 10000 rows are inserted into table abc. The first test case is used
165# to ensure that more than 1MB of dynamic memory is used to perform
166# the transaction.
167#
168# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
169# and tests to see that this limit is not exceeded at any point during
170# transaction execution.
171#
danielk1977aef0bf62005-12-30 16:28:01 +0000172# Before executing malloc5-4.* we save the value of the current soft heap
173# limit in variable ::soft_limit. The original value is restored after
174# running the tests.
175#
drh6aafc292006-01-05 15:50:06 +0000176set ::soft_limit [sqlite3_soft_heap_limit -1]
danielk19775591df52005-12-20 09:19:37 +0000177do_test malloc5-4.1 {
178 execsql {BEGIN;}
179 execsql {DELETE FROM abc;}
180 for {set i 0} {$i < 10000} {incr i} {
181 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
182 }
183 execsql {COMMIT;}
184 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
185 expr $::nMaxBytes > 1000000
186} {1}
187do_test malloc5-4.2 {
drh6aafc292006-01-05 15:50:06 +0000188 sqlite3_release_memory
danielk19775591df52005-12-20 09:19:37 +0000189 sqlite_malloc_outstanding -clearmaxbytes
drh6aafc292006-01-05 15:50:06 +0000190 sqlite3_soft_heap_limit 100000
danielk19775591df52005-12-20 09:19:37 +0000191 execsql {BEGIN;}
192 for {set i 0} {$i < 10000} {incr i} {
193 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
194 }
195 execsql {COMMIT;}
196 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
197 expr $::nMaxBytes <= 100000
198} {1}
199do_test malloc5-4.3 {
200 # Check that the content of table abc is at least roughly as expected.
201 execsql {
202 SELECT count(*), sum(a), sum(b) FROM abc;
203 }
204} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
205
danielk1977aef0bf62005-12-30 16:28:01 +0000206# Restore the soft heap limit.
drh6aafc292006-01-05 15:50:06 +0000207sqlite3_soft_heap_limit $::soft_limit
danielk19775591df52005-12-20 09:19:37 +0000208
danielk19770190d1d2005-12-19 14:18:11 +0000209finish_test