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

Improve error_code, make HDD1 errors be warnings #12983

Merged
merged 1 commit into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ void thread_base::set_name(std::string name)
u64 thread_base::finalize(thread_state result_state) noexcept
{
// Report pending errors
error_code::error_report(0, 0, 0, 0);
error_code::error_report(0, nullptr, nullptr, nullptr, nullptr);

#ifdef _WIN32
static thread_local ULONG64 tls_cycles{};
Expand Down
51 changes: 35 additions & 16 deletions rpcs3/Emu/Cell/ErrorCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,54 @@
#include "util/types.hpp"
#include "Utilities/StrFmt.h"

namespace logs
{
struct message;
}

// Error code type (return type), implements error reporting.
class error_code
{
s32 value;

public:
error_code() = default;

// Implementation must be provided independently
static s32 error_report(s32 result, const char* fmt, const fmt_type_info* sup, const u64* args);

// Common constructor
template<typename ET>
error_code(const ET& value)
: value(error_report(static_cast<s32>(value), " : %s", fmt::type_info_v<ET>, fmt_args_t<ET>{fmt_unveil<ET>::get(value)}))
template <typename ET> requires requires (ET v) { static_cast<s32>(v); }
error_code(const logs::message* ch, const ET& value) noexcept
: value(error_report(static_cast<s32>(value), ch, " : %s", fmt::type_info_v<ET>, fmt_args_t<ET>{fmt_unveil<ET>::get(value)}))
{
}

// Error constructor (2 args)
template<typename ET, typename T>
error_code(const ET& value, const T& arg)
: value(error_report(static_cast<s32>(value), " : %s, %s", fmt::type_info_v<ET, T>, fmt_args_t<ET, T>{fmt_unveil<ET>::get(value), fmt_unveil<T>::get(arg)}))
template <typename ET, typename T> requires requires (ET v) { static_cast<s32>(v); }
error_code(const logs::message* ch, const ET& value, const T& arg) noexcept
: value(error_report(static_cast<s32>(value), ch, " : %s, %s", fmt::type_info_v<ET, T>, fmt_args_t<ET, T>{fmt_unveil<ET>::get(value), fmt_unveil<T>::get(arg)}))
{
}

// Formatting constructor (error, format string, variadic list)
template <typename ET, typename... Args> requires (sizeof...(Args) > 0)
error_code(const ET& value, const const_str& fmt, const Args&... args)
: value(error_report(static_cast<s32>(value), fmt, fmt::type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...}))
template <typename ET, typename... Args> requires requires (ET v) { static_cast<s32>(v); }
error_code(const logs::message* ch, const ET& value, const const_str& fmt, const Args&... args) noexcept
: value(error_report(static_cast<s32>(value), ch, fmt, fmt::type_info_v<Args...>, fmt_args_t<Args...>{fmt_unveil<Args>::get(args)...}))
{
}

public:
// Implementation must be provided independently
static s32 error_report(s32 result, const logs::message* channel, const char* fmt, const fmt_type_info* sup, const u64* args);

error_code() = default;

// Constructor without channel
template <typename... Args> requires (sizeof...(Args) > 0 && !(std::is_same_v<std::decay_t<std::remove_pointer_t<Args>>, logs::message> || ...))
error_code(const Args&... args) noexcept
: error_code(std::add_pointer_t<const logs::message>{}, args...)
{
}

// Constructor with channel
template <typename... Args>
error_code(const logs::message& ch, const Args&... args) noexcept
: error_code(std::addressof(ch), args...)
{
}

Expand All @@ -51,7 +70,7 @@ enum CellNotAnError : s32

// Constructor specialization that doesn't trigger reporting
template <>
constexpr FORCE_INLINE error_code::error_code(const CellNotAnError& value)
constexpr FORCE_INLINE error_code::error_code(const CellNotAnError& value) noexcept
: value(value)
{
}
Expand Down
31 changes: 23 additions & 8 deletions rpcs3/Emu/Cell/lv2/sys_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ error_code sys_fs_open(ppu_thread& ppu, vm::cptr<char> path, s32 flags, vm::ptr<
return not_an_error(CELL_EEXIST);
}

return {error, path};
return {lv2_fs_object::get_mp(vpath) == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, error, path};
}

if (const u32 id = idm::import<lv2_fs_object, lv2_file>([&ppath = ppath, &file = file, mode, flags, &real = real, &type = type]() -> std::shared_ptr<lv2_file>
Expand Down Expand Up @@ -1246,7 +1246,7 @@ error_code sys_fs_opendir(ppu_thread& ppu, vm::cptr<char> path, vm::ptr<u32> fd)
{
if (ext.empty())
{
return {CELL_ENOENT, path};
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}

break;
Expand Down Expand Up @@ -1435,7 +1435,7 @@ error_code sys_fs_stat(ppu_thread& ppu, vm::cptr<char> path, vm::ptr<CellFsStat>
break;
}

return {CELL_ENOENT, path};
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}
default:
{
Expand Down Expand Up @@ -1563,8 +1563,14 @@ error_code sys_fs_mkdir(ppu_thread& ppu, vm::cptr<char> path, s32 mode)
{
switch (auto error = fs::g_tls_error)
{
case fs::error::noent: return {CELL_ENOENT, path};
case fs::error::exist: return {CELL_EEXIST, path};
case fs::error::noent:
{
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}
case fs::error::exist:
{
return {sys_fs.warning, CELL_EEXIST, path};
}
default: sys_fs.error("sys_fs_mkdir(): unknown error %s", error);
}

Expand Down Expand Up @@ -1737,7 +1743,10 @@ error_code sys_fs_unlink(ppu_thread& ppu, vm::cptr<char> path)
{
switch (auto error = fs::g_tls_error)
{
case fs::error::noent: return {CELL_ENOENT, path};
case fs::error::noent:
{
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}
default: sys_fs.error("sys_fs_unlink(): unknown error %s", error);
}

Expand Down Expand Up @@ -2690,7 +2699,10 @@ error_code sys_fs_truncate(ppu_thread& ppu, vm::cptr<char> path, u64 size)
{
switch (auto error = fs::g_tls_error)
{
case fs::error::noent: return {CELL_ENOENT, path};
case fs::error::noent:
{
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}
default: sys_fs.error("sys_fs_truncate(): unknown error %s", error);
}

Expand Down Expand Up @@ -2890,7 +2902,10 @@ error_code sys_fs_utime(ppu_thread& ppu, vm::cptr<char> path, vm::cptr<CellFsUti
{
switch (auto error = fs::g_tls_error)
{
case fs::error::noent: return {CELL_ENOENT, path};
case fs::error::noent:
{
return {mp == &g_mp_sys_dev_hdd1 ? sys_fs.warning : sys_fs.error, CELL_ENOENT, path};
}
default: sys_fs.error("sys_fs_utime(): unknown error %s", error);
}

Expand Down
19 changes: 12 additions & 7 deletions rpcs3/Emu/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2751,10 +2751,15 @@ std::string Emulator::GetFormattedTitle(double fps) const
return rpcs3::get_formatted_title(title_data);
}

s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* sup, const u64* args)
s32 error_code::error_report(s32 result, const logs::message* channel, const char* fmt, const fmt_type_info* sup, const u64* args)
{
static thread_local std::unordered_map<std::string, usz> g_tls_error_stats;
static thread_local std::string g_tls_error_str;
static thread_local std::unordered_map<std::string, usz> g_tls_error_stats;

if (!channel)
{
channel = &sys_log.error;
}

if (!sup && !args)
{
Expand All @@ -2765,7 +2770,7 @@ s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* s
{
if (pair.second > 3)
{
sys_log.error("Stat: %s [x%u]", pair.first, pair.second);
channel->operator()("Stat: %s [x%u]", pair.first, pair.second);
}
}

Expand All @@ -2776,7 +2781,6 @@ s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* s

ensure(fmt);

logs::channel* channel = &sys_log;
const char* func = "Unknown function";

if (auto ppu = get_current_cpu_thread<ppu_thread>())
Expand All @@ -2789,6 +2793,7 @@ s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* s

// Format log message (use preallocated buffer)
g_tls_error_str.clear();

fmt::append(g_tls_error_str, "'%s' failed with 0x%08x", func, result);

// Add spacer between error and fmt if necessary
Expand All @@ -2804,18 +2809,18 @@ s32 error_code::error_report(s32 result, const char* fmt, const fmt_type_info* s
if (!g_tls_error_stats.empty())
{
// Report and clean error state
error_report(0, nullptr, nullptr, nullptr);
error_report(0, nullptr, nullptr, nullptr, nullptr);
}

channel->error("%s", g_tls_error_str);
channel->operator()("%s", g_tls_error_str);
}
else
{
const auto stat = ++g_tls_error_stats[g_tls_error_str];

if (stat <= 3)
{
channel->error("%s [%u]", g_tls_error_str, stat);
channel->operator()("%s [%u]", g_tls_error_str, stat);
}
}

Expand Down