Bug 164974

Summary: IndexedDB 2.0: Cache and reuse SQLiteStatements in the SQLite backend
Product: WebKit Reporter: Brady Eidson <beidson>
Component: WebCore Misc.Assignee: Brady Eidson <beidson>
Status: RESOLVED FIXED    
Severity: Normal CC: achristensen, alecflett, buildbot, cdumez, commit-queue, jsbell, rniwa
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=165222
https://bugs.webkit.org/show_bug.cgi?id=165216
Bug Depends on:    
Bug Blocks: 160306    
Attachments:
Description Flags
Patch
none
Patch
none
Archive of layout-test-results from ews105 for mac-yosemite-wk2
none
Patch none

Description Brady Eidson 2016-11-18 17:23:19 PST
IndexedDB 2.0: Cache and reuse SQLiteStatements in the SQLite backend

sqlite_prepare_v2 is so hot that it's showing up at 15-20% on profiles of *one* specific test I'm working with.

Yikes!

Caching and re-using statements is an obvious win.
Comment 1 Brady Eidson 2016-11-29 14:21:20 PST
Created attachment 295640 [details]
Patch
Comment 2 Brady Eidson 2016-11-29 14:26:13 PST
This caught two patches in the upload, whoops. Sending only one now...
Comment 3 Brady Eidson 2016-11-29 14:27:03 PST
Created attachment 295642 [details]
Patch
Comment 4 Alex Christensen 2016-11-29 14:36:40 PST
Comment on attachment 295642 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=295642&action=review

r=me

> Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:2383
> +    return m_cachedStatements[static_cast<int>(sql)].get();

This should be bounds checked just to be extra careful, with an ASSERT_NOT_REACHED of some sort.
Also, size_t would be more appropriate than int.
Comment 5 Build Bot 2016-11-29 15:16:42 PST
Comment on attachment 295642 [details]
Patch

Attachment 295642 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.webkit.org/results/2591094

Number of test failures exceeded the failure limit.
Comment 6 Build Bot 2016-11-29 15:16:45 PST
Created attachment 295652 [details]
Archive of layout-test-results from ews105 for mac-yosemite-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews105  Port: mac-yosemite-wk2  Platform: Mac OS X 10.10.5
Comment 7 Brady Eidson 2016-11-29 15:26:25 PST
Those failures are due to the classic "w3c web server stopped working" issue.
Comment 8 Brady Eidson 2016-11-29 15:28:38 PST
https://trac.webkit.org/changeset/209096
Comment 9 Brady Eidson 2016-11-29 17:13:06 PST
This caused issues under Guardmalloc with deadlocked TransactionOperations.

Landing a stopgap fix until I can further explore why they aren't ref'ed
Comment 10 Brady Eidson 2016-11-29 17:16:53 PST
(In reply to comment #9)
> This caused issues under Guardmalloc with deadlocked TransactionOperations.
> 
> Landing a stopgap fix until I can further explore why they aren't ref'ed

Landed this in https://trac.webkit.org/changeset/209104
Comment 11 Brady Eidson 2016-11-29 19:46:01 PST
(In reply to comment #10)
> (In reply to comment #9)
> > This caused issues under Guardmalloc with deadlocked TransactionOperations.
> > 
> > Landing a stopgap fix until I can further explore why they aren't ref'ed
> 
> Landed this in https://trac.webkit.org/changeset/209104

This fix caused storage/indexeddb/modern/gc-closes-database.html and storage/indexeddb/modern/gc-closes-database-private.html to start timing out (I'm sure of why, also)

I'll skip them until I can resolve this mess tomorrow.
Comment 12 Brady Eidson 2016-11-29 19:51:53 PST
(In reply to comment #11)
> (In reply to comment #10)
> > (In reply to comment #9)
> > > This caused issues under Guardmalloc with deadlocked TransactionOperations.
> > > 
> > > Landing a stopgap fix until I can further explore why they aren't ref'ed
> > 
> > Landed this in https://trac.webkit.org/changeset/209104
> 
> This fix caused storage/indexeddb/modern/gc-closes-database.html and
> storage/indexeddb/modern/gc-closes-database-private.html to start timing out
> (I'm sure of why, also)
> 
> I'll skip them until I can resolve this mess tomorrow.

Skipped in https://trac.webkit.org/changeset/209114

Resolving the GuardMalloc *and* leak issue will happen over in bug https://bugs.webkit.org/show_bug.cgi?id=165186
Comment 13 Chris Dumez 2016-11-29 20:37:27 PST
Reverted r209096, r209104, and r209114 for reason:

Caused over 200 layout test failures on the bots

Committed r209117: <http://trac.webkit.org/changeset/209117>
Comment 14 Chris Dumez 2016-11-29 20:37:32 PST
Reverted r209096, r209104, and r209114 for reason:

Caused over 200 layout test failures on the bots

Committed r209117: <http://trac.webkit.org/changeset/209117>
Comment 15 Chris Dumez 2016-11-29 20:38:22 PST
(In reply to comment #14)
> Reverted r209096, r209104, and r209114 for reason:
> 
> Caused over 200 layout test failures on the bots
> 
> Committed r209117: <http://trac.webkit.org/changeset/209117>

E.g.
https://build.webkit.org/builders/Apple%20El%20Capitan%20Release%20WK2%20%28Tests%29/builds/11350
Comment 16 Chris Dumez 2016-11-29 20:40:42 PST
(In reply to comment #15)
> (In reply to comment #14)
> > Reverted r209096, r209104, and r209114 for reason:
> > 
> > Caused over 200 layout test failures on the bots
> > 
> > Committed r209117: <http://trac.webkit.org/changeset/209117>
> 
> E.g.
> https://build.webkit.org/builders/
> Apple%20El%20Capitan%20Release%20WK2%20%28Tests%29/builds/11350

Note that https://trac.webkit.org/changeset/209104 did not help fix those layout test failures.
Comment 17 Brady Eidson 2016-11-29 23:33:57 PST
(In reply to comment #16)
> (In reply to comment #15)
> > (In reply to comment #14)
> > > Reverted r209096, r209104, and r209114 for reason:
> > > 
> > > Caused over 200 layout test failures on the bots
> > > 
> > > Committed r209117: <http://trac.webkit.org/changeset/209117>
> > 
> > E.g.
> > https://build.webkit.org/builders/
> > Apple%20El%20Capitan%20Release%20WK2%20%28Tests%29/builds/11350
> 
> Note that https://trac.webkit.org/changeset/209104 did not help fix those
> layout test failures.

It wasn't meant to help fix those, which I didn't even know were happening.

It *was* meant to fix GuardMalloc failures from a previous changeset that I guess was now reintroduced.

It's somewhat surprising this change caused all that, but whatever - I'll sort it out in the A.M.
Comment 18 Chris Dumez 2016-11-30 08:14:39 PST
(In reply to comment #17)
> (In reply to comment #16)
> > (In reply to comment #15)
> > > (In reply to comment #14)
> > > > Reverted r209096, r209104, and r209114 for reason:
> > > > 
> > > > Caused over 200 layout test failures on the bots
> > > > 
> > > > Committed r209117: <http://trac.webkit.org/changeset/209117>
> > > 
> > > E.g.
> > > https://build.webkit.org/builders/
> > > Apple%20El%20Capitan%20Release%20WK2%20%28Tests%29/builds/11350
> > 
> > Note that https://trac.webkit.org/changeset/209104 did not help fix those
> > layout test failures.
> 
> It wasn't meant to help fix those, which I didn't even know were happening.
> 
> It *was* meant to fix GuardMalloc failures from a previous changeset that I
> guess was now reintroduced.

I rolled out all 3 changesets so I do not think I reintroduced failures but I could be wrong.

> 
> It's somewhat surprising this change caused all that, but whatever - I'll
> sort it out in the A.M.

Looks like the roll out was successful on the bots:
- Before: https://build.webkit.org/builders/Apple%20Yosemite%20Release%20WK2%20%28Tests%29/builds/19663
- After: https://build.webkit.org/builders/Apple%20Yosemite%20Release%20WK2%20%28Tests%29/builds/19664

The remaining unit test failure was caused by another change which was rolled out later on and we are green as of:
https://build.webkit.org/builders/Apple%20Yosemite%20Release%20WK2%20%28Tests%29/builds/19665
Comment 19 Brady Eidson 2016-11-30 10:23:17 PST
I can reproduce locally, only with the w3c tests, after a certain number of tests have run in a given process.

e.g.:
run-webkit-tests --child-processes=1 imported/w3c/web-platform-tests/IndexedDB/idbcursor_update_index3.htm --iterations=20000

Hammers the same test over and over, and after 68 runs it starts failing.

lsof confirms tons of handles to databases from past tests are still open, so opening new database files fails (limit to the number of file handles per process)

I'm still unclear how this change caused leaking file handles, but there's not much surface to go over.
Comment 20 Brady Eidson 2016-11-30 10:23:43 PST
(In reply to comment #19)
> I can reproduce locally, only with the w3c tests, after a certain number of
> tests have run in a given process.
> 
> e.g.:
> run-webkit-tests --child-processes=1
> imported/w3c/web-platform-tests/IndexedDB/idbcursor_update_index3.htm
> --iterations=20000
> 
> Hammers the same test over and over, and after 68 runs it starts failing.
> 
> lsof confirms tons of handles to databases from past tests are still open,
> so opening new database files fails (limit to the number of file handles per
> process)
> 
> I'm still unclear how this change caused leaking file handles, but there's
> not much surface to go over.

It's also quite unclear why this *doesn't* affect DRT, but only WKTR.  o_O
Comment 21 Brady Eidson 2016-11-30 10:25:40 PST
(In reply to comment #20)
> (In reply to comment #19)
> > I can reproduce locally, only with the w3c tests, after a certain number of
> > tests have run in a given process.
> > 
> > e.g.:
> > run-webkit-tests --child-processes=1
> > imported/w3c/web-platform-tests/IndexedDB/idbcursor_update_index3.htm
> > --iterations=20000
> > 
> > Hammers the same test over and over, and after 68 runs it starts failing.
> > 
> > lsof confirms tons of handles to databases from past tests are still open,
> > so opening new database files fails (limit to the number of file handles per
> > process)
> > 
> > I'm still unclear how this change caused leaking file handles, but there's
> > not much surface to go over.
> 
> It's also quite unclear why this *doesn't* affect DRT, but only WKTR.  o_O

DRT is leaking the handles, also. This must be "app process" vs "XPC service process" limitations.

DRT *does* start failing after enough runs (850 instead of 68)
Comment 22 Brady Eidson 2016-11-30 10:35:33 PST
Okay figured it out - If you close a sqlite3 handle while you still have prepared statements open, it leaks the file handles. o_O

Exact same patch with a couple of sqlite cleanup calls moved around fixes this.
Comment 23 Brady Eidson 2016-11-30 10:39:05 PST
Created attachment 295730 [details]
Patch
Comment 24 WebKit Commit Bot 2016-11-30 11:59:22 PST
Comment on attachment 295730 [details]
Patch

Clearing flags on attachment: 295730

Committed r209144: <http://trac.webkit.org/changeset/209144>
Comment 25 WebKit Commit Bot 2016-11-30 11:59:27 PST
All reviewed patches have been landed.  Closing bug.