|
1 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
2 | 7 | using System.Threading.Tasks; |
3 | 8 | using RAMSPDToolkit.I2CSMBus; |
4 | 9 | using RAMSPDToolkit.SPD; |
|
9 | 14 |
|
10 | 15 | namespace OpenHardwareMonitor.Hardware.Memory; |
11 | 16 |
|
12 | | -internal class MemoryGroup : IGroup |
| 17 | +internal class MemoryGroup : IGroup, IHardwareChanged |
13 | 18 | { |
14 | | - //Retry 12x |
15 | | - private const int RetryCount = 12; |
16 | | - //Retry every 2.5 seconds |
17 | | - private const int RetryTime = 2500; |
18 | 19 | private static readonly object _lock = new(); |
| 20 | + private List<Hardware> _hardware = []; |
19 | 21 |
|
20 | | - private readonly List<Hardware> _hardware = []; |
| 22 | + private CancellationTokenSource _cancellationTokenSource; |
| 23 | + private Exception _lastException; |
| 24 | + private bool _opened = false; |
21 | 25 |
|
22 | | - static MemoryGroup() |
| 26 | + public MemoryGroup(ISettings settings) |
23 | 27 | { |
24 | | - if (OperatingSystemHelper.IsAdministrator() && Ring0.IsOpen) |
| 28 | + if (Ring0.IsOpen && (DriverManager.Driver is null || !DriverManager.Driver.IsOpen)) |
25 | 29 | { |
26 | | - //Assign implementation of IDriver |
| 30 | + // Assign implementation of IDriver. |
27 | 31 | DriverManager.Driver = new RAMSPDToolkitDriver(Ring0.KernelDriver); |
28 | 32 | SMBusManager.UseWMI = false; |
29 | 33 | } |
30 | | - } |
31 | 34 |
|
32 | | - public MemoryGroup(ISettings settings) |
33 | | - { |
34 | | - _hardware.Add(new TotalMemory(settings)); |
35 | 35 | _hardware.Add(new VirtualMemory(settings)); |
| 36 | + _hardware.Add(new TotalMemory(settings)); |
36 | 37 |
|
37 | | - if (OperatingSystemHelper.IsAdministrator()) |
| 38 | + if (DriverManager.Driver == null) |
38 | 39 | { |
39 | | - Task.Run(async () => |
40 | | - { |
41 | | - var _elapsedCounter = 0; |
42 | | - while (true) |
43 | | - { |
44 | | - //SMBus might not be detected right after boot |
45 | | - if (DetectThermalSensors(out var accessors) || _elapsedCounter++ >= RetryCount) |
46 | | - { |
47 | | - if (accessors != null) |
48 | | - AddDimms(accessors, settings); |
49 | | - break; |
50 | | - } |
51 | | - await Task.Delay(RetryTime); |
52 | | - } |
53 | | - }); |
| 40 | + return; |
54 | 41 | } |
55 | | - } |
56 | 42 |
|
57 | | - public string GetReport() |
58 | | - { |
59 | | - return null; |
| 43 | + if (!TryAddDimms(settings)) |
| 44 | + { |
| 45 | + StartRetryTask(settings); |
| 46 | + } |
| 47 | + |
| 48 | + _opened = true; |
60 | 49 | } |
61 | 50 |
|
| 51 | + public event HardwareEventHandler HardwareAdded; |
| 52 | + public event HardwareEventHandler HardwareRemoved; |
| 53 | + |
62 | 54 | public IReadOnlyList<IHardware> Hardware => _hardware; |
63 | 55 |
|
64 | | - public void Close() |
| 56 | + public string GetReport() |
65 | 57 | { |
66 | | - foreach (Hardware ram in _hardware) |
67 | | - ram.Close(); |
| 58 | + StringBuilder report = new(); |
| 59 | + report.AppendLine("Memory Report:"); |
| 60 | + if (_lastException != null) |
| 61 | + { |
| 62 | + report.AppendLine($"Error while detecting memory: {_lastException.Message}"); |
| 63 | + } |
| 64 | + |
| 65 | + foreach (Hardware hardware in _hardware) |
| 66 | + { |
| 67 | + report.AppendLine($"{hardware.Name} ({hardware.Identifier}):"); |
| 68 | + report.AppendLine(); |
| 69 | + foreach (ISensor sensor in hardware.Sensors) |
| 70 | + { |
| 71 | + report.AppendLine($"{sensor.Name}: {sensor.Value?.ToString() ?? "No value"}"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return report.ToString(); |
68 | 76 | } |
69 | 77 |
|
70 | | - private static bool DetectThermalSensors(out List<SPDAccessor> accessors) |
| 78 | + public void Close() |
71 | 79 | { |
72 | 80 | lock (_lock) |
73 | 81 | { |
74 | | - var list = new List<SPDAccessor>(); |
| 82 | + _opened = false; |
| 83 | + foreach (Hardware ram in _hardware) |
| 84 | + ram.Close(); |
75 | 85 |
|
76 | | - bool ramDetected = false; |
| 86 | + _hardware.Clear(); |
77 | 87 |
|
78 | | - SMBusManager.DetectSMBuses(); |
| 88 | + _cancellationTokenSource?.Cancel(); |
| 89 | + _cancellationTokenSource?.Dispose(); |
| 90 | + _cancellationTokenSource = null; |
| 91 | + } |
| 92 | + } |
79 | 93 |
|
80 | | - //Go through detected SMBuses |
81 | | - foreach (var smbus in SMBusManager.RegisteredSMBuses) |
| 94 | + private bool TryAddDimms(ISettings settings) |
| 95 | + { |
| 96 | + try |
| 97 | + { |
| 98 | + lock (_lock) |
82 | 99 | { |
83 | | - //Go through possible RAM slots |
84 | | - for (byte i = SPDConstants.SPD_BEGIN; i <= SPDConstants.SPD_END; ++i) |
| 100 | + if (!_opened) |
85 | 101 | { |
86 | | - //Detect type of RAM, if available |
87 | | - var detector = new SPDDetector(smbus, i); |
| 102 | + return true; |
| 103 | + } |
88 | 104 |
|
89 | | - //RAM available and detected |
90 | | - if (detector.Accessor != null) |
| 105 | + if (DetectThermalSensors(out List<SPDAccessor> accessors)) |
| 106 | + { |
| 107 | + AddDimms(accessors, settings); |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + _lastException = ex; |
| 115 | + Debug.Assert(false, "Exception while detecting RAM: " + ex.Message); |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + private void StartRetryTask(ISettings settings) |
| 122 | + { |
| 123 | + _cancellationTokenSource = new CancellationTokenSource(); |
| 124 | + |
| 125 | + Task.Run(async () => |
| 126 | + { |
| 127 | + int retryRemaining = 5; |
| 128 | + |
| 129 | + while (!_cancellationTokenSource.IsCancellationRequested && --retryRemaining > 0) |
| 130 | + { |
| 131 | + await Task.Delay(TimeSpan.FromSeconds(2.5), _cancellationTokenSource.Token).ConfigureAwait(false); |
| 132 | + |
| 133 | + if (TryAddDimms(settings)) |
| 134 | + { |
| 135 | + lock (_lock) |
91 | 136 | { |
92 | | - //We are only interested in modules with thermal sensor |
93 | | - if (detector.Accessor is IThermalSensor { HasThermalSensor: true }) |
94 | | - list.Add(detector.Accessor); |
| 137 | + if (!_opened) |
| 138 | + { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + foreach (Hardware hardware in _hardware.OfType<DimmMemory>()) |
| 143 | + { |
| 144 | + HardwareAdded?.Invoke(hardware); |
| 145 | + } |
95 | 146 |
|
96 | | - ramDetected = true; |
| 147 | + _cancellationTokenSource.Dispose(); |
| 148 | + _cancellationTokenSource = null; |
| 149 | + |
| 150 | + break; |
97 | 151 | } |
| 152 | + |
98 | 153 | } |
99 | 154 | } |
| 155 | + }, _cancellationTokenSource.Token); |
| 156 | + } |
100 | 157 |
|
101 | | - accessors = list.Count > 0 ? list : []; |
102 | | - return ramDetected; |
| 158 | + private static bool DetectThermalSensors(out List<SPDAccessor> accessors) |
| 159 | + { |
| 160 | + accessors = []; |
| 161 | + |
| 162 | + bool ramDetected = false; |
| 163 | + |
| 164 | + SMBusManager.DetectSMBuses(); |
| 165 | + |
| 166 | + //Go through detected SMBuses |
| 167 | + foreach (SMBusInterface smbus in SMBusManager.RegisteredSMBuses) |
| 168 | + { |
| 169 | + //Go through possible RAM slots |
| 170 | + for (byte i = SPDConstants.SPD_BEGIN; i <= SPDConstants.SPD_END; ++i) |
| 171 | + { |
| 172 | + //Detect type of RAM, if available |
| 173 | + SPDDetector detector = new(smbus, i); |
| 174 | + |
| 175 | + //RAM available and detected |
| 176 | + if (detector.Accessor != null) |
| 177 | + { |
| 178 | + //We are only interested in modules with thermal sensor |
| 179 | + if (detector.Accessor is IThermalSensor { HasThermalSensor: true }) |
| 180 | + accessors.Add(detector.Accessor); |
| 181 | + |
| 182 | + ramDetected = true; |
| 183 | + } |
| 184 | + } |
103 | 185 | } |
| 186 | + |
| 187 | + return ramDetected; |
104 | 188 | } |
105 | 189 |
|
106 | 190 | private void AddDimms(List<SPDAccessor> accessors, ISettings settings) |
107 | 191 | { |
108 | | - foreach (var ram in accessors) |
| 192 | + List<Hardware> newHardwareList = [.. _hardware]; |
| 193 | + |
| 194 | + foreach (SPDAccessor ram in accessors) |
109 | 195 | { |
110 | 196 | //Default value |
111 | 197 | string name = $"DIMM #{ram.Index}"; |
112 | 198 |
|
113 | 199 | //Check if we can switch to the correct page |
114 | 200 | if (ram.ChangePage(PageData.ModulePartNumber)) |
115 | | - { |
116 | 201 | name = $"{ram.GetModuleManufacturerString()} - {ram.ModulePartNumber()} (#{ram.Index})"; |
117 | | - } |
118 | 202 |
|
119 | | - var memory = new DimmMemory(ram, name, new Identifier("ram"), settings); |
120 | | - |
121 | | - _hardware.Add(memory); |
| 203 | + DimmMemory memory = new(ram, name, new Identifier($"memory/dimm/{ram.Index}"), settings); |
| 204 | + newHardwareList.Add(memory); |
122 | 205 | } |
| 206 | + |
| 207 | + _hardware = newHardwareList; |
123 | 208 | } |
124 | 209 | } |
0 commit comments