Skip to content

Commit b5b46e8

Browse files
author
linyuan.yang
committed
本机信息
1 parent 45f6bf5 commit b5b46e8

3 files changed

Lines changed: 222 additions & 38 deletions

File tree

‎packages/app/src-tauri/src/tools/system_info.rs‎

Lines changed: 153 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub enum SectionData {
4949
Storage {
5050
drives: Vec<DriveInfo>,
5151
volumes: Vec<VolumeInfo>,
52+
/// 主板插槽(PCIe 等)。M.2 / 直连硬盘槽位的占用系统层拿不到,不在此列
53+
slots: Vec<BoardSlotInfo>,
5254
},
5355
Network {
5456
networks: Vec<NetworkInfo>,
@@ -106,6 +108,18 @@ pub struct DeviceInfo {
106108
pub uptime_secs: u64,
107109
}
108110

111+
/// 主板物理插槽(SMBIOS Type 9,Windows: Win32_SystemSlot,macOS: SPPCIDataType),挂在存储段展示。
112+
#[derive(Serialize)]
113+
#[serde(rename_all = "camelCase")]
114+
pub struct BoardSlotInfo {
115+
/// 插槽编号,如 "J6B2" / "PCIEX16_1"
116+
pub designation: String,
117+
/// 占用状态:空闲 / 使用中
118+
pub usage: Option<String>,
119+
/// 设备状态,如 "OK"(仅 Windows)
120+
pub status: Option<String>,
121+
}
122+
109123
#[derive(Serialize)]
110124
#[serde(rename_all = "camelCase")]
111125
pub struct CpuInfo {
@@ -151,10 +165,22 @@ pub struct MemoryInfo {
151165
pub slot_count: Option<u32>,
152166
/// 已安装的内存条
153167
pub modules: Vec<MemoryModule>,
168+
/// 全部插槽(含空闲),用于逐槽展示占用情况
169+
pub slots: Vec<MemorySlot>,
154170
}
155171

172+
/// 单个内存插槽:空闲时 module 为 None,系统未报名称的空槽 slot 为 None。
156173
#[derive(Serialize)]
157174
#[serde(rename_all = "camelCase")]
175+
pub struct MemorySlot {
176+
/// 插槽位置,如 "DDR5-A1"
177+
pub slot: Option<String>,
178+
pub occupied: bool,
179+
pub module: Option<MemoryModule>,
180+
}
181+
182+
#[derive(Clone, Serialize)]
183+
#[serde(rename_all = "camelCase")]
158184
pub struct MemoryModule {
159185
/// 插槽位置,如 "ChannelA-DIMM0"
160186
pub slot: Option<String>,
@@ -458,6 +484,7 @@ fn sysinfo_memory() -> MemoryInfo {
458484
swap_used_bytes: system.used_swap(),
459485
slot_count: None,
460486
modules: Vec::new(),
487+
slots: Vec::new(),
461488
}
462489
}
463490

@@ -552,8 +579,12 @@ fn collect_section(section: Section) -> Result<SectionData, String> {
552579
SectionData::Graphics { gpus, monitors }
553580
}
554581
Section::Storage => {
555-
let (drives, volumes) = platform::storage_section();
556-
SectionData::Storage { drives, volumes }
582+
let (drives, volumes, slots) = platform::storage_section();
583+
SectionData::Storage {
584+
drives,
585+
volumes,
586+
slots,
587+
}
557588
}
558589
Section::Network => SectionData::Network {
559590
networks: platform::network_section(),
@@ -659,6 +690,8 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
659690
[pscustomobject]@{
660691
drives = @(Get-CimInstance Win32_DiskDrive) | Select-Object Model, InterfaceType, MediaType, Size, SerialNumber, Partitions
661692
volumes = $volumes
693+
# SMBIOS Type 9:主板物理插槽(PCIe 等)。M.2 槽位多数主板不报,占用情况系统层拿不到
694+
slots = @(Get-CimInstance Win32_SystemSlot) | Select-Object SlotDesignation, CurrentUsage, Status
662695
} | ConvertTo-Json -Compress -Depth 3";
663696

664697
const NETWORK_SCRIPT: &str = "\
@@ -762,26 +795,41 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
762795
let Some(value) = run_cim(MEMORY_SCRIPT) else {
763796
return memory;
764797
};
765-
if let Some(modules) = as_items(value.get("memoryModules")) {
766-
memory.modules = modules
767-
.iter()
768-
.filter_map(|module| {
769-
// Capacity 为 0 或缺失的插槽没有插内存条
770-
let capacity = u64_field(module, "Capacity").filter(|capacity| *capacity > 0)?;
771-
Some(super::MemoryModule {
772-
slot: str_field(module, "DeviceLocator"),
773-
bank: str_field(module, "BankLabel"),
774-
manufacturer: str_field(module, "Manufacturer"),
775-
part_number: str_field(module, "PartNumber"),
776-
capacity_bytes: Some(capacity),
777-
speed_mhz: u64_field(module, "Speed"),
778-
configured_speed_mhz: u64_field(module, "ConfiguredClockSpeed"),
779-
kind: memory_kind(u64_field(module, "SMBIOSMemoryType").unwrap_or(0)),
798+
// Win32_PhysicalMemory 对应 SMBIOS Type 17,部分主板会为空槽也生成记录(Capacity 为 0)
799+
let mut slots: Vec<super::MemorySlot> = as_items(value.get("memoryModules"))
800+
.map(|items| {
801+
items
802+
.iter()
803+
.filter_map(|module| {
804+
let capacity = u64_field(module, "Capacity").unwrap_or(0);
805+
let occupied = capacity > 0;
806+
Some(super::MemorySlot {
807+
slot: str_field(module, "DeviceLocator").or_else(|| str_field(module, "BankLabel")),
808+
occupied,
809+
module: occupied.then(|| super::MemoryModule {
810+
slot: str_field(module, "DeviceLocator"),
811+
bank: str_field(module, "BankLabel"),
812+
manufacturer: str_field(module, "Manufacturer"),
813+
part_number: str_field(module, "PartNumber"),
814+
capacity_bytes: Some(capacity),
815+
speed_mhz: u64_field(module, "Speed"),
816+
configured_speed_mhz: u64_field(module, "ConfiguredClockSpeed"),
817+
kind: memory_kind(u64_field(module, "SMBIOSMemoryType").unwrap_or(0)),
818+
}),
819+
})
780820
})
781-
})
782-
.collect();
821+
.collect()
822+
})
823+
.unwrap_or_default();
824+
memory.slot_count = u64_field(&value, "memorySlots").map(|count| count as u32);
825+
// 主板只为已插条生成记录时,用 PhysicalMemoryArray 的槽位总数补齐空槽占位
826+
if let Some(total) = memory.slot_count {
827+
while slots.len() < total as usize {
828+
slots.push(super::MemorySlot { slot: None, occupied: false, module: None });
829+
}
783830
}
784-
memory.slot_count = u64_field(&value, "memorySlots").map(|slots| slots as u32);
831+
memory.slots = slots;
832+
memory.modules = memory.slots.iter().filter_map(|slot| slot.module.clone()).collect();
785833
memory
786834
}
787835

@@ -842,10 +890,24 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
842890
(gpus, monitors)
843891
}
844892

845-
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>) {
893+
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>, Vec<super::BoardSlotInfo>) {
846894
let Some(value) = run_cim(STORAGE_SCRIPT) else {
847-
return (Vec::new(), super::sysinfo_volumes());
895+
return (Vec::new(), super::sysinfo_volumes(), Vec::new());
848896
};
897+
let slots = as_items(value.get("slots"))
898+
.map(|items| {
899+
items
900+
.iter()
901+
.filter_map(|item| {
902+
Some(super::BoardSlotInfo {
903+
designation: str_field(item, "SlotDesignation")?,
904+
usage: u64_field(item, "CurrentUsage").map(slot_usage_name).map(|usage| usage.to_string()),
905+
status: str_field(item, "Status"),
906+
})
907+
})
908+
.collect()
909+
})
910+
.unwrap_or_default();
849911
let drives = as_items(value.get("drives"))
850912
.map(|items| {
851913
items
@@ -882,7 +944,7 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
882944
.collect()
883945
})
884946
.unwrap_or_default();
885-
(drives, volumes)
947+
(drives, volumes, slots)
886948
}
887949

888950
pub fn network_section() -> Vec<NetworkInfo> {
@@ -935,6 +997,17 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
935997
.unwrap_or_default()
936998
}
937999

1000+
/// Win32_SystemSlot.CurrentUsage 代码 → 占用状态名。
1001+
fn slot_usage_name(code: u64) -> &'static str {
1002+
match code {
1003+
3 => "空闲",
1004+
4 => "使用中",
1005+
5 => "不可用",
1006+
1 => "其他",
1007+
_ => "未知",
1008+
}
1009+
}
1010+
9381011
/// SMBIOS 内存类型代码 → 代际名。
9391012
fn memory_kind(kind: u64) -> Option<String> {
9401013
let name = match kind {
@@ -1070,6 +1143,32 @@ $volumes = @(Get-CimInstance Win32_LogicalDisk) | ForEach-Object {
10701143
assert!(device.timezone.is_some());
10711144
}
10721145

1146+
#[test]
1147+
fn memory_section_reports_slot_occupancy() {
1148+
let data = collect_section(Section::Memory).expect("内存段采集应成功");
1149+
let SectionData::Memory { memory } = data else {
1150+
panic!("内存段应返回 Memory 分支");
1151+
};
1152+
assert!(!memory.slots.is_empty(), "应能读取到内存插槽");
1153+
assert!(
1154+
memory.slots.iter().any(|slot| slot.occupied),
1155+
"至少应有一个插槽在使用中"
1156+
);
1157+
assert!(
1158+
memory.slot_count.is_none_or(|total| memory.slots.len() <= total as usize),
1159+
"补齐后插槽数不应超过 PhysicalMemoryArray 报告的总数"
1160+
);
1161+
}
1162+
1163+
#[test]
1164+
fn storage_section_reports_board_slots() {
1165+
let data = collect_section(Section::Storage).expect("存储段采集应成功");
1166+
let SectionData::Storage { slots, .. } = data else {
1167+
panic!("存储段应返回 Storage 分支");
1168+
};
1169+
assert!(!slots.is_empty(), "应能读取到主板插槽(Win32_SystemSlot)");
1170+
}
1171+
10731172
#[test]
10741173
fn graphics_section_reports_vram() {
10751174
let data = collect_section(Section::Graphics).expect("显卡段采集应成功");
@@ -1152,6 +1251,12 @@ mod macos_impl {
11521251
kind: str_field(module, "ddr_type").or_else(|| str_field(module, "memory_type")),
11531252
})
11541253
.collect();
1254+
// system_profiler 只列已插条(Apple Silicon 内存为板载),逐条标记占用
1255+
memory.slots = memory
1256+
.modules
1257+
.iter()
1258+
.map(|module| super::MemorySlot { slot: module.slot.clone(), occupied: true, module: Some(module.clone()) })
1259+
.collect();
11551260
}
11561261
memory
11571262
}
@@ -1203,7 +1308,7 @@ mod macos_impl {
12031308
(gpus, monitors)
12041309
}
12051310

1206-
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>) {
1311+
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>, Vec<super::BoardSlotInfo>) {
12071312
let storage = run_profiler("SPStorageDataType SPNVMeDataType");
12081313
let volumes = storage
12091314
.get("SPStorageDataType")
@@ -1246,7 +1351,25 @@ mod macos_impl {
12461351
.collect()
12471352
})
12481353
.unwrap_or_default();
1249-
(drives, volumes)
1354+
// PCIe 插槽(Mac Pro 等可扩展机型);Apple Silicon 全部板载,返回空。
1355+
// system_profiler 只列已插卡的槽,空槽不会出现
1356+
let slots = run_profiler("SPPCIDataType")
1357+
.get("SPPCIDataType")
1358+
.and_then(Value::as_array)
1359+
.map(|items| {
1360+
items
1361+
.iter()
1362+
.filter_map(|slot| {
1363+
Some(super::BoardSlotInfo {
1364+
designation: str_field(slot, "_name")?,
1365+
usage: Some("使用中".into()),
1366+
status: None,
1367+
})
1368+
})
1369+
.collect()
1370+
})
1371+
.unwrap_or_default();
1372+
(drives, volumes, slots)
12501373
}
12511374

12521375
pub fn network_section() -> Vec<NetworkInfo> {
@@ -1426,8 +1549,9 @@ mod linux_impl {
14261549
(list_gpus_from_lspci(), list_monitors_from_xrandr())
14271550
}
14281551

1429-
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>) {
1430-
(list_drives_from_lsblk(), filter_real_volumes(super::sysinfo_volumes()))
1552+
/// 主板插槽需要 root 权限的 dmidecode,普通进程拿不到,返回空。
1553+
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>, Vec<super::BoardSlotInfo>) {
1554+
(list_drives_from_lsblk(), filter_real_volumes(super::sysinfo_volumes()), Vec::new())
14311555
}
14321556

14331557
pub fn network_section() -> Vec<NetworkInfo> {
@@ -1649,8 +1773,8 @@ mod other_impl {
16491773
(Vec::new(), Vec::new())
16501774
}
16511775

1652-
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>) {
1653-
(Vec::new(), super::sysinfo_volumes())
1776+
pub fn storage_section() -> (Vec<DriveInfo>, Vec<VolumeInfo>, Vec<super::BoardSlotInfo>) {
1777+
(Vec::new(), super::sysinfo_volumes(), Vec::new())
16541778
}
16551779

16561780
pub fn network_section() -> Vec<NetworkInfo> {

0 commit comments

Comments
 (0)