.\" Automatically generated by Pandoc 2.5 .\" .TH "LIBVMEM" "7" "2019-11-08" "VMEM - vmem API version 1.1" "VMEM Programmer's Manual" .hy .\" Copyright 2014-2019, Intel Corporation .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" .\" * Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in .\" the documentation and/or other materials provided with the .\" distribution. .\" .\" * Neither the name of the copyright holder nor the names of its .\" contributors may be used to endorse or promote products derived .\" from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS .\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR .\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT .\" OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT .\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE .\" OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .SH NAME .PP \f[B]libvmem\f[R] \- volatile memory allocation library .SH SYNOPSIS .IP .nf \f[C] #include cc ... \-lvmem \f[R] .fi .SS Managing overall library behavior: .IP .nf \f[C] const char *vmem_check_version( unsigned major_required, unsigned minor_required); void vmem_set_funcs( void *(*malloc_func)(size_t size), void (*free_func)(void *ptr), void *(*realloc_func)(void *ptr, size_t size), char *(*strdup_func)(const char *s), void (*print_func)(const char *s)); \f[R] .fi .SS Error handling: .IP .nf \f[C] const char *vmem_errormsg(void); \f[R] .fi .SS Other library functions: .PP A description of other \f[B]libvmem\f[R] functions can be found on the following manual pages: .IP \[bu] 2 memory pool management: \f[B]vmem_create\f[R](3) .IP \[bu] 2 memory allocation related functions: \f[B]vmem_malloc\f[R](3) .SH DESCRIPTION .PP \f[B]libvmem\f[R] provides common \f[I]malloc\f[R]\-like interfaces to memory pools built on memory\-mapped files. These interfaces are for traditional \f[B]volatile\f[R] memory allocation but, unlike the functions described in \f[B]malloc\f[R](3), the memory managed by \f[B]libvmem\f[R] may have different attributes, depending on the file system containing the memory\-mapped files. .PP It is recommended that new code uses \f[B]memkind\f[R](3) instead of \f[B]libvmem\f[R], as this library is no longer actively developed and lacks certain features of \f[B]memkind\f[R] such as NUMA awareness. Nevertheless, it is mature, and is expected to be maintained for foreseable future. .PP \f[B]libvmem\f[R] uses the \f[B]mmap\f[R](2) system call to create a pool of volatile memory. The library is most useful when used with \f[I]Direct Access\f[R] 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 more familiar \f[I]malloc\f[R]\-like interfaces on top of those pools. .PP Under normal usage, \f[B]libvmem\f[R] will never print messages or intentionally cause the process to exit. Exceptions to this are prints caused by calls to \f[B]vmem_stats_print\f[R](3), or by enabling debugging as described under \f[B]DEBUGGING AND ERROR HANDLING\f[R] below. The library uses \f[B]pthreads\f[R] 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 \f[B]select\f[R](2) or \f[B]poll\f[R](2). The system memory allocation routines like \f[B]malloc\f[R](3) and \f[B]free\f[R](3) are used by \f[B]libvmem\f[R] for managing a small amount of run\-time state, but applications are allowed to override these calls if necessary (see the description of \f[B]vmem_set_funcs\f[R]() below). .PP \f[B]libvmem\f[R] interfaces are grouped into three categories: those that manage memory pools, those providing the basic memory allocation functions, and those interfaces less commonly used for managing the overall library behavior. .SH MANAGING LIBRARY BEHAVIOR .PP The \f[B]vmem_check_version\f[R]() function is used to see if the installed \f[B]libvmem\f[R] supports the version of the library API required by an application. The easiest way to do this is for the application to supply the compile\-time version information, supplied by defines in \f[B]\f[R], like this: .IP .nf \f[C] reason = vmem_check_version(VMEM_MAJOR_VERSION, VMEM_MINOR_VERSION); if (reason != NULL) { /* version check failed, reason string tells you why */ } \f[R] .fi .PP Any mismatch in the major version number is considered a failure, but a library with a newer minor version number will pass this check since increasing minor versions imply backwards compatibility. .PP An application can also check specifically for the existence of an interface by checking for the version where that interface was introduced. These versions are documented in this man page as follows: unless otherwise specified, all interfaces described here are available in version 1.0 of the library. Interfaces added after version 1.0 will contain the text \f[I]introduced in version x.y\f[R] in the section of this manual describing the feature. .PP When the version check is successful, \f[B]vmem_check_version\f[R]() returns NULL. Otherwise, \f[B]vmem_check_version\f[R]() returns a static string describing the reason for failing the version check. The returned string must not be modified or freed. .PP The \f[B]vmem_set_funcs\f[R]() function allows an application to override some interfaces used internally by \f[B]libvmem\f[R]. Passing NULL for any of the handlers will cause the \f[B]libvmem\f[R] default function to be used. The only functions in the malloc family used by the library are represented by the first four arguments to \f[B]vmem_set_funcs\f[R](). While the library does not make heavy use of the system malloc functions, it does allocate approximately 4\-8 kilobytes for each memory pool in use. The \f[I]print_func\f[R] function is called by \f[B]libvmem\f[R] when the \f[B]vmem_stats_print\f[R]() entry point is used, or when additional tracing is enabled in the debug version of the library as described in \f[B]DEBUGGING AND ERROR HANDLING\f[R], below. The default \f[I]print_func\f[R] used by the library prints to the file specified by the \f[B]VMEM_LOG_FILE\f[R] environment variable, or to \f[I]stderr\f[R] if that variable is not set. .SH CAVEATS .PP \f[B]libvmem\f[R] relies on the library destructor being called from the main thread. For this reason, all functions that might trigger destruction (e.g. \f[B]dlclose\f[R](3)) should be called in the main thread. Otherwise some of the resources associated with that thread might not be cleaned up properly. .SH DEBUGGING AND ERROR HANDLING .PP If an error is detected during the call to a \f[B]libvmem\f[R] function, the application may retrieve an error message describing the reason for the failure from \f[B]vmem_errormsg\f[R](). This function returns a pointer to a static buffer containing the last error message logged for the current thread. If \f[I]errno\f[R] was set, the error message may include a description of the corresponding error code as returned by \f[B]strerror\f[R](3). The error message buffer is thread\-local; errors encountered in one thread do not affect its value in other threads. The buffer is never cleared by any library function; its content is significant only when the return value of the immediately preceding call to a \f[B]libvmem\f[R] function indicated an error, or if \f[I]errno\f[R] was set. The application must not modify or free the error message string, but it may be modified by subsequent calls to other library functions. .PP Two versions of \f[B]libvmem\f[R] 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 \f[B]/usr/lib/vmem_debug\f[R], contains run\-time assertions and trace points. The typical way to access the debug version is to set the \f[B]LD_LIBRARY_PATH\f[R] environment variable to \f[B]/usr/lib/vmem_debug\f[R] or \f[B]/usr/lib64/vmem_debug\f[R], as appropriate. Debugging output is controlled using the following environment variables. These variables have no effect on the non\-debug version of the library. .IP \[bu] 2 \f[B]VMEM_LOG_LEVEL\f[R] .PP The value of \f[B]VMEM_LOG_LEVEL\f[R] enables trace points in the debug version of the library, as follows: .IP \[bu] 2 \f[B]0\f[R] \- Tracing is disabled. This is the default level when \f[B]VMEM_LOG_LEVEL\f[R] is not set. Only statistics are logged, and then only in response to a call to \f[B]vmem_stats_print\f[R](). .IP \[bu] 2 \f[B]1\f[R] \- Additional details on any errors detected are logged, in addition to returning the \f[I]errno\f[R]\-based errors as usual. .IP \[bu] 2 \f[B]2\f[R] \- A trace of basic operations is logged. .IP \[bu] 2 \f[B]3\f[R] \- Enables a very verbose amount of function call tracing in the library. .IP \[bu] 2 \f[B]4\f[R] \- Enables voluminous tracing information about all memory allocations and deallocations. .PP Unless \f[B]VMEM_LOG_FILE\f[R] is set, debugging output is written to \f[I]stderr\f[R]. .IP \[bu] 2 \f[B]VMEM_LOG_FILE\f[R] .PP Specifies the name of a file where all logging information should be written. If the last character in the name is \[lq]\-\[rq], the \f[I]PID\f[R] of the current process will be appended to the file name when the log file is created. If \f[B]VMEM_LOG_FILE\f[R] is not set, output is written to \f[I]stderr\f[R]. .SH EXAMPLE .PP The following example creates a memory pool, allocates some memory to contain the string \[lq]hello, world\[rq], and then frees that memory. .IP .nf \f[C] #include #include #include #include int main(int argc, char *argv[]) { VMEM *vmp; char *ptr; /* create minimum size pool of memory */ if ((vmp = vmem_create(\[dq]/pmem\-fs\[dq], VMEM_MIN_POOL)) == NULL) { perror(\[dq]vmem_create\[dq]); exit(1); } if ((ptr = vmem_malloc(vmp, 100)) == NULL) { perror(\[dq]vmem_malloc\[dq]); exit(1); } strcpy(ptr, \[dq]hello, world\[dq]); /* give the memory back */ vmem_free(vmp, ptr); /* ... */ vmem_delete(vmp); } \f[R] .fi .PP See for more examples using the \f[B]libvmem\f[R] API. .SH BUGS .PP Unlike the normal \f[B]malloc\f[R](3), which asks the system for additional memory when it runs out, \f[B]libvmem\f[R] allocates the size it is told to and never attempts to grow or shrink that memory pool. .SH ACKNOWLEDGEMENTS .PP \f[B]libvmem\f[R] depends on jemalloc, written by Jason Evans, to do the heavy lifting of managing dynamic memory allocation. See: .PP \f[B]libvmem\f[R] builds on the persistent memory programming model recommended by the SNIA NVM Programming Technical Work Group: .SH SEE ALSO .PP \f[B]mmap\f[R](2), \f[B]dlclose\f[R](3), \f[B]malloc\f[R](3), \f[B]strerror\f[R](3), \f[B]vmem_create\f[R](3), \f[B]vmem_malloc\f[R](3), and \f[B]\f[R] .PP On Linux: .PP \f[B]jemalloc\f[R](3), \f[B]pthreads\f[R](7) .PP On FreeBSD: .PP \f[B]pthread\f[R](3)