3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
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).
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
21 sqlite3_simulate_device
22 sqlite3 db test.db -vfs devsym
26 # io-1.* - Test that quick-balance does not journal pages unnecessarily.
28 # io-2.* - Test the "atomic-write optimization".
30 # io-3.* - Test the IO traffic enhancements triggered when the
31 # IOCAP_SEQUENTIAL device capability flag is set (no
32 # fsync() calls on the journal file).
34 # io-4.* - Test the IO traffic enhancements triggered when the
35 # IOCAP_SAFE_APPEND device capability flag is set (fewer
36 # fsync() calls on the journal file, no need to set nRec
37 # field in the single journal header).
39 # io-5.* - Test that the default page size is selected and used
45 set bt [btree_from_db $db]
47 array set stats [btree_pager_stats $bt]
49 set res [expr $stats(write) - $::nWrite]
50 set ::nWrite $stats(write)
56 set res [expr {$::sqlite_sync_count - $::nSync}]
57 set ::nSync $::sqlite_sync_count
63 PRAGMA auto_vacuum = OFF;
64 PRAGMA page_size = 1024;
65 CREATE TABLE abc(a,b);
70 # Insert into the table 4 records of aproximately 240 bytes each.
71 # This should completely fill the root-page of the table. Each
72 # INSERT causes 2 db pages to be written - the root-page of "abc"
73 # and page 1 (db change-counter page).
76 execsql { INSERT INTO abc VALUES(1,randstr(230,230)); }
77 lappend ret [nWrite db]
78 execsql { INSERT INTO abc VALUES(2,randstr(230,230)); }
79 lappend ret [nWrite db]
80 execsql { INSERT INTO abc VALUES(3,randstr(230,230)); }
81 lappend ret [nWrite db]
82 execsql { INSERT INTO abc VALUES(4,randstr(230,230)); }
83 lappend ret [nWrite db]
86 # Insert another 240 byte record. This causes two leaf pages
87 # to be added to the root page of abc. 4 pages in total
88 # are written to the db file - the two leaf pages, the root
89 # of abc and the change-counter page.
91 execsql { INSERT INTO abc VALUES(5,randstr(230,230)); }
95 # Insert another 3 240 byte records. After this, the tree consists of
96 # the root-node, which is close to empty, and two leaf pages, both of
100 execsql { INSERT INTO abc VALUES(6,randstr(230,230)); }
101 lappend ret [nWrite db]
102 execsql { INSERT INTO abc VALUES(7,randstr(230,230)); }
103 lappend ret [nWrite db]
104 execsql { INSERT INTO abc VALUES(8,randstr(230,230)); }
105 lappend ret [nWrite db]
108 # This insert should use the quick-balance trick to add a third leaf
109 # to the b-tree used to store table abc. It should only be necessary to
110 # write to 3 pages to do this: the change-counter, the root-page and
113 execsql { INSERT INTO abc VALUES(9,randstr(230,230)); }
117 ifcapable atomicwrite {
119 #----------------------------------------------------------------------
120 # Test cases io-2.* test the atomic-write optimization.
123 execsql { DELETE FROM abc; VACUUM; }
126 # Clear the write and sync counts.
129 # The following INSERT updates 2 pages and requires 4 calls to fsync():
131 # 1) The directory in which the journal file is created,
132 # 2) The journal file (to sync the page data),
133 # 3) The journal file (to sync the journal file header),
134 # 4) The database file.
137 execsql { INSERT INTO abc VALUES(1, 2) }
138 list [nWrite db] [nSync]
141 # Set the device-characteristic mask to include the SQLITE_IOCAP_ATOMIC,
142 # then do another INSERT similar to the one in io-2.2. This should
143 # only write 1 page and require a single fsync().
145 # The single fsync() is the database file. Only one page is reported as
146 # written because page 1 - the change-counter page - is written using
147 # an out-of-band method that bypasses the write counter.
149 sqlite3_simulate_device -char atomic
151 execsql { INSERT INTO abc VALUES(3, 4) }
152 list [nWrite db] [nSync]
155 # Test that the journal file is not created and the change-counter is
156 # updated when the atomic-write optimization is used.
161 INSERT INTO abc VALUES(5, 6);
163 sqlite3 db2 test.db -vfs devsym
164 execsql { SELECT * FROM abc } db2
167 file exists test.db-journal
171 execsql { SELECT * FROM abc } db2
175 # Test that the journal file is created and sync()d if the transaction
176 # modifies more than one database page, even if the IOCAP_ATOMIC flag
180 execsql { CREATE TABLE def(d, e) }
184 INSERT INTO abc VALUES(7, 8);
186 file exists test.db-journal
189 execsql { INSERT INTO def VALUES('a', 'b'); }
190 file exists test.db-journal
194 list [nWrite db] [nSync]
197 # Test that the journal file is created and sync()d if the transaction
198 # modifies a single database page and also appends a page to the file.
199 # Internally, this case is handled differently to the one above. The
200 # journal file is not actually created until the 'COMMIT' statement
203 # Changed 2010-03-27: The size of the database is now stored in
204 # bytes 28..31 and so when a page is added to the database, page 1
205 # is immediately modified and the journal file immediately comes into
206 # existance. To fix this test, the BEGIN is changed into a a
207 # BEGIN IMMEDIATE and the INSERT is omitted.
212 -- INSERT INTO abc VALUES(9, randstr(1000,1000));
214 file exists test.db-journal
217 # Create a file at "test.db-journal". This will prevent SQLite from
218 # opening the journal for exclusive access. As a result, the COMMIT
219 # should fail with SQLITE_CANTOPEN and the transaction rolled back.
221 file mkdir test.db-journal
223 INSERT INTO abc VALUES(9, randstr(1000,1000));
226 } {1 {unable to open database file}}
228 file delete -force test.db-journal
232 execsql { SELECT * FROM abc }
235 # Test that if the database modification is part of multi-file commit,
236 # the journal file is always created. In this case, the journal file
237 # is created during execution of the COMMIT statement, so we have to
238 # use the same technique to check that it is created as in the above
240 file delete -force test2.db test2.db-journal
244 ATTACH 'test2.db' AS aux;
245 PRAGMA aux.page_size = 1024;
246 CREATE TABLE aux.abc2(a, b);
248 INSERT INTO abc VALUES(9, 10);
250 file exists test.db-journal
253 execsql { INSERT INTO abc2 SELECT * FROM abc }
254 file exists test2.db-journal
257 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
258 } {1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10}
260 file mkdir test2.db-journal
262 } {1 {unable to open database file}}
264 file delete -force test2.db-journal
266 } {1 {cannot commit - no transaction is active}}
268 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
272 # Try an explicit ROLLBACK before the journal file is created.
279 file exists test.db-journal
282 execsql { SELECT * FROM abc }
291 # Test that the atomic write optimisation is not enabled if the sector
292 # size is larger than the page-size.
297 sqlite3_simulate_device -char atomic -sectorsize 2048
300 INSERT INTO abc VALUES(9, 10);
302 file exists test.db-journal
305 execsql { ROLLBACK; }
307 file delete -force test.db test.db-journal
308 sqlite3 db test.db -vfs devsym
310 PRAGMA auto_vacuum = OFF;
311 PRAGMA page_size = 2048;
312 CREATE TABLE abc(a, b);
316 INSERT INTO abc VALUES(9, 10);
318 file exists test.db-journal
324 # Test a couple of the more specific IOCAP_ATOMIC flags
325 # (i.e IOCAP_ATOMIC2K etc.).
328 sqlite3_simulate_device -char atomic1k
331 INSERT INTO abc VALUES(11, 12);
333 file exists test.db-journal
337 sqlite3_simulate_device -char atomic2k
340 INSERT INTO abc VALUES(11, 12);
342 file exists test.db-journal
350 PRAGMA locking_mode = exclusive;
353 } {exclusive exclusive}
356 INSERT INTO abc VALUES(11, 12);
358 file exists test.db-journal
363 PRAGMA locking_mode = normal;
364 INSERT INTO abc VALUES(13, 14);
366 file exists test.db-journal
369 } ;# /* ifcapable atomicwrite */
371 #----------------------------------------------------------------------
372 # Test cases io-3.* test the IOCAP_SEQUENTIAL optimization.
374 sqlite3_simulate_device -char sequential -sectorsize 0
375 ifcapable pager_pragmas {
378 file delete -force test.db test.db-journal
379 sqlite3 db test.db -vfs devsym
381 PRAGMA auto_vacuum=OFF;
383 # File size might be 1 due to the hack to work around ticket #3260.
384 # Search for #3260 in os_unix.c for additional information.
385 expr {[file size test.db]>1}
388 execsql { CREATE TABLE abc(a, b) }
391 PRAGMA temp_store = memory;
392 PRAGMA cache_size = 10;
394 INSERT INTO abc VALUES('hello', 'world');
395 INSERT INTO abc SELECT * FROM abc;
396 INSERT INTO abc SELECT * FROM abc;
397 INSERT INTO abc SELECT * FROM abc;
398 INSERT INTO abc SELECT * FROM abc;
399 INSERT INTO abc SELECT * FROM abc;
400 INSERT INTO abc SELECT * FROM abc;
401 INSERT INTO abc SELECT * FROM abc;
402 INSERT INTO abc SELECT * FROM abc;
403 INSERT INTO abc SELECT * FROM abc;
404 INSERT INTO abc SELECT * FROM abc;
405 INSERT INTO abc SELECT * FROM abc;
407 # File has grown - showing there was a cache-spill - but there
408 # have been no calls to fsync(). The file is probably about 30KB.
409 # But some VFS implementations (symbian) buffer writes so the actual
410 # size may be a little less than that. So this test case just tests
411 # that the file is now greater than 20000 bytes in size.
412 list [expr [file size test.db]>20000] [nSync]
415 # The COMMIT requires a single fsync() - to the database file.
417 list [file size test.db] [nSync]
421 #----------------------------------------------------------------------
422 # Test cases io-4.* test the IOCAP_SAFE_APPEND optimization.
424 sqlite3_simulate_device -char safe_append
426 # With the SAFE_APPEND flag set, simple transactions require 3, rather
427 # than 4, calls to fsync(). The fsync() calls are on:
429 # 1) The directory in which the journal file is created, (unix only)
430 # 2) The journal file (to sync the page data),
431 # 3) The database file.
433 # Normally, when the SAFE_APPEND flag is not set, there is another fsync()
434 # on the journal file between steps (2) and (3) above.
436 set expected_sync_count 2
437 if {$::tcl_platform(platform)=="unix"} {
439 incr expected_sync_count
444 execsql { DELETE FROM abc }
446 execsql { INSERT INTO abc VALUES('a', 'b') }
448 } $expected_sync_count
450 # With SAFE_APPEND set, the nRec field of the journal file header should
451 # be set to 0xFFFFFFFF before the first journal sync. The nRec field
452 # occupies bytes 8-11 of the journal file.
456 execsql { INSERT INTO abc VALUES('c', 'd') }
457 file exists test.db-journal
459 if {$::tcl_platform(platform)=="unix"} {
461 hexio_read test.db-journal 8 4
467 } $expected_sync_count
468 sqlite3_simulate_device -char safe_append
470 # With SAFE_APPEND set, there should only ever be one journal-header
471 # written to the database, even though the sync-mode is "full".
475 INSERT INTO abc SELECT * FROM abc;
476 INSERT INTO abc SELECT * FROM abc;
477 INSERT INTO abc SELECT * FROM abc;
478 INSERT INTO abc SELECT * FROM abc;
479 INSERT INTO abc SELECT * FROM abc;
480 INSERT INTO abc SELECT * FROM abc;
481 INSERT INTO abc SELECT * FROM abc;
482 INSERT INTO abc SELECT * FROM abc;
483 INSERT INTO abc SELECT * FROM abc;
484 INSERT INTO abc SELECT * FROM abc;
485 INSERT INTO abc SELECT * FROM abc;
487 expr {[file size test.db]/1024}
489 ifcapable pager_pragmas {
492 PRAGMA synchronous = full;
493 PRAGMA cache_size = 10;
501 UPDATE abc SET a = 'x';
503 file exists test.db-journal
505 if {$tcl_platform(platform) != "symbian"} {
506 # This test is not run on symbian because the file-buffer makes it
507 # difficult to predict the exact size of the file as reported by
510 # The UPDATE statement in the statement above modifies 41 pages
511 # (all pages in the database except page 1 and the root page of
512 # abc). Because the cache_size is set to 10, this must have required
513 # at least 4 cache-spills. If there were no journal headers written
514 # to the journal file after the cache-spill, then the size of the
515 # journal file is give by:
517 # <jrnl file size> = <jrnl header size> + nPage * (<page-size> + 8)
519 # If the journal file contains additional headers, this formula
520 # will not predict the size of the journal file.
522 file size test.db-journal
523 } [expr 512 + (1024+8)*41]
526 #----------------------------------------------------------------------
527 # Test cases io-5.* test that the default page size is selected and
531 foreach {char sectorsize pgsize} {
541 {atomic2K atomic} 512 8192
545 if {$pgsize>$::SQLITE_MAX_PAGE_SIZE} continue
547 file delete -force test.db test.db-journal
548 sqlite3_simulate_device -char $char -sectorsize $sectorsize
549 sqlite3 db test.db -vfs devsym
551 PRAGMA auto_vacuum=OFF;
553 ifcapable !atomicwrite {
554 if {[regexp {^atomic} $char]} continue
558 CREATE TABLE abc(a, b, c);
560 expr {[file size test.db]/2}
564 sqlite3_simulate_device -char {} -sectorsize 0