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
|
/* Simple NUMA library.
Copyright (C) 2003,2004,2005,2008 Andi Kleen,SuSE Labs and
Cliff Wickman,SGI.
libnuma is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; version
2.1.
libnuma is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should find a copy of v2.1 of the GNU Lesser General Public License
somewhere on your Linux system; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
All calls are undefined when numa_available returns an error. */
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/mman.h>
#include <limits.h>
#include "numa.h"
#include "numaif.h"
#include "numaint.h"
#include "util.h"
#define WEAK __attribute__((weak))
#define CPU_BUFFER_SIZE 4096 /* This limits you to 32768 CPUs */
/* these are the old (version 1) masks */
nodemask_t numa_no_nodes;
nodemask_t numa_all_nodes;
/* these are now the default bitmask (pointers to) (version 2) */
struct bitmask *numa_no_nodes_ptr = NULL;
struct bitmask *numa_all_nodes_ptr = NULL;
struct bitmask *numa_all_cpus_ptr = NULL;
/* I would prefer to use symbol versioning to create v1 and v2 versions
of numa_no_nodes and numa_all_nodes, but the loader does not correctly
handle versioning of BSS versus small data items */
static unsigned long *node_cpu_mask_v1[NUMA_NUM_NODES];
struct bitmask **node_cpu_mask_v2;
WEAK void numa_error(char *where);
#ifdef __thread
#warning "not threadsafe"
#endif
static __thread int bind_policy = MPOL_BIND;
static __thread unsigned int mbind_flags = 0;
static int sizes_set=0;
static int maxconfigurednode = -1;
static int maxconfiguredcpu = -1;
static int maxprocnode = -1;
static int maxproccpu = -1;
static int nodemask_sz = 0;
static int cpumask_sz = 0;
int numa_exit_on_error = 0;
int numa_exit_on_warn = 0;
static void set_sizes(void);
/*
* There are two special functions, _init(void) and _fini(void), which
* are called automatically by the dynamic loader whenever a library is loaded.
*
* The v1 library depends upon nodemask_t's of all nodes and no nodes.
*/
void
numa_init(void)
{
int max,i;
set_sizes();
/* numa_all_nodes should represent existing nodes on this system */
max = numa_num_configured_nodes();
for (i = 0; i < max; i++)
nodemask_set_compat((nodemask_t *)&numa_all_nodes, i);
memset(&numa_no_nodes, 0, sizeof(numa_no_nodes));
}
void
numa_fini(void)
{
if (numa_all_cpus_ptr)
numa_bitmask_free(numa_all_cpus_ptr);
if (numa_all_nodes_ptr)
numa_bitmask_free(numa_all_nodes_ptr);
if (numa_no_nodes_ptr)
numa_bitmask_free(numa_no_nodes_ptr);
}
/*
* The following bitmask declarations, bitmask_*() routines, and associated
* _setbit() and _getbit() routines are:
* Copyright (c) 2004_2007 Silicon Graphics, Inc. (SGI) All rights reserved.
* SGI publishes it under the terms of the GNU General Public License, v2,
* as published by the Free Software Foundation.
*/
static unsigned int
_getbit(const struct bitmask *bmp, unsigned int n)
{
if (n < bmp->size)
return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
else
return 0;
}
static void
_setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
{
if (n < bmp->size) {
if (v)
bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
else
bmp->maskp[n/bitsperlong] &= ~(1UL << (n % bitsperlong));
}
}
int
numa_bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
{
return _getbit(bmp, i);
}
struct bitmask *
numa_bitmask_setall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 1);
return bmp;
}
struct bitmask *
numa_bitmask_clearall(struct bitmask *bmp)
{
unsigned int i;
for (i = 0; i < bmp->size; i++)
_setbit(bmp, i, 0);
return bmp;
}
struct bitmask *
numa_bitmask_setbit(struct bitmask *bmp, unsigned int i)
{
_setbit(bmp, i, 1);
return bmp;
}
struct bitmask *
numa_bitmask_clearbit(struct bitmask *bmp, unsigned int i)
{
_setbit(bmp, i, 0);
return bmp;
}
unsigned int
numa_bitmask_nbytes(struct bitmask *bmp)
{
return longsperbits(bmp->size) * sizeof(unsigned long);
}
/* where n is the number of bits in the map */
/* This function should not exit on failure, but right now we cannot really
recover from this. */
struct bitmask *
numa_bitmask_alloc(unsigned int n)
{
struct bitmask *bmp;
if (n < 1) {
numa_error("request to allocate mask for invalid number; abort\n");
exit(1);
}
bmp = malloc(sizeof(*bmp));
if (bmp == 0)
goto oom;
bmp->size = n;
bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
if (bmp->maskp == 0) {
free(bmp);
goto oom;
}
return bmp;
oom:
numa_error("Out of memory allocating bitmask");
exit(1);
}
void
numa_bitmask_free(struct bitmask *bmp)
{
if (bmp == 0)
return;
free(bmp->maskp);
bmp->maskp = (unsigned long *)0xdeadcdef; /* double free tripwire */
free(bmp);
return;
}
/* True if two bitmasks are equal */
int
numa_bitmask_equal(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
unsigned int i;
for (i = 0; i < bmp1->size || i < bmp2->size; i++)
if (_getbit(bmp1, i) != _getbit(bmp2, i))
return 0;
return 1;
}
/* *****end of bitmask_ routines ************ */
/* Next two can be overwritten by the application for different error handling */
WEAK void numa_error(char *where)
{
int olde = errno;
perror(where);
if (numa_exit_on_error)
exit(1);
errno = olde;
}
WEAK void numa_warn(int num, char *fmt, ...)
{
static unsigned warned;
va_list ap;
int olde = errno;
/* Give each warning only once */
if ((1<<num) & warned)
return;
warned |= (1<<num);
va_start(ap,fmt);
fprintf(stderr, "libnuma: Warning: ");
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
va_end(ap);
errno = olde;
}
static void setpol(int policy, struct bitmask *bmp)
{
if (set_mempolicy(policy, bmp->maskp, bmp->size + 1) < 0)
numa_error("set_mempolicy");
}
static void getpol(int *oldpolicy, struct bitmask *bmp)
{
if (get_mempolicy(oldpolicy, bmp->maskp, bmp->size + 1, 0, 0) < 0)
numa_error("get_mempolicy");
}
static void dombind(void *mem, size_t size, int pol, struct bitmask *bmp)
{
if (mbind(mem, size, pol, bmp ? bmp->maskp : NULL, bmp ? bmp->size + 1 : 0,
mbind_flags) < 0)
numa_error("mbind");
}
/* (undocumented) */
/* gives the wrong answer for hugetlbfs mappings. */
int numa_pagesize(void)
{
static int pagesize;
if (pagesize > 0)
return pagesize;
pagesize = getpagesize();
return pagesize;
}
make_internal_alias(numa_pagesize);
/*
* Find the highest numbered existing memory node: maxconfigurednode.
*/
static void
set_configured_nodes(void)
{
DIR *d;
struct dirent *de;
d = opendir("/sys/devices/system/node");
if (!d) {
maxconfigurednode = 0;
} else {
while ((de = readdir(d)) != NULL) {
int nd;
if (strncmp(de->d_name, "node", 4))
continue;
nd = strtoul(de->d_name+4, NULL, 0);
if (maxconfigurednode < nd)
maxconfigurednode = nd;
}
closedir(d);
}
}
/*
* Convert the string length of an ascii hex mask to the number
* of bits represented by that mask.
*/
static int s2nbits(const char *s)
{
return strlen(s) * 32 / 9;
}
/* Is string 'pre' a prefix of string 's'? */
static int strprefix(const char *s, const char *pre)
{
return strncmp(s, pre, strlen(pre)) == 0;
}
static const char *mask_size_file = "/proc/self/status";
static const char *nodemask_prefix = "Mems_allowed:\t";
/*
* (do this the way Paul Jackson's libcpuset does it)
* The nodemask values in /proc/self/status are in an
* ascii format that uses 9 characters for each 32 bits of mask.
* (this could also be used to find the cpumask size)
*/
static void
set_nodemask_size(void)
{
FILE *fp;
char *buf = NULL;
size_t bufsize = 0;
if ((fp = fopen(mask_size_file, "r")) == NULL)
goto done;
while (getline(&buf, &bufsize, fp) > 0) {
if (strprefix(buf, nodemask_prefix)) {
nodemask_sz = s2nbits(buf + strlen(nodemask_prefix));
break;
}
}
free(buf);
fclose(fp);
done:
if (nodemask_sz == 0) {/* fall back on error */
int pol;
unsigned long *mask = NULL;
nodemask_sz = 16;
do {
nodemask_sz <<= 1;
mask = realloc(mask, nodemask_sz / 8);
if (!mask)
return;
} while (get_mempolicy(&pol, mask, nodemask_sz + 1, 0, 0) < 0 && errno == EINVAL &&
nodemask_sz < 4096*8);
free(mask);
}
}
/*
* Read a mask consisting of a sequence of hexadecimal longs separated by
* commas. Order them correctly and return the number of the last bit
* set.
*/
static int
read_mask(char *s, struct bitmask *bmp)
{
char *end = s;
int tmplen = (bmp->size + bitsperint - 1) / bitsperint;
unsigned int tmp[tmplen];
unsigned int *start = tmp;
unsigned int i, n = 0, m = 0;
if (!s)
return 0; /* shouldn't happen */
i = strtoul(s, &end, 16);
/* Skip leading zeros */
while (!i && *end++ == ',') {
i = strtoul(end, &end, 16);
}
if (!i)
/* End of string. No mask */
return -1;
start[n++] = i;
/* Read sequence of ints */
while (*end++ == ',') {
i = strtoul(end, &end, 16);
start[n++] = i;
/* buffer overflow */
if (n > tmplen)
return -1;
}
/*
* Invert sequence of ints if necessary since the first int
* is the highest and we put it first because we read it first.
*/
while (n) {
int w;
unsigned long x = 0;
/* read into long values in an endian-safe way */
for (w = 0; n && w < bitsperlong; w += bitsperint)
x |= ((unsigned long)start[n-- - 1] << w);
bmp->maskp[m++] = x;
}
m--;
/* Poor mans fls() */
for(i = bitsperlong - 1; i >= 0; i--)
if (test_bit(i, bmp->maskp + m))
break;
/*
* Return the last bit set
*/
return bitsperlong * m + i;
}
/*
* Read a processes constraints in terms of nodes and cpus from
* /proc/self/status.
*/
static void
set_task_constraints(void)
{
int hicpu = sysconf(_SC_NPROCESSORS_CONF)-1;
int i;
char *buffer = NULL;
size_t buflen = 0;
FILE *f;
numa_all_cpus_ptr = numa_allocate_cpumask();
numa_all_nodes_ptr = numa_allocate_nodemask();
numa_no_nodes_ptr = numa_allocate_nodemask();
f = fopen(mask_size_file, "r");
if (!f) {
//numa_warn(W_cpumap, "Cannot parse %s", mask_size_file);
return;
}
while (getline(&buffer, &buflen, f) > 0) {
/* mask starts after [last] tab */
char *mask = strrchr(buffer,'\t') + 1;
if (strncmp(buffer,"Cpus_allowed:",13) == 0)
maxproccpu = read_mask(mask, numa_all_cpus_ptr);
if (strncmp(buffer,"Mems_allowed:",13) == 0) {
maxprocnode =
read_mask(mask, numa_all_nodes_ptr);
}
}
fclose(f);
free(buffer);
/*
* Cpus_allowed in the kernel can be defined to all f's
* i.e. it may be a superset of the actual available processors.
* As such let's reduce maxproccpu to the number of actual
* available cpus - 1.
*/
if (maxproccpu <= 0) {
for (i = 0; i <= hicpu; i++)
numa_bitmask_setbit(numa_all_cpus_ptr, i);
maxproccpu = hicpu;
}
if (maxproccpu > hicpu) {
maxproccpu = hicpu;
for (i=hicpu+1; i<numa_all_cpus_ptr->size; i++) {
numa_bitmask_clearbit(numa_all_cpus_ptr, i);
}
}
if (maxprocnode <= 0) {
for (i = 0; i <= maxconfigurednode; i++)
numa_bitmask_setbit(numa_all_nodes_ptr, i);
maxprocnode = maxconfigurednode;
}
return;
}
/*
* Find the highest cpu number possible (in other words the size
* of a kernel cpumask_t (in bits) - 1)
*/
static void
set_numa_max_cpu(void)
{
int len = 4096;
int n;
int olde = errno;
struct bitmask *buffer;
do {
buffer = numa_bitmask_alloc(len);
n = numa_sched_getaffinity_v2_int(0, buffer);
/* on success, returns size of kernel cpumask_t, in bytes */
if (n < 0 && errno == EINVAL) {
if (len >= 1024*1024)
break;
len *= 2;
numa_bitmask_free(buffer);
continue;
}
} while (n < 0);
numa_bitmask_free(buffer);
errno = olde;
cpumask_sz = n*8;
}
/*
* get the total (configured) number of cpus - both online and offline
*/
static void
set_configured_cpus(void)
{
char *dirnamep = "/sys/devices/system/cpu";
struct dirent *dirent;
DIR *dir;
dir = opendir(dirnamep);
if (dir == NULL) {
/* fall back to using the online cpu count */
maxconfiguredcpu = sysconf(_SC_NPROCESSORS_CONF) - 1;
return;
}
while ((dirent = readdir(dir)) != 0) {
if (dirent->d_type == DT_DIR
&& !strncmp("cpu", dirent->d_name, 3)) {
long cpu = strtol(dirent->d_name + 3, NULL, 10);
if (cpu < INT_MAX && cpu > maxconfiguredcpu)
maxconfiguredcpu = cpu;
}
}
closedir(dir);
if (maxconfiguredcpu < 0) {
/* fall back to using the online cpu count */
maxconfiguredcpu = sysconf(_SC_NPROCESSORS_CONF) - 1;
}
}
/*
* Initialize all the sizes.
*/
static void
set_sizes(void)
{
sizes_set++;
set_configured_nodes(); /* configured nodes listed in /sys */
set_nodemask_size(); /* size of kernel nodemask_t */
set_numa_max_cpu(); /* size of kernel cpumask_t */
set_configured_cpus(); /* cpus listed in /sys/devices/system/cpu */
set_task_constraints(); /* cpus and nodes for current task */
}
int
numa_num_configured_nodes(void)
{
return maxconfigurednode+1;
}
int
numa_num_configured_cpus(void)
{
return maxconfiguredcpu+1;
}
int
numa_num_possible_nodes(void)
{
return nodemask_sz;
}
int
numa_num_possible_cpus(void)
{
return cpumask_sz;
}
int
numa_num_task_nodes(void)
{
return maxprocnode+1;
}
int
numa_num_task_cpus(void)
{
return maxproccpu+1;
}
/*
* Return the number of the highest node in this running system,
*/
int
numa_max_node(void)
{
return numa_num_configured_nodes()-1;
}
make_internal_alias(numa_max_node);
/*
* Return the number of the highest possible node in a system,
* which for v1 is the size of a numa.h nodemask_t(in bits)-1.
* but for v2 is the size of a kernel nodemask_t(in bits)-1.
*/
int
numa_max_possible_node_v1(void)
{
return ((sizeof(nodemask_t)*8)-1);
}
__asm__(".symver numa_max_possible_node_v1,numa_max_possible_node@libnuma_1.1");
int
numa_max_possible_node_v2(void)
{
return numa_num_possible_nodes()-1;
}
__asm__(".symver numa_max_possible_node_v2,numa_max_possible_node@@libnuma_1.2");
make_internal_alias(numa_max_possible_node_v1);
make_internal_alias(numa_max_possible_node_v2);
/*
* Allocate a bitmask for cpus, of a size large enough to
* match the kernel's cpumask_t.
*/
struct bitmask *
numa_allocate_cpumask()
{
int ncpus = numa_num_possible_cpus();
return numa_bitmask_alloc(ncpus);
}
/*
* Allocate a bitmask the size of a libnuma nodemask_t
*/
static struct bitmask *
allocate_nodemask_v1(void)
{
int nnodes = numa_max_possible_node_v1_int()+1;
return numa_bitmask_alloc(nnodes);
}
/*
* Allocate a bitmask for nodes, of a size large enough to
* match the kernel's nodemask_t.
*/
struct bitmask *
numa_allocate_nodemask(void)
{
struct bitmask *bmp;
int nnodes = numa_max_possible_node_v2_int() + 1;
bmp = numa_bitmask_alloc(nnodes);
return bmp;
}
/* (cache the result?) */
long long numa_node_size64(int node, long long *freep)
{
size_t len = 0;
char *line = NULL;
long long size = -1;
FILE *f;
char fn[64];
int ok = 0;
int required = freep ? 2 : 1;
if (freep)
*freep = -1;
sprintf(fn,"/sys/devices/system/node/node%d/meminfo", node);
f = fopen(fn, "r");
if (!f)
return -1;
while (getdelim(&line, &len, '\n', f) > 0) {
char *end;
char *s = strcasestr(line, "kB");
if (!s)
continue;
--s;
while (s > line && isspace(*s))
--s;
while (s > line && isdigit(*s))
--s;
if (strstr(line, "MemTotal")) {
size = strtoull(s,&end,0) << 10;
if (end == s)
size = -1;
else
ok++;
}
if (freep && strstr(line, "MemFree")) {
*freep = strtoull(s,&end,0) << 10;
if (end == s)
*freep = -1;
else
ok++;
}
}
fclose(f);
free(line);
if (ok != required)
numa_warn(W_badmeminfo, "Cannot parse sysfs meminfo (%d)", ok);
return size;
}
make_internal_alias(numa_node_size64);
long numa_node_size(int node, long *freep)
{
long long f2;
long sz = numa_node_size64_int(node, &f2);
if (freep)
*freep = f2;
return sz;
}
int numa_available(void)
{
if (get_mempolicy(NULL, NULL, 0, 0, 0) < 0 && errno == ENOSYS)
return -1;
return 0;
}
void
numa_interleave_memory_v1(void *mem, size_t size, const nodemask_t *mask)
{
struct bitmask bitmask;
bitmask.size = sizeof(nodemask_t) * 8;
bitmask.maskp = (unsigned long *)mask;
dombind(mem, size, MPOL_INTERLEAVE, &bitmask);
}
__asm__(".symver numa_interleave_memory_v1,numa_interleave_memory@libnuma_1.1");
void
numa_interleave_memory_v2(void *mem, size_t size, struct bitmask *bmp)
{
dombind(mem, size, MPOL_INTERLEAVE, bmp);
}
__asm__(".symver numa_interleave_memory_v2,numa_interleave_memory@@libnuma_1.2");
void numa_tonode_memory(void *mem, size_t size, int node)
{
struct bitmask *nodes;
nodes = numa_allocate_nodemask();
numa_bitmask_setbit(nodes, node);
dombind(mem, size, bind_policy, nodes);
numa_bitmask_free(nodes);
}
void
numa_tonodemask_memory_v1(void *mem, size_t size, const nodemask_t *mask)
{
struct bitmask bitmask;
bitmask.maskp = (unsigned long *)mask;
bitmask.size = sizeof(nodemask_t);
dombind(mem, size, bind_policy, &bitmask);
}
__asm__(".symver numa_tonodemask_memory_v1,numa_tonodemask_memory@libnuma_1.1");
void
numa_tonodemask_memory_v2(void *mem, size_t size, struct bitmask *bmp)
{
dombind(mem, size, bind_policy, bmp);
}
__asm__(".symver numa_tonodemask_memory_v2,numa_tonodemask_memory@@libnuma_1.2");
void numa_setlocal_memory(void *mem, size_t size)
{
dombind(mem, size, MPOL_PREFERRED, NULL);
}
void numa_police_memory(void *mem, size_t size)
{
int pagesize = numa_pagesize_int();
unsigned long i;
for (i = 0; i < size; i += pagesize)
asm volatile("" :: "r" (((volatile unsigned char *)mem)[i]));
}
make_internal_alias(numa_police_memory);
void *numa_alloc(size_t size)
{
char *mem;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
numa_police_memory_int(mem, size);
return mem;
}
void *numa_alloc_interleaved_subset_v1(size_t size, const nodemask_t *mask)
{
char *mem;
struct bitmask bitmask;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
bitmask.maskp = (unsigned long *)mask;
bitmask.size = sizeof(nodemask_t);
dombind(mem, size, MPOL_INTERLEAVE, &bitmask);
return mem;
}
__asm__(".symver numa_alloc_interleaved_subset_v1,numa_alloc_interleaved_subset@libnuma_1.1");
void *numa_alloc_interleaved_subset_v2(size_t size, struct bitmask *bmp)
{
char *mem;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
return NULL;
dombind(mem, size, MPOL_INTERLEAVE, bmp);
return mem;
}
__asm__(".symver numa_alloc_interleaved_subset_v2,numa_alloc_interleaved_subset@@libnuma_1.2");
make_internal_alias(numa_alloc_interleaved_subset_v1);
make_internal_alias(numa_alloc_interleaved_subset_v2);
void *
numa_alloc_interleaved(size_t size)
{
return numa_alloc_interleaved_subset_v2_int(size, numa_all_nodes_ptr);
}
/*
* given a user node mask, set memory policy to use those nodes
*/
void
numa_set_interleave_mask_v1(nodemask_t *mask)
{
struct bitmask *bmp;
int nnodes = numa_max_possible_node_v1_int()+1;
bmp = numa_bitmask_alloc(nnodes);
copy_nodemask_to_bitmask(mask, bmp);
if (numa_bitmask_equal(bmp, numa_no_nodes_ptr))
setpol(MPOL_DEFAULT, bmp);
else
setpol(MPOL_INTERLEAVE, bmp);
numa_bitmask_free(bmp);
}
__asm__(".symver numa_set_interleave_mask_v1,numa_set_interleave_mask@libnuma_1.1");
void
numa_set_interleave_mask_v2(struct bitmask *bmp)
{
if (numa_bitmask_equal(bmp, numa_no_nodes_ptr))
setpol(MPOL_DEFAULT, bmp);
else
setpol(MPOL_INTERLEAVE, bmp);
}
__asm__(".symver numa_set_interleave_mask_v2,numa_set_interleave_mask@@libnuma_1.2");
nodemask_t
numa_get_interleave_mask_v1(void)
{
int oldpolicy;
struct bitmask *bmp;
nodemask_t mask;
bmp = allocate_nodemask_v1();
getpol(&oldpolicy, bmp);
if (oldpolicy == MPOL_INTERLEAVE)
copy_bitmask_to_nodemask(bmp, &mask);
else
copy_bitmask_to_nodemask(numa_no_nodes_ptr, &mask);
numa_bitmask_free(bmp);
return mask;
}
__asm__(".symver numa_get_interleave_mask_v1,numa_get_interleave_mask@libnuma_1.1");
struct bitmask *
numa_get_interleave_mask_v2(void)
{
int oldpolicy;
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
getpol(&oldpolicy, bmp);
if (oldpolicy != MPOL_INTERLEAVE)
copy_bitmask_to_bitmask(numa_no_nodes_ptr, bmp);
return bmp;
}
__asm__(".symver numa_get_interleave_mask_v2,numa_get_interleave_mask@@libnuma_1.2");
/* (undocumented) */
int numa_get_interleave_node(void)
{
int nd;
if (get_mempolicy(&nd, NULL, 0, 0, MPOL_F_NODE) == 0)
return nd;
return 0;
}
void *numa_alloc_onnode(size_t size, int node)
{
char *mem;
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
numa_bitmask_setbit(bmp, node);
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
mem = NULL;
else
dombind(mem, size, bind_policy, bmp);
numa_bitmask_free(bmp);
return mem;
}
void *numa_alloc_local(size_t size)
{
char *mem;
mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
0, 0);
if (mem == (char *)-1)
mem = NULL;
else
dombind(mem, size, MPOL_PREFERRED, NULL);
return mem;
}
void numa_set_bind_policy(int strict)
{
if (strict)
bind_policy = MPOL_BIND;
else
bind_policy = MPOL_PREFERRED;
}
void
numa_set_membind_v1(const nodemask_t *mask)
{
struct bitmask bitmask;
bitmask.maskp = (unsigned long *)mask;
bitmask.size = sizeof(nodemask_t);
setpol(MPOL_BIND, &bitmask);
}
__asm__(".symver numa_set_membind_v1,numa_set_membind@libnuma_1.1");
void
numa_set_membind_v2(struct bitmask *bmp)
{
setpol(MPOL_BIND, bmp);
}
__asm__(".symver numa_set_membind_v2,numa_set_membind@@libnuma_1.2");
make_internal_alias(numa_set_membind_v2);
/*
* copy a bitmask map body to a numa.h nodemask_t structure
*/
void
copy_bitmask_to_nodemask(struct bitmask *bmp, nodemask_t *nmp)
{
int max, i;
memset(nmp, 0, sizeof(nodemask_t));
max = (sizeof(nodemask_t)*8);
for (i=0; i<bmp->size; i++) {
if (i >= max)
break;
if (numa_bitmask_isbitset(bmp, i))
nodemask_set_compat((nodemask_t *)nmp, i);
}
}
/*
* copy a bitmask map body to another bitmask body
* fill a larger destination with zeroes
*/
void
copy_bitmask_to_bitmask(struct bitmask *bmpfrom, struct bitmask *bmpto)
{
int bytes;
if (bmpfrom->size >= bmpto->size) {
memcpy(bmpto->maskp, bmpfrom->maskp, CPU_BYTES(bmpto->size));
} else if (bmpfrom->size < bmpto->size) {
bytes = CPU_BYTES(bmpfrom->size);
memcpy(bmpto->maskp, bmpfrom->maskp, bytes);
memset(((char *)bmpto->maskp)+bytes, 0,
CPU_BYTES(bmpto->size)-bytes);
}
}
/*
* copy a numa.h nodemask_t structure to a bitmask map body
*/
void
copy_nodemask_to_bitmask(nodemask_t *nmp, struct bitmask *bmp)
{
int max, i;
numa_bitmask_clearall(bmp);
max = (sizeof(nodemask_t)*8);
if (max > bmp->size)
max = bmp->size;
for (i=0; i<max; i++) {
if (nodemask_isset_compat(nmp, i))
numa_bitmask_setbit(bmp, i);
}
}
nodemask_t
numa_get_membind_v1(void)
{
int oldpolicy;
struct bitmask *bmp;
nodemask_t nmp;
bmp = allocate_nodemask_v1();
getpol(&oldpolicy, bmp);
if (oldpolicy == MPOL_BIND) {
copy_bitmask_to_nodemask(bmp, &nmp);
} else {
/* copy the body of the map to numa_all_nodes */
copy_bitmask_to_nodemask(bmp, &numa_all_nodes);
nmp = numa_all_nodes;
}
numa_bitmask_free(bmp);
return nmp;
}
__asm__(".symver numa_get_membind_v1,numa_get_membind@libnuma_1.1");
struct bitmask *
numa_get_membind_v2(void)
{
int oldpolicy;
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
getpol(&oldpolicy, bmp);
if (oldpolicy != MPOL_BIND)
copy_bitmask_to_bitmask(numa_all_nodes_ptr, bmp);
return bmp;
}
__asm__(".symver numa_get_membind_v2,numa_get_membind@@libnuma_1.2");
//TODO: do we need a v1 nodemask_t version?
struct bitmask *numa_get_mems_allowed(void)
{
struct bitmask *bmp;
/*
* can change, so query on each call.
*/
bmp = numa_allocate_nodemask();
getpol(NULL, bmp);
return bmp;
}
make_internal_alias(numa_get_mems_allowed);
void numa_free(void *mem, size_t size)
{
munmap(mem, size);
}
int
numa_parse_bitmap_v1(char *line, unsigned long *mask, int ncpus)
{
int i;
char *p = strchr(line, '\n');
if (!p)
return -1;
for (i = 0; p > line;i++) {
char *oldp, *endp;
oldp = p;
if (*p == ',')
--p;
while (p > line && *p != ',')
--p;
/* Eat two 32bit fields at a time to get longs */
if (p > line && sizeof(unsigned long) == 8) {
oldp--;
memmove(p, p+1, oldp-p+1);
while (p > line && *p != ',')
--p;
}
if (*p == ',')
p++;
if (i >= CPU_LONGS(ncpus))
return -1;
mask[i] = strtoul(p, &endp, 16);
if (endp != oldp)
return -1;
p--;
}
return 0;
}
__asm__(".symver numa_parse_bitmap_v1,numa_parse_bitmap@libnuma_1.1");
int
numa_parse_bitmap_v2(char *line, struct bitmask *mask)
{
int i, ncpus;
char *p = strchr(line, '\n');
if (!p)
return -1;
ncpus = mask->size;
for (i = 0; p > line;i++) {
char *oldp, *endp;
oldp = p;
if (*p == ',')
--p;
while (p > line && *p != ',')
--p;
/* Eat two 32bit fields at a time to get longs */
if (p > line && sizeof(unsigned long) == 8) {
oldp--;
memmove(p, p+1, oldp-p+1);
while (p > line && *p != ',')
--p;
}
if (*p == ',')
p++;
if (i >= CPU_LONGS(ncpus))
return -1;
mask->maskp[i] = strtoul(p, &endp, 16);
if (endp != oldp)
return -1;
p--;
}
return 0;
}
__asm__(".symver numa_parse_bitmap_v2,numa_parse_bitmap@@libnuma_1.2");
void
init_node_cpu_mask_v2(void)
{
int nnodes = numa_max_possible_node_v2_int() + 1;
node_cpu_mask_v2 = calloc (nnodes, sizeof(struct bitmask *));
}
/* This would be better with some locking, but I don't want to make libnuma
dependent on pthreads right now. The races are relatively harmless. */
int
numa_node_to_cpus_v1(int node, unsigned long *buffer, int bufferlen)
{
int err = 0;
char fn[64];
FILE *f;
char *line = NULL;
size_t len = 0;
struct bitmask bitmask;
int buflen_needed;
unsigned long *mask;
int ncpus = numa_num_possible_cpus();
int maxnode = numa_max_node_int();
buflen_needed = CPU_BYTES(ncpus);
if ((unsigned)node > maxnode || bufferlen < buflen_needed) {
errno = ERANGE;
return -1;
}
if (bufferlen > buflen_needed)
memset(buffer, 0, bufferlen);
if (node_cpu_mask_v1[node]) {
memcpy(buffer, node_cpu_mask_v1[node], buflen_needed);
return 0;
}
mask = malloc(buflen_needed);
if (!mask)
mask = (unsigned long *)buffer;
memset(mask, 0, buflen_needed);
sprintf(fn, "/sys/devices/system/node/node%d/cpumap", node);
f = fopen(fn, "r");
if (!f || getdelim(&line, &len, '\n', f) < 1) {
numa_warn(W_nosysfs2,
"/sys not mounted or invalid. Assuming one node: %s",
strerror(errno));
bitmask.maskp = (unsigned long *)mask;
bitmask.size = buflen_needed * 8;
numa_bitmask_setall(&bitmask);
err = -1;
}
if (f)
fclose(f);
if (line && (numa_parse_bitmap_v1(line, mask, ncpus) < 0)) {
numa_warn(W_cpumap, "Cannot parse cpumap. Assuming one node");
bitmask.maskp = (unsigned long *)mask;
bitmask.size = buflen_needed * 8;
numa_bitmask_setall(&bitmask);
err = -1;
}
free(line);
memcpy(buffer, mask, buflen_needed);
/* slightly racy, see above */
if (node_cpu_mask_v1[node]) {
if (mask != buffer)
free(mask);
} else {
node_cpu_mask_v1[node] = mask;
}
return err;
}
__asm__(".symver numa_node_to_cpus_v1,numa_node_to_cpus@libnuma_1.1");
/*
* test whether a node has cpus
*/
/* This would be better with some locking, but I don't want to make libnuma
dependent on pthreads right now. The races are relatively harmless. */
/*
* deliver a bitmask of cpus representing the cpus on a given node
*/
int
numa_node_to_cpus_v2(int node, struct bitmask *buffer)
{
int err = 0, bufferlen;
int nnodes = numa_num_configured_nodes();
char fn[64], *line = NULL;
FILE *f;
size_t len = 0;
struct bitmask *mask;
if (!node_cpu_mask_v2)
init_node_cpu_mask_v2();
bufferlen = numa_bitmask_nbytes(buffer);
if (node > nnodes-1) {
errno = ERANGE;
return -1;
}
numa_bitmask_clearall(buffer);
if (node_cpu_mask_v2[node]) {
/* have already constructed a mask for this node */
if (buffer->size < node_cpu_mask_v2[node]->size) {
numa_error("map size mismatch; abort\n");
return -1;
}
copy_bitmask_to_bitmask(node_cpu_mask_v2[node], buffer);
return 0;
}
/* need a new mask for this node */
mask = numa_allocate_cpumask();
/* this is a kernel cpumask_t (see node_read_cpumap()) */
sprintf(fn, "/sys/devices/system/node/node%d/cpumap", node);
f = fopen(fn, "r");
if (!f || getdelim(&line, &len, '\n', f) < 1) {
numa_warn(W_nosysfs2,
"/sys not mounted or invalid. Assuming one node: %s",
strerror(errno));
numa_bitmask_setall(mask);
err = -1;
}
if (f)
fclose(f);
if (line && (numa_parse_bitmap_v2(line, mask) < 0)) {
numa_warn(W_cpumap, "Cannot parse cpumap. Assuming one node");
numa_bitmask_setall(mask);
err = -1;
}
free(line);
copy_bitmask_to_bitmask(mask, buffer);
/* slightly racy, see above */
/* save the mask we created */
if (node_cpu_mask_v2[node]) {
/* how could this be? */
if (mask != buffer)
numa_bitmask_free(mask);
} else {
node_cpu_mask_v2[node] = mask;
}
return err;
}
__asm__(".symver numa_node_to_cpus_v2,numa_node_to_cpus@@libnuma_1.2");
make_internal_alias(numa_node_to_cpus_v1);
make_internal_alias(numa_node_to_cpus_v2);
/* report the node of the specified cpu */
int numa_node_of_cpu(int cpu)
{
struct bitmask *bmp;
int ncpus, nnodes, node, ret;
ncpus = numa_num_possible_cpus();
if (cpu > ncpus){
errno = EINVAL;
return -1;
}
bmp = numa_bitmask_alloc(ncpus);
nnodes = numa_num_configured_nodes();
for (node = 0; node < nnodes; node++){
numa_node_to_cpus_v2_int(node, bmp);
if (numa_bitmask_isbitset(bmp, cpu)){
ret = node;
goto end;
}
}
ret = -1;
errno = EINVAL;
end:
numa_bitmask_free(bmp);
return ret;
}
int
numa_run_on_node_mask_v1(const nodemask_t *mask)
{
int ncpus = numa_num_possible_cpus();
int i, k, err;
unsigned long cpus[CPU_LONGS(ncpus)], nodecpus[CPU_LONGS(ncpus)];
memset(cpus, 0, CPU_BYTES(ncpus));
for (i = 0; i < NUMA_NUM_NODES; i++) {
if (mask->n[i / BITS_PER_LONG] == 0)
continue;
if (nodemask_isset_compat(mask, i)) {
if (numa_node_to_cpus_v1_int(i, nodecpus, CPU_BYTES(ncpus)) < 0) {
numa_warn(W_noderunmask,
"Cannot read node cpumask from sysfs");
continue;
}
for (k = 0; k < CPU_LONGS(ncpus); k++)
cpus[k] |= nodecpus[k];
}
}
err = numa_sched_setaffinity_v1(0, CPU_BYTES(ncpus), cpus);
/* The sched_setaffinity API is broken because it expects
the user to guess the kernel cpuset size. Do this in a
brute force way. */
if (err < 0 && errno == EINVAL) {
int savederrno = errno;
char *bigbuf;
static int size = -1;
if (size == -1)
size = CPU_BYTES(ncpus) * 2;
bigbuf = malloc(CPU_BUFFER_SIZE);
if (!bigbuf) {
errno = ENOMEM;
return -1;
}
errno = savederrno;
while (size <= CPU_BUFFER_SIZE) {
memcpy(bigbuf, cpus, CPU_BYTES(ncpus));
memset(bigbuf + CPU_BYTES(ncpus), 0,
CPU_BUFFER_SIZE - CPU_BYTES(ncpus));
err = numa_sched_setaffinity_v1_int(0, size, (unsigned long *)bigbuf);
if (err == 0 || errno != EINVAL)
break;
size *= 2;
}
savederrno = errno;
free(bigbuf);
errno = savederrno;
}
return err;
}
__asm__(".symver numa_run_on_node_mask_v1,numa_run_on_node_mask@libnuma_1.1");
/*
* Given a node mask (size of a kernel nodemask_t) (probably populated by
* a user argument list) set up a map of cpus (map "cpus") on those nodes.
* Then set affinity to those cpus.
*/
int
numa_run_on_node_mask_v2(struct bitmask *bmp)
{
int ncpus, i, k, err;
struct bitmask *cpus, *nodecpus;
cpus = numa_allocate_cpumask();
ncpus = cpus->size;
nodecpus = numa_allocate_cpumask();
for (i = 0; i < bmp->size; i++) {
if (bmp->maskp[i / BITS_PER_LONG] == 0)
continue;
if (numa_bitmask_isbitset(bmp, i)) {
if (numa_node_to_cpus_v2_int(i, nodecpus) < 0) {
numa_warn(W_noderunmask,
"Cannot read node cpumask from sysfs");
continue;
}
for (k = 0; k < CPU_LONGS(ncpus); k++)
cpus->maskp[k] |= nodecpus->maskp[k];
}
}
err = numa_sched_setaffinity_v2_int(0, cpus);
numa_bitmask_free(cpus);
numa_bitmask_free(nodecpus);
/* used to have to consider that this could fail - it shouldn't now */
if (err < 0) {
numa_error("numa_sched_setaffinity_v2_int() failed; abort\n");
}
return err;
}
__asm__(".symver numa_run_on_node_mask_v2,numa_run_on_node_mask@@libnuma_1.2");
make_internal_alias(numa_run_on_node_mask_v2);
nodemask_t
numa_get_run_node_mask_v1(void)
{
int ncpus = numa_num_configured_cpus();
int i, k;
int max = numa_max_node_int();
struct bitmask *bmp, *cpus, *nodecpus;
nodemask_t nmp;
cpus = numa_allocate_cpumask();
if (numa_sched_getaffinity_v2_int(0, cpus) < 0){
nmp = numa_no_nodes;
goto free_cpus;
}
nodecpus = numa_allocate_cpumask();
bmp = allocate_nodemask_v1(); /* the size of a nodemask_t */
for (i = 0; i <= max; i++) {
if (numa_node_to_cpus_v2_int(i, nodecpus) < 0) {
/* It's possible for the node to not exist */
continue;
}
for (k = 0; k < CPU_LONGS(ncpus); k++) {
if (nodecpus->maskp[k] & cpus->maskp[k])
numa_bitmask_setbit(bmp, i);
}
}
copy_bitmask_to_nodemask(bmp, &nmp);
numa_bitmask_free(bmp);
numa_bitmask_free(nodecpus);
free_cpus:
numa_bitmask_free(cpus);
return nmp;
}
__asm__(".symver numa_get_run_node_mask_v1,numa_get_run_node_mask@libnuma_1.1");
struct bitmask *
numa_get_run_node_mask_v2(void)
{
int i, k;
int ncpus = numa_num_configured_cpus();
int max = numa_max_node_int();
struct bitmask *bmp, *cpus, *nodecpus;
bmp = numa_allocate_cpumask();
cpus = numa_allocate_cpumask();
if (numa_sched_getaffinity_v2_int(0, cpus) < 0){
copy_bitmask_to_bitmask(numa_no_nodes_ptr, bmp);
goto free_cpus;
}
nodecpus = numa_allocate_cpumask();
for (i = 0; i <= max; i++) {
if (numa_node_to_cpus_v2_int(i, nodecpus) < 0) {
/* It's possible for the node to not exist */
continue;
}
for (k = 0; k < CPU_LONGS(ncpus); k++) {
if (nodecpus->maskp[k] & cpus->maskp[k])
numa_bitmask_setbit(bmp, i);
}
}
numa_bitmask_free(nodecpus);
free_cpus:
numa_bitmask_free(cpus);
return bmp;
}
__asm__(".symver numa_get_run_node_mask_v2,numa_get_run_node_mask@@libnuma_1.2");
int
numa_migrate_pages(int pid, struct bitmask *fromnodes, struct bitmask *tonodes)
{
int numa_num_nodes = numa_num_possible_nodes();
return migrate_pages(pid, numa_num_nodes + 1, fromnodes->maskp,
tonodes->maskp);
}
int numa_move_pages(int pid, unsigned long count,
void **pages, const int *nodes, int *status, int flags)
{
return move_pages(pid, count, pages, nodes, status, flags);
}
int numa_run_on_node(int node)
{
int numa_num_nodes = numa_num_possible_nodes();
int ret = -1;
struct bitmask *cpus;
if (node >= numa_num_nodes){
errno = EINVAL;
goto out;
}
cpus = numa_allocate_cpumask();
if (node == -1)
numa_bitmask_setall(cpus);
else if (numa_node_to_cpus_v2_int(node, cpus) < 0){
numa_warn(W_noderunmask, "Cannot read node cpumask from sysfs");
goto free;
}
ret = numa_sched_setaffinity_v2_int(0, cpus);
free:
numa_bitmask_free(cpus);
out:
return ret;
}
int numa_preferred(void)
{
int policy;
int ret;
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
getpol(&policy, bmp);
if (policy == MPOL_PREFERRED || policy == MPOL_BIND) {
int i;
int max = numa_num_possible_nodes();
for (i = 0; i < max ; i++)
if (numa_bitmask_isbitset(bmp, i)){
ret = i;
goto end;
}
}
/* could read the current CPU from /proc/self/status. Probably
not worth it. */
ret = 0; /* or random one? */
end:
numa_bitmask_free(bmp);
return ret;
}
void numa_set_preferred(int node)
{
struct bitmask *bmp;
bmp = numa_allocate_nodemask();
if (node >= 0) {
numa_bitmask_setbit(bmp, node);
setpol(MPOL_PREFERRED, bmp);
} else
setpol(MPOL_DEFAULT, bmp);
numa_bitmask_free(bmp);
}
void numa_set_localalloc(void)
{
setpol(MPOL_DEFAULT, numa_no_nodes_ptr);
}
void numa_bind_v1(const nodemask_t *nodemask)
{
struct bitmask bitmask;
bitmask.maskp = (unsigned long *)nodemask;
bitmask.size = sizeof(nodemask_t);
numa_run_on_node_mask_v2_int(&bitmask);
numa_set_membind_v2_int(&bitmask);
}
__asm__(".symver numa_bind_v1,numa_bind@libnuma_1.1");
void numa_bind_v2(struct bitmask *bmp)
{
numa_run_on_node_mask_v2_int(bmp);
numa_set_membind_v2_int(bmp);
}
__asm__(".symver numa_bind_v2,numa_bind@@libnuma_1.2");
void numa_set_strict(int flag)
{
if (flag)
mbind_flags |= MPOL_MF_STRICT;
else
mbind_flags &= ~MPOL_MF_STRICT;
}
/*
* Extract a node or processor number from the given string.
* Allow a relative node / processor specification within the allowed
* set if "relative" is nonzero
*/
static unsigned long get_nr(char *s, char **end, struct bitmask *bmp, int relative)
{
long i, nr;
if (!relative)
return strtoul(s, end, 0);
nr = strtoul(s, end, 0);
if (s == *end)
return nr;
/* Find the nth set bit */
for (i = 0; nr >= 0 && i <= bmp->size; i++)
if (numa_bitmask_isbitset(bmp, i))
nr--;
return i-1;
}
/*
* numa_parse_nodestring() is called to create a node mask, given
* an ascii string such as 25 or 12-15 or 1,3,5-7 or +6-10.
* (the + indicates that the numbers are cpuset-relative)
*
* The nodes may be specified as absolute, or relative to the current cpuset.
* The list of available nodes is in a map pointed to by "numa_all_nodes_ptr",
* which may represent all nodes or the nodes in the current cpuset.
*
* The caller must free the returned bitmask.
*/
struct bitmask *
numa_parse_nodestring(char *s)
{
int maxnode = numa_max_node();
int invert = 0, relative = 0;
int conf_nodes = numa_num_configured_nodes();
int task_nodes = numa_num_task_nodes();
char *end;
struct bitmask *mask;
mask = numa_allocate_nodemask();
if (s[0] == 0){
copy_bitmask_to_bitmask(numa_no_nodes_ptr, mask);
return mask; /* return freeable mask */
}
if (*s == '!') {
invert = 1;
s++;
}
if (*s == '+') {
relative++;
s++;
}
do {
unsigned long arg;
int i;
if (!strcmp(s,"all")) {
int i;
for (i = 0; i < task_nodes; i++)
numa_bitmask_setbit(mask, i);
s+=4;
break;
}
arg = get_nr(s, &end, numa_all_nodes_ptr, relative);
if (end == s) {
numa_warn(W_nodeparse, "unparseable node description `%s'\n", s);
goto err;
}
if (arg > maxnode) {
numa_warn(W_nodeparse, "node argument %d is out of range\n", arg);
goto err;
}
i = arg;
numa_bitmask_setbit(mask, i);
s = end;
if (*s == '-') {
char *end2;
unsigned long arg2;
arg2 = get_nr(++s, &end2, numa_all_nodes_ptr, relative);
if (end2 == s) {
numa_warn(W_nodeparse, "missing node argument %s\n", s);
goto err;
}
if (arg2 >= task_nodes) {
numa_warn(W_nodeparse, "node argument %d out of range\n", arg2);
goto err;
}
while (arg <= arg2) {
i = arg;
if (numa_bitmask_isbitset(numa_all_nodes_ptr,i))
numa_bitmask_setbit(mask, i);
arg++;
}
s = end2;
}
} while (*s++ == ',');
if (s[-1] != '\0')
goto err;
if (invert) {
int i;
for (i = 0; i < conf_nodes; i++) {
if (numa_bitmask_isbitset(mask, i))
numa_bitmask_clearbit(mask, i);
else
numa_bitmask_setbit(mask, i);
}
}
return mask;
err:
numa_bitmask_free(mask);
return NULL;
}
/*
* numa_parse_cpustring() is called to create a bitmask, given
* an ascii string such as 25 or 12-15 or 1,3,5-7 or +6-10.
* (the + indicates that the numbers are cpuset-relative)
*
* The cpus may be specified as absolute, or relative to the current cpuset.
* The list of available cpus for this task is in the map pointed to by
* "numa_all_cpus_ptr", which may represent all cpus or the cpus in the
* current cpuset.
*
* The caller must free the returned bitmask.
*/
struct bitmask *
numa_parse_cpustring(char *s)
{
int invert = 0, relative=0;
int conf_cpus = numa_num_configured_cpus();
int task_cpus = numa_num_task_cpus();
char *end;
struct bitmask *mask;
mask = numa_allocate_cpumask();
if (s[0] == 0)
return mask;
if (*s == '!') {
invert = 1;
s++;
}
if (*s == '+') {
relative++;
s++;
}
do {
unsigned long arg;
int i;
if (!strcmp(s,"all")) {
int i;
for (i = 0; i < task_cpus; i++)
numa_bitmask_setbit(mask, i);
s+=4;
break;
}
arg = get_nr(s, &end, numa_all_cpus_ptr, relative);
if (end == s) {
numa_warn(W_cpuparse, "unparseable cpu description `%s'\n", s);
goto err;
}
if (arg >= task_cpus) {
numa_warn(W_cpuparse, "cpu argument %s is out of range\n", s);
goto err;
}
i = arg;
numa_bitmask_setbit(mask, i);
s = end;
if (*s == '-') {
char *end2;
unsigned long arg2;
int i;
arg2 = get_nr(++s, &end2, numa_all_cpus_ptr, relative);
if (end2 == s) {
numa_warn(W_cpuparse, "missing cpu argument %s\n", s);
goto err;
}
if (arg2 >= task_cpus) {
numa_warn(W_cpuparse, "cpu argument %s out of range\n", s);
goto err;
}
while (arg <= arg2) {
i = arg;
if (numa_bitmask_isbitset(numa_all_cpus_ptr, i))
numa_bitmask_setbit(mask, i);
arg++;
}
s = end2;
}
} while (*s++ == ',');
if (s[-1] != '\0')
goto err;
if (invert) {
int i;
for (i = 0; i < conf_cpus; i++) {
if (numa_bitmask_isbitset(mask, i))
numa_bitmask_clearbit(mask, i);
else
numa_bitmask_setbit(mask, i);
}
}
return mask;
err:
numa_bitmask_free(mask);
return NULL;
}
|