okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.0 KiB50 linesraw
1// #4733: see include/strata/platform/io_priority.hpp
2#include "strata/platform/io_priority.hpp"
3
4#include <cstdlib>
5
6#if defined(_WIN32)
7#ifndef WIN32_LEAN_AND_MEAN
8#define WIN32_LEAN_AND_MEAN
9#endif
10#ifndef NOMINMAX
11#define NOMINMAX
12#endif
13#include <windows.h>
14#endif
15
16namespace strata::platform {
17
18int restore_normal_io_priority(std::FILE* log) {
19#if defined(_WIN32)
20    const char* keep = std::getenv("STRATA_KEEP_IO_PRIORITY");
21    if (keep != nullptr && keep[0] == '1') return -1;
22    using Query = LONG(NTAPI*)(HANDLE, ULONG, PVOID, ULONG, PULONG);
23    using Set = LONG(NTAPI*)(HANDLE, ULONG, PVOID, ULONG);
24    HMODULE nt = GetModuleHandleW(L"ntdll.dll");
25    if (nt == nullptr) return -1;
26    auto query = reinterpret_cast<Query>(reinterpret_cast<void*>(GetProcAddress(nt, "NtQueryInformationProcess")));
27    auto set = reinterpret_cast<Set>(reinterpret_cast<void*>(GetProcAddress(nt, "NtSetInformationProcess")));
28    if (query == nullptr || set == nullptr) return -1;
29    constexpr ULONG kProcessIoPriority = 33;
30    ULONG now = 2, ret = 0;
31    if (query(GetCurrentProcess(), kProcessIoPriority, &now, sizeof now, &ret) != 0 || ret < sizeof now) return -1;
32    if (now >= 2) return (int) now;
33    ULONG want = 2;
34    if (set(GetCurrentProcess(), kProcessIoPriority, &want, sizeof want) != 0) {
35        if (log != nullptr)
36            std::fprintf(log, "strata: this program runs at a low disk priority (inherited from the program that started it) "
37                              "and Windows did not let it go back to normal; reading the model can be several times slower (#4733)\n");
38        return (int) now;
39    }
40    if (log != nullptr)
41        std::fprintf(log, "strata: the program that started the engine runs at a low disk priority, which the engine inherits and "
42                          "which makes model reads several times slower; the engine's disk priority is back to normal (#4733)\n");
43    return (int) now;
44#else
45    (void) log;
46    return -1;
47#endif
48}
49
50}  // namespace strata::platform