Skip to content

Commit 0de1ca4

Browse files
committed
x
1 parent 67e6263 commit 0de1ca4

7 files changed

Lines changed: 256 additions & 12 deletions

File tree

.github/workflows/pr_push.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@ permissions:
1919
jobs:
2020
CodeChecks:
2121
uses: ./.github/workflows/reusable_checks.yml
22-
FastBuild:
23-
name: Fast builds
24-
needs: [CodeChecks]
25-
uses: ./.github/workflows/reusable_fast.yml
2622
Build:
2723
name: Basic builds
28-
needs: [FastBuild]
24+
2925
uses: ./.github/workflows/reusable_basic.yml
3026
DevDax:
31-
needs: [FastBuild]
27+
3228
uses: ./.github/workflows/reusable_dax.yml
3329
MultiNuma:
34-
needs: [FastBuild]
30+
3531
uses: ./.github/workflows/reusable_multi_numa.yml
3632
L0:
3733
needs: [Build]
@@ -56,10 +52,10 @@ jobs:
5652
runner: "CUDA"
5753
shared_lib: "['ON']"
5854
Sanitizers:
59-
needs: [FastBuild]
55+
6056
uses: ./.github/workflows/reusable_sanitizers.yml
6157
QEMU:
62-
needs: [FastBuild]
58+
6359
uses: ./.github/workflows/reusable_qemu.yml
6460
with:
6561
short_run: true

include/umf/providers/provider_os_memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ typedef enum umf_numa_mode_t {
4646
/// umf_numa_split_partition_t can be passed in umf_os_memory_provider_params_t structure
4747
/// to specify other distribution.
4848
UMF_NUMA_MODE_SPLIT,
49+
4950
/// The memory is allocated on the node of the CPU that triggered the
5051
/// allocation. If this mode is specified, nodemask must be NULL and
5152
/// maxnode must be 0.
@@ -58,6 +59,7 @@ typedef struct umf_numa_split_partition_t {
5859
/// The weight of the partition, representing the proportion of
5960
/// the allocation that should be assigned to this NUMA node.
6061
unsigned weight;
62+
6163
/// The NUMA node where the pages assigned to this partition will be bound.
6264
unsigned target;
6365
} umf_numa_split_partition_t;

src/memtargets/memtarget_numa.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ static umf_result_t numa_get_capacity(void *memTarget, size_t *capacity) {
218218
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
219219
}
220220

221+
#if defined(_WIN32) || defined(__APPLE__)
222+
221223
hwloc_topology_t topology = umfGetTopology();
222224
if (!topology) {
223225
return UMF_RESULT_ERROR_NOT_SUPPORTED;
@@ -234,6 +236,44 @@ static umf_result_t numa_get_capacity(void *memTarget, size_t *capacity) {
234236
}
235237

236238
*capacity = numaNode->attr->numanode.local_memory;
239+
240+
#else // Linux
241+
242+
struct numa_memtarget_t *numaTarget = (struct numa_memtarget_t *)memTarget;
243+
unsigned node = numaTarget->physical_id;
244+
245+
char path[256];
246+
snprintf(path, sizeof(path), "/sys/devices/system/node/node%u/meminfo",
247+
node);
248+
FILE *file = fopen(path, "r");
249+
if (!file) {
250+
LOG_PDEBUG("Opening sysfs file %s failed", path);
251+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
252+
}
253+
254+
char line[256];
255+
size_t node_size = 0;
256+
while (fgets(line, sizeof(line), file)) {
257+
// search for the MemTotal line
258+
if (strncmp(line, "Node ", 5) == 0 &&
259+
sscanf(line, "Node %u MemTotal: %zu kB", &node, &node_size) == 2 &&
260+
node == numaTarget->physical_id) {
261+
// convert kB to bytes
262+
node_size *= 1024;
263+
break;
264+
}
265+
}
266+
fclose(file);
267+
268+
if (node_size == 0) {
269+
LOG_ERR("Failed to find MemTotal for node %u", numaTarget->physical_id);
270+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
271+
}
272+
273+
*capacity = (size_t)node_size;
274+
275+
#endif
276+
237277
return UMF_RESULT_SUCCESS;
238278
}
239279

@@ -254,9 +294,24 @@ static size_t memattr_get_worst_value(memattr_type_t type) {
254294
}
255295
}
256296

297+
static size_t memattr_get_best_value(memattr_type_t type) {
298+
switch (type) {
299+
case MEMATTR_TYPE_BANDWIDTH:
300+
return SIZE_MAX;
301+
case MEMATTR_TYPE_LATENCY:
302+
return 0;
303+
default:
304+
assert(0); // Should not be reachable
305+
return 0;
306+
}
307+
}
308+
257309
static umf_result_t query_attribute_value(void *srcMemoryTarget,
258310
void *dstMemoryTarget, size_t *value,
259311
memattr_type_t type) {
312+
313+
#if defined(_WIN32) || defined(__APPLE__)
314+
260315
hwloc_topology_t topology = umfGetTopology();
261316
if (!topology) {
262317
LOG_PERR("Retrieving cached topology failed");
@@ -315,6 +370,60 @@ static umf_result_t query_attribute_value(void *srcMemoryTarget,
315370

316371
*value = memAttrValue;
317372

373+
#else
374+
375+
struct numa_memtarget_t *srcNumaTarget =
376+
(struct numa_memtarget_t *)srcMemoryTarget;
377+
struct numa_memtarget_t *dstNumaTarget =
378+
(struct numa_memtarget_t *)dstMemoryTarget;
379+
380+
if (srcNumaTarget->physical_id == dstNumaTarget->physical_id) {
381+
// If both targets are the same, we return the best possible value.
382+
*value = memattr_get_best_value(type);
383+
return UMF_RESULT_SUCCESS;
384+
}
385+
386+
// For Linux, we use sysfs to query the bandwidth and latency.
387+
char path[256];
388+
if (type == MEMATTR_TYPE_BANDWIDTH) {
389+
snprintf(path, sizeof(path),
390+
"/sys/devices/system/node/node%u/node%u/memory_bandwidth",
391+
srcNumaTarget->physical_id, dstNumaTarget->physical_id);
392+
} else if (type == MEMATTR_TYPE_LATENCY) {
393+
snprintf(path, sizeof(path),
394+
"/sys/devices/system/node/node%u/node%u/memory_latency",
395+
srcNumaTarget->physical_id, dstNumaTarget->physical_id);
396+
} else {
397+
assert(0); // Shouldn't be reachable.
398+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
399+
}
400+
FILE *file = fopen(path, "r");
401+
if (!file) {
402+
LOG_PDEBUG("Opening sysfs file %s failed", path);
403+
*value = memattr_get_worst_value(type);
404+
return UMF_RESULT_SUCCESS;
405+
}
406+
407+
char line[64];
408+
if (!fgets(line, sizeof(line), file)) {
409+
LOG_PDEBUG("Reading sysfs file %s failed", path);
410+
fclose(file);
411+
*value = memattr_get_worst_value(type);
412+
return UMF_RESULT_SUCCESS;
413+
}
414+
fclose(file);
415+
char *endptr;
416+
long long val = strtoll(line, &endptr, 10);
417+
if (endptr == line || *endptr != '\n' || val < 0) {
418+
LOG_PDEBUG("Parsing sysfs file %s failed", path);
419+
*value = memattr_get_worst_value(type);
420+
return UMF_RESULT_SUCCESS;
421+
}
422+
423+
*value = (size_t)val;
424+
425+
#endif // _WIN32 || _APPLE_
426+
318427
return UMF_RESULT_SUCCESS;
319428
}
320429

src/provider/provider_os_memory.c

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
#include <assert.h>
99
#include <errno.h>
1010
#include <limits.h>
11-
1211
#include <stddef.h>
1312
#include <stdio.h>
1413
#include <stdlib.h>
1514
#include <string.h>
15+
16+
#if !defined(_WIN32) && !defined(__APPLE__)
17+
#include <numaif.h>
18+
#include <sys/syscall.h>
19+
#endif
20+
1621
#include <umf.h>
1722
#include <umf/base.h>
1823
#include <umf/memory_provider.h>
@@ -32,8 +37,8 @@
3237
#define CTL_PROVIDER_TYPE os_memory_provider_t
3338
#include "provider_ctl_stats_impl.h"
3439

40+
#define MAX_NUMNODES 1024
3541
#define NODESET_STR_BUF_LEN 1024
36-
3742
#define TLS_MSG_BUF_LEN 1024
3843

3944
static const char *DEFAULT_NAME = "OS";
@@ -152,8 +157,42 @@ static umf_result_t initialize_nodeset(os_memory_provider_t *os_provider,
152157
// Hwloc_set_area_membind fails if empty nodeset is passed so
153158
// if no node is specified, just pass all available nodes.
154159
// For modes where no node is needed, they will be ignored anyway.
160+
161+
#if defined(_WIN32) || defined(__APPLE__)
155162
out_nodeset[0] = hwloc_bitmap_dup(
156163
hwloc_topology_get_complete_nodeset(os_provider->topo));
164+
#else
165+
size_t *nodes = umf_ba_global_alloc(sizeof(size_t) * MAX_NUMNODES);
166+
if (!nodes) {
167+
umf_ba_global_free(out_nodeset);
168+
os_provider->nodeset_len = 0;
169+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
170+
}
171+
172+
out_nodeset[0] = hwloc_bitmap_alloc();
173+
if (!out_nodeset[0]) {
174+
umf_ba_global_free(out_nodeset);
175+
umf_ba_global_free(nodes);
176+
os_provider->nodeset_len = 0;
177+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
178+
}
179+
180+
size_t num = 0;
181+
int ret = utils_get_complete_nodeset(nodes, MAX_NUMNODES, &num);
182+
if (ret < 0) {
183+
hwloc_bitmap_free(out_nodeset[0]);
184+
umf_ba_global_free(out_nodeset);
185+
umf_ba_global_free(nodes);
186+
os_provider->nodeset_len = 0;
187+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
188+
}
189+
190+
for (size_t i = 0; i < num; i++) {
191+
hwloc_bitmap_set(out_nodeset[0], (int)nodes[i]);
192+
}
193+
umf_ba_global_free(nodes);
194+
#endif
195+
157196
if (!out_nodeset[0]) {
158197
goto err_free_list;
159198
}
@@ -518,6 +557,11 @@ translate_params(const umf_os_memory_provider_params_t *in_params,
518557

519558
provider->numa_flags =
520559
getHwlocMembindFlags(in_params->numa_mode, is_dedicated_node_bind);
560+
561+
#if !defined(_WIN32) && !defined(__APPLE__)
562+
provider->dedicated = is_dedicated_node_bind;
563+
#endif
564+
521565
provider->mode = in_params->numa_mode;
522566
provider->part_size = in_params->part_size;
523567

@@ -561,6 +605,11 @@ static umf_result_t os_initialize(const void *params, void **provider) {
561605
snprintf(os_provider->name, sizeof(os_provider->name), "%s",
562606
in_params->name);
563607

608+
#if defined(_WIN32) || defined(__APPLE__)
609+
610+
//struct timespec ts_init_start, ts_init_end;
611+
//clock_gettime(CLOCK_MONOTONIC, &ts_init_start);
612+
564613
int r = hwloc_topology_init(&os_provider->topo);
565614
if (r) {
566615
LOG_ERR("HWLOC topology init failed");
@@ -577,6 +626,13 @@ static umf_result_t os_initialize(const void *params, void **provider) {
577626
goto err_destroy_hwloc_topology;
578627
}
579628

629+
//clock_gettime(CLOCK_MONOTONIC, &ts_init_end);
630+
//LOG_FATAL("HWLOC topology initialized in %ld.%09ld seconds",
631+
// ts_init_end.tv_sec - ts_init_start.tv_sec,
632+
// ts_init_end.tv_nsec - ts_init_start.tv_nsec);
633+
634+
#endif // _WIN32
635+
580636
os_provider->fd_offset_map = critnib_new(NULL, NULL);
581637
if (!os_provider->fd_offset_map) {
582638
LOG_ERR("creating file descriptor offset map failed");
@@ -625,8 +681,11 @@ static umf_result_t os_initialize(const void *params, void **provider) {
625681
err_destroy_critnib:
626682
critnib_delete(os_provider->fd_offset_map);
627683
err_destroy_hwloc_topology:
684+
685+
#if defined(_WIN32) || defined(__APPLE__)
628686
hwloc_topology_destroy(os_provider->topo);
629687
err_free_os_provider:
688+
#endif
630689
umf_ba_global_free(os_provider);
631690
return ret;
632691
}
@@ -649,7 +708,10 @@ static umf_result_t os_finalize(void *provider) {
649708
if (os_provider->nodeset_str_buf) {
650709
umf_ba_global_free(os_provider->nodeset_str_buf);
651710
}
711+
712+
#if defined(_WIN32) || defined(__APPLE__)
652713
hwloc_topology_destroy(os_provider->topo);
714+
#endif
653715
umf_ba_global_free(os_provider);
654716
return UMF_RESULT_SUCCESS;
655717
}
@@ -1012,10 +1074,52 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
10121074

10131075
do {
10141076
errno = 0;
1077+
ret = 0;
1078+
1079+
#if defined(_WIN32) || defined(__APPLE__)
10151080
ret = hwloc_set_area_membind(os_provider->topo, membind.addr,
10161081
membind.bind_size, membind.bitmap,
10171082
os_provider->numa_policy,
10181083
os_provider->numa_flags);
1084+
#else // !_WIN32 && !_APPLE__
1085+
1086+
// NOTE: could we done this
1087+
1088+
// on Linux, use mbind syscall directly instead of hwloc
1089+
unsigned long nodemask = 0;
1090+
int maxnode = 8 * sizeof(nodemask); // up to 64 nodes
1091+
if (membind.bitmap) {
1092+
for (int i = 0; i < maxnode; ++i) {
1093+
if (hwloc_bitmap_isset(membind.bitmap, i)) {
1094+
nodemask |= (1UL << i);
1095+
}
1096+
}
1097+
}
1098+
1099+
int mbind_mode = MPOL_DEFAULT;
1100+
if (os_provider->mode == UMF_NUMA_MODE_INTERLEAVE &&
1101+
os_provider->dedicated == 0) {
1102+
mbind_mode = MPOL_INTERLEAVE;
1103+
} else if (os_provider->mode == UMF_NUMA_MODE_SPLIT) {
1104+
mbind_mode = MPOL_BIND;
1105+
} else if (os_provider->mode == UMF_NUMA_MODE_LOCAL) {
1106+
mbind_mode = MPOL_LOCAL;
1107+
nodemask = 0;
1108+
} else if (os_provider->mode == UMF_NUMA_MODE_PREFERRED) {
1109+
mbind_mode = MPOL_BIND;
1110+
} else if (os_provider->mode == UMF_NUMA_MODE_BIND ||
1111+
os_provider->dedicated) {
1112+
mbind_mode = MPOL_BIND;
1113+
}
1114+
1115+
unsigned long mbind_flags = 0;
1116+
if (os_provider->dedicated) {
1117+
mbind_flags |= MPOL_MF_STRICT;
1118+
}
1119+
1120+
ret = syscall(__NR_mbind, membind.addr, membind.bind_size,
1121+
mbind_mode, &nodemask, maxnode, mbind_flags);
1122+
#endif // !_WIN32 && !_APPLE__
10191123

10201124
if (ret) {
10211125
os_store_last_native_error(UMF_OS_RESULT_ERROR_BIND_FAILED,

src/provider/provider_os_memory_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ typedef struct os_memory_provider_t {
6868
unsigned partitions_len;
6969
size_t partitions_weight_sum;
7070

71+
#if defined(_WIN32) || defined(__APPLE__)
7172
hwloc_topology_t topo;
73+
#else
74+
// NOTE: on linux we don't want to use hwloc_topology_t directly because
75+
// of its long initialization time
76+
int dedicated;
77+
#endif
7278

7379
char name[64];
7480

src/utils/utils_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ size_t utils_max(size_t a, size_t b);
188188

189189
size_t utils_min(size_t a, size_t b);
190190

191+
int utils_get_complete_nodeset(size_t *ids, size_t size, size_t *);
192+
191193
#ifdef __cplusplus
192194
}
193195
#endif

0 commit comments

Comments
 (0)