Skip to content

Commit

Permalink
Merge pull request ppy#30547 from bdach/extended-nvapi-logging
Browse files Browse the repository at this point in the history
Add extended logging to NVAPI operations
  • Loading branch information
smoogipoo authored Nov 8, 2024
2 parents ce9c74a + d8fc7b1 commit df0d51c
Showing 1 changed file with 21 additions and 16 deletions.
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

0 comments on commit df0d51c

Please sign in to comment.