blob: 61f25a862e0c6e0f6c2bcddef52441dcbe46b892 [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#
danielk19775591df52005-12-20 09:19:37 +000012# $Id: malloc5.test,v 1.2 2005/12/20 09:19:38 danielk1977 Exp $
danielk19770190d1d2005-12-19 14:18:11 +000013
14#---------------------------------------------------------------------------
15# NOTES ON EXPECTED BEHAVIOUR
16#
17#---------------------------------------------------------------------------
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22do_test malloc5-1.1 {
23 # Simplest possible test. Call [db release_memory] when there is exactly
24 # one unused page in a single pager cache. This test case set's the
25 # value of the ::pgalloc variable, which is used in subsequent tests.
26 #
27 # Note: Even though executing this statement on an empty database
28 # modifies 2 pages (the root of sqlite_master and the new root page),
29 # the sqlite_master root (page 1) is never freed because the btree layer
30 # retains a reference to it for the entire transaction.
31 execsql {
32 BEGIN;
33 CREATE TABLE abc(a, b, c);
34 }
35 set ::pgalloc [db release_memory]
36 expr $::pgalloc > 0
37} {1}
38do_test malloc5-1.2 {
39 # Test that the transaction started in the above test is still active.
40 # Because the page freed had been written to, freeing it required a
41 # journal sync and exclusive lock on the database file. Test the file
42 # appears to be locked.
43 sqlite3 db2 test.db
44 catchsql {
45 SELECT * FROM abc;
46 } db2
47} {1 {database is locked}}
48do_test malloc5-1.3 {
49 # Again call [db release_memory] when there is exactly one unused page
50 # in the cache. The same amount of memory is required, but no journal-sync
51 # or exclusive lock should be established.
52 execsql {
53 COMMIT;
54 BEGIN;
55 SELECT * FROM abc;
56 }
57 db release_memory
58} $::pgalloc
59do_test malloc5-1.4 {
60 # Database should not be locked this time.
61 catchsql {
62 SELECT * FROM abc;
63 } db2
64} {0 {}}
65do_test malloc5-1.5 {
66 # Manipulate the cache so that it contains two unused pages. One requires
67 # a journal-sync to free, the other does not.
68 execsql {
69 SELECT * FROM abc;
70 CREATE TABLE def(d, e, f);
71 }
72 db release_memory 500
73} $::pgalloc
74do_test malloc5-1.6 {
75 # Database should not be locked this time. The above test case only
76 # requested 500 bytes of memory, which can be obtained by freeing the page
77 # that does not require an fsync().
78 catchsql {
79 SELECT * FROM abc;
80 } db2
81} {0 {}}
82do_test malloc5-1.7 {
83 # Release another 500 bytes of memory. This time we require a sync(),
84 # so the database file will be locked afterwards.
85 db release_memory 500
86} $::pgalloc
87do_test malloc5-1.8 {
88 catchsql {
89 SELECT * FROM abc;
90 } db2
91} {1 {database is locked}}
92do_test malloc5-1.9 {
93 execsql {
94 COMMIT;
95 }
96} {}
97
danielk19770190d1d2005-12-19 14:18:11 +000098do_test malloc5-2.1 {
99 # Put some data in tables abc and def. Both tables are still wholly
100 # contained within their root pages.
101 execsql {
102 INSERT INTO abc VALUES(1, 2, 3);
103 INSERT INTO abc VALUES(4, 5, 6);
104 INSERT INTO def VALUES(7, 8, 9);
105 INSERT INTO def VALUES(10,11,12);
106 }
107} {}
108do_test malloc5-2.2 {
109 # Load the root-page for table def into the cache. Then query table abc.
110 # Halfway through the query call sqlite3_release_memory(). The goal of this
111 # test is to make sure we don't free pages that are in use (specifically,
112 # the root of table abc).
113 set nRelease 0
114 execsql {
115 BEGIN;
116 SELECT * FROM def;
117 }
danielk19775591df52005-12-20 09:19:37 +0000118 set data [list]
danielk19770190d1d2005-12-19 14:18:11 +0000119 db eval {SELECT * FROM abc} {
120 incr nRelease [db release_memory]
121 lappend data $a $b $c
122 }
danielk19775591df52005-12-20 09:19:37 +0000123 execsql {
124 COMMIT;
125 }
danielk19770190d1d2005-12-19 14:18:11 +0000126 list $nRelease $data
127} [list $pgalloc [list 1 2 3 4 5 6]]
128
danielk19775591df52005-12-20 09:19:37 +0000129do_test malloc5-3.1 {
130 # Simple test to show that if two pagers are opened from within this
131 # thread, memory is freed from both when sqlite3_release_memory() is
132 # called.
133 execsql {
134 BEGIN;
135 SELECT * FROM abc;
136 }
137 execsql {
138 SELECT * FROM sqlite_master;
139 BEGIN;
140 SELECT * FROM def;
141 } db2
142 db release_memory
143} [expr $::pgalloc * 2]
144do_test malloc5-3.2 {
145 concat \
146 [execsql {SELECT * FROM abc; COMMIT}] \
147 [execsql {SELECT * FROM def; COMMIT} db2]
148} {1 2 3 4 5 6 7 8 9 10 11 12}
149
150db2 close
151sqlite_malloc_outstanding -clearmaxbytes
152
153# The following two test cases each execute a transaction in which
154# 10000 rows are inserted into table abc. The first test case is used
155# to ensure that more than 1MB of dynamic memory is used to perform
156# the transaction.
157#
158# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
159# and tests to see that this limit is not exceeded at any point during
160# transaction execution.
161#
162do_test malloc5-4.1 {
163 execsql {BEGIN;}
164 execsql {DELETE FROM abc;}
165 for {set i 0} {$i < 10000} {incr i} {
166 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
167 }
168 execsql {COMMIT;}
169 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
170 expr $::nMaxBytes > 1000000
171} {1}
172do_test malloc5-4.2 {
173 db release_memory
174 sqlite_malloc_outstanding -clearmaxbytes
175 db soft_heap_limit 100000
176 execsql {BEGIN;}
177 for {set i 0} {$i < 10000} {incr i} {
178 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
179 }
180 execsql {COMMIT;}
181 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
182 expr $::nMaxBytes <= 100000
183} {1}
184do_test malloc5-4.3 {
185 # Check that the content of table abc is at least roughly as expected.
186 execsql {
187 SELECT count(*), sum(a), sum(b) FROM abc;
188 }
189} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]]
190
191# Unset the soft-heap-limit value.
192db soft_heap_limit -1
193
danielk19770190d1d2005-12-19 14:18:11 +0000194finish_test
195
196