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

Add extended logging to NVAPI operations #30547

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
37 changes: 21 additions & 16 deletions osu.Desktop/NVAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public static bool IsLaptop

// Make sure that this is a laptop.
IntPtr[] gpus = new IntPtr[64];
if (checkError(EnumPhysicalGPUs(gpus, out int gpuCount)))
if (checkError(EnumPhysicalGPUs(gpus, out int gpuCount), nameof(EnumPhysicalGPUs)))
return false;

for (int i = 0; i < gpuCount; i++)
{
if (checkError(GetSystemType(gpus[i], out var type)))
if (checkError(GetSystemType(gpus[i], out var type), nameof(GetSystemType)))
return false;

if (type == NvSystemType.LAPTOP)
Expand Down Expand Up @@ -182,7 +182,7 @@ public static NvThreadControlSetting ThreadedOptimisations

bool success = setSetting(NvSettingID.OGL_THREAD_CONTROL_ID, (uint)value);

Logger.Log(success ? $"Threaded optimizations set to \"{value}\"!" : "Threaded optimizations set failed!");
Logger.Log(success ? $"[NVAPI] Threaded optimizations set to \"{value}\"!" : "[NVAPI] Threaded optimizations set failed!");
}
}

Expand All @@ -205,7 +205,7 @@ private static bool containsApplication(IntPtr profileHandle, NvProfile profile,

uint numApps = profile.NumOfApps;

if (checkError(EnumApplications(sessionHandle, profileHandle, 0, ref numApps, applications)))
if (checkError(EnumApplications(sessionHandle, profileHandle, 0, ref numApps, applications), nameof(EnumApplications)))
return false;

for (uint i = 0; i < numApps; i++)
Expand Down Expand Up @@ -236,10 +236,10 @@ private static bool getProfile(out IntPtr profileHandle, out NvApplication appli

isApplicationSpecific = true;

if (checkError(FindApplicationByName(sessionHandle, osu_filename, out profileHandle, ref application)))
if (checkError(FindApplicationByName(sessionHandle, osu_filename, out profileHandle, ref application), nameof(FindApplicationByName)))
{
isApplicationSpecific = false;
if (checkError(GetCurrentGlobalProfile(sessionHandle, out profileHandle)))
if (checkError(GetCurrentGlobalProfile(sessionHandle, out profileHandle), nameof(GetCurrentGlobalProfile)))
return false;
}

Expand All @@ -263,7 +263,7 @@ private static bool createProfile(out IntPtr profileHandle)

newProfile.GPUSupport[0] = 1;

if (checkError(CreateProfile(sessionHandle, ref newProfile, out profileHandle)))
if (checkError(CreateProfile(sessionHandle, ref newProfile, out profileHandle), nameof(CreateProfile)))
return false;

return true;
Expand All @@ -284,7 +284,7 @@ private static bool getSetting(NvSettingID settingId, IntPtr profileHandle, out
SettingID = settingId
};

if (checkError(GetSetting(sessionHandle, profileHandle, settingId, ref setting)))
if (checkError(GetSetting(sessionHandle, profileHandle, settingId, ref setting), nameof(GetSetting)))
return false;

return true;
Expand Down Expand Up @@ -313,15 +313,15 @@ private static bool setSetting(NvSettingID settingId, uint settingValue)
};

// Set the thread state
if (checkError(SetSetting(sessionHandle, profileHandle, ref newSetting)))
if (checkError(SetSetting(sessionHandle, profileHandle, ref newSetting), nameof(SetSetting)))
return false;

// Get the profile (needed to check app count)
NvProfile profile = new NvProfile
{
Version = NvProfile.Stride
};
if (checkError(GetProfileInfo(sessionHandle, profileHandle, ref profile)))
if (checkError(GetProfileInfo(sessionHandle, profileHandle, ref profile), nameof(GetProfileInfo)))
return false;

if (!containsApplication(profileHandle, profile, out application))
Expand All @@ -332,12 +332,12 @@ private static bool setSetting(NvSettingID settingId, uint settingValue)
application.AppName = osu_filename;
application.UserFriendlyName = APPLICATION_NAME;

if (checkError(CreateApplication(sessionHandle, profileHandle, ref application)))
if (checkError(CreateApplication(sessionHandle, profileHandle, ref application), nameof(CreateApplication)))
return false;
}

// Save!
return !checkError(SaveSettings(sessionHandle));
return !checkError(SaveSettings(sessionHandle), nameof(SaveSettings));
}

/// <summary>
Expand All @@ -346,20 +346,25 @@ private static bool setSetting(NvSettingID settingId, uint settingValue)
/// <returns>If the operation succeeded.</returns>
private static bool createSession()
{
if (checkError(CreateSession(out sessionHandle)))
if (checkError(CreateSession(out sessionHandle), nameof(CreateSession)))
return false;

// Load settings into session
if (checkError(LoadSettings(sessionHandle)))
if (checkError(LoadSettings(sessionHandle), nameof(LoadSettings)))
return false;

return true;
}

private static bool checkError(NvStatus status)
private static bool checkError(NvStatus status, string caller)
{
Status = status;
return status != NvStatus.OK;

bool hasError = status != NvStatus.OK;
if (hasError)
Logger.Log($"[NVAPI] {caller} call failed with status code {status}");

return hasError;
}

static NVAPI()
Expand Down