diff options
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index f01997775..d386de0cd 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -24,6 +24,7 @@ | |||
| 24 | #include <exception> | 24 | #include <exception> |
| 25 | #include <filesystem> | 25 | #include <filesystem> |
| 26 | #include <map> | 26 | #include <map> |
| 27 | #include <mutex> | ||
| 27 | #include <string> | 28 | #include <string> |
| 28 | #include <system_error> | 29 | #include <system_error> |
| 29 | #include <vector> | 30 | #include <vector> |
| @@ -231,15 +232,53 @@ int usage() { | |||
| 231 | // The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it | 232 | // The run ends with the title's Vulkan device alive, so no ~Device stops the background pipeline compiler: stop it |
| 232 | // here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles). | 233 | // here, before ExitProcess ends its thread, maybe inside the driver's compiler (rt::d3d12::stop_pipeline_compiles). |
| 233 | constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s | 234 | constexpr unsigned kExitCompileWaitMs = 2000; // a console close gives the whole process 5 s |
| 235 | // For a build still running on a title thread, then the session cache write (rt::d3d12::save_pipeline_caches_at_exit). | ||
| 236 | constexpr unsigned kExitCacheWaitMs = 1000; | ||
| 234 | 237 | ||
| 235 | static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) { | 238 | static BOOL WINAPI stop_compiles_on_console_exit(DWORD event) { |
| 236 | if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) { | 239 | if (event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT || event == CTRL_CLOSE_EVENT) { |
| 237 | rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer | 240 | rt::d3d12_xs::stop_list_values(500); // as at the run's end: no list-value thread cut off inside the layer |
| 238 | rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); | 241 | rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); |
| 242 | rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs); | ||
| 239 | } | 243 | } |
| 240 | return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before | 244 | return FALSE; // the next handler (the default one: ExitProcess) ends the process, as before |
| 241 | } | 245 | } |
| 242 | 246 | ||
| 247 | // A run that ends in an exception nothing handles (a fault in the title or one of its DLLs, the int3 after a device | ||
| 248 | // loss) would otherwise keep none of the pipelines it compiled, and the next boot builds them all again. The device is | ||
| 249 | // left alone apart from that write; the exception then goes on as before (the previous filter, or Windows' own | ||
| 250 | // handling), so the exit code is the exception's. Not for a stack overflow: no stack is left to write with. | ||
| 251 | static LPTOP_LEVEL_EXCEPTION_FILTER previous_fatal_filter = nullptr; | ||
| 252 | // The retail title installs a filter of its own (SetUnhandledExceptionFilter from api-ms-win-core-errorhandling), | ||
| 253 | // which replaced this one and ended the process without it (measured: a title fault at 354 s wrote nothing). Its | ||
| 254 | // call is served here instead: this filter stays installed and hands the exception to the title's, as Windows would. | ||
| 255 | static void* volatile title_fatal_filter = nullptr; | ||
| 256 | |||
| 257 | static LPTOP_LEVEL_EXCEPTION_FILTER WINAPI title_set_unhandled_exception_filter(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | ||
| 258 | return reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>( | ||
| 259 | InterlockedExchangePointer(&title_fatal_filter, reinterpret_cast<void*>(filter))); | ||
| 260 | } | ||
| 261 | |||
| 262 | static LONG WINAPI save_caches_on_fatal_exception(EXCEPTION_POINTERS* info) { | ||
| 263 | const DWORD code = info && info->ExceptionRecord ? info->ExceptionRecord->ExceptionCode : 0; | ||
| 264 | if (code != EXCEPTION_STACK_OVERFLOW) { | ||
| 265 | // Several title threads can fault at once (measured: eight within a second). The first writes; the others | ||
| 266 | // wait here for it, so none of them reaches the title's filter, which may end the process, mid-write. | ||
| 267 | static std::once_flag once; | ||
| 268 | std::call_once(once, [code] { | ||
| 269 | std::fprintf(stderr, "[rt] unhandled exception 0x%08lx: writing the session pipeline cache before the " | ||
| 270 | "process ends\n", static_cast<unsigned long>(code)); | ||
| 271 | rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); | ||
| 272 | rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs); | ||
| 273 | std::fflush(stderr); | ||
| 274 | }); | ||
| 275 | } | ||
| 276 | if (auto* title = reinterpret_cast<LPTOP_LEVEL_EXCEPTION_FILTER>( | ||
| 277 | InterlockedCompareExchangePointer(&title_fatal_filter, nullptr, nullptr))) | ||
| 278 | return title(info); | ||
| 279 | return previous_fatal_filter ? previous_fatal_filter(info) : EXCEPTION_CONTINUE_SEARCH; | ||
| 280 | } | ||
| 281 | |||
| 243 | static BOOL WINAPI show_package_window(HWND window, int command) { | 282 | static BOOL WINAPI show_package_window(HWND window, int command) { |
| 244 | const BOOL previous = ShowWindow(window, command); | 283 | const BOOL previous = ShowWindow(window, command); |
| 245 | const DWORD error = GetLastError(); | 284 | const DWORD error = GetLastError(); |
| @@ -347,6 +386,13 @@ int wmain(int argc, wchar_t** argv) { | |||
| 347 | rt::fs::configure(game_dir, overlay_dir); | 386 | rt::fs::configure(game_dir, overlay_dir); |
| 348 | #ifdef LEONIDA_VULKAN_DEVICE | 387 | #ifdef LEONIDA_VULKAN_DEVICE |
| 349 | if (run) { | 388 | if (run) { |
| 389 | // Before the image is loaded, so its import binds here (save_caches_on_fatal_exception). | ||
| 390 | const rt::shims::Entry filter_imports[] = { | ||
| 391 | {"api-ms-win-core-errorhandling-l1-1-0.dll", "SetUnhandledExceptionFilter", | ||
| 392 | reinterpret_cast<void*>(&title_set_unhandled_exception_filter)}, | ||
| 393 | {"kernel32.dll", "SetUnhandledExceptionFilter", reinterpret_cast<void*>(&title_set_unhandled_exception_filter)}, | ||
| 394 | }; | ||
| 395 | rt::shims::Table{filter_imports, std::size(filter_imports)}; | ||
| 350 | rt::package_splash::configure(game_dir); | 396 | rt::package_splash::configure(game_dir); |
| 351 | if (rt::package_splash::configured()) { | 397 | if (rt::package_splash::configured()) { |
| 352 | const rt::shims::Entry window_imports[] = { | 398 | const rt::shims::Entry window_imports[] = { |
| @@ -516,6 +562,7 @@ int wmain(int argc, wchar_t** argv) { | |||
| 516 | leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) | 562 | leonida::thread_sampler::start(main_module.base, main_module.size); // LEONIDA_SAMPLE_MS (diagnostic) |
| 517 | #ifdef LEONIDA_VULKAN_DEVICE | 563 | #ifdef LEONIDA_VULKAN_DEVICE |
| 518 | SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); | 564 | SetConsoleCtrlHandler(&stop_compiles_on_console_exit, TRUE); |
| 565 | previous_fatal_filter = SetUnhandledExceptionFilter(&save_caches_on_fatal_exception); | ||
| 519 | #endif | 566 | #endif |
| 520 | int code; | 567 | int code; |
| 521 | { | 568 | { |
| @@ -536,6 +583,7 @@ int wmain(int argc, wchar_t** argv) { | |||
| 536 | rt::d3d12_xs::stop_list_values(500); | 583 | rt::d3d12_xs::stop_list_values(500); |
| 537 | #ifdef LEONIDA_VULKAN_DEVICE | 584 | #ifdef LEONIDA_VULKAN_DEVICE |
| 538 | rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); | 585 | rt::d3d12::stop_pipeline_compiles(kExitCompileWaitMs); |
| 586 | rt::d3d12::save_pipeline_caches_at_exit(kExitCacheWaitMs); | ||
| 539 | #endif | 587 | #endif |
| 540 | rt::cleanup_tls_clones(); | 588 | rt::cleanup_tls_clones(); |
| 541 | ExitProcess(static_cast<UINT>(code)); | 589 | ExitProcess(static_cast<UINT>(code)); |