10190d1daSdanielk1977# 2005 November 30 20190d1daSdanielk1977# 30190d1daSdanielk1977# The author disclaims copyright to this source code. In place of 40190d1daSdanielk1977# a legal notice, here is a blessing: 50190d1daSdanielk1977# 60190d1daSdanielk1977# May you do good and not evil. 70190d1daSdanielk1977# May you find forgiveness for yourself and forgive others. 80190d1daSdanielk1977# May you share freely, never taking more than you give. 90190d1daSdanielk1977# 100190d1daSdanielk1977#*********************************************************************** 110190d1daSdanielk1977# 12*aef0bf64Sdanielk1977# This file contains test cases focused on the two memory-management APIs, 13*aef0bf64Sdanielk1977# sqlite3_soft_heap_limit() and sqlite3_release_memory(). 14*aef0bf64Sdanielk1977# 15*aef0bf64Sdanielk1977# $Id: malloc5.test,v 1.3 2005/12/30 16:28:02 danielk1977 Exp $ 160190d1daSdanielk1977 170190d1daSdanielk1977#--------------------------------------------------------------------------- 180190d1daSdanielk1977# NOTES ON EXPECTED BEHAVIOUR 190190d1daSdanielk1977# 200190d1daSdanielk1977#--------------------------------------------------------------------------- 210190d1daSdanielk1977 220190d1daSdanielk1977set testdir [file dirname $argv0] 230190d1daSdanielk1977source $testdir/tester.tcl 240190d1daSdanielk1977 25*aef0bf64Sdanielk1977# Only run these tests if memory debugging is turned on. 26*aef0bf64Sdanielk1977if {[info command sqlite_malloc_stat]==""} { 27*aef0bf64Sdanielk1977 puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." 28*aef0bf64Sdanielk1977 finish_test 29*aef0bf64Sdanielk1977 return 30*aef0bf64Sdanielk1977} 31*aef0bf64Sdanielk1977 320190d1daSdanielk1977do_test malloc5-1.1 { 330190d1daSdanielk1977 # Simplest possible test. Call [db release_memory] when there is exactly 340190d1daSdanielk1977 # one unused page in a single pager cache. This test case set's the 350190d1daSdanielk1977 # value of the ::pgalloc variable, which is used in subsequent tests. 360190d1daSdanielk1977 # 370190d1daSdanielk1977 # Note: Even though executing this statement on an empty database 380190d1daSdanielk1977 # modifies 2 pages (the root of sqlite_master and the new root page), 390190d1daSdanielk1977 # the sqlite_master root (page 1) is never freed because the btree layer 400190d1daSdanielk1977 # retains a reference to it for the entire transaction. 410190d1daSdanielk1977 execsql { 420190d1daSdanielk1977 BEGIN; 430190d1daSdanielk1977 CREATE TABLE abc(a, b, c); 440190d1daSdanielk1977 } 450190d1daSdanielk1977 set ::pgalloc [db release_memory] 460190d1daSdanielk1977 expr $::pgalloc > 0 470190d1daSdanielk1977} {1} 480190d1daSdanielk1977do_test malloc5-1.2 { 490190d1daSdanielk1977 # Test that the transaction started in the above test is still active. 500190d1daSdanielk1977 # Because the page freed had been written to, freeing it required a 510190d1daSdanielk1977 # journal sync and exclusive lock on the database file. Test the file 520190d1daSdanielk1977 # appears to be locked. 530190d1daSdanielk1977 sqlite3 db2 test.db 540190d1daSdanielk1977 catchsql { 550190d1daSdanielk1977 SELECT * FROM abc; 560190d1daSdanielk1977 } db2 570190d1daSdanielk1977} {1 {database is locked}} 580190d1daSdanielk1977do_test malloc5-1.3 { 590190d1daSdanielk1977 # Again call [db release_memory] when there is exactly one unused page 600190d1daSdanielk1977 # in the cache. The same amount of memory is required, but no journal-sync 610190d1daSdanielk1977 # or exclusive lock should be established. 620190d1daSdanielk1977 execsql { 630190d1daSdanielk1977 COMMIT; 640190d1daSdanielk1977 BEGIN; 650190d1daSdanielk1977 SELECT * FROM abc; 660190d1daSdanielk1977 } 670190d1daSdanielk1977 db release_memory 680190d1daSdanielk1977} $::pgalloc 690190d1daSdanielk1977do_test malloc5-1.4 { 700190d1daSdanielk1977 # Database should not be locked this time. 710190d1daSdanielk1977 catchsql { 720190d1daSdanielk1977 SELECT * FROM abc; 730190d1daSdanielk1977 } db2 740190d1daSdanielk1977} {0 {}} 750190d1daSdanielk1977do_test malloc5-1.5 { 760190d1daSdanielk1977 # Manipulate the cache so that it contains two unused pages. One requires 770190d1daSdanielk1977 # a journal-sync to free, the other does not. 780190d1daSdanielk1977 execsql { 790190d1daSdanielk1977 SELECT * FROM abc; 800190d1daSdanielk1977 CREATE TABLE def(d, e, f); 810190d1daSdanielk1977 } 820190d1daSdanielk1977 db release_memory 500 830190d1daSdanielk1977} $::pgalloc 840190d1daSdanielk1977do_test malloc5-1.6 { 850190d1daSdanielk1977 # Database should not be locked this time. The above test case only 860190d1daSdanielk1977 # requested 500 bytes of memory, which can be obtained by freeing the page 870190d1daSdanielk1977 # that does not require an fsync(). 880190d1daSdanielk1977 catchsql { 890190d1daSdanielk1977 SELECT * FROM abc; 900190d1daSdanielk1977 } db2 910190d1daSdanielk1977} {0 {}} 920190d1daSdanielk1977do_test malloc5-1.7 { 930190d1daSdanielk1977 # Release another 500 bytes of memory. This time we require a sync(), 940190d1daSdanielk1977 # so the database file will be locked afterwards. 950190d1daSdanielk1977 db release_memory 500 960190d1daSdanielk1977} $::pgalloc 970190d1daSdanielk1977do_test malloc5-1.8 { 980190d1daSdanielk1977 catchsql { 990190d1daSdanielk1977 SELECT * FROM abc; 1000190d1daSdanielk1977 } db2 1010190d1daSdanielk1977} {1 {database is locked}} 1020190d1daSdanielk1977do_test malloc5-1.9 { 1030190d1daSdanielk1977 execsql { 1040190d1daSdanielk1977 COMMIT; 1050190d1daSdanielk1977 } 1060190d1daSdanielk1977} {} 1070190d1daSdanielk1977 1080190d1daSdanielk1977do_test malloc5-2.1 { 1090190d1daSdanielk1977 # Put some data in tables abc and def. Both tables are still wholly 1100190d1daSdanielk1977 # contained within their root pages. 1110190d1daSdanielk1977 execsql { 1120190d1daSdanielk1977 INSERT INTO abc VALUES(1, 2, 3); 1130190d1daSdanielk1977 INSERT INTO abc VALUES(4, 5, 6); 1140190d1daSdanielk1977 INSERT INTO def VALUES(7, 8, 9); 1150190d1daSdanielk1977 INSERT INTO def VALUES(10,11,12); 1160190d1daSdanielk1977 } 1170190d1daSdanielk1977} {} 1180190d1daSdanielk1977do_test malloc5-2.2 { 1190190d1daSdanielk1977 # Load the root-page for table def into the cache. Then query table abc. 1200190d1daSdanielk1977 # Halfway through the query call sqlite3_release_memory(). The goal of this 1210190d1daSdanielk1977 # test is to make sure we don't free pages that are in use (specifically, 1220190d1daSdanielk1977 # the root of table abc). 1230190d1daSdanielk1977 set nRelease 0 1240190d1daSdanielk1977 execsql { 1250190d1daSdanielk1977 BEGIN; 1260190d1daSdanielk1977 SELECT * FROM def; 1270190d1daSdanielk1977 } 1285591df55Sdanielk1977 set data [list] 1290190d1daSdanielk1977 db eval {SELECT * FROM abc} { 1300190d1daSdanielk1977 incr nRelease [db release_memory] 1310190d1daSdanielk1977 lappend data $a $b $c 1320190d1daSdanielk1977 } 1335591df55Sdanielk1977 execsql { 1345591df55Sdanielk1977 COMMIT; 1355591df55Sdanielk1977 } 1360190d1daSdanielk1977 list $nRelease $data 1370190d1daSdanielk1977} [list $pgalloc [list 1 2 3 4 5 6]] 1380190d1daSdanielk1977 1395591df55Sdanielk1977do_test malloc5-3.1 { 1405591df55Sdanielk1977 # Simple test to show that if two pagers are opened from within this 1415591df55Sdanielk1977 # thread, memory is freed from both when sqlite3_release_memory() is 1425591df55Sdanielk1977 # called. 1435591df55Sdanielk1977 execsql { 1445591df55Sdanielk1977 BEGIN; 1455591df55Sdanielk1977 SELECT * FROM abc; 1465591df55Sdanielk1977 } 1475591df55Sdanielk1977 execsql { 1485591df55Sdanielk1977 SELECT * FROM sqlite_master; 1495591df55Sdanielk1977 BEGIN; 1505591df55Sdanielk1977 SELECT * FROM def; 1515591df55Sdanielk1977 } db2 1525591df55Sdanielk1977 db release_memory 1535591df55Sdanielk1977} [expr $::pgalloc * 2] 1545591df55Sdanielk1977do_test malloc5-3.2 { 1555591df55Sdanielk1977 concat \ 1565591df55Sdanielk1977 [execsql {SELECT * FROM abc; COMMIT}] \ 1575591df55Sdanielk1977 [execsql {SELECT * FROM def; COMMIT} db2] 1585591df55Sdanielk1977} {1 2 3 4 5 6 7 8 9 10 11 12} 1595591df55Sdanielk1977 1605591df55Sdanielk1977db2 close 1615591df55Sdanielk1977sqlite_malloc_outstanding -clearmaxbytes 1625591df55Sdanielk1977 1635591df55Sdanielk1977# The following two test cases each execute a transaction in which 1645591df55Sdanielk1977# 10000 rows are inserted into table abc. The first test case is used 1655591df55Sdanielk1977# to ensure that more than 1MB of dynamic memory is used to perform 1665591df55Sdanielk1977# the transaction. 1675591df55Sdanielk1977# 1685591df55Sdanielk1977# The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB) 1695591df55Sdanielk1977# and tests to see that this limit is not exceeded at any point during 1705591df55Sdanielk1977# transaction execution. 1715591df55Sdanielk1977# 172*aef0bf64Sdanielk1977# Before executing malloc5-4.* we save the value of the current soft heap 173*aef0bf64Sdanielk1977# limit in variable ::soft_limit. The original value is restored after 174*aef0bf64Sdanielk1977# running the tests. 175*aef0bf64Sdanielk1977# 176*aef0bf64Sdanielk1977set ::soft_limit [db soft_heap_limit -1] 1775591df55Sdanielk1977do_test malloc5-4.1 { 1785591df55Sdanielk1977 execsql {BEGIN;} 1795591df55Sdanielk1977 execsql {DELETE FROM abc;} 1805591df55Sdanielk1977 for {set i 0} {$i < 10000} {incr i} { 1815591df55Sdanielk1977 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" 1825591df55Sdanielk1977 } 1835591df55Sdanielk1977 execsql {COMMIT;} 1845591df55Sdanielk1977 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] 1855591df55Sdanielk1977 expr $::nMaxBytes > 1000000 1865591df55Sdanielk1977} {1} 1875591df55Sdanielk1977do_test malloc5-4.2 { 1885591df55Sdanielk1977 db release_memory 1895591df55Sdanielk1977 sqlite_malloc_outstanding -clearmaxbytes 1905591df55Sdanielk1977 db soft_heap_limit 100000 1915591df55Sdanielk1977 execsql {BEGIN;} 1925591df55Sdanielk1977 for {set i 0} {$i < 10000} {incr i} { 1935591df55Sdanielk1977 execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" 1945591df55Sdanielk1977 } 1955591df55Sdanielk1977 execsql {COMMIT;} 1965591df55Sdanielk1977 set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] 1975591df55Sdanielk1977 expr $::nMaxBytes <= 100000 1985591df55Sdanielk1977} {1} 1995591df55Sdanielk1977do_test malloc5-4.3 { 2005591df55Sdanielk1977 # Check that the content of table abc is at least roughly as expected. 2015591df55Sdanielk1977 execsql { 2025591df55Sdanielk1977 SELECT count(*), sum(a), sum(b) FROM abc; 2035591df55Sdanielk1977 } 2045591df55Sdanielk1977} [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]] 2055591df55Sdanielk1977 206*aef0bf64Sdanielk1977# Restore the soft heap limit. 207*aef0bf64Sdanielk1977db soft_heap_limit $::soft_limit 2085591df55Sdanielk1977 2090190d1daSdanielk1977finish_test 2100190d1daSdanielk1977 2110190d1daSdanielk1977 212