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

@@ -73,6 +73,24 @@ public class AmdGpuTemperatureProvider : IGpuTemperatureProvider {
return temperatureSensor.Value;
}
public int? GetGpuUse()
{
if (!IsValid)
return -1;
if (Adl2.NativeMethods.ADL2_New_QueryPMLogData_Get(_adlContextHandle, _internalDiscreteAdapter.AdapterIndex, out ADLPMLogDataOutput adlpmLogDataOutput) != Adl2.ADL_SUCCESS)
return -1;
ADLSingleSensorData gpuUsage = adlpmLogDataOutput.Sensors[(int)ADLSensorType.PMLOG_INFO_ACTIVITY_GFX];
if (gpuUsage.Supported == 0)
return -1;
return gpuUsage.Value;
}
private void ReleaseUnmanagedResources() {
if (_adlContextHandle != IntPtr.Zero) {
Adl2.NativeMethods.ADL2_Main_Control_Destroy(_adlContextHandle);

View File

@@ -3,4 +3,5 @@
public interface IGpuTemperatureProvider : IDisposable {
bool IsValid { get; }
int? GetCurrentTemperature();
int? GetGpuUse();
}

View File

@@ -1,21 +1,23 @@
using NvAPIWrapper.GPU;
using NvAPIWrapper.Native;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.GPU;
using NvAPIWrapper.Native.Interfaces.GPU;
namespace GHelper.Gpu;
public class NvidiaGpuTemperatureProvider : IGpuTemperatureProvider {
public class NvidiaGpuTemperatureProvider : IGpuTemperatureProvider
{
private readonly PhysicalGPU? _internalGpu;
public NvidiaGpuTemperatureProvider() {
public NvidiaGpuTemperatureProvider()
{
_internalGpu = GetInternalDiscreteGpu();
}
public bool IsValid => _internalGpu != null;
public int? GetCurrentTemperature() {
public int? GetCurrentTemperature()
{
if (!IsValid)
return null;
@@ -27,16 +29,28 @@ public class NvidiaGpuTemperatureProvider : IGpuTemperatureProvider {
return gpuSensor?.CurrentTemperature;
}
public void Dispose() {
public void Dispose()
{
}
private static PhysicalGPU? GetInternalDiscreteGpu() {
try {
private static PhysicalGPU? GetInternalDiscreteGpu()
{
try
{
return PhysicalGPU
.GetPhysicalGPUs()
.FirstOrDefault(gpu => gpu.SystemType == SystemType.Laptop);
} catch {
}
catch
{
return null;
}
}
public int? GetGpuUse()
{
return -1;
}
}

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,

View File

@@ -808,14 +808,14 @@ namespace GHelper
HardwareMonitor.ReadSensors();
if (HardwareMonitor.cpuTemp > 0)
cpuTemp = ": " + Math.Round((decimal)HardwareMonitor.cpuTemp).ToString() + "°C - ";
cpuTemp = ": " + Math.Round((decimal)HardwareMonitor.cpuTemp).ToString() + "°C ";
if (HardwareMonitor.batteryDischarge > 0)
battery = "Discharging: " + Math.Round((decimal)HardwareMonitor.batteryDischarge, 1).ToString() + "W";
if (HardwareMonitor.gpuTemp > 0)
{
gpuTemp = $": {HardwareMonitor.gpuTemp}°C - ";
gpuTemp = $": {HardwareMonitor.gpuTemp}°C ";
}
Program.settingsForm.BeginInvoke(delegate
@@ -1064,6 +1064,12 @@ namespace GHelper
if (eco == 1)
if ((GpuAuto && Plugged == PowerLineStatus.Online) || (ForceGPU && GpuMode == ASUSWmi.GPUModeStandard))
{
if (HardwareMonitor.IsUsedGPU())
{
DialogResult dialogResult = MessageBox.Show("Your dGPU seem to be in heavy use, disable it?", "Eco Mode", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No) return false;
}
SetEcoGPU(0);
return true;
}
@@ -1136,6 +1142,7 @@ namespace GHelper
public void SetEcoGPU(int eco)
{
ButtonEnabled(buttonOptimized, false);
ButtonEnabled(buttonEco, false);
ButtonEnabled(buttonStandard, false);