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
//! Memory binding
//!
//! This module is all about checking and changing the binding of memory
//! allocations to hardware NUMA nodes.
//!
//! Most of this module's functionality is exposed via [methods of the Topology
//! struct](../../topology/struct.Topology.html#memory-binding). The module
//! itself only hosts type definitions that are related to this functionality.
use crate::{
bitmap::{Bitmap, BitmapKind, OwnedSpecializedBitmap, SpecializedBitmap},
errors::{self, FlagsError, RawHwlocError},
memory::nodeset::NodeSet,
topology::Topology,
ProcessId,
};
#[cfg(doc)]
use crate::{cpu::cpuset::CpuSet, topology::support::MemoryBindingSupport};
use bitflags::bitflags;
use derive_more::Display;
use enum_iterator::Sequence;
use errno::Errno;
use hwlocality_sys::{
hwloc_bitmap_t, hwloc_const_bitmap_t, hwloc_const_topology_t, hwloc_membind_flags_t,
hwloc_membind_policy_t, hwloc_pid_t, HWLOC_MEMBIND_BIND, HWLOC_MEMBIND_BYNODESET,
HWLOC_MEMBIND_DEFAULT, HWLOC_MEMBIND_FIRSTTOUCH, HWLOC_MEMBIND_INTERLEAVE,
HWLOC_MEMBIND_MIGRATE, HWLOC_MEMBIND_MIXED, HWLOC_MEMBIND_NEXTTOUCH, HWLOC_MEMBIND_NOCPUBIND,
HWLOC_MEMBIND_PROCESS, HWLOC_MEMBIND_STRICT, HWLOC_MEMBIND_THREAD,
};
use libc::{ENOMEM, ENOSYS, EXDEV};
use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
#[allow(unused)]
#[cfg(test)]
use similar_asserts::assert_eq;
use std::{
borrow::{Borrow, BorrowMut},
ffi::{c_int, c_void},
fmt::{self, Debug, Display},
mem::MaybeUninit,
ops::{Deref, DerefMut},
ptr::{self, NonNull},
};
use thiserror::Error;
/// # Memory binding
///
/// Memory binding can be done three ways:
///
/// - Explicit memory allocation through [`allocate_bound_memory()`] and friends:
/// the binding will have effect on the memory allocated by these methods.
/// - Implicit memory binding through process/thread binding policy through
/// [`bind_memory()`] and friends: the binding will be applied to subsequent
/// memory allocations by the target process/thread.
/// - Migration of existing memory ranges through [`bind_memory_area()`] and
/// friends: already-allocated data will be migrated to the target NUMA
/// nodes.
///
/// Not all operating systems support all three ways.
/// [`Topology::feature_support()`] may be used to query about the actual memory
/// binding support in the currently used operating system. Individual memory
/// binding methods will clarify which support flags they require. The most
/// portable operation, where usable, is [`binding_allocate_memory()`].
///
/// By default, when the requested binding operation is not available, hwloc
/// will go for a similar binding operation (with side-effects, smaller
/// binding set, etc). You can inhibit this with flag [`STRICT`], at the
/// expense of reducing portability across operating systems.
///
/// Memory can be bound by [`CpuSet`] or [`NodeSet`], but memory binding by
/// CPU set cannot work for CPU-less NUMA memory nodes. Binding by node set
/// should therefore be preferred whenever possible.
///
/// You should specify one of the [`ASSUME_SINGLE_THREAD`], [`PROCESS`] and
/// [`THREAD`] flags (the former being best for portability) when using any of
/// the methods that target a process, but some methods may only support a
/// subset of these flags.
///
/// On some operating systems, memory binding affects CPU binding. You can avoid
/// this at the cost of reducing portability by specifying the
/// [`NO_CPU_BINDING`] flag.
///
/// [`allocate_bound_memory()`]: Topology::allocate_bound_memory()
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`bind_memory_area()`]: Topology::bind_memory_area()
/// [`bind_memory()`]: Topology::bind_memory()
/// [`binding_allocate_memory()`]: Topology::binding_allocate_memory()
/// [`NO_CPU_BINDING`]: MemoryBindingFlags::NO_CPU_BINDING
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`THREAD`]: MemoryBindingFlags::THREAD
//
// --- Implementation details ---
//
// Upstream docs: https://hwloc.readthedocs.io/en/v2.9/group__hwlocality__membinding.html
impl Topology {
/// Allocate some memory
///
/// This is equivalent to [`libc::malloc()`], except that it tries to
/// allocate page-aligned memory from the OS.
///
/// # Errors
///
/// - [`AllocationFailed`] if memory allocation failed
/// - [`Unsupported`] if the system cannot allocate page-aligned memory
///
/// [`AllocationFailed`]: MemoryBindingError::AllocationFailed
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_alloc")]
pub fn allocate_memory(&self, len: usize) -> Result<Bytes<'_>, MemoryAllocationError<NodeSet>> {
self.allocate_memory_generic(len)
}
/// Like [`allocate_memory()`], but polymorphic on `OwnedSet`
///
/// [`allocate_memory`]: Topology::allocate_memory
fn allocate_memory_generic<OwnedSet: OwnedSpecializedBitmap>(
&self,
len: usize,
) -> Result<Bytes<'_>, MemoryAllocationError<OwnedSet>> {
// SAFETY: - hwloc_alloc is accepted by definition
// - FFI is guaranteed to be passed valid (topology, len)
unsafe {
self.allocate_memory_impl("hwloc_alloc", &|| None, len, |topology, len| {
hwlocality_sys::hwloc_alloc(topology, len)
})
}
}
/// Allocate some memory on NUMA nodes specified by `set`
///
/// If you do not care about changing the binding of the current process or
/// thread, you can maximize portability by using
/// [`Topology::binding_allocate_memory()`] instead.
///
/// Memory can be bound by either [`CpuSet`] or [`NodeSet`]. Binding by
/// [`NodeSet`] is preferred because some NUMA memory nodes are not attached
/// to CPUs, and thus cannot be bound by [`CpuSet`].
///
/// Binding target flags [`ASSUME_SINGLE_THREAD`], [`PROCESS`],
/// [`THREAD`] and [`MIGRATE`] should not be used with this method.
///
/// Requires [`MemoryBindingSupport::allocate_bound()`].
///
/// # Errors
///
/// - [`AllocationFailed`] if memory allocation failed
/// - [`BadFlags`] if binding target flags were specified
/// - [`BadSet`] if the system can't bind memory to that CPU/node set
/// - [`Unsupported`] if the system cannot allocate bound memory with the
/// requested policy
///
/// [`AllocationFailed`]: MemoryBindingError::AllocationFailed
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadSet`]: MemoryBindingError::BadSet
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_alloc_membind")]
pub fn allocate_bound_memory<Set: SpecializedBitmap>(
&self,
len: usize,
set: &Set,
policy: MemoryBindingPolicy,
mut flags: MemoryBindingFlags,
) -> Result<Bytes<'_>, MemoryAllocationError<Set::Owned>> {
Self::adjust_flags_for::<Set::Owned>(&mut flags);
let Some(flags) = flags.validate(MemoryBoundObject::Area, MemoryBindingOperation::Allocate)
else {
return Err(MemoryBindingError::BadFlags(flags.into()));
};
// SAFETY: - Bitmap is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - hwloc_alloc_membind with set, policy and flags
// arguments curried away behaves like hwloc_alloc
// - FFI is guaranteed to be passed valid (topology, len)
// - All user-visible policies are accepted by hwloc
// - flags are validated to be correct
unsafe {
self.allocate_memory_impl(
"hwloc_alloc_membind",
&|| Some(set.to_owned()),
len,
|topology, len| {
hwlocality_sys::hwloc_alloc_membind(
topology,
len,
set.as_ref().as_ptr(),
policy.into(),
flags.bits(),
)
},
)
}
}
/// Allocate some memory on NUMA nodes specified by `set` and `flags`,
/// possibly rebinding current process or thread if needed
///
/// This works like [`Topology::allocate_bound_memory()`] unless the
/// allocation fails, in which case hwloc will attempt to change the current
/// process or thread memory binding policy as directed instead before
/// performing a normal allocation.
///
/// Allocating memory that matches the current process/thread configuration
/// is supported on more operating systems, so this is the most portable way
/// to obtain a bound memory buffer.
///
/// You must specify exactly one of the [`ASSUME_SINGLE_THREAD`],
/// [`PROCESS`] and [`THREAD`] binding target flags when using this method.
///
/// Requires either [`MemoryBindingSupport::allocate_bound()`], or one of
/// [`MemoryBindingSupport::set_current_process()`] and
/// [`MemoryBindingSupport::set_current_thread()`] depending on flags.
///
/// # Errors
///
/// - [`AllocationFailed`] if memory allocation failed
/// - [`BadFlags`] if the number of specified binding target flags is not
/// exactly one
/// - [`BadSet`] if the system can't bind memory to that CPU/node set
/// - [`Unsupported`] if the system can neither allocate bound memory
/// nor rebind the current thread/process with the requested policy
///
/// [`AllocationFailed`]: MemoryBindingError::AllocationFailed
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadSet`]: MemoryBindingError::BadSet
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_alloc_membind_policy")]
pub fn binding_allocate_memory<Set: SpecializedBitmap>(
&self,
len: usize,
set: &Set,
policy: MemoryBindingPolicy,
flags: MemoryBindingFlags,
) -> Result<Bytes<'_>, MemoryAllocationError<Set::Owned>> {
// Try allocate_bound_memory first
let set: &Set::Owned = set.borrow();
if let Ok(bytes) = self.allocate_bound_memory(len, set, policy, flags) {
return Ok(bytes);
}
// If that doesn't work, try binding the current process
self.bind_memory(set, policy, flags)?;
// If that succeeds, try allocating more memory
let mut bytes = self.allocate_memory_generic::<Set::Owned>(len)?;
// Depending on policy, we may or may not need to touch the memory to
// enforce the binding
match policy {
// Nothing to do, user expects first/next-touch lazy behavior
MemoryBindingPolicy::FirstTouch | MemoryBindingPolicy::NextTouch => {}
// All other cases expect eager binding, which may require touching
// to enforce
MemoryBindingPolicy::Bind | MemoryBindingPolicy::Interleave => {
bytes.fill(MaybeUninit::new(0));
}
}
Ok(bytes)
}
/// Set the default memory binding policy of the current process or thread
/// to prefer the NUMA node(s) specified by `set`.
///
/// Memory can be bound by either [`CpuSet`] or [`NodeSet`]. Binding by
/// [`NodeSet`] is preferred because some NUMA memory nodes are not attached
/// to CPUs, and thus cannot be bound by [`CpuSet`].
///
/// You must specify exactly one of the [`ASSUME_SINGLE_THREAD`],
/// [`PROCESS`] and [`THREAD`] binding target flags when using this method.
///
/// Requires [`MemoryBindingSupport::set_current_process()`] or
/// [`MemoryBindingSupport::set_current_thread()`] depending on flags.
///
/// # Errors
///
/// - [`BadFlags`] if the number of specified binding target flags is not
/// exactly one
/// - [`BadSet`] if the system can't bind memory to that CPU/node set
/// - [`Unsupported`] if the system cannot bind the current
/// thread/process with the requested policy
///
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadSet`]: MemoryBindingError::BadSet
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_set_membind")]
pub fn bind_memory<Set: SpecializedBitmap>(
&self,
set: &Set,
policy: MemoryBindingPolicy,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<Set::Owned>> {
// SAFETY: - ThisProgram is the correct target for this FFI
// - hwloc_set_membind is accepted by definition
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
unsafe {
self.bind_memory_impl(
"hwloc_set_membind",
set.borrow(),
policy,
flags,
MemoryBoundObject::ThisProgram,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_membind(topology, set, policy, flags)
},
)
}
}
/// Reset the memory allocation policy of the current process or thread to
/// the system default
///
/// Depending on the operating system, this may correspond to
/// [`MemoryBindingPolicy::FirstTouch`] (Linux, FreeBSD) or
/// [`MemoryBindingPolicy::Bind`] (AIX, HP-UX, Solaris, Windows).
///
/// You must specify exactly one of the [`ASSUME_SINGLE_THREAD`],
/// [`PROCESS`] and [`THREAD`] binding target flags when using this method,
/// but the [`STRICT`] and [`MIGRATE`] flags should **not** be used with
/// this method.
///
/// Requires [`MemoryBindingSupport::set_current_process()`] or
/// [`MemoryBindingSupport::set_current_thread()`] depending on flags.
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`STRICT`] and [`MIGRATE`] was specified,
/// or if the number of specified binding target flags is not exactly
/// one
/// - [`Unsupported`] if the system cannot unbind the current thread/process
///
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "HWLOC_MEMBIND_DEFAULT")]
pub fn unbind_memory(
&self,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<NodeSet>> {
// SAFETY: - ThisProgram is the correct target for this FFI
// - hwloc_set_membind is accepted by definition
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
unsafe {
self.unbind_memory_impl(
"hwloc_set_membind",
flags,
MemoryBoundObject::ThisProgram,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_membind(topology, set, policy, flags)
},
)
}
}
/// Query the default memory binding policy and physical locality of the
/// current process or thread
///
/// You must specify one of the [`ASSUME_SINGLE_THREAD`], [`PROCESS`] and
/// [`THREAD`] binding target flags when using this method. However, flags
/// [`MIGRATE`] and [`NO_CPU_BINDING`] should **not** be used with this
/// method.
///
/// The [`STRICT`] flag is only meaningful when [`PROCESS`] is also
/// specified. In this case, hwloc will check the default memory policies
/// and nodesets for all threads in the process. If they are not identical,
/// Err([`MixedResults`]) is returned. Otherwise,
/// the shared configuration is returned.
///
/// Otherwise, if [`PROCESS`] is specified and [`STRICT`] is not specified,
/// the default sets from each thread are logically OR'ed together. If all
/// threads' default policies are the same, that shared policy is returned,
/// otherwise no policy is returned.
///
/// In the [`THREAD`] and [`ASSUME_SINGLE_THREAD`] case, there is only one
/// set and policy, they are returned.
///
/// Bindings can be queried as [`CpuSet`] or [`NodeSet`]. Querying by
/// [`NodeSet`] is preferred because some NUMA memory nodes are not attached
/// to CPUs, and thus cannot be bound by [`CpuSet`].
///
/// Requires [`MemoryBindingSupport::get_current_process()`] or
/// [`MemoryBindingSupport::get_current_thread()`] depending on flags.
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`] and [`NO_CPU_BINDING`] was
/// specified, if flag [`STRICT`] was specified without [`PROCESS`], or
/// if the number of specified binding target flags is not exactly one
/// - [`MixedResults`] if flags [`STRICT`] and [`PROCESS`] were specified
/// and memory binding is inhomogeneous across threads in the process
/// - [`Unsupported`] if the system cannot query the current thread/process
/// binding
///
/// [`ASSUME_SINGLE_THREAD`]: MemoryBindingFlags::ASSUME_SINGLE_THREAD
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`MixedResults`]: MemoryBindingError::MixedResults
/// [`NO_CPU_BINDING`]: MemoryBindingFlags::NO_CPU_BINDING
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_get_membind")]
pub fn memory_binding<OwnedSet: OwnedSpecializedBitmap>(
&self,
flags: MemoryBindingFlags,
) -> Result<(OwnedSet, Option<MemoryBindingPolicy>), MemoryBindingError<OwnedSet>> {
// SAFETY: - ThisProgram is the correct target for this FFI
// - GetBinding is the correct operation for this FFI
// - hwloc_get_membind is accepted by definition
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
unsafe {
self.memory_binding_impl(
"hwloc_get_membind",
flags,
MemoryBoundObject::ThisProgram,
MemoryBindingOperation::GetBinding,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_get_membind(topology, set, policy, flags)
},
)
}
}
/// Set the default memory binding policy of the specified process to prefer
/// the NUMA node(s) specified by `set`.
///
/// See also [`Topology::bind_memory()`] for general semantics, except
/// binding target flag [`THREAD`] should not be used with this method, and
/// it requires [`MemoryBindingSupport::set_process()`].
///
/// # Errors
///
/// - [`BadFlags`] if flag [`THREAD`] was specified, or if the number of
/// specified binding target flags is not exactly one
/// - [`BadSet`] if the system can't bind memory to that CPU/node set
/// - [`Unsupported`] if the system cannot bind the specified
/// thread/process with the requested policy
///
/// # Panics
///
/// Some operating systems use signed PIDs, and do not support PIDs greater
/// than `i32::MAX`. This method will panic when passed such an obviously
/// invalid PID on these operating systems.
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadSet`]: MemoryBindingError::BadSet
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_set_proc_membind")]
pub fn bind_process_memory<Set: SpecializedBitmap>(
&self,
pid: ProcessId,
set: &Set,
policy: MemoryBindingPolicy,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<Set::Owned>> {
// SAFETY: - Process is the correct target for this FFI
// - hwloc_set_proc_membind with pid argument curried away
// behaves like hwloc_set_membind
// - FFI is guaranteed to be passed valid (topology,
// set, policy, flags)
// - PID cannot be validated (think TOCTOU), but hwloc should be
// able to handle an invalid PID
unsafe {
self.bind_memory_impl(
"hwloc_set_proc_membind",
set.borrow(),
policy,
flags,
MemoryBoundObject::Process(pid),
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_proc_membind(
topology,
hwloc_pid_t::try_from(pid).expect("shouldn't fail for a valid PID"),
set,
policy,
flags,
)
},
)
}
}
/// Reset the memory allocation policy of the specified process to the
/// system default
///
/// See also [`Topology::unbind_memory()`] for general semantics, except
/// binding target flag [`THREAD`] should not be used with this method, and
/// it requires [`MemoryBindingSupport::set_process()`].
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`], [`STRICT`] and [`THREAD`]
/// was specified, or if the number of specified binding target flags is
/// not exactly one
/// - [`Unsupported`] if the system cannot unbind the specified
/// thread/process
///
/// # Panics
///
/// Some operating systems use signed PIDs, and do not support PIDs greater
/// than `i32::MAX`. This method will panic when passed such an obviously
/// invalid PID on these operating systems.
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
pub fn unbind_process_memory(
&self,
pid: ProcessId,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<NodeSet>> {
// SAFETY: - Process is the correct target for this FFI
// - hwloc_set_proc_membind with pid argument curried away
// behaves like hwloc_set_membind
// - FFI is guaranteed to be passed valid (topology,
// set, policy, flags)
// - PID cannot be validated (think TOCTOU), but hwloc should be
// able to handle an invalid PID
unsafe {
self.unbind_memory_impl(
"hwloc_set_proc_membind",
flags,
MemoryBoundObject::Process(pid),
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_proc_membind(
topology,
hwloc_pid_t::try_from(pid).expect("shouldn't fail for a valid PID"),
set,
policy,
flags,
)
},
)
}
}
/// Query the default memory binding policy and physical locality of the
/// specified process
///
/// See [`Topology::memory_binding()`] for general semantics, except binding
/// target flag [`THREAD`] should not be used with this method, and it
/// requires [`MemoryBindingSupport::get_process()`].
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`], [`NO_CPU_BINDING`] and
/// [`THREAD`] was specified, if flag [`STRICT`] was specified without
/// [`PROCESS`], or if the number of specified binding target flags is
/// not exactly one
/// - [`MixedResults`] if flags [`STRICT`] and [`PROCESS`] were specified
/// and memory binding is inhomogeneous across threads in the process
/// - [`Unsupported`] if the system cannot query the specified
/// thread/process' binding
///
/// # Panics
///
/// Some operating systems use signed PIDs, and do not support PIDs greater
/// than `i32::MAX`. This method will panic when passed such an obviously
/// invalid PID on these operating systems.
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`MixedResults`]: MemoryBindingError::MixedResults
/// [`NO_CPU_BINDING`]: MemoryBindingFlags::NO_CPU_BINDING
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`THREAD`]: MemoryBindingFlags::THREAD
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_get_proc_membind")]
pub fn process_memory_binding<OwnedSet: OwnedSpecializedBitmap>(
&self,
pid: ProcessId,
flags: MemoryBindingFlags,
) -> Result<(OwnedSet, Option<MemoryBindingPolicy>), MemoryBindingError<OwnedSet>> {
// SAFETY: - Process is the correct target for this FFI
// - GetBinding is the correct operation for this FFI
// - hwloc_get_proc_membind with pid argument curried away
// behaves like hwloc_get_membind
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
// - PID cannot be validated (think TOCTOU), but hwloc should be
// able to handle an invalid PID
unsafe {
self.memory_binding_impl(
"hwloc_get_proc_membind",
flags,
MemoryBoundObject::Process(pid),
MemoryBindingOperation::GetBinding,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_get_proc_membind(
topology,
hwloc_pid_t::try_from(pid).expect("shouldn't fail for a valid PID"),
set,
policy,
flags,
)
},
)
}
}
/// Bind the memory identified by `target` to the NUMA node(s) specified by
/// `set`
///
/// Beware that only the memory directly targeted by the `target` reference
/// will be covered. So for example, you cannot pass in an `&Vec<T>` and
/// expect the Vec's contents to be covered, instead you must pass in the
/// `&[T]` corresponding to the Vec's contents as `&vec[..]`. You may want
/// to manually specify the `Target` type via turbofish to make sure that
/// you don't get tripped up by references of references like `&&[T]`.
///
/// See also [`Topology::bind_memory()`] for general semantics, except
/// binding target flags should not be used with this method, and it
/// requires [`MemoryBindingSupport::set_area()`].
///
/// # Errors
///
/// - [`BadFlags`] if a binding target flag was specified
/// - [`BadSet`] if the system can't bind memory to that CPU/node set
/// - [`BadTarget`] if `target` is a zero-sized object
/// - [`Unsupported`] if the system cannot bind the specified memory area
/// with the requested policy
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadSet`]: MemoryBindingError::BadSet
/// [`BadTarget`]: MemoryBindingError::BadTarget
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_set_area_membind")]
pub fn bind_memory_area<Target: ?Sized, Set: SpecializedBitmap>(
&self,
target: &Target,
set: &Set,
policy: MemoryBindingPolicy,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<Set::Owned>> {
let target_size = std::mem::size_of_val(target);
if target_size == 0 {
return Err(MemoryBindingError::BadTarget);
}
let target_ptr: *const Target = target;
// SAFETY: - Area is the correct target for this FFI
// - hwloc_set_area_membind with target_ptr and target_size
// arguments curried away behaves like hwloc_set_membind
// - FFI is guaranteed to be passed valid (topology,
// set, policy, flags)
// - target_ptr is valid as it originates from a & reference
// - target_size has been checked not to be zero
unsafe {
self.bind_memory_impl(
"hwloc_set_area_membind",
set.borrow(),
policy,
flags,
MemoryBoundObject::Area,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_area_membind(
topology,
target_ptr.cast::<c_void>(),
target_size,
set,
policy,
flags,
)
},
)
}
}
/// Reset the memory allocation policy of the memory identified by `target`
/// to the system default
///
/// The warning about `Target` coverage in the documentation of
/// [`Topology::bind_memory_area()`] also applies here.
///
/// See also [`Topology::unbind_memory()`] for general semantics, except
/// binding target flags should not be used with this method, and it
/// requires[`MemoryBindingSupport::set_area()`].
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`] and [`STRICT`] was specified,
/// or if a binding target flag was specified.
/// - [`BadTarget`] if `target` is a zero-sized object
/// - [`Unsupported`] if the system cannot unbind the specified memory area
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadTarget`]: MemoryBindingError::BadTarget
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`Unsupported`]: MemoryBindingError::Unsupported
pub fn unbind_memory_area<Target: ?Sized>(
&self,
target: &Target,
flags: MemoryBindingFlags,
) -> Result<(), MemoryBindingError<NodeSet>> {
let target_size = std::mem::size_of_val(target);
if target_size == 0 {
return Err(MemoryBindingError::BadTarget);
}
let target_ptr: *const Target = target;
// SAFETY: - Area is the correct target for this FFI
// - hwloc_set_area_membind with target_ptr and target_size
// arguments curried away behaves like hwloc_set_membind
// - FFI is guaranteed to be passed valid (topology,
// set, policy, flags)
// - target_ptr is valid as it originates from a & reference
// - target_size has been checked not to be zero
unsafe {
self.unbind_memory_impl(
"hwloc_set_area_membind",
flags,
MemoryBoundObject::Area,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_set_area_membind(
topology,
target_ptr.cast::<c_void>(),
target_size,
set,
policy,
flags,
)
},
)
}
}
/// Query the memory binding policy and physical locality of the
/// memory identified by `target`
///
/// The warning about `Target` coverage in the documentation of
/// [`Topology::bind_memory_area()`] also applies here.
///
/// If the [`STRICT`] flag is specified, hwloc will check the default memory
/// policies and nodesets for all memory pages covered by `target`. If these
/// are not identical,
/// Err([`MixedResults`]) is returned. Otherwise,
/// the shared configuration is returned.
///
/// If [`STRICT`] is not specified, the union of all NUMA nodes containing
/// pages in the address range is calculated. If all pages in the target
/// have the same policy, it is returned, otherwise no policy is returned.
///
/// See also [`Topology::memory_binding()`] for general semantics, except...
///
/// - Binding target flags should not be used with this method
/// - As mentioned above, [`STRICT`] has a specific meaning in the context
/// of this method.
/// - This method requires [`MemoryBindingSupport::get_area()`].
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`] and [`NO_CPU_BINDING`] was
/// specified, or if a binding target flag was specified.
/// - [`BadTarget`] if `target` is a zero-sized object
/// - [`MixedResults`] if flag [`STRICT`] was specified and memory binding
/// is inhomogeneous across target memory pages
/// - [`Unsupported`] if the system cannot query the specified
/// memory area's binding
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadTarget`]: MemoryBindingError::BadTarget
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`MixedResults`]: MemoryBindingError::MixedResults
/// [`NO_CPU_BINDING`]: MemoryBindingFlags::NO_CPU_BINDING
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_get_area_membind")]
pub fn area_memory_binding<Target: ?Sized, OwnedSet: OwnedSpecializedBitmap>(
&self,
target: &Target,
flags: MemoryBindingFlags,
) -> Result<(OwnedSet, Option<MemoryBindingPolicy>), MemoryBindingError<OwnedSet>> {
let target_size = std::mem::size_of_val(target);
if target_size == 0 {
return Err(MemoryBindingError::BadTarget);
}
let target_ptr: *const Target = target;
// SAFETY: - Area is the correct target for this FFI
// - GetBinding is the correct operation for this FFI
// - hwloc_get_area_membind with target_ptr and target_size
// arguments curried away behaves like hwloc_get_membind
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
// - target_ptr is valid as it originates from a & reference
// - target_size has been checked not to be zero
unsafe {
self.memory_binding_impl(
"hwloc_get_area_membind",
flags,
MemoryBoundObject::Area,
MemoryBindingOperation::GetBinding,
|topology, set, policy, flags| {
hwlocality_sys::hwloc_get_area_membind(
topology,
target_ptr.cast::<c_void>(),
target_size,
set,
policy,
flags,
)
},
)
}
}
/// Get the NUMA nodes where the memory identified by `target` is physically
/// allocated
///
/// The warning about `Target` coverage in the documentation of
/// [`Topology::bind_memory_area()`] also applies here.
///
/// If pages spread to multiple nodes, it is not specified whether they
/// spread equitably, or whether most of them are on a single node, etc.
///
/// The operating system may move memory pages from one processor to another
/// at any time according to their binding, so this method may return
/// something that is already outdated.
///
/// See also [`Topology::memory_binding()`] for general semantics, except
/// binding target flags should not be used with this method, and it
/// requires [`MemoryBindingSupport::get_area_memory_location()`].
///
/// # Errors
///
/// - [`BadFlags`] if one of flags [`MIGRATE`] and [`NO_CPU_BINDING`] was
/// specified, or if a binding target flag was specified.
/// - [`BadTarget`] if `target` is a zero-sized object
/// - [`MixedResults`] if flag [`STRICT`] was specified and memory binding
/// is inhomogeneous across target memory pages
/// - [`Unsupported`] if the system cannot query the specified
/// memory area's location
///
/// [`BadFlags`]: MemoryBindingError::BadFlags
/// [`BadTarget`]: MemoryBindingError::BadTarget
/// [`MIGRATE`]: MemoryBindingFlags::MIGRATE
/// [`MixedResults`]: MemoryBindingError::MixedResults
/// [`NO_CPU_BINDING`]: MemoryBindingFlags::NO_CPU_BINDING
/// [`STRICT`]: MemoryBindingFlags::STRICT
/// [`Unsupported`]: MemoryBindingError::Unsupported
#[doc(alias = "hwloc_get_area_memlocation")]
pub fn area_memory_location<Target: ?Sized, OwnedSet: OwnedSpecializedBitmap>(
&self,
target: &Target,
flags: MemoryBindingFlags,
) -> Result<OwnedSet, MemoryBindingError<OwnedSet>> {
let target_size = std::mem::size_of_val(target);
if target_size == 0 {
return Err(MemoryBindingError::BadTarget);
}
let target_ptr: *const Target = target;
// SAFETY: - ThisProgram is the correct target for this FFI
// - GetLastLocation is the correct operation for this FFI
// - hwloc_get_area_memlocation with target_ptr and target_size
// arguments curried away and policy placeholder'd behaves
// like hwloc_get_membind
// - FFI is guaranteed to be passed valid (topology,
// out set, out policy, flags)
// - target_ptr is valid as it originates from a & reference
// - target_size has been checked not to be zero
unsafe {
self.memory_binding_impl(
"hwloc_get_area_memlocation",
flags,
MemoryBoundObject::ThisProgram,
MemoryBindingOperation::GetLastLocation,
|topology, set, policy, flags| {
*policy = -1;
hwlocality_sys::hwloc_get_area_memlocation(
topology,
target_ptr.cast::<c_void>(),
target_size,
set,
flags,
)
},
)
.map(|(set, _policy)| set)
}
}
/// Adjust binding flags for a certain kind of Set
fn adjust_flags_for<OwnedSet: OwnedSpecializedBitmap>(flags: &mut MemoryBindingFlags) {
match OwnedSet::BITMAP_KIND {
BitmapKind::CpuSet => flags.remove(MemoryBindingFlags::BY_NODE_SET),
BitmapKind::NodeSet => flags.insert(MemoryBindingFlags::BY_NODE_SET),
}
}
/// Binding for `hwloc_alloc`-like functions
///
/// # Safety
///
/// - `ffi` should have semantics analogous to `hwloc_alloc`
/// - If so, this is guaranteed to call `ffi` with a valid (topology, size)
/// tuple
unsafe fn allocate_memory_impl<OwnedSet: OwnedSpecializedBitmap>(
&self,
api: &'static str,
clone_set: &dyn Fn() -> Option<OwnedSet>,
len: usize,
ffi: impl FnOnce(hwloc_const_topology_t, usize) -> *mut c_void,
) -> Result<Bytes<'_>, MemoryBindingError<OwnedSet>> {
if len > 0 {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - len was checked to be nonzero above
errors::call_hwloc_ptr_mut(api, || ffi(self.as_ptr(), len))
.map_err(|raw_err| {
decode_errno(
MemoryBoundObject::Area,
MemoryBindingOperation::Allocate,
clone_set,
raw_err.errno,
)
.expect("Unexpected errno value")
})
// SAFETY: If hwloc allocation successfully returns, this is
// assumed to be a valid allocation pointer
.map(|base| unsafe { Bytes::wrap(self, base, len) })
} else {
// SAFETY: Bytes accepts any pointer for zero-sized allocations
Ok(unsafe { Bytes::wrap(self, NonNull::dangling(), 0) })
}
}
/// Memory-binding interface for `hwloc_set_membind`-like functions
///
/// # Safety
///
/// - `ffi` should have semantics analogous to `hwloc_set_membind`
/// - `target` should accurately reflect the target which this function
/// is applied to, for flags validation purposes
/// - If all of the above is true, this is guaranteed to only call `ffi`
/// with a valid (topology, bitmap, policy, flags) tuple
unsafe fn bind_memory_impl<OwnedSet: OwnedSpecializedBitmap>(
&self,
api: &'static str,
set: &OwnedSet,
policy: MemoryBindingPolicy,
mut flags: MemoryBindingFlags,
target: MemoryBoundObject,
ffi: impl FnOnce(
hwloc_const_topology_t,
hwloc_const_bitmap_t,
hwloc_membind_policy_t,
hwloc_membind_flags_t,
) -> c_int,
) -> Result<(), MemoryBindingError<OwnedSet>> {
let operation = MemoryBindingOperation::Bind;
Self::adjust_flags_for::<OwnedSet>(&mut flags);
let Some(flags) = flags.validate(target, operation) else {
return Err(MemoryBindingError::BadFlags(flags.into()));
};
call_hwloc_int(api, target, operation, &|| Some(set.clone()), || {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - Bitmap is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - All user-visible policies are accepted by hwloc
// - flags should be valid if target is valid
ffi(
self.as_ptr(),
set.as_ref().as_ptr(),
policy.into(),
flags.bits(),
)
})
}
/// Memory-unbinding interface for `hwloc_set_membind`-like functions
///
/// # Safety
///
/// - `ffi` should have semantics analogous to `hwloc_set_membind`
/// - `target` should accurately reflect the target which this function
/// is applied to, for flags validation purposes
/// - If all of the above is true, this is guaranteed to only call `ffi`
/// with a valid (topology, bitmap, policy, flags) tuple
unsafe fn unbind_memory_impl(
&self,
api: &'static str,
flags: MemoryBindingFlags,
target: MemoryBoundObject,
ffi: impl FnOnce(
hwloc_const_topology_t,
hwloc_const_bitmap_t,
hwloc_membind_policy_t,
hwloc_membind_flags_t,
) -> c_int,
) -> Result<(), MemoryBindingError<NodeSet>> {
let operation = MemoryBindingOperation::Unbind;
let Some(flags) = flags.validate(target, operation) else {
return Err(MemoryBindingError::BadFlags(flags.into()));
};
call_hwloc_int::<NodeSet>(api, target, operation, &|| None, || {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - Passing a null set and the default policy is an
// hwloc-accepted way to reset the binding policy
// - All user-visible policies are accepted by hwloc
// - flags should be valid if target is valid
ffi(
self.as_ptr(),
ptr::null(),
HWLOC_MEMBIND_DEFAULT,
flags.bits(),
)
})
}
/// Binding for `hwloc_get_membind`-like functions
///
/// # Safety
///
/// - `ffi` should have semantics analogous to `hwloc_get_membind`
/// - `target` should accurately reflect the target which this function
/// is applied to, for flags validation purposes
/// - `operation` should accurately reflect the kind of operation being
/// performed, for flags validation purposes
/// - If all of the above is true, this is guaranteed to only call `ffi`
/// with a valid (topology, out bitmap, out policy, flags) tuple
unsafe fn memory_binding_impl<OwnedSet: OwnedSpecializedBitmap>(
&self,
api: &'static str,
mut flags: MemoryBindingFlags,
target: MemoryBoundObject,
operation: MemoryBindingOperation,
ffi: impl FnOnce(
hwloc_const_topology_t,
hwloc_bitmap_t,
*mut hwloc_membind_policy_t,
hwloc_membind_flags_t,
) -> c_int,
) -> Result<(OwnedSet, Option<MemoryBindingPolicy>), MemoryBindingError<OwnedSet>> {
Self::adjust_flags_for::<OwnedSet>(&mut flags);
let Some(flags) = flags.validate(target, operation) else {
return Err(MemoryBindingError::BadFlags(flags.into()));
};
let mut set = Bitmap::new();
let mut raw_policy = hwloc_membind_policy_t::MAX;
call_hwloc_int::<OwnedSet>(api, target, operation, &|| None, || {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - Bitmap is trusted to contain a valid ptr (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
// - As a pure out parameter, policy shouldn't be read by hwloc
// - flags should be valid if target & operation are valid
ffi(
self.as_ptr(),
set.as_mut_ptr(),
&mut raw_policy,
flags.bits(),
)
})
.map(|()| {
/// Polymorphized version of policy check (avoids generics bloat)
fn check_policy(raw_policy: hwloc_membind_policy_t) -> Option<MemoryBindingPolicy> {
match MemoryBindingPolicy::try_from(raw_policy) {
Ok(policy) => Some(policy),
Err(TryFromPrimitiveError {
number: HWLOC_MEMBIND_MIXED,
}) => None,
Err(TryFromPrimitiveError { number }) => {
panic!("Got unexpected memory policy #{number}")
}
}
}
(set.into(), check_policy(raw_policy))
})
}
}
bitflags! {
/// Memory binding flags.
///
/// These bit flags can be used to refine the binding policy. All flags can
/// be OR'ed together with the exception of the binding target flags
/// `ASSUME_SINGLE_THREAD`, `THREAD` and `PROCESS`, of which at most one
/// must be specified. The most portable option is `ASSUME_SINGLE_THREAD`,
/// when it is applicable.
///
/// When using one of the methods that target a process, you must use
/// exactly one of these flags. The most portable option is
/// `ASSUME_SINGLE_THREAD`, when it is applicable. These
/// flags must not be used with any other method.
///
/// Individual memory binding methods may not support all of these flags.
/// Please check the documentation of the [memory binding
/// method](../../topology/struct.Topology.html#memory-binding) that you are
/// calling for more information.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
#[doc(alias = "hwloc_membind_flags_t")]
pub struct MemoryBindingFlags: hwloc_membind_flags_t {
/// Assume that the target process is single threaded
///
/// This lets hwloc pick between thread and process binding for
/// increased portability.
///
/// This is mutually exclusive with `PROCESS` and `THREAD`.
//
// --- Implementation details ---
//
// This is not an actual hwloc flag, it is only used to detect
// incompatible configurations and must be cleared before invoking
// hwloc. validate() will clear the flag for you.
const ASSUME_SINGLE_THREAD = 1 << (hwloc_membind_flags_t::BITS - 2);
/// Apply command to all threads of the specified process
///
/// This is mutually exclusive with `ASSUME_SINGLE_THREAD` and `THREAD`.
#[doc(alias = "HWLOC_MEMBIND_PROCESS")]
const PROCESS = HWLOC_MEMBIND_PROCESS;
/// Apply command to the current thread of the current process
///
/// This is mutually exclusive with `ASSUME_SINGLE_THREAD` and `PROCESS`.
#[doc(alias = "HWLOC_MEMBIND_THREAD")]
const THREAD = HWLOC_MEMBIND_THREAD;
/// Request strict binding from the OS
///
/// If this flag is set, a binding method will fail if the binding can
/// not be guaranteed or completely enforced. Otherwise, hwloc will
/// attempt to achieve an approximation of the requested binding (e.g.
/// targeting more or less threads and NUMA nodes).
///
/// This flag has slightly different meanings depending on which
/// method it is used with.
#[doc(alias = "HWLOC_MEMBIND_STRICT")]
const STRICT = HWLOC_MEMBIND_STRICT;
/// Migrate existing allocated memory
///
/// If the memory cannot be migrated and the `STRICT` flag is set, an
/// error will be returned.
///
/// This flag is only meaningful on operations that bind memory.
///
/// Requires [`MemoryBindingSupport::migrate_flag()`].
#[doc(alias = "HWLOC_MEMBIND_MIGRATE")]
const MIGRATE = HWLOC_MEMBIND_MIGRATE;
/// Avoid any effect on CPU binding
///
/// On some operating systems, some underlying memory binding
/// methods also bind the application to the corresponding CPU(s).
/// Using this flag will cause hwloc to avoid using OS functions that
/// could potentially affect CPU bindings.
///
/// Note, however, that using this flag may reduce hwloc's overall
/// memory binding support.
#[doc(alias = "HWLOC_MEMBIND_NOCPUBIND")]
const NO_CPU_BINDING = HWLOC_MEMBIND_NOCPUBIND;
/// Consider the bitmap argument as a nodeset.
///
/// The bitmap argument is considered a nodeset if this flag is given,
/// or a cpuset otherwise by default.
///
/// Memory binding by CPU set cannot work for CPU-less NUMA memory nodes.
/// Binding by nodeset should therefore be preferred whenever possible.
//
// --- Implementation details ---
//
// This flag does not need to be visible as it is automatically set and
// cleared by the implementation as appropriate.
#[doc(hidden)]
#[doc(alias = "HWLOC_MEMBIND_BYNODESET")]
const BY_NODE_SET = HWLOC_MEMBIND_BYNODESET;
}
}
//
impl MemoryBindingFlags {
/// Truth that these flags are in a valid state
pub(crate) fn validate(
mut self,
target: MemoryBoundObject,
operation: MemoryBindingOperation,
) -> Option<Self> {
// Target flags should be specified for the Area and Process targets
// and only for these targets
let num_target_flags = (self & (Self::PROCESS | Self::THREAD | Self::ASSUME_SINGLE_THREAD))
.bits()
.count_ones();
let expected_num_target_flags = match target {
MemoryBoundObject::Area | MemoryBoundObject::Process(_) => 1,
MemoryBoundObject::ThisProgram => 0,
};
if num_target_flags != expected_num_target_flags {
return None;
}
// The THREAD target flag should not be used when targeting other processes
if matches!(target, MemoryBoundObject::Process(_)) && self.contains(Self::THREAD) {
return None;
}
// Operation-specific considerations
match operation {
MemoryBindingOperation::GetLastLocation => {
if self.intersects(Self::STRICT | Self::MIGRATE | Self::NO_CPU_BINDING) {
return None;
}
}
MemoryBindingOperation::GetBinding => {
if self.intersects(Self::MIGRATE | Self::NO_CPU_BINDING) {
return None;
}
match target {
MemoryBoundObject::Area | MemoryBoundObject::Process(_) => {}
MemoryBoundObject::ThisProgram => {
if self.contains(Self::STRICT) && !self.contains(Self::PROCESS) {
return None;
}
}
}
}
MemoryBindingOperation::Unbind => {
if self.intersects(Self::STRICT | Self::MIGRATE) {
return None;
}
}
MemoryBindingOperation::Allocate => {
if self.contains(Self::MIGRATE) {
return None;
}
}
MemoryBindingOperation::Bind => {}
}
// Clear virtual ASSUME_SINGLE_THREAD flag, which served its purpose
self.remove(Self::ASSUME_SINGLE_THREAD);
Some(self)
}
}
//
crate::impl_arbitrary_for_bitflags!(MemoryBindingFlags, hwloc_membind_flags_t);
//
// NOTE: No default because user must consciously think about the need for PROCESS
/// Object that is being bound to particular NUMA nodes
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MemoryBoundObject {
/// A process, identified by its PID
Process(ProcessId),
/// A range of memory adresses, identified by a reference
Area,
/// The currently running program
ThisProgram,
}
//
impl Display for MemoryBoundObject {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let display = match self {
Self::Process(pid) => format!("the process with PID {pid}"),
Self::Area => "the target location".to_owned(),
Self::ThisProgram => "the current process/thread".to_owned(),
};
f.pad(&display)
}
}
//
impl From<ProcessId> for MemoryBoundObject {
fn from(value: ProcessId) -> Self {
Self::Process(value)
}
}
/// Binding operation
#[derive(Copy, Clone, Debug, Display, Eq, Hash, PartialEq)]
pub(crate) enum MemoryBindingOperation {
/// Allocate memory
Allocate,
/// Bind memory to some NUMA nodes
Bind,
/// Query the current binding of some memory
GetBinding,
/// Query on which NUMA node(s) memory was last resident
GetLastLocation,
/// Un-bind memory
Unbind,
}
/// Memory binding policy
///
/// Not all systems support all kinds of binding.
/// [`Topology::feature_support()`] may be used to query the actual memory
/// binding support in the currently used operating system.
#[derive(
Copy,
Clone,
Debug,
Default,
Display,
Eq,
Hash,
IntoPrimitive,
PartialEq,
TryFromPrimitive,
Sequence,
)]
#[doc(alias = "hwloc_membind_policy_t")]
#[repr(i32)]
pub enum MemoryBindingPolicy {
/// Allocate each memory page individually on the local NUMA
/// node of the thread that touches it
///
/// The given nodeset should usually be [`Topology::nodeset()`]
/// so that the touching thread may run and allocate on any node in the
/// system.
///
/// On AIX, if the nodeset is smaller, pages are allocated locally (if the
/// local node is in the nodeset) or from a random non-local node (otherwise).
///
/// Requires [`MemoryBindingSupport::first_touch_policy()`].
#[doc(alias = "HWLOC_MEMBIND_FIRSTTOUCH")]
FirstTouch = HWLOC_MEMBIND_FIRSTTOUCH,
/// Allocate memory on the specified nodes (most portable option)
///
/// The actual behavior may slightly vary between operating systems,
/// especially when (some of) the requested nodes are full. On Linux, by
/// default, the `MPOL_PREFERRED_MANY` (or `MPOL_PREFERRED`) policy is used.
/// However, if the [`STRICT`] flag is also given, the Linux `MPOL_BIND`
/// policy is rather used.
///
/// Requires [`MemoryBindingSupport::bind_policy()`].
///
/// [`STRICT`]: MemoryBindingFlags::STRICT
#[default]
#[doc(alias = "HWLOC_MEMBIND_BIND")]
Bind = HWLOC_MEMBIND_BIND,
/// Allocate memory on the given nodes in an interleaved round-robin manner
///
/// The precise layout of the memory across multiple NUMA nodes is OS/system
/// specific.
///
/// Interleaving can be useful when threads distributed across the specified
/// NUMA nodes will all be accessing the whole memory range concurrently,
/// since the interleave will then balance the memory references.
///
/// Requires [`MemoryBindingSupport::interleave_policy()`].
#[doc(alias = "HWLOC_MEMBIND_INTERLEAVE")]
Interleave = HWLOC_MEMBIND_INTERLEAVE,
/// Migrate pages on next touch
///
/// For each page bound with this policy, by next time it is touched (and
/// next time only), it is moved from its current location to the local NUMA
/// node of the thread where the memory reference occurred (if it needs to
/// be moved at all).
///
/// Requires [`MemoryBindingSupport::next_touch_policy()`].
#[doc(alias = "HWLOC_MEMBIND_NEXTTOUCH")]
NextTouch = HWLOC_MEMBIND_NEXTTOUCH,
}
//
crate::impl_arbitrary_for_sequence!(MemoryBindingPolicy);
/// Errors that can occur when binding memory to NUMA nodes, querying bindings,
/// or allocating (possibly bound) memory
#[derive(Clone, Debug, Error, Eq, Hash, PartialEq)]
pub enum MemoryBindingError<OwnedSet: OwnedSpecializedBitmap> {
/// Memory allocation failed even before trying to bind
///
/// This error may only be returned by [`Topology::allocate_bound_memory()`]
/// and [`Topology::binding_allocate_memory()`].
#[error("failed to allocate memory")]
AllocationFailed,
/// Requested memory binding flags are not valid in this context
///
/// Not all memory binding flag combinations make sense, either in isolation
/// or in the context of a particular binding method. Please cross-check
/// the documentation of [`MemoryBindingFlags`] and the method you were
/// trying to call for more information.
#[error(transparent)]
BadFlags(FlagsError<MemoryBindingFlags>),
/// Cannot bind to the target CPU or node set
///
/// Operating systems can have various restrictions here, e.g. can only bind
/// to NUMA node.
///
/// This error should only be reported when trying to set memory bindings.
///
/// This error might not be reported if [`MemoryBindingFlags::STRICT`] is
/// not set. Instead, the implementation is allowed to try using a smaller
/// or larger set to make the operation succeed.
#[error("cannot bind memory of {0} to {1}")]
BadSet(MemoryBoundObject, OwnedSet),
/// Cannot get/set the memory binding of a zero-sized memory region
#[error("cannot query the memory location of a zero-sized target")]
BadTarget,
/// Memory policies and nodesets vary from one thread to another
///
/// This error is returned when querying a process' memory bindings with the
/// flags [`PROCESS`] and [`STRICT`] specified. It means that the default
/// memory policies and nodesets are not homogeneous across all threads of
/// the target process.
///
/// [`PROCESS`]: MemoryBindingFlags::PROCESS
/// [`STRICT`]: MemoryBindingFlags::STRICT
#[error("memory binding varies from one thread of the process to another")]
#[doc(alias = "HWLOC_MEMBIND_MIXED")]
MixedResults,
/// The system does not support the specified action or policy
///
/// For example, some systems only allow binding memory on a per-thread
/// basis, whereas other systems only allow binding memory for all threads
/// in a process.
///
/// This error might not be reported if [`MemoryBindingFlags::STRICT`] is
/// not set. Instead, the implementation is allowed to try to use a slightly
/// different operation (with side-effects, binding more objects, etc.) when
/// the requested operation is not exactly supported.
#[error("requested memory binding action or policy isn't supported")]
Unsupported,
/// An error occured, but we don't know which one
///
/// This may only happen on Windows. On this operating system, there are
/// multiple versions of the standard library (called the C Run-Times or
/// CRTs), and it is very easy to end up in a situation where your program
/// links against a different CRT than your hwloc build, which breaks
/// errno-based error reporting among other things.
#[cfg(windows)]
#[error("an unknown error occured")]
Unknown,
}
//
impl<OwnedSet: OwnedSpecializedBitmap> From<MemoryBindingFlags> for MemoryBindingError<OwnedSet> {
fn from(value: MemoryBindingFlags) -> Self {
Self::BadFlags(value.into())
}
}
/// Call an hwloc API that is about manipulating memory bindings and translate
/// known errors into higher-level `MemoryBindingError`s.
///
/// Validating flags is left up to the caller, to avoid allocating result
/// objects when it can be proved upfront that the request is invalid.
pub(crate) fn call_hwloc_int<OwnedSet: OwnedSpecializedBitmap>(
api: &'static str,
object: MemoryBoundObject,
operation: MemoryBindingOperation,
clone_set: &dyn Fn() -> Option<OwnedSet>,
ffi: impl FnOnce() -> c_int,
) -> Result<(), MemoryBindingError<OwnedSet>> {
match errors::call_hwloc_int_normal(api, ffi) {
Ok(_) => Ok(()),
Err(RawHwlocError { errno, .. }) => {
Err(decode_errno(object, operation, clone_set, errno).expect("Unexpected errno value"))
}
}
}
/// Errors that can occur when allocating memory
pub type MemoryAllocationError<OwnedSet> = MemoryBindingError<OwnedSet>;
/// Translating hwloc errno into high-level errors
fn decode_errno<OwnedSet: OwnedSpecializedBitmap>(
object: MemoryBoundObject,
operation: MemoryBindingOperation,
clone_set: &dyn Fn() -> Option<OwnedSet>,
errno: Option<Errno>,
) -> Option<MemoryBindingError<OwnedSet>> {
match errno {
Some(Errno(ENOSYS)) => Some(MemoryBindingError::Unsupported),
Some(Errno(EXDEV)) => match operation {
MemoryBindingOperation::Bind | MemoryBindingOperation::Allocate => {
Some(MemoryBindingError::BadSet(
object,
clone_set()
.expect("This error should only be observed on commands that set bindings"),
))
}
MemoryBindingOperation::GetBinding | MemoryBindingOperation::GetLastLocation => {
Some(MemoryBindingError::MixedResults)
}
MemoryBindingOperation::Unbind => {
unreachable!("The empty set should always be considered valid")
}
},
Some(Errno(ENOMEM)) => Some(MemoryBindingError::AllocationFailed),
#[cfg(windows)]
// Work around CRT mismatch issues on Windows, which break errno
None => Some(MemoryBindingError::Unknown),
_ => None,
}
}
/// Bytes allocated through hwloc
///
/// This behaves like a `Box<[MaybeUninit<u8>]>` and will similarly
/// automatically liberate the allocated memory when it goes out of scope.
//
// --- Implementation details ---
//
// # Safety
//
// - If the size is nonzero, `data` is an owned (valid, non-aliased)
// hwloc-originated allocation from `topology`, with correct size metadata,
// that should be freed on Drop
// - If the size is zero, `data` is a zero-sized slice with a dangling base
// pointer, that should not be freed on Drop
pub struct Bytes<'topology> {
/// Underlying hwloc topology
topology: &'topology Topology,
/// Previously allocated data pointer
data: NonNull<[MaybeUninit<u8>]>,
}
//
impl<'topology> Bytes<'topology> {
/// Wrap an hwloc allocation
///
/// # Safety
///
/// If the size is nonzero, `base` must originate from an hwloc memory
/// allocation function that was called on `topology` for `size` bytes.
pub(crate) unsafe fn wrap(
topology: &'topology Topology,
base: NonNull<c_void>,
size: usize,
) -> Self {
Self {
topology,
data: NonNull::slice_from_raw_parts(base.cast::<MaybeUninit<u8>>(), size),
}
}
}
//
impl AsRef<[MaybeUninit<u8>]> for Bytes<'_> {
fn as_ref(&self) -> &[MaybeUninit<u8>] {
// SAFETY: Per type invariant
unsafe { self.data.as_ref() }
}
}
//
impl AsMut<[MaybeUninit<u8>]> for Bytes<'_> {
fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
// SAFETY: Per type invariant
unsafe { self.data.as_mut() }
}
}
//
impl Borrow<[MaybeUninit<u8>]> for Bytes<'_> {
fn borrow(&self) -> &[MaybeUninit<u8>] {
self.as_ref()
}
}
//
impl BorrowMut<[MaybeUninit<u8>]> for Bytes<'_> {
fn borrow_mut(&mut self) -> &mut [MaybeUninit<u8>] {
self.as_mut()
}
}
//
impl Debug for Bytes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(self.as_ref(), f)
}
}
//
impl Deref for Bytes<'_> {
type Target = [MaybeUninit<u8>];
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
//
impl DerefMut for Bytes<'_> {
fn deref_mut(&mut self) -> &mut [MaybeUninit<u8>] {
self.as_mut()
}
}
//
impl Drop for Bytes<'_> {
#[allow(clippy::print_stderr)]
#[doc(alias = "hwloc_free")]
fn drop(&mut self) {
let len = self.data.len();
if len > 0 {
// SAFETY: - Topology is trusted to contain a valid ptr (type invariant)
// - self.data is trusted to be valid (type invariant)
// - hwloc ops are trusted not to modify *const parameters
// - Bytes will not be usable again after Drop
let result = errors::call_hwloc_int_normal("hwloc_free", || unsafe {
hwlocality_sys::hwloc_free(
self.topology.as_ptr(),
self.data.as_ptr().cast::<c_void>(),
len,
)
});
if let Err(e) = result {
// Cannot panic in Drop
eprintln!("ERROR: Failed to liberate hwloc allocation ({e}).",);
}
}
}
}
//
// SAFETY: Exposes no internal mutability
unsafe impl Send for Bytes<'_> {}
//
// SAFETY: Exposes no internal mutability
unsafe impl Sync for Bytes<'_> {}