blob: e883c90ae9f62914ab52a8713037e6e875b4d361 [file] [log] [blame]
danielk197746abae82007-08-21 13:30:07 +00001# 2007 August 21
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# The focus of this file is testing some specific characteristics of the
13# IO traffic generated by SQLite (making sure SQLite is not writing out
14# more database pages than it has to, stuff like that).
15#
danielk19774152e672007-09-12 17:01:45 +000016# $Id: io.test,v 1.10 2007/09/12 17:01:45 danielk1977 Exp $
danielk197746abae82007-08-21 13:30:07 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
danielk19772ca0f862007-08-23 08:06:44 +000021# Test summary:
22#
23# io-1.* - Test that quick-balance does not journal pages unnecessarily.
danielk19776897ca32007-08-23 16:27:21 +000024#
25# io-2.* - Test the "atomic-write optimization".
26#
27# io-3.* - Test the IO traffic enhancements triggered when the
28# IOCAP_SEQUENTIAL device capability flag is set (no
29# fsync() calls on the journal file).
30#
31# io-4.* - Test the IO traffic enhancements triggered when the
32# IOCAP_SAFE_APPEND device capability flag is set (fewer
33# fsync() calls on the journal file, no need to set nRec
34# field in the single journal header).
danielk19779663b8f2007-08-24 11:52:28 +000035#
36# io-5.* - Test that the default page size is selected and used
37# correctly.
danielk19772ca0f862007-08-23 08:06:44 +000038#
39
danielk197746abae82007-08-21 13:30:07 +000040set ::nWrite 0
41proc nWrite {db} {
42 set bt [btree_from_db $db]
drh27641702007-08-22 02:56:42 +000043 db_enter $db
danielk197746abae82007-08-21 13:30:07 +000044 array set stats [btree_pager_stats $bt]
drh27641702007-08-22 02:56:42 +000045 db_leave $db
danielk197746abae82007-08-21 13:30:07 +000046 set res [expr $stats(write) - $::nWrite]
47 set ::nWrite $stats(write)
48 set res
49}
50
danielk19772ca0f862007-08-23 08:06:44 +000051set ::nSync 0
52proc nSync {} {
53 set res [expr {$::sqlite_sync_count - $::nSync}]
54 set ::nSync $::sqlite_sync_count
55 set res
56}
57
danielk197746abae82007-08-21 13:30:07 +000058do_test io-1.1 {
59 execsql {
60 PRAGMA page_size = 1024;
61 CREATE TABLE abc(a,b);
62 }
63 nWrite db
64} {2}
65
66# Insert into the table 4 records of aproximately 240 bytes each.
67# This should completely fill the root-page of the table. Each
68# INSERT causes 2 db pages to be written - the root-page of "abc"
69# and page 1 (db change-counter page).
70do_test io-1.2 {
71 set ret [list]
72 execsql { INSERT INTO abc VALUES(1,randstr(230,230)); }
73 lappend ret [nWrite db]
74 execsql { INSERT INTO abc VALUES(2,randstr(230,230)); }
75 lappend ret [nWrite db]
76 execsql { INSERT INTO abc VALUES(3,randstr(230,230)); }
77 lappend ret [nWrite db]
78 execsql { INSERT INTO abc VALUES(4,randstr(230,230)); }
79 lappend ret [nWrite db]
80} {2 2 2 2}
81
82# Insert another 240 byte record. This causes two leaf pages
83# to be added to the root page of abc. 4 pages in total
84# are written to the db file - the two leaf pages, the root
85# of abc and the change-counter page.
86do_test io-1.3 {
87 execsql { INSERT INTO abc VALUES(5,randstr(230,230)); }
88 nWrite db
89} {4}
90
91# Insert another 3 240 byte records. After this, the tree consists of
92# the root-node, which is close to empty, and two leaf pages, both of
93# which are full.
94do_test io-1.4 {
95 set ret [list]
96 execsql { INSERT INTO abc VALUES(6,randstr(230,230)); }
97 lappend ret [nWrite db]
98 execsql { INSERT INTO abc VALUES(7,randstr(230,230)); }
99 lappend ret [nWrite db]
100 execsql { INSERT INTO abc VALUES(8,randstr(230,230)); }
101 lappend ret [nWrite db]
102} {2 2 2}
103
danielk197746abae82007-08-21 13:30:07 +0000104# This insert should use the quick-balance trick to add a third leaf
105# to the b-tree used to store table abc. It should only be necessary to
106# write to 3 pages to do this: the change-counter, the root-page and
107# the new leaf page.
108do_test io-1.5 {
109 execsql { INSERT INTO abc VALUES(9,randstr(230,230)); }
110 nWrite db
111} {3}
112
drhd6b93862007-08-29 17:59:42 +0000113ifcapable atomicwrite {
danielk19772ca0f862007-08-23 08:06:44 +0000114
115#----------------------------------------------------------------------
116# Test cases io-2.* test the atomic-write optimization.
117#
118do_test io-2.1 {
119 execsql { DELETE FROM abc; VACUUM; }
120} {}
121
122# Clear the write and sync counts.
123nWrite db ; nSync
124
125# The following INSERT updates 2 pages and requires 4 calls to fsync():
126#
127# 1) The directory in which the journal file is created,
128# 2) The journal file (to sync the page data),
129# 3) The journal file (to sync the journal file header),
130# 4) The database file.
131#
132do_test io-2.2 {
133 execsql { INSERT INTO abc VALUES(1, 2) }
134 list [nWrite db] [nSync]
135} {2 4}
136
137# Set the device-characteristic mask to include the SQLITE_IOCAP_ATOMIC,
138# then do another INSERT similar to the one in io-2.2. This should
139# only write 1 page and require a single fsync().
140#
141# The single fsync() is the database file. Only one page is reported as
142# written because page 1 - the change-counter page - is written using
143# an out-of-band method that bypasses the write counter.
144#
145sqlite3_simulate_device -char atomic
146do_test io-2.3 {
147 execsql { INSERT INTO abc VALUES(3, 4) }
148 list [nWrite db] [nSync]
149} {1 1}
150
151# Test that the journal file is not created and the change-counter is
152# updated when the atomic-write optimization is used.
153#
154do_test io-2.4.1 {
155 execsql {
156 BEGIN;
157 INSERT INTO abc VALUES(5, 6);
158 }
159 sqlite3 db2 test.db
160 execsql { SELECT * FROM abc } db2
161} {1 2 3 4}
162do_test io-2.4.2 {
163 file exists test.db-journal
164} {0}
165do_test io-2.4.3 {
166 execsql { COMMIT }
167 execsql { SELECT * FROM abc } db2
168} {1 2 3 4 5 6}
169db2 close
170
171# Test that the journal file is created and sync()d if the transaction
172# modifies more than one database page, even if the IOCAP_ATOMIC flag
173# is set.
174#
175do_test io-2.5.1 {
176 execsql { CREATE TABLE def(d, e) }
177 nWrite db ; nSync
178 execsql {
179 BEGIN;
180 INSERT INTO abc VALUES(7, 8);
181 }
182 file exists test.db-journal
183} {0}
184do_test io-2.5.2 {
185 execsql { INSERT INTO def VALUES('a', 'b'); }
186 file exists test.db-journal
187} {1}
188do_test io-2.5.3 {
189 execsql { COMMIT }
190 list [nWrite db] [nSync]
191} {3 4}
192
193# Test that the journal file is created and sync()d if the transaction
194# modifies a single database page and also appends a page to the file.
195# Internally, this case is handled differently to the one above. The
196# journal file is not actually created until the 'COMMIT' statement
197# is executed.
198#
199do_test io-2.6.1 {
200 execsql {
201 BEGIN;
202 INSERT INTO abc VALUES(9, randstr(1000,1000));
203 }
204 file exists test.db-journal
205} {0}
206do_test io-2.6.2 {
207 # Create a file at "test.db-journal". This will prevent SQLite from
208 # opening the journal for exclusive access. As a result, the COMMIT
209 # should fail with SQLITE_CANTOPEN and the transaction rolled back.
210 #
211 set fd [open test.db-journal w]
212 puts $fd "This is not a journal file"
213 close $fd
214 catchsql { COMMIT }
215} {1 {unable to open database file}}
216do_test io-2.6.3 {
217 file delete -force test.db-journal
218 catchsql { COMMIT }
219} {1 {cannot commit - no transaction is active}}
220do_test io-2.6.4 {
221 execsql { SELECT * FROM abc }
222} {1 2 3 4 5 6 7 8}
223
224
225# Test that if the database modification is part of multi-file commit,
226# the journal file is always created. In this case, the journal file
227# is created during execution of the COMMIT statement, so we have to
228# use the same technique to check that it is created as in the above
229# block.
230file delete -force test2.db test2.db-journal
231do_test io-2.7.1 {
232 execsql {
233 ATTACH 'test2.db' AS aux;
danielk19779663b8f2007-08-24 11:52:28 +0000234 PRAGMA aux.page_size = 1024;
danielk19772ca0f862007-08-23 08:06:44 +0000235 CREATE TABLE aux.abc2(a, b);
236 BEGIN;
237 INSERT INTO abc VALUES(9, 10);
238 }
239 file exists test.db-journal
240} {0}
241do_test io-2.7.2 {
242 execsql { INSERT INTO abc2 SELECT * FROM abc }
243 file exists test2.db-journal
244} {0}
245do_test io-2.7.3 {
246 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
247} {1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10}
248do_test io-2.7.4 {
249 set fd [open test2.db-journal w]
250 puts $fd "This is not a journal file"
251 close $fd
252 catchsql { COMMIT }
253} {1 {unable to open database file}}
254do_test io-2.7.5 {
255 file delete -force test2.db-journal
256 catchsql { COMMIT }
257} {1 {cannot commit - no transaction is active}}
258do_test io-2.7.6 {
259 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
260} {1 2 3 4 5 6 7 8}
261
262# Try an explicit ROLLBACK before the journal file is created.
263#
264do_test io-2.8.1 {
265 execsql {
266 BEGIN;
267 DELETE FROM abc;
268 }
269 file exists test.db-journal
270} {0}
271do_test io-2.8.2 {
272 execsql { SELECT * FROM abc }
273} {}
274do_test io-2.8.3 {
275 execsql {
276 ROLLBACK;
277 SELECT * FROM abc;
278 }
279} {1 2 3 4 5 6 7 8}
280
danielk1977f8940ae2007-08-23 11:07:10 +0000281# Test that the atomic write optimisation is not enabled if the sector
282# size is larger than the page-size.
283#
284do_test io-2.9.1 {
285 sqlite3_simulate_device -char atomic -sectorsize 2048
286 execsql {
287 BEGIN;
288 INSERT INTO abc VALUES(9, 10);
289 }
290 file exists test.db-journal
291} {1}
292do_test io-2.9.2 {
293 execsql { ROLLBACK; }
294 db close
295 file delete -force test.db test.db-journal
296 sqlite3 db test.db
297 execsql {
298 PRAGMA page_size = 2048;
299 CREATE TABLE abc(a, b);
300 }
301 execsql {
302 BEGIN;
303 INSERT INTO abc VALUES(9, 10);
304 }
305 file exists test.db-journal
306} {0}
307do_test io-2.9.3 {
308 execsql { COMMIT }
309} {}
310
311# Test a couple of the more specific IOCAP_ATOMIC flags
312# (i.e IOCAP_ATOMIC2K etc.).
313#
314do_test io-2.10.1 {
315 sqlite3_simulate_device -char atomic1k
316 execsql {
317 BEGIN;
318 INSERT INTO abc VALUES(11, 12);
319 }
320 file exists test.db-journal
321} {1}
322do_test io-2.10.2 {
323 execsql { ROLLBACK }
324 sqlite3_simulate_device -char atomic2k
325 execsql {
326 BEGIN;
327 INSERT INTO abc VALUES(11, 12);
328 }
329 file exists test.db-journal
330} {0}
danielk19776897ca32007-08-23 16:27:21 +0000331do_test io-2.10.3 {
332 execsql { ROLLBACK }
333} {}
danielk1977880c15b2007-09-01 18:24:55 +0000334
335do_test io-2.11.0 {
336 execsql {
337 PRAGMA locking_mode = exclusive;
338 PRAGMA locking_mode;
339 }
340} {exclusive exclusive}
341breakpoint
342do_test io-2.11.1 {
343 execsql {
344 INSERT INTO abc VALUES(11, 12);
345 }
346 file exists test.db-journal
347} {0}
348breakpoint
349
350do_test io-2.11.2 {
351 execsql {
352 PRAGMA locking_mode = normal;
353 INSERT INTO abc VALUES(13, 14);
354 }
355 file exists test.db-journal
356} {0}
357
drhd6b93862007-08-29 17:59:42 +0000358} ;# /* ifcapable atomicwrite */
danielk19776897ca32007-08-23 16:27:21 +0000359
360#----------------------------------------------------------------------
361# Test cases io-3.* test the IOCAP_SEQUENTIAL optimization.
362#
363sqlite3_simulate_device -char sequential -sectorsize 0
danielk19774152e672007-09-12 17:01:45 +0000364ifcapable pager_pragmas {
365 do_test io-3.1 {
366 db close
367 file delete -force test.db test.db-journal
368 sqlite3 db test.db
369 file size test.db
370 } {0}
371 do_test io-3.2 {
372 execsql { CREATE TABLE abc(a, b) }
373 nSync
374 execsql {
375 PRAGMA cache_size = 10;
376 BEGIN;
377 INSERT INTO abc VALUES('hello', 'world');
378 INSERT INTO abc SELECT * FROM abc;
379 INSERT INTO abc SELECT * FROM abc;
380 INSERT INTO abc SELECT * FROM abc;
381 INSERT INTO abc SELECT * FROM abc;
382 INSERT INTO abc SELECT * FROM abc;
383 INSERT INTO abc SELECT * FROM abc;
384 INSERT INTO abc SELECT * FROM abc;
385 INSERT INTO abc SELECT * FROM abc;
386 INSERT INTO abc SELECT * FROM abc;
387 INSERT INTO abc SELECT * FROM abc;
388 INSERT INTO abc SELECT * FROM abc;
389 }
390 # File has grown - showing there was a cache-spill - but there
391 # have been no calls to fsync():
392 list [file size test.db] [nSync]
393 } {31744 0}
394 do_test io-3.3 {
395 # The COMMIT requires a single fsync() - to the database file.
396 execsql { COMMIT }
397 list [file size test.db] [nSync]
398 } {39936 1}
399}
danielk19776897ca32007-08-23 16:27:21 +0000400
401#----------------------------------------------------------------------
402# Test cases io-4.* test the IOCAP_SAFE_APPEND optimization.
403#
404sqlite3_simulate_device -char safe_append
405
406# With the SAFE_APPEND flag set, simple transactions require 3, rather
407# than 4, calls to fsync(). The fsync() calls are on:
408#
drhdec6fae2007-09-03 17:02:50 +0000409# 1) The directory in which the journal file is created, (unix only)
danielk19776897ca32007-08-23 16:27:21 +0000410# 2) The journal file (to sync the page data),
411# 3) The database file.
412#
413# Normally, when the SAFE_APPEND flag is not set, there is another fsync()
414# on the journal file between steps (2) and (3) above.
415#
drhdec6fae2007-09-03 17:02:50 +0000416if {$::tcl_platform(platform)=="unix"} {
417 set expected_sync_count 3
418} else {
419 set expected_sync_count 2
420}
danielk19776897ca32007-08-23 16:27:21 +0000421do_test io-4.1 {
422 execsql { DELETE FROM abc }
423 nSync
424 execsql { INSERT INTO abc VALUES('a', 'b') }
425 nSync
drhdec6fae2007-09-03 17:02:50 +0000426} $expected_sync_count
danielk19776897ca32007-08-23 16:27:21 +0000427
428# With SAFE_APPEND set, the nRec field of the journal file header should
429# be set to 0xFFFFFFFF before the first journal sync. The nRec field
430# occupies bytes 8-11 of the journal file.
431#
432do_test io-4.2.1 {
433 execsql { BEGIN }
434 execsql { INSERT INTO abc VALUES('c', 'd') }
435 file exists test.db-journal
436} {1}
drhdec6fae2007-09-03 17:02:50 +0000437if {$::tcl_platform(platform)=="unix"} {
438 do_test io-4.2.2 {
439 set fd [open test.db-journal]
440 fconfigure $fd -translation binary -encoding binary
441 seek $fd 8
442 set blob [read $fd 4]
443 close $fd
444 binary scan $blob i res
445 format 0x%X $res
446 } {0xFFFFFFFF}
447}
danielk19776897ca32007-08-23 16:27:21 +0000448do_test io-4.2.3 {
449 execsql { COMMIT }
450 nSync
drhdec6fae2007-09-03 17:02:50 +0000451} $expected_sync_count
danielk19776897ca32007-08-23 16:27:21 +0000452sqlite3_simulate_device -char safe_append
453
454# With SAFE_APPEND set, there should only ever be one journal-header
455# written to the database, even though the sync-mode is "full".
456#
457do_test io-4.3.1 {
458 execsql {
459 INSERT INTO abc SELECT * FROM abc;
460 INSERT INTO abc SELECT * FROM abc;
461 INSERT INTO abc SELECT * FROM abc;
462 INSERT INTO abc SELECT * FROM abc;
463 INSERT INTO abc SELECT * FROM abc;
464 INSERT INTO abc SELECT * FROM abc;
465 INSERT INTO abc SELECT * FROM abc;
466 INSERT INTO abc SELECT * FROM abc;
467 INSERT INTO abc SELECT * FROM abc;
468 INSERT INTO abc SELECT * FROM abc;
469 INSERT INTO abc SELECT * FROM abc;
470 }
471 expr {[file size test.db]/1024}
472} {43}
danielk19774152e672007-09-12 17:01:45 +0000473ifcapable pager_pragmas {
474 do_test io-4.3.2 {
475 execsql {
476 PRAGMA synchronous = full;
477 PRAGMA cache_size = 10;
478 PRAGMA synchronous;
479 }
480 } {2}
481}
danielk19776897ca32007-08-23 16:27:21 +0000482do_test io-4.3.3 {
483 execsql {
484 BEGIN;
485 UPDATE abc SET a = 'x';
486 }
487 file exists test.db-journal
488} {1}
489do_test io-4.3.4 {
490 # The UPDATE statement in the statement above modifies 41 pages
491 # (all pages in the database except page 1 and the root page of
492 # abc). Because the cache_size is set to 10, this must have required
493 # at least 4 cache-spills. If there were no journal headers written
494 # to the journal file after the cache-spill, then the size of the
495 # journal file is give by:
496 #
497 # <jrnl file size> = <jrnl header size> + nPage * (<page-size> + 8)
498 #
499 # If the journal file contains additional headers, this formula
500 # will not predict the size of the journal file.
501 #
502 file size test.db-journal
503} [expr 1024 + (1024+8)*41]
danielk1977f8940ae2007-08-23 11:07:10 +0000504
danielk19779663b8f2007-08-24 11:52:28 +0000505#----------------------------------------------------------------------
506# Test cases io-5.* test that the default page size is selected and
507# used correctly.
508#
509set tn 0
510foreach {char sectorsize pgsize} {
511 {} 512 1024
512 {} 1024 1024
513 {} 2048 2048
514 {} 8192 8192
515 {} 16384 8192
516 {atomic} 512 8192
517 {atomic512} 512 1024
518 {atomic2K} 512 2048
519 {atomic2K} 4096 4096
520 {atomic2K atomic} 512 8192
521 {atomic64K} 512 1024
522} {
523 incr tn
524 db close
525 file delete -force test.db test.db-journal
526 sqlite3_simulate_device -char $char -sectorsize $sectorsize
527 sqlite3 db test.db
drhd6b93862007-08-29 17:59:42 +0000528 ifcapable !atomicwrite {
529 if {[regexp {^atomic} $char]} continue
530 }
danielk19779663b8f2007-08-24 11:52:28 +0000531 do_test io-5.$tn {
532 execsql {
533 CREATE TABLE abc(a, b, c);
534 }
535 expr {[file size test.db]/2}
536 } $pgsize
537}
538
danielk19772ca0f862007-08-23 08:06:44 +0000539sqlite3_simulate_device -char {} -sectorsize 0
danielk197746abae82007-08-21 13:30:07 +0000540finish_test