From cf0ee05da60a5e7aaf4ed852e557629aca0c05ee Mon Sep 17 00:00:00 2001 From: Xphalnos <164882787+Xphalnos@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:03:20 +0200 Subject: [PATCH] [MISC] Allow user to override process priority class --- src/xenia/base/system.h | 2 ++ src/xenia/base/system_android.cc | 2 ++ src/xenia/base/system_gnulinux.cc | 2 ++ src/xenia/base/system_win.cc | 16 ++++++++++++++++ src/xenia/emulator.cc | 13 +++++++++++++ 5 files changed, 35 insertions(+) diff --git a/src/xenia/base/system.h b/src/xenia/base/system.h index b77bdbc69..519d954e1 100644 --- a/src/xenia/base/system.h +++ b/src/xenia/base/system.h @@ -27,6 +27,8 @@ void ShutdownAndroidSystem(); void LaunchWebBrowser(const std::string_view url); void LaunchFileExplorer(const std::filesystem::path& path); +bool SetProcessPriorityClass(const uint32_t priority_class); + enum class SimpleMessageBoxType { Help, Warning, diff --git a/src/xenia/base/system_android.cc b/src/xenia/base/system_android.cc index 25c14a7bc..262c9d419 100644 --- a/src/xenia/base/system_android.cc +++ b/src/xenia/base/system_android.cc @@ -294,4 +294,6 @@ void ShowSimpleMessageBox(SimpleMessageBoxType type, std::string_view message) { // Java VM for the calling thread is needed. } +bool SetProcessPriorityClass(const uint32_t priority_class) { return true; } + } // namespace xe diff --git a/src/xenia/base/system_gnulinux.cc b/src/xenia/base/system_gnulinux.cc index 920c9ab9e..a5421241f 100644 --- a/src/xenia/base/system_gnulinux.cc +++ b/src/xenia/base/system_gnulinux.cc @@ -67,4 +67,6 @@ void ShowSimpleMessageBox(SimpleMessageBoxType type, std::string_view message) { } } +bool SetProcessPriorityClass(const uint32_t priority_class) { return true; } + } // namespace xe diff --git a/src/xenia/base/system_win.cc b/src/xenia/base/system_win.cc index ae77ae1d4..cd0ae622d 100644 --- a/src/xenia/base/system_win.cc +++ b/src/xenia/base/system_win.cc @@ -7,6 +7,7 @@ ****************************************************************************** */ +#include #include "xenia/base/platform_win.h" #include "xenia/base/string.h" #include "xenia/base/system.h" @@ -48,4 +49,19 @@ void ShowSimpleMessageBox(SimpleMessageBoxType type, type_flags); } +static std::map xeniaToWindowsPriorityClassMapping = { + {0, NORMAL_PRIORITY_CLASS}, + {1, ABOVE_NORMAL_PRIORITY_CLASS}, + {2, HIGH_PRIORITY_CLASS}, + {3, REALTIME_PRIORITY_CLASS}}; + +bool SetProcessPriorityClass(const uint32_t priority_class) { + if (!xeniaToWindowsPriorityClassMapping.count(priority_class)) { + return false; + } + + return SetPriorityClass(GetCurrentProcess(), + xeniaToWindowsPriorityClassMapping[priority_class]); +} + } // namespace xe diff --git a/src/xenia/emulator.cc b/src/xenia/emulator.cc index 952e28de1..8e00cb0ae 100644 --- a/src/xenia/emulator.cc +++ b/src/xenia/emulator.cc @@ -86,6 +86,12 @@ DECLARE_int32(user_language); DECLARE_bool(allow_plugins); +DEFINE_int32(priority_class, 0, + "Forces Xenia to use different process priority than default one. " + "It might affect performance and cause unexpected bugs. Possible " + "values: 0 - Normal, 1 - Above normal, 2 - High", + "General"); + namespace xe { using namespace xe::literals; @@ -125,6 +131,13 @@ Emulator::Emulator(const std::filesystem::path& command_line, paused_(false), restoring_(false), restore_fence_() { + if (cvars::priority_class != 0) { + if (SetProcessPriorityClass(cvars::priority_class)) { + XELOGI("Higher priority class request: Successful. New priority: {}", + cvars::priority_class); + } + } + #if XE_PLATFORM_WIN32 == 1 // Show a disclaimer that links to the quickstart // guide the first time they ever open the emulator