Repository navigation
Expand file tree
/
Copy pathpool.cpp
More file actions
1476 lines (1402 loc) · 71.4 KB
/
Copy pathpool.cpp
File metadata and controls
1476 lines (1402 loc) · 71.4 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
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
// src/kernels/cpu/pool.cpp - P2.S3: the CPU expert pool. Read pool.hpp first; it explains the protocol.
#include "strata/kernels/cpu/pool.hpp"
#include "strata/core/progress.hpp"
#include "strata/platform/aux_cpus.hpp"
#include "strata/kernels/cpu/expert_layout.hpp"
#include "strata/kernels/cpu/numa_pool.hpp"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <chrono>
#include "strata/platform/cpu_relax.hpp"
#if defined(_MSC_VER)
#include <intrin.h> // __cpuidex, __rdtsc (STRATA_POOL_UMWAIT)
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <string>
#include <thread>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "pool_affinity_win.hpp"
#else
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include "pool_affinity_linux.hpp"
#endif
namespace strata::kernels::cpu {
namespace {
// STRATA_POOL_UMWAIT=<TSC cycles> (experimental, x86 WAITPKG only): a parked worker waits with UMONITOR/UMWAIT on the
// epoch's cache line instead of spinning on _mm_pause. It wakes when the host publishes (a write to that line) or
// after the cycles, in the light C0.1 state; on a laptop the CPU then gives back the power the spin took, which the
// GPU's share of the budget gets. 0 or unset (or no WAITPKG): the spin, unchanged. STRATA_POOL_UMWAIT_C02=1: the
// deeper C0.2 state (slower wake). MSVC x64 only for now (the intrinsics need no target flag there); other builds spin.
uint64_t umwait_cycles() {
static const uint64_t c = [] {
const char* e = std::getenv("STRATA_POOL_UMWAIT");
if (e == nullptr || std::atoll(e) <= 0) return (uint64_t) 0;
#if defined(_MSC_VER) && defined(_M_X64) && !defined(__clang__) // MSVC proper: clang-cl (the Windows HIP build) has no WAITPKG intrinsics by default
int r[4] = {0, 0, 0, 0};
__cpuidex(r, 7, 0);
if (((r[2] >> 5) & 1) == 0) {
std::fprintf(stderr, "strata: STRATA_POOL_UMWAIT: this CPU has no WAITPKG, the workers spin\n");
return (uint64_t) 0;
}
std::fprintf(stderr, "strata: STRATA_POOL_UMWAIT: parked workers wait with UMWAIT (%lld cycles)\n", std::atoll(e));
return (uint64_t) std::atoll(e);
#else
return (uint64_t) 0;
#endif
}();
return c;
}
unsigned umwait_state() {
static const unsigned s = [] { const char* e = std::getenv("STRATA_POOL_UMWAIT_C02"); return e && *e == '1' ? 0u : 1u; }();
return s; // 1 = C0.1 (light), 0 = C0.2
}
// STRATA_POOL_UMWAIT_ESC=<TSC cycles> (experimental): an escalating wait - C0.1 for the first cycles of a park, C0.2 after
// (a wait that has lasted that long is a GPU phase, a draft or a prompt read, where a slower wake costs little).
uint64_t umwait_escalate() {
static const uint64_t c = [] { const char* e = std::getenv("STRATA_POOL_UMWAIT_ESC"); return e ? (uint64_t) (std::max)(0LL, std::atoll(e)) : (uint64_t) 0; }();
return c;
}
constexpr uint64_t pack_head(uint32_t epoch, uint32_t n, uint32_t i) {
return ((uint64_t) epoch << 32) | ((uint64_t) n << 16) | (uint64_t) i;
}
} // namespace
namespace {
std::atomic<int> g_host_core{(int) HostCore::First};
} // namespace
void set_host_core(HostCore where) { g_host_core.store((int) where); }
HostCore host_core_setting() { return (HostCore) g_host_core.load(); }
namespace {
std::vector<int> g_pool_cores; // --pool-cores: set once at start-up, before the pool exists
} // namespace
void set_pool_cores(const std::vector<int>& cpus) { g_pool_cores = cpus; }
const std::vector<int>& pool_cores_setting() { return g_pool_cores; }
std::vector<LogicalCpuInfo> logical_cpu_layout() {
std::vector<LogicalCpuInfo> out;
#if defined(_WIN32)
DWORD len = 0;
GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len);
if (len == 0) return out;
std::vector<char> buf(len);
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), &len))
return out;
// a `start /affinity` or a job limit on a PC with one processor group: only those processors are the process's
DWORD_PTR proc_mask = 0, sys_mask = 0;
const bool limited = GetActiveProcessorGroupCount() == 1 && GetProcessAffinityMask(GetCurrentProcess(), &proc_mask, &sys_mask);
long core = 0;
for (const char* p = buf.data(); p < buf.data() + len; p += ((const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p)->Size) {
const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p;
if (e->Relationship != RelationProcessorCore) continue;
for (WORD group = 0; group < e->Processor.GroupCount; ++group) {
const GROUP_AFFINITY& g = e->Processor.GroupMask[group];
for (int bit = 0; bit < 64; ++bit)
if ((g.Mask & (KAFFINITY(1) << bit)) && (!limited || (proc_mask & (DWORD_PTR(1) << bit))))
out.push_back({(int) (g.Group * 64 + bit), core});
}
++core;
}
#elif defined(__linux__)
std::vector<unsigned long> mask;
std::vector<int> allowed;
if (detail::get_thread_affinity(mask, &allowed) != 0) return out;
auto read = [](int cpu, const char* what) -> long {
char path[96];
std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what);
long v = -1;
if (std::FILE* f = std::fopen(path, "r")) {
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
}
return v;
};
for (int cpu : allowed) {
const long pkg = read(cpu, "physical_package_id"), core = read(cpu, "core_id");
out.push_back({cpu, pkg >= 0 && core >= 0 ? pkg * 1000000 + core : -1});
}
#else
for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) out.push_back({(int) i, -1});
#endif
return out;
}
static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affinity);
// The fields the default worker count needs (pool_plan.hpp): the logical processors the process may use, the CPU
// package of the host core and of every worker core, whether the OS told physical cores from SMT siblings, the
// container CPU quota. On a multi-socket CPU (not hybrid) the host package comes first in `worker_cores`.
static void fill_plan_fields(CpuTopology& topo, bool skip_first) {
topo.cpu_quota = container_cpu_quota();
topo.worker_package.assign(topo.worker_cores.size(), -1);
#if defined(_WIN32)
(void) skip_first;
topo.logical = (int) GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
return; // (the sockets of a Windows box are not told apart: one package)
#elif defined(__APPLE__)
(void) skip_first;
topo.logical = (int) std::thread::hardware_concurrency();
return; // no SMT on Apple silicon and no sysfs: the cores are the processors
#else
std::vector<unsigned long> mask;
std::vector<int> allowed;
topo.logical = detail::get_thread_affinity(mask, &allowed) == 0 ? (int) allowed.size()
: (int) std::thread::hardware_concurrency();
auto read = [](int cpu, const char* what) -> long {
char path[96];
std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what);
long v = -1;
if (std::FILE* f = std::fopen(path, "r")) {
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
}
return v;
};
bool known = !topo.worker_cores.empty() || topo.host_core >= 0;
auto pkg_of = [&](int cpu) {
const long pkg = read(cpu, "physical_package_id"), core = read(cpu, "core_id");
if (pkg < 0 || core < 0) known = false;
return (int) pkg;
};
if (topo.host_core >= 0) topo.host_package = pkg_of(topo.host_core);
for (size_t i = 0; i < topo.worker_cores.size(); ++i) topo.worker_package[i] = pkg_of(topo.worker_cores[i]);
topo.layout_known = known;
if (!known) return;
if (skip_first && !topo.is_hybrid && topo.host_core >= 0 && topo.host_package >= 0) {
// the host socket first (stable inside each socket): the default workers are the local ones
std::vector<size_t> order(topo.worker_cores.size());
for (size_t i = 0; i < order.size(); ++i) order[i] = i;
std::stable_partition(order.begin(), order.end(),
[&](size_t i) { return topo.worker_package[i] == topo.host_package; });
std::vector<int> cores, pkgs;
for (size_t i : order) { cores.push_back(topo.worker_cores[i]); pkgs.push_back(topo.worker_package[i]); }
topo.worker_cores = std::move(cores);
topo.worker_package = std::move(pkgs);
}
#endif
}
CpuTopology detect_cpu_topology(bool skip_first, PoolAffinity affinity) {
CpuTopology topo = detect_cpu_topology_impl(skip_first, affinity);
// --host-core last: the host takes the last core, the workers the others (the first included)
if (skip_first && host_core_setting() == HostCore::Last && !topo.is_hybrid && topo.host_core >= 0 &&
!topo.worker_cores.empty()) {
const int last = topo.worker_cores.back();
topo.worker_cores.pop_back();
topo.worker_cores.insert(topo.worker_cores.begin(), topo.host_core);
topo.host_core = last;
}
// --host-core sibling: the host on its core's other hardware thread; the first logical processor stays to the
// interrupts and to nothing else (a worker on it would share the host's core)
if (skip_first && host_core_setting() == HostCore::Sibling && topo.host_sibling >= 0) {
topo.worker_cores.erase(std::remove(topo.worker_cores.begin(), topo.worker_cores.end(), topo.host_sibling),
topo.worker_cores.end());
topo.host_core = topo.host_sibling;
}
fill_plan_fields(topo, skip_first);
return topo;
}
double container_cpu_quota() {
#if defined(__linux__)
// cgroup v2: "max 100000" (no limit) or "<quota> <period>"; v1: cpu.cfs_quota_us (-1: none) / cpu.cfs_period_us
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu.max", "r")) {
char a[32] = {0};
long period = 0;
double q = 0;
if (std::fscanf(f, "%31s %ld", a, &period) == 2 && std::strcmp(a, "max") != 0 && period > 0)
q = std::atof(a) / (double) period;
std::fclose(f);
return q > 0 ? q : 0.0;
}
long quota = -1, period = 0;
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r")) {
if (std::fscanf(f, "%ld", "a) != 1) quota = -1;
std::fclose(f);
}
if (std::FILE* f = std::fopen("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r")) {
if (std::fscanf(f, "%ld", &period) != 1) period = 0;
std::fclose(f);
}
return quota > 0 && period > 0 ? (double) quota / (double) period : 0.0;
#else
return 0.0;
#endif
}
PoolPlanInput pool_plan_input(const CpuTopology& topo) {
PoolPlanInput in;
in.logical = topo.logical > 0 ? topo.logical : (int) std::thread::hardware_concurrency();
in.cpu_quota = topo.cpu_quota;
if (topo.is_hybrid) { in.p_cores = topo.p_cores; in.e_cores = topo.e_cores; }
if (!topo.layout_known) return in; // cores_per_package stays empty
// the cores per package, the host package first; the host core counts in its package
std::vector<std::pair<int, int>> counts; // (package, cores)
auto add = [&](int pkg) {
for (auto& c : counts)
if (c.first == pkg) { ++c.second; return; }
counts.emplace_back(pkg, 1);
};
if (topo.host_core >= 0) add(topo.host_package);
for (size_t i = 0; i < topo.worker_cores.size(); ++i)
add(i < topo.worker_package.size() ? topo.worker_package[i] : -1);
if (topo.host_core >= 0) { // (the host package first even when it has no worker core listed)
auto it = std::find_if(counts.begin(), counts.end(), [&](const auto& c) { return c.first == topo.host_package; });
if (it != counts.begin()) std::iter_swap(counts.begin(), it);
}
for (const auto& c : counts) in.cores_per_package.push_back(c.second);
return in;
}
int planned_host_core(PoolAffinity affinity) { return detect_cpu_topology(true, affinity).host_core; }
static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affinity) {
CpuTopology topo;
#if defined(_WIN32)
// Ask the OS rather than assuming a layout. `hardware_concurrency()` returns LOGICAL processors, and on
// every SMT machine half of them are siblings - pinning one worker to each of the first N would put two
// workers on each physical core and halve the bandwidth the expert kernel is bound by.
DWORD len = 0;
GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len);
if (len == 0) {
// Preserve group identity even when detailed core topology is unavailable.
const WORD groups = GetActiveProcessorGroupCount();
for (WORD group = 0; group < groups; ++group) {
const DWORD count = GetActiveProcessorCount(group);
if (count == 0 || count == (DWORD) -1 || count > 64) continue;
for (DWORD i = 0; i < count; ++i) topo.worker_cores.push_back((int) group * 64 + (int) i);
}
if (skip_first && !topo.worker_cores.empty()) {
topo.host_core = topo.worker_cores.front();
topo.worker_cores.erase(topo.worker_cores.begin());
}
// The OS did not say which logical processors share a core, and every one is listed as a core above: the default
// worker count must not count them as cores (30 workers on a 16c/32t PC would sit two to a core). With the layout
// unknown the plan takes half the logical processors (pool_plan.hpp), as on Linux without sysfs.
topo.layout_known = false;
return topo;
}
std::vector<char> buf(len);
if (GetLogicalProcessorInformationEx(RelationProcessorCore,
(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), &len)) {
const char* p = buf.data();
const char* end = p + len;
struct CoreDesc {
uint8_t efficiency = 0;
bool has_smt = false;
std::vector<int> lps;
};
std::vector<CoreDesc> descs;
while (p < end) {
const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p;
if (e->Relationship == RelationProcessorCore) {
CoreDesc cd;
cd.efficiency = e->Processor.EfficiencyClass;
cd.has_smt = (e->Processor.Flags & LTP_PC_SMT) != 0;
for (WORD group = 0; group < e->Processor.GroupCount; ++group) {
const GROUP_AFFINITY& g = e->Processor.GroupMask[group];
for (int bit = 0; bit < 64; ++bit) {
if (g.Mask & (KAFFINITY(1) << bit)) {
cd.lps.push_back((int) (g.Group * 64 + bit));
}
}
}
if (!cd.lps.empty()) {
descs.push_back(std::move(cd));
}
}
p += e->Size;
}
uint8_t min_eff = 255, max_eff = 0;
for (const auto& c : descs) {
min_eff = (std::min)(min_eff, c.efficiency);
max_eff = (std::max)(max_eff, c.efficiency);
}
topo.is_hybrid = (max_eff > min_eff);
if (topo.is_hybrid) {
for (const auto& c : descs) {
if (c.efficiency == max_eff) {
topo.p_cores++;
topo.p_threads += (int) c.lps.size();
} else {
topo.e_cores++;
}
}
} else {
topo.p_cores = (int) descs.size();
for (const auto& c : descs) topo.p_threads += (int) c.lps.size();
}
if (affinity == PoolAffinity::All || !topo.is_hybrid) {
// #642 (from Hardin22's fork): on a hybrid CPU the P-cores first (the host takes the first of them), so a
// pool smaller than the core count (setup's --pool-workers for a hybrid CPU) runs on the P-cores and the
// first E-cores rather than on whatever the OS numbered first. All cores alike: the order is unchanged.
if (topo.is_hybrid)
std::stable_sort(descs.begin(), descs.end(),
[](const CoreDesc& x, const CoreDesc& y) { return x.efficiency > y.efficiency; });
for (const auto& c : descs) topo.worker_cores.push_back(c.lps[0]);
if (skip_first && !topo.worker_cores.empty()) {
topo.host_core = topo.worker_cores.front();
topo.worker_cores.erase(topo.worker_cores.begin());
if (descs.front().lps.size() > 1) topo.host_sibling = descs.front().lps[1];
}
return topo;
}
// Hybrid CPU with Auto or PCores affinity:
// Prioritize Performance cores:
// 1. Primary logical processor of each P-core (avoids SMT resource contention)
// 2. SMT sibling logical processors of P-cores
// 3. E-cores (only as overflow in Auto mode)
std::vector<int> p_primaries;
std::vector<int> p_siblings;
std::vector<int> e_cores;
for (const auto& c : descs) {
if (c.efficiency == max_eff) {
p_primaries.push_back(c.lps[0]);
for (size_t s = 1; s < c.lps.size(); ++s) {
p_siblings.push_back(c.lps[s]);
}
} else {
for (int lp : c.lps) e_cores.push_back(lp);
}
}
if (skip_first && !p_primaries.empty()) {
topo.host_core = p_primaries.front();
p_primaries.erase(p_primaries.begin());
for (const auto& c : descs)
if (c.lps[0] == topo.host_core) {
if (c.lps.size() > 1) topo.host_sibling = c.lps[1];
break;
}
}
for (int cpu : p_primaries) topo.worker_cores.push_back(cpu);
for (int cpu : p_siblings) topo.worker_cores.push_back(cpu);
if (affinity != PoolAffinity::PCores) {
for (int cpu : e_cores) topo.worker_cores.push_back(cpu);
}
return topo;
}
#else
// The logical CPUs this process may run on, ONE PER PHYSICAL CORE (issue #40): SMT siblings share a core's
// load/store bandwidth, so a worker on each would put two workers on one core, as the Windows branch above
// explains. sysfs names each CPU's (package, core); the first allowed CPU of each pair is kept, so a taskset
// that leaves out the first sibling still gets its core. Without sysfs every allowed CPU counts, as before.
auto topo_read = [](int cpu, const char* what) -> long {
char path[96];
std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what);
long v = -1;
if (std::FILE* f = std::fopen(path, "r")) {
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
}
return v;
};
auto cap_read = [](int cpu) -> long {
char path[96];
std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/cpu_capacity", cpu);
long v = -1;
if (std::FILE* f = std::fopen(path, "r")) {
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
}
return v;
};
// #798: a cpulist ("0-7,16") of one of the hybrid PMU's CPU sets: /sys/devices/cpu_core/cpus and cpu_atom/cpus
auto cpulist_read = [](const char* path, std::vector<int>& out) -> bool {
std::FILE* f = std::fopen(path, "r");
if (!f) return false;
char text[1024] = {0};
const size_t n = std::fread(text, 1, sizeof text - 1, f);
std::fclose(f);
text[n] = '\0';
for (const char* p = text; *p;) {
char* end = nullptr;
const long lo = std::strtol(p, &end, 10);
if (end == p) break;
long hi = lo;
if (*end == '-') {
p = end + 1;
hi = std::strtol(p, &end, 10);
}
for (long c = lo; c <= hi && c < 4096; ++c) out.push_back((int) c);
p = end;
while (*p == ',' || *p == ' ' || *p == '\n') ++p;
}
return !out.empty();
};
std::vector<int> allowed;
std::vector<unsigned long> allowed_mask;
if (detail::get_thread_affinity(allowed_mask, &allowed) != 0) {
for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) allowed.push_back((int) i);
}
struct CoreLinux {
int cpu = -1;
long pkg = -1;
long core = -1;
long cap = -1;
bool is_sibling = false;
};
std::vector<CoreLinux> all_cpus;
std::vector<std::pair<long, long>> seen_phys;
long max_cap = 0, min_cap = 1000000;
for (int cpu : allowed) {
CoreLinux cl;
cl.cpu = cpu;
cl.pkg = topo_read(cpu, "physical_package_id");
cl.core = topo_read(cpu, "core_id");
cl.cap = cap_read(cpu);
if (cl.cap > 0) {
max_cap = (std::max)(max_cap, cl.cap);
min_cap = (std::min)(min_cap, cl.cap);
}
if (cl.pkg >= 0 && cl.core >= 0) {
const std::pair<long, long> key{cl.pkg, cl.core};
if (std::find(seen_phys.begin(), seen_phys.end(), key) != seen_phys.end()) {
cl.is_sibling = true;
} else {
seen_phys.push_back(key);
}
}
all_cpus.push_back(cl);
}
// #798: which CPUs are E-cores. Intel's hybrid PMU lists them (cpu_atom/cpus beside cpu_core/cpus); without it a
// CPU is an E-core when its capacity is under 90% of the largest. Turbo Boost Max 3.0 gives the "favored"
// P-cores a slightly higher capacity (1024 against 1012 on a Core Ultra 7 270K Plus): "capacity == the maximum"
// counted 2 of its 8 P-cores and started 1 pool worker.
// #3262: Meteor Lake and Arrow Lake-H list their LP-E cores (SoC tile, no L3, ~2.5 GHz) in a third set,
// /sys/devices/cpu_lowpower/cpus, that is in neither of the other two; they are E-cores too (and sort last).
std::vector<int> pmu_core, pmu_atom, pmu_lowpower;
const bool pmu = cpulist_read("/sys/devices/cpu_core/cpus", pmu_core) &&
cpulist_read("/sys/devices/cpu_atom/cpus", pmu_atom);
cpulist_read("/sys/devices/cpu_lowpower/cpus", pmu_lowpower);
auto is_e = [&](const CoreLinux& cl) {
return hybrid_cpu_is_efficiency(cl.cpu, cl.cap, max_cap, pmu, pmu_core, pmu_atom, pmu_lowpower);
};
bool any_e = false, any_p = false;
for (const auto& cl : all_cpus) (is_e(cl) ? any_e : any_p) = true;
topo.is_hybrid = any_e && any_p;
if (topo.is_hybrid) {
for (const auto& cl : all_cpus) {
if (!is_e(cl)) {
if (!cl.is_sibling) topo.p_cores++;
topo.p_threads++;
} else {
if (!cl.is_sibling) topo.e_cores++;
}
}
} else {
topo.p_cores = (int) seen_phys.size();
topo.p_threads = (int) all_cpus.size();
}
// the host core's SMT sibling, an allowed CPU of the same (package, core) (--host-core sibling)
auto sibling_of = [&](int cpu) {
for (const auto& h : all_cpus)
if (h.cpu == cpu && h.pkg >= 0 && h.core >= 0)
for (const auto& cl : all_cpus)
if (cl.cpu != cpu && cl.pkg == h.pkg && cl.core == h.core) return cl.cpu;
return -1;
};
if (affinity == PoolAffinity::All || !topo.is_hybrid) {
if (topo.is_hybrid) // #642: the P-cores first (see the Windows branch)
std::stable_sort(all_cpus.begin(), all_cpus.end(),
[&](const CoreLinux& x, const CoreLinux& y) {
return is_e(x) != is_e(y) ? !is_e(x) : x.cap > y.cap;
});
for (const auto& cl : all_cpus) {
if (!cl.is_sibling) topo.worker_cores.push_back(cl.cpu);
}
if (skip_first && !topo.worker_cores.empty()) {
topo.host_core = topo.worker_cores.front();
topo.worker_cores.erase(topo.worker_cores.begin());
topo.host_sibling = sibling_of(topo.host_core);
}
return topo;
}
// Hybrid CPU on Linux:
std::vector<int> p_primaries;
std::vector<int> p_siblings;
std::vector<int> e_cores;
for (const auto& cl : all_cpus) {
if (!is_e(cl)) {
if (!cl.is_sibling) p_primaries.push_back(cl.cpu);
else p_siblings.push_back(cl.cpu);
} else {
e_cores.push_back(cl.cpu);
}
}
if (skip_first && !p_primaries.empty()) {
topo.host_core = p_primaries.front();
p_primaries.erase(p_primaries.begin());
topo.host_sibling = sibling_of(topo.host_core);
}
for (int cpu : p_primaries) topo.worker_cores.push_back(cpu);
for (int cpu : p_siblings) topo.worker_cores.push_back(cpu);
if (affinity != PoolAffinity::PCores) {
for (int cpu : e_cores) topo.worker_cores.push_back(cpu);
}
return topo;
#endif
return topo;
}
std::vector<int> physical_cores(bool skip_first, PoolAffinity affinity) {
return detect_cpu_topology(skip_first, affinity).worker_cores;
}
namespace {
bool pin_this_thread(int core, [[maybe_unused]] int worker = -1) {
if (core < 0) return false;
#if defined(_WIN32)
return detail::set_thread_group_affinity(core, worker);
#else
const int error = detail::pin_thread_to_cpu(core);
if (error != 0)
std::fprintf(stderr, "strata cpu pool: affinity for worker %d (CPU %d) failed: %d; previous affinity kept\n",
worker, core, error);
return error == 0;
#endif
}
#if defined(__linux__)
// Where the NUMA topology is read from: /sys. STRATA_TEST_SYSFS_ROOT=<dir> reads <dir>/devices/system/{node,cpu} instead,
// so a test can give the pool a made-up two-node machine (pool_tasks_test); nobody sets it for real.
std::string sysfs_root() {
const char* e = std::getenv("STRATA_TEST_SYSFS_ROOT");
return e != nullptr && *e != '\0' ? std::string(e) : std::string("/sys");
}
// The NUMA node of a logical CPU (sysfs), or -1.
int cpu_numa_node(int cpu) {
if (cpu < 0) return -1;
const std::string root = sysfs_root();
char path[512];
for (int node = 0; node < 64; ++node) {
std::snprintf(path, sizeof path, "%s/devices/system/cpu/cpu%d/node%d", root.c_str(), cpu, node);
if (access(path, F_OK) == 0) return node;
}
return -1;
}
#endif
// The NUMA nodes with CPUs this machine has (0.2.2: the split is tried by itself from 2 on). Linux: the sysfs node folder
// (numa_pool.hpp); Windows: GetNumaHighestNodeNumber; any other system, or a sandbox that hides the folder: 1.
int numa_node_count() {
#if defined(__linux__)
return (std::max)(1, numa_cpu_node_count_at(sysfs_root() + "/devices/system/node"));
#elif defined(_WIN32)
ULONG highest = 0;
return GetNumaHighestNodeNumber(&highest) ? (int) highest + 1 : 1;
#else
return 1;
#endif
}
// The real-node split for threads on `thread_core` (numa_pool.hpp). Linux only: the placement of the experts' memory
// (first touch by threads pinned to a node, move_pages to check) has no Windows or macOS version.
NumaSplitDecision decide_real_numa_split([[maybe_unused]] const std::vector<int>& thread_core, [[maybe_unused]] int n_workers,
[[maybe_unused]] bool pinned) {
#if defined(__linux__)
// the expert source's own reading of it (expert_source.cpp): exactly "1"
const char* rotate = std::getenv("STRATA_EXCHANGE_ROTATE");
return decide_numa_split(thread_core, n_workers, pinned, rotate != nullptr && std::strcmp(rotate, "1") == 0, cpu_numa_node);
#else
NumaSplitDecision d;
d.why = "the split needs Linux";
return d;
#endif
}
} // namespace
ThreadAffinity pin_current_thread(int core) {
if (core < 0) return {};
#if defined(_WIN32)
ThreadAffinity previous;
ULONG target = 0;
if (!detail::get_thread_cpu_sets(previous.cpu_sets) || !detail::cpu_set_for_core(core, target) ||
!SetThreadSelectedCpuSets(GetCurrentThread(), &target, 1)) {
std::fprintf(stderr, "strata cpu pool: host CPU Set selection for processor %d failed: %lu; previous placement kept\n",
core, (unsigned long) GetLastError());
return {};
}
previous.valid = true;
return previous;
#else
ThreadAffinity previous;
int error = detail::get_thread_affinity(previous.mask);
if (error == 0) error = detail::pin_thread_to_cpu(core);
if (error != 0) {
std::fprintf(stderr, "strata cpu pool: host affinity for CPU %d failed: %d; previous affinity kept\n", core, error);
return {};
}
previous.valid = true;
return previous;
#endif
}
void restore_thread_affinity(const ThreadAffinity& previous) {
if (!previous.valid) return;
#if defined(_WIN32)
// Clearing an originally empty selection restores process-default/all-group eligibility without
// turning the caller's implicit Windows 11 affinity into an explicit single-group hard mask.
if (!SetThreadSelectedCpuSets(GetCurrentThread(), previous.cpu_sets.empty() ? nullptr : previous.cpu_sets.data(),
(ULONG) previous.cpu_sets.size()))
std::fprintf(stderr, "strata cpu pool: host CPU Set restoration failed: %lu\n",
(unsigned long) GetLastError());
#else
const int error = detail::set_thread_affinity(previous.mask);
if (error != 0)
std::fprintf(stderr, "strata cpu pool: host affinity restoration failed: %d\n", error);
#endif
}
namespace {
std::atomic<const ExpertPool*> g_diag_pool{nullptr};
void diag_active_pool(std::FILE* f) {
if (const ExpertPool* p = g_diag_pool.load()) p->diag(f);
}
int64_t now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
.count();
}
int64_t now_ns() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now().time_since_epoch())
.count();
}
// CBA: one plain global, written by the constructor and destructor of the pool that owns it; fine while pools are
// built and destroyed on one thread (generate's measure pools, then the main one), an atomic owner if that changes.
NumaSplitPlan g_numa_plan;
const ExpertPool* g_numa_owner = nullptr;
} // namespace
const NumaSplitPlan& active_numa_split() { return g_numa_plan; }
void ExpertPool::diag(std::FILE* f) const {
const uint64_t h = head_.load();
uint32_t batch = (uint32_t) (h >> 32), claimed = (uint32_t) h & 0xffffu, total = (uint32_t) (h >> 16) & 0xffffu,
done = done_.load();
// CBA: this summed-slices branch is never run by a test (it needs a diag call mid-phase), it is a trivial sum, a mid-phase
// diag test if it ever misreports
if (numa_n_ > 0 && mode_ >= 5) { // a split native batch never writes head_/done_: the slices' sums instead
claimed = total = done = 0;
for (int k = 0; k < numa_n_; ++k) {
const uint64_t sh = nsync_[(size_t) k].head.load();
batch = (uint32_t) (sh >> 32); // every slice head carries the batch's epoch
claimed += (uint32_t) sh & 0xffffu;
total += (uint32_t) (sh >> 16) & 0xffffu;
done += nsync_[(size_t) k].done.load();
}
}
std::fprintf(f, " expert pool: epoch %u, batch epoch %u: %u of %u jobs claimed, %u done; %u of %d workers parked, "
"%u sleeping; mode %d\n", epoch_.load(), batch, claimed, total, done, parked_count(), n_,
sleepers_.load(), mode_);
if (numa_n_ > 0) std::fprintf(f, " expert pool NUMA slices:%s\n", numa_counters().c_str());
std::fprintf(f, " expert pool threads:");
for (int i = 0; i < n_; ++i) {
const int32_t s = wstate_[(size_t) i].load();
if (s == kParked) std::fprintf(f, " w%d=parked", i);
else if (s == kSleeping) std::fprintf(f, " w%d=sleeping", i);
else if (s == kBetween) std::fprintf(f, " w%d=draining", i);
else std::fprintf(f, " w%d=job%d", i, s);
}
const int32_t hs = hstate_.load();
const char* hn = hs == kIdle ? "idle" : hs == kWaitParked ? "waiting for the workers to park"
: hs == kWaitDone ? "waiting for the jobs to finish" : "running a job";
std::fprintf(f, "; host %s", hn);
if (hs >= 0) std::fprintf(f, " %d", hs);
std::fprintf(f, " for %lld ms\n", (long long) (now_ms() - hstate_ms_.load()));
}
ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works, PoolAffinity affinity, int tasks)
: tasks_(tasks), host_works_(host_works), affinity_(affinity), topo_(detect_cpu_topology(true, affinity)) {
if (tasks < 0 || tasks > kMaxTasks) throw std::invalid_argument("expert pool tasks must be in 0..4096");
pinned_ = pin;
layout_ = logical_cpu_layout(); // now, before anything pins the caller (Linux reads the calling thread's allowed CPUs)
if (const char* e = std::getenv("STRATA_POOL_SPIN_US")) // a test knob; see kSpinBeforeSleep
spin_before_sleep_ = std::chrono::microseconds((std::max)(0, std::atoi(e)));
// STRATA_NUMA_SPLIT, read here for the worker count (the split itself is decided below, once the count is known).
// 0.2.2: with 2 or more NUMA nodes and neither variable set the split is tried by itself, and STRATA_NUMA_POOL=0 turns that
// off (numa_pool.hpp). One node: pick_numa_pool says Unchanged and nothing below differs from the pool before the split.
const char* env = std::getenv("STRATA_NUMA_SPLIT");
const int numa_nodes = numa_node_count();
const NumaPoolPick pool_pick = pick_numa_pool(std::getenv("STRATA_NUMA_POOL"), env, numa_nodes);
if (!pool_pick.log.empty()) std::fprintf(stderr, "%s\n", pool_pick.log.c_str());
if (pool_pick.choice == NumaPoolChoice::Off) env = nullptr;
bool auto_pool = pool_pick.choice == NumaPoolChoice::Auto; // the split without STRATA_NUMA_SPLIT: only if it can run
bool env_off = !auto_pool && (env == nullptr || *env == '\0' || std::strcmp(env, "0") == 0);
bool env_known = auto_pool || (!env_off && (std::strcmp(env, "1") == 0 || std::strcmp(env, "test") == 0));
const PoolPlanInput plan_in = pool_plan_input(topo_);
const PoolPlan wplan = plan_pool_workers(plan_in);
// --pool-cores (experimental, off by default): the person's own list of logical processors for the workers. The plan above
// was made from the real topology; only the cores the workers are pinned to (and, by default, their count) come from the list.
if (!pool_cores_setting().empty()) {
const PoolCoresCheck chk = check_pool_cores(pool_cores_setting(), layout_, topo_.host_core);
static std::atomic<bool> told{false}; // (a throw-away pool of --pool-affinity measure builds the same list again)
const bool say = !told.exchange(true);
if (say)
for (const std::string& w : chk.warnings) std::fprintf(stderr, "strata cpu pool: --pool-cores: %s\n", w.c_str());
if (chk.cpus.empty()) {
if (say)
std::fprintf(stderr, "strata cpu pool: --pool-cores: no usable logical processor in the list; the pool uses its "
"default placement\n");
} else {
topo_.worker_cores = chk.cpus;
topo_.worker_package.assign(chk.cpus.size(), -1);
explicit_cores_ = true;
}
}
// the split needs workers on both nodes: its cores are taken round-robin over the nodes (the packages when a core's node
// is unknown), so any count under the core count (the default or a configured one) spans them evenly (one node: the order
// is kept)
auto interleave_cores = [&] {
if (explicit_cores_) return; // --pool-cores: the person's order stays
if (topo_.worker_package.size() != topo_.worker_cores.size()) return;
std::vector<int> key = topo_.worker_package;
#if defined(__linux__)
std::vector<int> by_node;
for (int c : topo_.worker_cores) by_node.push_back(cpu_numa_node(c));
if (std::none_of(by_node.begin(), by_node.end(), [](int n) { return n < 0; })) key = by_node;
#endif
std::vector<int> cores, pkgs;
for (size_t i : interleave_packages(key)) {
cores.push_back(topo_.worker_cores[i]);
pkgs.push_back(topo_.worker_package[i]);
}
topo_.worker_cores = std::move(cores);
topo_.worker_package = std::move(pkgs);
};
// the worker count: configured, else the hybrid P cores, else the plan (`span`: every socket counts, the split's default)
auto pick_workers = [&](bool span, const char** limit) -> int {
if (n_workers > 0) return n_workers;
if (explicit_cores_) {
*limit = "your --pool-cores list";
return (int) topo_.worker_cores.size();
}
if (topo_.is_hybrid && affinity_ != PoolAffinity::All) return (std::max)(1, topo_.p_cores - 1);
if (span) {
PoolPlanInput span_in = plan_in;
span_in.span_packages = true;
const PoolPlan sp = plan_pool_workers(span_in);
*limit = sp.limit;
return (std::min)(sp.workers, (std::max)(1, (int) topo_.worker_cores.size()));
}
*limit = wplan.limit;
return (std::min)(wplan.workers, (std::max)(1, (int) topo_.worker_cores.size()));
};
if (auto_pool) {
// try the split on a copy: when it cannot run the cores, the count and the plan stay exactly what they were
const CpuTopology plain_topo = topo_;
interleave_cores();
const char* try_limit = nullptr;
const int try_n = pick_workers(true, &try_limit);
std::vector<int> thread_core;
for (int j = 0; j < try_n; ++j)
thread_core.push_back(pin && j < (int) topo_.worker_cores.size() ? topo_.worker_cores[(size_t) j] : -1);
if (host_works_) thread_core.push_back(topo_.host_core);
const NumaSplitDecision tried = decide_real_numa_split(thread_core, try_n, pin);
if (!tried.on) {
topo_ = plain_topo;
auto_pool = env_known = false;
env_off = true;
std::fprintf(stderr, "strata cpu pool: NUMA pool off: %s (%d NUMA nodes found), the pool runs as before\n",
tried.why.c_str(), numa_nodes);
}
} else if (env_known) {
interleave_cores();
}
{
const char* limit = nullptr;
n_ = pick_workers(env_known, &limit);
if (limit != nullptr) plan_limit_ = limit;
}
plan_cores_ = wplan.cores;
plan_logical_ = plan_in.logical;
if (n_ < 1) n_ = 1;
scratch_.resize((size_t) n_);
wstate_.reset(new std::atomic<int32_t>[(size_t) n_]);
for (int i = 0; i < n_; ++i) wstate_[(size_t) i].store(kParked);
hstate_ms_.store(now_ms());
g_diag_pool.store(this);
strata::core::diag_pool_fn().store(&diag_active_pool);
split_.resize((size_t) kMaxSplit);
split_multi_.resize((size_t) kMaxSplitMulti);
// STRATA_NUMA_SPLIT: decided here, before any worker starts (a worker counts into its slice's `parked` from its
// first instruction), and never revised. Unset: the plan stays empty and nothing below runs.
NumaSplitPlan plan;
if (!env_off && !env_known) {
plan.why = std::string("unknown STRATA_NUMA_SPLIT value '") + env + "' (use 1 or test)";
std::fprintf(stderr, "strata cpu pool: NUMA split off: %s\n", plan.why.c_str());
} else if (env_known) {
plan.test = !auto_pool && std::strcmp(env, "test") == 0;
const int threads = n_ + (host_works_ ? 1 : 0); // thread j < n_: worker j; j == n_: the host
std::vector<int> slice((size_t) threads, 0);
std::string why;
if (threads < 2) {
why = "the pool has " + std::to_string(threads) + " thread, the split needs 2 or more";
} else if (plan.test) {
for (int j = 0; j < threads; ++j) slice[(size_t) j] = j % 2;
} else {
std::vector<int> thread_core;
for (int j = 0; j < threads; ++j)
thread_core.push_back(j == n_ ? topo_.host_core
: pin && j < (int) topo_.worker_cores.size() ? topo_.worker_cores[(size_t) j] : -1);
const NumaSplitDecision d = decide_real_numa_split(thread_core, n_, pin);
why = d.why;
if (d.on) {
slice = d.slice;
plan.nodes[0] = d.nodes[0];
plan.nodes[1] = d.nodes[1];
for (int j = 0; j < threads; ++j) plan.cores[slice[(size_t) j]].push_back(thread_core[(size_t) j]);
}
}
plan.why = why;
plan.on = plan.why.empty();
if (!plan.on) { // a refusal after the slices were planned (unbalanced) leaves no half-filled plan behind
plan.nodes[0] = plan.nodes[1] = -1;
plan.cores[0].clear();
plan.cores[1].clear();
}
if (plan.on) {
numa_n_ = 2;
nsync_.reset(new NodeSync[2]);
wslice_.assign(slice.begin(), slice.begin() + n_);
hslice_ = host_works_ ? slice[(size_t) n_] : 0;
slice_threads_.assign(2, 0);
for (int k : slice) ++slice_threads_[(size_t) k];
if (auto_pool)
std::fprintf(stderr, "strata cpu pool: NUMA pool on: %d NUMA nodes found, one slice of the pool per node "
"(STRATA_NUMA_POOL=0 turns it off)\n", numa_nodes);
if (plan.test)
std::fprintf(stderr, "strata cpu pool: NUMA split on (test, no real nodes): slice 0 = %d threads, "
"slice 1 = %d threads\n", slice_threads_[0], slice_threads_[1]);
else
std::fprintf(stderr, "strata cpu pool: NUMA split on: slice 0 = node %d (%d threads), slice 1 = node %d "
"(%d threads)\n", plan.nodes[0], slice_threads_[0], plan.nodes[1], slice_threads_[1]);
} else {
std::fprintf(stderr, "strata cpu pool: NUMA split off: %s\n", plan.why.c_str());
}
}
if (n_workers > 0) {
// recommend, never force: a count above what this CPU uses well runs as configured, with a plain warning
// (STRATA_NUMA_SPLIT on: workers on the other socket are the point, that case is not warned about)
PoolPlanInput warn_in = plan_in;
warn_in.span_packages = plan.on;
static std::atomic<bool> warned{false};
const std::string why = pool_workers_warning(n_workers, warn_in, plan_pool_workers(warn_in));
if (!why.empty() && !warned.exchange(true)) std::fprintf(stderr, "strata cpu pool: warning: %s\n", why.c_str());
}
g_numa_plan = std::move(plan);
g_numa_owner = this;
threads_.reserve((size_t) n_);
for (int i = 0; i < n_; ++i) {
const int core = pin ? (i < (int) topo_.worker_cores.size() ? topo_.worker_cores[(size_t) i] : -1) : -1;
threads_.emplace_back([this, i, core] {
strata::platform::name_this_thread("strata-pool", i);
strata::aux_cpus::note_owned_thread(); // --aux-cpus never moves a worker
// a failed pin only costs locality under the split: the worker keeps the slice it was given
if (!pin_this_thread(core, i) && core >= 0 && numa_n_ > 0)
std::fprintf(stderr, "strata cpu pool: NUMA split: worker %d not pinned to CPU %d, its slice's rows may "
"be read remotely\n", i, core);
worker(i);
});
}
}
std::string ExpertPool::placement_line() const {
if (n_ <= 0) return "";
if (!pinned_) return "CPU workers: " + std::to_string(n_) + " workers, not pinned to logical processors (the OS places them)";
// a worker beyond the cores listed runs unpinned (more --pool-workers than --pool-cores entries)
std::vector<int> on(topo_.worker_cores.begin(), topo_.worker_cores.begin() + (std::min)((size_t) n_, topo_.worker_cores.size()));
std::string s = describe_worker_placement(on, topo_.host_core, layout_, explicit_cores_);
if ((int) on.size() < n_) s += " (" + std::to_string(n_ - (int) on.size()) + " more workers run unpinned)";
return s;
}
ExpertPool::~ExpertPool() {
const ExpertPool* self = this;
g_diag_pool.compare_exchange_strong(self, nullptr);
if (g_numa_owner == this) {
g_numa_plan = NumaSplitPlan{};
g_numa_owner = nullptr;
}
stop_.store(true, std::memory_order_release);
// Bump the epoch so a PARKED worker notices the stop flag rather than sleeping through it.
publish();
for (auto& t : threads_) t.join();
}
void ExpertPool::publish() {
// Both sides are seq_cst, and that is the whole lost-wakeup argument: a worker going to sleep does
// `sleepers_++` and then reads `epoch_`, the host does `epoch_++` and then reads `sleepers_`. In one total
// order at least one of them sees the other's write - the worker sees the new epoch and does not sleep, or
// the host sees the sleeper and notifies under the mutex the worker holds until it is inside `wait`.
// On x86 the fetch_add is a locked xadd either way, so this costs the token path nothing.
epoch_.fetch_add(1, std::memory_order_seq_cst);
if (sleepers_.load(std::memory_order_seq_cst) != 0) {
std::lock_guard<std::mutex> lk(sleep_mu_);
sleep_cv_.notify_all();
}
}
void ExpertPool::worker(int id) {
uint32_t seen = 0;
// ARRIVE at the park before the first wait, so `parked_ == n_` is true from construction. Counting only
// on the RETURN from a drain leaves `parked_` at 0 until each worker has finished one batch, and the first
// `run()` - which waits for `parked_ == n_` before publishing - then deadlocks. It deadlocks on the very
// first call, which is the good case; a version that deadlocked on the second would be far worse.
parked_of(id).fetch_add(1, std::memory_order_acq_rel);
for (;;) {
// Park: wait for work. `_mm_pause` rather than a bare spin because it yields the pipeline to the
// sibling hyperthread; `epoch_` is bumped once per LAYER, not once per expert, so most of these
// iterations are spent here with nothing to do.
//
// **AND NOTHING ELSE HAPPENS IN HERE.** This loop used to do `pauses_.fetch_add(1)` on every iteration
// - a locked read-modify-write, five workers against one cache line - so the workers spent their wait
// invalidating each other's caches and the very line the host writes to publish work. The counter was
// diagnostic and nothing branched on it. See the note on the atomics in pool.hpp.
//
// After `kSpinBeforeSleep` with no work the worker sleeps instead (issue #4). The clock is read once
// every 1024 pauses, so the spin itself is unchanged.
const auto parked_at = std::chrono::steady_clock::now();
uint32_t spins = 0;
const uint64_t um = umwait_cycles();
#if defined(_MSC_VER) && defined(_M_X64) && !defined(__clang__) // MSVC proper: clang-cl (the Windows HIP build) has no WAITPKG intrinsics by default
const uint64_t um_esc = umwait_escalate();
const uint64_t um_t0 = um != 0 && um_esc != 0 ? __rdtsc() : 0;
#endif
while (epoch_.load(std::memory_order_acquire) == seen) {