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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
//! Memory attributes
//!
//! On some platforms, NUMA nodes do not represent homogeneous memory, but
//! instead reflect multiple tiers of memory with different performance
//! characteristics (HBM, DDR, NVRAM...).
//!
//! hwloc's memory attribute API, which this module is all about, lets you know
//! pick which memory you want to allocate from based on capacity, locality,
//! latency and bandwidth considerations.
//!
//! Most of this module's functionality is exposed via [methods of the Topology
//! struct](../../topology/struct.Topology.html#comparing-memory-node-attributes-for-finding-where-to-allocate-on).
//! The module itself only hosts type definitions that are related to this
//! functionality.
#[cfg(feature = "hwloc-2_12_0")]
use crate::memory::nodeset::NodeSet;
#[cfg(doc)]
use crate::topology::support::DiscoverySupport;
use crate::{
bitmap::{BitmapCow, BitmapRef},
cpu::cpuset::CpuSet,
errors::{self, FlagsError, ForeignObjectError, HybridError, NulError, RawHwlocError},
ffi::{
int,
string::LibcString,
transparent::{AsInner, AsNewtype},
},
object::{TopologyObject, TopologyObjectID, types::ObjectType},
topology::{Topology, editor::TopologyEditor},
};
use bitflags::bitflags;
use derive_more::{Display, From};
use errno::Errno;
#[cfg(feature = "hwloc-2_12_1")]
use hwlocality_sys::HWLOC_LOCAL_NUMANODE_FLAG_INTERSECT_LOCALITY;
use hwlocality_sys::{
HWLOC_LOCAL_NUMANODE_FLAG_ALL, HWLOC_LOCAL_NUMANODE_FLAG_LARGER_LOCALITY,
HWLOC_LOCAL_NUMANODE_FLAG_SMALLER_LOCALITY, HWLOC_LOCATION_TYPE_CPUSET,
HWLOC_LOCATION_TYPE_OBJECT, HWLOC_MEMATTR_FLAG_HIGHER_FIRST, HWLOC_MEMATTR_FLAG_LOWER_FIRST,
HWLOC_MEMATTR_FLAG_NEED_INITIATOR, HWLOC_MEMATTR_ID_BANDWIDTH, HWLOC_MEMATTR_ID_CAPACITY,
HWLOC_MEMATTR_ID_LATENCY, HWLOC_MEMATTR_ID_LOCALITY, hwloc_const_topology_t,
hwloc_local_numanode_flag_e, hwloc_location, hwloc_location_u, hwloc_memattr_flag_e,
hwloc_memattr_id_t, hwloc_obj,
};
#[cfg(feature = "hwloc-2_8_0")]
use hwlocality_sys::{
HWLOC_MEMATTR_ID_READ_BANDWIDTH, HWLOC_MEMATTR_ID_READ_LATENCY,
HWLOC_MEMATTR_ID_WRITE_BANDWIDTH, HWLOC_MEMATTR_ID_WRITE_LATENCY,
};
use libc::{EBUSY, EINVAL, ENOENT};
#[allow(unused)]
#[cfg(test)]
use similar_asserts::assert_eq;
use std::{
collections::{HashMap, HashSet},
ffi::{CStr, c_int, c_uint, c_ulong},
fmt::{self, Debug},
hash::Hash,
mem::MaybeUninit,
ptr::{self, NonNull},
};
use thiserror::Error;
/// # Comparing memory node attributes for finding where to allocate on
///
/// Platforms with heterogeneous memory require ways to decide whether a buffer
/// should be allocated on "fast" memory (such as HBM), "normal" memory (DDR) or
/// even "slow" but large-capacity memory (non-volatile memory). These memory
/// nodes are called "Targets" while the CPU accessing them is called the
/// "Initiator". Access performance depends on their locality (NUMA platforms)
/// as well as the intrinsic performance of the targets (heterogeneous platforms).
///
/// The following attributes describe the performance of memory accesses from an
/// Initiator to a memory Target, for instance their latency or bandwidth.
/// Initiators performing these memory accesses are usually some PUs or Cores
/// (described as a CPU set). Hence a Core may choose where to allocate a memory
/// buffer by comparing the attributes of different target memory nodes nearby.
///
/// There are also some attributes that are system-wide. Their value does not
/// depend on a specific initiator performing an access. The memory node
/// capacity is an example of such attribute without initiator.
///
/// One way to use this API is to start with a cpuset describing the Cores where
/// a program is bound. The best target NUMA node for allocating memory in this
/// program on these Cores may be obtained by passing this cpuset as an
/// initiator to [`MemoryAttribute::best_target()`] with the relevant
/// memory attribute. For instance, if the code is latency limited, use the
/// Latency attribute.
///
/// A more flexible approach consists in getting the list of local NUMA nodes by
/// passing this cpuset to [`Topology::local_numa_nodes()`]. Attribute values
/// for these nodes, if any, may then be obtained with
/// [`MemoryAttribute::value()`] and manually compared with the desired criteria.
///
/// Memory attributes are also used internally to build Memory Tiers which
/// provide an easy way to distinguish NUMA nodes of different kinds.
///
#[cfg_attr(
feature = "hwloc-2_12_0",
doc = "Beside tiers, hwloc defines a set of \"default\" nodes where normal memory"
)]
#[cfg_attr(feature = "hwloc-2_12_0", doc = "allocations should be made from (see")]
#[cfg_attr(
feature = "hwloc-2_12_0",
doc = "[`get_default_nodeset()`](Topology::get_default_nodeset)). This is also"
)]
#[cfg_attr(
feature = "hwloc-2_12_0",
doc = "useful for dividing the machine into a set of non-overlapping NUMA domains,"
)]
#[cfg_attr(
feature = "hwloc-2_12_0",
doc = "for instance for binding tasks per domain."
)]
#[cfg_attr(feature = "hwloc-2_12_0", doc = "")]
/// The API also supports specific objects as initiator, but it is currently not
/// used internally by hwloc. Users may for instance use it to provide custom
/// performance values for host memory accesses performed by GPUs.
/// The interface actually also accepts targets that are not NUMA nodes.
//
// --- Implementation details ---
//
// Upstream docs: https://hwloc.readthedocs.io/en/stable/group__hwlocality__memattrs.html
impl Topology {
/// Identifier of the memory attribute with the given name
///
/// Note that a number of predefined memory attributes have hard-coded
/// identifiers and need not be queried by name at runtime, see the
/// constructors of [`MemoryAttribute`] for more information.
///
/// # Errors
///
/// - [`NulError`] if `name` contains NUL chars.
#[doc(alias = "hwloc_memattr_get_by_name")]
pub fn memory_attribute_named(
&self,
name: &str,
) -> Result<Option<MemoryAttribute<'_>>, NulError> {
let name = LibcString::new(name)?;
let mut id = MaybeUninit::uninit();
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - name is trusted to be a valid C string (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - Per documentation, id is a pure out parameter that hwloc
// does not read
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_get_by_name", || unsafe {
hwlocality_sys::hwloc_memattr_get_by_name(self.as_ptr(), name.borrow(), id.as_mut_ptr())
});
match res {
// SAFETY: If hwloc indicates success, it should have initialized id
// to a valid memory attribute ID
Ok(()) => Ok(Some(unsafe {
MemoryAttribute::wrap(self, id.assume_init())
})),
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => Ok(None),
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. Since there is only one expected errno value here,
// we'll interpret lack of errno as EINVAL on Windows.
Ok(None)
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
}
/// Find NUMA nodes that are local to some CPUs or [`TopologyObject`].
///
/// If `target` is given as a [`TopologyObject`], its CPU set is used to
/// find NUMA nodes with the corresponding locality. If the object does not
/// have a CPU set (e.g. I/O object), the CPU parent (where the I/O object
/// is attached) is used.
///
/// Some of these NUMA nodes may not have any memory attribute values and
/// hence not be reported as actual targets in other functions.
///
/// When an object CPU set is given as locality, for instance a Package, and
/// when `flags` contains both [`LocalNUMANodeFlags::LARGER_LOCALITY`] and
/// [`LocalNUMANodeFlags::SMALLER_LOCALITY`], the returned array corresponds
/// to the nodeset of that object.
///
/// # Errors
///
/// - [`ForeignInitiatorError`] if `target` refers to a [`TopologyObject`]
/// that does not belong to this topology; or if it refers to a [`CpuSet`]
/// that is empty or that contains CPUs that do not belong to the
/// topology's [`complete_cpuset()`](Topology::complete_cpuset).
#[allow(clippy::missing_errors_doc)]
#[doc(alias = "hwloc_get_local_numanode_objs")]
pub fn local_numa_nodes<'target>(
&self,
target: impl Into<NUMAInitiator<'target>>,
) -> Result<Vec<&TopologyObject>, HybridError<ForeignInitiatorError>> {
/// Polymorphized version of this function (avoids generics code bloat)
fn polymorphized<'self_>(
self_: &'self_ Topology,
target: NUMAInitiator<'_>,
) -> Result<Vec<&'self_ TopologyObject>, HybridError<ForeignInitiatorError>> {
// Prepare to call hwloc
// SAFETY: Will only be used before returning from this function
let (location, flags) = unsafe { target.as_checked_raw(self_)? };
let mut nr = 0;
let call_ffi = |nr_mut, out_ptr| {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - The NUMAInitiator API is designed in such a way
// that an invalid (location, flags) tuple cannot
// happen.
// - nr_mut and out_ptr are call site dependent, see below.
errors::call_hwloc_zero_or_minus1("hwloc_get_local_numanode_objs", || unsafe {
let location_ptr: *const hwloc_location = location
.as_ref()
.map_or(ptr::null(), |location_ref| location_ref);
hwlocality_sys::hwloc_get_local_numanode_objs(
self_.as_ptr(),
location_ptr,
nr_mut,
out_ptr,
flags.bits(),
)
})
.map_err(HybridError::Hwloc)
};
// Query node count
// SAFETY: A null output is allowed, and 0 elements is the correct way
// to model it in the "nr" parameter. This will set nr to the
// actual buffer size we need to allocate.
call_ffi(&raw mut nr, ptr::null_mut())?;
let len = int::expect_usize(nr);
// Allocate storage and fill node list
let mut out = vec![ptr::null(); len];
let old_nr = nr;
// SAFETY: out is a valid buffer of size len, which is just nr as usize
call_ffi(&raw mut nr, out.as_mut_ptr())?;
assert_eq!(old_nr, nr, "Inconsistent node count from hwloc");
// Translate node pointers into node references
Ok(out
.into_iter()
.map(|ptr| {
assert!(!ptr.is_null(), "Invalid NUMA node pointer from hwloc");
// SAFETY: We trust that if hwloc emits a non-null pointer,
// it is valid, bound to the topology's lifetime,
// and points to a valid target.
unsafe { (&*ptr).as_newtype() }
})
.collect())
}
polymorphized(self, target.into())
}
/// Set of default NUMA nodes
///
/// In machines with heterogeneous memory, some NUMA nodes are
/// considered the default ones, i.e. where basic allocations should be made
/// from. These are usually DRAM nodes.
///
/// Other nodes may be reserved for specific use (I/O device memory, e.g.
/// GPU memory), small but high performance (HBM), large but slow memory
/// (NVM), etc. Buffers should usually not be allocated from there unless
/// explicitly required.
///
/// This function returns the [`NodeSet`] of NUMA nodes considered default.
///
/// It is guaranteed that these nodes have non-intersecting CPU sets, i.e.
/// cores may not have multiple local NUMA nodes anymore. Hence this may be
/// used to iterate over the platform divided into separate NUMA localities,
/// for instance for binding one task per NUMA domain.
///
/// Any core that had some local NUMA node(s) in the initial topology should
/// still have one in the default nodeset. Corner cases where this would be
/// wrong consist in asymmetric platforms with missing DRAM nodes, or
/// topologies that were already restricted to less NUMA nodes.
///
/// The returned nodeset may be passed to [`TopologyEditor::restrict()`] to
/// remove all non-default nodes from the topology. The resulting topology
/// will be easier to use when iterating over (now homogeneous) NUMA nodes.
///
/// The heuristics for finding default nodes relies on memory tiers and
/// subtypes as well as the assumption that hardware vendors list default
/// nodes first in hardware tables.
///
/// The returned nodeset usually contains all nodes from a single
/// memory tier, likely the DRAM one.
///
/// The returned nodeset is included in the list of available nodes
/// returned by [`nodeset()`](Topology::nodeset). It is
/// strictly smaller if the machine has heterogeneous memory.
///
/// The heuristics may return a suboptimal set of nodes if hwloc
/// could not guess memory types and/or if some default nodes were
/// removed earlier from the topology (e.g. with
/// [`TopologyEditor::restrict()`]).
#[allow(clippy::missing_errors_doc)]
#[cfg(feature = "hwloc-2_12_0")]
pub fn get_default_nodeset(&self) -> Result<NodeSet, RawHwlocError> {
let mut result = NodeSet::new();
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - Bitmaps are trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted to keep *mut parameters in a
// valid state unless stated otherwise
// - flags are set to zero as directed
errors::call_hwloc_zero_or_minus1("hwloc_topology_get_default_nodeset", || unsafe {
hwlocality_sys::hwloc_topology_get_default_nodeset(
self.as_ptr(),
result.as_mut_ptr(),
0,
)
})
.map(|()| result)
}
/// Dump the values of all memory attributes
pub(crate) fn dump_memory_attributes(&self) -> MultiAttributeDump<'_> {
MultiAttributeDump::new(self)
}
}
/// Dump of memory attributes, used to implement topology Debug and comparison
#[derive(Clone)]
pub(crate) struct MultiAttributeDump<'topology>(Vec<AttributeDump<'topology>>);
//
impl<'topology> MultiAttributeDump<'topology> {
/// Dump all memory attributes
fn new(topology: &'topology Topology) -> Self {
Self(
MemoryAttribute::all(topology)
.map(AttributeDump::new)
.collect(),
)
}
/// Truth that this dump contains the same data as another dump, assuming
/// both dumps originate from related topologies.
///
/// By related, we mean that `other` should either originate from the same
/// [`Topology`] as `self`, or from a (possibly modified) clone of that
/// topology, which allows us to use object global persistent indices as
/// object identifiers.
///
/// Comparing dumps from unrelated topologies will yield an unpredictable
/// boolean value.
pub(crate) fn eq_modulo_topology(&self, other: &Self) -> bool {
if self.0.len() != other.0.len() {
return false;
}
self.0
.iter()
.zip(&other.0)
.all(|(dump1, dump2)| dump1.eq_modulo_topology(dump2))
}
}
//
impl Debug for MultiAttributeDump<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut attr_to_dump = f.debug_map();
for attr_dump in &self.0 {
attr_to_dump.entry(&attr_dump.attribute, &attr_dump.targets);
}
attr_to_dump.finish()
}
}
/// Dump of a specific memory attribute
#[derive(Clone, Debug)]
struct AttributeDump<'topology> {
/// Attribute being dumped
attribute: MemoryAttribute<'topology>,
/// Data dump for each known attribute target
targets: AttributeDumpTargets<'topology>,
}
//
impl<'topology> AttributeDump<'topology> {
/// Dump the value of the attribute for each registered target
fn new(attribute: MemoryAttribute<'topology>) -> Self {
Self {
attribute,
targets: AttributeDumpTargets::new(attribute),
}
}
/// Truth that this dump contains the same data as another dump, assuming
/// both dumps originate from related topologies.
///
/// By related, we mean that `other` should either originate from the same
/// [`Topology`] as `self`, or from a (possibly modified) clone of that
/// topology, which allows us to use object global persistent indices as
/// object identifiers.
///
/// Comparing dumps from unrelated topologies will yield an unpredictable
/// boolean value.
fn eq_modulo_topology(&self, other: &Self) -> bool {
if self.attribute.id != other.attribute.id {
return false;
}
self.targets.eq_modulo_topology(&other.targets)
}
}
/// `targets` field of `AttributeDump`
///
/// Needs to be its own struct due to design limitations of the
/// `Debug`/`Formatter` machinery.
#[derive(Clone)]
struct AttributeDumpTargets<'topology>(Vec<TargetAttributeDump<'topology>>);
//
impl<'topology> AttributeDumpTargets<'topology> {
/// Dump the value of the attribute for each target on the system
fn new(attribute: MemoryAttribute<'topology>) -> Self {
let (targets, _values) = attribute
.targets(None)
.expect("targets() should never panic with a None input");
Self(
targets
.into_iter()
.map(|target| TargetAttributeDump::new(attribute, target))
.collect(),
)
}
/// Truth that this dump contains the same data as another dump, assuming
/// both dumps originate from related topologies.
///
/// By related, we mean that `other` should either originate from the same
/// [`Topology`] as `self`, or from a (possibly modified) clone of that
/// topology, which allows us to use object global persistent indices as
/// object identifiers.
///
/// Comparing dumps from unrelated topologies will yield an unpredictable
/// boolean value.
fn eq_modulo_topology(&self, other: &Self) -> bool {
if self.0.len() != other.0.len() {
return false;
}
self.0
.iter()
.zip(&other.0)
.all(|(dump1, dump2)| dump1.eq_modulo_topology(dump2))
}
}
//
impl Debug for AttributeDumpTargets<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut targets_to_initiators = f.debug_map();
for target_dump in &self.0 {
targets_to_initiators.entry(&target_dump.target, &target_dump.initiators_and_values);
}
targets_to_initiators.finish()
}
}
/// Dump of a specific memory attribute for a specific target
#[derive(Clone, Debug)]
struct TargetAttributeDump<'topology> {
/// Target for which the memory attribute values were queried
target: &'topology TopologyObject,
/// Result of the memory attribute value query
initiators_and_values: InitiatorsAndValues<'topology>,
}
//
impl<'topology> TargetAttributeDump<'topology> {
/// Dump the value of the attribute for a specific target
///
/// # Panics
///
/// Expects `target` to belong to the same topology as `attribute`.
fn new(attribute: MemoryAttribute<'topology>, target: &'topology TopologyObject) -> Self {
Self {
target,
initiators_and_values: InitiatorsAndValues::new(attribute, target),
}
}
/// Truth that this dump contains the same data as another dump, assuming
/// both dumps originate from related topologies.
///
/// By related, we mean that `other` should either originate from the same
/// [`Topology`] as `self`, or from a (possibly modified) clone of that
/// topology, which allows us to use object global persistent indices as
/// object identifiers.
///
/// Comparing dumps from unrelated topologies will yield an unpredictable
/// boolean value.
pub(crate) fn eq_modulo_topology(&self, other: &Self) -> bool {
if self.target.global_persistent_index() != other.target.global_persistent_index() {
return false;
}
self.initiators_and_values
.eq_modulo_topology(&other.initiators_and_values)
}
}
/// `initiators_and_values` field of `TargetAttributeDump`
///
/// Needs to be its own struct due to design limitations of the
/// `Debug`/`Formatter` machinery.
#[derive(Clone)]
enum InitiatorsAndValues<'topology> {
/// Has no initiator => Single attribute value for this target
NoInitiator(u64),
/// Has initiators => One attribute value per initiator for this target
HasInitiators {
/// Initiators for this target
initiators: Vec<Initiator<'topology>>,
/// Values for this target (same length)
values: Vec<u64>,
},
}
//
impl<'topology> InitiatorsAndValues<'topology> {
/// Dump the value of the attribute for a specific target
///
/// # Panics
///
/// Expects `target` to belong to the same topology as `attribute`.
fn new(attribute: MemoryAttribute<'topology>, target: &'topology TopologyObject) -> Self {
if attribute
.flags()
.contains(MemoryAttributeFlags::NEED_INITIATOR)
{
attribute
.initiators(target)
.map(|(initiators, values)| Self::HasInitiators { initiators, values })
.expect(
"local initiator provided when NEED_INITIATOR, \
target is local & valid, no other known error case",
)
} else {
attribute
.value(None, target)
.expect(
"initiator not provided when !NEED_INITIATOR, \
target is local & valid, no other known error case",
)
.map(Self::NoInitiator)
.expect("initiator from this attribute => should get a value")
}
}
/// Truth that this dump contains the same data as another dump, assuming
/// both dumps originate from related topologies.
///
/// By related, we mean that `other` should either originate from the same
/// [`Topology`] as `self`, or from a (possibly modified) clone of that
/// topology, which allows us to use object global persistent indices as
/// object identifiers.
///
/// Comparing dumps from unrelated topologies will yield an unpredictable
/// boolean value.
pub(crate) fn eq_modulo_topology(&self, other: &Self) -> bool {
// Compare presence of initiators
let (initiators1, values1, initiators2, values2) = match (self, other) {
// Both have no initiator => Value should match
(Self::NoInitiator(val1), Self::NoInitiator(val2)) => return val1 == val2,
// Attributes don't agree on this matter => Not equal
(Self::NoInitiator(_), Self::HasInitiators { .. })
| (Self::HasInitiators { .. }, Self::NoInitiator(_)) => return false,
// Both have initiators => Need more tests
#[cfg(not(tarpaulin_include))]
(
Self::HasInitiators {
initiators: initiators1,
values: values1,
},
Self::HasInitiators {
initiators: initiators2,
values: values2,
},
) => (initiators1, values1, initiators2, values2),
};
// Check initiator/length matches, then compare length
#[cfg(not(tarpaulin_include))]
assert_eq!(
initiators1.len(),
values1.len(),
"Should have the same amount of initiators and values"
);
#[cfg(not(tarpaulin_include))]
assert_eq!(
initiators2.len(),
values2.len(),
"Should have the same amount of initiators and values"
);
if initiators1.len() != initiators2.len() {
return false;
}
// If length matches, compare contents
(initiators1.iter().zip(values1))
.zip(initiators2.iter().zip(values2))
.all(|((i1, v1), (i2, v2))| {
// Values should match
if v1 != v2 {
return false;
}
// Initiator type and content should match
match (i1, i2) {
(Initiator::CpuSet(set1), Initiator::CpuSet(set2)) => set1 == set2,
(Initiator::Object(obj1), Initiator::Object(obj2)) => {
obj1.global_persistent_index() == obj2.global_persistent_index()
}
(Initiator::CpuSet(_), Initiator::Object(_))
| (Initiator::Object(_), Initiator::CpuSet(_)) => false,
}
})
}
}
//
impl Debug for InitiatorsAndValues<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Handle initiator-less attributes
let (initiators, values) = match self {
Self::NoInitiator(value) => return write!(f, "{value}"),
Self::HasInitiators { initiators, values } => (initiators, values),
};
// Handle initiator-ful attributes
let mut initiator_to_value = f.debug_map();
#[cfg(not(tarpaulin_include))]
assert_eq!(
initiators.len(),
values.len(),
"Should have the same amount of initiators and values"
);
for (initiator, value) in initiators.iter().zip(values) {
initiator_to_value.entry(&initiator, &value);
}
initiator_to_value.finish()
}
}
/// # Managing memory attributes
//
// --- Implementation details ---
//
// Upstream docs: https://hwloc.readthedocs.io/en/stable/group__hwlocality__memattrs__manage.html
impl<'topology> TopologyEditor<'topology> {
/// Register a new memory attribute
///
/// # Errors
///
/// - [`BadFlags`] if `flags` does not contain exactly one of the
/// [`HIGHER_IS_BEST`] and [`LOWER_IS_BEST`] flags.
/// - [`NameContainsNul`] if `name` contains NUL chars.
/// - [`NameTaken`] if another attribute called `name` already exists.
///
/// [`BadFlags`]: RegisterError::BadFlags
/// [`HIGHER_IS_BEST`]: [`MemoryAttributeFlags::HIGHER_IS_BEST`]
/// [`LOWER_IS_BEST`]: [`MemoryAttributeFlags::LOWER_IS_BEST`]
/// [`NameContainsNul`]: RegisterError::NameContainsNul
/// [`NameTaken`]: RegisterError::NameTaken
#[doc(alias = "hwloc_memattr_register")]
pub fn register_memory_attribute(
&mut self,
name: &str,
flags: MemoryAttributeFlags,
) -> Result<MemoryAttributeBuilder<'_, 'topology>, RegisterError> {
if !flags.is_valid() {
return Err(flags.into());
}
let libc_name = LibcString::new(name)?;
let mut id = hwloc_memattr_id_t::MAX;
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - libc_name is trusted to be a valid C string (type
// invariant)
// - hwloc ops are trusted not to modify *const parameters
// - hwloc ops are trusted to keep *mut parameters in a
// valid state unless stated otherwise
// - flags are validated to be correct
// - id is an out-parameter, so it can take any input value
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_register", || unsafe {
hwlocality_sys::hwloc_memattr_register(
self.topology_mut_ptr(),
libc_name.borrow(),
flags.bits(),
&raw mut id,
)
});
let handle_ebusy = || Err(RegisterError::NameTaken(name.into()));
match res {
Ok(()) => Ok(MemoryAttributeBuilder {
editor: self,
flags,
id,
name: name.into(),
}),
Err(RawHwlocError {
errno: Some(Errno(EBUSY)),
..
}) => handle_ebusy(),
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. Since there is only one expected errno value here,
// we'll interpret lack of errno as EBUSY on Windows.
handle_ebusy()
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
}
}
/// Error returned when trying to create an memory attribute
#[cfg_attr(windows, allow(variant_size_differences))]
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum RegisterError {
/// Provided `name` contains NUL chars
#[error("memory attribute name can't contain NUL chars")]
NameContainsNul,
/// Another attribute already uses this name
#[error("there is already a memory attribute named \"{0}\"")]
NameTaken(Box<str>),
/// Specified flags are not correct
///
/// You must specify exactly one of the [`HIGHER_IS_BEST`] and
/// [`LOWER_IS_BEST`] flags.
///
/// [`HIGHER_IS_BEST`]: [`MemoryAttributeFlags::HIGHER_IS_BEST`]
/// [`LOWER_IS_BEST`]: [`MemoryAttributeFlags::LOWER_IS_BEST`]
#[error(transparent)]
BadFlags(#[from] FlagsError<MemoryAttributeFlags>),
}
//
impl From<NulError> for RegisterError {
fn from(_: NulError) -> Self {
Self::NameContainsNul
}
}
//
impl From<MemoryAttributeFlags> for RegisterError {
fn from(value: MemoryAttributeFlags) -> Self {
Self::BadFlags(value.into())
}
}
/// Mechanism to configure a memory attribute
//
// --- Implementation details ---
//
// # Safety
//
// `id` must be a valid new memory attribute ID from `hwloc_memattr_register()`
#[derive(Debug)]
pub struct MemoryAttributeBuilder<'editor, 'topology> {
/// Underlying [`TopologyEditor`]
editor: &'editor mut TopologyEditor<'topology>,
/// Flags which this memory attribute was registered with
flags: MemoryAttributeFlags,
/// ID that `hwloc_memattr_register` allocated to this memory attribute
id: hwloc_memattr_id_t,
/// Name of this memory attribute
name: Box<str>,
}
//
impl MemoryAttributeBuilder<'_, '_> {
/// Set attribute values for (initiator, target node) pairs
///
/// Given a read-only view of the underlying [`Topology`], the provided
/// `find_values` callback should conceptually extract a list of
/// `(initiator, target, value)` tuples if this memory attribute has
/// initiators (flag [`MemoryAttributeFlags::NEED_INITIATOR`] was set at
/// attribute registration time), and a list of `(target, value)` tuples
/// if the attribute has no initiators.
///
/// However, for efficiency reasons, the callback does not literally return
/// a list of ternary tuples with an optional initiator member, as this
/// would require one initiator check per attribute value. Instead, the
/// callback returns a list of `(target, value)` pairs along with an
/// optional list of initiators. If a list of initiators is provided, it
/// must be as long as the provided list of `(target, value)` pairs.
///
/// Initiators should be specified as [`CpuSet`](Initiator::CpuSet) when
/// referring to accesses performed by CPU cores. Any CPU in the `CpuSet` is
/// then considered an initiator for this target. In other words, if you
/// later query the attribute using a non-empty subset of this `CpuSet` as
/// the initiator, you will get the same result as if you queried using the
/// full `CpuSet`.
///
/// The [`Object`](Initiator::Object) initiator type is currently unused
/// internally by hwloc, but users may for instance use it to provide custom
/// information about host memory accesses performed by GPUs. However, as of
/// hwloc v2.12.1, it cannot be used because of an hwloc bug that may cause
/// memory attributes with object initiators to be corrupted and has no
/// obvious workaround. Until [the clean fix for this
/// bug](https://github.com/open-mpi/hwloc/pull/725) lands into a stable
/// hwloc release, likely v3.0.0, using this initiator type is therefore
/// forbidden.
///
/// # Errors
///
/// - [`InconsistentDataLen`] if the number of provided initiators and
/// attribute values does not match
/// - [`OverlappingValues`] if there are multiple values that match for a
/// given (initiator, target) pair.
/// - [`ForeignInitiator`] if some of the provided initiators are
/// [`TopologyObject`]s that do not belong to this [`Topology`]; or are
/// `CpuSet`s that are empty or contain CPUs outside of the topology's
/// [`complete_cpuset()`](Topology::complete_cpuset).
/// - [`ForeignTarget`] if some of the provided targets are
/// [`TopologyObject`]s that do not belong to this [`Topology`]
/// - [`NeedInitiator`] if initiators were not specified for an attribute
/// that requires them
/// - [`UnwantedInitiator`] if initiators were specified for an attribute
/// that does not requires them
/// - [`ObjectInitiator`] if an initiator of the [`Object`] type is
/// specified on an hwloc version that does not properly support these.
///
/// [`ForeignInitiator`]: InitiatorInputError::ForeignInitiator
/// [`ForeignTarget`]: ValueInputError::ForeignTarget
/// [`InconsistentDataLen`]: ValueInputError::InconsistentDataLen
/// [`NeedInitiator`]: InitiatorInputError::NeedInitiator
/// [`Object`]: Initiator::Object
/// [`ObjectInitiator`]: ValueInputError::ObjectInitiator
/// [`OverlappingValues`]: ValueInputError::OverlappingValues
/// [`UnwantedInitiator`]: InitiatorInputError::UnwantedInitiator
#[doc(alias = "hwloc_memattr_set_value")]
pub fn set_values(
&mut self,
find_values: impl FnOnce(&Topology) -> (Option<Vec<Initiator<'_>>>, Vec<(&TopologyObject, u64)>),
) -> Result<(), HybridError<ValueInputError>> {
/// Polymorphized subset of this function (reduces generics code bloat)
///
/// # Safety
///
/// - `initiators` must have just gone through the `as_checked_raw()`
/// validation process against this attribute's topology.
/// - `target_ptrs_and_values` must only contain pointers to objects
/// from this memory attribute's topology.
unsafe fn polymorphized(
self_: &mut MemoryAttributeBuilder<'_, '_>,
initiators: RawInitiators,
target_ptrs_and_values: RawTargetsAndValues,
) -> Result<(), HybridError<ValueInputError>> {
// Massage initiators into an iterator of what hwloc wants
let initiator_ptrs = initiators
.iter()
.flatten()
.map(|initiator_ref| {
let initiator_ptr: *const hwloc_location = initiator_ref;
initiator_ptr
})
.chain(std::iter::repeat_with(ptr::null));
// Set memory attribute values
for (initiator_ptr, (target_ptr, value)) in initiator_ptrs.zip(target_ptrs_and_values) {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted to keep *mut parameters in a
// valid state unless stated otherwise
// - id from hwloc_memattr_register is trusted to be valid
// - target_ptr was checked to belong to this topology
// - initiator_ptr was checked to belong to this topology
// - Flags must be 0 for now
// - Any attribute value is valid
errors::call_hwloc_zero_or_minus1("hwloc_memattr_set_value", || unsafe {
hwlocality_sys::hwloc_memattr_set_value(
self_.editor.topology_mut_ptr(),
self_.id,
target_ptr.as_ptr(),
initiator_ptr,
0,
value,
)
})
.map_err(HybridError::Hwloc)?;
}
Ok(())
}
// Run user callback
let topology = self.editor.topology();
let (initiators, targets_and_values) = find_values(topology);
// Check user inputs and get them closer to hwloc expectations
//
// SAFETY: Raw initiators and targets will be used before the end of
// this function, without tampering
let (initiators, target_ptrs_and_values) = unsafe {
Self::checked_set_values_inputs(
self.flags,
&self.name,
topology,
initiators.as_deref(),
&targets_and_values[..],
)?
};
// Invoke polymorphized subset with the results
// SAFETY: Initiators and target_ptrs_and_values have been checked to
// belong to this memory attribute's topology, and initiators
// have not been tampered with.
unsafe { polymorphized(self, initiators, target_ptrs_and_values) }
}
/// Check inputs to `set_values` for usage errors. If everything is alright,
/// return lower-level inputs for the polymorphic subset of `set_values`.
///
/// # Safety
///
/// The output values must be used before the end of the lifetime of the
/// `initiators` and `targets_and_values` inputs. Inputs should not have
/// been tampered with before this happens.
unsafe fn checked_set_values_inputs<'topology>(
flags: MemoryAttributeFlags,
name: &str,
topology: &'topology Topology,
initiators: Option<&[Initiator<'topology>]>,
targets_and_values: &[(&'topology TopologyObject, u64)],
) -> Result<(RawInitiators, RawTargetsAndValues), HybridError<ValueInputError>> {
// Set up output storage
let mut out_initiators = None;
let mut target_ptrs_and_values = Vec::with_capacity(targets_and_values.len());
/// Common code for adding a (target, value) pair + checking the target
macro_rules! try_push_target_and_value {
($target_and_value:expr) => {
let (target_ref, value): (&TopologyObject, u64) = $target_and_value;
if !topology.contains(target_ref) {
return Err(ValueInputError::ForeignTarget(target_ref.into()).into());
}
let target_ptr = NonNull::from(target_ref).as_inner();
target_ptrs_and_values.push((target_ptr, value));
};
}
// Case where the user provided initiators
if let Some(initiators) = &initiators {
// The memory attribute should call for an initiator
if !flags.contains(MemoryAttributeFlags::NEED_INITIATOR) {
return Err(ValueInputError::BadInitiators(
InitiatorInputError::UnwantedInitiator(name.into()),
)
.into());
}
// Object initiators should not be used on hwloc versions prior to
// v3 as those have a bug that make them return garbage initiator
// pointers when this initiator type is used (see
// https://github.com/open-mpi/hwloc/pull/725), resulting in memory
// unsafety that this safe binding cannot expose.
if cfg!(not(feature = "hwloc-3_0_0"))
&& initiators
.iter()
.any(|init| matches!(init, Initiator::Object(_)))
{
return Err(ValueInputError::ObjectInitiator.into());
}
// There should be as many initiators as there are targets/values
if initiators.len() != targets_and_values.len() {
return Err(ValueInputError::InconsistentDataLen.into());
}
// Iterate over (initiator, target, value) triplets
let mut initiator_targets = InitiatorTargetSet::new();
let out_initiators = out_initiators.insert(Vec::with_capacity(initiators.len()));
for (initiator, (target, value)) in
initiators.iter().zip(targets_and_values.iter().copied())
{
// Check for absence of (initiator, target) overlap
if !initiator_targets.insert(initiator, target) {
return Err(ValueInputError::OverlappingValues.into());
}
// Record initiator, target and value into output Vecs
out_initiators.push(
// SAFETY: Per function precondition
unsafe {
initiator.as_checked_raw(topology).map_err(|e| {
ValueInputError::BadInitiators(InitiatorInputError::ForeignInitiator(e))
})?
},
);
try_push_target_and_value!((target, value));
}
} else {
// Case where the user did not provide initiators. This is only
// valid if the memory attribute doesn't call for initiators
if flags.contains(MemoryAttributeFlags::NEED_INITIATOR) {
return Err(
ValueInputError::BadInitiators(InitiatorInputError::NeedInitiator(name.into()))
.into(),
);
}
// Record (target, value) pairs, tracking target uniqueness
let mut unique_target_ids = HashSet::with_capacity(targets_and_values.len());
for (target, value) in targets_and_values.iter().copied() {
if !unique_target_ids.insert(target.global_persistent_index()) {
return Err(ValueInputError::OverlappingValues.into());
}
try_push_target_and_value!((target, value));
}
}
Ok((out_initiators, target_ptrs_and_values))
}
}
/// Set of `(initiator, target)` pairs, used to detect duplicates
#[derive(Default)]
struct InitiatorTargetSet {
/// Mapping from targets which were seen before, to associated initiators
targets_to_initiators: HashMap<TopologyObjectID, InitiatorSet>,
}
//
impl InitiatorTargetSet {
/// Set up an `InitiatorTargetSet`
fn new() -> Self {
Self::default()
}
/// Insert an `(initiator, target)` pair, tell if this was a true insertion
/// (as opposed to some (initiator, target) pairs being already present)
fn insert(&mut self, initiator: &Initiator<'_>, target: &TopologyObject) -> bool {
let initiators = self
.targets_to_initiators
.entry(target.global_persistent_index())
.or_default();
initiators.insert(initiator)
}
}
//
/// Set of initiators for a particular target, used to detect duplicates
#[derive(Default)]
struct InitiatorSet {
/// CPUs that were used as initiators before
initiator_cpus: CpuSet,
/// Objects that were used as initiators before
initiator_objs: HashSet<TopologyObjectID>,
}
//
impl InitiatorSet {
/// Insert an initiator, tell if this was a true insertion (as opposed to
/// some initiators being already present in the set)
fn insert(&mut self, initiator: &Initiator<'_>) -> bool {
match initiator {
Initiator::CpuSet(set) => {
let result = !self.initiator_cpus.intersects(set.as_ref());
self.initiator_cpus |= set.as_ref();
result
}
Initiator::Object(obj) => self.initiator_objs.insert(obj.global_persistent_index()),
}
}
}
/// Predigested initiators for [`MemoryAttributeBuilder::set_values`]
type RawInitiators = Option<Vec<hwloc_location>>;
/// Predigested targets and values for [`MemoryAttributeBuilder::set_values`]
type RawTargetsAndValues = Vec<(NonNull<hwloc_obj>, u64)>;
/// Error returned by [`MemoryAttributeBuilder::set_values`] when the
/// `find_values` callback returns incorrect initiators or targets
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum ValueInputError {
/// The number of provided initiators does not match the number of attribute values
#[error("number of memory attribute values doesn't match number of initiators")]
InconsistentDataLen,
/// Multiple values match for some (initiator, target) pairs
#[error("multiple values match for some (initiator, target) pairs")]
OverlappingValues,
/// Specified initiators for these attribute values are not valid
#[error(transparent)]
BadInitiators(#[from] InitiatorInputError),
/// Some provided initiator have the `Object` type, which the currently
/// enabled set of hwlocs version does not properly support (need hwloc
/// v3.0.0 or newer)
#[error(
"some initiators are Objects, but you need to request hwloc v3+ via feature hwloc-3_0_0 for that"
)]
ObjectInitiator,
/// Some provided targets are [`TopologyObject`]s that do not belong to
/// the topology being modified
#[error("memory attribute target {0}")]
ForeignTarget(ForeignObjectError),
}
/// Generate [`MemoryAttribute`] constructors around predefined memory attribute
/// IDs from hwloc with minimal boilerplate
///
/// # Safety
///
/// IDs must be ID constants from hwloc
macro_rules! wrap_ids_unchecked {
(
$(
$(#[$attr:meta])*
$id:ident -> $constructor:ident
),*
) => {
$(
$(#[$attr])*
// FIXME: Not supported by rustdoc yet, see
// https://github.com/rust-lang/rust/issues/94180
//
// #[doc(alias = stringify!($id))]
pub const fn $constructor(topology: &'topology Topology) -> Self {
// SAFETY: Per macro precondition
unsafe { Self::wrap(topology, $id) }
}
)*
};
}
/// Memory attribute
///
/// May be either one of the predefined attributes (see associated const fns)
/// or a new attribute created using
/// [`TopologyEditor::register_memory_attribute()`].
//
// --- Implementation details ---
//
// # Safety
//
// `id` must be a valid identifier to a memory attribute known of `topology`.
#[derive(Copy, Clone)]
#[doc(alias = "hwloc_memattr_id_e")]
#[doc(alias = "hwloc_memattr_id_t")]
pub struct MemoryAttribute<'topology> {
/// Topology for which memory attribute is defined
topology: &'topology Topology,
/// Identifier of the memory attribute being manipulated
id: hwloc_memattr_id_t,
}
//
/// # Predefined memory attributes
impl<'topology> MemoryAttribute<'topology> {
wrap_ids_unchecked!(
/// Node capacity in bytes (see [`TopologyObject::total_memory()`])
///
/// This attribute involves no initiator.
///
/// Requires [`DiscoverySupport::numa_memory()`].
HWLOC_MEMATTR_ID_CAPACITY -> capacity,
/// Number of PUs in that locality (i.e. cpuset weight)
///
/// Smaller locality is better. This attribute involves no initiator.
///
/// Requires [`DiscoverySupport::pu_count()`].
HWLOC_MEMATTR_ID_LOCALITY -> locality,
/// Average bandwidth in MiB/s, as seen from the given initiator
///
/// This is the average bandwidth for read and write accesses. If the
/// platform provides individual read and write bandwidths but no
/// explicit average value, hwloc computes and returns the average.
HWLOC_MEMATTR_ID_BANDWIDTH -> bandwidth,
/// Read bandwidth in MiB/s, as seen from the given initiator
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_READ_BANDWIDTH -> read_bandwidth,
/// Write bandwidth in MiB/s, as seen from the given initiator
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_WRITE_BANDWIDTH -> write_bandwidth,
/// Average latency in nanoseconds, as seen from the given initiator
///
/// This is the average latency for read and write accesses. If the
/// platform value provides individual read and write latencies but no
/// explicit average, hwloc computes and returns the average.
HWLOC_MEMATTR_ID_LATENCY -> latency,
/// Read latency in nanoseconds, as seen from the given initiator
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_READ_LATENCY -> read_latency,
/// Write latency in nanoseconds, as seen from the given initiator
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_WRITE_LATENCY -> write_latency
// TODO: If you add new attributes, add support to static_flags and
// a matching MemoryAttribute constructor below
);
/// Enumerate all registered memory attributes
fn all(topology: &'topology Topology) -> impl Iterator<Item = Self> {
(0..).map_while(move |id| {
let mut flags = 0;
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - It has been stated by upstream that probing flags for
// an invalid ID is fine and will just lead to EINVAL
// - flags is an out parameter, its initial value doesn't matter
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_get_flags", || unsafe {
hwlocality_sys::hwloc_memattr_get_flags(topology.as_ptr(), id, &raw mut flags)
});
match res {
Ok(()) => Some(
// SAFETY: Checked id validity per upstream suggestion
unsafe { Self::wrap(topology, id) },
),
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => None,
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. Since there is only one expected errno value here,
// we'll interpret lack of errno as EINVAL on Windows.
None
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
})
}
}
//
/// # Memory attribute API
impl<'topology> MemoryAttribute<'topology> {
/// Extend an [`hwloc_memattr_id_t`] with topology access to enable method syntax
///
/// # Safety
///
/// `id` must be a valid memory attribute ID, corresponding either to one of
/// hwloc's predefined attributes or an attribute that was user-allocated
/// using `hwloc_memattr_register()`.
pub(crate) const unsafe fn wrap(topology: &'topology Topology, id: hwloc_memattr_id_t) -> Self {
Self { id, topology }
}
/// Name of this memory attribute
#[doc(alias = "hwloc_memattr_get_name")]
pub fn name(&self) -> &'topology CStr {
let mut name = ptr::null();
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - id is assumed to be valid (type invariant)
// - name is an out parameter, its initial value doesn't matter
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_get_name", || unsafe {
hwlocality_sys::hwloc_memattr_get_name(self.topology.as_ptr(), self.id, &raw mut name)
});
#[cfg(not(tarpaulin_include))]
let handle_einval =
|| unreachable!("MemoryAttribute should only hold valid attribute indices");
match res {
Ok(()) => {}
#[cfg(not(tarpaulin_include))]
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => handle_einval(),
#[cfg(not(tarpaulin_include))]
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. Since there is only one expected errno value here,
// we'll interpret lack of errno as EINVAL on Windows.
handle_einval()
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
#[cfg(not(tarpaulin_include))]
assert!(
!name.is_null(),
"Memory attributes should have non-NULL names"
);
// SAFETY: If hwloc does emit a non-NULL name in a successful query run,
// we trust that name to be a valid `char*` C string pointer
// bound to the topology's lifetime
unsafe { CStr::from_ptr(name) }
}
/// Flags of this memory attribute
#[doc(alias = "hwloc_memattr_get_flags")]
pub fn flags(&self) -> MemoryAttributeFlags {
let flags = Self::static_flags(self.id).unwrap_or_else(|| self.dynamic_flags());
assert!(flags.is_valid(), "hwloc emitted invalid flags");
flags
}
/// Tell attribute flags if known at compile time
fn static_flags(id: hwloc_memattr_id_t) -> Option<MemoryAttributeFlags> {
let bandwidth_flags =
Some(MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR);
let latency_flags =
Some(MemoryAttributeFlags::LOWER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR);
match id {
HWLOC_MEMATTR_ID_CAPACITY => Some(MemoryAttributeFlags::HIGHER_IS_BEST),
HWLOC_MEMATTR_ID_LOCALITY => Some(MemoryAttributeFlags::LOWER_IS_BEST),
HWLOC_MEMATTR_ID_BANDWIDTH => bandwidth_flags,
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_READ_BANDWIDTH | HWLOC_MEMATTR_ID_WRITE_BANDWIDTH => bandwidth_flags,
HWLOC_MEMATTR_ID_LATENCY => latency_flags,
#[cfg(feature = "hwloc-2_8_0")]
HWLOC_MEMATTR_ID_READ_LATENCY | HWLOC_MEMATTR_ID_WRITE_LATENCY => latency_flags,
_ => None,
}
}
/// Dynamically query this memory attribute's flags
fn dynamic_flags(&self) -> MemoryAttributeFlags {
let mut flags = 0;
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - id is assumed to be valid (type invariant)
// - flags is an out parameter, its initial value doesn't matter
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_get_flags", || unsafe {
hwlocality_sys::hwloc_memattr_get_flags(self.topology.as_ptr(), self.id, &raw mut flags)
});
#[cfg(not(tarpaulin_include))]
let handle_einval =
|| unreachable!("MemoryAttribute should only hold valid attribute indices");
match res {
Ok(()) => MemoryAttributeFlags::from_bits_retain(flags),
#[cfg(not(tarpaulin_include))]
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => handle_einval(),
#[cfg(not(tarpaulin_include))]
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. Since there is only one expected errno value here,
// we'll interpret lack of errno as EINVAL on Windows.
handle_einval()
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
}
/// Value of this attribute for a specific initiator and target NUMA node,
/// if any.
///
/// `initiator` should be specified if and only if this attribute has the
/// flag [`MemoryAttributeFlags::NEED_INITIATOR`].
///
/// The initiator should be a [`CpuSet`] when refering to accesses performed
/// by CPU cores. [`Initiator::Object`] is currently unused internally by
/// hwloc, but user-defined memory attributes may for instance use it to
/// provide custom information about host memory accesses performed by GPUs.
///
/// For certain attributes, `target_node` should satisfy extra propreties:
///
/// - It must be a NUMA node when `self` is [`MemoryAttribute::capacity()`]
/// - It must have a CPU set when `self` is [`MemoryAttribute::locality()`]
///
/// If this is not true, the [`InvalidTarget`] error will be returned.
///
/// # Errors
///
/// - [`ForeignInitiator`] if the `initiator` parameter was set to a
/// [`TopologyObject`] that does not belong to this topology; or a
/// [`CpuSet`] that is empty or contains CPUs outside of the topology's
/// [`complete_cpuset()`](Topology::complete_cpuset)
/// - [`ForeignTarget`] if the `target_node` object does not belong to this
/// topology
/// - [`InvalidTarget`] if `target_node` is not a valid target for this
/// attribute.
/// - [`NeedInitiator`] if no `initiator` was provided but this memory
/// attribute needs one
/// - [`UnwantedInitiator`] if an `initiator` was provided but this memory
/// attribute doesn't need one
///
/// [`ForeignInitiator`]: InitiatorInputError::ForeignInitiator
/// [`ForeignTarget`]: ValueQueryError::ForeignTarget
/// [`InvalidTarget`]: ValueQueryError::InvalidTarget
/// [`NeedInitiator`]: InitiatorInputError::NeedInitiator
/// [`UnwantedInitiator`]: InitiatorInputError::UnwantedInitiator
#[doc(alias = "hwloc_memattr_get_value")]
pub fn value(
&self,
initiator: Option<Initiator<'_>>,
target_node: &TopologyObject,
) -> Result<Option<u64>, HybridError<ValueQueryError>> {
// Check and translate initiator argument
// SAFETY: Will only be used before returning from this function
let initiator = unsafe {
self.checked_initiator(initiator.as_ref(), false)
.map_err(|err| HybridError::Rust(ValueQueryError::BadInitiator(err)))?
};
// Check target argument
if !self.topology.contains(target_node) {
return Err(ValueQueryError::ForeignTarget(target_node.into()).into());
}
if (self.id == HWLOC_MEMATTR_ID_CAPACITY
&& target_node.object_type() != ObjectType::NUMANode)
|| (self.id == HWLOC_MEMATTR_ID_LOCALITY && target_node.cpuset().is_none())
{
return Err(
ValueQueryError::InvalidTarget(target_node.global_persistent_index()).into(),
);
}
// Run the query
let mut value = u64::MAX;
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - id is trusted to be valid (type invariant)
// - target_node has been checked to come from this topology
// - initiator has been checked to come from this topology and
// to be NULL if and only if the attribute has no initiator
// - flags must be 0
// - Value is an out parameter, its initial value isn't read
let res = errors::call_hwloc_zero_or_minus1("hwloc_memattr_get_value", || unsafe {
let initiator_ptr: *const hwloc_location = initiator
.as_ref()
.map_or(ptr::null(), |initiator_ref| initiator_ref);
hwlocality_sys::hwloc_memattr_get_value(
self.topology.as_ptr(),
self.id,
target_node.as_inner(),
initiator_ptr,
0,
&raw mut value,
)
});
match res {
Ok(()) => Ok(Some(value)),
// We should have handled all sources of EINVAL besides calling
// value() with an (initiator, target_node) for which there is no
// memory attribute value, so returning None feels appropriate here.
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => Ok(None),
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. But only EINVAL is expected here so we're fine.
Ok(None)
}
Err(other_err) => Err(HybridError::Hwloc(other_err)),
}
}
/// Best target node and associated attribute value, if any, for a given initiator
///
/// The notes on initiator semantics in [`MemoryAttribute::value()`] also
/// apply to this function.
///
/// If multiple targets have the same attribute values, only one is returned
/// (and there is no way to clarify how that one is chosen). Applications
/// that want to detect targets with identical/similar values, or that want
/// to look at values for multiple attributes, should rather get all values
/// using [`MemoryAttribute::value()`] and manually select the target they
/// consider the best.
///
/// # Errors
///
/// - [`ForeignInitiator`] if the `initiator` parameter was set to a
/// [`TopologyObject`] that does not belong to this topology; or a
/// [`CpuSet`] that is empty or contains CPUs outside of the topology's
/// [`complete_cpuset()`](Topology::complete_cpuset)
/// - [`NeedInitiator`] if no `initiator` was provided but this memory
/// attribute needs one
/// - [`UnwantedInitiator`] if an `initiator` was provided but this memory
/// attribute doesn't need one
///
/// [`ForeignInitiator`]: InitiatorInputError::ForeignInitiator
/// [`NeedInitiator`]: InitiatorInputError::NeedInitiator
/// [`UnwantedInitiator`]: InitiatorInputError::UnwantedInitiator
#[doc(alias = "hwloc_memattr_get_best_target")]
pub fn best_target(
&self,
initiator: Option<Initiator<'_>>,
) -> Result<Option<(&'topology TopologyObject, u64)>, InitiatorInputError> {
// Validate the query
// SAFETY: Will only be used before returning from this function,
let initiator = unsafe { self.checked_initiator(initiator.as_ref(), false)? };
// Run the query
let mut best_target = ptr::null();
// SAFETY: - hwloc_memattr_get_best_target is a "best X" query
// - Parameters are forwarded in the right order
// - initiator has been checked to come from this topology and
// to be NULL if and only if the attribute has no initiator
// - best_target is an out parameter, its initial value isn't read
let opt = unsafe {
self.get_best(
"hwloc_memattr_get_best_target",
|topology, attribute, flags, value| {
let initiator_ptr: *const hwloc_location = initiator
.as_ref()
.map_or(ptr::null(), |initiator_ref| initiator_ref);
hwlocality_sys::hwloc_memattr_get_best_target(
topology,
attribute,
initiator_ptr,
flags,
&raw mut best_target,
value,
)
},
)
};
// Convert target node into a safe high-level form
// SAFETY: Target originates from a query against this topology
Ok(opt.map(|value| (unsafe { self.encapsulate_target_node(best_target) }, value)))
}
/// Best initiator and associated attribute value, if any, for a given target node
///
/// If multiple initiators have the same attribute values, only one is
/// returned (and there is no way to clarify how that one is chosen).
/// Applications that want to detect initiators with identical/similar
/// values, or that want to look at values for multiple attributes, should
/// rather get all values using [`MemoryAttribute::value()`] and manually
/// select the initiator they consider the best.
///
/// # Errors
///
/// - [`NoInitiator`] if this memory attribute doesn't have initiators
/// - [`ForeignTarget`] if `target` does not belong to this topology
///
/// [`ForeignTarget`]: InitiatorQueryError::ForeignTarget
/// [`NoInitiator`]: InitiatorQueryError::NoInitiator
#[doc(alias = "hwloc_memattr_get_best_initiator")]
pub fn best_initiator(
&self,
target: &TopologyObject,
) -> Result<Option<(Initiator<'topology>, u64)>, InitiatorQueryError> {
// Validate the query
self.check_initiator_query_target(target)?;
// Run the query
// SAFETY: This is an out parameter, initial value won't be read
let mut best_initiator = INVALID_LOCATION;
// SAFETY: - hwloc_memattr_get_best_initiator is a "best X" query
// - Parameters are forwarded in the right order
// - target node has been checked to come from this topology
// - best_initiator is an out parameter, its initial value isn't read
let opt = unsafe {
self.get_best(
"hwloc_memattr_get_best_initiator",
|topology, attribute, flags, value| {
hwlocality_sys::hwloc_memattr_get_best_initiator(
topology,
attribute,
target.as_inner(),
flags,
&raw mut best_initiator,
value,
)
},
)
};
// Convert initiator into a safe high-level form
// SAFETY: Initiator originates from a query against this topology
opt.map(|value| Ok((unsafe { self.encapsulate_initiator(best_initiator) }, value)))
.transpose()
}
/// Target NUMA nodes that have some values for a given attribute, along
/// with the associated values if possible.
///
/// An `initiator` may only be specified if this attribute has the flag
/// [`MemoryAttributeFlags::NEED_INITIATOR`]. In that case, specifying an
/// initiator is optional and does two things:
///
/// 1. It acts as a filter to only report targets that have a value for this
/// initiator.
/// 2. It reports the memory attribute values for each listed target and
/// this initiator. Otherwise no values are reported, i.e. the output
/// `Option<Vec<u64>>` is `None`.
///
/// The initiator should be a [`CpuSet`] when refering to accesses performed
/// by CPU cores. [`Initiator::Object`] is currently unused internally by
/// hwloc, but user-defined memory attributes may for instance use it to
/// provide custom information about host memory accesses performed by GPUs.
///
/// In the case of memory attributes that have no initiators, `initiator`
/// should be `None`, and memory attribute values will always be reported.
///
/// This function is meant for tools and debugging (listing internal
/// information) rather than for application queries. Applications should
/// rather select useful NUMA nodes with [`Topology::local_numa_nodes()`]
/// and then look at their attribute values.
///
/// # Errors
///
/// - [`ForeignInitiator`] if the `initiator` parameter was set to a
/// [`TopologyObject`] that does not belong to this topology; or a
/// [`CpuSet`] that is empty or contains CPUs outside of the topology's
/// [`complete_cpuset()`](Topology::complete_cpuset)
/// - [`UnwantedInitiator`] if an `initiator` was provided but this memory
/// attribute doesn't need one
///
/// [`ForeignInitiator`]: InitiatorInputError::ForeignInitiator
/// [`UnwantedInitiator`]: InitiatorInputError::UnwantedInitiator
#[allow(clippy::type_complexity)]
#[doc(alias = "hwloc_memattr_get_targets")]
pub fn targets(
&self,
initiator: Option<Initiator<'_>>,
) -> Result<(Vec<&'topology TopologyObject>, Option<Vec<u64>>), HybridError<InitiatorInputError>>
{
// Validate the query + translate initiator to hwloc format
let get_values =
initiator.is_some() || !self.flags().contains(MemoryAttributeFlags::NEED_INITIATOR);
// SAFETY: - Will only be used before returning from this function,
// - get_targets is documented to accept a NULL initiator
let initiator = unsafe { self.checked_initiator(initiator.as_ref(), true)? };
// Run the query
// SAFETY: - hwloc_memattr_get_targets is indeed an array query
// - Parameters are forwarded in the right order
// - initiator has been checked to come from this topology and
// is allowed by the API contract to be NULL
let (targets, values) = unsafe {
self.array_query(
"hwloc_memattr_get_targets",
ptr::null(),
get_values,
|topology, attribute, flags, nr, targets, values| {
let initiator_ptr: *const hwloc_location = initiator
.as_ref()
.map_or(ptr::null(), |initiator_ref| initiator_ref);
hwlocality_sys::hwloc_memattr_get_targets(
topology,
attribute,
initiator_ptr,
flags,
nr,
targets,
values,
)
},
)
.map_err(HybridError::Hwloc)?
};
// Convert target list into a safe high-level form
let targets = targets
.into_iter()
// SAFETY: Targets originate from a query against this topology
.map(|node_ptr| unsafe { self.encapsulate_target_node(node_ptr) })
.collect();
let values = get_values.then_some(values);
Ok((targets, values))
}
/// Initiators that have values for a given attribute for a specific target
/// NUMA node, along with the associated values
///
/// This function is meant for tools and debugging (listing internal
/// information) rather than for application queries. Applications should
/// rather select useful NUMA nodes with [`Topology::local_numa_nodes()`]
/// and then look at their attribute values.
///
/// # Errors
///
/// - [`NoInitiator`] if this memory attribute doesn't have initiators
/// - [`ForeignTarget`] if `target` does not belong to this topology
///
/// [`ForeignTarget`]: InitiatorQueryError::ForeignTarget
/// [`NoInitiator`]: InitiatorQueryError::NoInitiator
#[doc(alias = "hwloc_memattr_get_initiators")]
pub fn initiators(
&self,
target_node: &TopologyObject,
) -> Result<(Vec<Initiator<'topology>>, Vec<u64>), HybridError<InitiatorQueryError>> {
// Validate the query
self.check_initiator_query_target(target_node)?;
// Run the query
// SAFETY: - hwloc_memattr_get_initiators is indeed an array query
// - Parameters are forwarded in the right order
// - target_node has been checked to come from this topology
let (initiators, values) = unsafe {
self.array_query(
"hwloc_memattr_get_initiators",
INVALID_LOCATION,
true,
|topology, attribute, flags, nr, initiators, values| {
hwlocality_sys::hwloc_memattr_get_initiators(
topology,
attribute,
target_node.as_inner(),
flags,
nr,
initiators,
values,
)
},
)
.map_err(HybridError::Hwloc)?
};
// Convert initiators into a safe high-level form
let initiators = initiators
.into_iter()
// SAFETY: Initiators originate from a query against this topology
.map(|initiator| unsafe { self.encapsulate_initiator(initiator) })
.collect();
Ok((initiators, values))
}
/// Perform a `get_targets` style memory attribute query
///
/// `get_values` indicates whether the caller is interested in associated
/// memory attribute values. If so, the output `Vec<u64>` will be filled
/// with as many values as the number of initiators/targets, if not it will
/// be an empty `Vec`.
///
/// # Safety
///
/// `query` must be one of the `hwloc_memattr_<plural>` queries, with the
/// parameter list simplified to the elements that are common to all of
/// these queries:
///
/// - Topology
/// - Memory attribute id
/// - Flags
/// - In/out number of memory attribute values
/// - Output endpoint buffer with capacity given above
/// - Output value buffer with capacity given above
unsafe fn array_query<Endpoint: Copy>(
&self,
api: &'static str,
placeholder: Endpoint,
get_values: bool,
mut query: impl FnMut(
hwloc_const_topology_t,
hwloc_memattr_id_t,
c_ulong,
*mut c_uint,
*mut Endpoint,
*mut u64,
) -> c_int,
) -> Result<(Vec<Endpoint>, Vec<u64>), RawHwlocError> {
// Prepare to call hwloc
let mut call_ffi = |nr_mut, endpoint_out, values_out| {
errors::call_hwloc_zero_or_minus1(api, || {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - id is trusted to be valid (type invariant)
// - flags must be 0 for all of these queries
// - Correctness of nr_mut, enpoint_out and values_out
// is call site dependent, see below
query(
self.topology.as_ptr(),
self.id,
0,
nr_mut,
endpoint_out,
values_out,
)
})
};
// Determine the right size for arrays of endpoints and values
// SAFETY: 1 elements + throw-away buffers is the correct way to request
// the buffer size to be allocated from hwloc
let mut nr = 1;
let res = call_ffi(
&raw mut nr,
[placeholder].as_mut_ptr(),
[u64::MAX].as_mut_ptr(),
);
let len = int::expect_usize(nr);
let mut endpoints = vec![placeholder; len];
// At the time of writing, all error cases of hwloc_memattr_get_targets
// are either avoided through API design or checked before attempting to
// call array_query(), so errors can only emerged through hwloc changes.
// But this is not true for hwloc_memattr_get_initiators. In that case,
// EINVAL is returned if the specified target has no memory attribute
// attached to it. This is only subtly different from having a memory
// attribute defined for this target, but no value/initiator attached,
// which yields no errors (just an empty initiator/value list), so we
// choose to handle it identically by returning an empty list.
if api == "hwloc_memattr_get_initiators" {
match res {
Ok(()) => {}
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) => return Ok((Vec::new(), Vec::new())),
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. But only EINVAL is expected here so we're fine.
return Ok((Vec::new(), Vec::new()));
}
Err(other_err) => return Err(other_err),
}
} else {
res?;
}
// Get the values if requested
let old_nr = nr;
let values = if get_values {
let mut values = vec![u64::MAX; len];
// SAFETY: - endpoints and values are indeed arrays of nr = len elements
// - Input array contents don't matter as this is an out-parameter
call_ffi(&raw mut nr, endpoints.as_mut_ptr(), values.as_mut_ptr())?;
values
} else {
// SAFETY: - endpoints is indeed an array of nr = len elements,
// values can be null to indicate lack of interest
// - Input array contents don't matter as this is an out-parameter
call_ffi(&raw mut nr, endpoints.as_mut_ptr(), ptr::null_mut())?;
Vec::new()
};
assert_eq!(old_nr, nr, "Inconsistent node count from hwloc");
Ok((endpoints, values))
}
/// Perform a `get_best_initiator`-style memory attribute query, assuming
/// the query arguments have been checked for correctness
///
/// # Safety
///
/// `query` must be one of the `hwloc_memattr_get_best_` queries, with the
/// parameter list simplified to the elements that are common to all of
/// these queries:
///
/// - Topology
/// - Memory attribute id
/// - Flags
/// - Best value output
unsafe fn get_best(
&self,
api: &'static str,
query: impl FnOnce(hwloc_const_topology_t, hwloc_memattr_id_t, c_ulong, *mut u64) -> c_int,
) -> Option<u64> {
/// Polymorphized subset of this function (avoids generics code bloat)
fn process_result(
api: &'static str,
final_value: u64,
result: Result<(), RawHwlocError>,
) -> Option<u64> {
match result {
Ok(()) => Some(final_value),
Err(RawHwlocError {
errno: Some(Errno(ENOENT)),
..
}) => None,
// EINVAL can mean several different things here:
// - Queried an initiator on a memory attribute that doesn't
// have them. This error is handled before calling this, so we
// should never observe an associated EINVAL.
// - Flags are nonzero. We prevent this by not letting the user
// set flags.
// - Memory attribute ID is invalid. We prevent this by not
// letting the user create a MemoryAttribute with an invalid
// ID.
// - Requested best initiator on a target that is not associated
// with this memory attribute. This error is only subtly
// different fom ENOENT which means that the target is
// registered but no initiator is registered. This is also
// inconsistent with best_target queries which return ENOENT
// in the case of a nonexistent initiator. Since Windows may
// not know the difference anyway (see below), we do not
// expose this distinction in the API and handle every case
// where there is no "best X for this Y" by returning None.
Err(RawHwlocError {
errno: Some(Errno(EINVAL)),
..
}) if api == "hwloc_memattr_get_best_initiator" => None,
#[cfg(windows)]
Err(RawHwlocError { errno: None, .. }) => {
// As explained in the RawHwlocError documentation, errno values
// may not correctly propagate from hwloc to hwlocality on
// Windows. But only EINVAL and ENOENT are expected here,
// and we handle them identically above, so we're fine.
None
}
Err(raw_err) => unreachable!("Unexpected hwloc error: {raw_err}"),
}
}
// Call hwloc and process its results
let mut value = u64::MAX;
let result = errors::call_hwloc_zero_or_minus1(api, || {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - id is trusted to be valid (type invariant)
// - flags must be 0 for all of these queries
// - value is an out-parameter, input value doesn't matter
query(self.topology.as_ptr(), self.id, 0, &raw mut value)
});
process_result(api, value, result)
}
/// Encapsulate a `*const TopologyObject` to a target NUMA node from hwloc
///
/// # Safety
///
/// `node_ptr` must originate from a query against this attribute
unsafe fn encapsulate_target_node(
&self,
node_ptr: *const hwloc_obj,
) -> &'topology TopologyObject {
assert!(!node_ptr.is_null(), "Got null target pointer from hwloc");
// SAFETY: Lifetime per input precondition, query output assumed valid
unsafe { (&*node_ptr).as_newtype() }
}
/// Encapsulate an initiator location from hwloc
///
/// # Safety
///
/// `initiator` must originate a query against this attribute
unsafe fn encapsulate_initiator(&self, initiator: hwloc_location) -> Initiator<'topology> {
// SAFETY: Lifetime per input precondition, query output assumed valid
unsafe { Initiator::from_raw(self.topology, initiator) }
.expect("Failed to decode location from hwloc")
}
/// Check the initiator argument to some query, then translate it into the
/// lower-level format that hwloc expects
///
/// If `is_optional` is true, it is okay not to provide an initiator even
/// if the memory attribute has flag [`MemoryAttributeFlags::NEED_INITIATOR`].
///
/// # Safety
///
/// - Do not use the output after the `'initiator` lifetime has expired.
/// - `is_optional` should only be set to `true` for recipients that are
/// documented to accept NULL initiators.
#[allow(clippy::needless_lifetimes)]
unsafe fn checked_initiator<'initiator>(
&self,
initiator: Option<&Initiator<'initiator>>,
is_optional: bool,
) -> Result<Option<hwloc_location>, InitiatorInputError> {
// Collect flags
let flags = self.flags();
// Handle missing initiator case
if flags.contains(MemoryAttributeFlags::NEED_INITIATOR)
&& initiator.is_none()
&& !is_optional
{
return Err(InitiatorInputError::NeedInitiator(self.error_name()));
}
// Handle unexpected initiator case
if !flags.contains(MemoryAttributeFlags::NEED_INITIATOR) && initiator.is_some() {
return Err(InitiatorInputError::UnwantedInitiator(self.error_name()));
}
// Handle expected absence of initiator
let Some(initiator) = initiator else {
// SAFETY: Per input precondition of is_optional + check above that
// initiator can only be NULL if initiator is optional
return Ok(None);
};
// Make sure initiator does belong to this topology
// SAFETY: Per function precondition on output usage
unsafe {
initiator
.as_checked_raw(self.topology)
.map(Some)
.map_err(InitiatorInputError::ForeignInitiator)
}
}
/// Check the target argument to some initiator query
fn check_initiator_query_target(
&self,
target: &TopologyObject,
) -> Result<(), InitiatorQueryError> {
if !self.flags().contains(MemoryAttributeFlags::NEED_INITIATOR) {
return Err(InitiatorQueryError::NoInitiator(self.error_name()));
}
if !self.topology.contains(target) {
return Err(target.into());
}
Ok(())
}
/// Translate this attribute's name to a form suitable for error reporting
fn error_name(&self) -> Box<str> {
self.name().to_string_lossy().into()
}
}
//
impl Debug for MemoryAttribute<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = format!("{}(#{})", self.name().to_string_lossy(), self.id);
f.pad(&name)
}
}
/// An invalid initiator was passed to a memory attribute function
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum InitiatorInputError {
/// Provided initiator does not belong to this topology
#[error("memory attribute initiator {0}")]
ForeignInitiator(#[from] ForeignInitiatorError),
/// An initiator had to be provided, but was not provided
#[error("memory attribute {0} needs an initiator but none was provided")]
NeedInitiator(Box<str>),
/// No initiator should have been provided, but one was provided
#[error("memory attribute {0} has no initiator but an initiator was provided")]
UnwantedInitiator(Box<str>),
}
/// A query for memory attribute initiators is invalid
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum InitiatorQueryError {
/// The specified `target` object does not belong to this topology
#[error("memory attribute target {0}")]
ForeignTarget(#[from] ForeignObjectError),
/// This memory attribute doesn't have initiators
#[error("memory attribute {0} has no initiator but its initiator was queried")]
NoInitiator(Box<str>),
}
//
impl<'topology> From<&'topology TopologyObject> for InitiatorQueryError {
fn from(object: &'topology TopologyObject) -> Self {
Self::ForeignTarget(object.into())
}
}
/// A memory attribute value query is invalid
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum ValueQueryError {
/// Specified `initiator` is bad
#[error(transparent)]
BadInitiator(#[from] InitiatorInputError),
/// Specified target object does not belong to this topology
#[error("memory attribute target {0}")]
ForeignTarget(ForeignObjectError),
/// Specified target object is not valid for this attribute
#[error("target object #{0} is not valid for this attribute")]
InvalidTarget(TopologyObjectID),
}
/// Initiator from the perspective of which memory attributes are measured
#[doc(alias = "hwloc_location")]
#[doc(alias = "hwloc_location::location")]
#[doc(alias = "hwloc_location::type")]
#[doc(alias = "hwloc_location_u")]
#[doc(alias = "hwloc_location::hwloc_location_u")]
#[doc(alias = "hwloc_location_type_e")]
#[derive(Clone, Debug, Display)]
pub enum Initiator<'target> {
/// Directly provide CPU set to find NUMA nodes with corresponding locality
///
/// This is the only initiator type supported by most memory attribute
/// queries on hwloc-defined memory attributes, though `Object` remains an
/// option for user-defined memory attributes.
#[doc(alias = "HWLOC_LOCATION_TYPE_CPUSET")]
CpuSet(BitmapCow<'target, CpuSet>),
/// Use a topology object as an initiator
///
/// Most memory attribute queries on hwloc-defined memory attributes do not
/// support this initiator type, or translate it to a cpuset (going up the
/// ancestor chain if necessary). But user-defined memory attributes may for
/// instance use it to provide custom information about host memory accesses
/// performed by GPUs.
///
/// Only objects belonging to the topology to which memory attributes are
/// attached should be used here.
#[doc(alias = "HWLOC_LOCATION_TYPE_OBJECT")]
Object(&'target TopologyObject),
}
//
impl<'target> Initiator<'target> {
/// Convert to the C representation for the purpose of running an hwloc
/// query against `topology`.
///
/// # Errors
///
/// [`ForeignObjectError`] if this [`Initiator`] is constructed from an
/// `&TopologyObject` that does not belong to the target [`Topology`].
///
/// # Safety
///
/// Do not use the output after the source lifetime has expired
pub(crate) unsafe fn as_checked_raw(
&self,
topology: &Topology,
) -> Result<hwloc_location, ForeignInitiatorError> {
match self {
Self::CpuSet(cpuset) => {
if !cpuset.is_empty() && topology.complete_cpuset().includes(cpuset.as_ref()) {
Ok(hwloc_location {
ty: HWLOC_LOCATION_TYPE_CPUSET,
location: hwloc_location_u {
cpuset: cpuset.as_ptr(),
},
})
} else {
Err(cpuset.clone().into_owned().into())
}
}
Self::Object(object) => {
if topology.contains(object) {
Ok(hwloc_location {
ty: HWLOC_LOCATION_TYPE_OBJECT,
location: hwloc_location_u {
object: object.as_inner(),
},
})
} else {
Err((*object).into())
}
}
}
}
/// Convert from the C representation
///
/// # Safety
///
/// This function should only be used to encapsulate [`hwloc_location`] structs
/// from hwloc topology queries, and the `_topology` parameter should match
/// the [`Topology`] from which the location was extracted.
unsafe fn from_raw(
_topology: &'target Topology,
raw: hwloc_location,
) -> Result<Self, LocationTypeError> {
// SAFETY: - Location type information from hwloc is assumed to be
// correct and tells us which union variant we should read.
// - Pointer is assumed to point to a valid CpuSet or
// TopologyObject that is owned by _topology, and thus has a
// lifetime of 'target or greater.
unsafe {
match raw.ty {
HWLOC_LOCATION_TYPE_CPUSET => {
let ptr = NonNull::new(raw.location.cpuset.cast_mut())
.expect("Unexpected null CpuSet from hwloc");
let cpuset = CpuSet::borrow_from_nonnull(ptr);
Ok(Initiator::from(cpuset))
}
HWLOC_LOCATION_TYPE_OBJECT => {
let ptr = NonNull::new(raw.location.object.cast_mut())
.expect("Unexpected null TopologyObject from hwloc");
Ok(Initiator::Object(ptr.as_ref().as_newtype()))
}
#[cfg(not(tarpaulin_include))]
unknown => Err(LocationTypeError(unknown)),
}
}
}
}
//
impl<'target> From<CpuSet> for Initiator<'target> {
fn from(cpuset: CpuSet) -> Self {
BitmapCow::from(cpuset).into()
}
}
//
impl<'target> From<BitmapCow<'target, CpuSet>> for Initiator<'target> {
fn from(cpuset: BitmapCow<'target, CpuSet>) -> Self {
Self::CpuSet(cpuset)
}
}
//
impl<'target> From<BitmapRef<'target, CpuSet>> for Initiator<'target> {
fn from(cpuset: BitmapRef<'target, CpuSet>) -> Self {
BitmapCow::from(cpuset).into()
}
}
//
impl<'target> From<&'target CpuSet> for Initiator<'target> {
fn from(cpuset: &'target CpuSet) -> Self {
BitmapCow::from(cpuset).into()
}
}
//
impl<'target> From<&'target TopologyObject> for Initiator<'target> {
fn from(object: &'target TopologyObject) -> Self {
Self::Object(object)
}
}
/// An out-of-topology initiator was passed to a memory attribute function
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
pub enum ForeignInitiatorError {
/// [`TopologyObject`] that does not belong to this topology
#[error(transparent)]
Object(#[from] ForeignObjectError),
/// [`CpuSet`] that is empty or contains CPUs outside of
/// [`Topology::complete_cpuset()`]
#[error("cpuset {0} is empty or contains CPUs outside of this topology")]
CpuSet(CpuSet),
}
//
impl From<&TopologyObject> for ForeignInitiatorError {
fn from(obj: &TopologyObject) -> Self {
Self::Object(obj.into())
}
}
//
impl From<CpuSet> for ForeignInitiatorError {
fn from(set: CpuSet) -> Self {
Self::CpuSet(set)
}
}
//
impl From<Initiator<'_>> for ForeignInitiatorError {
fn from(initiator: Initiator<'_>) -> Self {
match initiator {
Initiator::Object(obj) => obj.into(),
Initiator::CpuSet(set) => set.into_owned().into(),
}
}
}
/// Error returned when an unknown location type is observed
#[derive(Copy, Clone, Debug, Eq, Error, From, Hash, PartialEq)]
#[error("hwloc provided a memory attribute location of unknown type {0}")]
struct LocationTypeError(c_int);
/// Invalid `hwloc_location`, which hwloc is assumed not to observe
///
/// # Safety
///
/// Do not expose this location value to an hwloc function that reads it.
const INVALID_LOCATION: hwloc_location = hwloc_location {
ty: HWLOC_LOCATION_TYPE_CPUSET,
location: hwloc_location_u {
cpuset: ptr::null(),
},
};
bitflags! {
/// Flags for selecting more target NUMA nodes
///
/// By default only NUMA nodes whose locality is exactly the given initiator
/// are selected.
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
#[doc(alias = "hwloc_local_numanode_flag_e")]
pub struct LocalNUMANodeFlags: hwloc_local_numanode_flag_e {
/// Select NUMA nodes whose locality is larger than the given cpuset
///
/// For instance, if a single PU (or its cpuset) is given in `initiator`,
/// select all nodes close to the package that contains this PU.
#[doc(alias = "HWLOC_LOCAL_NUMANODE_FLAG_LARGER_LOCALITY")]
const LARGER_LOCALITY = HWLOC_LOCAL_NUMANODE_FLAG_LARGER_LOCALITY;
/// Select NUMA nodes whose locality is smaller than the given cpuset
///
/// For instance, if a package (or its cpuset) is given in `initiator`,
/// also select nodes that are attached to only a half of that package.
#[doc(alias = "HWLOC_LOCAL_NUMANODE_FLAG_SMALLER_LOCALITY")]
const SMALLER_LOCALITY = HWLOC_LOCAL_NUMANODE_FLAG_SMALLER_LOCALITY;
/// Select NUMA nodes whose locality intersects the given cpuset
///
/// This includes larger and smaller localities (as if `LARGER_LOCALITY`
/// and `SMALLER_LOCALITY` were both specified), and also localities
/// that are only partially included.
///
/// For instance, on a multi-CPU system, if the locality is one core of
/// both packages, a NUMA node local to one package is neither larger
/// nor smaller than this locality, but it intersects it.
#[cfg(feature = "hwloc-2_12_1")]
#[doc(alias = "HWLOC_LOCAL_NUMANODE_FLAG_INTERSECT_LOCALITY")]
const INTERSECT_LOCALITY = HWLOC_LOCAL_NUMANODE_FLAG_INTERSECT_LOCALITY;
/// Select all NUMA nodes in the topology
///
/// The initiator is ignored.
///
/// This flag is automatically set when users specify
/// [`NUMAInitiator::All`] as the target NUMA node set.
#[doc(hidden)]
const ALL = HWLOC_LOCAL_NUMANODE_FLAG_ALL;
}
}
//
crate::impl_arbitrary_for_bitflags!(LocalNUMANodeFlags, hwloc_local_numanode_flag_e);
/// Scope of a [`Topology::local_numa_nodes()`] query
#[derive(Clone, Debug)]
pub enum NUMAInitiator<'target> {
/// Nodes local to some initiator object
Local {
/// Initiator which NUMA nodes should be local to
///
/// By default, the search only returns NUMA nodes whose locality is
/// exactly the given `initiator`. More nodes can be selected using
/// `flags`.
initiator: Initiator<'target>,
/// Flags for enlarging the NUMA node search
flags: LocalNUMANodeFlags,
},
/// All NUMA nodes in the topology
#[doc(alias = "HWLOC_LOCAL_NUMANODE_FLAG_ALL")]
All,
}
//
impl NUMAInitiator<'_> {
/// Convert to the inputs expected by a `hwloc_get_local_numanode_objs`
/// query against `topology`
///
/// # Errors
///
/// [`ForeignInitiatorError`] if the input initiator is a [`TopologyObject`]
/// that does not belong to the target [`Topology`]; or if it is a
/// [`CpuSet`] that is empty or contains CPUs outside of the topology's
/// [`complete_cpuset()`](Topology::complete_cpuset).
///
/// # Safety
///
/// Do not use the output raw location after the source lifetime has expired
pub(crate) unsafe fn as_checked_raw(
&self,
topology: &Topology,
) -> Result<(Option<hwloc_location>, LocalNUMANodeFlags), ForeignInitiatorError> {
match self {
Self::Local { initiator, flags } => {
let mut flags = *flags;
flags.remove(LocalNUMANodeFlags::ALL);
// SAFETY: Per function precondition
Ok((Some(unsafe { initiator.as_checked_raw(topology)? }), flags))
}
// SAFETY: In presence of the ALL flag, the initiator is ignored,
// so a null location is fine.
Self::All => Ok((None, LocalNUMANodeFlags::ALL)),
}
}
}
//
impl<'target, T: Into<Initiator<'target>>> From<T> for NUMAInitiator<'target> {
fn from(initiator: T) -> Self {
Self::Local {
initiator: initiator.into(),
flags: LocalNUMANodeFlags::empty(),
}
}
}
bitflags! {
/// Memory attribute flags
///
/// Exactly one of the `HIGHER_IS_BEST` and `LOWER_IS_BEST` flags must be set.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[doc(alias = "hwloc_memattr_flag_e")]
pub struct MemoryAttributeFlags: hwloc_memattr_flag_e {
/// The best nodes for this memory attribute are those with the higher
/// values
///
/// This flag is mutually exclusive with [`LOWER_IS_BEST`].
///
/// See for instance [`MemoryAttribute::bandwidth()`].
///
/// [`LOWER_IS_BEST`]: Self::LOWER_IS_BEST
#[doc(alias = "HWLOC_MEMATTR_FLAG_HIGHER_FIRST")]
const HIGHER_IS_BEST = HWLOC_MEMATTR_FLAG_HIGHER_FIRST;
/// The best nodes for this memory attribute are those with the lower
/// values
///
/// This flag is mutually exclusive with [`HIGHER_IS_BEST`].
///
/// See for instance [`MemoryAttribute::latency()`].
///
/// [`HIGHER_IS_BEST`]: Self::HIGHER_IS_BEST
#[doc(alias = "HWLOC_MEMATTR_FLAG_LOWER_FIRST")]
const LOWER_IS_BEST = HWLOC_MEMATTR_FLAG_LOWER_FIRST;
/// The value returned for this memory attribute depends on the given
/// initiator
///
/// See for instance [`bandwidth()`] and [`latency()`], but not
/// [`capacity()`].
///
/// [`bandwidth()`]: MemoryAttribute::bandwidth()
/// [`latency()`]: MemoryAttribute::latency()
/// [`capacity()`]: MemoryAttribute::capacity()
#[doc(alias = "HWLOC_MEMATTR_FLAG_NEED_INITIATOR")]
const NEED_INITIATOR = HWLOC_MEMATTR_FLAG_NEED_INITIATOR;
}
}
//
impl MemoryAttributeFlags {
/// Truth that these flags are in a valid state
pub(crate) fn is_valid(self) -> bool {
self.contains(Self::HIGHER_IS_BEST) ^ self.contains(Self::LOWER_IS_BEST)
}
}
//
crate::impl_arbitrary_for_bitflags!(MemoryAttributeFlags, hwloc_memattr_flag_e);
#[cfg(test)]
mod tests {
use super::*;
use crate::{
object::types::ObjectType,
strategies::{any_object, any_string, set_with_reference, topology_related_set},
topology::support::{DiscoverySupport, FeatureSupport},
};
use proptest::{prelude::*, sample::SizeRange};
#[allow(unused)]
use similar_asserts::assert_eq;
use std::{cmp::Ordering, collections::HashMap, ffi::CString, sync::OnceLock};
/// Test a predefined memory attribute
fn test_predefined(
constructor: impl FnOnce(&Topology) -> MemoryAttribute<'_>,
expected_name: &CStr,
expected_flags: MemoryAttributeFlags,
extra_tests: impl FnOnce(&Topology, MemoryAttribute<'_>),
) {
let topology = Topology::test_instance();
let attribute = constructor(topology);
assert_eq!(attribute.name(), expected_name);
assert_eq!(attribute.flags(), expected_flags);
assert_eq!(attribute.dynamic_flags(), expected_flags);
extra_tests(topology, attribute);
}
//
/// Extra tests for `NUMANode` convenience attributes
fn extra_tests_numa(
required_support: fn(&DiscoverySupport) -> bool,
mut expected_value: impl FnMut(&TopologyObject) -> u64,
) -> impl FnOnce(&Topology, MemoryAttribute<'_>) {
move |topology, attribute| {
// They require a certain kind of support
if !topology.supports(FeatureSupport::discovery, required_support) {
return;
}
// They have an expected value that's easy to query
let mut numa_set = HashSet::new();
for numa in topology.objects_with_type(ObjectType::NUMANode) {
assert!(matches!(
attribute.initiators(numa),
Err(HybridError::Rust(InitiatorQueryError::NoInitiator(_)))
));
assert_eq!(attribute.value(None, numa), Ok(Some(expected_value(numa))));
numa_set.insert(numa.global_persistent_index());
}
// The targets query returns values and a target set that matches
// the numa node set
let (targets, values) = attribute.targets(None).unwrap();
assert!(values.is_some());
let target_set = targets
.into_iter()
.map(TopologyObject::global_persistent_index)
.collect::<HashSet<_>>();
assert_eq!(numa_set, target_set);
}
}
//
#[test]
fn capacity() {
test_predefined(
|t| MemoryAttribute::capacity(t),
&CString::new("Capacity").unwrap(),
MemoryAttributeFlags::HIGHER_IS_BEST,
extra_tests_numa(DiscoverySupport::numa_memory, TopologyObject::total_memory),
);
}
//
#[test]
fn locality() {
test_predefined(
|t| MemoryAttribute::locality(t),
&CString::new("Locality").unwrap(),
MemoryAttributeFlags::LOWER_IS_BEST,
extra_tests_numa(DiscoverySupport::pu_count, |numa| {
numa.cpuset().unwrap().weight().unwrap() as u64
}),
);
}
//
#[test]
fn bandwidth() {
test_predefined(
|t| MemoryAttribute::bandwidth(t),
&CString::new("Bandwidth").unwrap(),
MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
//
#[test]
fn latency() {
test_predefined(
|t| MemoryAttribute::latency(t),
&CString::new("Latency").unwrap(),
MemoryAttributeFlags::LOWER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
//
#[cfg(feature = "hwloc-2_8_0")]
mod hwloc28 {
use super::*;
#[test]
fn read_bandwidth() {
test_predefined(
|t| MemoryAttribute::read_bandwidth(t),
&CString::new("ReadBandwidth").unwrap(),
MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
#[test]
fn write_bandwidth() {
test_predefined(
|t| MemoryAttribute::write_bandwidth(t),
&CString::new("WriteBandwidth").unwrap(),
MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
#[test]
fn read_latency() {
test_predefined(
|t| MemoryAttribute::read_latency(t),
&CString::new("ReadLatency").unwrap(),
MemoryAttributeFlags::LOWER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
#[test]
fn write_latency() {
test_predefined(
|t| MemoryAttribute::write_latency(t),
&CString::new("WriteLatency").unwrap(),
MemoryAttributeFlags::LOWER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
|_, _| {},
);
}
}
/// Mechanism to track the best value of a memory attribute and the
/// endpoints for which the memory attribute has this value.
struct BestValueEndpoints<Endpoint> {
higher_is_best: bool,
best_value: u64,
best_endpoints: Vec<Endpoint>,
}
//
impl<Endpoint> BestValueEndpoints<Endpoint> {
fn new(higher_is_best: bool) -> Self {
Self {
higher_is_best,
best_value: if higher_is_best { u64::MIN } else { u64::MAX },
best_endpoints: Vec::new(),
}
}
fn push(&mut self, endpoint: Endpoint, value: u64) {
match (self.higher_is_best, value.cmp(&self.best_value)) {
(true, Ordering::Greater) | (false, Ordering::Less) => {
self.best_value = value;
self.best_endpoints.clear();
self.best_endpoints.push(endpoint);
}
(_, Ordering::Equal) => {
self.best_endpoints.push(endpoint);
}
_ => {}
}
}
fn collect_value_and_endpoints(self) -> Option<(Vec<Endpoint>, u64)> {
(!self.best_endpoints.is_empty()).then_some((self.best_endpoints, self.best_value))
}
}
/// Test all memory attributes for all queries that should succeed
#[test]
fn successful_queries() {
let topology = Topology::test_instance();
for attr in MemoryAttribute::all(topology) {
let name = attr.name().to_str().unwrap();
let by_name = topology
.memory_attribute_named(name)
.expect("Memory attributes should not have a NUL in their name")
.expect("MemoryAttribute::all() should not yield nonexistent attributes");
assert!(ptr::eq(by_name.topology, attr.topology));
assert_eq!(by_name.id, attr.id);
let flags = attr.flags();
assert_eq!(
flags
.intersection(
MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::LOWER_IS_BEST
)
.iter()
.count(),
1
);
if flags.contains(MemoryAttributeFlags::NEED_INITIATOR) {
test_targets_with_initiators(attr);
} else {
test_targets_wo_initiators(attr);
}
}
}
/// Target and initiator testing for memory attributes that need an
/// initiator
fn test_targets_with_initiators(attr_with_initiator: MemoryAttribute<'_>) {
// Check basic attribute properties and query targets
let attr = attr_with_initiator;
let higher_is_best = attr.flags().contains(MemoryAttributeFlags::HIGHER_IS_BEST);
let (target_nodes, no_values) = attr.targets(None).unwrap();
assert!(no_values.is_none());
// For each target...
let mut key_to_initiator = HashMap::new();
let mut initiator_key_to_target_ids_and_values = HashMap::<_, HashSet<_>>::new();
for target in target_nodes {
// Check initiators, collect best initiator and targets for
// each initiator
let (initiators, values) = attr.initiators(target).unwrap();
let mut expected_best_initiators = BestValueEndpoints::new(higher_is_best);
for (initiator, value) in initiators.into_iter().zip(values) {
assert_eq!(attr.value(Some(initiator.clone()), target), Ok(Some(value)));
/// Unique initiator identifier
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum InitiatorKey {
CpuSet(CpuSet),
Object(TopologyObjectID),
}
let initiator_key = match initiator.clone() {
Initiator::CpuSet(set) => InitiatorKey::CpuSet(set.into_owned()),
Initiator::Object(obj) => InitiatorKey::Object(obj.global_persistent_index()),
};
key_to_initiator.insert(initiator_key.clone(), initiator.clone());
let inserted = initiator_key_to_target_ids_and_values
.entry(initiator_key)
.or_default()
.insert((target.global_persistent_index(), value));
assert!(inserted);
expected_best_initiators.push(initiator, value);
}
// Check best initiator
let expected_best_initiators = expected_best_initiators.collect_value_and_endpoints();
let best_initiator = attr.best_initiator(target).unwrap();
assert_eq!(expected_best_initiators.is_none(), best_initiator.is_none());
if let (
Some((expected_best_initiators, expected_best_value)),
Some((best_initiator, best_value)),
) = (expected_best_initiators, best_initiator)
{
assert_eq!(best_value, expected_best_value);
assert!(expected_best_initiators.into_iter().any(|initiator| {
match (initiator, &best_initiator) {
(Initiator::CpuSet(set1), Initiator::CpuSet(set2)) => set1 == *set2,
(Initiator::Object(obj1), Initiator::Object(obj2)) => {
obj1.global_persistent_index() == obj2.global_persistent_index()
}
_ => false,
}
}));
}
}
// For each initiator...
for (initiator_key, target_ids_and_values) in initiator_key_to_target_ids_and_values {
// Check targets, find best targets
let initiator = &key_to_initiator[&initiator_key];
let (targets, values) = attr.targets(Some(initiator.clone())).unwrap();
let values = values.unwrap();
assert_eq!(targets.len(), values.len());
assert_eq!(targets.len(), target_ids_and_values.len());
let mut expected_best_targets = BestValueEndpoints::new(higher_is_best);
for (target, value) in targets.iter().zip(values) {
assert!(target_ids_and_values.contains(&(target.global_persistent_index(), value)));
expected_best_targets.push(target.global_persistent_index(), value);
}
// Check best target
let expected_best_targets = expected_best_targets.collect_value_and_endpoints();
let best_target = attr.best_target(Some(initiator.clone())).unwrap();
assert_eq!(expected_best_targets.is_none(), best_target.is_none());
if let (
Some((expected_best_targets, expected_best_value)),
Some((best_target, best_value)),
) = (expected_best_targets, best_target)
{
assert_eq!(best_value, expected_best_value);
assert!(
expected_best_targets
.into_iter()
.any(|target_id| { best_target.global_persistent_index() == target_id })
);
}
}
}
/// Target and initiator testing for memory attributes that have no
/// initiator
fn test_targets_wo_initiators(attr_wo_initiator: MemoryAttribute<'_>) {
// Check basic attribute properties and query targets
let attr = attr_wo_initiator;
let higher_is_best = attr.flags().contains(MemoryAttributeFlags::HIGHER_IS_BEST);
let (target_nodes, values) = attr.targets(None).unwrap();
let values_via_targets = values.unwrap();
assert_eq!(target_nodes.len(), values_via_targets.len());
// Check values, find best targets
let mut expected_best_targets = BestValueEndpoints::new(higher_is_best);
for (target, value) in target_nodes.into_iter().zip(values_via_targets) {
assert_eq!(attr.value(None, target), Ok(Some(value)));
expected_best_targets.push(target, value);
}
// Check best target
let expected_best_targets = expected_best_targets.collect_value_and_endpoints();
let best_target = attr.best_target(None).unwrap();
assert_eq!(expected_best_targets.is_none(), best_target.is_none());
if let (
Some((expected_best_targets, expected_best_value)),
Some((best_target, best_value)),
) = (expected_best_targets, best_target)
{
assert_eq!(best_value, expected_best_value);
assert!(
expected_best_targets
.into_iter()
.any(|target| target.global_persistent_index()
== best_target.global_persistent_index())
);
}
}
proptest! {
#[test]
fn memory_attribute_named(name in any_string()) {
let topology = Topology::test_instance();
let res = topology.memory_attribute_named(&name);
let name_contains_nul = name.contains('\0');
let Ok(maybe_attr) = res else {
prop_assert!(name_contains_nul);
return Ok(());
};
prop_assert!(!name_contains_nul);
if maybe_attr.is_none() {
let name = CString::new(name).unwrap();
for attr in MemoryAttribute::all(topology) {
prop_assert_ne!(attr.name(), name.as_c_str());
}
}
}
}
/// Pick a memory attribute
fn memory_attribute() -> impl Strategy<Value = MemoryAttribute<'static>> {
prop::sample::select(MemoryAttribute::all(Topology::test_instance()).collect::<Vec<_>>())
}
/// Pick a memory attribute and a target that has a chance of being correct
/// for this attribute, but may be a random object possibly coming from
/// another topology.
fn memory_attribute_and_target()
-> impl Strategy<Value = (MemoryAttribute<'static>, &'static TopologyObject)> {
memory_attribute().prop_flat_map(|attr| {
let targets = attr.targets(None).unwrap().0;
let target = if targets.is_empty() {
any_object().boxed()
} else {
prop_oneof![
2 => prop::sample::select(targets),
3 => any_object(),
]
.boxed()
};
(Just(attr), target)
})
}
proptest! {
#[test]
fn initiator_query_errors(
(attr, target) in memory_attribute_and_target()
) {
let no_initiators = !attr.flags().contains(MemoryAttributeFlags::NEED_INITIATOR);
let foreign_target = !attr.topology.contains(target);
let check_normalized_error = |res_wo_ok: Result<(), InitiatorQueryError>| {
match res_wo_ok {
Ok(()) => prop_assert!(!(no_initiators || foreign_target)),
Err(InitiatorQueryError::ForeignTarget(_)) => prop_assert!(foreign_target),
Err(InitiatorQueryError::NoInitiator(_)) => prop_assert!(no_initiators),
}
Ok(())
};
check_normalized_error(attr.best_initiator(target).map(std::mem::drop))?;
check_normalized_error(attr.initiators(target).map(std::mem::drop).map_err(|e| {
let HybridError::Rust(e) = e else {
unreachable!("No known error cases that aren't handled on the Rust side, yet got {e:?}")
};
e
}))?;
}
}
/// Pick a memory attribute initiator that has a low odd of being valid
fn any_initiator() -> impl Strategy<Value = Option<Initiator<'static>>> {
prop_oneof![
topology_related_set(Topology::cpuset).prop_map(|set| Some(Initiator::from(set))),
any_object().prop_map(|obj| Some(Initiator::from(obj)))
]
}
/// Pick an (attribute, target, initiator) triple where (target, initiator)
/// has a chance of being correct for this attribute, but may be a random
/// object/cpuset possibly coming from another topology.
fn memory_attribute_target_initiator() -> impl Strategy<
Value = (
MemoryAttribute<'static>,
&'static TopologyObject,
Option<Initiator<'static>>,
),
> {
memory_attribute_and_target().prop_flat_map(move |(attr, target)| {
let initiator = if attr.flags().contains(MemoryAttributeFlags::NEED_INITIATOR) {
let (actual_initiators, _values) = attr.initiators(target).unwrap_or_default();
if actual_initiators.is_empty() {
prop_oneof![
1 => Just(None),
4 => any_initiator(),
]
.boxed()
} else {
let actual_initiator = prop::sample::select(actual_initiators.clone());
prop_oneof![
1 => Just(None),
2 => actual_initiator.prop_flat_map(move |actual_initiator| {
match actual_initiator {
Initiator::CpuSet(set) => {
set_with_reference(set)
.prop_map(|set| Some(Initiator::from(set)))
.boxed()
}
Initiator::Object(obj) => {
Just(Some(Initiator::from(obj)))
.boxed()
}
}
}),
2 => any_initiator(),
]
.boxed()
}
} else {
prop_oneof![
2 => Just(None),
3 => any_initiator(),
]
.boxed()
};
(Just(attr), Just(target), initiator)
})
}
/// Truth that an initiator does not belong to a topology
fn is_foreign_initiator<'topology>(
topology: &'topology Topology,
initiator: &Initiator<'topology>,
) -> bool {
match initiator {
Initiator::Object(obj) => !topology.contains(obj),
Initiator::CpuSet(set) => {
set.is_empty() || !topology.complete_cpuset().includes(set.as_ref())
}
}
}
proptest! {
#[test]
fn value_query_errors(
(attr, target, initiator) in memory_attribute_target_initiator()
) {
// Detect errors which are handled on the Rust side
let foreign_initiator = initiator.as_ref().is_some_and(|initiator| is_foreign_initiator(attr.topology, initiator));
let foreign_target = !attr.topology.contains(target);
let attr_needs_initiator = attr.flags().contains(MemoryAttributeFlags::NEED_INITIATOR);
let need_initiator = attr_needs_initiator && initiator.is_none();
let unwanted_initiator = !attr_needs_initiator && initiator.is_some();
let invalid_target =
(attr.id == HWLOC_MEMATTR_ID_CAPACITY && target.object_type() != ObjectType::NUMANode)
|| (attr.id == HWLOC_MEMATTR_ID_LOCALITY && target.cpuset().is_none());
let any_error =
foreign_initiator
|| foreign_target
|| need_initiator
|| unwanted_initiator
|| invalid_target;
// Detect invalid (target, initiator) pairs.
let (valid_targets, _values) = attr.targets(None).unwrap();
let invalid_initiator_or_target = (attr_needs_initiator != initiator.is_some()) || valid_targets.iter().all(|candidate| {
if candidate.global_persistent_index() != target.global_persistent_index() {
return true;
}
if !attr_needs_initiator {
return false;
}
let (initiators, _values) = attr.initiators(candidate).unwrap();
initiators.iter().all(|candidate| match (candidate, &initiator) {
(Initiator::CpuSet(candidate), Some(Initiator::CpuSet(set))) => {
!candidate.includes(set.as_ref())
}
(Initiator::Object(candidate), Some(Initiator::Object(obj))) => {
candidate.global_persistent_index() != obj.global_persistent_index()
},
_ => true,
})
});
// Call value query and check result
match attr.value(initiator.clone(), target) {
Ok(value) => {
prop_assert!(!any_error);
// NOTE: Right now we're only testing this direction because
// hwloc can yield attribute values for invalid
// (initiator, target) pairs.
if value.is_none() {
prop_assert!(invalid_initiator_or_target);
}
}
Err(HybridError::Rust(er)) => match er {
ValueQueryError::BadInitiator(bad) => match bad {
InitiatorInputError::ForeignInitiator(fi) => {
prop_assert!(foreign_initiator);
prop_assert!(initiator.is_some());
prop_assert_eq!(fi, ForeignInitiatorError::from(initiator.unwrap()));
}
InitiatorInputError::NeedInitiator(_) => prop_assert!(need_initiator),
InitiatorInputError::UnwantedInitiator(_) => prop_assert!(unwanted_initiator),
},
ValueQueryError::ForeignTarget(_) => prop_assert!(foreign_target),
ValueQueryError::InvalidTarget(_) => prop_assert!(invalid_target),
},
Err(HybridError::Hwloc(e)) => unreachable!("Unexpected hwloc error {e}"),
};
}
}
/// Given a memory attribute, pick an initiator that has a chance of being
/// correct for this attribute, but may be a random object possibly coming
/// from another topology.
fn memory_attribute_and_initiator()
-> impl Strategy<Value = (MemoryAttribute<'static>, Option<Initiator<'static>>)> {
memory_attribute_target_initiator().prop_map(|(attr, _target, initiator)| (attr, initiator))
}
proptest! {
#[test]
fn target_query_errors(
(attr, initiator) in memory_attribute_and_initiator()
) {
let foreign_initiator = initiator.as_ref().is_some_and(|initiator| is_foreign_initiator(attr.topology, initiator));
let attr_needs_initiator = attr.flags().contains(MemoryAttributeFlags::NEED_INITIATOR);
let need_initiator = attr_needs_initiator && initiator.is_none();
let unwanted_initiator = !attr_needs_initiator && initiator.is_some();
let check_normalized_error = |res_wo_ok: Result<(), InitiatorInputError>,
initiator_is_optional: bool| {
match res_wo_ok {
Ok(()) => prop_assert!(
!(foreign_initiator || unwanted_initiator)
&& (initiator_is_optional || !need_initiator)
),
Err(InitiatorInputError::ForeignInitiator(_)) => prop_assert!(foreign_initiator),
Err(InitiatorInputError::NeedInitiator(_)) => prop_assert!(
need_initiator && !initiator_is_optional
),
Err(InitiatorInputError::UnwantedInitiator(_)) => prop_assert!(unwanted_initiator),
}
Ok(())
};
check_normalized_error(
attr.best_target(initiator.clone()).map(std::mem::drop),
false,
)?;
check_normalized_error(
attr.targets(initiator).map(std::mem::drop).map_err(|e| {
let HybridError::Rust(e) = e else {
unreachable!("No known error cases that aren't handled on the Rust side, yet got {e:?}")
};
e
}),
true,
)?;
}
}
/// Generate a `NUMAInitiator`
fn numa_initiator() -> impl Strategy<Value = NUMAInitiator<'static>> {
let topology = Topology::test_instance();
prop_oneof![
1 => Just(NUMAInitiator::All),
2 => {
(any_object(), any::<LocalNUMANodeFlags>()).prop_map(|(obj, flags)| {
NUMAInitiator::Local {
initiator: Initiator::from(obj),
flags,
}
})
},
2 => {
let numa_nodes = topology.objects_with_type(ObjectType::NUMANode).filter(|numa| {
numa.cpuset().unwrap().weight().unwrap() > 0
}).collect::<Vec<_>>();
prop::sample::select(numa_nodes).prop_flat_map(|numa| {
(set_with_reference(numa.cpuset().unwrap()), any::<LocalNUMANodeFlags>()).prop_map(|(set, flags)| {
NUMAInitiator::Local {
initiator: Initiator::from(set),
flags,
}
})
})
}
]
}
proptest! {
#[test]
fn local_numa_nodes(initiator in numa_initiator()) {
let topology = Topology::test_instance();
let res = topology.local_numa_nodes(initiator.clone());
let foreign_object = match &initiator {
NUMAInitiator::Local { initiator, .. } => is_foreign_initiator(topology, initiator),
NUMAInitiator::All => false,
};
prop_assert_eq!(res.is_err(), foreign_object);
let Ok(nodes) = res else { return Ok(()); };
let node_ids = nodes.into_iter()
.map(TopologyObject::global_persistent_index)
.collect::<HashSet<_>>();
match initiator {
NUMAInitiator::All => {
let all_node_ids = topology.objects_with_type(ObjectType::NUMANode)
.map(TopologyObject::global_persistent_index)
.collect::<HashSet<_>>();
prop_assert_eq!(node_ids, all_node_ids);
}
NUMAInitiator::Local { initiator, flags } => {
let cpuset = match initiator {
Initiator::CpuSet(set) => set,
Initiator::Object(obj) => {
obj.cpuset()
.unwrap_or_else(|| obj.first_non_io_ancestor()
.unwrap()
.cpuset()
.unwrap())
.into()
}
};
let expected_node_ids = topology
.objects_with_type(ObjectType::NUMANode)
.filter_map(|node| {
let node_cpuset = node.cpuset().unwrap();
let flags = flags - LocalNUMANodeFlags::ALL;
#[allow(unused_mut)]
let mut is_match = (node_cpuset == cpuset)
|| (flags.contains(LocalNUMANodeFlags::LARGER_LOCALITY) && node_cpuset.includes(cpuset.as_ref()))
|| (flags.contains(LocalNUMANodeFlags::SMALLER_LOCALITY) && cpuset.includes(node_cpuset));
#[cfg(feature = "hwloc-2_12_1")]
{
is_match |= flags.contains(LocalNUMANodeFlags::INTERSECT_LOCALITY) && cpuset.intersects(node_cpuset);
}
is_match.then(|| node.global_persistent_index())
})
.collect::<HashSet<_>>();
prop_assert_eq!(expected_node_ids, node_ids);
}
}
}
}
/// On hwloc v2.12.0+, test that the default nodeset makes sense
#[cfg(feature = "hwloc-2_12_0")]
#[test]
fn get_default_nodeset() {
// Get the default nodeset
let topology = Topology::test_instance();
let Ok(default_nodeset) = topology.get_default_nodeset() else {
// Error cases are not documented by hwloc, so there's little we can
// do when an error is encountered.
return;
};
// The returned nodeset is included in the topology nodeset
assert!(topology.nodeset().includes(&default_nodeset));
// It is guaranteed that nodes from the default nodeset have
// non-intersecting CPU sets
let mut cpuset_so_far = CpuSet::new();
for node in topology.nodes_from_nodeset(&default_nodeset) {
let node_cpuset = node.cpuset().unwrap();
assert!(!cpuset_so_far.intersects(node_cpuset));
cpuset_so_far |= node_cpuset;
}
// Any core that had some local NUMA nodes in the initial topology
// should still have one in the default nodeset.
for pu in topology.objects_with_type(ObjectType::PU) {
let pu_nodeset = pu.nodeset().unwrap();
assert!(pu_nodeset.intersects(&default_nodeset) || pu_nodeset.is_empty());
}
}
fn attr_names() -> &'static [String] {
static NAMES: OnceLock<Vec<String>> = OnceLock::new();
NAMES
.get_or_init(|| {
MemoryAttribute::all(Topology::test_instance())
.map(|attr| attr.name().to_str().unwrap().to_owned())
.collect::<Vec<_>>()
})
.as_slice()
}
// New memory attribute name with a fair chance of collision
fn attribute_name() -> impl Strategy<Value = String> {
prop_oneof![
2 => prop::sample::select(attr_names()),
3 => any_string(),
]
}
proptest! {
#[test]
fn build_empty_attribute(
name in attribute_name(),
flags: MemoryAttributeFlags
) {
let initial_topology = Topology::test_instance();
let mut topology = initial_topology.clone();
let res = topology.edit(|editor| {
editor
.register_memory_attribute(&name, flags)
.map(std::mem::drop)
});
if check_register_errors(&name, flags, res)?.is_none() {
prop_assert_eq!(&topology, initial_topology);
return Ok(());
}
prop_assert_ne!(&topology, initial_topology);
let attr = topology.memory_attribute_named(&name).unwrap().unwrap();
let cname = CString::new(name).unwrap();
prop_assert_eq!(attr.name(), cname.as_c_str());
prop_assert_eq!(attr.flags(), flags);
let (targets, values) = attr.targets(None).unwrap();
prop_assert!(targets.is_empty());
prop_assert_eq!(
values,
(!flags.contains(MemoryAttributeFlags::NEED_INITIATOR)).then_some(Vec::new())
);
let mut expected_dump = initial_topology.dump_memory_attributes();
expected_dump.0.push(AttributeDump::new(attr));
prop_assert!(topology.dump_memory_attributes().eq_modulo_topology(&expected_dump));
}
}
// Check memory attribute builder creation errors
fn check_register_errors<T>(
name: &String,
flags: MemoryAttributeFlags,
res: Result<T, RegisterError>,
) -> Result<Option<T>, TestCaseError> {
let bad_flags = (flags
& (MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::LOWER_IS_BEST))
.iter()
.count()
!= 1;
let name_contains_nul = name.contains('\0');
let name_taken = attr_names().contains(name);
match res {
Ok(o) => {
prop_assert!(!(bad_flags || name_contains_nul || name_taken));
Ok(Some(o))
}
Err(e) => {
match e {
RegisterError::BadFlags(bf) => {
prop_assert!(
bad_flags,
"Got unexpected BadFlags error with flags {flags:?}"
);
prop_assert_eq!(bf, FlagsError::from(flags));
}
RegisterError::NameContainsNul => prop_assert!(name_contains_nul),
RegisterError::NameTaken(taken) => {
prop_assert!(name_taken);
prop_assert_eq!(name, &*taken);
}
}
Ok(None)
}
}
}
/// Pick a set of targets and values for a memory attribute
fn targets_and_values() -> impl Strategy<Value = TargetsAndValues> {
// Pick attribute targets
let topology = Topology::test_instance();
let all_objects = topology.objects().collect::<Vec<_>>();
let num_objects = all_objects.len();
let targets = prop_oneof![
1 => Just(Vec::new()),
3 => prop::sample::subsequence(
all_objects,
1..=num_objects,
).prop_shuffle(),
1 => prop::collection::vec(
any_object(),
SizeRange::default(),
),
];
// Associate values with targets
targets.prop_flat_map(|targets| {
let len = targets.len();
let values = prop::collection::vec(any::<u64>(), len);
(Just(targets), values)
.prop_map(|(targets, values)| targets.into_iter().zip(values).collect::<Vec<_>>())
})
}
//
type TargetsAndValues = Vec<(&'static TopologyObject, u64)>;
/// Given a set of flags, targets and values, pick a matching set of initiators
fn initiators(
flags: MemoryAttributeFlags,
targets_and_values: &TargetsAndValues,
) -> impl Strategy<Value = Initiators> + use<> {
// Query basic topology properties
let topology = Topology::test_instance();
let num_objects = topology.objects().count();
// Pick a number of initiators that may or may not be correct
let num_values = targets_and_values.len();
let num_initiators = prop_oneof![
4 => Just(num_values),
1 => 0..=num_objects,
];
// Pick initiators
let initiators = num_initiators
.prop_flat_map(move |num_initiators| {
// Determine how many initiators will be cpusets, knowing
// that there has to be at least one cpu in the CPUset so we
// cannot have more cpuset initiators than we have CPUs.
let num_cpus = topology.complete_cpuset().weight().unwrap();
// Until hwloc PR #725, which I hope will be integrated in hwloc
// v3.0.0, object initiators were badly handled and should not
// be used. So on newer hwloc we're fine...
if cfg!(feature = "hwloc-3_0_0") {
(Just(num_initiators), 0..=num_initiators.min(num_cpus)).boxed()
} else {
// ...but on older hwloc releases we mostly stick with
// cpuset initiators, as object initiators are treated as
// an error anyway.
let good_initiators = num_initiators.min(num_cpus);
prop_oneof![
4 => (Just(good_initiators), Just(good_initiators)),
1 => (Just(num_initiators), 0..=num_initiators.min(num_cpus))
]
.boxed()
}
})
.prop_flat_map(move |(num_initiators, num_initiator_sets)| {
// Pick initiator cpusets, starting from a full list of CPUs
let correct_initiator_sets = prop::sample::subsequence(
topology.complete_cpuset().iter_set().collect::<Vec<_>>(),
num_initiator_sets,
)
.prop_flat_map(move |first_cpu_per_set| {
// Start by allocating one CPU to each initiator
// cpuset, so that none is empty. Remove them
// from the pool of available CPUs.
let mut remaining_cpus = topology.complete_cpuset().clone_target();
for &cpu in &first_cpu_per_set {
remaining_cpus.unset(cpu)
}
// Allocate remaining CPUs to cpusets randomly,
// leaving some CPUs unallocated.
let set_index = if num_initiator_sets == 0 {
Just(None).boxed()
} else {
prop_oneof![Just(None), (0..num_initiator_sets).prop_map(Some)].boxed()
};
prop::collection::vec(set_index, remaining_cpus.weight().unwrap()).prop_map(
move |set_indices| {
let mut cpusets = first_cpu_per_set
.iter()
.copied()
.map(CpuSet::from)
.collect::<Vec<_>>();
for (cpu, set_idx) in remaining_cpus.iter_set().zip(set_indices) {
if let Some(set_idx) = set_idx {
cpusets[set_idx].set(cpu);
}
}
cpusets
},
)
});
let initiator_sets = prop_oneof![
4 => correct_initiator_sets,
1 => prop::collection::vec(
topology_related_set(Topology::complete_cpuset),
num_initiator_sets,
)
];
// Pick initiator objects
let num_initiator_objs = num_initiators - num_initiator_sets;
let initiator_objs = if num_initiator_objs <= num_objects {
prop_oneof![
4 => prop::sample::subsequence(
topology.objects().collect::<Vec<_>>(),
num_initiator_objs
),
1 => prop::collection::vec(
any_object(),
num_initiator_objs
)
]
.boxed()
} else {
prop::collection::vec(any_object(), num_initiator_objs).boxed()
};
// Put cpusets and objects together, randomize order
(initiator_sets, initiator_objs)
.prop_map(|(sets, objs)| {
sets.into_iter()
.map(Initiator::from)
.chain(objs.into_iter().map(Initiator::from))
.collect::<Vec<_>>()
})
.prop_shuffle()
});
// Provide initiators or not, in such a way that the correct case
// has a reasonably good chance of occuring
if flags.contains(MemoryAttributeFlags::NEED_INITIATOR) {
prop_oneof![
1 => Just(None),
4 => initiators.prop_map(Some)
]
} else {
prop_oneof![
3 => Just(None),
2 => initiators.prop_map(Some)
]
}
}
//
type Initiators = Option<Vec<Initiator<'static>>>;
/// Pick memory attribute building blocks that have a high chance of being
/// consistent with each other, but may not be
fn attribute_building_blocks() -> impl Strategy<Value = AttributeBuildingBlocks> {
// Pick attribute flags
let flags = any::<MemoryAttributeFlags>();
// Given a (flags, targets, values) configuration...
(flags, targets_and_values()).prop_flat_map(move |(flags, targets_and_values)| {
// ...set up a set of matching initiators
let initiators = initiators(flags, &targets_and_values);
// Randomly inject duplicate (initiator, target) pairs
let initiators_targets_values = (initiators, Just(targets_and_values)).prop_flat_map(
move |(initiators, targets_and_values)| {
// Can't do this if there are no values
let num_values = targets_and_values.len();
if num_values == 0 {
return (Just(initiators), Just(targets_and_values)).boxed();
}
// Otherwise stochastically insert one duplicate
prop_oneof![
3 => (Just(initiators.clone()), Just(targets_and_values.clone())),
2 => (Just(initiators), Just(targets_and_values), 0..num_values, any::<u64>(), 0..=num_values)
.prop_flat_map(move |(mut initiators, mut targets_and_values, dupe_src_idx, dupe_value, dupe_dst_idx)| {
let dupe_target = targets_and_values[dupe_src_idx].0;
targets_and_values.insert(dupe_dst_idx, (dupe_target, dupe_value));
if let Some(initiators) = &mut initiators {
if initiators.len() == num_values {
let dupe_initiator = initiators[dupe_src_idx].clone();
initiators.insert(dupe_dst_idx, dupe_initiator);
}
}
(Just(initiators), Just(targets_and_values))
})
].boxed()
},
);
// And add back the flags at the end
initiators_targets_values.prop_map(move |(initiators, targets_and_values)| {
AttributeBuildingBlocks { flags, initiators, targets_and_values }
})
})
}
//
#[derive(Debug)]
struct AttributeBuildingBlocks {
flags: MemoryAttributeFlags,
initiators: Initiators,
targets_and_values: TargetsAndValues,
}
proptest! {
#[test]
fn build_any_attribute(
name in any_string(),
building_blocks in attribute_building_blocks(),
) {
check_build_any_attribute(name, building_blocks)?;
}
}
/// Reproducer for the object initiator bug that is fixed by [hwloc PR
/// #725](https://github.com/open-mpi/hwloc/pull/725), which I expect to be
/// integrated in hwloc v3.0.0.
#[test]
fn build_any_attribute_hwloc725() {
let initial_topology = Topology::test_instance();
let name = "test".to_owned();
let building_blocks = AttributeBuildingBlocks {
flags: MemoryAttributeFlags::HIGHER_IS_BEST | MemoryAttributeFlags::NEED_INITIATOR,
initiators: Some(vec![
initial_topology
.objects_with_type(ObjectType::PU)
.next()
.unwrap()
.into(),
initial_topology
.objects_with_type(ObjectType::PU)
.nth(1)
.unwrap()
.into(),
]),
targets_and_values: vec![
(
initial_topology
.objects_with_type(ObjectType::NUMANode)
.next()
.unwrap(),
1,
),
(
initial_topology
.objects_with_type(ObjectType::NUMANode)
.next()
.unwrap(),
2,
),
],
};
check_build_any_attribute(name, building_blocks).unwrap();
}
/// Implementation of the `build_any_attribute` proptest
fn check_build_any_attribute(
name: String,
building_blocks: AttributeBuildingBlocks,
) -> Result<(), TestCaseError> {
// Set up work topology from reference topology
let initial_topology = Topology::test_instance();
let mut topology = initial_topology.clone();
// Predict value setup errors
let overlapping_values = has_duplicates(&building_blocks);
let (inconsistent_data_len, foreign_initiator, object_initiator) = if let Some(initiators) =
&building_blocks.initiators
{
(
initiators.len() != building_blocks.targets_and_values.len(),
initiators.iter().any(|initiator| match initiator {
Initiator::CpuSet(set) => {
set.is_empty() || !initial_topology.complete_cpuset().includes(set.as_ref())
}
Initiator::Object(obj) => !initial_topology.contains(obj),
}),
cfg!(not(feature = "hwloc-3_0_0"))
&& initiators
.iter()
.any(|initiator| matches!(initiator, Initiator::Object(_))),
)
} else {
(false, false, false)
};
let foreign_target = building_blocks
.targets_and_values
.iter()
.any(|(target, _value)| !initial_topology.contains(target));
let need_initiator = building_blocks
.flags
.contains(MemoryAttributeFlags::NEED_INITIATOR)
&& building_blocks.initiators.is_none();
let unwanted_initiator = !building_blocks
.flags
.contains(MemoryAttributeFlags::NEED_INITIATOR)
&& building_blocks.initiators.is_some();
let will_error = inconsistent_data_len
|| overlapping_values
|| foreign_initiator
|| foreign_target
|| need_initiator
|| object_initiator
|| unwanted_initiator;
// Start editing the topology
topology.edit(|editor| {
// Register the memory attribute
let builder = editor.register_memory_attribute(&name, building_blocks.flags);
let Some(mut builder) = check_register_errors(&name, building_blocks.flags, builder)?
else {
prop_assert_eq!(editor.topology(), initial_topology);
return Ok(());
};
// Back up the topology before value setup
let topology_with_empty_attribute = builder.editor.topology().clone();
// Run the value setup
let res = builder.set_values(|final_topology| {
translate_building_blocks(
initial_topology,
building_blocks.initiators.as_deref(),
&building_blocks.targets_and_values,
final_topology,
)
});
// Check the result against expectation
match res {
Ok(()) => prop_assert!(!will_error),
Err(HybridError::Rust(e)) => {
match e {
ValueInputError::InconsistentDataLen => prop_assert!(inconsistent_data_len),
ValueInputError::OverlappingValues => prop_assert!(overlapping_values),
ValueInputError::BadInitiators(bi) => match bi {
InitiatorInputError::ForeignInitiator(_) => {
prop_assert!(foreign_initiator)
}
InitiatorInputError::NeedInitiator(_) => prop_assert!(need_initiator),
InitiatorInputError::UnwantedInitiator(_) => {
prop_assert!(unwanted_initiator)
}
},
ValueInputError::ObjectInitiator => prop_assert!(object_initiator),
ValueInputError::ForeignTarget(_) => prop_assert!(foreign_target),
}
prop_assert_eq!(editor.topology(), &topology_with_empty_attribute);
return Ok(());
}
Err(other) => panic!("unexpected error: {other}"),
}
// If control reached this point, the memory attribute should
// have been added. Check the new topology state.
check_new_topology_attribute(
initial_topology,
&name,
&building_blocks,
editor.topology(),
)?;
Ok(())
})?;
Ok(())
}
/// Truth that [`AttributeBuildingBlocks`] contain duplicate entries
fn has_duplicates(building_blocks: &AttributeBuildingBlocks) -> bool {
if let Some(initiators) = &building_blocks.initiators {
let mut set = InitiatorTargetSet::new();
for (initiator, (target, _value)) in
initiators.iter().zip(&building_blocks.targets_and_values)
{
if !set.insert(initiator, target) {
return true;
}
}
} else {
let mut hash_set = HashSet::with_capacity(building_blocks.targets_and_values.len());
for (target, _value) in &building_blocks.targets_and_values {
if !hash_set.insert(target.global_persistent_index()) {
return true;
}
}
}
false
}
/// Translate an initiator from one topology to another
fn translate_building_blocks<'out, 'initial: 'out, 'final_: 'out>(
initial_topology: &Topology,
initiators: Option<&[Initiator<'initial>]>,
targets_and_values: &[(&'initial TopologyObject, u64)],
final_topology: &'final_ Topology,
) -> (
Option<Vec<Initiator<'out>>>,
Vec<(&'out TopologyObject, u64)>,
) {
let id_to_object = final_topology
.objects()
.map(|obj| (obj.global_persistent_index(), obj))
.collect::<HashMap<_, _>>();
let initiators = initiators.as_ref().map(|initiators| {
initiators
.iter()
.cloned()
.map(|initiator| match initiator {
Initiator::Object(obj) => {
let obj = if initial_topology.contains(obj) {
id_to_object[&obj.global_persistent_index()]
} else {
obj
};
Initiator::from(obj)
}
Initiator::CpuSet(set) => Initiator::from(set),
})
.collect::<Vec<_>>()
});
let targets_and_values = targets_and_values
.iter()
.copied()
.map(|(target, value)| {
let target = if initial_topology.contains(target) {
id_to_object[&target.global_persistent_index()]
} else {
target
};
(target, value)
})
.collect::<Vec<_>>();
(initiators, targets_and_values)
}
/// Part of [`build_any_attribute()`] that checks topology state after
/// successfully adding an attribute
fn check_new_topology_attribute(
initial_topology: &Topology,
name: &str,
building_blocks: &AttributeBuildingBlocks,
final_topology: &Topology,
) -> Result<(), TestCaseError> {
// Attributes that used to be around are still around, with the same values
let mut initial_attrs = MemoryAttribute::all(initial_topology);
let mut final_attrs = MemoryAttribute::all(final_topology);
let new_attr = loop {
match (initial_attrs.next(), final_attrs.next()) {
(Some(i), Some(f)) => {
prop_assert!(AttributeDump::new(i).eq_modulo_topology(&AttributeDump::new(f)))
}
(None, Some(f)) => break f,
(_, None) => unreachable!(),
}
};
// New attribute has the expected properties
prop_assert_eq!(new_attr.name().to_str().unwrap(), name);
prop_assert_eq!(new_attr.flags(), building_blocks.flags);
let (initiators, targets_and_values) = translate_building_blocks(
initial_topology,
building_blocks.initiators.as_deref(),
&building_blocks.targets_and_values,
final_topology,
);
let mut id_to_target = HashMap::new();
let target_id_to_value = if let Some(initiators) = &initiators {
// Query individual values, preparing for subsequent bulk queries
for (initiator, (target, expected_value)) in initiators.iter().zip(&targets_and_values)
{
prop_assert_eq!(
new_attr.value(Some(initiator.clone()), target),
Ok(Some(*expected_value))
);
let target_id = target.global_persistent_index();
id_to_target.entry(target_id).or_insert(target);
}
// No target-to-value mapping if there is an initiator
None
} else {
// Query individual values, preparing for subsequent bulk queries
let mut target_id_to_value = HashMap::new();
for (target, expected_value) in &targets_and_values {
prop_assert_eq!(new_attr.value(None, target), Ok(Some(*expected_value)));
let target_id = target.global_persistent_index();
id_to_target.entry(target_id).or_insert(target);
prop_assert!(
target_id_to_value
.insert(target_id, *expected_value)
.is_none()
);
}
// There is no initiator, so there's a target-to-value mapping
Some(target_id_to_value)
};
// Query full target list
let expected_target_ids = id_to_target.keys().copied().collect::<HashSet<_>>();
let (targets, values) = new_attr.targets(None).unwrap();
prop_assert_eq!(
targets
.iter()
.map(|target| target.global_persistent_index())
.collect::<HashSet<_>>(),
expected_target_ids,
);
let expected_values = target_id_to_value.map(|mut target_id_to_value| {
targets
.into_iter()
.map(|target| {
target_id_to_value
.remove(&target.global_persistent_index())
.unwrap()
})
.collect()
});
prop_assert_eq!(values, expected_values);
// New attribute should not compare equal to existing attributes because
// it does not have the same ID.
let new_dump = AttributeDump::new(new_attr);
for initial_attr in MemoryAttribute::all(initial_topology) {
let initial_dump = AttributeDump::new(initial_attr);
prop_assert!(!new_dump.eq_modulo_topology(&initial_dump));
}
// Test attribute dumps using this new attribute dump, which has the
// advantage of having contents under our control.
test_attribute_dump(new_dump)?;
Ok(())
}
/// Test attribute dumps, used for debugging and topology comparison
fn test_attribute_dump(dump: AttributeDump<'_>) -> Result<(), TestCaseError> {
// Dump should compare equal to itself
prop_assert!(dump.eq_modulo_topology(&dump));
// Can't do more if the dump doesn't have targets
if dump.targets.0.is_empty() {
return Ok(());
}
// Dump should not compare equal to another dump with less targets
{
let mut less_targets = dump.clone();
less_targets.targets.0.pop();
prop_assert!(!dump.eq_modulo_topology(&less_targets));
}
// Can't do more if the dump doesn't have 2+ targets
if dump.targets.0.len() < 2 {
return Ok(());
}
// Dump should not compare equal to another dump with targets swapped
{
let mut swapped_targets = dump.clone();
swapped_targets.targets.0.swap(0, 1);
prop_assert!(!dump.eq_modulo_topology(&swapped_targets));
}
// Need an attribute with initiators for the next tests
if !dump
.attribute
.flags()
.contains(MemoryAttributeFlags::NEED_INITIATOR)
{
return Ok(());
}
// Replacing a target with initiators with one without initiators should
// make the comparison fail.
{
let mut without_initiator = dump.clone();
without_initiator.targets.0[0].initiators_and_values =
InitiatorsAndValues::NoInitiator(42);
prop_assert!(!dump.eq_modulo_topology(&without_initiator));
prop_assert!(!without_initiator.eq_modulo_topology(&dump));
}
// Changing the number of initiators for one target should make the
// comparison fail
{
let mut less_initiators = dump.clone();
let InitiatorsAndValues::HasInitiators { initiators, values } =
&mut less_initiators.targets.0[0].initiators_and_values
else {
panic!("attribute with NEED_INITIATOR should have an initiator")
};
initiators.pop();
values.pop();
prop_assert!(!dump.eq_modulo_topology(&less_initiators));
}
// Changing one value should make the comparison fail
{
let mut different_value = dump.clone();
let InitiatorsAndValues::HasInitiators { values, .. } =
&mut different_value.targets.0[0].initiators_and_values
else {
panic!("attribute with NEED_INITIATOR should have an initiator")
};
values[0] = values[0].wrapping_add(1);
prop_assert!(!dump.eq_modulo_topology(&different_value));
}
// Heterogeneous initiators should make the comparison fail
{
let topology = dump.attribute.topology;
let mut different_initiator_type = dump.clone();
let InitiatorsAndValues::HasInitiators { initiators, .. } =
&mut different_initiator_type.targets.0[0].initiators_and_values
else {
panic!("attribute with NEED_INITIATOR should have an initiator")
};
match &mut initiators[0] {
r @ Initiator::CpuSet(_) => *r = topology.root_object().into(),
r @ Initiator::Object(_) => *r = topology.complete_cpuset().into(),
};
prop_assert!(!dump.eq_modulo_topology(&different_initiator_type));
prop_assert!(!different_initiator_type.eq_modulo_topology(&dump));
}
// Debug impl should not crash
let _debug_didnt_crash = format!("{dump:?}");
Ok(())
}
// Test Initiator constructors
proptest! {
/// Test initiator construction from cpuset
#[test]
fn initiator_from_cpuset(cset in topology_related_set(Topology::complete_cpuset)) {
{
let from_owned = Initiator::from(cset.clone());
prop_assert!(matches!(
&from_owned,
Initiator::CpuSet(cset2) if *cset2 == cset
));
check_numa_initiator(from_owned)?;
}
{
let from_rust_ref = Initiator::from(&cset);
prop_assert!(matches!(
&from_rust_ref,
Initiator::CpuSet(cset2) if *cset2 == cset
));
check_numa_initiator(from_rust_ref)?;
}
{
let from_bitmap_ref = Initiator::from(BitmapRef::from(&cset));
prop_assert!(matches!(
&from_bitmap_ref,
Initiator::CpuSet(cset2) if *cset2 == cset
));
check_numa_initiator(from_bitmap_ref)?;
}
{
let from_cow_ref = Initiator::from(BitmapCow::from(&cset));
prop_assert!(matches!(
&from_cow_ref,
Initiator::CpuSet(cset2) if *cset2 == cset
));
check_numa_initiator(from_cow_ref)?;
}
{
let from_cow_owned = Initiator::from(BitmapCow::from(cset.clone()));
prop_assert!(matches!(
&from_cow_owned,
Initiator::CpuSet(cset2) if *cset2 == cset
));
check_numa_initiator(from_cow_owned)?;
}
}
/// Test initiator construction from topology object
#[test]
fn initiator_from_object(obj in any_object()) {
let from_obj = Initiator::from(obj);
prop_assert!(matches!(
&from_obj,
Initiator::Object(obj2)
if obj.global_persistent_index() == obj2.global_persistent_index()
));
check_numa_initiator(from_obj)?;
}
}
/// Test conversion of [`Initiator`] to [`NUMAInitiator`]
fn check_numa_initiator(initiator: Initiator<'_>) -> Result<(), TestCaseError> {
let numa = NUMAInitiator::from(initiator.clone());
let NUMAInitiator::Local {
initiator: initiator2,
flags,
} = numa
else {
panic!("Initiator to NUMAInitiator conversion should produce a local NUMA initiator");
};
prop_assert_eq!(flags, LocalNUMANodeFlags::empty());
match (initiator, initiator2) {
(Initiator::CpuSet(cset), Initiator::CpuSet(cset2)) => {
prop_assert_eq!(cset, cset2);
}
(Initiator::Object(obj), Initiator::Object(obj2)) => {
prop_assert_eq!(
obj.global_persistent_index(),
obj2.global_persistent_index()
);
}
(Initiator::CpuSet(_), Initiator::Object(_))
| (Initiator::Object(_), Initiator::CpuSet(_)) => {
panic!("Initiator to NUMAInitiator conversion should preserve initiator type")
}
}
Ok(())
}
}