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

[STFS] Add critical section to file reading #2123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Gliniak
Copy link
Member

@Gliniak Gliniak commented Feb 5, 2023

Affected titles:
xenia-project/game-compatibility#827
xenia-project/game-compatibility#125

Description:
These titles use multiple threads to load same file, which in case of extracted file doesn't make any issue, however for STFS package it is huge problem as it can randomly receive incorrect data.

I checked it multiple times and calculated hashes of loaded data between extracted and packed game.
Conclusion for me is simple, at certain point thread starts getting invalid data, which is simply resolved by including critical section

Seems like reading same file concurrently from many threads
can cause situation when random thread receives incorrect data
auto& file = entry_->files()->at(record.file);
xe::filesystem::Seek(file, record.offset + read_offset, SEEK_SET);
auto num_read = fread(p, 1, read_length, file);
auto global_lock = global_critical_region_.Acquire();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a race between guess threads accessing the same guest file handle, or different guest handles implemented through one host file handle? If that's the latter, we should be opening the host container file multiple times, for each guest handle, or if possible, use a memory mapping. Using the global critical region here will create a lot of contention that may block other threads for milliseconds — the comment in xenia/base/mutex.h explicitly says that no I/O must be done there.

Copy link
Member Author

@Gliniak Gliniak Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh my bad for not reading comments in mutex.h.
It uses multiple handles, so rewrites then to allow memory mapping probably (if possible)

Copy link
Member

@gibbed gibbed Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's strange, I thought it was already using memory mapping for STFS? Maybe I'm forgetting when it got changed. I know I wanted the capability to do both, since we need the ability to load STFS packages nested within a non-host filesystem (ie, such as another STFS package).

@Margen67 Margen67 added the vfs label Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants