-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathcpu_topo.c
More file actions
2508 lines (2148 loc) · 78 KB
/
Copy pathcpu_topo.c
File metadata and controls
2508 lines (2148 loc) · 78 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
#define _GNU_SOURCE
#include <dirent.h>
#include <sched.h>
#include <string.h>
#include <unistd.h>
#include <haproxy/api.h>
#include <haproxy/cfgparse.h>
#include <haproxy/cpuset.h>
#include <haproxy/cpu_topo.h>
#include <haproxy/global.h>
#include <haproxy/log.h>
#include <haproxy/tools.h>
/* for cpu_set.flags below */
#define CPU_SET_FL_NONE 0x0000
#define CPU_SET_FL_DO_RESET 0x0001
/* cpu_policy_conf flags */
#define CPU_POLICY_ONE_THREAD_PER_CORE (1 << 0)
#define CPU_POLICY_SET_IN_CONFIG (1 << 1)
/* cpu_policy_conf affinities */
#define CPU_AFFINITY_PER_GROUP (1 << 0)
#define CPU_AFFINITY_PER_CORE (1 << 1)
#define CPU_AFFINITY_PER_THREAD (1 << 2)
#define CPU_AFFINITY_PER_CCX (1 << 3)
/*
* Specific to the per-group affinity
*/
#define CPU_AFFINITY_PER_GROUP_LOOSE (1 << 8)
/* CPU topology information, ha_cpuset_size() entries, allocated at boot */
int cpu_topo_maxcpus = -1; // max number of CPUs supported by OS/haproxy
int cpu_topo_lastcpu = -1; // last supposed online CPU (no need to look beyond)
struct ha_cpu_topo *ha_cpu_topo = NULL;
struct ha_cpu_cluster *ha_cpu_clusters = NULL;
struct cpu_map *cpu_map;
/* non-zero if we're certain that taskset or similar was used to force CPUs */
int cpu_mask_forced = 0;
/* "cpu-set" global configuration */
struct cpu_set_cfg {
uint flags; // CPU_SET_FL_XXX above
/* CPU numbers to accept / reject */
struct hap_cpuset only_cpus;
struct hap_cpuset drop_cpus;
/* node numbers to accept / reject */
struct hap_cpuset only_nodes;
struct hap_cpuset drop_nodes;
/* cluster numbers to accept / reject */
struct hap_cpuset only_clusters;
struct hap_cpuset drop_clusters;
/* core numbers to accept / reject */
struct hap_cpuset only_cores;
struct hap_cpuset drop_cores;
/* thread numbers to accept / reject */
struct hap_cpuset only_threads;
struct hap_cpuset drop_threads;
} cpu_set_cfg;
/* CPU policy choice */
struct {
int cpu_policy;
int flags;
int affinity;
} cpu_policy_conf = {
1, /* "performance" policy */
0, /* Default flags */
0, /* Default affinity */
};
struct cpu_affinity_optional {
char *name;
int affinity_flag;
};
static struct cpu_affinity_optional per_group_optional[] = {
{"loose", CPU_AFFINITY_PER_GROUP_LOOSE},
{"auto", 0},
{NULL, 0}
};
static struct cpu_affinity {
char *name;
int affinity_flags;
struct cpu_affinity_optional *optional;
} ha_cpu_affinity[] = {
{"per-core", CPU_AFFINITY_PER_CORE, NULL},
{"per-group", CPU_AFFINITY_PER_GROUP, per_group_optional},
{"per-thread", CPU_AFFINITY_PER_THREAD, NULL},
{"per-ccx", CPU_AFFINITY_PER_CCX, NULL},
{"auto", 0, NULL},
{NULL, 0, NULL}
};
/* list of CPU policies for "cpu-policy". The default one is the first one. */
static int cpu_policy_first_usable_node(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static int cpu_policy_group_by_ccx(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static int cpu_policy_group_by_cluster(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static int cpu_policy_performance(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static int cpu_policy_efficiency(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static int cpu_policy_resource(int policy, int tmin, int tmax, int gmin, int gmax, char **err);
static struct ha_cpu_policy ha_cpu_policy[] = {
{ .name = "none", .desc = "use all available CPUs", .fct = NULL },
{ .name = "performance", .desc = "make one thread group per perf. core cluster", .fct = cpu_policy_performance , .arg = 0 },
{ .name = "group-by-ccx", .desc = "make one thread group per CCX", .fct = cpu_policy_group_by_ccx , .arg = 1 },
{ .name = "group-by-2-ccx", .desc = "make one thread group per 2 CCX", .fct = cpu_policy_group_by_ccx , .arg = 2 },
{ .name = "group-by-3-ccx", .desc = "make one thread group per 3 CCX", .fct = cpu_policy_group_by_ccx , .arg = 3 },
{ .name = "group-by-4-ccx", .desc = "make one thread group per 4 CCX", .fct = cpu_policy_group_by_ccx , .arg = 4 },
{ .name = "group-by-cluster", .desc = "make one thread group per core cluster", .fct = cpu_policy_group_by_cluster , .arg = 1 },
{ .name = "group-by-2-clusters",.desc = "make one thread group per 2 core clusters", .fct = cpu_policy_group_by_cluster , .arg = 2 },
{ .name = "group-by-3-clusters",.desc = "make one thread group per 3 core clusters", .fct = cpu_policy_group_by_cluster , .arg = 3 },
{ .name = "group-by-4-clusters",.desc = "make one thread group per 4 core clusters", .fct = cpu_policy_group_by_cluster , .arg = 4 },
{ .name = "efficiency", .desc = "make one thread group per eff. core cluster", .fct = cpu_policy_efficiency , .arg = 0 },
{ .name = "resource", .desc = "make one thread group from the smallest cluster", .fct = cpu_policy_resource , .arg = 0 },
{ .name = "first-usable-node", .desc = "use only first usable node if nbthreads not set", .fct = cpu_policy_first_usable_node, .arg = 0 },
{ 0 } /* end */
};
/* Detects CPUs that are online on the system. It may rely on FS access (e.g.
* /sys on Linux). Returns the number of CPUs detected or 0 if the detection
* failed.
*/
int ha_cpuset_detect_online(struct hap_cpuset *set)
{
#if defined(__linux__)
ha_cpuset_zero(set);
/* contains a list of CPUs in the format <low>[-<high>][,...] */
if (read_line_to_trash("%s/cpu/online", NUMA_DETECT_SYSTEM_SYSFS_PATH) >= 0) {
const char *parse_cpu_set_args[2] = { trash.area, "\0" };
if (parse_cpu_set(parse_cpu_set_args, set, NULL) != 0)
ha_cpuset_zero(set);
}
#elif defined(__FreeBSD__)
struct hap_cpuset node_cpu_set;
int ndomains, domain;
size_t len = sizeof(ndomains);
ha_cpuset_zero(set);
/* retrieve the union of NUMA nodes as online CPUs */
if (sysctlbyname("vm.ndomains", &ndomains, &len, NULL, 0) == 0) {
BUG_ON(ndomains > MAXMEMDOM);
for (domain = 0; domain < ndomains; domain++) {
ha_cpuset_zero(&node_cpu_set);
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_DOMAIN, domain,
sizeof(node_cpu_set.cpuset), &node_cpu_set.cpuset) == -1)
continue;
ha_cpuset_or(set, &node_cpu_set);
}
}
#else // !__linux__, !__FreeBSD__
ha_cpuset_zero(set);
#endif
return ha_cpuset_count(set);
}
/* Detects the CPUs that will be used based on the ones the process is bound to
* at boot. The principle is the following: all CPUs from the boot cpuset will
* be used since we don't know upfront how individual threads will be mapped to
* groups and CPUs.
*
* Returns non-zero on success, zero on failure. Note that it may not be
* performed in the function above because some calls may rely on other items
* being allocated (e.g. trash).
*/
int cpu_detect_usable(void)
{
struct hap_cpuset boot_set = { };
int cpu;
if (!(cpu_set_cfg.flags & CPU_SET_FL_DO_RESET)) {
/* update the list with the CPUs currently bound to the current process */
ha_cpuset_detect_bound(&boot_set);
/* remove the known-excluded CPUs */
for (cpu = 0; cpu < cpu_topo_maxcpus; cpu++)
if (!ha_cpuset_isset(&boot_set, cpu))
ha_cpu_topo[cpu].st |= HA_CPU_F_EXCLUDED;
}
/* remove CPUs in the drop-cpu set or not in the only-cpu set */
for (cpu = 0; cpu < cpu_topo_maxcpus; cpu++) {
if ( ha_cpuset_isset(&cpu_set_cfg.drop_cpus, cpu) ||
!ha_cpuset_isset(&cpu_set_cfg.only_cpus, cpu))
ha_cpu_topo[cpu].st |= HA_CPU_F_DONT_USE;
}
/* Update the list of currently offline CPUs. Normally it's a subset
* of the unbound ones, but we cannot infer anything if we don't have
* the info so we only update what we know. We take this opportunity
* for detecting that some online CPUs are not bound, indicating that
* taskset or equivalent was used.
*/
if (ha_cpuset_detect_online(&boot_set)) {
for (cpu = 0; cpu < cpu_topo_maxcpus; cpu++) {
if (!ha_cpuset_isset(&boot_set, cpu)) {
ha_cpu_topo[cpu].st |= HA_CPU_F_OFFLINE;
} else {
cpu_topo_lastcpu = cpu;
if (ha_cpu_topo[cpu].st & HA_CPU_F_EXCLUDED)
cpu_mask_forced = 1;
}
}
}
return 0;
}
/* Detects CPUs that are bound to the current process. Returns the number of
* CPUs detected or 0 if the detection failed.
*/
int ha_cpuset_detect_bound(struct hap_cpuset *set)
{
ha_cpuset_zero(set);
/* detect bound CPUs depending on the OS's API */
if (0
#if defined(__linux__)
|| sched_getaffinity(0, sizeof(set->cpuset), &set->cpuset) != 0
#elif defined(__FreeBSD__)
|| cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset) != 0
#else
|| 1 // unhandled platform
#endif
) {
/* detection failed */
return 0;
}
return ha_cpuset_count(set);
}
/* Returns true if at least one cpu-map directive was configured, otherwise
* false.
*/
int cpu_map_configured(void)
{
int grp, thr;
for (grp = 0; grp < MAX_TGROUPS; grp++) {
for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
if (ha_cpuset_count(&cpu_map[grp].thread[thr]))
return 1;
}
return 0;
}
/* Dump the CPU topology <topo> for up to cpu_topo_maxcpus CPUs for
* debugging purposes. Offline CPUs are skipped.
*/
void cpu_topo_debug(const struct ha_cpu_topo *topo)
{
int has_smt = 0;
int cpu, lvl;
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].th_cnt > 1) {
has_smt = 1;
break;
}
}
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].st & HA_CPU_F_OFFLINE)
continue;
printf("[%s] cpu=%3d pk=%02d no=%02d cl=%03d(%03d)",
(ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK) ? "----" : "keep",
ha_cpu_topo[cpu].idx,
ha_cpu_topo[cpu].pk_id,
ha_cpu_topo[cpu].no_id,
ha_cpu_topo[cpu].cl_gid,
ha_cpu_topo[cpu].cl_lid);
/* list only relevant cache levels */
for (lvl = 4; lvl >= 0; lvl--) {
if (ha_cpu_topo[cpu].ca_id[lvl] < 0)
continue;
printf(lvl < 3 ? " l%d=%02d" : " l%d=%03d", lvl, ha_cpu_topo[cpu].ca_id[lvl]);
}
printf(" ts=%03d capa=%d",
ha_cpu_topo[cpu].ts_id,
ha_cpu_topo[cpu].capa);
if (has_smt) {
if (ha_cpu_topo[cpu].th_cnt > 1)
printf(" smt=%d/%d",
ha_cpu_topo[cpu].th_id,
ha_cpu_topo[cpu].th_cnt);
else
printf(" smt=%d",
ha_cpu_topo[cpu].th_cnt);
}
putchar('\n');
}
}
/* Dump the summary of CPU topology <topo>: clusters info and thread-cpu
* bindings.
*/
void cpu_topo_dump_summary(const struct ha_cpu_topo *topo, struct buffer *trash)
{
int cpu, grp, thr;
chunk_appendf(trash, "CPU clusters:\n");
for (cpu = 0; cpu < cpu_topo_maxcpus; cpu++) {
if (!ha_cpu_clusters[cpu].nb_cpu)
continue;
chunk_appendf(trash, " %3u cpus=%3u cores=%3u capa=%u\n",
cpu, ha_cpu_clusters[cpu].nb_cpu,
ha_cpu_clusters[cpu].nb_cores,
ha_cpu_clusters[cpu].capa);
}
chunk_appendf(trash, "Thread CPU Bindings:\n Tgrp/Thr Tid CPU set\n");
for (grp = 0; grp < global.nbtgroups; grp++) {
int first, last;
int min, max;
first = ha_tgroup_info[grp].base;
last = ha_tgroup_info[grp].base + ha_tgroup_info[grp].count - 1;
min = max = -1;
for (thr = first; thr <= last; thr++) {
if (min < 0)
min = thr;
if (thr == last ||
!ha_cpuset_isequal(&cpu_map[grp].thread[min - first],
&cpu_map[grp].thread[thr + 1 - first]))
max = thr;
if (min >= 0 && max >= 0) {
/* we have a range */
char str[1024];
int len = 0;
int len2;
/* print group/thread-range */
len += snprintf(str + len, sizeof(str) - len, "%d/%d", grp + 1, min - first + 1);
if (min != max)
len += snprintf(str + len, sizeof(str) - len, "-%d", max - first + 1);
/* max len is 8: "64/64-64", plus 2 spaces = 10 */
while (len < 10) {
str[len++] = ' ';
str[len] = 0;
}
/* append global thread range */
len += snprintf(str + len, sizeof(str) - len, "%d", min + 1);
if (min != max)
len += snprintf(str + len, sizeof(str) - len, "-%d", max + 1);
/* max len is 9: "4096-4096", plus 2 spaces = 11, plus 10 initial chars = 21 */
while (len < 21) {
str[len++] = ' ';
str[len] = 0;
}
if (ha_cpuset_count(&cpu_map[grp].thread[thr - first]))
len += snprintf(str + len, sizeof(str) - len, "%d: ", ha_cpuset_count(&cpu_map[grp].thread[thr - first]));
len2 = print_cpu_set(str + len, sizeof(str) - len, &cpu_map[grp].thread[thr - first]);
if (len2 > sizeof(str) - len)
snprintf(str + len, sizeof(str) - len, "<too_large>");
else if (len2 == 0)
snprintf(str + len, sizeof(str) - len, "<all>");
chunk_appendf(trash, " %s\n", str);
min = max = -1;
}
}
}
}
/* function used by qsort to re-arrange CPUs by index only, to restore original
* ordering.
*/
int _cmp_cpu_index(const void *a, const void *b)
{
const struct ha_cpu_topo *l = (const struct ha_cpu_topo *)a;
const struct ha_cpu_topo *r = (const struct ha_cpu_topo *)b;
/* next, IDX, so that SMT ordering is preserved */
if (l->idx >= 0 && l->idx < r->idx)
return -1;
if (l->idx > r->idx && r->idx >= 0)
return 1;
/* exactly the same (e.g. absent, should not happen) */
return 0;
}
/* function used by qsort to compare two hwcpus and arrange them by vicinity
* only. -1 says a<b, 1 says a>b. The goal is to arrange the closest CPUs
* together, preferring locality over performance in order to keep latency
* as low as possible, so that when picking a fixed number of threads, the
* closest ones are used in priority. It's also used to help arranging groups
* at the end.
*/
int _cmp_cpu_locality(const void *a, const void *b)
{
const struct ha_cpu_topo *l = (const struct ha_cpu_topo *)a;
const struct ha_cpu_topo *r = (const struct ha_cpu_topo *)b;
/* first, online vs offline */
if (!(l->st & HA_CPU_F_EXCL_MASK) && (r->st & HA_CPU_F_EXCL_MASK))
return -1;
if (!(r->st & HA_CPU_F_EXCL_MASK) && (l->st & HA_CPU_F_EXCL_MASK))
return 1;
/* next, package ID */
if (l->pk_id >= 0 && l->pk_id < r->pk_id)
return -1;
if (l->pk_id > r->pk_id && r->pk_id >= 0)
return 1;
/* next, node ID */
if (l->no_id >= 0 && l->no_id < r->no_id)
return -1;
if (l->no_id > r->no_id && r->no_id >= 0)
return 1;
/* next, L4 */
if (l->ca_id[4] >= 0 && l->ca_id[4] < r->ca_id[4])
return -1;
if (l->ca_id[4] > r->ca_id[4] && r->ca_id[4] >= 0)
return 1;
/* next, L3 */
if (l->ca_id[3] >= 0 && l->ca_id[3] < r->ca_id[3])
return -1;
if (l->ca_id[3] > r->ca_id[3] && r->ca_id[3] >= 0)
return 1;
/* next, cluster */
if (l->cl_gid >= 0 && l->cl_gid < r->cl_gid)
return -1;
if (l->cl_gid > r->cl_gid && r->cl_gid >= 0)
return 1;
/* next, L2 */
if (l->ca_id[2] >= 0 && l->ca_id[2] < r->ca_id[2])
return -1;
if (l->ca_id[2] > r->ca_id[2] && r->ca_id[2] >= 0)
return 1;
/* next, thread set */
if (l->ts_id >= 0 && l->ts_id < r->ts_id)
return -1;
if (l->ts_id > r->ts_id && r->ts_id >= 0)
return 1;
/* next, L1 */
if (l->ca_id[1] >= 0 && l->ca_id[1] < r->ca_id[1])
return -1;
if (l->ca_id[1] > r->ca_id[1] && r->ca_id[1] >= 0)
return 1;
/* next, L0 */
if (l->ca_id[0] >= 0 && l->ca_id[0] < r->ca_id[0])
return -1;
if (l->ca_id[0] > r->ca_id[0] && r->ca_id[0] >= 0)
return 1;
/* next, IDX, so that SMT ordering is preserved */
if (l->idx >= 0 && l->idx < r->idx)
return -1;
if (l->idx > r->idx && r->idx >= 0)
return 1;
/* exactly the same (e.g. absent) */
return 0;
}
/* function used by qsort to compare two hwcpus and arrange them by vicinity
* then capacity. -1 says a<b, 1 says a>b. The goal is to detect different
* CPU capacities among clusters.
*/
int _cmp_cpu_cluster_capa(const void *a, const void *b)
{
const struct ha_cpu_topo *l = (const struct ha_cpu_topo *)a;
const struct ha_cpu_topo *r = (const struct ha_cpu_topo *)b;
/* first, online vs offline */
if (!(l->st & HA_CPU_F_EXCL_MASK) && (r->st & HA_CPU_F_EXCL_MASK))
return -1;
if (!(r->st & HA_CPU_F_EXCL_MASK) && (l->st & HA_CPU_F_EXCL_MASK))
return 1;
/* next, package ID */
if (l->pk_id >= 0 && l->pk_id < r->pk_id)
return -1;
if (l->pk_id > r->pk_id && r->pk_id >= 0)
return 1;
/* next, node ID */
if (l->no_id >= 0 && l->no_id < r->no_id)
return -1;
if (l->no_id > r->no_id && r->no_id >= 0)
return 1;
/* next, L4 */
if (l->ca_id[4] >= 0 && l->ca_id[4] < r->ca_id[4])
return -1;
if (l->ca_id[4] > r->ca_id[4] && r->ca_id[4] >= 0)
return 1;
/* next, L3 */
if (l->ca_id[3] >= 0 && l->ca_id[3] < r->ca_id[3])
return -1;
if (l->ca_id[3] > r->ca_id[3] && r->ca_id[3] >= 0)
return 1;
/* next, cluster */
if (l->cl_gid >= 0 && l->cl_gid < r->cl_gid)
return -1;
if (l->cl_gid > r->cl_gid && r->cl_gid >= 0)
return 1;
/* Same cluster. For CPU capacity, we tolerate a +/- 5% margin however
* so that if some values come from measurement we don't end up
* reorganizing everything.
*/
if (l->capa > 0 && (int)l->capa * 19 > (int)r->capa * 20)
return -1;
if (r->capa > 0 && (int)l->capa * 20 < (int)r->capa * 19)
return 1;
/* next, L2 */
if (l->ca_id[2] >= 0 && l->ca_id[2] < r->ca_id[2])
return -1;
if (l->ca_id[2] > r->ca_id[2] && r->ca_id[2] >= 0)
return 1;
/* next, thread set */
if (l->ts_id >= 0 && l->ts_id < r->ts_id)
return -1;
if (l->ts_id > r->ts_id && r->ts_id >= 0)
return 1;
/* next, L1 */
if (l->ca_id[1] >= 0 && l->ca_id[1] < r->ca_id[1])
return -1;
if (l->ca_id[1] > r->ca_id[1] && r->ca_id[1] >= 0)
return 1;
/* next, L0 */
if (l->ca_id[0] >= 0 && l->ca_id[0] < r->ca_id[0])
return -1;
if (l->ca_id[0] > r->ca_id[0] && r->ca_id[0] >= 0)
return 1;
/* next, IDX, so that SMT ordering is preserved */
if (l->idx >= 0 && l->idx < r->idx)
return -1;
if (l->idx > r->idx && r->idx >= 0)
return 1;
/* exactly the same */
return 0;
}
/* function used by qsort to compare two hwcpus and arrange them by cluster to
* make sure no cluster crosses L3 boundaries. -1 says a<b, 1 says a>b. It's
* only used during topology detection.
*/
int _cmp_cpu_cluster(const void *a, const void *b)
{
const struct ha_cpu_topo *l = (const struct ha_cpu_topo *)a;
const struct ha_cpu_topo *r = (const struct ha_cpu_topo *)b;
/* first, online vs offline */
if (!(l->st & HA_CPU_F_EXCL_MASK) && (r->st & HA_CPU_F_EXCL_MASK))
return -1;
if (!(r->st & HA_CPU_F_EXCL_MASK) && (l->st & HA_CPU_F_EXCL_MASK))
return 1;
/* next, cluster */
if (l->cl_gid >= 0 && l->cl_gid < r->cl_gid)
return -1;
if (l->cl_gid > r->cl_gid && r->cl_gid >= 0)
return 1;
/* next, package ID */
if (l->pk_id >= 0 && l->pk_id < r->pk_id)
return -1;
if (l->pk_id > r->pk_id && r->pk_id >= 0)
return 1;
/* next, node ID */
if (l->no_id >= 0 && l->no_id < r->no_id)
return -1;
if (l->no_id > r->no_id && r->no_id >= 0)
return 1;
/* next, L3 */
if (l->ca_id[3] >= 0 && l->ca_id[3] < r->ca_id[3])
return -1;
if (l->ca_id[3] > r->ca_id[3] && r->ca_id[3] >= 0)
return 1;
/* if no L3, then L2 */
if (l->ca_id[2] >= 0 && l->ca_id[2] < r->ca_id[2])
return -1;
if (l->ca_id[2] > r->ca_id[2] && r->ca_id[2] >= 0)
return 1;
/* next, IDX, so that SMT ordering is preserved */
if (l->idx >= 0 && l->idx < r->idx)
return -1;
if (l->idx > r->idx && r->idx >= 0)
return 1;
/* exactly the same (e.g. absent) */
return 0;
}
/* re-order a CPU topology array by CPU index only. This is mostly used before
* listing CPUs regardless of their characteristics.
*/
void cpu_reorder_by_index(struct ha_cpu_topo *topo, int entries)
{
qsort(topo, entries, sizeof(*topo), _cmp_cpu_index);
}
/* re-order a CPU topology array by locality to help form groups. */
void cpu_reorder_by_locality(struct ha_cpu_topo *topo, int entries)
{
qsort(topo, entries, sizeof(*topo), _cmp_cpu_locality);
}
/* re-order a CPU topology array by cluster id. */
void cpu_reorder_by_cluster(struct ha_cpu_topo *topo, int entries)
{
qsort(topo, entries, sizeof(*topo), _cmp_cpu_cluster);
}
/* re-order a CPU topology array by locality and capacity to detect clusters. */
void cpu_reorder_by_cluster_capa(struct ha_cpu_topo *topo, int entries)
{
qsort(topo, entries, sizeof(*topo), _cmp_cpu_cluster_capa);
}
/* functions below act on ha_cpu_cluster structs */
/* function used by qsort to reorder clusters by index */
int _cmp_cluster_index(const void *a, const void *b)
{
const struct ha_cpu_cluster *l = (const struct ha_cpu_cluster *)a;
const struct ha_cpu_cluster *r = (const struct ha_cpu_cluster *)b;
return l->idx - r->idx;
}
/* function used by qsort to order clusters by reverse capacity */
int _cmp_cluster_capa(const void *a, const void *b)
{
const struct ha_cpu_cluster *l = (const struct ha_cpu_cluster *)a;
const struct ha_cpu_cluster *r = (const struct ha_cpu_cluster *)b;
return r->capa - l->capa;
}
/* function used by qsort to order clusters by average reverse capacity */
int _cmp_cluster_avg_capa(const void *a, const void *b)
{
const struct ha_cpu_cluster *l = (const struct ha_cpu_cluster *)a;
const struct ha_cpu_cluster *r = (const struct ha_cpu_cluster *)b;
if (!r->nb_cores || !l->nb_cores)
return r->nb_cores - l->nb_cores;
return r->capa * l->nb_cores - l->capa * r->nb_cores;
}
/* re-order a cluster array by cluster index only */
void cpu_cluster_reorder_by_index(struct ha_cpu_cluster *clusters, int entries)
{
qsort(clusters, entries, sizeof(*clusters), _cmp_cluster_index);
}
/* re-order a CPU topology array by locality and capacity to detect clusters. */
void cpu_cluster_reorder_by_capa(struct ha_cpu_cluster *clusters, int entries)
{
qsort(clusters, entries, sizeof(*clusters), _cmp_cluster_capa);
}
/* re-order a CPU topology array by locality and avg capacity to detect clusters. */
void cpu_cluster_reorder_by_avg_capa(struct ha_cpu_cluster *clusters, int entries)
{
qsort(clusters, entries, sizeof(*clusters), _cmp_cluster_avg_capa);
}
/* returns an optimal maxcpus for the current system. It will take into
* account what is reported by the OS, if any, otherwise will fall back
* to the cpuset size, which serves as an upper limit in any case.
*/
static int cpu_topo_get_maxcpus(void)
{
int abs_max = ha_cpuset_size();
#if defined(_SC_NPROCESSORS_CONF)
int n = (int)sysconf(_SC_NPROCESSORS_CONF);
if (n > 0 && n <= abs_max)
return n;
#endif
return abs_max;
}
/* This function is responsible for trying to fill in the missing info after
* topology detection and making sure we don't leave any ID at -1, but rather
* we assign unused ones.
*/
void cpu_fixup_topology(void)
{
struct hap_cpuset cpuset;
int cpu, cpu2;
int curr_id, prev_id;
int min_id, neg;
int cl_cpu, small_cl;
/* fill the package id, node id and thread_id. First we'll build a bitmap
* of all unassigned ones so that we can spot the lowest unassigned one
* and assign it to those currently set to -1.
*/
/* package id */
ha_cpuset_zero(&cpuset);
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++)
ha_cpuset_set(&cpuset, cpu);
for (cpu = neg = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].pk_id < 0)
neg++;
else
ha_cpuset_clr(&cpuset, ha_cpu_topo[cpu].pk_id);
}
/* get the first unused pkg id */
min_id = ha_cpuset_ffs(&cpuset) - 1;
for (cpu = 0; neg && cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].pk_id < 0) {
ha_cpu_topo[cpu].pk_id = min_id;
neg--;
}
}
/* node id */
ha_cpuset_zero(&cpuset);
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++)
ha_cpuset_set(&cpuset, cpu);
for (cpu = neg = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].no_id < 0)
neg++;
else
ha_cpuset_clr(&cpuset, ha_cpu_topo[cpu].no_id);
}
/* get the first unused node id */
min_id = ha_cpuset_ffs(&cpuset) - 1;
for (cpu = 0; neg && cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].no_id < 0) {
ha_cpu_topo[cpu].no_id = min_id;
neg--;
}
}
/* thread id */
ha_cpuset_zero(&cpuset);
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++)
ha_cpuset_set(&cpuset, cpu);
for (cpu = neg = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].th_id < 0)
neg++;
else
ha_cpuset_clr(&cpuset, ha_cpu_topo[cpu].th_id);
}
/* get the first unused thr id */
min_id = ha_cpuset_ffs(&cpuset) - 1;
for (cpu = 0; neg && cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].th_id < 0) {
ha_cpu_topo[cpu].th_id = min_id;
ha_cpu_topo[cpu].th_cnt = min_id + 1;
neg--;
}
}
/* Some machines (typically ARM cortex A76 and Neoverse-N1) report 1
* cluster per pair of cores due to the internal architecture. While
* this can occasionally make sense (i.e. big.LITTLE etc), when there
* are many clusters of few cores, this is totally pointless. Here
* we'll check if there are at least 4 2-cpu clusters, and if so, all
* the 2-cpu clusters will be cancelled.
*/
cpu_reorder_by_cluster(ha_cpu_topo, cpu_topo_maxcpus);
curr_id = -1;
cl_cpu = small_cl = 0;
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].cl_gid < 0)
continue;
if (ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK)
continue;
if (ha_cpu_topo[cpu].cl_gid != curr_id) {
if (curr_id >= 0 && cl_cpu <= 2)
small_cl++;
cl_cpu = 0;
curr_id = ha_cpu_topo[cpu].cl_gid;
}
cl_cpu++;
}
/* last one */
if (cl_cpu && cl_cpu <= 2)
small_cl++;
/* here we have the number of small clusters (<=2 cpu) in small_cl */
if (small_cl >= 4) {
for (cpu = cpu2 = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].cl_gid < 0)
continue;
if (ha_cpu_topo[cpu].st & HA_CPU_F_EXCL_MASK)
continue;
if (ha_cpu_topo[cpu].cl_gid != curr_id) {
if (curr_id >= 0 && cl_cpu <= 2) {
/* small cluster found for curr_id */
while (cpu2 < cpu) {
if (ha_cpu_topo[cpu2].cl_gid == curr_id)
ha_cpu_topo[cpu2].cl_gid = -1;
cpu2++;
}
}
cl_cpu = 0;
cpu2 = cpu;
curr_id = ha_cpu_topo[cpu].cl_gid;
}
cl_cpu++;
}
/* handle the last cluster */
while (curr_id >= 0 && cl_cpu <= 2 && cpu2 < cpu) {
if (ha_cpu_topo[cpu2].cl_gid == curr_id)
ha_cpu_topo[cpu2].cl_gid = -1;
cpu2++;
}
}
cpu_reorder_by_index(ha_cpu_topo, cpu_topo_maxcpus);
/* assign capacity if not filled, based on the number of threads on the
* core: in a same package, SMT-capable cores are generally those
* optimized for performers while non-SMT ones are generally those
* optimized for efficiency. We'll reflect that by assigning 100 and 50
* respectively to those.
*/
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].capa < 0)
ha_cpu_topo[cpu].capa = (ha_cpu_topo[cpu].th_cnt > 1) ? 100 : 50;
}
/* First, on some machines, L3 is not reported. But some also don't
* have L3. However, no L3 when there are more than 2 L2 is quite
* unheard of, and while we don't really care about firing 2 groups for
* 2 L2, we'd rather avoid this if there are 8! In this case we'll add
* an L3 instance to fix the situation.
*/
cpu_reorder_by_locality(ha_cpu_topo, cpu_topo_maxcpus);
prev_id = -2; // make sure it cannot match even unassigned ones
curr_id = -1;
for (cpu = cpu2 = 0; cpu <= cpu_topo_lastcpu; cpu++) {
if (ha_cpu_topo[cpu].ca_id[3] >= 0)
continue;
/* L3 not assigned, count L2 instances */
if (!cpu ||
(ha_cpu_topo[cpu].pk_id != ha_cpu_topo[cpu-1].pk_id) ||
(ha_cpu_topo[cpu].no_id != ha_cpu_topo[cpu-1].no_id) ||
(ha_cpu_topo[cpu].ca_id[4] != ha_cpu_topo[cpu-1].ca_id[4])) {
curr_id = 0;
prev_id = -2;
cpu2 = cpu;
}
else if (ha_cpu_topo[cpu].ca_id[2] != prev_id) {
curr_id++;
if (curr_id >= 2) {
/* let's assign L3 id to zero for all those.
* We can go till the end since we'll just skip
* them on next passes above.
*/
for (; cpu2 <= cpu_topo_lastcpu; cpu2++) {
if (ha_cpu_topo[cpu2].ca_id[3] < 0 &&
ha_cpu_topo[cpu2].pk_id == ha_cpu_topo[cpu].pk_id &&
ha_cpu_topo[cpu2].no_id == ha_cpu_topo[cpu].no_id &&
ha_cpu_topo[cpu2].ca_id[4] == ha_cpu_topo[cpu].ca_id[4])
ha_cpu_topo[cpu2].ca_id[3] = 0;
}
}
}
}
/* let's make core numbers contiguous and per (pkg,node) as well, as
* holes may exist due to SMT.
*/
prev_id = -2; // make sure it cannot match even unassigned ones
curr_id = -1;
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
/* renumber clusters and assign unassigne ones at the same
* time. For this, we'll compare pkg/die/llc with the last
* CPU's and verify if we need to create a new cluster ID.
* Note that some platforms don't report cache. The value is
* local to the pkg+node combination so that we reset it when
* changing.
*/
if (!cpu ||
(ha_cpu_topo[cpu].pk_id != ha_cpu_topo[cpu-1].pk_id) ||
(ha_cpu_topo[cpu].no_id != ha_cpu_topo[cpu-1].no_id)) {
curr_id = 0;
}
else if (ha_cpu_topo[cpu].ts_id != prev_id ||
ha_cpu_topo[cpu].ca_id[4] != ha_cpu_topo[cpu-1].ca_id[4] ||
(ha_cpu_topo[cpu].ca_id[4] < 0 && // no l4 ? check L3
((ha_cpu_topo[cpu].ca_id[3] != ha_cpu_topo[cpu-1].ca_id[3]) ||
(ha_cpu_topo[cpu].ca_id[3] < 0 && // no l3 ? check L2
(ha_cpu_topo[cpu].ca_id[2] != ha_cpu_topo[cpu-1].ca_id[2]))))) {
curr_id++;
}
prev_id = ha_cpu_topo[cpu].ts_id;
ha_cpu_topo[cpu].ts_id = curr_id;
}
cpu_reorder_by_index(ha_cpu_topo, cpu_topo_maxcpus);
}
/* This function is responsible for composing clusters based on existing info
* on the CPU topology.
*/
void cpu_compose_clusters(void)
{
int cpu, core;
int curr_gid, prev_gid;
int curr_lid, prev_lid;
/* Now we'll sort CPUs by topology/cluster/capacity and assign cluster
* IDs to those that don't have one, based on the die/pkg/lcc, and
* double-check that capacity within a cluster doesn't vary by +/- 5%,
* otherwise it indicates different clusters (typically big.little).
*/
cpu_reorder_by_cluster_capa(ha_cpu_topo, cpu_topo_maxcpus);
prev_gid = prev_lid = -2; // make sure it cannot match even unassigned ones
curr_gid = curr_lid = -1;
core = -1;
for (cpu = 0; cpu <= cpu_topo_lastcpu; cpu++) {
/* renumber clusters and assign unassigned ones at the same
* time. For this, we'll compare pkg/die/llc with the last
* CPU's and verify if we need to create a new cluster ID.
* Note that some platforms don't report cache. The local value
* is local to the pkg+node combination so that we reset it
* when changing, contrary to the global one which grows.
*/
if (!cpu ||
(ha_cpu_topo[cpu].pk_id != ha_cpu_topo[cpu-1].pk_id) ||
(ha_cpu_topo[cpu].no_id != ha_cpu_topo[cpu-1].no_id)) {
curr_gid++;
curr_lid = 0;
core = -1;
}
else if (ha_cpu_topo[cpu].cl_gid != prev_gid ||
ha_cpu_topo[cpu].ca_id[4] != ha_cpu_topo[cpu-1].ca_id[4] ||
(ha_cpu_topo[cpu].ca_id[4] < 0 && // no l4 ? check L3