mirror of
https://github.com/jkocon/g-helper.git
synced 2026-02-23 13:00:52 +01:00
Added GPU usage check
This commit is contained in:
@@ -73,6 +73,24 @@ public class AmdGpuTemperatureProvider : IGpuTemperatureProvider {
|
|||||||
return temperatureSensor.Value;
|
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() {
|
private void ReleaseUnmanagedResources() {
|
||||||
if (_adlContextHandle != IntPtr.Zero) {
|
if (_adlContextHandle != IntPtr.Zero) {
|
||||||
Adl2.NativeMethods.ADL2_Main_Control_Destroy(_adlContextHandle);
|
Adl2.NativeMethods.ADL2_Main_Control_Destroy(_adlContextHandle);
|
||||||
|
|||||||
@@ -3,4 +3,5 @@
|
|||||||
public interface IGpuTemperatureProvider : IDisposable {
|
public interface IGpuTemperatureProvider : IDisposable {
|
||||||
bool IsValid { get; }
|
bool IsValid { get; }
|
||||||
int? GetCurrentTemperature();
|
int? GetCurrentTemperature();
|
||||||
|
int? GetGpuUse();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +1,56 @@
|
|||||||
using NvAPIWrapper.GPU;
|
using NvAPIWrapper.GPU;
|
||||||
using NvAPIWrapper.Native;
|
using NvAPIWrapper.Native;
|
||||||
using NvAPIWrapper.Native.Exceptions;
|
|
||||||
using NvAPIWrapper.Native.GPU;
|
using NvAPIWrapper.Native.GPU;
|
||||||
using NvAPIWrapper.Native.Interfaces.GPU;
|
using NvAPIWrapper.Native.Interfaces.GPU;
|
||||||
|
|
||||||
namespace GHelper.Gpu;
|
namespace GHelper.Gpu;
|
||||||
|
|
||||||
public class NvidiaGpuTemperatureProvider : IGpuTemperatureProvider {
|
public class NvidiaGpuTemperatureProvider : IGpuTemperatureProvider
|
||||||
|
{
|
||||||
private readonly PhysicalGPU? _internalGpu;
|
private readonly PhysicalGPU? _internalGpu;
|
||||||
|
|
||||||
public NvidiaGpuTemperatureProvider() {
|
public NvidiaGpuTemperatureProvider()
|
||||||
|
{
|
||||||
_internalGpu = GetInternalDiscreteGpu();
|
_internalGpu = GetInternalDiscreteGpu();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsValid => _internalGpu != null;
|
public bool IsValid => _internalGpu != null;
|
||||||
|
|
||||||
public int? GetCurrentTemperature() {
|
public int? GetCurrentTemperature()
|
||||||
|
{
|
||||||
if (!IsValid)
|
if (!IsValid)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
PhysicalGPU internalGpu = _internalGpu!;
|
PhysicalGPU internalGpu = _internalGpu!;
|
||||||
IThermalSensor? gpuSensor =
|
IThermalSensor? gpuSensor =
|
||||||
GPUApi.GetThermalSettings(internalGpu.Handle).Sensors
|
GPUApi.GetThermalSettings(internalGpu.Handle).Sensors
|
||||||
.FirstOrDefault(s => s.Target == ThermalSettingsTarget.GPU);
|
.FirstOrDefault(s => s.Target == ThermalSettingsTarget.GPU);
|
||||||
|
|
||||||
return gpuSensor?.CurrentTemperature;
|
return gpuSensor?.CurrentTemperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private static PhysicalGPU? GetInternalDiscreteGpu() {
|
private static PhysicalGPU? GetInternalDiscreteGpu()
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
return PhysicalGPU
|
return PhysicalGPU
|
||||||
.GetPhysicalGPUs()
|
.GetPhysicalGPUs()
|
||||||
.FirstOrDefault(gpu => gpu.SystemType == SystemType.Laptop);
|
.FirstOrDefault(gpu => gpu.SystemType == SystemType.Laptop);
|
||||||
} catch {
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int? GetGpuUse()
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using GHelper;
|
using GHelper;
|
||||||
using GHelper.Gpu;
|
using GHelper.Gpu;
|
||||||
|
using NvAPIWrapper.GPU;
|
||||||
|
|
||||||
public static class HardwareMonitor
|
public static class HardwareMonitor
|
||||||
{
|
{
|
||||||
@@ -14,6 +15,9 @@ public static class HardwareMonitor
|
|||||||
public static string? gpuFan;
|
public static string? gpuFan;
|
||||||
public static string? midFan;
|
public static string? midFan;
|
||||||
|
|
||||||
|
public static List<int> gpuUsage = new List<int>();
|
||||||
|
public static int? gpuUse;
|
||||||
|
|
||||||
public static int GetFanMax()
|
public static int GetFanMax()
|
||||||
{
|
{
|
||||||
int max = 58;
|
int max = 58;
|
||||||
@@ -48,6 +52,7 @@ public static class HardwareMonitor
|
|||||||
{
|
{
|
||||||
batteryDischarge = -1;
|
batteryDischarge = -1;
|
||||||
gpuTemp = -1;
|
gpuTemp = -1;
|
||||||
|
gpuUse = -1;
|
||||||
|
|
||||||
cpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan));
|
cpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan));
|
||||||
gpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan));
|
gpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan));
|
||||||
@@ -55,7 +60,6 @@ public static class HardwareMonitor
|
|||||||
|
|
||||||
cpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_CPU);
|
cpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_CPU);
|
||||||
|
|
||||||
|
|
||||||
if (cpuTemp < 0) try
|
if (cpuTemp < 0) try
|
||||||
{
|
{
|
||||||
var ct = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM", true);
|
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");
|
Logger.WriteLine("Failed reading CPU temp");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gpuTemp < 0) try
|
try
|
||||||
{
|
{
|
||||||
gpuTemp = GpuTemperatureProvider?.GetCurrentTemperature();
|
gpuTemp = GpuTemperatureProvider?.GetCurrentTemperature();
|
||||||
|
|
||||||
@@ -80,6 +84,20 @@ public static class HardwareMonitor
|
|||||||
if (gpuTemp is null || gpuTemp < 0)
|
if (gpuTemp is null || gpuTemp < 0)
|
||||||
gpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_GPU);
|
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
|
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() {
|
public static void RecreateGpuTemperatureProviderWithDelay() {
|
||||||
|
|
||||||
// Re-enabling the discrete GPU takes a bit of time,
|
// Re-enabling the discrete GPU takes a bit of time,
|
||||||
|
|||||||
@@ -808,14 +808,14 @@ namespace GHelper
|
|||||||
HardwareMonitor.ReadSensors();
|
HardwareMonitor.ReadSensors();
|
||||||
|
|
||||||
if (HardwareMonitor.cpuTemp > 0)
|
if (HardwareMonitor.cpuTemp > 0)
|
||||||
cpuTemp = ": " + Math.Round((decimal)HardwareMonitor.cpuTemp).ToString() + "°C - ";
|
cpuTemp = ": " + Math.Round((decimal)HardwareMonitor.cpuTemp).ToString() + "°C ";
|
||||||
|
|
||||||
if (HardwareMonitor.batteryDischarge > 0)
|
if (HardwareMonitor.batteryDischarge > 0)
|
||||||
battery = "Discharging: " + Math.Round((decimal)HardwareMonitor.batteryDischarge, 1).ToString() + "W";
|
battery = "Discharging: " + Math.Round((decimal)HardwareMonitor.batteryDischarge, 1).ToString() + "W";
|
||||||
|
|
||||||
if (HardwareMonitor.gpuTemp > 0)
|
if (HardwareMonitor.gpuTemp > 0)
|
||||||
{
|
{
|
||||||
gpuTemp = $": {HardwareMonitor.gpuTemp}°C - ";
|
gpuTemp = $": {HardwareMonitor.gpuTemp}°C ";
|
||||||
}
|
}
|
||||||
|
|
||||||
Program.settingsForm.BeginInvoke(delegate
|
Program.settingsForm.BeginInvoke(delegate
|
||||||
@@ -1064,6 +1064,12 @@ namespace GHelper
|
|||||||
if (eco == 1)
|
if (eco == 1)
|
||||||
if ((GpuAuto && Plugged == PowerLineStatus.Online) || (ForceGPU && GpuMode == ASUSWmi.GPUModeStandard))
|
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);
|
SetEcoGPU(0);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1136,6 +1142,7 @@ namespace GHelper
|
|||||||
public void SetEcoGPU(int eco)
|
public void SetEcoGPU(int eco)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
ButtonEnabled(buttonOptimized, false);
|
ButtonEnabled(buttonOptimized, false);
|
||||||
ButtonEnabled(buttonEco, false);
|
ButtonEnabled(buttonEco, false);
|
||||||
ButtonEnabled(buttonStandard, false);
|
ButtonEnabled(buttonStandard, false);
|
||||||
|
|||||||
Reference in New Issue
Block a user