CppBenchmark 1.0.5.0
C++ Benchmark Library
Loading...
Searching...
No Matches
environment.cpp
Go to the documentation of this file.
1
10
11#if defined(__GNUC__)
12#pragma GCC diagnostic push
13#pragma GCC diagnostic ignored "-Wdeprecated-declarations" // GCC: warning: 'wstring_convert' is deprecated
14#endif
15
16#include <chrono>
17#include <codecvt>
18#include <cstring>
19#include <locale>
20#include <sstream>
21
22#if defined(__APPLE__)
23#include <sys/sysctl.h>
24#elif defined(unix) || defined(__unix) || defined(__unix__)
25#include <sys/stat.h>
26#include <sys/utsname.h>
27#include <fstream>
28#include <regex>
29#endif
30#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
31#include <windows.h>
32#include <winternl.h>
33#define STATUS_SUCCESS 0x00000000
34#endif
35
36namespace CppBenchmark {
37
39{
40 return !Is64BitOS();
41}
42
44{
45#if defined(__APPLE__)
46 return true;
47#elif defined(linux) || defined(__linux) || defined(__linux__)
48 struct stat buffer;
49 return (stat("/lib64/ld-linux-x86-64.so.2", &buffer) == 0);
50#elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
51#if defined(_WIN64)
52 return true;
53#elif defined(_WIN32) || defined(__CYGWIN__)
54 BOOL bWow64Process = FALSE;
55 return IsWow64Process(GetCurrentProcess(), &bWow64Process) && bWow64Process;
56#endif
57#else
58 #error Unsupported platform
59#endif
60}
61
63{
64 return !Is64BitProcess();
65}
66
68{
69#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
70#if defined(__x86_64__) || defined(__amd64__) || defined(__aarch64__) || defined(__ia64__) || defined(__ppc64__)
71 return true;
72#else
73 return false;
74#endif
75#elif defined(_WIN32) || defined(_WIN64)
76#if defined(_WIN64)
77 return true;
78#elif defined(_WIN32)
79 return false;
80#endif
81#else
82 #error Unsupported platform
83#endif
84}
85
87{
88 return !IsRelease();
89}
90
92{
93#if defined(NDEBUG)
94 return true;
95#else
96 return false;
97#endif
98}
99
101{
102 char16_t test = 0x0102;
103 return ((char*)&test)[0] == 0x01;
104}
105
107{
108 char16_t test = 0x0102;
109 return ((char*)&test)[0] == 0x02;
110}
111
113{
114#if defined(__APPLE__)
115 char result[1024];
116 size_t size = sizeof(result);
117 if (sysctlbyname("kern.osrelease", result, &size, nullptr, 0) == 0)
118 return result;
119
120 return "<apple>";
121#elif defined(__CYGWIN__)
122 struct utsname name;
123 if (uname(&name) == 0)
124 {
125 std::string result(name.sysname);
126 result.append(" ");
127 result.append(name.release);
128 result.append(" ");
129 result.append(name.version);
130 return result;
131 }
132
133 return "<cygwin>";
134#elif defined(linux) || defined(__linux) || defined(__linux__)
135 static std::regex pattern("DISTRIB_DESCRIPTION=\"(.*)\"");
136
137 std::string line;
138 std::ifstream stream("/etc/lsb-release");
139 while (getline(stream, line))
140 {
141 std::smatch matches;
142 if (std::regex_match(line, matches, pattern))
143 return matches[1];
144 }
145
146 return "<linux>";
147#elif defined(_WIN32) || defined(_WIN64)
148 static NTSTATUS(__stdcall *RtlGetVersion)(OUT PRTL_OSVERSIONINFOEXW lpVersionInformation) = (NTSTATUS(__stdcall*)(PRTL_OSVERSIONINFOEXW))GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlGetVersion");
149 static void(__stdcall *GetNativeSystemInfo)(OUT LPSYSTEM_INFO lpSystemInfo) = (void(__stdcall*)(LPSYSTEM_INFO))GetProcAddress(GetModuleHandle("kernel32.dll"), "GetNativeSystemInfo");
150 static BOOL(__stdcall *GetProductInfo)(IN DWORD dwOSMajorVersion, IN DWORD dwOSMinorVersion, IN DWORD dwSpMajorVersion, IN DWORD dwSpMinorVersion, OUT PDWORD pdwReturnedProductType) = (BOOL(__stdcall*)(DWORD, DWORD, DWORD, DWORD, PDWORD))GetProcAddress(GetModuleHandle("kernel32.dll"), "GetProductInfo");
151
152 OSVERSIONINFOEXW osvi;
153 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEXW));
154 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
155
156 if (RtlGetVersion != nullptr)
157 {
158 NTSTATUS ntRtlGetVersionStatus = RtlGetVersion(&osvi);
159 if (ntRtlGetVersionStatus != STATUS_SUCCESS)
160 return "<windows>";
161 }
162 else
163 {
164#if defined(_MSC_VER)
165#pragma warning(push)
166#pragma warning(disable: 4996) // C4996: 'function': was declared deprecated
167#endif
168 BOOL bOsVersionInfoEx = GetVersionExW((OSVERSIONINFOW*)&osvi);
169 if (bOsVersionInfoEx == 0)
170 return "<windows>";
171#if defined(_MSC_VER)
172#pragma warning(pop)
173#endif
174 }
175
176 SYSTEM_INFO si;
177 ZeroMemory(&si, sizeof(SYSTEM_INFO));
178
179 if (GetNativeSystemInfo != nullptr)
180 GetNativeSystemInfo(&si);
181 else
182 GetSystemInfo(&si);
183
184 if ((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) || (osvi.dwMajorVersion <= 4))
185 {
186 return "<windows>";
187 }
188
189 std::stringstream os;
190
191 // Windows version
192 os << "Microsoft ";
193 if (osvi.dwMajorVersion >= 6)
194 {
195 if (osvi.dwMajorVersion == 10)
196 {
197 if (osvi.dwMinorVersion == 0)
198 {
199 if (osvi.wProductType == VER_NT_WORKSTATION)
200 {
201 if (osvi.dwBuildNumber >= 22000)
202 os << "Windows 11 ";
203 else
204 os << "Windows 10 ";
205 }
206 else
207 {
208 if (osvi.dwBuildNumber >= 20348)
209 os << "Windows Server 2022 ";
210 else if (osvi.dwBuildNumber >= 17763)
211 os << "Windows Server 2019 ";
212 else
213 os << "Windows Server 2016 ";
214 }
215 }
216 }
217 else if (osvi.dwMajorVersion == 6)
218 {
219 if (osvi.dwMinorVersion == 3)
220 {
221 if (osvi.wProductType == VER_NT_WORKSTATION)
222 os << "Windows 8.1 ";
223 else
224 os << "Windows Server 2012 R2 ";
225 }
226 else if (osvi.dwMinorVersion == 2)
227 {
228 if (osvi.wProductType == VER_NT_WORKSTATION)
229 os << "Windows 8 ";
230 else
231 os << "Windows Server 2012 ";
232 }
233 else if (osvi.dwMinorVersion == 1)
234 {
235 if (osvi.wProductType == VER_NT_WORKSTATION)
236 os << "Windows 7 ";
237 else
238 os << "Windows Server 2008 R2 ";
239 }
240 else if (osvi.dwMinorVersion == 0)
241 {
242 if (osvi.wProductType == VER_NT_WORKSTATION)
243 os << "Windows Vista ";
244 else
245 os << "Windows Server 2008 ";
246 }
247 }
248
249 DWORD dwType;
250 if ((GetProductInfo != nullptr) && GetProductInfo(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType))
251 {
252 switch (dwType)
253 {
254 case PRODUCT_ULTIMATE:
255 os << "Ultimate Edition";
256 break;
257 case PRODUCT_PROFESSIONAL:
258 os << "Professional";
259 break;
260 case PRODUCT_HOME_PREMIUM:
261 os << "Home Premium Edition";
262 break;
263 case PRODUCT_HOME_BASIC:
264 os << "Home Basic Edition";
265 break;
266 case PRODUCT_ENTERPRISE:
267 os << "Enterprise Edition";
268 break;
269 case PRODUCT_BUSINESS:
270 os << "Business Edition";
271 break;
272 case PRODUCT_STARTER:
273 os << "Starter Edition";
274 break;
275 case PRODUCT_CLUSTER_SERVER:
276 os << "Cluster Server Edition";
277 break;
278 case PRODUCT_DATACENTER_SERVER:
279 os << "Datacenter Edition";
280 break;
281 case PRODUCT_DATACENTER_SERVER_CORE:
282 os << "Datacenter Edition (core installation)";
283 break;
284 case PRODUCT_ENTERPRISE_SERVER:
285 os << "Enterprise Edition";
286 break;
287 case PRODUCT_ENTERPRISE_SERVER_CORE:
288 os << "Enterprise Edition (core installation)";
289 break;
290 case PRODUCT_ENTERPRISE_SERVER_IA64:
291 os << "Enterprise Edition for Itanium-based Systems";
292 break;
293 case PRODUCT_SMALLBUSINESS_SERVER:
294 os << "Small Business Server";
295 break;
296 case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
297 os << "Small Business Server Premium Edition";
298 break;
299 case PRODUCT_STANDARD_SERVER:
300 os << "Standard Edition";
301 break;
302 case PRODUCT_STANDARD_SERVER_CORE:
303 os << "Standard Edition (core installation)";
304 break;
305 case PRODUCT_WEB_SERVER:
306 os << "Web Server Edition";
307 break;
308 }
309 }
310 }
311 else if ((osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion == 2))
312 {
313 if (GetSystemMetrics(SM_SERVERR2))
314 os << "Windows Server 2003 R2, ";
315 else if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER)
316 os << "Windows Storage Server 2003";
317 else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
318 os << "Windows Home Server";
319 else if ((osvi.wProductType == VER_NT_WORKSTATION) && (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
320 os << "Windows XP Professional x64 Edition";
321 else
322 os << "Windows Server 2003, ";
323 if (osvi.wProductType != VER_NT_WORKSTATION)
324 {
325 if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
326 {
327 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
328 os << "Datacenter Edition for Itanium-based Systems";
329 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
330 os << "Enterprise Edition for Itanium-based Systems";
331 }
332 else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
333 {
334 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
335 os << "Datacenter x64 Edition";
336 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
337 os << "Enterprise x64 Edition";
338 else
339 os << "Standard x64 Edition";
340 }
341 else
342 {
343 if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
344 os << "Compute Cluster Edition";
345 else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
346 os << "Datacenter Edition";
347 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
348 os << "Enterprise Edition";
349 else if (osvi.wSuiteMask & VER_SUITE_BLADE)
350 os << "Web Edition";
351 else
352 os << "Standard Edition";
353 }
354 }
355 }
356 else if ((osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion == 1))
357 {
358 os << "Windows XP ";
359 if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
360 os << "Home Edition";
361 else
362 os << "Professional";
363 }
364 else if ((osvi.dwMajorVersion == 5) && (osvi.dwMinorVersion == 0))
365 {
366 os << "Windows 2000 ";
367 if (osvi.wProductType == VER_NT_WORKSTATION)
368 os << "Professional";
369 else
370 {
371 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
372 os << "Datacenter Server";
373 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
374 os << "Advanced Server";
375 else
376 os << "Server";
377 }
378 }
379
380 // Windows Service Pack version
381 std::wstring sp_version(osvi.szCSDVersion);
382 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
383 if (std::wcslen(osvi.szCSDVersion) > 0)
384 os << " " << convert.to_bytes(sp_version.data(), sp_version.data() + sp_version.size());
385
386 // Windows build
387 os << " (build " << osvi.dwBuildNumber << ")";
388
389 // Windows architecture
390 if (osvi.dwMajorVersion >= 6)
391 {
392 if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
393 os << ", 32-bit";
394 else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
395 os << ", 64-bit";
396 else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
397 os << ", Intel Itanium";
398 else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM)
399 os << ", ARM";
400#if !defined(__MINGW32__) && !defined(__MINGW64__)
401 else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM64)
402 os << ", ARM64";
403#endif
404 }
405
406 return os.str();
407#else
408 #error Unsupported platform
409#endif
410}
411
413{
414#if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
415 return "\n";
416#elif defined(_WIN32) || defined(_WIN64)
417 return "\r\n";
418#else
419 #error Unsupported platform
420#endif
421}
422
424{
425 return "\n";
426}
427
429{
430 return "\r\n";
431}
432
434{
435 return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
436}
437
438} // namespace CppBenchmark
439
440#if defined(__GNUC__)
441#pragma GCC diagnostic pop
442#endif
static bool IsDebug()
Is compiled in debug mode?
static std::string OSVersion()
Get OS version string.
static bool IsRelease()
Is compiled in release mode?
static time_t Timestamp()
Get the current time in seconds.
static bool Is64BitProcess()
Is 64-bit running process?
static bool IsLittleEndian()
Is little-endian system?
static std::string WindowsEndLine()
Get Windows text end line separator.
static std::string EndLine()
Get text end line separator.
static std::string UnixEndLine()
Get Unix text end line separator.
static bool Is64BitOS()
Is 64-bit OS?
static bool Is32BitOS()
Is 32-bit OS?
static bool Is32BitProcess()
Is 32-bit running process?
static bool IsBigEndian()
Is big-endian system?
Environment management definition.
C++ Benchmark project definitions.
Definition barrier.h:15