1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/malloc5.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,390 @@
1.4 +# 2005 November 30
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +#
1.15 +# This file contains test cases focused on the two memory-management APIs,
1.16 +# sqlite3_soft_heap_limit() and sqlite3_release_memory().
1.17 +#
1.18 +# Prior to version 3.6.2, calling sqlite3_release_memory() or exceeding
1.19 +# the configured soft heap limit could cause sqlite to upgrade database
1.20 +# locks and flush dirty pages to the file system. As of 3.6.2, this is
1.21 +# no longer the case. In version 3.6.2, sqlite3_release_memory() only
1.22 +# reclaims clean pages. This test file has been updated accordingly.
1.23 +#
1.24 +# $Id: malloc5.test,v 1.20 2008/08/27 16:38:57 danielk1977 Exp $
1.25 +
1.26 +set testdir [file dirname $argv0]
1.27 +source $testdir/tester.tcl
1.28 +source $testdir/malloc_common.tcl
1.29 +db close
1.30 +
1.31 +# Only run these tests if memory debugging is turned on.
1.32 +#
1.33 +if {!$MEMDEBUG} {
1.34 + puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..."
1.35 + finish_test
1.36 + return
1.37 +}
1.38 +
1.39 +# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time.
1.40 +ifcapable !memorymanage {
1.41 + finish_test
1.42 + return
1.43 +}
1.44 +
1.45 +sqlite3_soft_heap_limit 0
1.46 +sqlite3 db test.db
1.47 +
1.48 +do_test malloc5-1.1 {
1.49 + # Simplest possible test. Call sqlite3_release_memory when there is exactly
1.50 + # one unused page in a single pager cache. The page cannot be freed, as
1.51 + # it is dirty. So sqlite3_release_memory() returns 0.
1.52 + #
1.53 + execsql {
1.54 + PRAGMA auto_vacuum=OFF;
1.55 + BEGIN;
1.56 + CREATE TABLE abc(a, b, c);
1.57 + }
1.58 + sqlite3_release_memory
1.59 +} {0}
1.60 +
1.61 +do_test malloc5-1.2 {
1.62 + # Test that the transaction started in the above test is still active.
1.63 + # The lock on the database file should not have been upgraded (this was
1.64 + # not the case before version 3.6.2).
1.65 + #
1.66 + sqlite3 db2 test.db
1.67 + execsql { SELECT * FROM sqlite_master } db2
1.68 +} {}
1.69 +do_test malloc5-1.3 {
1.70 + # Call [sqlite3_release_memory] when there is exactly one unused page
1.71 + # in the cache belonging to db2.
1.72 + #
1.73 + set ::pgalloc [sqlite3_release_memory]
1.74 + expr $::pgalloc > 0
1.75 +} {1}
1.76 +
1.77 +do_test malloc5-1.4 {
1.78 + # Commit the transaction and open a new one. Read 1 page into the cache.
1.79 + # Because the page is not dirty, it is eligible for collection even
1.80 + # before the transaction is concluded.
1.81 + #
1.82 + execsql {
1.83 + COMMIT;
1.84 + BEGIN;
1.85 + SELECT * FROM abc;
1.86 + }
1.87 + sqlite3_release_memory
1.88 +} $::pgalloc
1.89 +
1.90 +do_test malloc5-1.5 {
1.91 + # Conclude the transaction opened in the previous [do_test] block. This
1.92 + # causes another page (page 1) to become eligible for recycling.
1.93 + #
1.94 + execsql { COMMIT }
1.95 + sqlite3_release_memory
1.96 +} $::pgalloc
1.97 +
1.98 +do_test malloc5-1.6 {
1.99 + # Manipulate the cache so that it contains two unused pages. One requires
1.100 + # a journal-sync to free, the other does not.
1.101 + db2 close
1.102 + execsql {
1.103 + BEGIN;
1.104 + SELECT * FROM abc;
1.105 + CREATE TABLE def(d, e, f);
1.106 + }
1.107 + sqlite3_release_memory 500
1.108 +} $::pgalloc
1.109 +
1.110 +do_test malloc5-1.7 {
1.111 + # Database should not be locked this time.
1.112 + sqlite3 db2 test.db
1.113 + catchsql { SELECT * FROM abc } db2
1.114 +} {0 {}}
1.115 +do_test malloc5-1.8 {
1.116 + # Try to release another block of memory. This will fail as the only
1.117 + # pages currently in the cache are dirty (page 3) or pinned (page 1).
1.118 + db2 close
1.119 + sqlite3_release_memory 500
1.120 +} 0
1.121 +do_test malloc5-1.8 {
1.122 + # Database is still not locked.
1.123 + #
1.124 + sqlite3 db2 test.db
1.125 + catchsql { SELECT * FROM abc } db2
1.126 +} {0 {}}
1.127 +do_test malloc5-1.9 {
1.128 + execsql {
1.129 + COMMIT;
1.130 + }
1.131 +} {}
1.132 +
1.133 +do_test malloc5-2.1 {
1.134 + # Put some data in tables abc and def. Both tables are still wholly
1.135 + # contained within their root pages.
1.136 + execsql {
1.137 + INSERT INTO abc VALUES(1, 2, 3);
1.138 + INSERT INTO abc VALUES(4, 5, 6);
1.139 + INSERT INTO def VALUES(7, 8, 9);
1.140 + INSERT INTO def VALUES(10,11,12);
1.141 + }
1.142 +} {}
1.143 +do_test malloc5-2.2 {
1.144 + # Load the root-page for table def into the cache. Then query table abc.
1.145 + # Halfway through the query call sqlite3_release_memory(). The goal of this
1.146 + # test is to make sure we don't free pages that are in use (specifically,
1.147 + # the root of table abc).
1.148 + sqlite3_release_memory
1.149 + set nRelease 0
1.150 + execsql {
1.151 + BEGIN;
1.152 + SELECT * FROM def;
1.153 + }
1.154 + set data [list]
1.155 + db eval {SELECT * FROM abc} {
1.156 + incr nRelease [sqlite3_release_memory]
1.157 + lappend data $a $b $c
1.158 + }
1.159 + execsql {
1.160 + COMMIT;
1.161 + }
1.162 + list $nRelease $data
1.163 +} [list $pgalloc [list 1 2 3 4 5 6]]
1.164 +
1.165 +do_test malloc5-3.1 {
1.166 + # Simple test to show that if two pagers are opened from within this
1.167 + # thread, memory is freed from both when sqlite3_release_memory() is
1.168 + # called.
1.169 + execsql {
1.170 + BEGIN;
1.171 + SELECT * FROM abc;
1.172 + }
1.173 + execsql {
1.174 + SELECT * FROM sqlite_master;
1.175 + BEGIN;
1.176 + SELECT * FROM def;
1.177 + } db2
1.178 + sqlite3_release_memory
1.179 +} [expr $::pgalloc * 2]
1.180 +do_test malloc5-3.2 {
1.181 + concat \
1.182 + [execsql {SELECT * FROM abc; COMMIT}] \
1.183 + [execsql {SELECT * FROM def; COMMIT} db2]
1.184 +} {1 2 3 4 5 6 7 8 9 10 11 12}
1.185 +
1.186 +db2 close
1.187 +puts "Highwater mark: [sqlite3_memory_highwater]"
1.188 +
1.189 +# The following two test cases each execute a transaction in which
1.190 +# 10000 rows are inserted into table abc. The first test case is used
1.191 +# to ensure that more than 1MB of dynamic memory is used to perform
1.192 +# the transaction.
1.193 +#
1.194 +# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB)
1.195 +# and tests to see that this limit is not exceeded at any point during
1.196 +# transaction execution.
1.197 +#
1.198 +# Before executing malloc5-4.* we save the value of the current soft heap
1.199 +# limit in variable ::soft_limit. The original value is restored after
1.200 +# running the tests.
1.201 +#
1.202 +set ::soft_limit [sqlite3_soft_heap_limit -1]
1.203 +execsql {PRAGMA cache_size=2000}
1.204 +do_test malloc5-4.1 {
1.205 + execsql {BEGIN;}
1.206 + execsql {DELETE FROM abc;}
1.207 + for {set i 0} {$i < 10000} {incr i} {
1.208 + execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
1.209 + }
1.210 + execsql {COMMIT;}
1.211 + sqlite3_release_memory
1.212 + sqlite3_memory_highwater 1
1.213 + execsql {SELECT * FROM abc}
1.214 + set nMaxBytes [sqlite3_memory_highwater 1]
1.215 + puts -nonewline " (Highwater mark: $nMaxBytes) "
1.216 + expr $nMaxBytes > 1000000
1.217 +} {1}
1.218 +do_test malloc5-4.2 {
1.219 + sqlite3_release_memory
1.220 + sqlite3_soft_heap_limit 100000
1.221 + sqlite3_memory_highwater 1
1.222 + execsql {SELECT * FROM abc}
1.223 + set nMaxBytes [sqlite3_memory_highwater 1]
1.224 + puts -nonewline " (Highwater mark: $nMaxBytes) "
1.225 + expr $nMaxBytes <= 100000
1.226 +} {1}
1.227 +do_test malloc5-4.3 {
1.228 + # Check that the content of table abc is at least roughly as expected.
1.229 + execsql {
1.230 + SELECT count(*), sum(a), sum(b) FROM abc;
1.231 + }
1.232 +} [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]]
1.233 +
1.234 +# Restore the soft heap limit.
1.235 +sqlite3_soft_heap_limit $::soft_limit
1.236 +
1.237 +# Test that there are no problems calling sqlite3_release_memory when
1.238 +# there are open in-memory databases.
1.239 +#
1.240 +# At one point these tests would cause a seg-fault.
1.241 +#
1.242 +do_test malloc5-5.1 {
1.243 + db close
1.244 + sqlite3 db :memory:
1.245 + execsql {
1.246 + BEGIN;
1.247 + CREATE TABLE abc(a, b, c);
1.248 + INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL);
1.249 + INSERT INTO abc SELECT * FROM abc;
1.250 + INSERT INTO abc SELECT * FROM abc;
1.251 + INSERT INTO abc SELECT * FROM abc;
1.252 + INSERT INTO abc SELECT * FROM abc;
1.253 + INSERT INTO abc SELECT * FROM abc;
1.254 + INSERT INTO abc SELECT * FROM abc;
1.255 + INSERT INTO abc SELECT * FROM abc;
1.256 + }
1.257 + sqlite3_release_memory
1.258 +} 0
1.259 +do_test malloc5-5.2 {
1.260 + sqlite3_soft_heap_limit 5000
1.261 + execsql {
1.262 + COMMIT;
1.263 + PRAGMA temp_store = memory;
1.264 + SELECT * FROM abc ORDER BY a;
1.265 + }
1.266 + expr 1
1.267 +} {1}
1.268 +sqlite3_soft_heap_limit $::soft_limit
1.269 +
1.270 +#-------------------------------------------------------------------------
1.271 +# The following test cases (malloc5-6.*) test the new global LRU list
1.272 +# used to determine the pages to recycle when sqlite3_release_memory is
1.273 +# called and there is more than one pager open.
1.274 +#
1.275 +proc nPage {db} {
1.276 + set bt [btree_from_db $db]
1.277 + array set stats [btree_pager_stats $bt]
1.278 + set stats(page)
1.279 +}
1.280 +db close
1.281 +file delete -force test.db test.db-journal test2.db test2.db-journal
1.282 +
1.283 +# This block of test-cases (malloc5-6.1.*) prepares two database files
1.284 +# for the subsequent tests.
1.285 +do_test malloc5-6.1.1 {
1.286 + sqlite3 db test.db
1.287 + execsql {
1.288 + PRAGMA page_size=1024;
1.289 + PRAGMA default_cache_size=10;
1.290 + }
1.291 + execsql {
1.292 + PRAGMA temp_store = memory;
1.293 + BEGIN;
1.294 + CREATE TABLE abc(a PRIMARY KEY, b, c);
1.295 + INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100));
1.296 + INSERT INTO abc
1.297 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.298 + INSERT INTO abc
1.299 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.300 + INSERT INTO abc
1.301 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.302 + INSERT INTO abc
1.303 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.304 + INSERT INTO abc
1.305 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.306 + INSERT INTO abc
1.307 + SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc;
1.308 + COMMIT;
1.309 + }
1.310 + copy_file test.db test2.db
1.311 + sqlite3 db2 test2.db
1.312 + list \
1.313 + [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20]
1.314 +} {1 1}
1.315 +do_test malloc5-6.1.2 {
1.316 + list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2]
1.317 +} {10 10}
1.318 +
1.319 +do_test malloc5-6.2.1 {
1.320 +breakpoint
1.321 + execsql {SELECT * FROM abc} db2
1.322 + execsql {SELECT * FROM abc} db
1.323 + expr [nPage db] + [nPage db2]
1.324 +} {20}
1.325 +
1.326 +do_test malloc5-6.2.2 {
1.327 + # If we now try to reclaim some memory, it should come from the db2 cache.
1.328 + sqlite3_release_memory 3000
1.329 + expr [nPage db] + [nPage db2]
1.330 +} {17}
1.331 +do_test malloc5-6.2.3 {
1.332 + # Access the db2 cache again, so that all the db2 pages have been used
1.333 + # more recently than all the db pages. Then try to reclaim 3000 bytes.
1.334 + # This time, 3 pages should be pulled from the db cache.
1.335 + execsql { SELECT * FROM abc } db2
1.336 + sqlite3_release_memory 3000
1.337 + expr [nPage db] + [nPage db2]
1.338 +} {17}
1.339 +
1.340 +do_test malloc5-6.3.1 {
1.341 + # Now open a transaction and update 2 pages in the db2 cache. Then
1.342 + # do a SELECT on the db cache so that all the db pages are more recently
1.343 + # used than the db2 pages. When we try to free memory, SQLite should
1.344 + # free the non-dirty db2 pages, then the db pages, then finally use
1.345 + # sync() to free up the dirty db2 pages. The only page that cannot be
1.346 + # freed is page1 of db2. Because there is an open transaction, the
1.347 + # btree layer holds a reference to page 1 in the db2 cache.
1.348 + execsql {
1.349 + BEGIN;
1.350 + UPDATE abc SET c = randstr(100,100)
1.351 + WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc);
1.352 + } db2
1.353 + execsql { SELECT * FROM abc } db
1.354 + expr [nPage db] + [nPage db2]
1.355 +} {20}
1.356 +do_test malloc5-6.3.2 {
1.357 + # Try to release 7700 bytes. This should release all the
1.358 + # non-dirty pages held by db2.
1.359 + sqlite3_release_memory [expr 7*1100]
1.360 + list [nPage db] [nPage db2]
1.361 +} {10 3}
1.362 +do_test malloc5-6.3.3 {
1.363 + # Try to release another 1000 bytes. This should come fromt the db
1.364 + # cache, since all three pages held by db2 are either in-use or diry.
1.365 + sqlite3_release_memory 1000
1.366 + list [nPage db] [nPage db2]
1.367 +} {9 3}
1.368 +do_test malloc5-6.3.4 {
1.369 + # Now release 9900 more (about 9 pages worth). This should expunge
1.370 + # the rest of the db cache. But the db2 cache remains intact, because
1.371 + # SQLite tries to avoid calling sync().
1.372 + sqlite3_release_memory 9900
1.373 + list [nPage db] [nPage db2]
1.374 +} {0 3}
1.375 +do_test malloc5-6.3.5 {
1.376 + # But if we are really insistent, SQLite will consent to call sync()
1.377 + # if there is no other option. UPDATE: As of 3.6.2, SQLite will not
1.378 + # call sync() in this scenario. So no further memory can be reclaimed.
1.379 + sqlite3_release_memory 1000
1.380 + list [nPage db] [nPage db2]
1.381 +} {0 3}
1.382 +do_test malloc5-6.3.6 {
1.383 + # The referenced page (page 1 of the db2 cache) will not be freed no
1.384 + # matter how much memory we ask for:
1.385 + sqlite3_release_memory 31459
1.386 + list [nPage db] [nPage db2]
1.387 +} {0 3}
1.388 +
1.389 +db2 close
1.390 +
1.391 +sqlite3_soft_heap_limit $::soft_limit
1.392 +finish_test
1.393 +catch {db close}