-
Notifications
You must be signed in to change notification settings - Fork 12
/
ADLXPlugin.cs
121 lines (102 loc) · 3.77 KB
/
ADLXPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using ADLXWrapper;
using FanControl.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FanControl.ADLX
{
public class ADLXPlugin : IPlugin2
{
private readonly IPluginLogger _pluginLogger;
private readonly IPluginDialog _pluginDialog;
private ADLXWrapper.ADLXWrapper _wrapper;
private SystemServices _system;
private IReadOnlyList<GPU> _gpus;
private GPUTuningService _tuning;
private PerformanceMonitor _perf;
private Dictionary<int, ManualFanTuning> _fans;
private IDisposable _tracking;
private bool _initialized;
private GPUMetricsProvider[] _metricsProviders;
public ADLXPlugin(IPluginLogger pluginLogger, IPluginDialog pluginDialog)
{
_pluginLogger = pluginLogger;
_pluginDialog = pluginDialog;
}
public string Name => "ADLX";
public void Initialize()
{
try
{
_wrapper = new ADLXWrapper.ADLXWrapper();
_system = _wrapper.GetSystemServices();
_gpus = _system.GetGPUs();
_tuning = _system.GetGPUTuningService();
_perf = _system.GetPerformanceMonitor();
_fans = _gpus.Where(_tuning.IsManualFanTuningSupported)
.ToDictionary(x => x.UniqueId, x => _tuning.GetManualFanTuning(x));
//_tracking = _perf.StartTracking(1000);
_metricsProviders = _gpus.Select(x => new GPUMetricsProvider(_perf, x)).ToArray();
_initialized = true;
}
catch (Exception ex)
{
Log(ex.ToString());
DisposeAll();
_initialized = false;
}
}
public void Load(IPluginSensorsContainer _container)
{
if (!_initialized)
{
return;
}
ADLXControl[] controls = _gpus.Where(x => _fans.ContainsKey(x.UniqueId)).Select(x => new ADLXControl(x, _fans[x.UniqueId])).ToArray();
ADLXFanSensor[] fanSensors = _gpus.Zip(_metricsProviders, (gpu, m) => new ADLXFanSensor(gpu, m)).ToArray();
ADLXTemperatureSensor[] hotspots = _gpus.Zip(_metricsProviders, (gpu, m) => new ADLXTemperatureSensor("Hotspot", gpu, () => m.Current.GPUHotspotTemperature)).ToArray();
ADLXTemperatureSensor[] gpuTemps = _gpus.Zip(_metricsProviders, (gpu, m) => new ADLXTemperatureSensor("GPU", gpu, () => m.Current.GPUTemperature)).ToArray();
foreach (var control in controls)
{
_container.ControlSensors.Add(control);
}
foreach (var fan in fanSensors)
{
_container.FanSensors.Add(fan);
}
foreach (var temp in hotspots.Concat(gpuTemps))
{
_container.TempSensors.Add(temp);
}
}
public void Update()
{
if (!_initialized) return;
foreach (var provider in _metricsProviders)
provider.UpdateMetrics();
}
public void Close()
{
if (!_initialized)
return;
DisposeAll();
_initialized = false;
}
private void Log(string message)
{
_pluginLogger.Log($"ADLX plugin: {message}");
}
private void DisposeAll()
{
_tracking?.Dispose();
_fans?.Values.ToList().ForEach(x => x.Dispose());
_fans.Clear();
_perf?.Dispose();
_tuning?.Dispose();
_gpus?.ToList().ForEach(x => x.Dispose());
_gpus = null;
_system?.Dispose();
_wrapper?.Dispose();
}
}
}