-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemInformation.cxx
5625 lines (4954 loc) · 153 KB
/
SystemInformation.cxx
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#if defined(_WIN32)
# define NOMINMAX // use our min,max
# if !defined(_WIN32_WINNT) && defined(_MSC_VER) && _MSC_VER >= 1800
# define _WIN32_WINNT 0x0600 // vista
# endif
# if !defined(_WIN32_WINNT) && !(defined(_MSC_VER) && _MSC_VER < 1300)
# define _WIN32_WINNT 0x0501
# endif
# include <winsock.h> // WSADATA, include before sys/types.h
#endif
#if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif
// TODO:
// We need an alternative implementation for many functions in this file
// when USE_ASM_INSTRUCTIONS gets defined as 0.
//
// Consider using these on Win32/Win64 for some of them:
//
// IsProcessorFeaturePresent
// https://msdn.microsoft.com/en-us/library/ms724482(VS.85).aspx
//
// GetProcessMemoryInfo
// https://msdn.microsoft.com/en-us/library/ms683219(VS.85).aspx
#include "kwsysPrivate.h"
#include KWSYS_HEADER(SystemInformation.hxx)
#include KWSYS_HEADER(Process.h)
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
#if 0
# include "Process.h.in"
# include "SystemInformation.hxx.in"
#endif
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <limits>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#if defined(_WIN32)
# include <windows.h>
# if defined(_MSC_VER) && _MSC_VER >= 1800
# define KWSYS_WINDOWS_DEPRECATED_GetVersionEx
# endif
# include <errno.h>
# if defined(KWSYS_SYS_HAS_PSAPI)
# include <psapi.h>
# endif
# if !defined(siginfo_t)
typedef int siginfo_t;
# endif
#else
# include <sys/types.h>
# include <cerrno> // extern int errno;
# include <csignal>
# include <fcntl.h>
# include <sys/resource.h> // getrlimit
# include <sys/time.h>
# include <sys/utsname.h> // int uname(struct utsname *buf);
# include <unistd.h>
#endif
#if defined(__CYGWIN__) && !defined(_WIN32)
# include <windows.h>
# undef _WIN32
#endif
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__DragonFly__)
# include <netdb.h>
# include <netinet/in.h>
# include <sys/param.h>
# include <sys/socket.h>
# include <sys/sysctl.h>
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
# include <ifaddrs.h>
# include <net/if.h>
# define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
# endif
#endif
#if defined(KWSYS_SYS_HAS_MACHINE_CPU_H)
# include <machine/cpu.h>
#endif
#ifdef __APPLE__
# include <mach/host_info.h>
# include <mach/mach.h>
# include <mach/mach_types.h>
# include <mach/vm_statistics.h>
# include <netdb.h>
# include <netinet/in.h>
# include <sys/socket.h>
# include <sys/sysctl.h>
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
# include <ifaddrs.h>
# include <net/if.h>
# define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
# endif
# if !(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ - 0 >= 1050)
# undef KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE
# endif
#endif
#if defined(__linux) || defined(__sun) || defined(_SCO_DS) || \
defined(__GLIBC__) || defined(__GNU__)
# include <netdb.h>
# include <netinet/in.h>
# include <sys/socket.h>
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
# include <ifaddrs.h>
# include <net/if.h>
# if defined(__LSB_VERSION__)
/* LSB has no getifaddrs */
# elif defined(__ANDROID_API__) && __ANDROID_API__ < 24
/* Android has no getifaddrs prior to API 24. */
# else
# define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
# endif
# endif
# if defined(KWSYS_CXX_HAS_RLIMIT64)
using ResourceLimitType = struct rlimit64;
# define GetResourceLimit getrlimit64
# else
typedef struct rlimit ResourceLimitType;
# define GetResourceLimit getrlimit
# endif
#elif defined(__hpux)
# include <sys/param.h>
# include <sys/pstat.h>
# if defined(KWSYS_SYS_HAS_MPCTL_H)
# include <sys/mpctl.h>
# endif
#endif
#ifdef __HAIKU__
# include <OS.h>
#endif
#if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
# include <execinfo.h>
# if defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
# include <cxxabi.h>
# endif
# if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
# include <dlfcn.h>
# endif
#else
# undef KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE
# undef KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP
#endif
#include <cctype> // int isdigit(int c);
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory.h>
#if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64) && \
!defined(__clang__)
# define USE_ASM_INSTRUCTIONS 1
#else
# define USE_ASM_INSTRUCTIONS 0
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__clang__) && \
!defined(_M_ARM64)
# include <intrin.h>
# define USE_CPUID_INTRINSICS 1
#else
# define USE_CPUID_INTRINSICS 0
#endif
#if USE_ASM_INSTRUCTIONS || USE_CPUID_INTRINSICS
# define USE_CPUID 1
#else
# define USE_CPUID 0
#endif
#if USE_CPUID
# define CPUID_AWARE_COMPILER
/**
* call CPUID instruction
*
* Will return false if the instruction failed.
*/
static bool call_cpuid(int select, int result[4])
{
# if USE_CPUID_INTRINSICS
__cpuid(result, select);
return true;
# else
int tmp[4];
# if defined(_MSC_VER)
// Use SEH to determine CPUID presence
__try {
_asm {
# ifdef CPUID_AWARE_COMPILER
; we must push/pop the registers <<CPUID>> writes to, as the
; optimiser does not know about <<CPUID>>, and so does not expect
; these registers to change.
push eax
push ebx
push ecx
push edx
# endif
; <<CPUID>>
mov eax, select
# ifdef CPUID_AWARE_COMPILER
cpuid
# else
_asm _emit 0x0f
_asm _emit 0xa2
# endif
mov tmp[0 * TYPE int], eax
mov tmp[1 * TYPE int], ebx
mov tmp[2 * TYPE int], ecx
mov tmp[3 * TYPE int], edx
# ifdef CPUID_AWARE_COMPILER
pop edx
pop ecx
pop ebx
pop eax
# endif
}
} __except (1) {
return false;
}
memcpy(result, tmp, sizeof(tmp));
# endif
// The cpuid instruction succeeded.
return true;
# endif
}
#endif
namespace KWSYS_NAMESPACE {
template <typename T>
T min(T a, T b)
{
return a < b ? a : b;
}
extern "C" {
using SigAction = void (*)(int, siginfo_t*, void*);
}
// Define SystemInformationImplementation class
using DELAY_FUNC = void (*)(unsigned int);
class SystemInformationImplementation
{
public:
SystemInformationImplementation();
~SystemInformationImplementation() = default;
const char* GetVendorString() const;
const char* GetVendorID();
std::string GetTypeID() const;
std::string GetFamilyID() const;
std::string GetModelID() const;
std::string GetModelName() const;
std::string GetSteppingCode() const;
const char* GetExtendedProcessorName() const;
const char* GetProcessorSerialNumber() const;
int GetProcessorCacheSize() const;
unsigned int GetLogicalProcessorsPerPhysical() const;
float GetProcessorClockFrequency() const;
int GetProcessorAPICID() const;
int GetProcessorCacheXSize(long int) const;
bool DoesCPUSupportFeature(long int) const;
const char* GetOSName();
const char* GetHostname();
int GetFullyQualifiedDomainName(std::string& fqdn);
const char* GetOSRelease();
const char* GetOSVersion();
const char* GetOSPlatform();
bool Is64Bits() const;
unsigned int GetNumberOfLogicalCPU() const; // per physical cpu
unsigned int GetNumberOfPhysicalCPU() const;
bool DoesCPUSupportCPUID();
// Retrieve memory information in MiB.
size_t GetTotalVirtualMemory() const;
size_t GetAvailableVirtualMemory() const;
size_t GetTotalPhysicalMemory() const;
size_t GetAvailablePhysicalMemory() const;
long long GetProcessId();
// Retrieve memory information in KiB.
long long GetHostMemoryTotal();
long long GetHostMemoryAvailable(const char* hostLimitEnvVarName);
long long GetHostMemoryUsed();
long long GetProcMemoryAvailable(const char* hostLimitEnvVarName,
const char* procLimitEnvVarName);
long long GetProcMemoryUsed();
double GetLoadAverage();
// enable/disable stack trace signal handler.
static void SetStackTraceOnError(int enable);
// get current stack
static std::string GetProgramStack(int firstFrame, int wholePath);
/** Run the different checks */
void RunCPUCheck();
void RunOSCheck();
void RunMemoryCheck();
public:
using ID = struct tagID
{
int Type;
int Family;
int Model;
int Revision;
int ExtendedFamily;
int ExtendedModel;
std::string ProcessorName;
std::string Vendor;
std::string SerialNumber;
std::string ModelName;
};
using CPUPowerManagement = struct tagCPUPowerManagement
{
bool HasVoltageID;
bool HasFrequencyID;
bool HasTempSenseDiode;
};
using CPUExtendedFeatures = struct tagCPUExtendedFeatures
{
bool Has3DNow;
bool Has3DNowPlus;
bool SupportsMP;
bool HasMMXPlus;
bool HasSSEMMX;
unsigned int LogicalProcessorsPerPhysical;
int APIC_ID;
CPUPowerManagement PowerManagement;
};
using CPUFeatures = struct CPUtagFeatures
{
bool HasFPU;
bool HasTSC;
bool HasMMX;
bool HasSSE;
bool HasSSEFP;
bool HasSSE2;
bool HasIA64;
bool HasAPIC;
bool HasCMOV;
bool HasMTRR;
bool HasACPI;
bool HasSerial;
bool HasThermal;
int CPUSpeed;
int L1CacheSize;
int L2CacheSize;
int L3CacheSize;
CPUExtendedFeatures ExtendedFeatures;
};
enum Manufacturer
{
AMD,
Intel,
NSC,
UMC,
Cyrix,
NexGen,
IDT,
Rise,
Transmeta,
Sun,
IBM,
Motorola,
HP,
Hygon,
Zhaoxin,
Apple,
UnknownManufacturer
};
protected:
// For windows
bool RetrieveCPUFeatures();
bool RetrieveCPUIdentity();
bool RetrieveCPUCacheDetails();
bool RetrieveClassicalCPUCacheDetails();
bool RetrieveCPUClockSpeed();
bool RetrieveClassicalCPUClockSpeed();
bool RetrieveCPUExtendedLevelSupport(int);
bool RetrieveExtendedCPUFeatures();
bool RetrieveProcessorSerialNumber();
bool RetrieveCPUPowerManagement();
bool RetrieveClassicalCPUIdentity();
bool RetrieveExtendedCPUIdentity();
// Processor information
Manufacturer ChipManufacturer;
CPUFeatures Features;
ID ChipID;
float CPUSpeedInMHz;
unsigned int NumberOfLogicalCPU;
unsigned int NumberOfPhysicalCPU;
void CPUCountWindows(); // For windows
unsigned char GetAPICId(); // For windows
bool IsSMTSupported() const;
static long long GetCyclesDifference(DELAY_FUNC,
unsigned int); // For windows
// For Linux and Cygwin, /proc/cpuinfo formats are slightly different
bool RetrieveInformationFromCpuInfoFile();
std::string ExtractValueFromCpuInfoFile(std::string buffer, const char* word,
size_t init = 0);
bool QueryLinuxMemory();
bool QueryCygwinMemory();
static void Delay(unsigned int);
static void DelayOverhead(unsigned int);
void FindManufacturer(const std::string& family = "");
// For Mac
bool ParseSysCtl();
int CallSwVers(const char* arg, std::string& ver);
void TrimNewline(std::string&);
std::string ExtractValueFromSysCtl(const char* word);
std::string SysCtlBuffer;
// For Solaris
bool QuerySolarisMemory();
bool QuerySolarisProcessor();
std::string ParseValueFromKStat(const char* arguments);
std::string RunProcess(std::vector<const char*> args);
// For Haiku OS
bool QueryHaikuInfo();
// For QNX
bool QueryQNXMemory();
bool QueryQNXProcessor();
// For OpenBSD, FreeBSD, NetBSD, DragonFly
bool QueryBSDMemory();
bool QueryBSDProcessor();
// For HP-UX
bool QueryHPUXMemory();
bool QueryHPUXProcessor();
// For Microsoft Windows
bool QueryWindowsMemory();
// For AIX
bool QueryAIXMemory();
bool QueryProcessorBySysconf();
bool QueryProcessor();
// Evaluate the memory information.
bool QueryMemoryBySysconf();
bool QueryMemory();
size_t TotalVirtualMemory;
size_t AvailableVirtualMemory;
size_t TotalPhysicalMemory;
size_t AvailablePhysicalMemory;
size_t CurrentPositionInFile;
// Operating System information
bool QueryOSInformation();
std::string OSName;
std::string Hostname;
std::string OSRelease;
std::string OSVersion;
std::string OSPlatform;
bool OSIs64Bit;
};
SystemInformation::SystemInformation()
{
this->Implementation = new SystemInformationImplementation;
}
SystemInformation::~SystemInformation()
{
delete this->Implementation;
}
const char* SystemInformation::GetVendorString()
{
return this->Implementation->GetVendorString();
}
const char* SystemInformation::GetVendorID()
{
return this->Implementation->GetVendorID();
}
std::string SystemInformation::GetTypeID()
{
return this->Implementation->GetTypeID();
}
std::string SystemInformation::GetFamilyID()
{
return this->Implementation->GetFamilyID();
}
std::string SystemInformation::GetModelID()
{
return this->Implementation->GetModelID();
}
std::string SystemInformation::GetModelName()
{
return this->Implementation->GetModelName();
}
std::string SystemInformation::GetSteppingCode()
{
return this->Implementation->GetSteppingCode();
}
const char* SystemInformation::GetExtendedProcessorName()
{
return this->Implementation->GetExtendedProcessorName();
}
const char* SystemInformation::GetProcessorSerialNumber()
{
return this->Implementation->GetProcessorSerialNumber();
}
int SystemInformation::GetProcessorCacheSize()
{
return this->Implementation->GetProcessorCacheSize();
}
unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
{
return this->Implementation->GetLogicalProcessorsPerPhysical();
}
float SystemInformation::GetProcessorClockFrequency()
{
return this->Implementation->GetProcessorClockFrequency();
}
int SystemInformation::GetProcessorAPICID()
{
return this->Implementation->GetProcessorAPICID();
}
int SystemInformation::GetProcessorCacheXSize(long int l)
{
return this->Implementation->GetProcessorCacheXSize(l);
}
bool SystemInformation::DoesCPUSupportFeature(long int i)
{
return this->Implementation->DoesCPUSupportFeature(i);
}
std::string SystemInformation::GetCPUDescription()
{
std::ostringstream oss;
oss << this->GetNumberOfPhysicalCPU() << " core ";
if (this->GetModelName().empty()) {
oss << this->GetProcessorClockFrequency() << " MHz "
<< this->GetVendorString() << " " << this->GetExtendedProcessorName();
} else {
oss << this->GetModelName();
}
// remove extra spaces
std::string tmp = oss.str();
size_t pos;
while ((pos = tmp.find(" ")) != std::string::npos) {
tmp.replace(pos, 2, " ");
}
return tmp;
}
const char* SystemInformation::GetOSName()
{
return this->Implementation->GetOSName();
}
const char* SystemInformation::GetHostname()
{
return this->Implementation->GetHostname();
}
std::string SystemInformation::GetFullyQualifiedDomainName()
{
std::string fqdn;
this->Implementation->GetFullyQualifiedDomainName(fqdn);
return fqdn;
}
const char* SystemInformation::GetOSRelease()
{
return this->Implementation->GetOSRelease();
}
const char* SystemInformation::GetOSVersion()
{
return this->Implementation->GetOSVersion();
}
const char* SystemInformation::GetOSPlatform()
{
return this->Implementation->GetOSPlatform();
}
int SystemInformation::GetOSIsWindows()
{
#if defined(_WIN32)
return 1;
#else
return 0;
#endif
}
int SystemInformation::GetOSIsLinux()
{
#if defined(__linux)
return 1;
#else
return 0;
#endif
}
int SystemInformation::GetOSIsApple()
{
#if defined(__APPLE__)
return 1;
#else
return 0;
#endif
}
std::string SystemInformation::GetOSDescription()
{
std::ostringstream oss;
oss << this->GetOSName() << " " << this->GetOSRelease() << " "
<< this->GetOSVersion();
return oss.str();
}
bool SystemInformation::Is64Bits()
{
return this->Implementation->Is64Bits();
}
unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
{
return this->Implementation->GetNumberOfLogicalCPU();
}
unsigned int SystemInformation::GetNumberOfPhysicalCPU()
{
return this->Implementation->GetNumberOfPhysicalCPU();
}
bool SystemInformation::DoesCPUSupportCPUID()
{
return this->Implementation->DoesCPUSupportCPUID();
}
// Retrieve memory information in MiB.
size_t SystemInformation::GetTotalVirtualMemory()
{
return this->Implementation->GetTotalVirtualMemory();
}
size_t SystemInformation::GetAvailableVirtualMemory()
{
return this->Implementation->GetAvailableVirtualMemory();
}
size_t SystemInformation::GetTotalPhysicalMemory()
{
return this->Implementation->GetTotalPhysicalMemory();
}
size_t SystemInformation::GetAvailablePhysicalMemory()
{
return this->Implementation->GetAvailablePhysicalMemory();
}
std::string SystemInformation::GetMemoryDescription(
const char* hostLimitEnvVarName, const char* procLimitEnvVarName)
{
std::ostringstream oss;
oss << "Host Total: " << this->GetHostMemoryTotal()
<< " KiB, Host Available: "
<< this->GetHostMemoryAvailable(hostLimitEnvVarName)
<< " KiB, Process Available: "
<< this->GetProcMemoryAvailable(hostLimitEnvVarName, procLimitEnvVarName)
<< " KiB";
return oss.str();
}
// host memory info in units of KiB.
long long SystemInformation::GetHostMemoryTotal()
{
return this->Implementation->GetHostMemoryTotal();
}
long long SystemInformation::GetHostMemoryAvailable(
const char* hostLimitEnvVarName)
{
return this->Implementation->GetHostMemoryAvailable(hostLimitEnvVarName);
}
long long SystemInformation::GetHostMemoryUsed()
{
return this->Implementation->GetHostMemoryUsed();
}
// process memory info in units of KiB.
long long SystemInformation::GetProcMemoryAvailable(
const char* hostLimitEnvVarName, const char* procLimitEnvVarName)
{
return this->Implementation->GetProcMemoryAvailable(hostLimitEnvVarName,
procLimitEnvVarName);
}
long long SystemInformation::GetProcMemoryUsed()
{
return this->Implementation->GetProcMemoryUsed();
}
double SystemInformation::GetLoadAverage()
{
return this->Implementation->GetLoadAverage();
}
long long SystemInformation::GetProcessId()
{
return this->Implementation->GetProcessId();
}
void SystemInformation::SetStackTraceOnError(int enable)
{
SystemInformationImplementation::SetStackTraceOnError(enable);
}
std::string SystemInformation::GetProgramStack(int firstFrame, int wholePath)
{
return SystemInformationImplementation::GetProgramStack(firstFrame,
wholePath);
}
/** Run the different checks */
void SystemInformation::RunCPUCheck()
{
this->Implementation->RunCPUCheck();
}
void SystemInformation::RunOSCheck()
{
this->Implementation->RunOSCheck();
}
void SystemInformation::RunMemoryCheck()
{
this->Implementation->RunMemoryCheck();
}
// SystemInformationImplementation starts here
#if USE_CPUID
# define STORE_TLBCACHE_INFO(x, y) x = (x < (y)) ? (y) : x
# define TLBCACHE_INFO_UNITS (15)
#endif
#if USE_ASM_INSTRUCTIONS
# define CLASSICAL_CPU_FREQ_LOOP 10000000
# define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
#endif
#define INITIAL_APIC_ID_BITS 0xFF000000
// initial APIC ID for the processor this code is running on.
// Default value = 0xff if HT is not supported
// Hide implementation details in an anonymous namespace.
namespace {
// *****************************************************************************
#if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
int LoadLines(FILE* file, std::vector<std::string>& lines)
{
// Load each line in the given file into a the vector.
int nRead = 0;
const int bufSize = 1024;
char buf[bufSize] = { '\0' };
while (!feof(file) && !ferror(file)) {
errno = 0;
if (fgets(buf, bufSize, file) == nullptr) {
if (ferror(file) && (errno == EINTR)) {
clearerr(file);
}
continue;
}
char* pBuf = buf;
while (*pBuf) {
if (*pBuf == '\n')
*pBuf = '\0';
pBuf += 1;
}
lines.emplace_back(buf);
++nRead;
}
if (ferror(file)) {
return 0;
}
return nRead;
}
# if defined(__linux) || defined(__CYGWIN__)
// *****************************************************************************
int LoadLines(const char* fileName, std::vector<std::string>& lines)
{
FILE* file = fopen(fileName, "r");
if (file == nullptr) {
return 0;
}
int nRead = LoadLines(file, lines);
fclose(file);
return nRead;
}
# endif
// ****************************************************************************
template <typename T>
int NameValue(std::vector<std::string> const& lines, std::string const& name,
T& value)
{
size_t nLines = lines.size();
for (size_t i = 0; i < nLines; ++i) {
size_t at = lines[i].find(name);
if (at == std::string::npos) {
continue;
}
std::istringstream is(lines[i].substr(at + name.size()));
is >> value;
return 0;
}
return -1;
}
#endif
#if defined(__linux) || defined(__CYGWIN__)
// ****************************************************************************
template <typename T>
int GetFieldsFromFile(const char* fileName, const char** fieldNames, T* values)
{
std::vector<std::string> fields;
if (!LoadLines(fileName, fields)) {
return -1;
}
int i = 0;
while (fieldNames[i] != nullptr) {
int ierr = NameValue(fields, fieldNames[i], values[i]);
if (ierr) {
return -(i + 2);
}
i += 1;
}
return 0;
}
// ****************************************************************************
template <typename T>
int GetFieldFromFile(const char* fileName, const char* fieldName, T& value)
{
const char* fieldNames[2] = { fieldName, nullptr };
T values[1] = { T(0) };
int ierr = GetFieldsFromFile(fileName, fieldNames, values);
if (ierr) {
return ierr;
}
value = values[0];
return 0;
}
#endif
// ****************************************************************************
#if defined(__APPLE__)
template <typename T>
int GetFieldsFromCommand(const char* command, const char** fieldNames,
T* values)
{
FILE* file = popen(command, "r");
if (file == nullptr) {
return -1;
}
std::vector<std::string> fields;
int nl = LoadLines(file, fields);
pclose(file);
if (nl == 0) {
return -1;
}
int i = 0;
while (fieldNames[i] != nullptr) {
int ierr = NameValue(fields, fieldNames[i], values[i]);
if (ierr) {
return -(i + 2);
}
i += 1;
}
return 0;
}
#endif
// ****************************************************************************
#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
void StacktraceSignalHandler(int sigNo, siginfo_t* sigInfo,
void* /*sigContext*/)
{
# if defined(__linux) || defined(__APPLE__)
std::ostringstream oss;