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.18 2008/02/18 22:24:58 drh Exp $ 16 17#--------------------------------------------------------------------------- 18# NOTES ON EXPECTED BEHAVIOUR 19# 20#--------------------------------------------------------------------------- 21 22 23set testdir [file dirname $argv0] 24source $testdir/tester.tcl 25source $testdir/malloc_common.tcl 26db close 27 28# Only run these tests if memory debugging is turned on. 29# 30if {!$MEMDEBUG} { 31 puts "Skipping malloc5 tests: not compiled with -DSQLITE_MEMDEBUG..." 32 finish_test 33 return 34} 35 36# Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time. 37ifcapable !memorymanage { 38 finish_test 39 return 40} 41 42sqlite3_soft_heap_limit 0 43sqlite3 db test.db 44 45do_test malloc5-1.1 { 46 # Simplest possible test. Call sqlite3_release_memory when there is exactly 47 # one unused page in a single pager cache. This test case set's the 48 # value of the ::pgalloc variable, which is used in subsequent tests. 49 # 50 # Note: Even though executing this statement on an empty database 51 # modifies 2 pages (the root of sqlite_master and the new root page), 52 # the sqlite_master root (page 1) is never freed because the btree layer 53 # retains a reference to it for the entire transaction. 54 execsql { 55 PRAGMA auto_vacuum=OFF; 56 BEGIN; 57 CREATE TABLE abc(a, b, c); 58 } 59 set ::pgalloc [sqlite3_release_memory] 60 expr $::pgalloc > 0 61} {1} 62do_test malloc5-1.2 { 63 # Test that the transaction started in the above test is still active. 64 # Because the page freed had been written to, freeing it required a 65 # journal sync and exclusive lock on the database file. Test the file 66 # appears to be locked. 67 sqlite3 db2 test.db 68 catchsql { 69 SELECT * FROM abc; 70 } db2 71} {1 {database is locked}} 72do_test malloc5-1.3 { 73 # Again call [sqlite3_release_memory] when there is exactly one unused page 74 # in the cache. The same amount of memory is required, but no journal-sync 75 # or exclusive lock should be established. 76 execsql { 77 COMMIT; 78 BEGIN; 79 SELECT * FROM abc; 80 } 81 sqlite3_release_memory 82} $::pgalloc 83do_test malloc5-1.4 { 84 # Database should not be locked this time. 85 catchsql { 86 SELECT * FROM abc; 87 } db2 88} {0 {}} 89do_test malloc5-1.5 { 90 # Manipulate the cache so that it contains two unused pages. One requires 91 # a journal-sync to free, the other does not. 92 db2 close 93 execsql { 94 SELECT * FROM abc; 95 CREATE TABLE def(d, e, f); 96 } 97 sqlite3_release_memory 500 98} $::pgalloc 99do_test malloc5-1.6 { 100 # Database should not be locked this time. The above test case only 101 # requested 500 bytes of memory, which can be obtained by freeing the page 102 # that does not require an fsync(). 103 sqlite3 db2 test.db 104 catchsql { 105 SELECT * FROM abc; 106 } db2 107} {0 {}} 108do_test malloc5-1.7 { 109 # Release another 500 bytes of memory. This time we require a sync(), 110 # so the database file will be locked afterwards. 111 db2 close 112 sqlite3_release_memory 500 113} $::pgalloc 114do_test malloc5-1.8 { 115 sqlite3 db2 test.db 116 catchsql { 117 SELECT * FROM abc; 118 } db2 119} {1 {database is locked}} 120do_test malloc5-1.9 { 121 execsql { 122 COMMIT; 123 } 124} {} 125 126do_test malloc5-2.1 { 127 # Put some data in tables abc and def. Both tables are still wholly 128 # contained within their root pages. 129 execsql { 130 INSERT INTO abc VALUES(1, 2, 3); 131 INSERT INTO abc VALUES(4, 5, 6); 132 INSERT INTO def VALUES(7, 8, 9); 133 INSERT INTO def VALUES(10,11,12); 134 } 135} {} 136do_test malloc5-2.2 { 137 # Load the root-page for table def into the cache. Then query table abc. 138 # Halfway through the query call sqlite3_release_memory(). The goal of this 139 # test is to make sure we don't free pages that are in use (specifically, 140 # the root of table abc). 141 set nRelease 0 142 execsql { 143 BEGIN; 144 SELECT * FROM def; 145 } 146 set data [list] 147 db eval {SELECT * FROM abc} { 148 incr nRelease [sqlite3_release_memory] 149 lappend data $a $b $c 150 } 151 execsql { 152 COMMIT; 153 } 154 list $nRelease $data 155} [list $pgalloc [list 1 2 3 4 5 6]] 156 157do_test malloc5-3.1 { 158 # Simple test to show that if two pagers are opened from within this 159 # thread, memory is freed from both when sqlite3_release_memory() is 160 # called. 161 execsql { 162 BEGIN; 163 SELECT * FROM abc; 164 } 165 execsql { 166 SELECT * FROM sqlite_master; 167 BEGIN; 168 SELECT * FROM def; 169 } db2 170 sqlite3_release_memory 171} [expr $::pgalloc * 2] 172do_test malloc5-3.2 { 173 concat \ 174 [execsql {SELECT * FROM abc; COMMIT}] \ 175 [execsql {SELECT * FROM def; COMMIT} db2] 176} {1 2 3 4 5 6 7 8 9 10 11 12} 177 178db2 close 179puts "Highwater mark: [sqlite3_memory_highwater]" 180 181# The following two test cases each execute a transaction in which 182# 10000 rows are inserted into table abc. The first test case is used 183# to ensure that more than 1MB of dynamic memory is used to perform 184# the transaction. 185# 186# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB) 187# and tests to see that this limit is not exceeded at any point during 188# transaction execution. 189# 190# Before executing malloc5-4.* we save the value of the current soft heap 191# limit in variable ::soft_limit. The original value is restored after 192# running the tests. 193# 194set ::soft_limit [sqlite3_soft_heap_limit -1] 195execsql {PRAGMA cache_size=2000} 196do_test malloc5-4.1 { 197 execsql {BEGIN;} 198 execsql {DELETE FROM abc;} 199 for {set i 0} {$i < 10000} {incr i} { 200 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" 201 } 202 execsql {COMMIT;} 203 set nMaxBytes [sqlite3_memory_highwater 1] 204 puts -nonewline " (Highwater mark: $nMaxBytes) " 205 expr $nMaxBytes > 1000000 206} {1} 207do_test malloc5-4.2 { 208 sqlite3_release_memory 209 sqlite3_soft_heap_limit 100000 210 sqlite3_memory_highwater 1 211 execsql {BEGIN;} 212 for {set i 0} {$i < 10000} {incr i} { 213 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" 214 } 215 execsql {COMMIT;} 216 set nMaxBytes [sqlite3_memory_highwater 1] 217 puts -nonewline " (Highwater mark: $nMaxBytes) " 218 219 # We used to test ($nMaxBytes<100000), because the soft-heap-limit is 220 # 100000 bytes. But if an allocation that will exceed the 221 # soft-heap-limit is requested from within the only pager instance in 222 # the system, then there is no way to free memory and the limit has to 223 # be exceeded. An exception is memory allocated to store actual page 224 # data (the code contains a special case for this). 225 # 226 # This is not a problem because all allocations apart from those 227 # used to store cached page data are both small and transient. 228 # 229 # Summary: the actual high-water mark for memory usage may be slightly 230 # higher than the soft-heap-limit. The specific allocations that cause 231 # the problem are the calls to sqlite3_malloc() inserted into selected 232 # sqlite3OsXXX() functions in test builds. 233 # 234 expr $nMaxBytes <= 100100 235} {1} 236do_test malloc5-4.3 { 237 # Check that the content of table abc is at least roughly as expected. 238 execsql { 239 SELECT count(*), sum(a), sum(b) FROM abc; 240 } 241} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]] 242 243# Restore the soft heap limit. 244sqlite3_soft_heap_limit $::soft_limit 245 246# Test that there are no problems calling sqlite3_release_memory when 247# there are open in-memory databases. 248# 249# At one point these tests would cause a seg-fault. 250# 251do_test malloc5-5.1 { 252 db close 253 sqlite3 db :memory: 254 execsql { 255 BEGIN; 256 CREATE TABLE abc(a, b, c); 257 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL); 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 INSERT INTO abc SELECT * FROM abc; 265 } 266 sqlite3_release_memory 267} 0 268do_test malloc5-5.2 { 269 sqlite3_soft_heap_limit 5000 270 execsql { 271 COMMIT; 272 PRAGMA temp_store = memory; 273 SELECT * FROM abc ORDER BY a; 274 } 275 expr 1 276} {1} 277sqlite3_soft_heap_limit $::soft_limit 278 279#------------------------------------------------------------------------- 280# The following test cases (malloc5-6.*) test the new global LRU list 281# used to determine the pages to recycle when sqlite3_release_memory is 282# called and there is more than one pager open. 283# 284proc nPage {db} { 285 set bt [btree_from_db $db] 286 array set stats [btree_pager_stats $bt] 287 set stats(page) 288} 289db close 290file delete -force test.db test.db-journal test2.db test2.db-journal 291 292# This block of test-cases (malloc5-6.1.*) prepares two database files 293# for the subsequent tests. 294do_test malloc5-6.1.1 { 295 sqlite3 db test.db 296 execsql { 297 PRAGMA page_size=1024; 298 PRAGMA default_cache_size=10; 299 BEGIN; 300 CREATE TABLE abc(a PRIMARY KEY, b, c); 301 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100)); 302 INSERT INTO abc 303 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 304 INSERT INTO abc 305 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 306 INSERT INTO abc 307 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 308 INSERT INTO abc 309 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 310 INSERT INTO abc 311 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 312 INSERT INTO abc 313 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 314 COMMIT; 315 } 316 copy_file test.db test2.db 317 sqlite3 db2 test2.db 318 list \ 319 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20] 320} {1 1} 321do_test malloc5-6.1.2 { 322 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2] 323} {10 10} 324 325do_test malloc5-6.2.1 { 326 execsql { SELECT * FROM abc } db2 327 execsql {SELECT * FROM abc} db 328 list [nPage db] [nPage db2] 329} {10 10} 330do_test malloc5-6.2.2 { 331 # If we now try to reclaim some memory, it should come from the db2 cache. 332 sqlite3_release_memory 3000 333 list [nPage db] [nPage db2] 334} {10 7} 335do_test malloc5-6.2.3 { 336 # Access the db2 cache again, so that all the db2 pages have been used 337 # more recently than all the db pages. Then try to reclaim 3000 bytes. 338 # This time, 3 pages should be pulled from the db cache. 339 execsql { SELECT * FROM abc } db2 340 sqlite3_release_memory 3000 341 list [nPage db] [nPage db2] 342} {7 10} 343 344 345do_test malloc5-6.3.1 { 346 # Now open a transaction and update 2 pages in the db2 cache. Then 347 # do a SELECT on the db cache so that all the db pages are more recently 348 # used than the db2 pages. When we try to free memory, SQLite should 349 # free the non-dirty db2 pages, then the db pages, then finally use 350 # sync() to free up the dirty db2 pages. The only page that cannot be 351 # freed is page1 of db2. Because there is an open transaction, the 352 # btree layer holds a reference to page 1 in the db2 cache. 353 execsql { 354 BEGIN; 355 UPDATE abc SET c = randstr(100,100) 356 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc); 357 } db2 358 execsql { SELECT * FROM abc } db 359 list [nPage db] [nPage db2] 360} {10 10} 361do_test malloc5-6.3.2 { 362 # Try to release 7700 bytes. This should release all the 363 # non-dirty pages held by db2. 364 sqlite3_release_memory [expr 7*1100] 365 list [nPage db] [nPage db2] 366} {10 3} 367do_test malloc5-6.3.3 { 368 # Try to release another 1000 bytes. This should come fromt the db 369 # cache, since all three pages held by db2 are either in-use or diry. 370 sqlite3_release_memory 1000 371 list [nPage db] [nPage db2] 372} {9 3} 373do_test malloc5-6.3.4 { 374 # Now release 9900 more (about 9 pages worth). This should expunge 375 # the rest of the db cache. But the db2 cache remains intact, because 376 # SQLite tries to avoid calling sync(). 377 sqlite3_release_memory 9900 378 list [nPage db] [nPage db2] 379} {0 3} 380do_test malloc5-6.3.5 { 381 # But if we are really insistent, SQLite will consent to call sync() 382 # if there is no other option. 383 sqlite3_release_memory 1000 384 list [nPage db] [nPage db2] 385} {0 2} 386do_test malloc5-6.3.6 { 387 # The referenced page (page 1 of the db2 cache) will not be freed no 388 # matter how much memory we ask for: 389 sqlite3_release_memory 31459 390 list [nPage db] [nPage db2] 391} {0 1} 392 393db2 close 394 395sqlite3_soft_heap_limit $::soft_limit 396finish_test 397catch {db close} 398