Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SharedCache] Rework how file accessors are handled #6392

Closed
wants to merge 1 commit into from

Conversation

bdash
Copy link
Contributor

@bdash bdash commented Feb 5, 2025

Previously, MMappedFileAccessor::Open attempted to impose a fixed limit on the number of file accessors that were live at one time. This was done because the default file descriptor limit on some platforms is relatively low (256 on macOS). Given that recent iOS shared caches can contain 60+ files it is easy to tie up a large percentage of this limit by opening one or two shared caches.

This is problematic as if the limit imposed by MMappedFileAccessor::Open is reached, the attempt to access the file will block waiting for another file accessor to be closed. This can lead to a deadlock.

This commit makes three changes to this strategy:

  1. It attempts to raise the file descriptor limit to 1024. Unix systems support both soft and hard file descriptor limits. The soft file descriptor limit is what is enforced, but a process can explicitly raise the limit to any value below the hard limit if it wishes.

  2. The fixed limit on open files accessors is changed to a soft limit. FileAccessorCache is introduced to manage the caching of file accessors. It provides a basic LRU cache. Whenever a new file is opened, the cache of open accessors is pruned to stay below the target limit (50% of the soft file descriptor limit). This limiting is primarily done to allow files containing dirty pages to be unmapped if they're no longer being used.

    LRU isn't the optimal strategy for this, but it is simple to implement and understand. Ideally there'd be some access time component to the cache so files that haven't been accessed can be released.

  3. File accessors are cached even for sessions corresponding to views that have been closed. The "Open Selection with Options..." context menu hits this case as a view is created and destroyed as part of populating the options dialog, and the real view is then created in the same session.

The most significant benefit of this change is that the logic around opening / closing file accessors is simpler and can no longer deadlock.

While working on this I noticed that SharedCache opened some files via MMappedFileAccessor::Open without specifying a post-open operation to apply slide information. Instead, it would explicitly apply the slide information after opening the file. This works fine so long as the file accessor limit is never hit. If it is hit and the accessor is closed, the next time the file is opened it will not have any slide information applied. This would lead to very confusing bugs.

Previously, `MMappedFileAccessor::Open` attempted to impose a fixed limit
on the number of file accessors that were live at one time. This was
done because the default file descriptor limit on some platforms is
relatively low (256 on macOS). Given that recent iOS shared caches can
contain 60+ files it is easy to tie up a large percentage of this limit
by opening one or two shared caches.

This is problematic as if the limit imposed by `MMappedFileAccessor::Open`
is reached, the attempt to access the file will block waiting for
another file accessor to be closed. This can lead to a deadlock.

This commit makes three changes to this strategy:
1. It attempts to raise the file descriptor limit to 1024. Unix systems
   support both soft and hard file descriptor limits. The soft file
   descriptor limit is what is enforced, but a process can explicitly
   raise the limit to any value below the hard limit if it wishes.
2. The fixed limit on open files accessors is changed to a soft limit.
   `FileAccessorCache` is introduced to manage the caching of file
   accessors. It provides a basic LRU cache. Whenever a new file is
   opened, the cache of open accessors is pruned to stay below the
   target limit (50% of the soft file descriptor limit). This limiting
   is primarily done to allow files containing dirty pages to be
   unmapped if they're no longer being used.

   LRU isn't the optimal strategy for this, but it is simple to
   implement and understand. Ideally there'd be some access time
   component to the cache so files that haven't been accessed can be
   released.
3. File accessors are cached even for sessions corresponding to views
   that have been closed. The "Open Selection with Options..." context
   menu hits this case as a view is created and destroyed as part of
   populating the options dialog, and the real view is then created in
   the same session.

The most significant benefit of this change is that the logic around
opening / closing file accessors is simpler and can no longer deadlock.

While working on this I noticed that `SharedCache` opened some files via
`MMappedFileAccessor::Open` without specifying a post-open operation to
apply slide information. Instead, it would explicitly apply the slide
information after opening the file. This works fine so long as the file
accessor limit is never hit. If it is hit and the accessor is closed,
the next time the file is opened it will not have any slide information
applied. This would lead to very confusing bugs.
@plafosse plafosse requested a review from 0cyn February 5, 2025 13:50
@0cyn 0cyn requested review from emesare and removed request for 0cyn February 11, 2025 10:41
@0cyn 0cyn assigned emesare and unassigned 0cyn Feb 11, 2025
@bdash
Copy link
Contributor Author

bdash commented Feb 13, 2025

This was cherry-picked into dev via 1ffdd4d.

@bdash bdash closed this Feb 13, 2025
@bdash bdash deleted the dsc-file-accessors branch February 13, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants