libvmmalloc man page

libvmmalloc -- general purpose volatile memory allocation library

Synopsis

$ LD_PRELOAD=libvmmalloc.so command [ args... ]

or

#include <stdlib.h>
#ifndef __FreeBSD__
    #include <malloc.h>
#else
    #include <malloc_np.h>
#endif
#include <libvmmalloc.h>

cc [ flag... ] file... -lvmmalloc [ library... ]
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t number, size_t size);
void *realloc(void *ptr, size_t size);
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);
void *memalign(size_t alignment, size_t size);
void *valloc(size_t size);
void *pvalloc(size_t size);
size_t malloc_usable_size(const void *ptr);
void cfree(void *ptr);

Description

libvmmalloc transparently converts all dynamic memory allocations into Persistent Memory allocations.

The typical usage of libvmmalloc does not require any modification of the target program. It is enough to load libvmmalloc before all other libraries by setting the environment variable LD_PRELOAD. When used in that way, libvmmalloc interposes the standard system memory allocation routines, as defined in malloc(3), posix_memalign(3) and malloc_usable_size(3), and provides that all dynamic memory allocations are made from a memory pool built on a memory-mapped file, instead of the system heap. The memory managed by libvmmalloc may have different attributes, depending on the file system containing the memory-mapped file. In particular, libvmmalloc is part of the Persistent Memory Development Kit because it is sometimes useful to use non-volatile memory as a volatile memory pool, leveraging its capacity, cost, or performance characteristics.

libvmmalloc may be also linked to the program, by providing the **-lvmmalloc* argument to the linker. Then it becomes the default memory allocator for the program.

NOTE: Due to the fact the library operates on a memory-mapped file, it may not work properly with programs that perform fork(2) not followed by exec(3). There are two variants of experimental fork(2) support available in libvmmalloc. The desired library behavior may be selected by setting the VMMALLOC_FORK environment variable. By default variant #1 is enabled. See Environment for more details.

libvmmalloc uses the mmap(2) system call to create a pool of volatile memory. The library is most useful when used with Direct Access storage (DAX), which is memory-addressable persistent storage that supports load/store access without being paged via the system page cache. A Persistent Memory-aware file system is typically used to provide this type of access. Memory-mapping a file from a Persistent Memory-aware file system provides the raw memory pools, and this library supplies the traditional malloc interfaces on top of those pools.

The memory pool acting as a system heap replacement is created automatically at library initialization time. The user may control its location and size by setting the environment variables described in Environment, below. The allocated file space is reclaimed when the process terminates or in case of system crash.

Under normal usage, libvmmalloc will never print messages or intentionally cause the process to exit. The library uses pthreads(7) to be fully MT-safe, but never creates or destroys threads itself. The library does not make use of any signals, networking, and never calls select(2) or poll(2).

Environment

The VMMALLOC_POOL_DIR and VMMALLOC_POOL_SIZE environment variables must be set for libvmmalloc to work properly. If either of them is not specified, or if their values are not valid, the library prints an appropriate error message and terminates the process. Any other environment variables are optional.

Specifies a path to the directory where the memory pool file should be created. The directory must exist and be writable.

Defines the desired size (in bytes) of the memory pool file. It must be not less than the minimum allowed size VMMALLOC_MIN_POOL as defined in <libvmmalloc.h>.

NOTE: Due to the fact the library adds some metadata to the memory pool, the amount of actual usable space is typically less than the size of the memory pool file.

VMMALLOC_FORK controls the behavior of libvmmalloc in case of fork(3), and can be set to the following values:

This is the default option if VMMALLOC_FORK is not set.

Environment variables used for debugging are described in Debugging, below.

Caveats

libvmmalloc relies on the library destructor being called from the main thread. For this reason, all functions that might trigger destruction (e.g. dlclose(3)) should be called in the main thread. Otherwise some of the resources associated with that thread might not be cleaned up properly.

Debugging

Two versions of libvmmalloc are typically available on a development system. The normal version is optimized for performance. That version skips checks that impact performance and never logs any trace information or performs any run-time assertions. A second version, accessed when using libraries from /usr/lib/pmdk_debug, contains run-time assertions and trace points. The typical way to access the debug version is to set the LD_LIBRARY_PATH environment variable to /usr/lib/pmdk_debug or /usr/lib64/pmdk_debug, as appropriate. Debugging output is controlled using the following environment variables. These variables have no effect on the non-debug version of the library.

The value of VMMALLOC_LOG_LEVEL enables trace points in the debug version of the library, as follows:

Unless VMMALLOC_LOG_FILE is set, debugging output is written to stderr.

Specifies the name of a file where all logging information should be written. If the last character in the name is "-", the PID of the current process will be appended to the file name when the log file is created. If VMMALLOC_LOG_FILE is not set, output is written to stderr.

Setting VMMALLOC_LOG_STATS to 1 enables logging human-readable summary statistics at program termination.

Notes

Unlike the normal malloc(3), which asks the system for additional memory when it runs out, libvmmalloc allocates the size it is told to and never attempts to grow or shrink that memory pool.

Bugs

libvmmalloc may not work properly with programs that perform fork(2) and do not call exec(3) immediately afterwards. See Environment for more details about experimental fork(2) support.

If logging is enabled in the debug version of the library and the process performs fork(2), no new log file is created for the child process, even if the configured log file name ends with "-". All logging information from the child process will be written to the log file owned by the parent process, which may lead to corruption or partial loss of log data.

Malloc hooks (see malloc_hook(3)), are not supported when using libvmmalloc.

Acknowledgements

libvmmalloc depends on jemalloc, written by Jason Evans, to do the heavy lifting of managing dynamic memory allocation. See: <http://www.canonware.com/jemalloc>

See Also

fork(2), dlclose(3), exec(3), malloc(3), malloc_usable_size(3), posix_memalign(3), libpmem(7), libvmem(7) and <http://pmem.io>

On Linux:

jemalloc(3), malloc_hook(3), pthreads(7), ld.so(8)

On FreeBSD:

ld.so(1), pthread(3)

Info

2018-03-13 PMDK - vmmalloc API version 1.1 PMDK Programmer's Manual