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.15 2007/09/03 11:04:22 danielk1977 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.2 { 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} 260sqlite3_soft_heap_limit $::soft_limit 261 262#------------------------------------------------------------------------- 263# The following test cases (malloc5-6.*) test the new global LRU list 264# used to determine the pages to recycle when sqlite3_release_memory is 265# called and there is more than one pager open. 266# 267proc nPage {db} { 268 set bt [btree_from_db $db] 269 array set stats [btree_pager_stats $bt] 270 set stats(page) 271} 272db close 273file delete -force test.db test.db-journal test2.db test2.db-journal 274 275# This block of test-cases (malloc5-6.1.*) prepares two database files 276# for the subsequent tests. 277do_test malloc5-6.1.1 { 278 sqlite3 db test.db 279 execsql { 280 PRAGMA page_size=1024; 281 PRAGMA default_cache_size=10; 282 BEGIN; 283 CREATE TABLE abc(a PRIMARY KEY, b, c); 284 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100)); 285 INSERT INTO abc 286 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 287 INSERT INTO abc 288 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 289 INSERT INTO abc 290 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 291 INSERT INTO abc 292 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 293 INSERT INTO abc 294 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 295 INSERT INTO abc 296 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 297 COMMIT; 298 } 299 copy_file test.db test2.db 300 sqlite3 db2 test2.db 301 list \ 302 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20] 303} {1 1} 304do_test malloc5-6.1.2 { 305 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2] 306} {10 10} 307 308do_test malloc5-6.2.1 { 309 execsql { SELECT * FROM abc } db2 310 execsql {SELECT * FROM abc} db 311 list [nPage db] [nPage db2] 312} {10 10} 313do_test malloc5-6.2.2 { 314 # If we now try to reclaim some memory, it should come from the db2 cache. 315 sqlite3_release_memory 3000 316 list [nPage db] [nPage db2] 317} {10 7} 318do_test malloc5-6.2.3 { 319 # Access the db2 cache again, so that all the db2 pages have been used 320 # more recently than all the db pages. Then try to reclaim 3000 bytes. 321 # This time, 3 pages should be pulled from the db cache. 322 execsql { SELECT * FROM abc } db2 323 sqlite3_release_memory 3000 324 list [nPage db] [nPage db2] 325} {7 10} 326 327 328do_test malloc5-6.3.1 { 329 # Now open a transaction and update 2 pages in the db2 cache. Then 330 # do a SELECT on the db cache so that all the db pages are more recently 331 # used than the db2 pages. When we try to free memory, SQLite should 332 # free the non-dirty db2 pages, then the db pages, then finally use 333 # sync() to free up the dirty db2 pages. The only page that cannot be 334 # freed is page1 of db2. Because there is an open transaction, the 335 # btree layer holds a reference to page 1 in the db2 cache. 336 execsql { 337 BEGIN; 338 UPDATE abc SET c = randstr(100,100) 339 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc); 340 } db2 341 execsql { SELECT * FROM abc } db 342 list [nPage db] [nPage db2] 343} {10 10} 344do_test malloc5-6.3.2 { 345 # Try to release 7700 bytes. This should release all the 346 # non-dirty pages held by db2. 347 sqlite3_release_memory [expr 7*1100] 348 list [nPage db] [nPage db2] 349} {10 3} 350do_test malloc5-6.3.3 { 351 # Try to release another 1000 bytes. This should come fromt the db 352 # cache, since all three pages held by db2 are either in-use or diry. 353 sqlite3_release_memory 1000 354 list [nPage db] [nPage db2] 355} {9 3} 356do_test malloc5-6.3.4 { 357 # Now release 9900 more (about 9 pages worth). This should expunge 358 # the rest of the db cache. But the db2 cache remains intact, because 359 # SQLite tries to avoid calling sync(). 360 sqlite3_release_memory 9900 361 list [nPage db] [nPage db2] 362} {0 3} 363do_test malloc5-6.3.5 { 364 # But if we are really insistent, SQLite will consent to call sync() 365 # if there is no other option. 366 sqlite3_release_memory 1000 367 list [nPage db] [nPage db2] 368} {0 2} 369do_test malloc5-6.3.6 { 370 # The referenced page (page 1 of the db2 cache) will not be freed no 371 # matter how much memory we ask for: 372 sqlite3_release_memory 31459 373 list [nPage db] [nPage db2] 374} {0 1} 375 376db2 close 377 378sqlite3_soft_heap_limit $::soft_limit 379finish_test 380catch {db close} 381