misc: various boot optimizations (#942)

## Link to GitHub Issue or related Pull Request, if one exists
Various fixes copied from https://github.com/l4kr/spicefast

## Description of change
* make local Windows event logs appear empty to game DLLs, avoiding
startup scans without modifying system logs or remote reads.
* when -nvprofile is on, skip writes and profile saves when settings
already match
* defer SetupAPI device name lookups during HID enumeration until it's
absolutely needed
* remove the fixed 100 ms delay after AVS boot

## Testing
This commit is contained in:
bicarus authored and GitHub committed 2026-12-05 03:37:32 -08:00
1 parent 1e0de890bc
commit af66dd5b50
8 files changed
+221 -90

No files matched your search

+1
View File
@@ -6,6 +6,7 @@
*.iws
# CMake
/build/
cmake-build*
# user config
+1
View File
@@ -599,6 +599,7 @@ set(SOURCE_FILES ${SOURCE_FILES}
hooks/cfgmgr32hook.cpp
hooks/debughook.cpp
hooks/devicehook.cpp
hooks/eventlog.cpp
hooks/graphics/graphics.cpp
hooks/graphics/graphics_windowed.cpp
hooks/graphics/jpeg_encoder.cpp
-3
View File
@@ -2116,9 +2116,6 @@ namespace avs {
log_fatal("avs-core", "unknown AVS boot procedure");
}
// wait a bit for output
Sleep(100);
// destroy config
config_destroy(config);
}
+153
View File
@@ -0,0 +1,153 @@
#include "eventlog.h"
#include <mutex>
#include <set>
#include "util/detour.h"
#include "util/logging.h"
namespace hooks::eventlog {
static decltype(OpenEventLogA) *OpenEventLogA_orig = nullptr;
static decltype(OpenEventLogW) *OpenEventLogW_orig = nullptr;
static decltype(CloseEventLog) *CloseEventLog_orig = nullptr;
static decltype(GetNumberOfEventLogRecords) *GetNumberOfEventLogRecords_orig = nullptr;
static decltype(ReadEventLogA) *ReadEventLogA_orig = nullptr;
static decltype(ReadEventLogW) *ReadEventLogW_orig = nullptr;
// count and read calls only receive a handle, not the server name used at open
static std::mutex handles_mutex;
static std::set<HANDLE> local_log_handles;
static HANDLE record_open(HANDLE handle, bool local) {
// bookkeeping must not change the last error left by OpenEventLog
auto error = GetLastError();
{
std::lock_guard lock(handles_mutex);
if (local) {
local_log_handles.insert(handle);
} else {
// another DLL may have closed a local handle without going through our hook
local_log_handles.erase(handle);
}
}
SetLastError(error);
return handle;
}
static bool is_local_log(HANDLE handle) {
std::lock_guard lock(handles_mutex);
return local_log_handles.count(handle) != 0;
}
static HANDLE WINAPI OpenEventLogA_hook(LPCSTR server, LPCSTR source) {
auto handle = OpenEventLogA_orig(server, source);
return handle ? record_open(handle, !server || !*server) : nullptr;
}
static HANDLE WINAPI OpenEventLogW_hook(LPCWSTR server, LPCWSTR source) {
auto handle = OpenEventLogW_orig(server, source);
return handle ? record_open(handle, !server || !*server) : nullptr;
}
static BOOL WINAPI CloseEventLog_hook(HANDLE handle) {
// finish removing this handle before another open can register its reused value
std::unique_lock lock(handles_mutex);
auto result = CloseEventLog_orig(handle);
auto error = GetLastError();
if (result) {
local_log_handles.erase(handle);
}
lock.unlock();
SetLastError(error);
return result;
}
static BOOL WINAPI GetNumberOfEventLogRecords_hook(HANDLE handle, PDWORD count) {
// let Windows validate the request; only replace a successful local count
auto result = GetNumberOfEventLogRecords_orig(handle, count);
auto error = GetLastError();
if (result && is_local_log(handle)) {
*count = 0;
}
SetLastError(error);
return result;
}
static bool valid_read_flags(DWORD flags) {
// exactly one positioning mode and one direction; pass unknown bits through to Windows
switch (flags) {
case EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ:
case EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ:
case EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ:
case EVENTLOG_SEEK_READ | EVENTLOG_BACKWARDS_READ:
return true;
default:
return false;
}
}
static BOOL read_event_log(decltype(ReadEventLogA) *original, HANDLE handle, DWORD flags,
DWORD offset, LPVOID buffer, DWORD size, DWORD *bytes_read, DWORD *needed) {
if (!valid_read_flags(flags) || !is_local_log(handle)) {
return original(handle, flags, offset, buffer, size, bytes_read, needed);
}
if (!buffer || !bytes_read || !needed) {
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
// callers that read without checking the count should also see an empty log
*bytes_read = 0;
*needed = 0;
SetLastError(ERROR_HANDLE_EOF);
return FALSE;
}
static BOOL WINAPI ReadEventLogA_hook(HANDLE handle, DWORD flags, DWORD offset, LPVOID buffer,
DWORD size, DWORD *bytes_read, DWORD *needed) {
return read_event_log(ReadEventLogA_orig, handle, flags, offset, buffer, size, bytes_read, needed);
}
static BOOL WINAPI ReadEventLogW_hook(HANDLE handle, DWORD flags, DWORD offset, LPVOID buffer,
DWORD size, DWORD *bytes_read, DWORD *needed) {
return read_event_log(ReadEventLogW_orig, handle, flags, offset, buffer, size, bytes_read, needed);
}
void init(HMODULE module) {
// some games like SDVX read the Application event log, one record at a time, looking for
// Windows activation timestamps; present local logs as empty to avoid these scans.
if (!module) {
return;
}
constexpr auto dll = "advapi32.dll";
if (!detour::iat_find("OpenEventLogA", module, dll) &&
!detour::iat_find("OpenEventLogW", module, dll)) {
return;
}
CloseEventLog_orig = detour::iat_try("CloseEventLog", CloseEventLog_hook, module, dll);
if (!CloseEventLog_orig) {
log_misc("eventlog", "no CloseEventLog import; leaving event log access unchanged");
return;
}
GetNumberOfEventLogRecords_orig = detour::iat_try(
"GetNumberOfEventLogRecords", GetNumberOfEventLogRecords_hook, module, dll);
ReadEventLogA_orig = detour::iat_try("ReadEventLogA", ReadEventLogA_hook, module, dll);
ReadEventLogW_orig = detour::iat_try("ReadEventLogW", ReadEventLogW_hook, module, dll);
if (!GetNumberOfEventLogRecords_orig && !ReadEventLogA_orig && !ReadEventLogW_orig) {
return;
}
// start tracking opens only after we can intercept reads or counts and observe closes
OpenEventLogA_orig = detour::iat_try("OpenEventLogA", OpenEventLogA_hook, module, dll);
OpenEventLogW_orig = detour::iat_try("OpenEventLogW", OpenEventLogW_hook, module, dll);
if (OpenEventLogA_orig || OpenEventLogW_orig) {
log_info("eventlog", "hooks installed");
}
}
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <windows.h>
namespace hooks::eventlog {
void init(HMODULE module);
}
+2
View File
@@ -82,6 +82,7 @@
#include "hooks/audio/backends/wasapi/downmix.h"
#include "hooks/debughook.h"
#include "hooks/devicehook.h"
#include "hooks/eventlog.h"
#include "hooks/graphics/nvenc_hook.h"
#include "hooks/input/dinput8/hook.h"
#include "hooks/graphics/graphics.h"
@@ -2709,6 +2710,7 @@ int main_implementation(int argc, char *argv[]) {
// load game
avs::game::load_dll();
hooks::eventlog::init(avs::game::DLL_INSTANCE);
// attach games
for (auto game : games) {
+49 -78
View File
@@ -155,7 +155,8 @@ namespace nvapi {
log_misc("nvapi", "NVAPI module initialized");
}
NvAPI_Status create_profile_if_needed(NvDRSSessionHandle h_session, NvDRSProfileHandle *h_profile) {
static NvAPI_Status create_profile_if_needed(NvDRSSessionHandle h_session,
NvDRSProfileHandle *h_profile, bool &changed) {
NvAPI_UnicodeString app_name;
#ifdef SPICE64
memcpy_s(app_name, sizeof(app_name), L"spice64.exe", 12 * sizeof(wchar_t));
@@ -203,57 +204,38 @@ namespace nvapi {
return status;
}
changed = true;
return NVAPI_OK;
}
NvAPI_Status set_gpu_power_state(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = PREFERRED_PSTATE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = PREFERRED_PSTATE_PREFER_MAX;
drs_setting.u32CurrentValue = PREFERRED_PSTATE_PREFER_MAX;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
static void apply_dword_setting(NvDRSSessionHandle session, NvDRSProfileHandle profile,
NvU32 id, NvU32 value, const char *name, bool &changed) {
NVDRS_SETTING setting {};
setting.version = NVDRS_SETTING_VER;
auto status = NvAPI_DRS_GetSetting(session, profile, id, &setting);
if (status == NVAPI_OK) {
// an inherited or predefined value is not an explicit application override
if (setting.settingLocation == NVDRS_CURRENT_PROFILE_LOCATION &&
!setting.isCurrentPredefined && setting.settingType == NVDRS_DWORD_TYPE &&
setting.u32CurrentValue == value) {
return;
}
} else if (status != NVAPI_SETTING_NOT_FOUND) {
log_warning("nvapi", "could not read {}: {}", name, get_error_message(status));
}
NvAPI_Status set_gsync_mode(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = VRR_APP_OVERRIDE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = VRR_APP_OVERRIDE_FIXED_REFRESH;
drs_setting.u32CurrentValue = VRR_APP_OVERRIDE_FIXED_REFRESH;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_vsync_mode(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = VSYNCMODE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = VSYNCMODE_PASSIVE;
drs_setting.u32CurrentValue = VSYNCMODE_PASSIVE;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_refresh_rate(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = REFRESH_RATE_OVERRIDE_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED;
drs_setting.u32CurrentValue = REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
}
NvAPI_Status set_max_fps(NvDRSSessionHandle h_session, NvDRSProfileHandle h_profile) {
auto drs_setting = NVDRS_SETTING {};
drs_setting.version = NVDRS_SETTING_VER;
drs_setting.settingId = FRL_FPS_ID;
drs_setting.settingType = NVDRS_DWORD_TYPE;
drs_setting.u32PredefinedValue = FRL_FPS_DISABLED;
drs_setting.u32CurrentValue = FRL_FPS_DISABLED;
return NvAPI_DRS_SetSetting(h_session, h_profile, &drs_setting);
setting = {};
setting.version = NVDRS_SETTING_VER;
setting.settingId = id;
setting.settingType = NVDRS_DWORD_TYPE;
setting.u32PredefinedValue = value;
setting.u32CurrentValue = value;
status = NvAPI_DRS_SetSetting(session, profile, &setting);
if (status == NVAPI_OK) {
changed = true;
} else {
log_warning("nvapi", "could not set {}: {}", name, get_error_message(status));
}
}
void set_profile_settings() {
@@ -278,41 +260,30 @@ namespace nvapi {
NvDRSProfileHandle h_profile = nullptr;
log_info("nvapi", "creating NVIDIA profile for spice(64).exe...");
if (create_profile_if_needed(h_session, &h_profile) != NVAPI_OK) {
bool changed = false;
if (create_profile_if_needed(h_session, &h_profile, changed) != NVAPI_OK) {
NvAPI_DRS_DestroySession(h_session);
return;
}
log_info("nvapi", "setting VSync to Application Controlled...");
if ((status = set_vsync_mode(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set VSync mode: {}", get_error_message(status));
}
apply_dword_setting(h_session, h_profile, VSYNCMODE_ID, VSYNCMODE_PASSIVE,
"VSync mode", changed);
apply_dword_setting(h_session, h_profile, PREFERRED_PSTATE_ID, PREFERRED_PSTATE_PREFER_MAX,
"preferred PState", changed);
apply_dword_setting(h_session, h_profile, VRR_APP_OVERRIDE_ID, VRR_APP_OVERRIDE_FIXED_REFRESH,
"G-SYNC mode", changed);
apply_dword_setting(h_session, h_profile, REFRESH_RATE_OVERRIDE_ID,
REFRESH_RATE_OVERRIDE_APPLICATION_CONTROLLED, "preferred refresh rate", changed);
apply_dword_setting(h_session, h_profile, FRL_FPS_ID, FRL_FPS_DISABLED,
"max framerate", changed);
log_info("nvapi", "applying preferred PState to Maximum Performance...");
if ((status = set_gpu_power_state(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set preferred PState: {}", get_error_message(status));
}
log_info("nvapi", "disabling G-SYNC...");
if ((status = set_gsync_mode(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set G-Sync mode: {}", get_error_message(status));
}
log_info("nvapi", "setting preferred refresh rate to Application Controlled...");
if ((status = set_refresh_rate(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not set preferred refresh rate: {}", get_error_message(status));
}
log_info("nvapi", "disabling max framerate...");
if ((status = set_max_fps(h_session, h_profile)) != NVAPI_OK) {
log_warning("nvapi", "could not disable max framerate: {}", get_error_message(status));
}
log_info("nvapi", "saving settings for DRS session...");
if ((status = NvAPI_DRS_SaveSettings(h_session)) != NVAPI_OK) {
log_warning("nvapi", "could not save driver settings: {}", get_error_message(status));
NvAPI_DRS_DestroySession(h_session);
return;
if (changed) {
log_info("nvapi", "saving settings for DRS session...");
if ((status = NvAPI_DRS_SaveSettings(h_session)) != NVAPI_OK) {
log_warning("nvapi", "could not save driver settings: {}", get_error_message(status));
}
} else {
log_misc("nvapi", "no driver profile changes to save");
}
log_info("nvapi", "destroying DRS session...");
+8 -9
View File
@@ -275,7 +275,6 @@ void rawinput::RawInputManager::devices_scan_rawinput(RAWINPUTDEVICELIST *device
// extract information out of name
auto device_info = rawinput::RawInputManager::get_device_info(device_name);
auto device_description = rawinput::RawInputManager::rawinput_get_device_description(device_info, device_name);
// get device information
RID_DEVICE_INFO rawinput_device_info {};
@@ -301,15 +300,16 @@ void rawinput::RawInputManager::devices_scan_rawinput(RAWINPUTDEVICELIST *device
new_device.id = unique_id;
new_device.handle = device->hDevice;
new_device.name = device_name;
new_device.desc = device_description;
new_device.info = device_info;
new_device.input_time = get_performance_seconds();
switch (device->dwType) {
case RIM_TYPEMOUSE:
new_device.desc = rawinput_get_device_description(device_info, device_name);
new_device.type = MOUSE;
new_device.mouseInfo = new DeviceMouseInfo();
break;
case RIM_TYPEKEYBOARD:
new_device.desc = rawinput_get_device_description(device_info, device_name);
new_device.type = KEYBOARD;
new_device.keyboardInfo = new DeviceKeyboardInfo();
break;
@@ -402,13 +402,6 @@ void rawinput::RawInputManager::devices_scan_rawinput(RAWINPUTDEVICELIST *device
prod_ws = wchar_to_u8(prod_str_buffer);
}
// For Bluetooth LE HID devices (e.g., newer Xbox controllers) HidD_GetProductString
// and HidD_GetManufacturerString will return blank strings.
// https://docs.microsoft.com/en-us/answers/questions/401236/hidd-getproductstring-with-ble-hid-device.html
if (man_ws.empty() && prod_ws.empty()) {
prod_ws = device_description;
}
// build desc string
std::string desc_ws;
if (string_begins_with(prod_ws, man_ws)) {
@@ -427,6 +420,12 @@ void rawinput::RawInputManager::devices_scan_rawinput(RAWINPUTDEVICELIST *device
log_warning("rawinput", "failed to get HID device attributes for {}", device_name);
}
}
// For Bluetooth LE HID devices (e.g., newer Xbox controllers) HidD_GetProductString
// and HidD_GetManufacturerString will return blank strings.
// https://docs.microsoft.com/en-us/answers/questions/401236/hidd-getproductstring-with-ble-hid-device.html
if (new_device.desc.empty()) {
new_device.desc = rawinput_get_device_description(device_info, device_name);
}
// get button caps
USHORT button_cap_length = caps.NumberInputButtonCaps;