1
0

memsubsys2.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # 2008 June 18
  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 tests of the memory allocation subsystem.
  13. #
  14. # $Id: memsubsys2.test,v 1.2 2008/08/12 15:21:12 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. sqlite3_reset_auto_extension
  18. # This procedure constructs a new database in test.db. It fills
  19. # this database with many small records (enough to force multiple
  20. # rebalance operations in the btree-layer and to require a large
  21. # page cache), verifies correct results, then returns.
  22. #
  23. proc build_test_db {testname pragmas} {
  24. catch {db close}
  25. forcedelete test.db test.db-journal
  26. sqlite3 db test.db
  27. db eval $pragmas
  28. db eval {
  29. CREATE TABLE t1(x, y);
  30. CREATE TABLE t2(a, b);
  31. CREATE INDEX i1 ON t1(x,y);
  32. INSERT INTO t1 VALUES(1, 100);
  33. INSERT INTO t1 VALUES(2, 200);
  34. }
  35. for {set i 2} {$i<5000} {incr i $i} {
  36. db eval {INSERT INTO t2 SELECT * FROM t1}
  37. db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
  38. db eval {DELETE FROM t2}
  39. }
  40. do_test $testname.1 {
  41. db eval {SELECT count(*) FROM t1}
  42. } 8192
  43. integrity_check $testname.2
  44. }
  45. # Reset all of the highwater marks.
  46. #
  47. proc reset_highwater_marks {} {
  48. sqlite3_status SQLITE_STATUS_MEMORY_USED 1
  49. sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
  50. sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
  51. sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
  52. sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
  53. sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
  54. sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
  55. sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
  56. sqlite3_status SQLITE_STATUS_PARSER_STACK 1
  57. }
  58. # Test 1: Verify that calling sqlite3_malloc(0) returns a NULL
  59. # pointer.
  60. #
  61. set highwater [sqlite3_memory_highwater 0]
  62. do_test memsubsys2-1.1 {
  63. sqlite3_malloc 0
  64. } {0}
  65. do_test memsubsys2-1.2 {
  66. sqlite3_memory_highwater 0
  67. } $highwater
  68. # Test 2: Verify that the highwater mark increases after a large
  69. # allocation.
  70. #
  71. sqlite3_memory_highwater 1
  72. set highwater [sqlite3_memory_highwater 0]
  73. do_test memsubsys2-2.1 {
  74. sqlite3_free [set x [sqlite3_malloc 100000]]
  75. expr {$x!="0"}
  76. } {1}
  77. do_test memsubsys2-2.2 {
  78. expr {[sqlite3_memory_highwater 0]>=[sqlite3_memory_used]+$highwater}
  79. } {1}
  80. # Test 3: Verify that turning of memstatus disables the statistics
  81. # tracking.
  82. #
  83. db close
  84. sqlite3_shutdown
  85. sqlite3_config_memstatus 0
  86. sqlite3_initialize
  87. reset_highwater_marks
  88. set highwater [sqlite3_memory_highwater 0]
  89. do_test memsubsys2-3.1 {
  90. set highwater
  91. } {0}
  92. do_test memsubsys2-3.2 {
  93. sqlite3_malloc 0
  94. } {0}
  95. do_test memsubsys2-3.3 {
  96. sqlite3_memory_highwater 0
  97. } {0}
  98. do_test memsubsys2-3.4 {
  99. sqlite3_memory_used
  100. } {0}
  101. do_test memsubsys2-3.5 {
  102. set ::allocation [sqlite3_malloc 100000]
  103. expr {$::allocation!="0"}
  104. } {1}
  105. do_test memsubsys2-3.6 {
  106. sqlite3_memory_highwater 0
  107. } {0}
  108. do_test memsubsys2-3.7 {
  109. sqlite3_memory_used
  110. } {0}
  111. do_test memsubsys2-3.8 {
  112. sqlite3_free $::allocation
  113. } {}
  114. do_test memsubsys2-3.9 {
  115. sqlite3_free 0
  116. } {}
  117. # Test 4: Verify that turning on memstatus reenables the statistics
  118. # tracking.
  119. #
  120. sqlite3_shutdown
  121. sqlite3_config_memstatus 1
  122. sqlite3_initialize
  123. reset_highwater_marks
  124. set highwater [sqlite3_memory_highwater 0]
  125. do_test memsubsys2-4.1 {
  126. set highwater
  127. } {0}
  128. do_test memsubsys2-4.2 {
  129. sqlite3_malloc 0
  130. } {0}
  131. do_test memsubsys2-4.3 {
  132. sqlite3_memory_highwater 0
  133. } {0}
  134. do_test memsubsys2-4.4 {
  135. sqlite3_memory_used
  136. } {0}
  137. do_test memsubsys2-4.5 {
  138. set ::allocation [sqlite3_malloc 100000]
  139. expr {$::allocation!="0"}
  140. } {1}
  141. do_test memsubsys2-4.6 {
  142. expr {[sqlite3_memory_highwater 0]>=100000}
  143. } {1}
  144. do_test memsubsys2-4.7 {
  145. expr {[sqlite3_memory_used]>=100000}
  146. } {1}
  147. do_test memsubsys2-4.8 {
  148. sqlite3_free $::allocation
  149. } {}
  150. do_test memsubsys2-4.9 {
  151. sqlite3_free 0
  152. } {}
  153. do_test memsubsys2-4.10 {
  154. expr {[sqlite3_memory_highwater 0]>=100000}
  155. } {1}
  156. do_test memsubsys2-4.11 {
  157. sqlite3_memory_used
  158. } {0}
  159. autoinstall_test_functions
  160. finish_test