Cache2 I/O thread 100% CPU due to O(n) linear search in CacheIndexIterator::RemoveRecord with large cache
Categories
(Core :: Networking: Cache, defect, P2)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox152 | --- | fixed |
People
(Reporter: onajugutqv, Assigned: leggert, NeedInfo)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved, Whiteboard: [necko-priority-queue][necko-triaged])
Attachments
(1 file, 1 external file)
|
3.52 KB,
text/markdown
|
Details |
User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0
Steps to reproduce:
- Used Firefox normally for an extended period without clearing cache
- Cache accumulated 811,561 entries (~1.9GB) over time
- Checked cache size with:
ls ~/.cache/mozilla/firefox/*.default*/cache2/entries/ | wc -l - Opened about:processes to monitor CPU usage
Actual results:
- Firefox main process shows 130%+ CPU usage continuously
- The "Cache2 I/O" thread is consuming all CPU (confirmed via
perf record) - Firefox becomes sluggish and nearly unusable
- CPU usage persists indefinitely until cache is manually cleared
Expected results:
- Cache operations should complete in reasonable time
- CPU usage should remain low during normal browsing
- Cache management should scale efficiently with large number of entries
Comment 1•9 months ago
|
||
The rest of the AI-formatted bug report is in the attachment; pasting here for convenience and readability:
Root Cause Analysis
Using Linux perf record profiling, I identified the hot spot in CacheIndexIterator.cpp:
// netwerk/cache2/CacheIndexIterator.cpp:91
bool CacheIndexIterator::RemoveRecord(CacheIndexRecordWrapper* aRecord,
const StaticMutexAutoLock& aProofOfLock) {
return mRecords.RemoveElement(aRecord); // O(n) linear search!
}
mRecords is a plain nsTArray. With 811,561 cache entries, every cache update triggers an O(n) linear search, causing CPU saturation.
Disassembly of the hot loop confirms this is a simple pointer comparison loop:
54f4e94: cmp %rsi,0x8(%rax,%rdx,1) ; compare element
54f4e9b: add $0x8,%rdx ; index += 8
54f4e9f: cmp %rdx,%rcx ; check bounds
54f4ea2: jne 54f4e94 ; loop
Environment
- Firefox 147.0.2
- Arch Linux x86_64, kernel 6.18.7-arch1-1
- Cache entries: 811,561 files (~1.9GB)
Perf Profile Data
Top functions from perf report:
48.43% Cache2 I/O libxul.so [.] 0x00000000054f4e49
37.98% Cache2 I/O libxul.so [.] 0x00000000054f4e45
2.54% Cache2 I/O libxul.so [.] 0x00000000054f4e42
strace shows heavy mprotect/mmap activity:
61.58% mprotect
34.27% mmap
Suggested Fix
Replace nsTArray with nsTHashtable in CacheIndexIterator::mRecords for O(1) lookup/removal.
Related Bugs
- Bug 1082735 - CacheIndex::InsertRecordToFrecencyArray uses InsertElementSorted (FIXED)
- Bug 1248389 - Cache index causing CPU loops (FIXED in Firefox 47)
Both bugs addressed similar symptoms but different code paths. This bug affects
CacheIndexIterator::RemoveRecord() which was NOT fixed in those bugs.
Workaround
Clearing the cache via about:preferences#privacy resolves the issue temporarily.
Comment 2•9 months ago
|
||
This is a performance problem, not a security vulnerability
Comment 3•9 months ago
|
||
Note that we also do an O(n) iteration here mozilla::net::CacheIndexIterator::CloseInternal, which we should probably fix.
Replace nsTArray with nsTHashtable in CacheIndexIterator::mRecords for O(1) lookup/removal.
That probably makes sense.
I was wondering if we need the iterator to still be FIFO - see mozilla::net::CacheIndexIterator::GetNextHash - but I think since the records get popped when calling GetNext that ensures we eventually iterate all records that get added to the iterator.
And this method only gets called from mozilla::net::CacheFileContextEvictor::EvictEntries and mozilla::net::CacheStorageServiceInternal::WalkDiskCacheRunnable::Run where order doesn't seem to matter.
Comment 4•7 months ago
|
||
We could use a profile of this. It's also odd that you would have >800K entries in your cache of 1.9GB -- I checked about a dozen profiles, some very heavily used (7K tabs, 30 windows, used constantly) and they had no more than 20K entries with 1GB used.
Is there anything unusual about your setup? Any unusual extensions?
If you have the linux perf profile, that would help (our tools including profiler.firefox.com can ingest that)
| Assignee | ||
Comment 5•7 months ago
|
||
Updated•7 months ago
|
Comment 8•7 months ago
|
||
Reverted this because it was causing failures in PLDHashTable.h.
- Revert link
- Push with failures
- Failure Log
- Failure line: Assertion failure: IsIdle(oldState), at checkouts/gecko/xpcom/ds/PLDHashTable.h:141
Comment 10•7 months ago
|
||
| bugherder | ||
Comment 11•7 months ago
•
|
||
rjesup, does this profile have the same function in cache2 I/O thread on the latest nightly : https://share.firefox.dev/4vMXOPr ?
And another: https://share.firefox.dev/3OF14fd
| Assignee | ||
Updated•7 months ago
|
Description
•