citra-emu
/
citra-canary
Archived
1
0
Fork 0

Merge pull request #3440 from B3n30/file_read_delay

File_Sys: Add a size dependent delay for each file read
This commit is contained in:
James Rowe 2018-03-04 21:32:50 -07:00 committed by GitHub
commit f3234db5e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 184 additions and 17 deletions

View File

@ -57,6 +57,7 @@ add_library(core STATIC
file_sys/disk_archive.h file_sys/disk_archive.h
file_sys/errors.h file_sys/errors.h
file_sys/file_backend.h file_sys/file_backend.h
file_sys/delay_generator.h
file_sys/ivfc_archive.cpp file_sys/ivfc_archive.cpp
file_sys/ivfc_archive.h file_sys/ivfc_archive.h
file_sys/ncch_container.cpp file_sys/ncch_container.cpp

View File

@ -27,7 +27,9 @@ namespace FileSys {
*/ */
class FixSizeDiskFile : public DiskFile { class FixSizeDiskFile : public DiskFile {
public: public:
FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) { FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode,
std::unique_ptr<DelayGenerator> delay_generator_)
: DiskFile(std::move(file), mode, std::move(delay_generator_)) {
size = GetSize(); size = GetSize();
} }
@ -53,6 +55,21 @@ private:
u64 size{}; u64 size{};
}; };
class ExtSaveDataDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// This is the delay measured for a savedate read,
// not for extsaveData
// For now we will take that
static constexpr u64 slope(183);
static constexpr u64 offset(524879);
static constexpr u64 minimum(631826);
u64 ipc_delay_nanoseconds =
std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return ipc_delay_nanoseconds;
}
};
/** /**
* Archive backend for general extsave data archive type. * Archive backend for general extsave data archive type.
* The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for
@ -118,7 +135,10 @@ public:
Mode rwmode; Mode rwmode;
rwmode.write_flag.Assign(1); rwmode.write_flag.Assign(1);
rwmode.read_flag.Assign(1); rwmode.read_flag.Assign(1);
auto disk_file = std::make_unique<FixSizeDiskFile>(std::move(file), rwmode); std::unique_ptr<DelayGenerator> delay_generator =
std::make_unique<ExtSaveDataDelayGenerator>();
auto disk_file =
std::make_unique<FixSizeDiskFile>(std::move(file), rwmode, std::move(delay_generator));
return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file)); return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
} }

View File

@ -77,14 +77,17 @@ ResultVal<std::unique_ptr<FileBackend>> NCCHArchive::OpenFile(const Path& path,
u64 romfs_size = 0; u64 romfs_size = 0;
result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size);
file = std::make_unique<IVFCFile>(std::move(romfs_file), romfs_offset, romfs_size); std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<RomFSDelayGenerator>();
file = std::make_unique<IVFCFile>(std::move(romfs_file), romfs_offset, romfs_size,
std::move(delay_generator));
} else if (filepath_type == NCCHFilePathType::Code || } else if (filepath_type == NCCHFilePathType::Code ||
filepath_type == NCCHFilePathType::ExeFS) { filepath_type == NCCHFilePathType::ExeFS) {
std::vector<u8> buffer; std::vector<u8> buffer;
// Load NCCH .code or icon/banner/logo // Load NCCH .code or icon/banner/logo
result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer); result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer);
file = std::make_unique<NCCHFile>(std::move(buffer)); std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<ExeFSDelayGenerator>();
file = std::make_unique<NCCHFile>(std::move(buffer), std::move(delay_generator));
} else { } else {
LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type); LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type);
result = Loader::ResultStatus::Error; result = Loader::ResultStatus::Error;
@ -194,7 +197,10 @@ u64 NCCHArchive::GetFreeBytes() const {
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
NCCHFile::NCCHFile(std::vector<u8> buffer) : file_buffer(std::move(buffer)) {} NCCHFile::NCCHFile(std::vector<u8> buffer, std::unique_ptr<DelayGenerator> delay_generator_)
: file_buffer(std::move(buffer)) {
delay_generator = std::move(delay_generator_);
}
ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const { ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const {
LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length); LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length);

View File

@ -17,7 +17,7 @@ namespace Service {
namespace FS { namespace FS {
enum class MediaType : u32; enum class MediaType : u32;
} }
} } // namespace Service
namespace FileSys { namespace FileSys {
@ -51,7 +51,7 @@ protected:
// File backend for NCCH files // File backend for NCCH files
class NCCHFile : public FileBackend { class NCCHFile : public FileBackend {
public: public:
explicit NCCHFile(std::vector<u8> buffer); explicit NCCHFile(std::vector<u8> buffer, std::unique_ptr<DelayGenerator> delay_generator_);
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override; ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;

View File

@ -17,6 +17,20 @@
namespace FileSys { namespace FileSys {
class SDMCDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// This is the delay measured on O3DS and O2DS with
// https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
// from the results the average of each length was taken.
static constexpr u64 slope(183);
static constexpr u64 offset(524879);
static constexpr u64 minimum(631826);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFile(const Path& path, ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFile(const Path& path,
const Mode& mode) const { const Mode& mode) const {
Mode modified_mode; Mode modified_mode;
@ -82,7 +96,8 @@ ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFileBase(const Path& pa
return ERROR_NOT_FOUND; return ERROR_NOT_FOUND;
} }
auto disk_file = std::make_unique<DiskFile>(std::move(file), mode); std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<SDMCDelayGenerator>();
auto disk_file = std::make_unique<DiskFile>(std::move(file), mode, std::move(delay_generator));
return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file)); return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
} }
@ -343,6 +358,7 @@ u64 SDMCArchive::GetFreeBytes() const {
ArchiveFactory_SDMC::ArchiveFactory_SDMC(const std::string& sdmc_directory) ArchiveFactory_SDMC::ArchiveFactory_SDMC(const std::string& sdmc_directory)
: sdmc_directory(sdmc_directory) { : sdmc_directory(sdmc_directory) {
LOG_DEBUG(Service_FS, "Directory %s set as SDMC.", sdmc_directory.c_str()); LOG_DEBUG(Service_FS, "Directory %s set as SDMC.", sdmc_directory.c_str());
} }

View File

@ -171,8 +171,11 @@ public:
private: private:
ResultVal<std::unique_ptr<FileBackend>> OpenRomFS() const { ResultVal<std::unique_ptr<FileBackend>> OpenRomFS() const {
if (ncch_data.romfs_file) { if (ncch_data.romfs_file) {
return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>( std::unique_ptr<DelayGenerator> delay_generator =
ncch_data.romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size)); std::make_unique<RomFSDelayGenerator>();
return MakeResult<std::unique_ptr<FileBackend>>(
std::make_unique<IVFCFile>(ncch_data.romfs_file, ncch_data.romfs_offset,
ncch_data.romfs_size, std::move(delay_generator)));
} else { } else {
LOG_INFO(Service_FS, "Unable to read RomFS"); LOG_INFO(Service_FS, "Unable to read RomFS");
return ERROR_ROMFS_NOT_FOUND; return ERROR_ROMFS_NOT_FOUND;
@ -181,9 +184,11 @@ private:
ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const { ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const {
if (ncch_data.update_romfs_file) { if (ncch_data.update_romfs_file) {
std::unique_ptr<DelayGenerator> delay_generator =
std::make_unique<RomFSDelayGenerator>();
return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>( return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
ncch_data.update_romfs_file, ncch_data.update_romfs_offset, ncch_data.update_romfs_file, ncch_data.update_romfs_offset,
ncch_data.update_romfs_size)); ncch_data.update_romfs_size, std::move(delay_generator)));
} else { } else {
LOG_INFO(Service_FS, "Unable to read update RomFS"); LOG_INFO(Service_FS, "Unable to read update RomFS");
return ERROR_ROMFS_NOT_FOUND; return ERROR_ROMFS_NOT_FOUND;

View File

@ -0,0 +1,29 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
namespace FileSys {
class DelayGenerator {
public:
virtual u64 GetReadDelayNs(size_t length) = 0;
// TODO (B3N30): Add getter for all other file/directory io operations
};
class DefaultDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// This is the delay measured for a romfs read.
// For now we will take that as a default
static constexpr u64 slope(94);
static constexpr u64 offset(582778);
static constexpr u64 minimum(663124);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
} // namespace FileSys

View File

@ -22,8 +22,10 @@ namespace FileSys {
class DiskFile : public FileBackend { class DiskFile : public FileBackend {
public: public:
DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) DiskFile(FileUtil::IOFile&& file_, const Mode& mode_,
std::unique_ptr<DelayGenerator> delay_generator_)
: file(new FileUtil::IOFile(std::move(file_))) { : file(new FileUtil::IOFile(std::move(file_))) {
delay_generator = std::move(delay_generator_);
mode.hex = mode_.hex; mode.hex = mode_.hex;
} }

View File

@ -4,9 +4,12 @@
#pragma once #pragma once
#include <algorithm>
#include <cstddef> #include <cstddef>
#include <memory>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/hle/result.h" #include "core/hle/result.h"
#include "delay_generator.h"
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// FileSys namespace // FileSys namespace
@ -37,6 +40,20 @@ public:
*/ */
virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) = 0; virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) = 0;
/**
* Get the amount of time a 3ds needs to read those data
* @param length Length in bytes of data read from file
* @return Nanoseconds for the delay
*/
u64 GetReadDelayNs(size_t length) {
if (delay_generator != nullptr) {
return delay_generator->GetReadDelayNs(length);
}
LOG_ERROR(Service_FS, "Delay generator was not initalized. Using default");
delay_generator = std::make_unique<DefaultDelayGenerator>();
return delay_generator->GetReadDelayNs(length);
}
/** /**
* Get the size of the file in bytes * Get the size of the file in bytes
* @return Size of the file in bytes * @return Size of the file in bytes
@ -60,6 +77,9 @@ public:
* Flushes the file * Flushes the file
*/ */
virtual void Flush() const = 0; virtual void Flush() const = 0;
protected:
std::unique_ptr<DelayGenerator> delay_generator;
}; };
} // namespace FileSys } // namespace FileSys

View File

@ -23,8 +23,9 @@ std::string IVFCArchive::GetName() const {
ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path, ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path,
const Mode& mode) const { const Mode& mode) const {
std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<IVFCDelayGenerator>();
return MakeResult<std::unique_ptr<FileBackend>>( return MakeResult<std::unique_ptr<FileBackend>>(
std::make_unique<IVFCFile>(romfs_file, data_offset, data_size)); std::make_unique<IVFCFile>(romfs_file, data_offset, data_size, std::move(delay_generator)));
} }
ResultCode IVFCArchive::DeleteFile(const Path& path) const { ResultCode IVFCArchive::DeleteFile(const Path& path) const {
@ -89,8 +90,11 @@ u64 IVFCArchive::GetFreeBytes() const {
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size) IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size,
: romfs_file(std::move(file)), data_offset(offset), data_size(size) {} std::unique_ptr<DelayGenerator> delay_generator_)
: romfs_file(std::move(file)), data_offset(offset), data_size(size) {
delay_generator = std::move(delay_generator_);
}
ResultVal<size_t> IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const { ResultVal<size_t> IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const {
LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length); LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length);

View File

@ -19,6 +19,46 @@
namespace FileSys { namespace FileSys {
class IVFCDelayGenerator : public DelayGenerator {
u64 GetReadDelayNs(size_t length) override {
// This is the delay measured for a romfs read.
// For now we will take that as a default
static constexpr u64 slope(94);
static constexpr u64 offset(582778);
static constexpr u64 minimum(663124);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
class RomFSDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// The delay was measured on O3DS and O2DS with
// https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
// from the results the average of each length was taken.
static constexpr u64 slope(94);
static constexpr u64 offset(582778);
static constexpr u64 minimum(663124);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
class ExeFSDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// The delay was measured on O3DS and O2DS with
// https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
// from the results the average of each length was taken.
static constexpr u64 slope(94);
static constexpr u64 offset(582778);
static constexpr u64 minimum(663124);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
/** /**
* Helper which implements an interface to deal with IVFC images used in some archives * Helper which implements an interface to deal with IVFC images used in some archives
* This should be subclassed by concrete archive types, which will provide the * This should be subclassed by concrete archive types, which will provide the
@ -50,7 +90,8 @@ protected:
class IVFCFile : public FileBackend { class IVFCFile : public FileBackend {
public: public:
IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size); IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size,
std::unique_ptr<DelayGenerator> delay_generator_);
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override; ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override; ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;

View File

@ -13,6 +13,20 @@
namespace FileSys { namespace FileSys {
class SaveDataDelayGenerator : public DelayGenerator {
public:
u64 GetReadDelayNs(size_t length) override {
// The delay was measured on O3DS and O2DS with
// https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
// from the results the average of each length was taken.
static constexpr u64 slope(183);
static constexpr u64 offset(524879);
static constexpr u64 minimum(631826);
u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
ResultVal<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& path, ResultVal<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& path,
const Mode& mode) const { const Mode& mode) const {
LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
@ -67,7 +81,8 @@ ResultVal<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& pa
return ERROR_FILE_NOT_FOUND; return ERROR_FILE_NOT_FOUND;
} }
auto disk_file = std::make_unique<DiskFile>(std::move(file), mode); std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<SaveDataDelayGenerator>();
auto disk_file = std::make_unique<DiskFile>(std::move(file), mode, std::move(delay_generator));
return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file)); return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
} }

View File

@ -31,6 +31,7 @@
#include "core/hle/ipc_helpers.h" #include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_port.h"
#include "core/hle/kernel/client_session.h" #include "core/hle/kernel/client_session.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/server_session.h"
#include "core/hle/result.h" #include "core/hle/result.h"
@ -104,6 +105,13 @@ void File::Read(Kernel::HLERequestContext& ctx) {
rb.Push<u32>(*read); rb.Push<u32>(*read);
} }
rb.PushMappedBuffer(buffer); rb.PushMappedBuffer(buffer);
std::chrono::nanoseconds read_timeout_ns{backend->GetReadDelayNs(length)};
ctx.SleepClientThread(Kernel::GetCurrentThread(), "file::read", read_timeout_ns,
[](Kernel::SharedPtr<Kernel::Thread> thread,
Kernel::HLERequestContext& ctx, ThreadWakeupReason reason) {
// Nothing to do here
});
} }
void File::Write(Kernel::HLERequestContext& ctx) { void File::Write(Kernel::HLERequestContext& ctx) {