.\" Automatically generated by Pandoc 2.2.1 .\" .TH "LIBPMEM" "7" "2020-11-05" "PMDK - pmem API version 1.1" "PMDK Programmer's Manual" .hy .\" SPDX-License-Identifier: BSD-3-Clause .\" Copyright 2016-2018, Intel Corporation .SH NAME .PP \f[B]libpmem\f[] \- persistent memory support library .SH SYNOPSIS .IP .nf \f[C] #include\ cc\ ...\ \-lpmem \f[] .fi .SS Library API versioning: .IP .nf \f[C] const\ char\ *pmem_check_version( \ \ \ \ unsigned\ major_required, \ \ \ \ unsigned\ minor_required); \f[] .fi .SS Error handling: .IP .nf \f[C] const\ char\ *pmem_errormsg(void); \f[] .fi .SS Other library functions: .PP A description of other \f[B]libpmem\f[] functions can be found on the following manual pages: .IP \[bu] 2 most commonly used functions: \f[B]pmem_is_pmem\f[](3) .IP \[bu] 2 partial flushing operations: \f[B]pmem_flush\f[](3) .IP \[bu] 2 copying to persistent memory: \f[B]pmem_memmove_persist\f[](3) .SH DESCRIPTION .PP \f[B]libpmem\f[] provides low\-level \f[I]persistent memory\f[] (pmem) support for applications using direct access storage (DAX), which is storage that supports load/store access without paging blocks from a block storage device. Some types of \f[I]non\-volatile memory DIMMs\f[] (NVDIMMs) provide this type of byte addressable access to storage. A \f[I]persistent memory aware file system\f[] is typically used to expose the direct access to applications. Memory mapping a file from this type of file system results in the load/store, non\-paged access to pmem. .PP This library is for applications that use persistent memory directly, without the help of any library\-supplied transactions or memory allocation. Higher\-level libraries that build on \f[B]libpmem\f[] are available and are recommended for most applications, see: .IP \[bu] 2 \f[B]libpmemobj\f[](7), a general use persistent memory API, providing memory allocation and transactional operations on variable\-sized objects. .IP \[bu] 2 \f[B]libpmemblk\f[](7), providing pmem\-resident arrays of fixed\-sized blocks with atomic updates. .IP \[bu] 2 \f[B]libpmemlog\f[](7), providing a pmem\-resident log file. .PP Under normal usage, \f[B]libpmem\f[] will never print messages or intentionally cause the process to exit. The only exception to this is the debugging information, when enabled, as described under \f[B]DEBUGGING AND ERROR HANDLING\f[] below. .SH CAVEATS .PP \f[B]libpmem\f[] 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[](3)) should be called in the main thread. Otherwise some of the resources associated with that thread might not be cleaned up properly. .SH LIBRARY API VERSIONING .PP This section describes how the library API is versioned, allowing applications to work with an evolving API. .PP The \f[B]pmem_check_version\f[]() function is used to determine whether the installed \f[B]libpmem\f[] 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[], like this: .IP .nf \f[C] reason\ =\ pmem_check_version(PMEM_MAJOR_VERSION, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PMEM_MINOR_VERSION); if\ (reason\ !=\ NULL)\ { \ \ \ \ /*\ version\ check\ failed,\ reason\ string\ tells\ you\ why\ */ } \f[] .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[] in the section of this manual describing the feature. .PP When the version check performed by \f[B]pmem_check_version\f[]() is successful, the return value is NULL. Otherwise the return value is a static string describing the reason for failing the version check. The string returned by \f[B]pmem_check_version\f[]() must not be modified or freed. .SH ENVIRONMENT .PP \f[B]libpmem\f[] can change its default behavior based on the following environment variables. These are largely intended for testing and are not normally required. .IP \[bu] 2 \f[B]PMEM_IS_PMEM_FORCE\f[]=\f[I]val\f[] .PP If \f[I]val\f[] is 0 (zero), then \f[B]pmem_is_pmem\f[](3) will always return false. Setting \f[I]val\f[] to 1 causes \f[B]pmem_is_pmem\f[](3) to always return true. This variable is mostly used for testing but can be used to force pmem behavior on a system where a range of pmem is not detectable as pmem for some reason. .RS .PP NOTE: Unlike the other variables, the value of \f[B]PMEM_IS_PMEM_FORCE\f[] is not queried (and cached) at library initialization time, but on the first call to \f[B]pmem_is_pmem\f[](3). This means that in case of \f[B]libpmemlog\f[](7), \f[B]libpmemblk\f[](7), and \f[B]libpmemobj\f[](7), \f[B]PMEM_IS_PMEM_FORCE\f[] may still be set or modified by the program until the first attempt to create or open the persistent memory pool. .RE .IP \[bu] 2 \f[B]PMEM_NO_CLWB\f[]=1 .PP Setting this environment variable to 1 forces \f[B]libpmem\f[] to never issue the \f[B]CLWB\f[] instruction on Intel hardware, falling back to other cache flush instructions instead (\f[B]CLFLUSHOPT\f[] or \f[B]CLFLUSH\f[] on Intel hardware). Without this environment variable, \f[B]libpmem\f[] will always use the \f[B]CLWB\f[] instruction for flushing processor caches on platforms that support the instruction. This variable is intended for use during library testing but may be required for some rare cases where using \f[B]CLWB\f[] has a negative impact on performance. .IP \[bu] 2 \f[B]PMEM_NO_CLFLUSHOPT\f[]=1 .PP Setting this environment variable to 1 forces \f[B]libpmem\f[] to never issue the \f[B]CLFLUSHOPT\f[] instruction on Intel hardware, falling back to the \f[B]CLFLUSH\f[] instructions instead. Without this environment variable, \f[B]libpmem\f[] will always use the \f[B]CLFLUSHOPT\f[] instruction for flushing processor caches on platforms that support the instruction, but where \f[B]CLWB\f[] is not available. This variable is intended for use during library testing. .IP \[bu] 2 \f[B]PMEM_NO_FLUSH\f[]=1 .PP Setting this environment variable to 1 forces most \f[B]libpmem\f[] functions to never issue any of \f[B]CLFLUSH\f[], \f[B]CLFLUSHOPT\f[] or \f[B]CLWB\f[] instructions on Intel hardware. The only exceptions are \f[B]pmem_deep_flush\f[](3) and \f[B]pmem_deep_persist\f[](3) functions. .IP \[bu] 2 \f[B]PMEM_NO_FLUSH\f[]=0 .PP Setting this environment variable to 0 forces to always flush CPU caches using one of \f[B]CLFLUSH\f[], \f[B]CLFLUSHOPT\f[] or \f[B]CLWB\f[] instructions even if \f[B]pmem_has_auto_flush\f[](3) function returns true and the platform supports flushing the processor caches on power loss or system crash. .IP \[bu] 2 \f[B]PMEM_NO_MOVNT\f[]=1 .PP Setting this environment variable to 1 forces \f[B]libpmem\f[] to never use the \f[I]non\-temporal\f[] move instructions on Intel hardware. Without this environment variable, \f[B]libpmem\f[] will use the non\-temporal instructions for copying larger ranges to persistent memory on platforms that support the instructions. This variable is intended for use during library testing. .IP \[bu] 2 \f[B]PMEM_MOVNT_THRESHOLD\f[]=\f[I]val\f[] .PP This environment variable allows overriding the minimum length of the \f[B]pmem_memmove_persist\f[](3) operations, for which \f[B]libpmem\f[] uses \f[I]non\-temporal\f[] move instructions. Setting this environment variable to 0 forces \f[B]libpmem\f[] to always use the \f[I]non\-temporal\f[] move instructions if available. It has no effect if \f[B]PMEM_NO_MOVNT\f[] is set to 1. This variable is intended for use during library testing. .IP \[bu] 2 \f[B]PMEM_MMAP_HINT\f[]=\f[I]val\f[] .PP This environment variable allows overriding the hint address used by \f[B]pmem_map_file\f[](). If set, it also disables mapping address randomization. This variable is intended for use during library testing and debugging. Setting it to some fairly large value (i.e.\ 0x10000000000) will very likely result in mapping the file at the specified address (if not used) or at the first unused region above given address, without adding any random offset. When debugging, this makes it easier to calculate the actual address of the persistent memory block, based on its offset in the file. In case of \f[B]libpmemobj\f[] it simplifies conversion of a persistent object identifier (OID) into a direct pointer to the object. .RS .PP NOTE: \f[B]Setting this environment variable affects all the PMDK libraries,\f[] disabling mapping address randomization and causing the specified address to be used as a hint about where to place the mapping. .RE .SH DEBUGGING AND ERROR HANDLING .PP If an error is detected during the call to a \f[B]libpmem\f[] function, the application may retrieve an error message describing the reason of the failure from \f[B]pmem_errormsg\f[](). This function returns a pointer to a static buffer containing the last error message logged for the current thread. If \f[I]errno\f[] was set, the error message may include a description of the corresponding error code as returned by \f[B]strerror\f[](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]libpmem\f[] function indicated an error. The application must not modify or free the error message string. Subsequent calls to other library functions may modify the previous message. .PP Two versions of \f[B]libpmem\f[] are typically available on a development system. The normal version, accessed when a program is linked using the \f[B]\-lpmem\f[] option, is optimized for performance. That version skips checks that impact performance and never logs any trace information or performs any run\-time assertions. .PP A second version of \f[B]libpmem\f[], accessed when a program uses the libraries under \f[B]/usr/lib/pmdk_debug\f[], contains run\-time assertions and trace points. The typical way to access the debug version is to set the environment variable \f[B]LD_LIBRARY_PATH\f[] to \f[B]/usr/lib/pmdk_debug\f[] or \f[B]/usr/lib64/pmdk_debug\f[], as appropriate. Debugging output is controlled using the following environment variables. These variables have no effect on the non\-debug version of the library. .RS .PP NOTE: On Debian/Ubuntu systems, this extra debug version of the library is shipped in the respective \f[B]\-debug\f[] Debian package and placed in the \f[B]/usr/lib/$ARCH/pmdk_dbg/\f[] directory. .RE .IP \[bu] 2 \f[B]PMEM_LOG_LEVEL\f[] .PP The value of \f[B]PMEM_LOG_LEVEL\f[] enables trace points in the debug version of the library, as follows: .IP \[bu] 2 \f[B]0\f[] \- This is the default level when \f[B]PMEM_LOG_LEVEL\f[] is not set. No log messages are emitted at this level. .IP \[bu] 2 \f[B]1\f[] \- Additional details on any errors detected are logged, in addition to returning the \f[I]errno\f[]\-based errors as usual. The same information may be retrieved using \f[B]pmem_errormsg\f[](). .IP \[bu] 2 \f[B]2\f[] \- A trace of basic operations is logged. .IP \[bu] 2 \f[B]3\f[] \- Enables a very verbose amount of function call tracing in the library. .IP \[bu] 2 \f[B]4\f[] \- Enables voluminous and fairly obscure tracing information that is likely only useful to the \f[B]libpmem\f[] developers. .PP Unless \f[B]PMEM_LOG_FILE\f[] is set, debugging output is written to \f[I]stderr\f[]. .IP \[bu] 2 \f[B]PMEM_LOG_FILE\f[] .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[] of the current process will be appended to the file name when the log file is created. If \f[B]PMEM_LOG_FILE\f[] is not set, output is written to \f[I]stderr\f[]. .SH EXAMPLE .PP The following example uses \f[B]libpmem\f[] to flush changes made to raw, memory\-mapped persistent memory. .RS .PP WARNING: There is nothing transactional about the \f[B]pmem_persist\f[](3) or \f[B]pmem_msync\f[](3) calls in this example. Interrupting the program may result in a partial write to pmem. Use a transactional library such as \f[B]libpmemobj\f[](7) to avoid torn updates. .RE .IP .nf \f[C] #include\ #include\ #include\ #include\ #include\ #include\ #include\ #include\ #include\ /*\ using\ 4k\ of\ pmem\ for\ this\ example\ */ #define\ PMEM_LEN\ 4096 #define\ PATH\ "/pmem\-fs/myfile" int main(int\ argc,\ char\ *argv[]) { \ \ \ \ char\ *pmemaddr; \ \ \ \ size_t\ mapped_len; \ \ \ \ int\ is_pmem; \ \ \ \ /*\ create\ a\ pmem\ file\ and\ memory\ map\ it\ */ \ \ \ \ if\ ((pmemaddr\ =\ pmem_map_file(PATH,\ PMEM_LEN,\ PMEM_FILE_CREATE, \ \ \ \ \ \ \ \ \ \ \ \ 0666,\ &mapped_len,\ &is_pmem))\ ==\ NULL)\ { \ \ \ \ \ \ \ \ perror("pmem_map_file"); \ \ \ \ \ \ \ \ exit(1); \ \ \ \ } \ \ \ \ /*\ store\ a\ string\ to\ the\ persistent\ memory\ */ \ \ \ \ strcpy(pmemaddr,\ "hello,\ persistent\ memory"); \ \ \ \ /*\ flush\ above\ strcpy\ to\ persistence\ */ \ \ \ \ if\ (is_pmem) \ \ \ \ \ \ \ \ pmem_persist(pmemaddr,\ mapped_len); \ \ \ \ else \ \ \ \ \ \ \ \ pmem_msync(pmemaddr,\ mapped_len); \ \ \ \ /* \ \ \ \ \ *\ Delete\ the\ mappings.\ The\ region\ is\ also \ \ \ \ \ *\ automatically\ unmapped\ when\ the\ process\ is \ \ \ \ \ *\ terminated. \ \ \ \ \ */ \ \ \ \ pmem_unmap(pmemaddr,\ mapped_len); } \f[] .fi .PP See for more examples using the \f[B]libpmem\f[] API. .SH ACKNOWLEDGEMENTS .PP \f[B]libpmem\f[] builds on the persistent memory programming model recommended by the SNIA NVM Programming Technical Work Group: .SH SEE ALSO .PP \f[B]dlclose\f[](3), \f[B]pmem_flush\f[](3), \f[B]pmem_is_pmem\f[](3), \f[B]pmem_memmove_persist\f[](3), \f[B]pmem_msync\f[](3), \f[B]pmem_persist\f[](3), \f[B]strerror\f[](3), \f[B]libpmemblk\f[](7), \f[B]libpmemlog\f[](7), \f[B]libpmemobj\f[](7) and \f[B]\f[]