malloc5.test 12 KB

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