This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathramlist.cpp
More file actions
329 lines (310 loc) · 8.98 KB
/
Copy pathramlist.cpp
File metadata and controls
329 lines (310 loc) · 8.98 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* @file ramlist.cpp
* @brief The implementation for the RAMList class.
*
* @author Evan Elias Young
* @date 2019-04-04
* @date 2020-02-29
* @copyright Copyright 2019-2020 Evan Elias Young. All rights reserved.
*/
#include "pch.h"
#include "ramlist.h"
#include "ram.h"
#include "os.h"
#include "utils.h"
#pragma region "Contructors"
/**
* @brief Construct a new RAMList object
*/
RAMList::RAMList()
{
chips = std::vector<RAM>();
total = std::uint64_t(0);
}
/**
* @brief Construct a new RAMList object with help from the assistants
*
* @param plt The platform of the system
*/
RAMList::RAMList(std::uint8_t plt)
{
switch (plt)
{
case OS_WIN:
GetWin();
break;
case OS_MAC:
GetMac();
break;
case OS_LUX:
GetLux();
break;
}
}
/**
* @brief Construct a new RAMList object from another RAMList object
*
* @param o The RAMList object to copy from
*/
RAMList::RAMList(const RAMList &o)
{
chips = o.chips;
total = o.total;
}
#pragma endregion "Contructors"
#pragma region "Constructors' Assistants"
/**
* @brief Fills in the RAMList information for Darwin systems
*/
void RAMList::GetMac()
{
std::map<std::string, std::string> manufMap = {
{"0x014F", "Transcend Information"},
{"0x2C00", "Micron Technology Inc."},
{"0x802C", "Micron Technology Inc."},
{"0x80AD", "Hynix Semiconductor Inc."},
{"0x80CE", "Samsung Electronics Inc."},
{"0xAD00", "Hynix Semiconductor Inc."},
{"0xCE00", "Samsung Electronics Inc."},
{"0x02FE", "Elpida"},
{"0x5105", "Qimonda AG i. In."},
{"0x8551", "Qimonda AG i. In."},
{"0x859B", "Crucial"}};
std::vector<std::string> allChips;
RAM tempChip;
std::uint64_t tempSize = 0;
std::string tempBank = "";
std::string tempType = "";
std::uint64_t tempSpeed = 0;
std::string tempFormFactor = "";
std::string tempManufacturer = "";
std::string tempPart = "";
std::string tempSerial = "";
float tempVoltageConfigured = 0;
float tempVoltageMin = 0;
float tempVoltageMax = 0;
std::string key;
std::string val;
splitStringVector(runCommand("system_profiler SPMemoryDataType"), "\n", &allChips);
for (std::size_t i = 0; i < allChips.size(); ++i)
{
if (trim(allChips[i]).empty())
{
continue;
}
if (splitKeyValuePair(trim(allChips[i]), &key, &val))
{
if (key == "Size")
{
tempSize = std::stoull(val.substr(0, val.size() - 3)) * pow(10, 9);
}
else if (startswith(key, "BANK "))
{
tempBank = key.substr(key.find_first_of("/") + 1);
}
else if (key == "Type")
{
tempType = val;
}
else if (key == "Speed")
{
tempSpeed = std::stof(val.substr(0, val.size() - 4)) * pow(10, 6);
}
else if (key == "Manufacturer")
{
tryGetValue(manufMap, val, &tempManufacturer);
}
else if (key == "Part Number")
{
tempPart = val;
}
else if (key == "Serial Number")
{
tempSerial = val;
}
if (i == allChips.size() - 3 || (i > 7 && i < allChips.size() - 3 && startswith(trim(allChips[i + 2]), "BANK ")))
{
tempChip = (new RAM(tempSize, tempBank, tempType, tempSpeed, tempFormFactor, tempManufacturer, tempPart, tempSerial, tempVoltageConfigured, tempVoltageMin, tempVoltageMax));
chips.push_back(tempChip);
}
}
}
}
/**
* @brief Fills in the RAMList information for Windows systems
*/
void RAMList::GetWin()
{
std::string wmic = getWmicPath();
std::vector<std::map<std::string, std::string>> allChips = runListMultiWmic("memorychip get", &wmic);
std::string memoryTypes[31] = {
"Unknown", "Other", "DRAM", "Synchronous DRAM",
"Cache DRAM", "EDO", "EDRAM", "VRAM",
"SRAM", "RAM", "ROM", "FLASH",
"EEPROM", "FEPROM", "EPROM", "CDRAM",
"3DRAM", "SDRAM", "SGRAM", "RDRAM",
"DDR", "DDR2", "DDR2 FB-DIMM", "Reserved",
"DDR3", "FBD2", "DDR4", "LPDDR",
"LPDDR2", "LPDDR3", "LPDDR4"};
std::string memoryForms[24] = {
"Unknown", "Other", "SIP", "DIP",
"ZIP", "SOJ", "Proprietary", "SIMM",
"DIMM", "TSOP", "PGA", "RIMM",
"SODIMM", "SRIMM", "SMD", "SSMP",
"QFP", "TQFP", "SOIC", "LCC",
"PLCC", "BGA", "FPBGA", "LGA"};
RAM tempChip;
std::uint64_t tempSize = 0;
std::string tempBank = "";
std::string tempType = "";
std::uint64_t tempSpeed = 0;
std::string tempFormFactor = "";
std::string tempManufacturer = "";
std::string tempPart = "";
std::string tempSerial = "";
float tempVoltageConfigured = 0;
float tempVoltageMin = 0;
float tempVoltageMax = 0;
for (std::size_t i = 0; i < allChips.size(); ++i)
{
tempSize = !allChips[i]["Capacity"].empty() ? std::stoull(allChips[i]["Capacity"]) / pow(1024, 3) * pow(1000, 3) : 0;
total += tempSize;
tempBank = !allChips[i]["BankLabel"].empty() ? allChips[i]["BankLabel"] : allChips[i]["DeviceLocator"];
tempType = memoryTypes[std::stoi(allChips[i]["MemoryType"])];
tempSpeed = !allChips[i]["ConfiguredClockSpeed"].empty() ? std::stoull(allChips[i]["ConfiguredClockSpeed"]) : !allChips[i]["Speed"].empty() ? std::stoull(allChips[i]["Speed"]) : 0;
tempSpeed *= pow(10, 6);
tempFormFactor = memoryForms[!allChips[i]["FormFactor"].empty() ? std::stoi(allChips[i]["FormFactor"]) : 0];
tempManufacturer = allChips[i]["Manufacturer"];
tempPart = allChips[i]["PartNumber"];
tempSerial = allChips[i]["SerialNumber"];
tempVoltageConfigured = !allChips[i]["ConfiguredVoltage"].empty() ? std::stof(allChips[i]["ConfiguredVoltage"]) / 1000 : 0;
tempVoltageMin = !allChips[i]["MinVoltage"].empty() ? std::stof(allChips[i]["MinVoltage"]) / 1000 : 0;
tempVoltageMax = !allChips[i]["MaxVoltage"].empty() ? std::stof(allChips[i]["MaxVoltage"]) / 1000 : 0;
tempChip = (new RAM(tempSize, tempBank, tempType, tempSpeed, tempFormFactor, tempManufacturer, tempPart, tempSerial, tempVoltageConfigured, tempVoltageMin, tempVoltageMax));
chips.push_back(tempChip);
}
}
/**
* @brief Fills in the RAMList information for Linux systems
*/
void RAMList::GetLux()
{
std::vector<std::string> lines;
std::string line;
std::string key;
std::string val;
RAM tempChip;
std::uint64_t tempSize = 0;
std::string tempBank = "";
std::string tempType = "";
std::uint64_t tempSpeed = 0;
std::string tempFormFactor = "";
std::string tempManufacturer = "";
std::string tempPart = "";
std::string tempSerial = "";
float tempVoltageConfigured = 0;
float tempVoltageMin = 0;
float tempVoltageMax = 0;
splitStringVector(runCommand("export LC_ALL=C; sudo dmidecode -t memory 2>/dev/null | grep -iE \"Size:|Type|Speed|Manufacturer|Form Factor|Locator|Memory Device|Serial Number|Voltage|Part Number\"; unset LC_ALL"), "\n", &lines);
if (lines.size() <= 1)
{
return;
}
for (std::size_t i = 3; i < lines.size(); ++i)
{
// While in range and NOT starting a new RAM chip
while (i < lines.size() && !startswith(lines[i], "Handle 0x"))
{
line = trim(lines[++i]);
// Residual header for new RAM chip
if (startswith(line, "Memory Device"))
{
continue;
}
// No module in the slot, skip it
if (endswith(line, "No Module Installed"))
{
break;
}
splitKeyValuePair(line, &key, &val);
if (key == "Size")
{
tempSize = std::stoull(val.substr(0, val.size() - 3));
tempSize *= pow(1024, endswith(val, "MB") ? 2 : 3);
total += tempSize;
}
if (key == "Locator")
{
tempBank = val;
}
if (key == "Type")
{
tempType = val;
}
if (key == "Speed" && val != "Unknown")
{
tempSpeed = std::stoull(val.substr(0, val.size() - 4)) * pow(1024, 2);
}
if (key == "Form Factor")
{
tempFormFactor = val;
}
if (key == "Manufacturer")
{
tempManufacturer = val;
}
if (key == "Part Number")
{
tempPart = val;
}
if (key == "Serial Number")
{
tempSerial = val;
}
}
// New RAM chip beginning
if (startswith(lines[i], "Handle 0x") && tempManufacturer != "FFFFFFFFFFFF")
{
tempChip = (new RAM(tempSize, tempBank, tempType, tempSpeed, tempFormFactor, tempManufacturer, tempPart, tempSerial, tempVoltageConfigured, tempVoltageMin, tempVoltageMax));
chips.push_back(tempChip);
}
}
}
#pragma endregion
#pragma region "Operators"
/**
* @brief Reserves memory for a new RAM object
*
* @param size The amount of memory to allocate
* @return void* A pointer to the allocated memory
*/
void *RAMList::operator new(std::size_t size)
{
void *o = ::new (RAMList);
return o;
}
/**
* @brief Sets equal two RAM objects
*
* @param o The RAM object to copy from
*/
void RAMList::operator=(const RAMList &o)
{
if (&o == this)
{
return;
}
chips = o.chips;
total = o.total;
}
/**
* @brief Sets equal two RAM objects
*
* @param o The RAM object to copy from
*/
void RAMList::operator=(RAMList *o)
{
chips = o->chips;
total = o->total;
}
#pragma endregion "Operators"