1# 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# 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20db close 21sqlite3_simulate_device 22sqlite3 db test.db -vfs devsym 23 24# Test summary: 25# 26# io-1.* - Test that quick-balance does not journal pages unnecessarily. 27# 28# io-2.* - Test the "atomic-write optimization". 29# 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). 33# 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). 38# 39# io-5.* - Test that the default page size is selected and used 40# correctly. 41# 42 43set ::nWrite 0 44proc nWrite {db} { 45 set bt [btree_from_db $db] 46 db_enter $db 47 array set stats [btree_pager_stats $bt] 48 db_leave $db 49 set res [expr $stats(write) - $::nWrite] 50 set ::nWrite $stats(write) 51 set res 52} 53 54set ::nSync 0 55proc nSync {} { 56 set res [expr {$::sqlite_sync_count - $::nSync}] 57 set ::nSync $::sqlite_sync_count 58 set res 59} 60 61do_test io-1.1 { 62 execsql { 63 PRAGMA auto_vacuum = OFF; 64 PRAGMA page_size = 1024; 65 CREATE TABLE abc(a,b); 66 } 67 nWrite db 68} {2} 69 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). 74do_test io-1.2 { 75 set ret [list] 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] 84} {2 2 2 2} 85 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. 90do_test io-1.3 { 91 execsql { INSERT INTO abc VALUES(5,randstr(230,230)); } 92 nWrite db 93} {4} 94 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 97# which are full. 98do_test io-1.4 { 99 set ret [list] 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] 106} {2 2 2} 107 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 111# the new leaf page. 112do_test io-1.5 { 113 execsql { INSERT INTO abc VALUES(9,randstr(230,230)); } 114 nWrite db 115} {3} 116 117ifcapable atomicwrite { 118 119#---------------------------------------------------------------------- 120# Test cases io-2.* test the atomic-write optimization. 121# 122do_test io-2.1 { 123 execsql { DELETE FROM abc; VACUUM; } 124} {} 125 126# Clear the write and sync counts. 127nWrite db ; nSync 128 129# The following INSERT updates 2 pages and requires 4 calls to fsync(): 130# 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. 135# 136do_test io-2.2 { 137 execsql { INSERT INTO abc VALUES(1, 2) } 138 list [nWrite db] [nSync] 139} {2 4} 140 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(). 144# 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. 148# 149# UPDATE: As of [05f98d4eec] (adding SQLITE_DBSTATUS_CACHE_WRITE), the 150# second write is also counted. So this now reports two writes and a 151# single fsync. 152# 153sqlite3_simulate_device -char atomic 154do_test io-2.3 { 155 execsql { INSERT INTO abc VALUES(3, 4) } 156 list [nWrite db] [nSync] 157} {2 1} 158 159# Test that the journal file is not created and the change-counter is 160# updated when the atomic-write optimization is used. 161# 162do_test io-2.4.1 { 163 execsql { 164 BEGIN; 165 INSERT INTO abc VALUES(5, 6); 166 } 167 sqlite3 db2 test.db -vfs devsym 168 execsql { SELECT * FROM abc } db2 169} {1 2 3 4} 170do_test io-2.4.2 { 171 file exists test.db-journal 172} {0} 173do_test io-2.4.3 { 174 execsql { COMMIT } 175 execsql { SELECT * FROM abc } db2 176} {1 2 3 4 5 6} 177db2 close 178 179# Test that the journal file is created and sync()d if the transaction 180# modifies more than one database page, even if the IOCAP_ATOMIC flag 181# is set. 182# 183do_test io-2.5.1 { 184 execsql { CREATE TABLE def(d, e) } 185 nWrite db ; nSync 186 execsql { 187 BEGIN; 188 INSERT INTO abc VALUES(7, 8); 189 } 190 file exists test.db-journal 191} {0} 192do_test io-2.5.2 { 193 execsql { INSERT INTO def VALUES('a', 'b'); } 194 file exists test.db-journal 195} {1} 196do_test io-2.5.3 { 197 execsql { COMMIT } 198 list [nWrite db] [nSync] 199} {3 4} 200 201# Test that the journal file is created and sync()d if the transaction 202# modifies a single database page and also appends a page to the file. 203# Internally, this case is handled differently to the one above. The 204# journal file is not actually created until the 'COMMIT' statement 205# is executed. 206# 207# Changed 2010-03-27: The size of the database is now stored in 208# bytes 28..31 and so when a page is added to the database, page 1 209# is immediately modified and the journal file immediately comes into 210# existance. To fix this test, the BEGIN is changed into a a 211# BEGIN IMMEDIATE and the INSERT is omitted. 212# 213do_test io-2.6.1 { 214 execsql { 215 BEGIN IMMEDIATE; 216 -- INSERT INTO abc VALUES(9, randstr(1000,1000)); 217 } 218 file exists test.db-journal 219} {0} 220do_test io-2.6.2 { 221 # Create a file at "test.db-journal". This will prevent SQLite from 222 # opening the journal for exclusive access. As a result, the COMMIT 223 # should fail with SQLITE_CANTOPEN and the transaction rolled back. 224 # 225 file mkdir test.db-journal 226 catchsql { 227 INSERT INTO abc VALUES(9, randstr(1000,1000)); 228 COMMIT 229 } 230} {1 {unable to open database file}} 231do_test io-2.6.3 { 232 forcedelete test.db-journal 233 catchsql { COMMIT } 234} {0 {}} 235do_test io-2.6.4 { 236 execsql { SELECT * FROM abc } 237} {1 2 3 4 5 6 7 8} 238 239# Test that if the database modification is part of multi-file commit, 240# the journal file is always created. In this case, the journal file 241# is created during execution of the COMMIT statement, so we have to 242# use the same technique to check that it is created as in the above 243# block. 244forcedelete test2.db test2.db-journal 245ifcapable attach { 246 do_test io-2.7.1 { 247 execsql { 248 ATTACH 'test2.db' AS aux; 249 PRAGMA aux.page_size = 1024; 250 CREATE TABLE aux.abc2(a, b); 251 BEGIN; 252 INSERT INTO abc VALUES(9, 10); 253 } 254 file exists test.db-journal 255 } {0} 256 do_test io-2.7.2 { 257 execsql { INSERT INTO abc2 SELECT * FROM abc } 258 file exists test2.db-journal 259 } {0} 260 do_test io-2.7.3 { 261 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 } 262 } {1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10} 263 do_test io-2.7.4 { 264 file mkdir test2.db-journal 265 catchsql { COMMIT } 266 } {1 {unable to open database file}} 267 do_test io-2.7.5 { 268 forcedelete test2.db-journal 269 catchsql { COMMIT } 270 } {1 {cannot commit - no transaction is active}} 271 do_test io-2.7.6 { 272 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 } 273 } {1 2 3 4 5 6 7 8} 274} 275 276# Try an explicit ROLLBACK before the journal file is created. 277# 278do_test io-2.8.1 { 279 execsql { 280 BEGIN; 281 DELETE FROM abc; 282 } 283 file exists test.db-journal 284} {0} 285do_test io-2.8.2 { 286 execsql { SELECT * FROM abc } 287} {} 288do_test io-2.8.3 { 289 execsql { 290 ROLLBACK; 291 SELECT * FROM abc; 292 } 293} {1 2 3 4 5 6 7 8} 294 295# Test that the atomic write optimisation is not enabled if the sector 296# size is larger than the page-size. 297# 298do_test io-2.9.1 { 299 db close 300 sqlite3 db test.db 301 sqlite3_simulate_device -char atomic -sectorsize 2048 302 execsql { 303 BEGIN; 304 INSERT INTO abc VALUES(9, 10); 305 } 306 file exists test.db-journal 307} {1} 308do_test io-2.9.2 { 309 execsql { ROLLBACK; } 310 db close 311 forcedelete test.db test.db-journal 312 sqlite3 db test.db -vfs devsym 313 execsql { 314 PRAGMA auto_vacuum = OFF; 315 PRAGMA page_size = 2048; 316 CREATE TABLE abc(a, b); 317 } 318 execsql { 319 BEGIN; 320 INSERT INTO abc VALUES(9, 10); 321 } 322 file exists test.db-journal 323} {0} 324do_test io-2.9.3 { 325 execsql { COMMIT } 326} {} 327 328# Test a couple of the more specific IOCAP_ATOMIC flags 329# (i.e IOCAP_ATOMIC2K etc.). 330# 331do_test io-2.10.1 { 332 sqlite3_simulate_device -char atomic1k 333 execsql { 334 BEGIN; 335 INSERT INTO abc VALUES(11, 12); 336 } 337 file exists test.db-journal 338} {1} 339do_test io-2.10.2 { 340 execsql { ROLLBACK } 341 sqlite3_simulate_device -char atomic2k 342 execsql { 343 BEGIN; 344 INSERT INTO abc VALUES(11, 12); 345 } 346 file exists test.db-journal 347} {0} 348do_test io-2.10.3 { 349 execsql { ROLLBACK } 350} {} 351 352do_test io-2.11.0 { 353 execsql { 354 PRAGMA locking_mode = exclusive; 355 PRAGMA locking_mode; 356 } 357} {exclusive exclusive} 358do_test io-2.11.1 { 359 execsql { 360 INSERT INTO abc VALUES(11, 12); 361 } 362 file exists test.db-journal 363} {0} 364 365do_test io-2.11.2 { 366 execsql { 367 PRAGMA locking_mode = normal; 368 INSERT INTO abc VALUES(13, 14); 369 } 370 file exists test.db-journal 371} {0} 372 373} ;# /* ifcapable atomicwrite */ 374 375#---------------------------------------------------------------------- 376# Test cases io-3.* test the IOCAP_SEQUENTIAL optimization. 377# 378sqlite3_simulate_device -char sequential -sectorsize 0 379ifcapable pager_pragmas { 380 do_test io-3.1 { 381 db close 382 forcedelete test.db test.db-journal 383 sqlite3 db test.db -vfs devsym 384 db eval { 385 PRAGMA auto_vacuum=OFF; 386 } 387 # File size might be 1 due to the hack to work around ticket #3260. 388 # Search for #3260 in os_unix.c for additional information. 389 expr {[file size test.db]>1} 390 } {0} 391 do_test io-3.2 { 392 execsql { CREATE TABLE abc(a, b) } 393 nSync 394 execsql { 395 PRAGMA temp_store = memory; 396 PRAGMA cache_size = 10; 397 BEGIN; 398 INSERT INTO abc VALUES('hello', 'world'); 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; 406 INSERT INTO abc SELECT * FROM abc; 407 INSERT INTO abc SELECT * FROM abc; 408 INSERT INTO abc SELECT * FROM abc; 409 INSERT INTO abc SELECT * FROM abc; 410 } 411 # File has grown - showing there was a cache-spill - but there 412 # have been no calls to fsync(). The file is probably about 30KB. 413 # But some VFS implementations (symbian) buffer writes so the actual 414 # size may be a little less than that. So this test case just tests 415 # that the file is now greater than 20000 bytes in size. 416 list [expr [file size test.db]>20000] [nSync] 417 } {1 0} 418 do_test io-3.3 { 419 # The COMMIT requires a single fsync() - to the database file. 420 execsql { COMMIT } 421 list [file size test.db] [nSync] 422 } {39936 1} 423} 424 425#---------------------------------------------------------------------- 426# Test cases io-4.* test the IOCAP_SAFE_APPEND optimization. 427# 428sqlite3_simulate_device -char safe_append 429 430# With the SAFE_APPEND flag set, simple transactions require 3, rather 431# than 4, calls to fsync(). The fsync() calls are on: 432# 433# 1) The directory in which the journal file is created, (unix only) 434# 2) The journal file (to sync the page data), 435# 3) The database file. 436# 437# Normally, when the SAFE_APPEND flag is not set, there is another fsync() 438# on the journal file between steps (2) and (3) above. 439# 440set expected_sync_count 2 441if {$::tcl_platform(platform)=="unix"} { 442 ifcapable dirsync { 443 incr expected_sync_count 444 } 445} 446 447do_test io-4.1 { 448 execsql { DELETE FROM abc } 449 nSync 450 execsql { INSERT INTO abc VALUES('a', 'b') } 451 nSync 452} $expected_sync_count 453 454# With SAFE_APPEND set, the nRec field of the journal file header should 455# be set to 0xFFFFFFFF before the first journal sync. The nRec field 456# occupies bytes 8-11 of the journal file. 457# 458do_test io-4.2.1 { 459 execsql { BEGIN } 460 execsql { INSERT INTO abc VALUES('c', 'd') } 461 file exists test.db-journal 462} {1} 463if {$::tcl_platform(platform)=="unix"} { 464 do_test io-4.2.2 { 465 hexio_read test.db-journal 8 4 466 } {FFFFFFFF} 467} 468do_test io-4.2.3 { 469 execsql { COMMIT } 470 nSync 471} $expected_sync_count 472sqlite3_simulate_device -char safe_append 473 474# With SAFE_APPEND set, there should only ever be one journal-header 475# written to the database, even though the sync-mode is "full". 476# 477do_test io-4.3.1 { 478 execsql { 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; 486 INSERT INTO abc SELECT * FROM abc; 487 INSERT INTO abc SELECT * FROM abc; 488 INSERT INTO abc SELECT * FROM abc; 489 INSERT INTO abc SELECT * FROM abc; 490 } 491 expr {[file size test.db]/1024} 492} {43} 493ifcapable pager_pragmas { 494 do_test io-4.3.2 { 495 execsql { 496 PRAGMA synchronous = full; 497 PRAGMA cache_size = 10; 498 PRAGMA synchronous; 499 } 500 } {2} 501} 502do_test io-4.3.3 { 503 execsql { 504 BEGIN; 505 UPDATE abc SET a = 'x'; 506 } 507 file exists test.db-journal 508} {1} 509if {$tcl_platform(platform) != "symbian"} { 510 # This test is not run on symbian because the file-buffer makes it 511 # difficult to predict the exact size of the file as reported by 512 # [file size]. 513 do_test io-4.3.4 { 514 # The UPDATE statement in the statement above modifies 41 pages 515 # (all pages in the database except page 1 and the root page of 516 # abc). Because the cache_size is set to 10, this must have required 517 # at least 4 cache-spills. If there were no journal headers written 518 # to the journal file after the cache-spill, then the size of the 519 # journal file is give by: 520 # 521 # <jrnl file size> = <jrnl header size> + nPage * (<page-size> + 8) 522 # 523 # If the journal file contains additional headers, this formula 524 # will not predict the size of the journal file. 525 # 526 file size test.db-journal 527 } [expr 512 + (1024+8)*41] 528} 529 530#---------------------------------------------------------------------- 531# Test cases io-5.* test that the default page size is selected and 532# used correctly. 533# 534set tn 0 535foreach {char sectorsize pgsize} { 536 {} 512 1024 537 {} 1024 1024 538 {} 2048 2048 539 {} 8192 8192 540 {} 16384 8192 541 {atomic} 512 8192 542 {atomic512} 512 1024 543 {atomic2K} 512 2048 544 {atomic2K} 4096 4096 545 {atomic2K atomic} 512 8192 546 {atomic64K} 512 1024 547} { 548 incr tn 549 if {$pgsize>$::SQLITE_MAX_PAGE_SIZE} continue 550 db close 551 forcedelete test.db test.db-journal 552 sqlite3_simulate_device -char $char -sectorsize $sectorsize 553 sqlite3 db test.db -vfs devsym 554 db eval { 555 PRAGMA auto_vacuum=OFF; 556 } 557 ifcapable !atomicwrite { 558 if {[regexp {^atomic} $char]} continue 559 } 560 do_test io-5.$tn { 561 execsql { 562 CREATE TABLE abc(a, b, c); 563 } 564 expr {[file size test.db]/2} 565 } $pgsize 566} 567 568sqlite3_simulate_device -char {} -sectorsize 0 569finish_test 570