Skip to content

Commit

Permalink
Merge branch 'seerge:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaYslmn authored Apr 6, 2023
2 parents 91965ed + f2ef6d1 commit 3acfe89
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 76 deletions.
2 changes: 1 addition & 1 deletion app/ASUSWmi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class ASUSWmi
public const int GPUModeUltimate = 2;


public const int MaxTotal = 150;
public const int MaxTotal = 180;
public const int MinTotal = 5;
public const int DefaultTotal = 125;

Expand Down
42 changes: 21 additions & 21 deletions app/Fans.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/Fans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void SetChart(Chart chart, int device)
chart.ChartAreas[0].AxisY.Maximum = 100;

chart.ChartAreas[0].AxisY.LabelStyle.Font = new Font("Arial", 7F);

chart.ChartAreas[0].AxisX.MajorGrid.LineColor = chartGrid;
chart.ChartAreas[0].AxisY.MajorGrid.LineColor = chartGrid;
chart.ChartAreas[0].AxisX.LineColor = chartGrid;
Expand Down
18 changes: 18 additions & 0 deletions app/Gpu/AmdGpuTemperatureProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public AmdGpuTemperatureProvider() {
return temperatureSensor.Value;
}


public int? GetGpuUse()
{
if (!IsValid)
return null;

if (Adl2.NativeMethods.ADL2_New_QueryPMLogData_Get(_adlContextHandle, _internalDiscreteAdapter.AdapterIndex, out ADLPMLogDataOutput adlpmLogDataOutput) != Adl2.ADL_SUCCESS)
return null;

ADLSingleSensorData gpuUsage = adlpmLogDataOutput.Sensors[(int)ADLSensorType.PMLOG_INFO_ACTIVITY_GFX];
if (gpuUsage.Supported == 0)
return null;

return gpuUsage.Value;

}


private void ReleaseUnmanagedResources() {
if (_adlContextHandle != IntPtr.Zero) {
Adl2.NativeMethods.ADL2_Main_Control_Destroy(_adlContextHandle);
Expand Down
1 change: 1 addition & 0 deletions app/Gpu/IGpuTemperatureProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface IGpuTemperatureProvider : IDisposable {
bool IsValid { get; }
int? GetCurrentTemperature();
int? GetGpuUse();
}
48 changes: 37 additions & 11 deletions app/Gpu/NvidiaGpuTemperatureProvider.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
using NvAPIWrapper.GPU;
using NvAPIWrapper.Native;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.GPU;
using NvAPIWrapper.Native.Interfaces.GPU;

namespace GHelper.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;

PhysicalGPU internalGpu = _internalGpu!;
IThermalSensor? gpuSensor =
IThermalSensor? gpuSensor =
GPUApi.GetThermalSettings(internalGpu.Handle).Sensors
.FirstOrDefault(s => s.Target == ThermalSettingsTarget.GPU);

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()
{
if (!IsValid)
return null;

PhysicalGPU internalGpu = _internalGpu!;

IUtilizationDomainInfo? gpuUsage = GPUApi.GetUsages(internalGpu.Handle).GPU;

if (gpuUsage == null)
return null;

return
(int)gpuUsage?.Percentage;

}

}
66 changes: 49 additions & 17 deletions app/HardwareMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Diagnostics;
using GHelper;
using GHelper;
using GHelper.Gpu;
using System.Diagnostics;

public static class HardwareMonitor
{
Expand All @@ -14,6 +14,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;
Expand Down Expand Up @@ -41,45 +44,66 @@ private static string FormatFan(int fan)
if (Program.config.getConfig("fan_rpm") == 1)
return " Fan: " + (fan * 100).ToString() + "RPM";
else
return " Fan: " + Math.Min(Math.Round((float)fan/fanMax*100), 100).ToString() + "%"; // relatively to 6000 rpm
return " Fan: " + Math.Min(Math.Round((float)fan / fanMax * 100), 100).ToString() + "%"; // relatively to 6000 rpm
}

private static int GetGpuUse()
{
try
{
int? gpuUse = GpuTemperatureProvider?.GetGpuUse();
if (gpuUse is not null) return (int)gpuUse;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}

return 0;
}

public static void ReadSensors()
{
batteryDischarge = -1;
gpuTemp = -1;
gpuUse = -1;

cpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.CPU_Fan));
gpuFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.GPU_Fan));
midFan = FormatFan(Program.wmi.DeviceGet(ASUSWmi.Mid_Fan));

cpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_CPU);


if (cpuTemp < 0) try
{
var ct = new PerformanceCounter("Thermal Zone Information", "Temperature", @"\_TZ.THRM", true);
cpuTemp = ct.NextValue() - 273;
ct.Dispose();
} catch
}
catch
{
Logger.WriteLine("Failed reading CPU temp");
Debug.WriteLine("Failed reading CPU temp");
}

if (gpuTemp < 0) try
try
{
gpuTemp = GpuTemperatureProvider?.GetCurrentTemperature();
gpuTemp = GpuTemperatureProvider?.GetCurrentTemperature();

}
catch (Exception ex) {
catch (Exception ex)
{
gpuTemp = -1;
Logger.WriteLine("Failed reading GPU temp");
Logger.WriteLine(ex.ToString());
Debug.WriteLine("Failed reading GPU temp");
Debug.WriteLine(ex.ToString());
}

if (gpuTemp is null || gpuTemp < 0)
gpuTemp = Program.wmi.DeviceGet(ASUSWmi.Temp_GPU);

/*
gpuUsage.Add(GetGpuUse());
if (gpuUsage.Count > 3) gpuUsage.RemoveAt(0);
*/

try
{
Expand All @@ -90,15 +114,22 @@ public static void ReadSensors()
}
catch
{
Logger.WriteLine("Failed reading Battery discharge");
Debug.WriteLine("Failed reading Battery discharge");
}
}

public static void RecreateGpuTemperatureProviderWithDelay() {
public static bool IsUsedGPU(int threshold = 50)
{
return (GetGpuUse() > threshold);
}

public static void RecreateGpuTemperatureProviderWithDelay()
{

// Re-enabling the discrete GPU takes a bit of time,
// so a simple workaround is to refresh again after that happens
Task.Run(async () => {
Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(5));
RecreateGpuTemperatureProvider();
});
Expand All @@ -107,7 +138,8 @@ public static void RecreateGpuTemperatureProviderWithDelay() {

}

public static void RecreateGpuTemperatureProvider() {
public static void RecreateGpuTemperatureProvider()
{
try
{
GpuTemperatureProvider?.Dispose();
Expand All @@ -134,8 +166,8 @@ public static void RecreateGpuTemperatureProvider() {
GpuTemperatureProvider = null;
}
catch (Exception ex)
{
{
Debug.WriteLine(ex.ToString());
}
}
}
}
Loading

0 comments on commit 3acfe89

Please sign in to comment.