Skip to content

Commit 986ca00

Browse files
committed
oshmem: spml: add memory allocation hook
The hook is called from memheap when memory range is going to be allocated by smalloc(), realloc() and others. ucx spml uses this hook to call ucp_mem_advise in order to speedup non blocking memory mapping. Signed-off-by: Alex Mikheev <[email protected]>
1 parent 896434b commit 986ca00

File tree

9 files changed

+53
-2
lines changed

9 files changed

+53
-2
lines changed

oshmem/mca/memheap/buddy/memheap_buddy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static int _do_alloc(uint32_t order,
469469
}
470470

471471
*p_buff = (void*) addr;
472-
/* no barrier because it is not required by spec! */
472+
MCA_SPML_CALL(memuse_hook(addr, 1<<order));
473473
return OSHMEM_SUCCESS;
474474

475475
alloc_error: _buddy_free(&memheap_buddy, offset, order, heap);

oshmem/mca/memheap/ptmalloc/memheap_ptmalloc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int mca_memheap_ptmalloc_alloc(size_t size, void** p_buff)
8585
if (NULL == *p_buff)
8686
return OSHMEM_ERROR;
8787

88+
MCA_SPML_CALL(memuse_hook(*p_buff, size));
8889
return OSHMEM_SUCCESS;
8990
}
9091

@@ -113,6 +114,7 @@ int mca_memheap_ptmalloc_align(size_t align, size_t size, void **p_buff)
113114
if (NULL == *p_buff)
114115
return OSHMEM_ERROR;
115116

117+
MCA_SPML_CALL(memuse_hook(*p_buff, size));
116118
return OSHMEM_SUCCESS;
117119
}
118120

@@ -132,6 +134,7 @@ int mca_memheap_ptmalloc_realloc(size_t new_size,
132134
if (!*p_new_buff)
133135
return OSHMEM_ERR_OUT_OF_RESOURCE;
134136

137+
MCA_SPML_CALL(memuse_hook(*p_new_buff, new_size));
135138
return OSHMEM_SUCCESS;
136139
}
137140

oshmem/mca/spml/base/base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ OSHMEM_DECLSPEC int mca_spml_base_get_nb(void *dst_addr,
8484
int src,
8585
void **handle);
8686

87+
OSHMEM_DECLSPEC void mca_spml_base_memuse_hook(void *addr, size_t length);
8788
/*
8889
* MCA framework
8990
*/

oshmem/mca/spml/base/spml_base.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,7 @@ int mca_spml_base_get_nb(void *dst_addr, size_t size,
177177
{
178178
return OSHMEM_ERROR;
179179
}
180+
181+
void mca_spml_base_memuse_hook(void *addr, size_t length)
182+
{
183+
}

oshmem/mca/spml/ikrit/spml_ikrit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ mca_spml_ikrit_t mca_spml_ikrit = {
171171
mca_spml_ikrit_fence,
172172
mca_spml_ikrit_cache_mkeys,
173173
mca_spml_base_rmkey_free,
174+
mca_spml_base_memuse_hook,
174175

175176
(void*)&mca_spml_ikrit
176177
}

oshmem/mca/spml/spml.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ typedef int (*mca_spml_base_module_fence_fn_t)(void);
277277
*/
278278
typedef int (*mca_spml_base_module_wait_nb_fn_t)(void *);
279279

280+
/**
281+
* Called by memheap when memory is allocated by shmalloc(),
282+
* shcalloc(), shmemalign() or shrealloc()
283+
*
284+
* @param addr base address of the registered buffer.
285+
* @param size the size of the buffer to be registered.
286+
*/
287+
typedef void (*mca_spml_base_module_memuse_hook_fn_t)(void *, size_t);
288+
280289
/**
281290
* SPML instance.
282291
*/
@@ -304,6 +313,8 @@ struct mca_spml_base_module_1_0_0_t {
304313

305314
mca_spml_base_module_mkey_unpack_fn_t spml_rmkey_unpack;
306315
mca_spml_base_module_mkey_free_fn_t spml_rmkey_free;
316+
317+
mca_spml_base_module_memuse_hook_fn_t spml_memuse_hook;
307318
void *self;
308319
};
309320

oshmem/mca/spml/ucx/spml_ucx.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#define SPML_UCX_PUT_DEBUG 0
4444
#endif
4545

46-
4746
mca_spml_ucx_t mca_spml_ucx = {
4847
{
4948
/* Init mca_spml_base_module_t */
@@ -65,6 +64,7 @@ mca_spml_ucx_t mca_spml_ucx = {
6564
every spml */
6665
mca_spml_ucx_rmkey_unpack,
6766
mca_spml_ucx_rmkey_free,
67+
mca_spml_ucx_memuse_hook,
6868
(void*)&mca_spml_ucx
6969
},
7070

@@ -385,6 +385,34 @@ void mca_spml_ucx_rmkey_unpack(sshmem_mkey_t *mkey, uint32_t segno, int pe, int
385385
return;
386386
}
387387

388+
void mca_spml_ucx_memuse_hook(void *addr, size_t length)
389+
{
390+
int my_pe = oshmem_my_proc_id();
391+
spml_ucx_mkey_t *ucx_mkey;
392+
ucp_mem_advise_params_t params;
393+
ucs_status_t status;
394+
395+
if (!(mca_spml_ucx.heap_reg_nb && memheap_is_va_in_segment(addr, HEAP_SEG_INDEX))) {
396+
return;
397+
}
398+
399+
ucx_mkey = &mca_spml_ucx.ucp_peers[my_pe].mkeys[HEAP_SEG_INDEX].key;
400+
401+
params.field_mask = UCP_MEM_ADVISE_PARAM_FIELD_ADDRESS |
402+
UCP_MEM_ADVISE_PARAM_FIELD_LENGTH |
403+
UCP_MEM_ADVISE_PARAM_FIELD_ADVICE;
404+
405+
params.address = addr;
406+
params.length = length;
407+
params.advice = UCP_MADV_WILLNEED;
408+
409+
status = ucp_mem_advise(mca_spml_ucx.ucp_context, ucx_mkey->mem_h, &params);
410+
if (UCS_OK != status) {
411+
SPML_ERROR("ucp_mem_advise failed addr %p len %llu",
412+
addr, (unsigned long long)length);
413+
}
414+
}
415+
388416
sshmem_mkey_t *mca_spml_ucx_register(void* addr,
389417
size_t size,
390418
uint64_t shmid,

oshmem/mca/spml/ucx/spml_ucx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ extern sshmem_mkey_t *mca_spml_ucx_register(void* addr,
108108
int *count);
109109
extern int mca_spml_ucx_deregister(sshmem_mkey_t *mkeys);
110110

111+
extern void mca_spml_ucx_memuse_hook(void *addr, size_t length);
112+
111113
extern void mca_spml_ucx_rmkey_unpack(sshmem_mkey_t *mkey, uint32_t segno, int pe, int tr_id);
112114
extern void mca_spml_ucx_rmkey_free(sshmem_mkey_t *mkey);
113115

oshmem/mca/spml/yoda/spml_yoda.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ mca_spml_yoda_module_t mca_spml_yoda = {
6565
mca_spml_yoda_fence,
6666
mca_spml_base_rmkey_unpack,
6767
mca_spml_base_rmkey_free,
68+
mca_spml_base_memuse_hook,
6869

6970
(void *)&mca_spml_yoda
7071
}

0 commit comments

Comments
 (0)