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