Skip to content

Commit 96bb42a

Browse files
committed
x
1 parent 67e6263 commit 96bb42a

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

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/libumf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ umf_result_t umfInit(void) {
4545
utils_mutex_lock(&initMutex);
4646

4747
if (umfRefCount == 0) {
48+
// debug comment
49+
LOG_FATAL("umfInit");
50+
4851
utils_log_init();
4952
umf_result_t umf_result = umfMemoryTrackerCreate(&TRACKER);
5053
if (umf_result != UMF_RESULT_SUCCESS) {

src/provider/provider_os_memory.c

Lines changed: 105 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+
#ifndef _WIN32
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+
#ifdef _WIN32
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,12 @@ 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+
#ifndef _WIN32
562+
provider->umf_numa_mode = in_params->numa_mode;
563+
provider->dedicated = is_dedicated_node_bind;
564+
#endif
565+
521566
provider->mode = in_params->numa_mode;
522567
provider->part_size = in_params->part_size;
523568

@@ -561,6 +606,11 @@ static umf_result_t os_initialize(const void *params, void **provider) {
561606
snprintf(os_provider->name, sizeof(os_provider->name), "%s",
562607
in_params->name);
563608

609+
#ifdef _WIN32
610+
611+
//struct timespec ts_init_start, ts_init_end;
612+
//clock_gettime(CLOCK_MONOTONIC, &ts_init_start);
613+
564614
int r = hwloc_topology_init(&os_provider->topo);
565615
if (r) {
566616
LOG_ERR("HWLOC topology init failed");
@@ -577,6 +627,13 @@ static umf_result_t os_initialize(const void *params, void **provider) {
577627
goto err_destroy_hwloc_topology;
578628
}
579629

630+
//clock_gettime(CLOCK_MONOTONIC, &ts_init_end);
631+
//LOG_FATAL("HWLOC topology initialized in %ld.%09ld seconds",
632+
// ts_init_end.tv_sec - ts_init_start.tv_sec,
633+
// ts_init_end.tv_nsec - ts_init_start.tv_nsec);
634+
635+
#endif // _WIN32
636+
580637
os_provider->fd_offset_map = critnib_new(NULL, NULL);
581638
if (!os_provider->fd_offset_map) {
582639
LOG_ERR("creating file descriptor offset map failed");
@@ -625,8 +682,10 @@ static umf_result_t os_initialize(const void *params, void **provider) {
625682
err_destroy_critnib:
626683
critnib_delete(os_provider->fd_offset_map);
627684
err_destroy_hwloc_topology:
685+
#ifdef _WIN32
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,9 @@ 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+
#ifdef _WIN32
652712
hwloc_topology_destroy(os_provider->topo);
713+
#endif
653714
umf_ba_global_free(os_provider);
654715
return UMF_RESULT_SUCCESS;
655716
}
@@ -1012,10 +1073,52 @@ static umf_result_t os_alloc(void *provider, size_t size, size_t alignment,
10121073

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

10201123
if (ret) {
10211124
os_store_last_native_error(UMF_OS_RESULT_ERROR_BIND_FAILED,

src/provider/provider_os_memory_internal.h

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

71+
#ifndef _WIN32
72+
// NOTE: on linux we don't want to use hwloc_topology_t directly because
73+
// of its long initialization time
74+
umf_numa_mode_t umf_numa_mode;
75+
int dedicated;
76+
77+
#else
7178
hwloc_topology_t topo;
79+
#endif
7280

7381
char name[64];
7482

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

src/utils/utils_linux_common.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
*
88
*/
99

10+
#include <dirent.h>
1011
#include <errno.h>
1112
#include <fcntl.h>
1213
#include <stdbool.h>
14+
#include <stdlib.h>
15+
#include <string.h>
1316
#include <sys/mman.h>
1417
#include <sys/stat.h>
1518
#include <sys/syscall.h>
@@ -240,3 +243,25 @@ int utils_create_anonymous_fd(void) {
240243

241244
return fd;
242245
}
246+
247+
int utils_get_complete_nodeset(size_t *nodes, size_t nodes_size, size_t *num) {
248+
DIR *dir = opendir("/sys/devices/system/node/");
249+
if (!dir) {
250+
return -1;
251+
}
252+
253+
struct dirent *entry;
254+
while ((entry = readdir(dir)) != NULL) {
255+
if (strncmp(entry->d_name, "node", 4) == 0) {
256+
char *endptr;
257+
long node_id = strtol(entry->d_name + 4, &endptr, 10);
258+
if (*endptr == '\0' && node_id >= 0 && *num < nodes_size) {
259+
nodes[*num] = (size_t)node_id;
260+
(*num)++;
261+
}
262+
}
263+
}
264+
265+
closedir(dir);
266+
return 0;
267+
}

0 commit comments

Comments
 (0)