-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathHybridDetect.h
1596 lines (1403 loc) · 49.6 KB
/
HybridDetect.h
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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2021-2024, Intel Corporation
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of
// the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <stdint.h>
#include <array>
#include <vector>
#include <string>
#include <stdio.h>
#include <bitset>
#include <map>
#include <thread>
#include <string.h>
#ifndef HYBRIDDETECT_DEBUG_REQUIRE
#include <assert.h>
#define HYBRIDDETECT_DEBUG_REQUIRE(x) assert(x)
#endif
#ifdef _WIN32
#define HYBRIDDETECT_OS_WIN 1
#if defined(_M_X64) || __x86_64__
#define HYBRIDDETECT_CPU_X86_64 1
#else
#define HYBRIDDETECT_CPU_X86_64 0
#endif
#else
#if __APPLE__
#include <sys/sysctl.h>
#include <TargetConditionals.h>
#if TARGET_CPU_X86_64
#define HYBRIDDETECT_CPU_X86_64 1
#else
#define HYBRIDDETECT_CPU_X86_64 0
#endif
#else
#if __x86_64__
#define HYBRIDDETECT_CPU_X86_64 1
#else
#define HYBRIDDETECT_CPU_X86_64 0
#endif
#endif
#endif
#ifdef HYBRIDDETECT_OS_WIN
#include <Windows.h>
#include <powrprof.h>
#include <VersionHelpers.h>
#include <intrin.h>
#include <malloc.h>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-identifier"
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wsign-compare"
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#endif
#pragma comment(lib, "Powrprof.lib")
#else
typedef unsigned long ULONG;
typedef unsigned long long ULONG64;
typedef size_t SIZE_T;
typedef unsigned char BYTE;
typedef void* HANDLE;
#endif
#ifndef HYBRID_DETECT_TRACE_ENABLED_VOLUME
#define HYBRID_DETECT_TRACE_ENABLED_VOLUME 0 // Scale of 1-10, 10 being most verbose
#endif
#if HYBRID_DETECT_TRACE_ENABLED_VOLUME
#define HYBRID_DETECT_TRACE(vol, fmt, ...) if (vol <= HYBRID_DETECT_TRACE_ENABLED_VOLUME){ \
printf("[%s][vol=%2d]: "##fmt##"\n", __FUNCTION__, vol, ##__VA_ARGS__); \
fflush(stdout); \
}
#else
#define HYBRID_DETECT_TRACE(vol, fmt, ...)
#endif
namespace HybridDetect
{
#if defined(_WIN32)
#if !defined(_M_ARM64)
// Simple wrapper for __cpuid intrinsic, created for cross platform support e.g. WIN32
#define CPUID(registers, function) __cpuid((int*)registers, (int)function);
#define CPUIDEX(registers, function, extFunction) __cpuidex((int*)registers, (int)function, (int)extFunction);
#define XGETBV(xcrReg) _xgetbv(xcrReg)
#else
// This is just to get LibXPUInfo to compile for arm64. It should be a dead path otherwise.
#define CPUID(registers, function) 0;
#define CPUIDEX(registers, function, extFunction) 0;
#define XGETBV(xcrReg) 0;
#endif
#elif __APPLE__
#if TARGET_CPU_X86_64
#define CPUID(registers, function) asm volatile ("cpuid" : "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) : "a" (function), "c" (0));
#define CPUIDEX(registers, function, extFunction) asm volatile ("cpuid" : "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) : "a" (function), "c" (extFunction));
inline std::uint64_t xgetbv_asm(int xcrReg)
{
std::uint64_t xcrh;
asm volatile ("xgetbv" : "=a" (xcrReg), "=d" (xcrh) : "c" (0));
return xcrReg | (xcrh << 32);
}
#define XGETBV(xcrReg) xgetbv_asm(xcrReg)
#else
#define CPUID(registers, function)
#define CPUIDEX(registers, function, extFunction)
#define XGETBV(xcrReg) 0
#endif
#else
// Linux Stuff
#define CPUID(registers, function) asm volatile ("cpuid" : "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) : "a" (function), "c" (0));
#define CPUIDEX(registers, function, extFunction) asm volatile ("cpuid" : "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]) : "a" (function), "c" (extFunction));
inline std::uint64_t xgetbv_asm(int xcrReg)
{
std::uint64_t xcrh;
asm volatile ("xgetbv" : "=a" (xcrReg), "=d" (xcrh) : "c" (0));
return xcrReg | (xcrh << 32);
}
#define XGETBV(xcrReg) xgetbv_asm(xcrReg)
#endif
// Enables/Disables Hybrid Detect
#define ENABLE_HYBRID_DETECT
// Tells the application to treat the target system as a heterogeneous software proxy.
//#define ENABLE_SOFTWARE_PROXY
// Enables/Disables Run On API
#define ENABLE_RUNON
// Enables/Disables ThreadPriority Based on Core-Type
//#define ENABLE_RUNON_PRIORITY
// Enables/Disables SetThreadInformation Memory Priority Based on Core-Type
//#define ENABLE_RUNON_MEMORY_PRIORITY
// Enables/Disables SetThreadInformation Execution Speed based on Core-Type
//#define ENABLE_RUNON_EXECUTION_SPEED
// Enables CPU-Sets and Disables ThreadAffinityMasks
#define ENABLE_CPU_SETS
// No current CPUs have ISA support that varies between cores
#ifndef ENABLE_PER_LOGICAL_CPUID_ISA_DETECTION
#define ENABLE_PER_LOGICAL_CPUID_ISA_DETECTION 1 // Default to enabled for backward compatibility
#endif
// Simple conversion from an ordinal, n, to a set bit at position n
#define IndexToMask(n) (1UL << n)
// Macros to store values for CPUID register ordinals
#define CPUID_EAX 0
#define CPUID_EBX 1
#define CPUID_ECX 2
#define CPUID_EDX 3
#define LEAF_CPUID_BASIC 0x00 // Basic CPUID Information
#define LEAF_THERMAL_POWER 0x06 // Thermal and Power Management Leaf
#define LEAF_EXTENDED_FEATURE_FLAGS 0x07 // Structured Extended Feature Flags Enumeration Leaf (Output depends on ECX input value)
#define LEAF_EXTENDED_STATE 0x0D // Processor Extended State Enumeration Main Leaf (EAX = 0DH, ECX = 0)
#define LEAF_FREQUENCY_INFORMATION 0x16 // Processor Frequency Information Leaf function 0x16 only works on Skylake or newer.
#define LEAF_HYBRID_INFORMATION 0x1A // Hybrid Information Sub - leaf(EAX = 1AH, ECX = 0)
#define LEAF_EXTENDED_INFORMATION_0 0x80000000 // Extended Function CPUID Information
#define LEAF_EXTENDED_INFORMATION_1 0x80000001 // Extended Function CPUID Information
#define LEAF_EXTENDED_BRAND_STRING_1 0x80000002 // Extended Function CPUID Information
#define LEAF_EXTENDED_BRAND_STRING_2 0x80000003 // Extended Function CPUID Information
#define LEAF_EXTENDED_BRAND_STRING_3 0x80000004 // Extended Function CPUID Information
#define LEAF_EXTENDED_INFORMATION_5 0x80000005 // Extended Function CPUID Information
#define LEAF_EXTENDED_INFORMATION_6 0x80000006 // Extended Function CPUID Information
#define LEAF_EXTENDED_INFORMATION_7 0x80000007 // Extended Function CPUID Information
#define LEAF_EXTENDED_INFORMATION_8 0x80000008 // Extended Function CPUID Information
enum CoreTypes
{
ANY = -1,
NONE = 0x00,
#if HYBRIDDETECT_CPU_X86_64
RESERVED0 = 0x10,
INTEL_ATOM = 0x20,
RESERVED1 = 0x30,
INTEL_CORE = 0x40,
#else
PERFLEVEL0 = 1,
PERFLEVEL1 = 2,
#endif
};
// Struct to store information for each Cache.
typedef struct _CACHE_INFO
{
unsigned group = 0;
std::bitset<64> processorMask = 0;
unsigned level = 0;
unsigned size = 0;
unsigned lineSize = 0;
unsigned type = 0; // PROCESSOR_CACHE_TYPE
unsigned associativity = 0;
} CACHE_INFO, * PCACHE_INFO;
// Struct to store Power information for a logical processor.
typedef struct _LOGICAL_PROCESSOR_POWER_INFORMATION {
ULONG number = 0;
ULONG maxMhz = 0;
ULONG currentMhz = 0;
ULONG mhzLimit = 0;
ULONG maxIdleState = 0;
ULONG currentIdleState = 0;
} LOGICAL_PROCESSOR_POWER_INFORMATION, * PLOGICAL_PROCESSOR_POWER_INFORMATION;
// Struct to store Power information for a logical processor.
typedef struct _LOGICAL_PROCESSOR_INFO
{
unsigned id = 0;
unsigned group = 0;
unsigned node = 0;
unsigned coreIndex = 0;
unsigned logicalProcessorIndex = 0;
std::bitset<64> processorMask = 0;
unsigned baseFrequency = 0;
unsigned currentFrequency = 0;
unsigned maximumFrequency = 0;
unsigned busFrequency = 0;
#if ENABLE_PER_LOGICAL_CPUID_ISA_DETECTION
unsigned SSE : 1;
unsigned AVX : 1;
unsigned AVX2 : 1;
unsigned AVX512 : 1;
unsigned AVX512F : 1;
unsigned AVX512DQ : 1;
unsigned AVX512PF : 1;
unsigned AVX512ER : 1;
unsigned AVX512CD : 1;
unsigned AVX512BW : 1;
unsigned AVX512VL : 1;
unsigned AVX512_IFMA : 1;
unsigned AVX512_VBMI : 1;
unsigned AVX512_VBMI2 : 1;
unsigned AVX512_VNNI : 1;
unsigned AVX512_BITALG : 1;
unsigned AVX512_VPOPCNTDQ : 1;
unsigned AVX512_4VNNIW : 1;
unsigned AVX512_4FMAPS : 1;
unsigned AVX512_VP2INTERSECT : 1;
unsigned SGX : 1;
unsigned SHA : 1;
#endif // ENABLE_PER_LOGICAL_CPUID_ISA_DETECTION
unsigned parked : 1;
unsigned allocated : 1;
unsigned allocatedToTargetProcess : 1;
unsigned realTime : 1;
ULONG64 allocationTag = 0;
unsigned efficiencyClass = 0;
unsigned schedulingClass = 0;
CoreTypes coreType = CoreTypes::NONE;
LOGICAL_PROCESSOR_POWER_INFORMATION powerInformation;
} LOGICAL_PROCESSOR_INFO, * PLOGICAL_PROCESSOR_INFO;
typedef struct _GROUP_INFO
{
unsigned activeGroupCount = 0;
unsigned maximumGroupCount = 0;
unsigned activeProcessorCount = 0;
unsigned maximumProcessorCount = 0;
ULONG64 activeProcessorMask = 0;
} GROUP_INFO, * PGROUP_INFO;
typedef struct _NUMA_NODE_INFO
{
unsigned nodeNumber = 0;
unsigned group = 0;
ULONG64 mask = 0;
} NUMA_NODE_INFO, * PNUMA_NODE_INFO;
// Feature flags
struct FeatureFlags
{
// CPUID.1:ECX
unsigned SSE3 : 1; // 0, All x64 CPUs have at least SSE3
unsigned PCLMULQDQ : 1; // 1
unsigned SSSE3 : 1; // 9
unsigned FMA : 1; // 12, See 14.5.3 - On top of AVX
unsigned SSE4_1 : 1; // 19
unsigned SSE4_2 : 1; // 20
unsigned AES : 1; // 25
unsigned XSAVE : 1; // 26, Safe to call xgetbv with ecx=0
unsigned OSXSAVE : 1; // 27
unsigned AVX : 1; // 28. See 14.3
unsigned F16C : 1; // 29
unsigned RDRAND : 1; // 30
//AVX2: CPUID.(EAX=07H, ECX=0H):EBX.AVX2[bit 5]=1
unsigned AVX2 : 1; // Check OSXSAVE and AVX and OS_Supports_YMM
//Verify both CPUID.0x7.0:EBX.AVX512F[bit 16] = 1
unsigned AVX512F : 1; // 16
unsigned AVX512DQ : 1; // 17
unsigned AVX512_IFMA : 1; // 21
unsigned AVX512CD : 1; // 28
unsigned AVX512BW : 1; // 30
unsigned AVX512VL : 1; // 31
// Derived
unsigned OS_Supports_YMM : 1;
unsigned OS_Supports_ZMM : 1;
bool AVX_Supported() const
{
return AVX && OS_Supports_YMM;
}
bool F16C_Supported() const
{
return AVX_Supported() && F16C;
}
bool AVX2_Supported() const
{
return AVX_Supported() && AVX2;
}
bool AVX512_State_Supported() const
{
return OS_Supports_ZMM;
}
// Initial set of AVX512 features supported on SkylakeX
bool AVX512_SKX_Supported() const
{
return OS_Supports_ZMM && AVX512F && AVX512VL && AVX512BW && AVX512DQ && AVX512CD;
}
};
// Struct to store Processor information
typedef struct _PROCESSOR_INFO
{
char vendorID[13];
char brandString[64];
unsigned numGroups = 0;
unsigned numNUMANodes = 0;
unsigned numProcessorPackages = 0;
unsigned numPhysicalCores = 0;
unsigned numLogicalCores = 0;
unsigned numL1Caches = 0;
unsigned numL2Caches = 0;
unsigned numL3Caches = 0;
bool hybrid = false;
bool turboBoost = false;
bool turboBoost3_0 = false;
std::vector<GROUP_INFO> groups;
std::vector<NUMA_NODE_INFO> nodes;
std::vector<CACHE_INFO> caches;
std::vector<LOGICAL_PROCESSOR_INFO> cores;
// Store map of logical processors returned from GLPI.
// short = Core Type, ULONG64 = 64-bit processor mask
std::map<short, ULONG64> coreMasks;
#ifdef ENABLE_CPU_SETS
// Store map of logical processors returned from GetSystemCPUSetInformation.
// unsined custom logical cluster key, use short to store EffeciencyClass as a key
// std::vector<ULONG> = list of CPU Set IDs
std::map<unsigned, std::vector<ULONG>> cpuSets;
#endif
bool IsIntel() const { return !strcmp("GenuineIntel", vendorID); }
bool IsAMD() const { return !strcmp("AuthenticAMD", vendorID); }
inline int GetCoreTypeCount(CoreTypes coreType)
{
#ifdef ENABLE_CPU_SETS
return (int)cpuSets[(ULONG)coreType].size();
#else
std::bitset<64> bits = coreMasks[coreType];
return (int)bits.count();
#endif
}
unsigned cpuid_1_eax = 0; // Basic CPU family/model/stepping
union
{
FeatureFlags flags;
std::uint64_t flagsUI64 = 0; // Make flags field 8-bytes for extensibility and easier (de)serialization
};
} PROCESSOR_INFO, * PPROCESSOR_INFO;
// Type to String Conversion Helper Function
inline const char* CoreTypeString(CoreTypes type)
{
switch (type)
{
case CoreTypes::NONE:
return "None";
#if HYBRIDDETECT_CPU_X86_64
case CoreTypes::RESERVED0:
return "Reserved_0";
case CoreTypes::INTEL_ATOM:
return "E-Core";
case CoreTypes::RESERVED1:
return "Reserved_1";
case CoreTypes::INTEL_CORE:
return "P-Core";
#else
case CoreTypes::PERFLEVEL0:
return "PerfLevel0";
case CoreTypes::PERFLEVEL1:
return "PerfLevel1";
#endif
default:
return "Any";
}
}
#ifdef HYBRIDDETECT_OS_WIN
// Cache to String Conversion Helper Function
inline const char* CacheTypeString(int type)
{
switch (type)
{
case CacheUnified:
return "Unified";
case CacheInstruction:
return "Instruction";
case CacheData:
return "Data";
case CacheTrace:
return "Trace";
}
return "Any";
}
#endif
// Helper function to Call the CPUID intrinsic
inline bool CallCPUID(unsigned function, std::array<unsigned, 4>& registers, unsigned extFunction = 0, unsigned CPUIDFunctionMax = LEAF_EXTENDED_INFORMATION_8)
{
HYBRID_DETECT_TRACE(10, ">>> (0x%.8x)", function);
if (function > CPUIDFunctionMax) return false;
CPUIDEX(registers.data(), function, extFunction);
HYBRID_DETECT_TRACE(10, "<<<");
return true;
}
template<typename T>
T* AdvanceBytes(T* p, SIZE_T cb)
{
return reinterpret_cast<T*>(reinterpret_cast<BYTE*>(p) + cb);
}
#ifdef HYBRIDDETECT_OS_WIN
class EnumLogicalProcessorInformation
{
// Based On: https://devblogs.microsoft.com/oldnewthing/20131028-00/?p=2823
public:
EnumLogicalProcessorInformation(LOGICAL_PROCESSOR_RELATIONSHIP Relationship)
: m_pinfoBase(nullptr), m_pinfoCurrent(nullptr), m_cbRemaining(0)
{
DWORD cb = 0;
if (GetLogicalProcessorInformationEx(Relationship,
nullptr, &cb)) return;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
m_pinfoBase =
reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>
(LocalAlloc(LMEM_FIXED, cb));
if (!m_pinfoBase) return;
if (!GetLogicalProcessorInformationEx(Relationship,
m_pinfoBase, &cb)) return;
m_pinfoCurrent = m_pinfoBase;
m_cbRemaining = cb;
}
~EnumLogicalProcessorInformation() { LocalFree(m_pinfoBase); }
void MoveNext()
{
if (m_pinfoCurrent) {
m_cbRemaining -= m_pinfoCurrent->Size;
if (m_cbRemaining) {
m_pinfoCurrent = AdvanceBytes(m_pinfoCurrent,
m_pinfoCurrent->Size);
}
else {
m_pinfoCurrent = nullptr;
}
}
}
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* Current()
{
return m_pinfoCurrent;
}
private:
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* m_pinfoBase;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* m_pinfoCurrent;
DWORD m_cbRemaining;
};
inline void PrintMask(KAFFINITY Mask)
{
#if 0
printf("0x%.16llx", Mask);
#else
for (int i = (sizeof(Mask) * 8) - 1; i >= 0; i--) {
if (Mask & (static_cast<KAFFINITY>(1) << i))
printf("%d", 1);
else
printf("%d", 0);
}
#endif
}
inline bool IsHybridOSEx()
{
OSVERSIONINFOEX versionInfo;
DWORDLONG conditionMask = 0;
constexpr BYTE operation = VER_GREATER_EQUAL;
ZeroMemory(&versionInfo, sizeof(OSVERSIONINFOEX));
versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
versionInfo.dwMajorVersion = 10;
versionInfo.dwMinorVersion = 0;
versionInfo.dwBuildNumber = 22000;
//versionInfo.wServicePackMajor = 0;
//versionInfo.wServicePackMinor = 0;
VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, operation);
VER_SET_CONDITION(conditionMask, VER_MINORVERSION, operation);
VER_SET_CONDITION(conditionMask, VER_BUILDNUMBER, operation);
//VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMAJOR, operation);
//VER_SET_CONDITION(conditionMask, VER_SERVICEPACKMINOR, operation);
return VerifyVersionInfo(&versionInfo,
VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER, conditionMask);
}
// Calls CPUID & GetLogicalProcessors to fill in PROCESSOR_INFO Caches & Cores
inline bool GetLogicalProcessors(PROCESSOR_INFO& procInfo)
{
HYBRID_DETECT_TRACE(7, ">>>");
#ifdef ENABLE_HYBRID_DETECT
#ifdef ENABLE_CPU_SETS
unsigned long bufferSize;
// Get the Current Process Handle.
HANDLE curProc = GetCurrentProcess();
// Get total number (size) of elements in the data structure.
GetSystemCpuSetInformation(nullptr, 0, &bufferSize, curProc, 0);
// Allocate data structures based on size returned from first call.
auto buffer = std::make_unique<uint8_t[]>(bufferSize);
// Get all of the CPUSet elements
if(!GetSystemCpuSetInformation(reinterpret_cast<PSYSTEM_CPU_SET_INFORMATION>(buffer.get()), bufferSize, &bufferSize, curProc, 0))
{
return false;
}
uint8_t* cpuSetPtr = buffer.get();
for (ULONG cpuSetSize = 0; cpuSetSize < bufferSize; )
{
auto nextCPUSet = reinterpret_cast<PSYSTEM_CPU_SET_INFORMATION>(cpuSetPtr);
if (nextCPUSet->Type == CPU_SET_INFORMATION_TYPE::CpuSetInformation)
{
// Store Logical Processor Information for Later Use.
LOGICAL_PROCESSOR_INFO core;
core.id = nextCPUSet->CpuSet.Id;
core.group = nextCPUSet->CpuSet.Group;
core.node = nextCPUSet->CpuSet.NumaNodeIndex;
core.logicalProcessorIndex = nextCPUSet->CpuSet.LogicalProcessorIndex;
core.coreIndex = nextCPUSet->CpuSet.CoreIndex;
core.realTime = nextCPUSet->CpuSet.RealTime;
core.parked = nextCPUSet->CpuSet.Parked;
core.allocated = nextCPUSet->CpuSet.Allocated;
core.allocatedToTargetProcess = nextCPUSet->CpuSet.AllocatedToTargetProcess;
core.allocationTag = nextCPUSet->CpuSet.AllocationTag;
core.efficiencyClass = nextCPUSet->CpuSet.EfficiencyClass;
core.schedulingClass = nextCPUSet->CpuSet.SchedulingClass;
procInfo.cores.push_back(core);
procInfo.numLogicalCores++;
// Bin logical processors based on efficiency class here
// ...
}
cpuSetPtr += nextCPUSet->Size;
cpuSetSize += nextCPUSet->Size;
}
HYBRID_DETECT_TRACE(7, "<<<");
return true;
#else
// Based On: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformation?redirectedfrom=MSDN
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION procInfoStart = NULL;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION currProcInfo = NULL;
DWORD length = 0;
while (true)
{
if (!GetLogicalProcessorInformation(procInfoStart, &length))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
//TODO: need hooks for malloc/free
if (procInfoStart)
free(procInfoStart);
procInfoStart = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(length);
if (procInfoStart == NULL)
return false;
}
else
{
return false;
}
}
else
{
break;
}
}
currProcInfo = procInfoStart;
for (DWORD offset = 0; offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= length; offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION), currProcInfo++)
{
switch (currProcInfo->Relationship)
{
case RelationNumaNode:
// Non-NUMA systems report a single record of this type.
procInfo.numNUMANodes++;
break;
case RelationProcessorCore:
// A hyperthreaded core supplies more than one logical processor.
procInfo.numPhysicalCores++;
procInfo.numLogicalCores += std::bitset<64>(currProcInfo->ProcessorMask).count();
break;
case RelationProcessorPackage:
// Logical processors share a physical package.
procInfo.numProcessorPackages++;
break;
default:
break;
}
}
//TODO: need hooks for malloc/free
free(procInfoStart);
return true;
#endif
#else
return false;
#endif
}
#endif // HYBRIDDETECT_OS_WIN
// Calls CPUID & GetLogicalProcessors to fill in PROCESSOR_INFO Caches & Cores
inline bool GetLogicalProcessorsEx(PROCESSOR_INFO& procInfo)
{
HYBRID_DETECT_TRACE(7, ">>>");
#ifdef HYBRIDDETECT_OS_WIN
for (EnumLogicalProcessorInformation enumInfo(RelationGroup);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext()) {
GROUP_INFO group;
procInfo.numGroups++;
group.activeGroupCount = pinfo->Group.ActiveGroupCount;
group.maximumGroupCount = pinfo->Group.MaximumGroupCount;
group.activeProcessorCount = pinfo->Group.GroupInfo->ActiveProcessorCount;
group.maximumProcessorCount = pinfo->Group.GroupInfo->MaximumProcessorCount;
group.activeProcessorMask = (ULONG)pinfo->Group.GroupInfo->ActiveProcessorMask;
HYBRID_DETECT_TRACE(5, "=== group %d: ActiveProcessorCount = %d, MaximumProcessorCount = %d", procInfo.numGroups - 1, pinfo->Group.GroupInfo->ActiveProcessorCount, pinfo->Group.GroupInfo->MaximumProcessorCount);
procInfo.groups.push_back(group);
}
for (EnumLogicalProcessorInformation enumInfo(RelationNumaNode);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext()) {
NUMA_NODE_INFO node;
procInfo.numNUMANodes++;
node.nodeNumber = pinfo->NumaNode.NodeNumber;
node.group = pinfo->NumaNode.GroupMask.Group;
node.mask = (ULONG)pinfo->NumaNode.GroupMask.Mask;
procInfo.nodes.push_back(node);
}
for (EnumLogicalProcessorInformation enumInfo(RelationProcessorPackage);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext()) {
++procInfo.numProcessorPackages;
}
for (EnumLogicalProcessorInformation enumInfo(RelationProcessorCore);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext()) {
procInfo.numPhysicalCores++;
}
for (EnumLogicalProcessorInformation enumInfo(RelationCache);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext()) {
CACHE_INFO cacheInfo;
switch (pinfo->Cache.Level) {
case 1: procInfo.numL1Caches++; break;
case 2: procInfo.numL2Caches++; break;
case 3: procInfo.numL3Caches++; break;
}
cacheInfo.group = pinfo->Cache.GroupMask.Group;
cacheInfo.processorMask = pinfo->Cache.GroupMask.Mask;
cacheInfo.level = pinfo->Cache.Level;
cacheInfo.size = pinfo->Cache.CacheSize;
cacheInfo.lineSize = pinfo->Cache.LineSize;
cacheInfo.associativity = pinfo->Cache.Associativity;
cacheInfo.type = pinfo->Cache.Type;
procInfo.caches.push_back(cacheInfo);
}
HYBRID_DETECT_TRACE(7, "<<<");
return true;
#elif defined(__APPLE__)
// See https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_system_capabilities
int nperfLevels=0;
{
size_t len=sizeof(nperfLevels);
sysctlbyname("hw.nperflevels", &nperfLevels, &len, NULL, 0);
}
{
for (int i=0; i < nperfLevels; ++i)
{
size_t l2cacheSize=0;
size_t len=sizeof(l2cacheSize);
size_t cacheSize=0;
int physicalCPU=0;
int cpusperL2=0;
size_t len4=sizeof(physicalCPU);
std::string perfName = "hw.perflevel" + std::to_string(i);
std::string cacheName = perfName + ".l2cachesize";
int err;
err = sysctlbyname((perfName+".physicalcpu").c_str(), &physicalCPU, &len4, NULL, 0);
err = sysctlbyname((perfName+".cpusperl2").c_str(), &cpusperL2, &len4, NULL, 0);
err = sysctlbyname(cacheName.c_str(), &cacheSize, &len, NULL, 0);
if (!err)
{
l2cacheSize = (physicalCPU / cpusperL2) * cacheSize;
CACHE_INFO cacheInfo{};
cacheInfo.level = 2;
procInfo.numL2Caches++;
cacheInfo.size = (unsigned)l2cacheSize;
procInfo.caches.push_back(cacheInfo);
}
}
}
#if 0
{
std::string tempStr;
tempStr.resize(64);
size_t len=tempStr.capacity();
sysctlbyname("hw.cachesize", tempStr.data(), &len, NULL, 0);
}
#endif
return true;
#else
return false;
#endif
}
inline void UpdateProcessorInfo(PROCESSOR_INFO& procInfo)
{
#ifdef HYBRIDDETECT_OS_WIN
// TODO: Not sure how this works with processor groups
// Query Current Frequency for each Logical Processor using CallNTPowerInformation
// https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation
std::vector<LOGICAL_PROCESSOR_POWER_INFORMATION> pwrInfo(procInfo.numLogicalCores);
DWORD size = sizeof(LOGICAL_PROCESSOR_POWER_INFORMATION) * procInfo.numLogicalCores;
CallNtPowerInformation(ProcessorInformation, nullptr, 0, &pwrInfo[0], size);
for (int i = 0; i < procInfo.cores.size(); i++)
{
procInfo.cores[i].currentFrequency = pwrInfo[i].currentMhz;
procInfo.cores[i].powerInformation = pwrInfo[i];
}
#endif
}
// Calls CPUID & GetLogicalProcessors & CallNTPowerInformation to fill in PROCESSOR_INFO
inline void GetProcessorInfo(PROCESSOR_INFO& procInfo)
{
HYBRID_DETECT_TRACE(7, ">>>");
#ifdef ENABLE_HYBRID_DETECT
// https://software.intel.com/content/www/us/en/develop/download/intel-architecture-instruction-set-extensions-programming-reference.html
// Store registers from CPUID
std::array<unsigned, 4> cpuInfo{}; // zero-init
// Maximum leaf ordinal Reported by CPUID
unsigned CPUIDFunctionMax;
std::bitset<32> bits;
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::ANY, ULONG64(0)));
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::NONE, ULONG64(0)));
#if HYBRIDDETECT_CPU_X86_64
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::RESERVED0, ULONG64(0)));
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::INTEL_ATOM, ULONG64(0)));
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::RESERVED1, ULONG64(0)));
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::INTEL_CORE, ULONG64(0)));
#else
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::PERFLEVEL0, ULONG64(0)));
procInfo.coreMasks.emplace(std::make_pair<short, ULONG64>(CoreTypes::PERFLEVEL1, ULONG64(0)));
#endif
#ifdef ENABLE_CPU_SETS
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::ANY, std::vector<ULONG>()));
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::NONE, std::vector<ULONG>()));
#if HYBRIDDETECT_CPU_X86_64
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::RESERVED0, std::vector<ULONG>()));
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::INTEL_ATOM, std::vector<ULONG>()));
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::RESERVED1, std::vector<ULONG>()));
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::INTEL_CORE, std::vector<ULONG>()));
#else
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::PERFLEVEL0, std::vector<ULONG>()));
procInfo.cpuSets.emplace(std::make_pair((short)CoreTypes::PERFLEVEL1, std::vector<ULONG>()));
#endif
#endif
//Basic CPUID Information
CallCPUID(LEAF_CPUID_BASIC, cpuInfo);
// Store highest function number.
CPUIDFunctionMax = cpuInfo[CPUID_EAX];
#if HYBRIDDETECT_CPU_X86_64
// Read Vendor ID from CPUID
memcpy(procInfo.vendorID + 0, &cpuInfo[CPUID_EBX], sizeof(cpuInfo[CPUID_EBX]));
memcpy(procInfo.vendorID + 4, &cpuInfo[CPUID_EDX], sizeof(cpuInfo[CPUID_EDX]));
memcpy(procInfo.vendorID + 8, &cpuInfo[CPUID_ECX], sizeof(cpuInfo[CPUID_ECX]));
procInfo.vendorID[12] = '\0';
#else
static const char kVendorApple[] = "Apple";
strcpy(procInfo.vendorID, kVendorApple);
procInfo.brandString[0] = 0;
#endif
CallCPUID(1, cpuInfo);
procInfo.cpuid_1_eax = cpuInfo[CPUID_EAX];
bits = cpuInfo[CPUID_ECX];
procInfo.flags.SSE3 = bits[0];
procInfo.flags.PCLMULQDQ = bits[1];
procInfo.flags.SSSE3 = bits[9];
procInfo.flags.FMA = bits[12];
procInfo.flags.SSE4_1 = bits[19];
procInfo.flags.SSE4_2 = bits[20];
procInfo.flags.AES = bits[25];
procInfo.flags.XSAVE = bits[26];
procInfo.flags.OSXSAVE = bits[27];
procInfo.flags.AVX = bits[28];
procInfo.flags.F16C = bits[29];
procInfo.flags.RDRAND = bits[30];
if (procInfo.flags.OSXSAVE)
{
uint64_t xcr0 = XGETBV(0);
if (xcr0 & 0x6)
{
procInfo.flags.OS_Supports_YMM = 1;
if (xcr0 & 0xe0)
{
procInfo.flags.OS_Supports_ZMM = 1;
}
}
}
CallCPUID(LEAF_EXTENDED_FEATURE_FLAGS, cpuInfo, 0, CPUIDFunctionMax);
{
bits = cpuInfo[CPUID_EBX];
procInfo.flags.AVX2 = bits[5];
procInfo.flags.AVX512F = bits[16];
procInfo.flags.AVX512DQ = bits[17];
procInfo.flags.AVX512_IFMA = bits[21];
procInfo.flags.AVX512CD = bits[28];
procInfo.flags.AVX512BW = bits[30];
procInfo.flags.AVX512VL = bits[31];
}
#if HYBRIDDETECT_CPU_X86_64
// Read Brand String from Extended CPUID information
CallCPUID(LEAF_EXTENDED_BRAND_STRING_1, cpuInfo);
memcpy(procInfo.brandString + 00, cpuInfo.data(), sizeof(cpuInfo));
CallCPUID(LEAF_EXTENDED_BRAND_STRING_2, cpuInfo);
memcpy(procInfo.brandString + 16, cpuInfo.data(), sizeof(cpuInfo));
CallCPUID(LEAF_EXTENDED_BRAND_STRING_3, cpuInfo);
memcpy(procInfo.brandString + 32, cpuInfo.data(), sizeof(cpuInfo));
#elif defined(__APPLE__)
{
size_t len = sizeof(procInfo.brandString);
sysctlbyname("machdep.cpu.brand_string", &procInfo.brandString, &len, NULL, 0);
}
#endif
// Structured Extended Feature Flags Enumeration Leaf
// (Output depends on ECX input value)
CallCPUID(LEAF_EXTENDED_FEATURE_FLAGS, cpuInfo);
{
bits = cpuInfo[CPUID_EDX];
#ifndef ENABLE_SOFTWARE_PROXY
procInfo.hybrid = bits[15];
#else
procInfo.hybrid = true;
#endif
}
// Thermal and Power Management Leaf
CallCPUID(LEAF_THERMAL_POWER, cpuInfo);
{
bits = cpuInfo[CPUID_EAX];
procInfo.turboBoost = bits[1];
procInfo.turboBoost3_0 = bits[14];
}
#ifdef HYBRIDDETECT_OS_WIN
DWORD_PTR affinityMask;
DWORD_PTR processAffinityMask;
DWORD_PTR sysAffinityMask;
// What does the process & system allow
GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &sysAffinityMask);
#endif
GetLogicalProcessorsEx(procInfo);
#ifdef HYBRIDDETECT_OS_WIN
// Fill in Logical Processors, short circuit if error.
if (GetLogicalProcessors(procInfo))
{
HYBRID_DETECT_TRACE(5, "=== procInfo.numLogicalCores = %d, procInfo.numGroups = %d", procInfo.numLogicalCores, procInfo.numGroups);
// Query Current Frequency for each Logical Processor using CallNTPowerInformation
// https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation
std::vector<LOGICAL_PROCESSOR_POWER_INFORMATION> pwrInfo(procInfo.numLogicalCores);
DWORD size = sizeof(LOGICAL_PROCESSOR_POWER_INFORMATION) * procInfo.numLogicalCores;
CallNtPowerInformation(ProcessorInformation, nullptr, 0, &pwrInfo[0], size);
for (unsigned group = 0; group < procInfo.numGroups; group++)
{
// TODO Use Thread Group Affinity https://docs.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-setthreadgroupaffinity
GROUP_AFFINITY nextGroup;
GROUP_AFFINITY prevGroup;
nextGroup.Group = static_cast<WORD>(group);
nextGroup.Mask = 0xffffffff;
SetThreadGroupAffinity(GetCurrentThread(), &nextGroup, &prevGroup);
HYBRID_DETECT_TRACE(5, "=== group = %d, procInfo.groups[group].maximumProcessorCount = %d, procInfo.groups[group].activeProcessorCount = %d", group,
procInfo.groups[group].maximumProcessorCount, procInfo.groups[group].activeProcessorCount);
// Enumerate each logical core. Need active or maximum processor count?
for (unsigned core = 0; core < procInfo.groups[group].activeProcessorCount; core++)
{
HYBRID_DETECT_TRACE(5, "=== core = %d", core);
// Logical Processor Info struct for storage.
#ifdef ENABLE_CPU_SETS
LOGICAL_PROCESSOR_INFO& logicalCore = procInfo.cores[core];
#else
LOGICAL_PROCESSOR_INFO logicalCore;
logicalCore.logicalProcessorIndex = core;
#endif
// Get the ID of the current processor;
//int prevProcessor = GetCurrentProcessorNumber();
// Convert the oridinal position to an affinity mask.
affinityMask = (DWORD)IndexToMask(core);
// Thread Affinity Mask is enough to switch to current thread immediatlely.
SetThreadAffinityMask(GetCurrentThread(), processAffinityMask & affinityMask);
logicalCore.processorMask = std::bitset<64>(affinityMask);