danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 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 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 12 | # This file contains test cases focused on the two memory-management APIs, |
| 13 | # sqlite3_soft_heap_limit() and sqlite3_release_memory(). |
| 14 | # |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame^] | 15 | # $Id: malloc5.test,v 1.8 2007/04/02 05:07:48 danielk1977 Exp $ |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 16 | |
| 17 | #--------------------------------------------------------------------------- |
| 18 | # NOTES ON EXPECTED BEHAVIOUR |
| 19 | # |
| 20 | #--------------------------------------------------------------------------- |
| 21 | |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 22 | |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 23 | set testdir [file dirname $argv0] |
| 24 | source $testdir/tester.tcl |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 25 | db close |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 26 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 27 | # Only run these tests if memory debugging is turned on. |
| 28 | if {[info command sqlite_malloc_stat]==""} { |
| 29 | puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..." |
| 30 | finish_test |
| 31 | return |
| 32 | } |
| 33 | |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 34 | # Skip these tests if OMIT_MEMORY_MANAGEMENT was defined at compile time. |
| 35 | ifcapable !memorymanage { |
| 36 | finish_test |
| 37 | return |
| 38 | } |
| 39 | |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 40 | sqlite3 db test.db |
| 41 | |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 42 | do_test malloc5-1.1 { |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 43 | # Simplest possible test. Call sqlite3_release_memory when there is exactly |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 44 | # one unused page in a single pager cache. This test case set's the |
| 45 | # value of the ::pgalloc variable, which is used in subsequent tests. |
| 46 | # |
| 47 | # Note: Even though executing this statement on an empty database |
| 48 | # modifies 2 pages (the root of sqlite_master and the new root page), |
| 49 | # the sqlite_master root (page 1) is never freed because the btree layer |
| 50 | # retains a reference to it for the entire transaction. |
| 51 | execsql { |
| 52 | BEGIN; |
| 53 | CREATE TABLE abc(a, b, c); |
| 54 | } |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 55 | set ::pgalloc [sqlite3_release_memory] |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 56 | expr $::pgalloc > 0 |
| 57 | } {1} |
| 58 | do_test malloc5-1.2 { |
| 59 | # Test that the transaction started in the above test is still active. |
| 60 | # Because the page freed had been written to, freeing it required a |
| 61 | # journal sync and exclusive lock on the database file. Test the file |
| 62 | # appears to be locked. |
| 63 | sqlite3 db2 test.db |
| 64 | catchsql { |
| 65 | SELECT * FROM abc; |
| 66 | } db2 |
| 67 | } {1 {database is locked}} |
| 68 | do_test malloc5-1.3 { |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 69 | # Again call [sqlite3_release_memory] when there is exactly one unused page |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 70 | # in the cache. The same amount of memory is required, but no journal-sync |
| 71 | # or exclusive lock should be established. |
| 72 | execsql { |
| 73 | COMMIT; |
| 74 | BEGIN; |
| 75 | SELECT * FROM abc; |
| 76 | } |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 77 | sqlite3_release_memory |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 78 | } $::pgalloc |
| 79 | do_test malloc5-1.4 { |
| 80 | # Database should not be locked this time. |
| 81 | catchsql { |
| 82 | SELECT * FROM abc; |
| 83 | } db2 |
| 84 | } {0 {}} |
| 85 | do_test malloc5-1.5 { |
| 86 | # Manipulate the cache so that it contains two unused pages. One requires |
| 87 | # a journal-sync to free, the other does not. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame^] | 88 | db2 close |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 89 | execsql { |
| 90 | SELECT * FROM abc; |
| 91 | CREATE TABLE def(d, e, f); |
| 92 | } |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 93 | sqlite3_release_memory 500 |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 94 | } $::pgalloc |
| 95 | do_test malloc5-1.6 { |
| 96 | # Database should not be locked this time. The above test case only |
| 97 | # requested 500 bytes of memory, which can be obtained by freeing the page |
| 98 | # that does not require an fsync(). |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame^] | 99 | sqlite3 db2 test.db |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 100 | catchsql { |
| 101 | SELECT * FROM abc; |
| 102 | } db2 |
| 103 | } {0 {}} |
| 104 | do_test malloc5-1.7 { |
| 105 | # Release another 500 bytes of memory. This time we require a sync(), |
| 106 | # so the database file will be locked afterwards. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame^] | 107 | db2 close |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 108 | sqlite3_release_memory 500 |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 109 | } $::pgalloc |
| 110 | do_test malloc5-1.8 { |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame^] | 111 | sqlite3 db2 test.db |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 112 | catchsql { |
| 113 | SELECT * FROM abc; |
| 114 | } db2 |
| 115 | } {1 {database is locked}} |
| 116 | do_test malloc5-1.9 { |
| 117 | execsql { |
| 118 | COMMIT; |
| 119 | } |
| 120 | } {} |
| 121 | |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 122 | do_test malloc5-2.1 { |
| 123 | # Put some data in tables abc and def. Both tables are still wholly |
| 124 | # contained within their root pages. |
| 125 | execsql { |
| 126 | INSERT INTO abc VALUES(1, 2, 3); |
| 127 | INSERT INTO abc VALUES(4, 5, 6); |
| 128 | INSERT INTO def VALUES(7, 8, 9); |
| 129 | INSERT INTO def VALUES(10,11,12); |
| 130 | } |
| 131 | } {} |
| 132 | do_test malloc5-2.2 { |
| 133 | # Load the root-page for table def into the cache. Then query table abc. |
| 134 | # Halfway through the query call sqlite3_release_memory(). The goal of this |
| 135 | # test is to make sure we don't free pages that are in use (specifically, |
| 136 | # the root of table abc). |
| 137 | set nRelease 0 |
| 138 | execsql { |
| 139 | BEGIN; |
| 140 | SELECT * FROM def; |
| 141 | } |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 142 | set data [list] |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 143 | db eval {SELECT * FROM abc} { |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 144 | incr nRelease [sqlite3_release_memory] |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 145 | lappend data $a $b $c |
| 146 | } |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 147 | execsql { |
| 148 | COMMIT; |
| 149 | } |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 150 | list $nRelease $data |
| 151 | } [list $pgalloc [list 1 2 3 4 5 6]] |
| 152 | |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 153 | do_test malloc5-3.1 { |
| 154 | # Simple test to show that if two pagers are opened from within this |
| 155 | # thread, memory is freed from both when sqlite3_release_memory() is |
| 156 | # called. |
| 157 | execsql { |
| 158 | BEGIN; |
| 159 | SELECT * FROM abc; |
| 160 | } |
| 161 | execsql { |
| 162 | SELECT * FROM sqlite_master; |
| 163 | BEGIN; |
| 164 | SELECT * FROM def; |
| 165 | } db2 |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 166 | sqlite3_release_memory |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 167 | } [expr $::pgalloc * 2] |
| 168 | do_test malloc5-3.2 { |
| 169 | concat \ |
| 170 | [execsql {SELECT * FROM abc; COMMIT}] \ |
| 171 | [execsql {SELECT * FROM def; COMMIT} db2] |
| 172 | } {1 2 3 4 5 6 7 8 9 10 11 12} |
| 173 | |
| 174 | db2 close |
| 175 | sqlite_malloc_outstanding -clearmaxbytes |
| 176 | |
| 177 | # The following two test cases each execute a transaction in which |
| 178 | # 10000 rows are inserted into table abc. The first test case is used |
| 179 | # to ensure that more than 1MB of dynamic memory is used to perform |
| 180 | # the transaction. |
| 181 | # |
| 182 | # The second test case sets the "soft-heap-limit" to 100,000 bytes (0.1 MB) |
| 183 | # and tests to see that this limit is not exceeded at any point during |
| 184 | # transaction execution. |
| 185 | # |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 186 | # Before executing malloc5-4.* we save the value of the current soft heap |
| 187 | # limit in variable ::soft_limit. The original value is restored after |
| 188 | # running the tests. |
| 189 | # |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 190 | set ::soft_limit [sqlite3_soft_heap_limit -1] |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 191 | do_test malloc5-4.1 { |
| 192 | execsql {BEGIN;} |
| 193 | execsql {DELETE FROM abc;} |
| 194 | for {set i 0} {$i < 10000} {incr i} { |
| 195 | execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" |
| 196 | } |
| 197 | execsql {COMMIT;} |
| 198 | set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 199 | if {$::nMaxBytes==""} {set ::nMaxBytes 1000001} |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 200 | expr $::nMaxBytes > 1000000 |
| 201 | } {1} |
| 202 | do_test malloc5-4.2 { |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 203 | sqlite3_release_memory |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 204 | sqlite_malloc_outstanding -clearmaxbytes |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 205 | sqlite3_soft_heap_limit 100000 |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 206 | execsql {BEGIN;} |
| 207 | for {set i 0} {$i < 10000} {incr i} { |
| 208 | execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');" |
| 209 | } |
| 210 | execsql {COMMIT;} |
| 211 | set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 212 | if {$::nMaxBytes==""} {set ::nMaxBytes 0} |
danielk1977 | 5591df5 | 2005-12-20 09:19:37 +0000 | [diff] [blame] | 213 | expr $::nMaxBytes <= 100000 |
| 214 | } {1} |
| 215 | do_test malloc5-4.3 { |
| 216 | # Check that the content of table abc is at least roughly as expected. |
| 217 | execsql { |
| 218 | SELECT count(*), sum(a), sum(b) FROM abc; |
| 219 | } |
| 220 | } [list 20000 [expr int(20000.0 * 4999.5)] [expr int(20000.0 * 4999.5)]] |
| 221 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 222 | # Restore the soft heap limit. |
drh | 6aafc29 | 2006-01-05 15:50:06 +0000 | [diff] [blame] | 223 | sqlite3_soft_heap_limit $::soft_limit |
danielk1977 | 0190d1d | 2005-12-19 14:18:11 +0000 | [diff] [blame] | 224 | finish_test |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 225 | |
| 226 | catch {db close} |
danielk1977 | 5262282 | 2006-01-09 09:59:49 +0000 | [diff] [blame] | 227 | |