Closed Bug 2014160 Opened 9 months ago Closed 7 months ago

Cache2 I/O thread 100% CPU due to O(n) linear search in CacheIndexIterator::RemoveRecord with large cache

Categories

(Core :: Networking: Cache, defect, P2)

Firefox 147
defect

Tracking

()

RESOLVED FIXED
152 Branch
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)

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0

Steps to reproduce:

  1. Used Firefox normally for an extended period without clearing cache
  2. Cache accumulated 811,561 entries (~1.9GB) over time
  3. Checked cache size with: ls ~/.cache/mozilla/firefox/*.default*/cache2/entries/ | wc -l
  4. 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

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.

Group: core-security → network-core-security
Keywords: ai-involved

This is a performance problem, not a security vulnerability

Group: network-core-security

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.

Blocks: necko-perf
Severity: -- → S3
Priority: -- → P2
Whiteboard: [necko-priority-queue][necko-triaged]

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)

Flags: needinfo?(onajugutqv)
Assignee: nobody → leggert
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
See Also: → 1959090
Pushed by sstanca@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/1ddd0c4b3781 https://hg.mozilla.org/integration/autoland/rev/ccbe847c8835 Revert "Bug 2014160 - Use nsTHashSet in CacheIndexIterator::mRecords r=jesup,necko-reviewers" for causing failures in PLDHashTable.h.

Reverted this because it was causing failures in PLDHashTable.h.

Flags: needinfo?(leggert)
Status: ASSIGNED → RESOLVED
Closed: 7 months ago
Resolution: --- → FIXED
Target Milestone: --- → 152 Branch

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

Flags: needinfo?(rjesup)
Flags: needinfo?(leggert)
See Also: → 2035809
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: