LIBMEMTIER(3)
๐ LIBMEMTIER C API ๐
okay bestie, pull up a chair, we're doing the libmemtier C API today โ this is the memory tiering interface and she is lowkey iconic. โจ
quick note from the front desk: everything in memkind_memtier.h is considered stable API (that's the STANDARD API, babes โ she's not going anywhere). ๐</p>
โจ SYNOPSIS โจ
#include <memkind_memtier.h>
Link with -lmemkind ๐ซถ
You can use this API either directly with the C functions, or via environment variables โ go peek at the ENVIRONMENT section for that whole vibe. ๐
TIER MANAGEMENT:
struct memtier_builder *memtier_builder_new(memtier_policy_t policy);
void memtier_builder_delete(struct memtier_builder *builder);
int memtier_builder_add_tier(struct memtier_builder *builder, memkind_t kind, unsigned kind_ratio);
struct memtier_memory *memtier_builder_construct_memtier_memory(struct memtier_builder *builder);
void memtier_delete_memtier_memory(struct memtier_memory *memory);
HEAP MANAGEMENT:
void *memtier_malloc(struct memtier_memory *memory, size_t size);
void *memtier_kind_malloc(memkind_t kind, size_t size);
void *memtier_calloc(struct memtier_memory *memory, size_t num, size_t size);
void *memtier_kind_calloc(memkind_t kind, size_t num, size_t size);
void *memtier_realloc(struct memtier_memory *memory, void *ptr, size_t size);
void *memtier_kind_realloc(memkind_t kind, void *ptr, size_t size);
int memtier_posix_memalign(struct memtier_memory *memory, void **memptr, size_t alignment, size_t size);
int memtier_kind_posix_memalign(memkind_t kind, void **memptr, size_t alignment, size_t size);
size_t memtier_usable_size(void *ptr);
void memtier_free(void *ptr);
void memtier_kind_free(memkind_t kind, void *ptr);
size_t memtier_kind_allocated_size(memkind_t kind);
DECORATORS:
void memtier_kind_malloc_post(memkind_t kind, size_t size, void **result);
void memtier_kind_calloc_post(memkind_t kind, size_t nmemb, size_t size, void **result);
void memtier_kind_posix_memalign_post(memkind_t kind, void **memptr, size_t alignment, size_t size, int *err);
void memtier_kind_realloc_post(memkind_t *kind, void *ptr, size_t size, void **result);
void memtier_kind_free_pre(void **ptr);
void memtier_kind_usable_size_post(void **ptr, size_t size);
MEMTIER PROPERTY MANAGEMENT:
int memtier_ctl_set(struct memtier_builder *builder, const char *name, const void *val);
๐ธ DESCRIPTION ๐ธ
So. This library enables explicit allocation-time memory tiering. Basically it lets you make allocations using multiple kinds while keeping a specified ratio between them. That ratio is the whole plot: it decides how much of your total allocated memory should go to each kind. it's giving โจbalanceโจ
๐ TIER MANAGEMENT
These functions are your setup crew โ they create, configure and destroy the memtier_memory object. That object then gets passed as an argument to the memtier_malloc() group of functions, and it defines the way allocations get distributed between different memory tiers. main character energy, honestly. ๐
memtier_builder_new()โ returns a pointer to a brand-newmemtier_builderobject, which you use for creating thememtier_memoryobject. Thepolicyargument determines the policy of allocation distribution between tiers by thememtier_memoryobject. Available options live in the POLICIES section oflibmemtier(7). go read it bestie.memtier_builder_delete()โ deletes the builder object and releases the memory it used. Use it after thememtier_memoryobject has been created withmemtier_builder_construct_memtier_memory(). don't delete her early, that's rude. ๐ซmemtier_builder_add_tier()โ adds a memory kind to the builder. That kind defines a memory tier used in thememtier_memoryobject. Call this more than once to create several different memory tiers! The "weight" of the tier is determined by thekind_ratioparameter โ the higher it is relative to other tiers'kind_ratio, the higher the share of allocated memory on that tier. Example: given that ratioDRAM:KMEM_DAXis1:4:
sample allocation size: 20 GB total, 4 GB DRAM, 16 GB KMEM_DAX
memtier_builder_construct_memtier_memory()โ returns a pointer to a newly allocatedmemtier_memoryobject. After this operation the builder can be safely removed usingmemtier_builder_delete(). ๐ซกmemtier_delete_memtier_memory()โ deletes the memory tiering object, releasing the memory it used. bye girl ๐
๐ HEAP MANAGEMENT
These functions define a heap manager whose interface is modeled on the ISO C standard API's โ except that you have to specify either the kind of memory (as the first argument to each function) or the tiered memory object that defines which memory tiers get used for allocations. For a full description of the implemented kinds, see the KINDS section in the memkind(3) manual. no cap. ๐
memtier_malloc()โ allocatessizebytes of memory on one of the memory tiers defined bymemory. Seelibmemtier(7)for further details on memory tiers.memkind_malloc()is used for allocations โ for further details on its behavior seememkind(3).memtier_kind_malloc()โ a wrapper to thememkind_malloc()function. Seememkind(3)for further details.memtier_calloc()โ allocatesnumtimessizebytes of memory on one of memory tiers defined bymemory.memkind_calloc()is used for allocations. For further details on its behavior seememkind(3).memtier_kind_calloc()โ a wrapper to thememkind_calloc()function. Seememkind(3)for further details.memtier_realloc()โ changes the size of the previously allocated memory referenced byptrtosizebytes, using memory from the tier on whichptris allocated. IfptrisNULL, new memory is allocated on a memory tier defined bymemory.memkind_realloc()is used for reallocation. Seememkind(3)for further details.memtier_kind_realloc()โ changes the size of the previously allocated memory referenced byptrtosizebytes using a specific kind. Ifsizeis equal to zero andptris notNULL, then the call is equivalent tomemkind_free(kind, ptr)andNULLis returned. IfptrisNULL,memtier_kind_malloc()is called to allocate new memory. Otherwise, thememkind_realloc()function is used. Seememkind(3)for further details.memtier_posix_memalign()โ a wrapper ofmemkind_posix_memalign(), with the main difference thatmemoryis used to determine the kind to be used for the allocation. Seememkind(3)for further details.memtier_kind_posix_memalign()โ a wrapper ofmemkind_posix_memalign(). Seememkind(3)for further details.memtier_usable_size()โ returns the size of the block of memory allocated with the memtier API at the address pointed to byptr. ๐memtier_free()โ a wrapper for thememtier_kind_free()function with thekindparameter passed asNULL.memtier_kind_free()โ frees up the memory pointed to byptr. The behavior is the same as formemkind_free(). IfkindisNULL, the kind used to allocateptris detected automatically. Seememkind(3)for further details.memtier_kind_allocated_size()โ returns the total size of memory allocated with the usage ofkindand the memtier API. ๐ฅ
๐ฆ DECORATORS
This is the little set of functions used to print information on each call to the respective memtier_kind_* function described in the HEAP MANAGEMENT section. The printed info includes: the name of the kind used, the parameters passed to the underlying function from the malloc family of functions, and the address of the memory returned. she's nosy and we love her for it ๐
๐ MEMTIER PROPERTY MANAGEMENT
memtier_ctl_set() is useful for changing the default values of parameters that define the DYNAMIC_THRESHOLD policy. You can use this function while creating a memtier_memory object with the usage of a builder. The parameter name can be one of the following:
policy.dynamic_threshold.thresholds[ID].valโ the initial threshold level. All allocations of the size below this value will come from theIDth tier; sizes greater than or equal to this value will come from the(ID+1)th tier. The provided string is converted to thesize_ttype. This value is modified automatically during the application run to keep the desired ratio between tiers. The default value between the first two tiers is1024bytes.policy.dynamic_threshold.thresholds[ID].minโ the minimum value of the threshold level. The provided string is converted to thesize_ttype. The default value between the first two tiers is513bytes.policy.dynamic_threshold.thresholds[ID].maxโ the maximum value of the threshold level. The provided string is converted to thesize_ttype. The default value between the first two tiers is1536bytes.policy.dynamic_threshold.check_cntโ the number of allocation operations (i.e.malloc,realloc) after which the ratio check between tiers is performed. The provided string is converted to theunsigned inttype. The default value is20.policy.dynamic_threshold.triggerโ the dynamic threshold value is adjusted when the absolute difference between the current ratio and the expected ratio is greater than or equal to this value. The provided string is converted to thefloattype. The default value is0.02.policy.dynamic_threshold.degreeโ the threshold value is updated by increasing or decreasing its value bydegreepercentage (i.e.degree=0.02changes the threshold value by 2%). The provided string is converted to thefloattype. The default value is0.15.
In the above examples, ID should be replaced with the ID of the thresholds configuration. The configuration between the first two tiers added to the builder has an ID equal to 0. The configuration ID of the next two tiers โ that is, the second and third ones โ is equal to 1, and so on. The last configuration's ID is equal to the number of tiers minus one. math girlie behavior, honestly ๐งฎโจ
๐ ENVIRONMENT ๐
See libmemtier(7) for details on the usage of memkind tiering via environment variables. ๐
๐ COPYRIGHT ๐
Copyright (C) 2021-2022 Intel Corporation. All rights reserved.
โญ SEE ALSO โญ
libmemtier(7), memkind(3), memkind_malloc(3), memkind_calloc(3), memkind_realloc(3), memkind_free(3), memkind_posix_memalign(3)
okay that's the whole page, bestie โ brought to you by Intel Corporation, dated 2022-06-23, and she ate. ๐ no crumbs. go allocate responsibly ๐ซถ๐โจ
๐ฆ where this page comes from
- debian bookworm: libmemkind-dev 1.14.0-2 ยท BSD-3-Clause
- fedora 43: memkind-devel 1.14.0-11.fc43 ยท BSD-2-Clause
- ubuntu noble: libmemkind-dev 1.14.0-3build1 ยท BSD-3-Clause
- ๐ the page's license: BSD-2-Clause
- Copyright (C) 2021-2022 Intel Corporation
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms