Skip to content

Commit 7529be5

Browse files
committed
Optimization DeviceIO
1 parent 018e9eb commit 7529be5

File tree

7 files changed

+92
-122
lines changed

7 files changed

+92
-122
lines changed

Hardware/Hardware.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="LibreHardwareMonitorLib" Version="0.8.9-pre47" />
12+
<PackageReference Include="LibreHardwareMonitorLib" Version="0.8.9-pre50" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
namespace Hardware.Windows.DeviceIoInfo
1+
using System;
2+
using DeviceIOControl;
3+
using DeviceIOControl.Disc;
4+
5+
namespace Hardware.Windows.DeviceIoInfo
26
{
3-
public class Device
7+
public class Device : IDisposable
48
{
9+
public short Index { get; set; }
510
public string Letter { get; set; }
611
public VendorEnum Vendor;
712
public string Model { get; set; }
8-
public byte DeviceID { get; set; }
913
public long TotalSpace { get; set; }
1014
public long FreeSpace { get; set; }
1115
public long UsedSpace { get; set; }
1216
public byte PercentUsedSpace { get; set; }
1317
public float TotalWrite { get; set; }
1418
public byte Health { get; set; }
1519
public bool IsSmart { get; set; }
20+
public float PrevReadIo { get; set; }
21+
public float PrevWriteIo { get; set; }
22+
public DeviceIoControl DeviceControl { get; set; }
23+
public Performance DevicePerformance { get; set; }
1624

1725
public Device()
1826
{
1927
IsSmart = true;
2028
}
29+
30+
public void Dispose()
31+
{
32+
DevicePerformance?.Dispose();
33+
DeviceControl?.Dispose();
34+
}
2135
}
2236
}

Hardware/Windows/DeviceIoInfo/DeviceInfo.cs

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,63 @@
44
using System.IO;
55
using DeviceIOControl;
66
using DeviceIOControl.Native;
7-
using DeviceIOControl.Disc;
87
using DeviceIOControl.Disc.Smart;
98

109
namespace Hardware.Windows.DeviceIoInfo
1110
{
1211
public static class DeviceInfo
1312
{
14-
public static float ReadIO { get; private set; }
15-
public static float WriteIO { get; private set; }
13+
public static float ReadIo { get; private set; }
14+
public static float WriteIo { get; private set; }
1615

1716
public static List<Device> LogicalDrives { get; }
18-
private static Dictionary<string, DeviceIoControl> _deviceIoList;
1917

20-
private static Performance[] _perfomances;
21-
private static float[] _prevReadValues;
22-
private static float[] _prevWriteValues;
23-
2418
static DeviceInfo()
2519
{
2620
LogicalDrives = new();
27-
_deviceIoList = new();
2821
}
2922

3023
private static void SetDiscCollection()
3124
{
32-
LogicalDrives.Clear();
33-
_deviceIoList.Clear();
25+
Close();
3426

3527
IEnumerable<LogicalDrive> logicalDevicesIo = DeviceIoControl.GetLogicalDrives();
3628

29+
short devIndex = 0;
30+
3731
foreach (LogicalDrive logical in logicalDevicesIo)
3832
{
3933
if (logical.Type != WinApi.DRIVE.FIXED)
4034
continue;
4135

42-
Device device = new();
43-
device.Letter = logical.Name;
36+
Device device = new()
37+
{
38+
Index = devIndex,
39+
Letter = logical.Name,
40+
DeviceControl = new DeviceIoControl(logical.Name)
41+
};
42+
device.DevicePerformance = device.DeviceControl.Disc.GetDiscPerformance();
4443

4544
LogicalDrives.Add(device);
46-
_deviceIoList.Add(device.Letter, new DeviceIoControl(device.Letter));
47-
}
48-
49-
byte index = 0;
50-
51-
_prevReadValues = new float[LogicalDrives.Count];
52-
_prevWriteValues = new float[LogicalDrives.Count];
53-
_perfomances = new Performance[LogicalDrives.Count];
5445

46+
devIndex++;
47+
}
48+
5549
foreach (Device dev in LogicalDrives)
5650
{
57-
var smart = _deviceIoList[dev.Letter].Disc.Smart;
51+
SmartInfoCollection smart = dev.DeviceControl.Disc.Smart;
5852

59-
if (smart != null) dev.Model = smart.SystemParams.ModelNumber.Trim(' ');
60-
else dev.Model = new DriveInfo(dev.Letter).VolumeLabel;
53+
dev.Model = smart != null ? smart.SystemParams.ModelNumber.Trim(' ') : new DriveInfo(dev.Letter).VolumeLabel;
6154

6255
if (dev.Model.Contains("samsung", StringComparison.OrdinalIgnoreCase)) dev.Vendor = VendorEnum.Samsung;
6356
else if (dev.Model.Contains("kingston", StringComparison.OrdinalIgnoreCase)) dev.Vendor = VendorEnum.Kingston;
6457
else if (dev.Model.Contains("wdc", StringComparison.OrdinalIgnoreCase)) dev.Vendor = VendorEnum.WDC;
6558
else dev.Vendor = VendorEnum.Other;
6659

67-
_perfomances[index] = _deviceIoList[dev.Letter].Disc.GetDiscPerformance();
68-
69-
DiscApi.DISK_PERFORMANCE diskPerformance = _perfomances[index].QueryPerformanceInfo();
60+
DiscApi.DISK_PERFORMANCE diskPerformance = dev.DevicePerformance.QueryPerformanceInfo();
7061

71-
_prevReadValues[index] = diskPerformance.BytesRead;
72-
_prevWriteValues[index] = diskPerformance.BytesWritten;
73-
74-
index++;
62+
dev.PrevReadIo = diskPerformance.BytesRead;
63+
dev.PrevWriteIo = diskPerformance.BytesWritten;
7564
}
7665
}
7766

@@ -90,7 +79,7 @@ public static bool IsUpdateDiscs()
9079
public static void UpdateSizeOfDiscs()
9180
{
9281
if (LogicalDrives == null) SetDiscCollection();
93-
82+
9483
foreach (Device logic in LogicalDrives)
9584
{
9685
DriveInfo drive = new(logic.Letter);
@@ -112,54 +101,50 @@ public static void UpdateInfoOfDiscs()
112101
{
113102
if (LogicalDrives == null) SetDiscCollection();
114103

115-
foreach (Device logical in LogicalDrives)
104+
foreach (Device dev in LogicalDrives)
116105
{
117-
SmartInfoCollection smart = _deviceIoList[logical.Letter].Disc.Smart;
106+
SmartInfoCollection smart = dev.DeviceControl.Disc.Smart;
107+
108+
if (smart == null) continue;
109+
110+
DiscApi.DRIVEATTRIBUTE[] driveAttributes = smart.GetAttributes();
118111

119-
if (smart != null)
112+
foreach (DiscApi.DRIVEATTRIBUTE attribute in driveAttributes)
120113
{
121-
DiscApi.DRIVEATTRIBUTE[] driveAttributes = smart.GetAttributes();
114+
if (dev.Vendor == VendorEnum.Samsung)
115+
{
116+
if (attribute.bAttrID == 177) dev.Health = attribute.bAttrValue;
117+
if (attribute.bAttrID == 241) dev.TotalWrite = attribute.RawValue / (float)int.MaxValue;
118+
}
122119

123-
foreach (DiscApi.DRIVEATTRIBUTE attribute in driveAttributes)
120+
if (dev.Vendor == VendorEnum.Kingston)
124121
{
125-
if (logical.Vendor == VendorEnum.Samsung)
126-
{
127-
if (attribute.bAttrID == 177) logical.Health = attribute.bAttrValue;
128-
if (attribute.bAttrID == 241) logical.TotalWrite = attribute.RawValue / (float)int.MaxValue;
129-
}
130-
131-
if (logical.Vendor == VendorEnum.Kingston)
132-
{
133-
if (attribute.bAttrID == 231) logical.Health = attribute.bAttrValue;
134-
if (attribute.bAttrID == 241) logical.TotalWrite = attribute.RawValue / 1024f;
135-
}
122+
if (attribute.bAttrID == 231) dev.Health = attribute.bAttrValue;
123+
if (attribute.bAttrID == 241) dev.TotalWrite = attribute.RawValue / 1024f;
136124
}
137125
}
138126
}
139127
}
140128

141-
public static void PerfomanceDiskUpdate(byte index)
129+
public static void PerfomanceDiskUpdate(short index)
142130
{
143-
DiscApi.DISK_PERFORMANCE dISK_PERFORMANCE = _perfomances[index].QueryPerformanceInfo();
131+
DiscApi.DISK_PERFORMANCE dIskPerformance = LogicalDrives[index].DevicePerformance.QueryPerformanceInfo();
144132

145-
ReadIO = (dISK_PERFORMANCE.BytesRead - _prevReadValues[index]) / 1000000;
146-
WriteIO = (dISK_PERFORMANCE.BytesWritten - _prevWriteValues[index]) / 1000000;
133+
ReadIo = (dIskPerformance.BytesRead - LogicalDrives[index].PrevReadIo) / 1000000;
134+
WriteIo = (dIskPerformance.BytesWritten - LogicalDrives[index].PrevWriteIo) / 1000000;
147135

148-
_prevReadValues[index] = dISK_PERFORMANCE.BytesRead;
149-
_prevWriteValues[index] = dISK_PERFORMANCE.BytesWritten;
136+
LogicalDrives[index].PrevReadIo = dIskPerformance.BytesRead;
137+
LogicalDrives[index].PrevWriteIo = dIskPerformance.BytesWritten;
150138
}
151139

152140
public static void Close()
153141
{
154-
foreach (Performance performance in _perfomances)
155-
{
156-
performance?.Dispose();
157-
}
158-
159-
foreach (DeviceIoControl device in _deviceIoList.Values)
142+
foreach (Device dev in LogicalDrives)
160143
{
161-
device.Dispose();
144+
dev.Dispose();
162145
}
146+
147+
LogicalDrives.Clear();
163148
}
164149
}
165150
}

SysMonAvalonia/Data/DeviceIoData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class DeviceIoData : ReactiveObject
1818
private float _readIO;
1919
private readonly ChartModel _chartModel = new();
2020

21+
public short Index { get; set; }
22+
2123
public string Model
2224
{
2325
get => _model;

SysMonAvalonia/Models/ChartModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ChartModel
1616
public ISeries[] Series { get; set; }
1717
public Axis[] XAxis { get; set; }
1818
public Axis[] YAxis { get; set; }
19-
public SolidColorPaint Fill {
19+
public SolidColorPaint? Fill {
2020
set => _lineSeries.Fill = value;
2121
}
2222
public double Value

SysMonAvalonia/Models/DeviceIoModel.cs

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.IO;
5+
using System.Linq;
56
using System.Reactive.Linq;
7+
using Avalonia;
68
using Avalonia.Controls;
79
using DynamicData;
810
using Hardware.Windows.DeviceIoInfo;
@@ -39,9 +41,10 @@ public void UpdateDeviceCollection()
3941
if (ExistDeviceInCollection(dev)) continue;
4042

4143
DeviceIoData device = new();
44+
device.Index = dev.Index;
4245
device.Model = dev.Model;
4346
device.Letter = dev.Letter;
44-
device.DeviceChart.Fill = App.Current.FindResource("DeviceChartFill") as SolidColorPaint;
47+
device.DeviceChart.Fill = Application.Current.FindResource("DeviceChartFill") as SolidColorPaint;
4548

4649
_deviceList.Add(device);
4750
}
@@ -59,20 +62,12 @@ public void UpdateDeviceSpace()
5962
UpdateDeviceCollection();
6063
DeviceInfo.UpdateSizeOfDiscs();
6164
}
62-
65+
6366
foreach (DeviceIoData devData in _deviceList.Items)
6467
{
65-
foreach (Device dev in DeviceInfo.LogicalDrives)
66-
{
67-
if (devData.Letter.Contains(dev.Letter))
68-
{
69-
devData.TotalSize = dev.TotalSpace;
70-
devData.UsedSize = dev.UsedSpace;
71-
devData.PercentUsed = dev.PercentUsedSpace;
72-
73-
break;
74-
}
75-
}
68+
devData.TotalSize = DeviceInfo.LogicalDrives[devData.Index].TotalSpace;
69+
devData.UsedSize = DeviceInfo.LogicalDrives[devData.Index].UsedSpace;
70+
devData.PercentUsed = DeviceInfo.LogicalDrives[devData.Index].PercentUsedSpace;
7671
}
7772
}
7873

@@ -82,66 +77,40 @@ public void UpdateDeviceInfo()
8277

8378
foreach (DeviceIoData devData in _deviceList.Items)
8479
{
85-
foreach (Device dev in DeviceInfo.LogicalDrives)
86-
{
87-
if (devData.Letter.Contains(dev.Letter))
88-
{
89-
devData.Health = dev.Health;
90-
devData.TotalWrite = dev.TotalWrite;
91-
92-
devData.IsHealth = dev.Health != 0;
93-
devData.IsTotalWrite = dev.TotalWrite != 0;
94-
}
95-
}
80+
devData.Health = DeviceInfo.LogicalDrives[devData.Index].Health;
81+
devData.TotalWrite = DeviceInfo.LogicalDrives[devData.Index].TotalWrite;
82+
83+
devData.IsHealth = devData.Health != 0;
84+
devData.IsTotalWrite = devData.TotalWrite != 0;
9685
}
9786
}
9887

9988
public void UpdatePerformance()
10089
{
101-
byte count = 0;
102-
10390
foreach (DeviceIoData dId in _deviceList.Items)
10491
{
105-
DeviceInfo.PerfomanceDiskUpdate(count);
92+
DeviceInfo.PerfomanceDiskUpdate(dId.Index);
10693

107-
dId.ReadIO = DeviceInfo.ReadIO;
108-
dId.WriteIO = DeviceInfo.WriteIO;
109-
dId.DeviceChart.Value = DeviceInfo.ReadIO + DeviceInfo.WriteIO;
110-
111-
count++;
94+
dId.ReadIO = DeviceInfo.ReadIo;
95+
dId.WriteIO = DeviceInfo.WriteIo;
96+
dId.DeviceChart.Value = DeviceInfo.ReadIo + DeviceInfo.WriteIo;
11297
}
11398
}
11499

115100
private bool ExistDeviceInCollection(Device device)
116101
{
117-
foreach (DeviceIoData dID in _deviceList.Items)
118-
{
119-
if (dID.Model.Contains(device.Model) && dID.TotalSize == device.TotalSpace)
120-
return true;
121-
}
122-
123-
return false;
102+
return _deviceList.Items.Any(did => did.Model.Contains(device.Model) && did.TotalSize == device.TotalSpace);
124103
}
125104

126-
private void RemoveOldDeviceInCollection(List<Device> device)
105+
private void RemoveOldDeviceInCollection(IReadOnlyCollection<Device> device)
127106
{
128-
byte index = 0;
107+
short index = 0;
129108

130109
foreach (DeviceIoData item in _deviceList.Items)
131110
{
132-
bool isExist = false;
133-
134-
foreach (Device dev in device)
135-
{
136-
if (dev.Model.Contains(item.Model) && dev.TotalSpace == item.TotalSize)
137-
{
138-
isExist = true;
139-
break;
140-
}
141-
}
142-
143-
if (!isExist) _deviceList.RemoveAt(index);
144-
111+
if (!device.Any(dev => dev.Model.Contains(item.Model) && dev.TotalSpace == item.TotalSize))
112+
_deviceList.RemoveAt(index);
113+
145114
index++;
146115
}
147116
}

SysMonAvalonia/SysMonAvalonia.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.8" />
2828
<PackageReference Include="LiveChartsCore.SkiaSharpView.Avalonia" Version="2.0.0-beta.71" />
2929
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
30-
<PackageReference Include="Projektanker.Icons.Avalonia" Version="3.1.2" />
31-
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="3.1.2" />
30+
<PackageReference Include="Projektanker.Icons.Avalonia" Version="3.2.0" />
31+
<PackageReference Include="Projektanker.Icons.Avalonia.FontAwesome" Version="3.2.0" />
3232
<PackageReference Include="TaskScheduler" Version="2.9.2" />
3333
</ItemGroup>
3434
<ItemGroup>

0 commit comments

Comments
 (0)