From 06a7676ed1241046546df9d516dabd0a445aed48 Mon Sep 17 00:00:00 2001 From: B3n30 Date: Sat, 24 Feb 2018 14:14:54 +0100 Subject: [PATCH] Add DelayGenerator for all file backends --- src/core/CMakeLists.txt | 1 + src/core/file_sys/archive_extsavedata.cpp | 23 ++++++++++-- src/core/file_sys/archive_ncch.cpp | 12 +++++-- src/core/file_sys/archive_ncch.h | 4 +-- src/core/file_sys/archive_sdmc.cpp | 18 +++++++++- src/core/file_sys/archive_selfncch.cpp | 33 ++++++++--------- src/core/file_sys/delay_generator.h | 29 +++++++++++++++ src/core/file_sys/disk_archive.cpp | 14 -------- src/core/file_sys/disk_archive.h | 5 +-- src/core/file_sys/file_backend.h | 22 ++++++------ src/core/file_sys/ivfc_archive.cpp | 21 ++++------- src/core/file_sys/ivfc_archive.h | 44 +++++++++++++++++++++-- src/core/file_sys/savedata_archive.cpp | 17 ++++++++- 13 files changed, 172 insertions(+), 71 deletions(-) create mode 100644 src/core/file_sys/delay_generator.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index f838b6b04..4afeec200 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -57,6 +57,7 @@ add_library(core STATIC file_sys/disk_archive.h file_sys/errors.h file_sys/file_backend.h + file_sys/delay_generator.h file_sys/ivfc_archive.cpp file_sys/ivfc_archive.h file_sys/ncch_container.cpp diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp index 77a735292..4066461fe 100644 --- a/src/core/file_sys/archive_extsavedata.cpp +++ b/src/core/file_sys/archive_extsavedata.cpp @@ -27,7 +27,9 @@ namespace FileSys { */ class FixSizeDiskFile : public DiskFile { public: - FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) { + FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode, + std::unique_ptr delay_generator_) + : DiskFile(std::move(file), mode, std::move(delay_generator_)) { size = GetSize(); } @@ -53,6 +55,20 @@ private: 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 IPCDelayNanoseconds = std::max(static_cast(length) * slope + offset, minimum); + return IPCDelayNanoseconds; + } +}; + /** * Archive backend for general extsave data archive type. * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for @@ -118,7 +134,10 @@ public: Mode rwmode; rwmode.write_flag.Assign(1); rwmode.read_flag.Assign(1); - auto disk_file = std::make_unique(std::move(file), rwmode); + std::unique_ptr delay_generator = + std::make_unique(); + auto disk_file = + std::make_unique(std::move(file), rwmode, std::move(delay_generator)); return MakeResult>(std::move(disk_file)); } diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp index 2c4ed2d45..6dd1f384a 100644 --- a/src/core/file_sys/archive_ncch.cpp +++ b/src/core/file_sys/archive_ncch.cpp @@ -77,14 +77,17 @@ ResultVal> NCCHArchive::OpenFile(const Path& path, u64 romfs_size = 0; result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size); - file = std::make_unique(std::move(romfs_file), romfs_offset, romfs_size); + std::unique_ptr delay_generator = std::make_unique(); + file = std::make_unique(std::move(romfs_file), romfs_offset, romfs_size, + std::move(delay_generator)); } else if (filepath_type == NCCHFilePathType::Code || filepath_type == NCCHFilePathType::ExeFS) { std::vector buffer; // Load NCCH .code or icon/banner/logo result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer); - file = std::make_unique(std::move(buffer)); + std::unique_ptr delay_generator = std::make_unique(); + file = std::make_unique(std::move(buffer), std::move(delay_generator)); } else { LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type); result = Loader::ResultStatus::Error; @@ -194,7 +197,10 @@ u64 NCCHArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// -NCCHFile::NCCHFile(std::vector buffer) : file_buffer(std::move(buffer)) {} +NCCHFile::NCCHFile(std::vector buffer, std::unique_ptr delay_generator_) + : file_buffer(std::move(buffer)) { + delay_generator = std::move(delay_generator_); +} ResultVal NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const { LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length); diff --git a/src/core/file_sys/archive_ncch.h b/src/core/file_sys/archive_ncch.h index f2ba888d8..c62d27f7c 100644 --- a/src/core/file_sys/archive_ncch.h +++ b/src/core/file_sys/archive_ncch.h @@ -17,7 +17,7 @@ namespace Service { namespace FS { enum class MediaType : u32; } -} +} // namespace Service namespace FileSys { @@ -51,7 +51,7 @@ protected: // File backend for NCCH files class NCCHFile : public FileBackend { public: - explicit NCCHFile(std::vector buffer); + explicit NCCHFile(std::vector buffer, std::unique_ptr delay_generator_); ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) override; diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp index fe3dce5d4..0d0c725e9 100644 --- a/src/core/file_sys/archive_sdmc.cpp +++ b/src/core/file_sys/archive_sdmc.cpp @@ -17,6 +17,20 @@ 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(static_cast(length) * slope + offset, minimum); + return IPCDelayNanoseconds; + } +}; + ResultVal> SDMCArchive::OpenFile(const Path& path, const Mode& mode) const { Mode modified_mode; @@ -82,7 +96,8 @@ ResultVal> SDMCArchive::OpenFileBase(const Path& pa return ERROR_NOT_FOUND; } - auto disk_file = std::make_unique(std::move(file), mode); + std::unique_ptr delay_generator = std::make_unique(); + auto disk_file = std::make_unique(std::move(file), mode, std::move(delay_generator)); return MakeResult>(std::move(disk_file)); } @@ -343,6 +358,7 @@ u64 SDMCArchive::GetFreeBytes() const { ArchiveFactory_SDMC::ArchiveFactory_SDMC(const std::string& sdmc_directory) : sdmc_directory(sdmc_directory) { + LOG_DEBUG(Service_FS, "Directory %s set as SDMC.", sdmc_directory.c_str()); } diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp index 32d5fff23..e5864953d 100644 --- a/src/core/file_sys/archive_selfncch.cpp +++ b/src/core/file_sys/archive_selfncch.cpp @@ -56,17 +56,6 @@ public: return ERROR_UNSUPPORTED_OPEN_FLAGS; } - u64 GetReadDelayNs(size_t length) const { - // 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(static_cast(length) * slope + offset, minimum); - return IPCDelayNanoseconds; - } - u64 GetSize() const override { return data->size(); } @@ -182,8 +171,11 @@ public: private: ResultVal> OpenRomFS() const { if (ncch_data.romfs_file) { - return MakeResult>(std::make_unique( - ncch_data.romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size)); + std::unique_ptr delay_generator = + std::make_unique(); + return MakeResult>( + std::make_unique(ncch_data.romfs_file, ncch_data.romfs_offset, + ncch_data.romfs_size, std::move(delay_generator))); } else { LOG_INFO(Service_FS, "Unable to read RomFS"); return ERROR_ROMFS_NOT_FOUND; @@ -192,9 +184,11 @@ private: ResultVal> OpenUpdateRomFS() const { if (ncch_data.update_romfs_file) { + std::unique_ptr delay_generator = + std::make_unique(); return MakeResult>(std::make_unique( 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 { LOG_INFO(Service_FS, "Unable to read update RomFS"); return ERROR_ROMFS_NOT_FOUND; @@ -251,8 +245,9 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) { program_id); if (ncch_data.find(program_id) != ncch_data.end()) { - LOG_WARNING(Service_FS, "Registering program %016" PRIX64 - " with SelfNCCH will override existing mapping", + LOG_WARNING(Service_FS, + "Registering program %016" PRIX64 + " with SelfNCCH will override existing mapping", program_id); } @@ -266,9 +261,9 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) { } std::shared_ptr update_romfs_file; - if (Loader::ResultStatus::Success == - app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset, - data.update_romfs_size)) { + if (Loader::ResultStatus::Success == app_loader.ReadUpdateRomFS(update_romfs_file, + data.update_romfs_offset, + data.update_romfs_size)) { data.update_romfs_file = std::move(update_romfs_file); } diff --git a/src/core/file_sys/delay_generator.h b/src/core/file_sys/delay_generator.h new file mode 100644 index 000000000..410f60ee8 --- /dev/null +++ b/src/core/file_sys/delay_generator.h @@ -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(static_cast(length) * slope + offset, minimum); + return IPCDelayNanoseconds; + } +}; + +} // namespace FileSys diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp index 0fae8301f..677f92467 100644 --- a/src/core/file_sys/disk_archive.cpp +++ b/src/core/file_sys/disk_archive.cpp @@ -36,20 +36,6 @@ ResultVal DiskFile::Write(const u64 offset, const size_t length, const b return MakeResult(written); } -u64 DiskFile::GetReadDelayNs(size_t length) const { - // TODO(B3N30): figure out the time a 3ds needs for those write - // for that backend. - // For now take the results from the romfs test. - // 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(static_cast(length) * slope + offset, minimum); - return IPCDelayNanoseconds; -} - u64 DiskFile::GetSize() const { return file->GetSize(); } diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index 2c22da661..0de3747a1 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -22,14 +22,15 @@ namespace FileSys { class DiskFile : public FileBackend { public: - DiskFile(FileUtil::IOFile&& file_, const Mode& mode_) + DiskFile(FileUtil::IOFile&& file_, const Mode& mode_, + std::unique_ptr delay_generator_) : file(new FileUtil::IOFile(std::move(file_))) { + delay_generator = std::move(delay_generator_); mode.hex = mode_.hex; } ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) override; - u64 GetReadDelayNs(size_t length) const override; u64 GetSize() const override; bool SetSize(u64 size) const override; bool Close() const override; diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h index 003e24429..182e6e7b1 100644 --- a/src/core/file_sys/file_backend.h +++ b/src/core/file_sys/file_backend.h @@ -8,6 +8,7 @@ #include #include "common/common_types.h" #include "core/hle/result.h" +#include "delay_generator.h" //////////////////////////////////////////////////////////////////////////////////////////////////// // FileSys namespace @@ -43,17 +44,13 @@ public: * @param length Length in bytes of data read from file * @return Nanoseconds for the delay */ - virtual u64 GetReadDelayNs(size_t length) const { - // Return the default delay for the case that the subclass backend didn't - // implement one. We take the one measured for romfs reads - // This should be removed as soon as every subclass backend - // has one implemented - LOG_WARNING(Service_FS, "Using default delay for read"); - static constexpr u64 slope(94); - static constexpr u64 offset(582778); - static constexpr u64 minimum(663124); - u64 IPCDelayNanoseconds = std::max(static_cast(length) * slope + offset, minimum); - return IPCDelayNanoseconds; + 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(); + return delay_generator->GetReadDelayNs(length); } /** @@ -79,6 +76,9 @@ public: * Flushes the file */ virtual void Flush() const = 0; + +protected: + std::unique_ptr delay_generator; }; } // namespace FileSys diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp index 18b260487..15c2cc383 100644 --- a/src/core/file_sys/ivfc_archive.cpp +++ b/src/core/file_sys/ivfc_archive.cpp @@ -23,8 +23,9 @@ std::string IVFCArchive::GetName() const { ResultVal> IVFCArchive::OpenFile(const Path& path, const Mode& mode) const { + std::unique_ptr delay_generator = std::make_unique(); return MakeResult>( - std::make_unique(romfs_file, data_offset, data_size)); + std::make_unique(romfs_file, data_offset, data_size, std::move(delay_generator))); } ResultCode IVFCArchive::DeleteFile(const Path& path) const { @@ -89,8 +90,11 @@ u64 IVFCArchive::GetFreeBytes() const { //////////////////////////////////////////////////////////////////////////////////////////////////// -IVFCFile::IVFCFile(std::shared_ptr file, u64 offset, u64 size) - : romfs_file(std::move(file)), data_offset(offset), data_size(size) {} +IVFCFile::IVFCFile(std::shared_ptr file, u64 offset, u64 size, + std::unique_ptr delay_generator_) + : romfs_file(std::move(file)), data_offset(offset), data_size(size) { + delay_generator = std::move(delay_generator_); +} ResultVal IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const { LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length); @@ -107,17 +111,6 @@ ResultVal IVFCFile::Write(const u64 offset, const size_t length, const b return MakeResult(0); } -u64 IVFCFile::GetReadDelayNs(size_t length) const { - // 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(static_cast(length) * slope + offset, minimum); - return IPCDelayNanoseconds; -} - u64 IVFCFile::GetSize() const { return data_size; } diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h index a6f26199c..d21379b57 100644 --- a/src/core/file_sys/ivfc_archive.h +++ b/src/core/file_sys/ivfc_archive.h @@ -19,6 +19,46 @@ 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(static_cast(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(static_cast(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(static_cast(length) * slope + offset, minimum); + return IPCDelayNanoseconds; + } +}; + /** * 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 @@ -50,11 +90,11 @@ protected: class IVFCFile : public FileBackend { public: - IVFCFile(std::shared_ptr file, u64 offset, u64 size); + IVFCFile(std::shared_ptr file, u64 offset, u64 size, + std::unique_ptr delay_generator_); ResultVal Read(u64 offset, size_t length, u8* buffer) const override; ResultVal Write(u64 offset, size_t length, bool flush, const u8* buffer) override; - u64 GetReadDelayNs(size_t length) const override; u64 GetSize() const override; bool SetSize(u64 size) const override; bool Close() const override { diff --git a/src/core/file_sys/savedata_archive.cpp b/src/core/file_sys/savedata_archive.cpp index f8f811ba0..52a935cbc 100644 --- a/src/core/file_sys/savedata_archive.cpp +++ b/src/core/file_sys/savedata_archive.cpp @@ -13,6 +13,20 @@ 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(static_cast(length) * slope + offset, minimum); + return IPCDelayNanoseconds; + } +}; + ResultVal> SaveDataArchive::OpenFile(const Path& path, const Mode& mode) const { LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex); @@ -67,7 +81,8 @@ ResultVal> SaveDataArchive::OpenFile(const Path& pa return ERROR_FILE_NOT_FOUND; } - auto disk_file = std::make_unique(std::move(file), mode); + std::unique_ptr delay_generator = std::make_unique(); + auto disk_file = std::make_unique(std::move(file), mode, std::move(delay_generator)); return MakeResult>(std::move(disk_file)); }