/
CpuInfo.cpp
315 lines (254 loc) Β· 6.84 KB
/
CpuInfo.cpp
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
///
/// @file CpuInfo.cpp
/// @brief Get the CPUs cache sizes in bytes.
///
/// Copyright (C) 2017 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///
#include <primesieve/CpuInfo.hpp>
#include <cstddef>
#if defined(__APPLE__)
#if !defined(__has_include)
#define APPLE_SYSCTL
#elif __has_include(<sys/types.h>) && \
__has_include(<sys/sysctl.h>)
#define APPLE_SYSCTL
#endif
#endif
#if defined(_WIN32)
#include <windows.h>
#include <vector>
#include <iostream>
#elif defined(APPLE_SYSCTL)
#include <sys/types.h>
#include <sys/sysctl.h>
#include <vector>
#else // all other OSes
#include <exception>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
namespace {
string getString(const string& filename)
{
ifstream file(filename);
string str;
if (file)
{
ostringstream oss;
oss << file.rdbuf();
str = oss.str();
}
return str;
}
size_t getValue(const string& filename)
{
try
{
// first character after number
size_t idx = 0;
string str = getString(filename);
size_t val = stol(str, &idx);
if (idx < str.size())
{
// Last character may be:
// 'K' = kilobytes
// 'M' = megabytes
// 'G' = gigabytes
if (str[idx] == 'K')
val *= 1024;
if (str[idx] == 'M')
val *= 1024 * 1024;
if (str[idx] == 'G')
val *= 1024 * 1024 * 1024;
}
return val;
}
catch (exception&)
{
return 0;
}
}
} // namespace
#endif
using namespace std;
namespace primesieve {
CpuInfo::CpuInfo()
: l1CacheSize_(0),
l2CacheSize_(0),
privateL2Cache_(false)
{
initCache();
}
size_t CpuInfo::l1CacheSize() const
{
return l1CacheSize_;
}
size_t CpuInfo::l2CacheSize() const
{
return l2CacheSize_;
}
bool CpuInfo::privateL2Cache() const
{
return privateL2Cache_;
}
bool CpuInfo::hasL1Cache() const
{
return l1CacheSize_ >= (1 << 12) &&
l1CacheSize_ <= (1ull << 40);
}
bool CpuInfo::hasL2Cache() const
{
return l2CacheSize_ >= (1 << 12) &&
l2CacheSize_ <= (1ull << 40);
}
#if defined(APPLE_SYSCTL)
void CpuInfo::initCache()
{
size_t l1Length = sizeof(l1CacheSize_);
size_t l2Length = sizeof(l2CacheSize_);
sysctlbyname("hw.l1dcachesize", &l1CacheSize_, &l1Length, NULL, 0);
sysctlbyname("hw.l2cachesize" , &l2CacheSize_, &l2Length, NULL, 0);
size_t size = 0;
if (!sysctlbyname("hw.cacheconfig", NULL, &size, NULL, 0))
{
size_t n = size / sizeof(size);
vector<size_t> values(n);
if (values.size() > 2)
{
// https://developer.apple.com/library/content/releasenotes/Performance/RN-AffinityAPI/index.html
sysctlbyname("hw.cacheconfig" , &values[0], &size, NULL, 0);
size_t l1CacheConfig = values[1];
size_t l2CacheConfig = values[2];
if (l2CacheConfig > 0 &&
l1CacheConfig == l2CacheConfig)
{
privateL2Cache_ = true;
}
}
}
}
#elif defined(_WIN32)
void CpuInfo::initCache()
{
#if _WIN32_WINNT >= 0x0601
try
{
std::cout << "Kim Yes!" << std::endl;
typedef BOOL (WINAPI *LPFN_GLPI)(LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
LPFN_GLPI glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformationEx");
if (!glpi)
return;
DWORD bytes = 0;
glpi(RelationAll, 0, &bytes);
vector<char> buffer(bytes);
if (!glpi(RelationAll, (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) &buffer[0], &bytes))
return;
for (size_t i = 0; i < bytes;)
{
auto info = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) &buffer[i];
i += info->Size;
if (info->Relationship == RelationCache)
{
std::cout << "info->Cache.Level: " << (int) info->Cache.Level << std::endl;
std::cout << "info->Cache.CacheSize: " << (int) info->Cache.CacheSize << std::endl;
std::cout << "Cache.Group: " << info->Cache.GroupMask.Group << std::endl;
std::cout << "Cache.Mask: " << info->Cache.GroupMask.Mask << std::endl << std::endl;
if (info->Cache.Level == 1)
{
l1CacheSize_ = info->Cache.CacheSize;
}
if (info->Cache.Level == 2)
{
// Using the Windows API it is not possible to find out
// whether the L2 cache is private or shared hence
// we assume the L2 cache is private
privateL2Cache_ = true;
l2CacheSize_ = info->Cache.CacheSize;
}
}
}
}
catch (exception& e)
{
std::cerr << "Exception in initCache():" << e.what() << std::endl;
}
#else
typedef BOOL (WINAPI *LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
LPFN_GLPI glpi = (LPFN_GLPI) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
if (glpi)
{
DWORD bytes = 0;
glpi(0, &bytes);
size_t size = bytes / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> info(size);
glpi(info.data(), &bytes);
for (size_t i = 0; i < size; i++)
{
if (info[i].Relationship == RelationCache)
{
if (info[i].Cache.Level == 1)
{
l1CacheSize_ = info[i].Cache.Size;
std::cout << "Cache.GroupMask1: " << info[i].GroupMask << std::endl;
}
if (info[i].Cache.Level == 2)
{
// Using the Windows API it is not possible to find out
// whether the L2 cache is private or shared hence
// we assume the L2 cache is private
privateL2Cache_ = true;
l2CacheSize_ = info[i].Cache.Size;
std::cout << "Cache.GroupMask2: " << info[i].GroupMask << std::endl;
}
}
}
}
#endif
}
#else
/// This works on Linux and Android. We also use this
/// for all unknown OSes, it might work.
///
void CpuInfo::initCache()
{
string l1CacheMap;
string l2CacheMap;
for (int i = 0; i <= 3; i++)
{
string filename = "/sys/devices/system/cpu/cpu0/cache/index" + to_string(i);
string cacheLevel = filename + "/level";
string cacheSize = filename + "/size";
string cacheMap = filename + "/shared_cpu_map";
string cacheType = filename + "/type";
size_t level = getValue(cacheLevel);
string type = getString(cacheType);
if (level == 1 &&
(type.find("Data") != string::npos ||
type.find("Unified") != string::npos))
{
l1CacheSize_ = getValue(cacheSize);
l1CacheMap = getString(cacheMap);
}
if (level == 2 &&
(type.find("Data") != string::npos ||
type.find("Unified") != string::npos))
{
l2CacheSize_ = getValue(cacheSize);
l2CacheMap = getString(cacheMap);
}
}
if (hasL2Cache() &&
!l2CacheMap.empty() &&
l2CacheMap == l1CacheMap)
{
privateL2Cache_ = true;
}
}
#endif
/// Singleton
const CpuInfo cpuInfo;
} // namespace