-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware_info.cpp
More file actions
198 lines (177 loc) · 5.01 KB
/
Copy pathhardware_info.cpp
File metadata and controls
198 lines (177 loc) · 5.01 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "hardware_info.hpp"
#include "os_registry.hpp"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#else
#include <sys/statvfs.h>
#include <unistd.h>
#include <cstdio>
#endif
namespace droidcli::cli {
namespace {
#ifdef _WIN32
// Same registry location Task Manager/System Information read the CPU name
// from - no WMI/COM dependency needed for a value that's already sitting in
// the registry. Goes through os_registry's shared open/read/close primitive
// (droidcli-infra), not a private RegOpenKeyExA/RegQueryValueExA pair.
core::String detect_cpu_name()
{
const core::String name = read_registry_string(
RegistryRoot::LocalMachine,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"ProcessorNameString");
return name.empty() ? "unknown" : name;
}
int32_t detect_cpu_core_count()
{
SYSTEM_INFO info;
GetSystemInfo(&info);
return static_cast<int32_t>(info.dwNumberOfProcessors);
}
int64_t detect_total_ram_bytes()
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
if (!GlobalMemoryStatusEx(&status))
{
return 0;
}
return static_cast<int64_t>(status.ullTotalPhys);
}
// EnumDisplayDevicesA (user32, already an implicit link on this target - see
// window_list.cpp's EnumWindows) rather than WMI/DXGI: no COM initialization,
// no new link dependency, and it's the same call the old Windows "Display
// adapter properties" dialog is backed by.
core::Array<GpuAdapter> detect_gpus()
{
core::Array<GpuAdapter> gpus;
DISPLAY_DEVICEA device;
device.cb = sizeof(device);
for (DWORD index = 0; EnumDisplayDevicesA(nullptr, index, &device, 0); ++index)
{
// DISPLAY_DEVICE_ATTACHED_TO_DESKTOP filters out disabled/phantom
// adapters Windows still enumerates (e.g. a disconnected dock's GPU).
if ((device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0)
{
gpus.push_back(GpuAdapter{ core::String(device.DeviceString) });
}
}
return gpus;
}
core::Array<DiskVolume> detect_disks()
{
core::Array<DiskVolume> disks;
const DWORD drive_mask = GetLogicalDrives();
for (int letter = 0; letter < 26; ++letter)
{
if ((drive_mask & (1u << letter)) == 0)
{
continue;
}
core::String root = core::String(1, static_cast<char>('A' + letter)) + ":\\";
if (GetDriveTypeA(root.c_str()) != DRIVE_FIXED)
{
// Skips removable/network/CD drives - "what's this machine made
// of" means its own fixed storage, not every mapped network share.
continue;
}
ULARGE_INTEGER free_bytes { };
ULARGE_INTEGER total_bytes { };
if (!GetDiskFreeSpaceExA(root.c_str(), &free_bytes, &total_bytes, nullptr))
{
continue;
}
DiskVolume volume;
volume.drive = root;
volume.total_bytes = static_cast<int64_t>(total_bytes.QuadPart);
volume.free_bytes = static_cast<int64_t>(free_bytes.QuadPart);
disks.push_back(volume);
}
return disks;
}
#else
// Best-effort POSIX equivalents - deliberately minimal, no /proc/cpuinfo
// parsing beyond the model-name line, since this is scoped to "what is this
// machine made of," not a full hwinfo/lshw replacement.
core::String detect_cpu_name()
{
std::FILE* file = std::fopen("/proc/cpuinfo", "r");
if (file == nullptr)
{
return "unknown";
}
char line[512];
core::String name = "unknown";
while (std::fgets(line, sizeof(line), file) != nullptr)
{
core::String text(line);
if (text.rfind("model name", 0) == 0)
{
const size_t colon = text.find(':');
if (colon != core::String::npos)
{
name = text.substr(colon + 2);
if (!name.empty() && name.back() == '\n')
{
name.pop_back();
}
}
break;
}
}
std::fclose(file);
return name;
}
int32_t detect_cpu_core_count()
{
const long count = sysconf(_SC_NPROCESSORS_ONLN);
return count > 0 ? static_cast<int32_t>(count) : 0;
}
int64_t detect_total_ram_bytes()
{
const long pages = sysconf(_SC_PHYS_PAGES);
const long page_size = sysconf(_SC_PAGE_SIZE);
if (pages <= 0 || page_size <= 0)
{
return 0;
}
return static_cast<int64_t>(pages) * static_cast<int64_t>(page_size);
}
// GPU enumeration has no single portable POSIX API (varies by distro/driver
// stack) - deliberately left empty rather than guessing via a fragile lspci
// shell-out, which would also violate the "no shelling out for a core query"
// spirit every other detect_* function here follows.
core::Array<GpuAdapter> detect_gpus()
{
return {};
}
core::Array<DiskVolume> detect_disks()
{
core::Array<DiskVolume> disks;
struct statvfs stats { };
if (statvfs("/", &stats) == 0)
{
DiskVolume volume;
volume.drive = "/";
volume.total_bytes = static_cast<int64_t>(stats.f_blocks) * static_cast<int64_t>(stats.f_frsize);
volume.free_bytes = static_cast<int64_t>(stats.f_bavail) * static_cast<int64_t>(stats.f_frsize);
disks.push_back(volume);
}
return disks;
}
#endif
} // namespace
HardwareInfo scan_hardware_info()
{
HardwareInfo info;
info.cpu_name = detect_cpu_name();
info.cpu_core_count = detect_cpu_core_count();
info.total_ram_bytes = detect_total_ram_bytes();
info.gpus = detect_gpus();
info.disks = detect_disks();
return info;
}
} // namespace droidcli::cli