Add DelayGenerator for all file backends

This commit is contained in:
B3n30 2018-02-24 14:14:54 +01:00
parent 58b16c5459
commit 06a7676ed1
13 changed files with 172 additions and 71 deletions

View file

@ -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

View file

@ -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<DelayGenerator> 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<u64>(static_cast<u64>(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<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));
}

View file

@ -77,14 +77,17 @@ ResultVal<std::unique_ptr<FileBackend>> NCCHArchive::OpenFile(const Path& path,
u64 romfs_size = 0;
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 ||
filepath_type == NCCHFilePathType::ExeFS) {
std::vector<u8> buffer;
// Load NCCH .code or icon/banner/logo
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 {
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<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 {
LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length);

View file

@ -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<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> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;

View file

@ -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<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFile(const Path& path,
const Mode& mode) const {
Mode modified_mode;
@ -82,7 +96,8 @@ ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFileBase(const Path& pa
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));
}
@ -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());
}

View file

@ -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<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
u64 GetSize() const override {
return data->size();
}
@ -182,8 +171,11 @@ public:
private:
ResultVal<std::unique_ptr<FileBackend>> OpenRomFS() const {
if (ncch_data.romfs_file) {
return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
ncch_data.romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size));
std::unique_ptr<DelayGenerator> delay_generator =
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 {
LOG_INFO(Service_FS, "Unable to read RomFS");
return ERROR_ROMFS_NOT_FOUND;
@ -192,9 +184,11 @@ private:
ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const {
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>(
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<FileUtil::IOFile> 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);
}

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

@ -36,20 +36,6 @@ ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const b
return MakeResult<size_t>(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<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
u64 DiskFile::GetSize() const {
return file->GetSize();
}

View file

@ -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<DelayGenerator> delay_generator_)
: file(new FileUtil::IOFile(std::move(file_))) {
delay_generator = std::move(delay_generator_);
mode.hex = mode_.hex;
}
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;
u64 GetReadDelayNs(size_t length) const override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override;

View file

@ -8,6 +8,7 @@
#include <cstddef>
#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<u64>(static_cast<u64>(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<DefaultDelayGenerator>();
return delay_generator->GetReadDelayNs(length);
}
/**
@ -79,6 +76,9 @@ public:
* Flushes the file
*/
virtual void Flush() const = 0;
protected:
std::unique_ptr<DelayGenerator> delay_generator;
};
} // namespace FileSys

View file

@ -23,8 +23,9 @@ std::string IVFCArchive::GetName() const {
ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path,
const Mode& mode) const {
std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<IVFCDelayGenerator>();
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 {
@ -89,8 +90,11 @@ u64 IVFCArchive::GetFreeBytes() const {
////////////////////////////////////////////////////////////////////////////////////////////////////
IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
: romfs_file(std::move(file)), data_offset(offset), data_size(size) {}
IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 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 {
LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length);
@ -107,17 +111,6 @@ ResultVal<size_t> IVFCFile::Write(const u64 offset, const size_t length, const b
return MakeResult<size_t>(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<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
u64 IVFCFile::GetSize() const {
return data_size;
}

View file

@ -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<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
* 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<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> 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 {

View file

@ -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<u64>(static_cast<u64>(length) * slope + offset, minimum);
return IPCDelayNanoseconds;
}
};
ResultVal<std::unique_ptr<FileBackend>> 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<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& pa
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));
}