Added GPU usage check

This commit is contained in:
seerge
2023-04-05 19:33:53 +02:00
parent 8d119b386d
commit cf3a84aa3d
5 changed files with 79 additions and 15 deletions

View File

@@ -1,6 +1,7 @@
using System.Diagnostics;
using GHelper;
using GHelper.Gpu;
using NvAPIWrapper.GPU;
public static class HardwareMonitor
{
@@ -14,6 +15,9 @@ public static class HardwareMonitor
public static string? gpuFan;
public static string? midFan;
public static List<int> gpuUsage = new List<int>();
public static int? gpuUse;
public static int GetFanMax()
{
int max = 58;
@@ -48,6 +52,7 @@ public static class HardwareMonitor
{
batteryDischarge = -1;
gpuTemp = -1;
gpuUse = -1;
cpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan));
gpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan));
@@ -55,7 +60,6 @@ public static class HardwareMonitor
cpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_CPU);
if (cpuTemp < 0) try
{
var ct = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM", true);
@@ -66,7 +70,7 @@ public static class HardwareMonitor
Logger.WriteLine("Failed reading CPU temp");
}
if (gpuTemp < 0) try
try
{
gpuTemp = GpuTemperatureProvider?.GetCurrentTemperature();
@@ -80,6 +84,20 @@ public static class HardwareMonitor
if (gpuTemp is null || gpuTemp < 0)
gpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_GPU);
try
{
gpuUse = GpuTemperatureProvider?.GetGpuUse();
} catch (Exception ex)
{
gpuUse = -1;
Debug.WriteLine(ex.ToString());
}
if (gpuUse is not null && gpuUse >= 0)
{
gpuUsage.Add((int)gpuUse);
if (gpuUsage.Count > 3) gpuUsage.RemoveAt(0);
}
try
{
@@ -94,6 +112,12 @@ public static class HardwareMonitor
}
}
public static bool IsUsedGPU(int threshold = 50)
{
if (gpuUsage.Count < 2) return false;
return (gpuUsage.Average() > threshold);
}
public static void RecreateGpuTemperatureProviderWithDelay() {
// Re-enabling the discrete GPU takes a bit of time,