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# Prior to version 3.6.2, calling sqlite3_release_memory() or exceeding 16# the configured soft heap limit could cause sqlite to upgrade database 17# locks and flush dirty pages to the file system. As of 3.6.2, this is 18# no longer the case. In version 3.6.2, sqlite3_release_memory() only 19# reclaims clean pages. This test file has been updated accordingly. 20# 21# $Id: malloc5.test,v 1.22 2009/04/11 19:09:54 drh Exp $ 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 44db eval {PRAGMA cache_size=1} 45 46do_test malloc5-1.1 { 47 # Simplest possible test. Call sqlite3_release_memory when there is exactly 48 # one unused page in a single pager cache. The page cannot be freed, as 49 # it is dirty. So sqlite3_release_memory() returns 0. 50 # 51 execsql { 52 PRAGMA auto_vacuum=OFF; 53 BEGIN; 54 CREATE TABLE abc(a, b, c); 55 } 56 sqlite3_release_memory 57} {0} 58 59do_test malloc5-1.2 { 60 # Test that the transaction started in the above test is still active. 61 # The lock on the database file should not have been upgraded (this was 62 # not the case before version 3.6.2). 63 # 64 sqlite3 db2 test.db 65 execsql {PRAGMA cache_size=2; SELECT * FROM sqlite_master } db2 66} {} 67do_test malloc5-1.3 { 68 # Call [sqlite3_release_memory] when there is exactly one unused page 69 # in the cache belonging to db2. 70 # 71 set ::pgalloc [sqlite3_release_memory] 72} {0} 73 74# The sizes of memory allocations from system malloc() might vary, 75# depending on the memory allocator algorithms used. The following 76# routine is designed to support answers that fall within a range 77# of values while also supplying easy-to-understand "expected" values 78# when errors occur. 79# 80proc value_in_range {target x args} { 81 set v [lindex $args 0] 82 if {$v!=""} { 83 if {$v<$target*$x} {return $v} 84 if {$v>$target/$x} {return $v} 85 } 86 return "number between [expr {int($target*$x)}] and [expr {int($target/$x)}]" 87} 88set mrange 0.98 ;# plus or minus 2% 89 90 91do_test malloc5-1.4 { 92 # Commit the transaction and open a new one. Read 1 page into the cache. 93 # Because the page is not dirty, it is eligible for collection even 94 # before the transaction is concluded. 95 # 96 execsql { 97 COMMIT; 98 BEGIN; 99 SELECT * FROM abc; 100 } 101 value_in_range $::pgalloc $::mrange [sqlite3_release_memory] 102} [value_in_range $::pgalloc $::mrange] 103 104do_test malloc5-1.5 { 105 # Conclude the transaction opened in the previous [do_test] block. This 106 # causes another page (page 1) to become eligible for recycling. 107 # 108 execsql { COMMIT } 109 value_in_range $::pgalloc $::mrange [sqlite3_release_memory] 110} [value_in_range $::pgalloc $::mrange] 111 112do_test malloc5-1.6 { 113 # Manipulate the cache so that it contains two unused pages. One requires 114 # a journal-sync to free, the other does not. 115 db2 close 116 execsql { 117 BEGIN; 118 SELECT * FROM abc; 119 CREATE TABLE def(d, e, f); 120 } 121 value_in_range $::pgalloc $::mrange [sqlite3_release_memory 500] 122} [value_in_range $::pgalloc $::mrange] 123 124do_test malloc5-1.7 { 125 # Database should not be locked this time. 126 sqlite3 db2 test.db 127 catchsql { SELECT * FROM abc } db2 128} {0 {}} 129do_test malloc5-1.8 { 130 # Try to release another block of memory. This will fail as the only 131 # pages currently in the cache are dirty (page 3) or pinned (page 1). 132 db2 close 133 sqlite3_release_memory 500 134} 0 135do_test malloc5-1.8 { 136 # Database is still not locked. 137 # 138 sqlite3 db2 test.db 139 catchsql { SELECT * FROM abc } db2 140} {0 {}} 141do_test malloc5-1.9 { 142 execsql { 143 COMMIT; 144 } 145} {} 146 147do_test malloc5-2.1 { 148 # Put some data in tables abc and def. Both tables are still wholly 149 # contained within their root pages. 150 execsql { 151 INSERT INTO abc VALUES(1, 2, 3); 152 INSERT INTO abc VALUES(4, 5, 6); 153 INSERT INTO def VALUES(7, 8, 9); 154 INSERT INTO def VALUES(10,11,12); 155 } 156} {} 157do_test malloc5-2.2 { 158 # Load the root-page for table def into the cache. Then query table abc. 159 # Halfway through the query call sqlite3_release_memory(). The goal of this 160 # test is to make sure we don't free pages that are in use (specifically, 161 # the root of table abc). 162 sqlite3_release_memory 163 set nRelease 0 164 execsql { 165 BEGIN; 166 SELECT * FROM def; 167 } 168 set data [list] 169 db eval {SELECT * FROM abc} { 170 incr nRelease [sqlite3_release_memory] 171 lappend data $a $b $c 172 } 173 execsql { 174 COMMIT; 175 } 176 list $nRelease $data 177} [list $pgalloc [list 1 2 3 4 5 6]] 178 179do_test malloc5-3.1 { 180 # Simple test to show that if two pagers are opened from within this 181 # thread, memory is freed from both when sqlite3_release_memory() is 182 # called. 183 execsql { 184 BEGIN; 185 SELECT * FROM abc; 186 } 187 execsql { 188 SELECT * FROM sqlite_master; 189 BEGIN; 190 SELECT * FROM def; 191 } db2 192 value_in_range [expr $::pgalloc*2] 0.99 [sqlite3_release_memory] 193} [value_in_range [expr $::pgalloc * 2] 0.99] 194do_test malloc5-3.2 { 195 concat \ 196 [execsql {SELECT * FROM abc; COMMIT}] \ 197 [execsql {SELECT * FROM def; COMMIT} db2] 198} {1 2 3 4 5 6 7 8 9 10 11 12} 199 200db2 close 201puts "Highwater mark: [sqlite3_memory_highwater]" 202 203# The following two test cases each execute a transaction in which 204# 10000 rows are inserted into table abc. The first test case is used 205# to ensure that more than 1MB of dynamic memory is used to perform 206# the transaction. 207# 208# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB) 209# and tests to see that this limit is not exceeded at any point during 210# transaction execution. 211# 212# Before executing malloc5-4.* we save the value of the current soft heap 213# limit in variable ::soft_limit. The original value is restored after 214# running the tests. 215# 216set ::soft_limit [sqlite3_soft_heap_limit -1] 217execsql {PRAGMA cache_size=2000} 218do_test malloc5-4.1 { 219 execsql {BEGIN;} 220 execsql {DELETE FROM abc;} 221 for {set i 0} {$i < 10000} {incr i} { 222 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" 223 } 224 execsql {COMMIT;} 225 db cache flush 226 sqlite3_release_memory 227 sqlite3_memory_highwater 1 228 execsql {SELECT * FROM abc} 229 set nMaxBytes [sqlite3_memory_highwater 1] 230 puts -nonewline " (Highwater mark: $nMaxBytes) " 231 expr $nMaxBytes > 1000000 232} {1} 233do_test malloc5-4.2 { 234 db eval {PRAGMA cache_size=1} 235 db cache flush 236 sqlite3_release_memory 237 sqlite3_soft_heap_limit 100000 238 sqlite3_memory_highwater 1 239 execsql {SELECT * FROM abc} 240 set nMaxBytes [sqlite3_memory_highwater 1] 241 puts -nonewline " (Highwater mark: $nMaxBytes) " 242 expr $nMaxBytes <= 110000 243} {1} 244do_test malloc5-4.3 { 245 # Check that the content of table abc is at least roughly as expected. 246 execsql { 247 SELECT count(*), sum(a), sum(b) FROM abc; 248 } 249} [list 10000 [expr int(10000.0 * 4999.5)] [expr int(10000.0 * 4999.5)]] 250 251# Restore the soft heap limit. 252sqlite3_soft_heap_limit $::soft_limit 253 254# Test that there are no problems calling sqlite3_release_memory when 255# there are open in-memory databases. 256# 257# At one point these tests would cause a seg-fault. 258# 259do_test malloc5-5.1 { 260 db close 261 sqlite3 db :memory: 262 execsql { 263 BEGIN; 264 CREATE TABLE abc(a, b, c); 265 INSERT INTO abc VALUES('abcdefghi', 1234567890, NULL); 266 INSERT INTO abc SELECT * FROM abc; 267 INSERT INTO abc SELECT * FROM abc; 268 INSERT INTO abc SELECT * FROM abc; 269 INSERT INTO abc SELECT * FROM abc; 270 INSERT INTO abc SELECT * FROM abc; 271 INSERT INTO abc SELECT * FROM abc; 272 INSERT INTO abc SELECT * FROM abc; 273 } 274 sqlite3_release_memory 275} 0 276do_test malloc5-5.2 { 277 sqlite3_soft_heap_limit 5000 278 execsql { 279 COMMIT; 280 PRAGMA temp_store = memory; 281 SELECT * FROM abc ORDER BY a; 282 } 283 expr 1 284} {1} 285sqlite3_soft_heap_limit $::soft_limit 286 287#------------------------------------------------------------------------- 288# The following test cases (malloc5-6.*) test the new global LRU list 289# used to determine the pages to recycle when sqlite3_release_memory is 290# called and there is more than one pager open. 291# 292proc nPage {db} { 293 set bt [btree_from_db $db] 294 array set stats [btree_pager_stats $bt] 295 set stats(page) 296} 297db close 298forcedelete test.db test.db-journal test2.db test2.db-journal 299 300# This block of test-cases (malloc5-6.1.*) prepares two database files 301# for the subsequent tests. 302do_test malloc5-6.1.1 { 303 sqlite3 db test.db 304 execsql { 305 PRAGMA page_size=1024; 306 PRAGMA default_cache_size=2; 307 } 308 execsql { 309 PRAGMA temp_store = memory; 310 BEGIN; 311 CREATE TABLE abc(a PRIMARY KEY, b, c); 312 INSERT INTO abc VALUES(randstr(50,50), randstr(75,75), randstr(100,100)); 313 INSERT INTO abc 314 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 315 INSERT INTO abc 316 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 317 INSERT INTO abc 318 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 319 INSERT INTO abc 320 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 321 INSERT INTO abc 322 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 323 INSERT INTO abc 324 SELECT randstr(50,50), randstr(75,75), randstr(100,100) FROM abc; 325 COMMIT; 326 } 327 forcecopy test.db test2.db 328 sqlite3 db2 test2.db 329 db2 eval {PRAGMA cache_size=2} 330 list \ 331 [expr ([file size test.db]/1024)>20] [expr ([file size test2.db]/1024)>20] 332} {1 1} 333do_test malloc5-6.1.2 { 334 list [execsql {PRAGMA cache_size}] [execsql {PRAGMA cache_size} db2] 335} {2 2} 336 337do_test malloc5-6.2.1 { 338 execsql {SELECT * FROM abc} db2 339 execsql {SELECT * FROM abc} db 340 expr [nPage db] + [nPage db2] 341} {4} 342 343do_test malloc5-6.2.2 { 344 # If we now try to reclaim some memory, it should come from the db2 cache. 345 sqlite3_release_memory 3000 346 expr [nPage db] + [nPage db2] 347} {4} 348do_test malloc5-6.2.3 { 349 # Access the db2 cache again, so that all the db2 pages have been used 350 # more recently than all the db pages. Then try to reclaim 3000 bytes. 351 # This time, 3 pages should be pulled from the db cache. 352 execsql { SELECT * FROM abc } db2 353 sqlite3_release_memory 3000 354 expr [nPage db] + [nPage db2] 355} {4} 356 357do_test malloc5-6.3.1 { 358 # Now open a transaction and update 2 pages in the db2 cache. Then 359 # do a SELECT on the db cache so that all the db pages are more recently 360 # used than the db2 pages. When we try to free memory, SQLite should 361 # free the non-dirty db2 pages, then the db pages, then finally use 362 # sync() to free up the dirty db2 pages. The only page that cannot be 363 # freed is page1 of db2. Because there is an open transaction, the 364 # btree layer holds a reference to page 1 in the db2 cache. 365 execsql { 366 BEGIN; 367 UPDATE abc SET c = randstr(100,100) 368 WHERE rowid = 1 OR rowid = (SELECT max(rowid) FROM abc); 369 } db2 370 execsql { SELECT * FROM abc } db 371 expr [nPage db] + [nPage db2] 372} {4} 373do_test malloc5-6.3.2 { 374 # Try to release 7700 bytes. This should release all the 375 # non-dirty pages held by db2. 376 sqlite3_release_memory [expr 7*1132] 377 list [nPage db] [nPage db2] 378} {1 3} 379do_test malloc5-6.3.3 { 380 # Try to release another 1000 bytes. This should come fromt the db 381 # cache, since all three pages held by db2 are either in-use or diry. 382 sqlite3_release_memory 1000 383 list [nPage db] [nPage db2] 384} {1 3} 385do_test malloc5-6.3.4 { 386 # Now release 9900 more (about 9 pages worth). This should expunge 387 # the rest of the db cache. But the db2 cache remains intact, because 388 # SQLite tries to avoid calling sync(). 389 if {$::tcl_platform(wordSize)==8} { 390 sqlite3_release_memory 10500 391 } else { 392 sqlite3_release_memory 9900 393 } 394 list [nPage db] [nPage db2] 395} {1 3} 396do_test malloc5-6.3.5 { 397 # But if we are really insistent, SQLite will consent to call sync() 398 # if there is no other option. UPDATE: As of 3.6.2, SQLite will not 399 # call sync() in this scenario. So no further memory can be reclaimed. 400 sqlite3_release_memory 1000 401 list [nPage db] [nPage db2] 402} {1 3} 403do_test malloc5-6.3.6 { 404 # The referenced page (page 1 of the db2 cache) will not be freed no 405 # matter how much memory we ask for: 406 sqlite3_release_memory 31459 407 list [nPage db] [nPage db2] 408} {1 3} 409 410db2 close 411 412sqlite3_soft_heap_limit $::soft_limit 413finish_test 414catch {db close} 415