VMEMCACHE(3)
okay bestie, welcome to VMEMCACHE(3) from the PMDK Programmer's Manual π
this is the whole vibe, and it's lowkey iconic.
πΈ NAME πΈ
vmemcache β buffer-based LRU cache. That's the name, no cap. π
β¨ SYNOPSIS β¨
#include <libvmemcache.h>
VMEMcache *vmemcache_new();
void vmemcache_delete(VMEMcache *cache);
int vmemcache_set_eviction_policy(VMEMcache *cache,
enum vmemcache_repl_p repl_p);
int vmemcache_set_size(VMEMcache *cache, size_t size);
int vmemcache_set_extent_size(VMEMcache *cache, size_t extent_size);
int vmemcache_add(VMEMcache *cache, const char *path);
void vmemcache_callback_on_evict(VMEMcache *cache,
vmemcache_on_evict *evict, void *arg);
void vmemcache_callback_on_miss(VMEMcache *cache,
vmemcache_on_miss *miss, void *arg);
ssize_t vmemcache_get(VMEMcache *cache,
const void *key, size_t key_size,
void *vbuf, size_t vbufsize, size_t offset, size_t *vsize);
int vmemcache_put(VMEMcache *cache,
const void *key, size_t key_size,
const void *value, size_t value_size);
int vmemcache_exists(VMEMcache *cache,
const void *key, size_t key_size);
int vmemcache_evict(VMEMcache *cache, const void *key, size_t ksize);
int vmemcache_get_stat(VMEMcache *cache,
enum vmemcache_statistic stat,
void *value, size_t value_size);
const char *vmemcache_errormsg(void);
π¦ DESCRIPTION π¦
libvmemcache is a volatile key-value store optimized for operating on NVDIMM based space, although it can work with any filesystem, stored in memory (tmpfs) or, less performant, on some kind of a disk.
π Creation π
VMEMcache *vmemcache_new();β creates an empty unconfigured vmemcache instance.int vmemcache_set_size(VMEMcache *cache, size_t size);β sets the size of the cache; it will be rounded up towards a whole page size alignment (4KB on x86).int vmemcache_set_extent_size(VMEMcache *cache, size_t extent_size);β sets block size of the cache β 256 bytes minimum, strongly recommended to be a multiple of 64 bytes. If the cache is backed by a non byte-addressable medium, the extent size should be 4096 (or a multiple) or performance will greatly suffer.vmemcache_set_eviction_policyβ sets what should happen on a put into a full cache.
int vmemcache_set_eviction_policy(VMEMcache *cache,
enum vmemcache_repl_p repl_p);
VMEMCACHE_REPLACEMENT_NONE: manual eviction only - puts into a full cache will failVMEMCACHE_REPLACEMENT_LRU: least recently accessed entry will be evicted to make space when needed
vmemcache_addβ associate the cache with a backing medium in the givenpath, which may be:
int vmemcache_add(VMEMcache *cache, const char *path);
- a
/dev/daxdevice - a directory on a regular filesystem (which may or may not be mounted with
-o dax, either on persistent memory or any other backing storage)
void vmemcache_delete(VMEMcache *cache);β frees any structures associated with the cache.
π Use π
vmemcache_getβ searches for an entry with the given key; it doesnβt have to be zero-terminated or be text - any sequence of bytes of lengthkey_sizeis okay. If found, the entryβs value is copied tovbufthat has space forvbufsizebytes, optionally skippingoffsetbytes at the start. No matter if the copy was truncated or not, its true size is stored intovsize;vsizeremains unmodified if the key was not found.
ssize_t vmemcache_get(VMEMcache *cache,
const void *key, size_t key_size,
void *vbuf, size_t vbufsize, size_t offset, size_t *vsize);
Return value is number of bytes successfully copied, or -1 on error. In particular, if thereβs no entry for the given key in the cache, the errno will be ENOENT.
vmemcache_putβ inserts the given key:value pair into the cache. Returns0on success,-1on error. Inserting a key that already exists will fail withEEXIST.
int vmemcache_put(VMEMcache *cache,
const void *key, size_t key_size,
const void *value, size_t value_size);
vmemcache_existsβ searches for an entry with the given key, and returns1if found,0if not found, and-1if search couldnβt be performed. The size of the found entry is stored intovsize;vsizeremains unmodified if the key was not found. This function does not impact the replacement policy or statistics.
int vmemcache_exists(VMEMcache *cache, const void *key, size_t key_size,
size_t *vsize);
int vmemcache_evict(VMEMcache *cache, const void *key, size_t ksize);β removes the given key from the cache. Ifkeyis null and there is a replacement policy set, the oldest entry will be removed. Returns0if an entry has been evicted,-1otherwise.
πΈ Callbacks πΈ
You can register a hook to be called during eviction or after a cache miss, using vmemcache_callback_on_evict() or vmemcache_callback_on_miss(), respectively:
vmemcache_callback_on_evict
void vmemcache_callback_on_evict(VMEMcache *cache,
vmemcache_on_evict *evict, void *arg);
vmemcache_callback_on_miss
void vmemcache_callback_on_miss(VMEMcache *cache,
vmemcache_on_miss *miss, void *arg);
The extra arg will be passed to your function.
A hook to be called during eviction has to have the following signature:
void vmemcache_on_evict(VMEMcache *cache, const void *key, size_t
key_size, void *arg);
Called when an entry is being removed from the cache. The eviction canβt be prevented, but until the callback returns, the entry remains available for queries. The thread that triggered the eviction is blocked in the meantime.
A hook to be called after a cache miss has to have the following signature:
void vmemcache_on_miss(VMEMcache *cache, const void *key, size_t
key_size, void *arg);
Called when a get query fails, to provide an opportunity to insert the missing key. If the callback calls put for that specific key, the get will return its value, even if it did not fit into the cache.
π§ Misc π§
vmemcache_get_statβ obtains a piece of statistics about the cache. Thestatmay be:
int vmemcache_get_stat(VMEMcache *cache, enum vmemcache_statistic stat,
void *value, size_t value_size);
VMEMCACHE_STAT_PUTβ count of putsVMEMCACHE_STAT_GETβ count of getsVMEMCACHE_STAT_HITβ count of gets that were served from the cacheVMEMCACHE_STAT_MISSβ count of gets that were not present in the cacheVMEMCACHE_STAT_EVICTβ count of evictionsVMEMCACHE_STAT_ENTRIESβ current number of cache entries (key:value pairs)VMEMCACHE_STAT_DRAM_SIZE_USEDβ current amount of DRAM usedVMEMCACHE_STAT_POOL_SIZE_USEDβ current usage of data poolVMEMCACHE_STAT_HEAP_ENTRIESβ current number of discontiguous unused regions (ie, free space fragmentation)
Statistics are enabled by default. They can be disabled at the compile time of the vmemcache library if the STATS_ENABLED CMake option is set to OFF.
const char *vmemcache_errormsg(void);β retrieves a human-friendly description of the last error.
π Errors π
On an error, a machine-usable description is passed in errno. It may be:
EINVALβ nonsensical/invalid parameterENOMEMβ out of DRAMEEXISTβ (put) entry for that key already existsENOENTβ (evict,get) no entry for that keyESRCHβ (evict) could not find an evictable entryEAGAINβ (evict) an entry was used and could not be evicted, please try againENOSPCβ (create,put) not enough space in the memory pool
mwah π that's the whole page, bestie. This was VMEMCACHE(3) from the PMDK Programmer's Manual, dated 2020-11-20. PMDK β 2020-11-20 β VMEMCACHE(3), periodt. π
π¦ where this page comes from
- ubuntu jammy: libvmemcache-dev 0.8.1-4 Β· BSD-3
- Copyright 2018-2020, Intel Corporation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms