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