mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-12-18 08:22:04 +00:00
creport: optimize ScopedFile performance
This commit is contained in:
parent
027e209073
commit
009f581721
4 changed files with 56 additions and 11 deletions
|
@ -26,6 +26,8 @@ namespace ams::creport {
|
|||
static_assert(DyingMessageAddressOffset == AMS_OFFSETOF(ams::svc::aarch64::ProcessLocalRegion, dying_message_region_address));
|
||||
static_assert(DyingMessageAddressOffset == AMS_OFFSETOF(ams::svc::aarch32::ProcessLocalRegion, dying_message_region_address));
|
||||
|
||||
constexpr size_t CrashReportDataCacheSize = 256_KB;
|
||||
|
||||
/* Helper functions. */
|
||||
bool TryGetCurrentTimestamp(u64 *out) {
|
||||
/* Clear output. */
|
||||
|
@ -307,7 +309,15 @@ namespace ams::creport {
|
|||
/* Save crash report. */
|
||||
util::SNPrintf(file_path, sizeof(file_path), "sdmc:/atmosphere/crash_reports/%011lu_%016lx.log", timestamp, m_process_info.program_id);
|
||||
{
|
||||
ScopedFile file(file_path);
|
||||
/* Try to allocate data cache. */
|
||||
void * const data_cache = lmem::AllocateFromExpHeap(m_heap_handle, CrashReportDataCacheSize + os::MemoryPageSize);
|
||||
ON_SCOPE_EXIT { if (data_cache != nullptr) { lmem::FreeToExpHeap(m_heap_handle, data_cache); } };
|
||||
|
||||
/* Align up the data cache. This is safe because null will align up to null. */
|
||||
void * const aligned_cache = reinterpret_cast<void *>(util::AlignUp(reinterpret_cast<uintptr_t>(data_cache), os::MemoryPageSize));
|
||||
|
||||
/* Open and save the file using the cache. */
|
||||
ScopedFile file(file_path, aligned_cache, aligned_cache != nullptr ? CrashReportDataCacheSize : 0);
|
||||
if (file.IsOpen()) {
|
||||
this->SaveToFile(file);
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace ams::creport {
|
|||
}
|
||||
|
||||
void ModuleList::SaveToFile(ScopedFile &file) {
|
||||
file.WriteFormat(" Number of Modules: %zu\n", m_num_modules);
|
||||
file.WriteFormat(" Number of Modules: %02zu\n", m_num_modules);
|
||||
for (size_t i = 0; i < m_num_modules; i++) {
|
||||
const auto& module = m_modules[i];
|
||||
file.WriteFormat(" Module %02zu:\n", i);
|
||||
|
|
|
@ -60,22 +60,24 @@ namespace ams::creport {
|
|||
|
||||
/* Print the line prefix. */
|
||||
if (first) {
|
||||
this->WriteFormat("%s", prefix);
|
||||
this->Write(prefix, prefix_len);
|
||||
first = false;
|
||||
} else {
|
||||
this->WriteFormat("%*s", prefix_len, "");
|
||||
std::memset(g_format_buffer, ' ', prefix_len);
|
||||
this->Write(g_format_buffer, prefix_len);
|
||||
}
|
||||
|
||||
/* Dump up to 0x20 of hex memory. */
|
||||
{
|
||||
char hex[MaximumLineLength * 2 + 2] = {};
|
||||
for (size_t i = 0; i < cur_size; i++) {
|
||||
util::SNPrintf(hex + i * 2, 3, "%02X", data_u8[data_ofs++]);
|
||||
hex[i * 2 + 0] = "0123456789ABCDEF"[data_u8[data_ofs] >> 4];
|
||||
hex[i * 2 + 1] = "0123456789ABCDEF"[data_u8[data_ofs] & 0xF];
|
||||
++data_ofs;
|
||||
}
|
||||
hex[cur_size * 2 + 0] = '\n';
|
||||
hex[cur_size * 2 + 1] = '\x00';
|
||||
|
||||
this->WriteString(hex);
|
||||
this->Write(hex, cur_size * 2 + 1);
|
||||
}
|
||||
|
||||
/* Continue. */
|
||||
|
@ -83,16 +85,40 @@ namespace ams::creport {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void ScopedFile::Write(const void *data, size_t size) {
|
||||
/* If we're not open, we can't write. */
|
||||
if (!this->IsOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Advance, if we write successfully. */
|
||||
if (R_SUCCEEDED(fs::WriteFile(m_file, m_offset, data, size, fs::WriteOption::Flush))) {
|
||||
m_offset += size;
|
||||
/* If we have a cache, write to it. */
|
||||
if (m_cache != nullptr) {
|
||||
/* Write into the cache, if we can. */
|
||||
if (m_cache_size - m_cache_offset >= size || R_SUCCEEDED(this->TryWriteCache())) {
|
||||
std::memcpy(m_cache + m_cache_offset, data, size);
|
||||
m_cache_offset += size;
|
||||
}
|
||||
} else {
|
||||
/* Advance, if we write successfully. */
|
||||
if (R_SUCCEEDED(fs::WriteFile(m_file, m_offset, data, size, fs::WriteOption::None))) {
|
||||
m_offset += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Result ScopedFile::TryWriteCache() {
|
||||
/* If there's no cached data, there's nothing to do. */
|
||||
R_SUCCEED_IF(m_cache_offset == 0);
|
||||
|
||||
/* Try to write any cached data. */
|
||||
R_TRY(fs::WriteFile(m_file, m_offset, m_cache, m_cache_offset, fs::WriteOption::None));
|
||||
|
||||
/* Update our extents. */
|
||||
m_offset += m_cache_offset;
|
||||
m_cache_offset = 0;
|
||||
|
||||
R_SUCCEED();
|
||||
}
|
||||
|
||||
}
|
|
@ -25,8 +25,11 @@ namespace ams::creport {
|
|||
fs::FileHandle m_file;
|
||||
s64 m_offset;
|
||||
bool m_opened;
|
||||
u8 *m_cache;
|
||||
const size_t m_cache_size;
|
||||
s64 m_cache_offset;
|
||||
public:
|
||||
ScopedFile(const char *path) : m_file(), m_offset(), m_opened(false) {
|
||||
ScopedFile(const char *path, void *cache = nullptr, size_t cache_size = 0) : m_file(), m_offset(), m_opened(false), m_cache(static_cast<u8*>(cache)), m_cache_size(cache_size), m_cache_offset(0) {
|
||||
if (R_SUCCEEDED(fs::CreateFile(path, 0))) {
|
||||
m_opened = R_SUCCEEDED(fs::OpenFile(std::addressof(m_file), path, fs::OpenMode_Write | fs::OpenMode_AllowAppend));
|
||||
}
|
||||
|
@ -34,6 +37,10 @@ namespace ams::creport {
|
|||
|
||||
~ScopedFile() {
|
||||
if (m_opened) {
|
||||
if (m_cache != nullptr) {
|
||||
this->TryWriteCache();
|
||||
}
|
||||
fs::FlushFile(m_file);
|
||||
fs::CloseFile(m_file);
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +54,8 @@ namespace ams::creport {
|
|||
void DumpMemory(const char *prefix, const void *data, size_t size);
|
||||
|
||||
void Write(const void *data, size_t size);
|
||||
private:
|
||||
Result TryWriteCache();
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue