oneapi-src / oneapi-src/unified-memory-framework

malloc user callbacks for the proxy lib proposal

Open
#1,048 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C
Stars
98
Forks
48
Avg merge
3d 20h
Merged PRs (30d)
6

Description

Malloc user callbacks for the proxy lib proposal.

Rationale

Some users are interested in performing additional actions each time the app launches with the UMF proxy library, which utilizes malloc/free functions. These actions may include printing messages, updating statistics, adding user-defined metadata to the allocation, or modifying the default behaviors of the UMF based on callback parameters. These objectives can be achieved by adding an API that allows the registration of callbacks to be invoked by the UMF.

Description

This API proposal introduces functions to the UMF Proxy Library to register user-defined callbacks and define their signatures. Each function managed by the UMF Proxy Library will trigger Pre and Post callbacks, which are called before and after the UMF performs the desired action, respectively. Each callback will also receive a pointer to user-defined data, which can be unique for each callback. Users can "clear" a callback by setting NULL as a parameter to the umfSetProxyLibHandler*Pre/Post functions, which may be important to do before the application closes to not crash any system malloc/free calls.

Note that, where applicable, the parameters passed to the callbacks can be both read and written, allowing users to modify, for example, the size of the allocation in mallocPre or to shift the pointer in mallocPost.

Question: Currently, the callbacks are not "chained," meaning that for a particular Pre/Post action, only one callback can be defined. Should we allow multiple callbacks to be set for a single Pre/Post action?

API Changes

// callback signatures - malloc
void (*umf_proxy_lib_handler_malloc_pre_t)(void *user_data, size_t *size)
void (*umf_proxy_lib_handler_malloc_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// aligned alloc
void (*umf_proxy_lib_handler_aligned_malloc_pre_t)(void *user_data, umf_memory_pool_handle_t *pool, size_t *size, size_t *alignment)
void (*umf_proxy_lib_handler_aligned_malloc_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// free
(*umf_proxy_lib_handler_free_pre_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)
(*umf_proxy_lib_handler_free_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// TODO calloc, realloc, malloc_usable_size

// setters

void umfSetProxyLibHandlerMallocPre(umf_proxy_lib_handler_malloc_pre_t handler, void *user_data);
void umfSetProxyLibHandlerMallocPost(umf_proxy_lib_handler_malloc_post_t handler, void *user_data);

void umfSetProxyLibHandlerAlignedMallocPre(umf_proxy_lib_handler_aligned_malloc_pre_t handler, void *user_data);
void umfSetProxyLibHandlerAlignedMallocPost(umf_proxy_lib_handler_aligned_malloc_post_t handler, void *user_data);

// etc ...

POC: https://github.com/oneapi-src/unified-memory-framework/pull/978/files

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the callback API proposal and the proof-of-concept changes in PR 978. Resolve the open question about chaining callbacks and determine the scope of the TODO callbacks before defining what a complete implementation and validation would require.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.