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