.\" Automatically generated by Pandoc 1.16.0.2
.\"
.TH "LIBPMEMCTO" "7" "2017-12-22" "PMDK - libpmemcto API version 1.0" "PMDK Programmer's Manual"
.hy
.\" Copyright 2014-2017, 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]libpmemcto\f[] \-\- close\-to\-open persistence
.SH SYNOPSIS
.IP
.nf
\f[C]
#include\ <libpmemcto.h>
cc\ ...\ \-lpmemcto\ \-lpmem
\f[]
.fi
.SS Library API versioning:
.IP
.nf
\f[C]
const\ char\ *pmemcto_check_version(
\ \ \ \ unsigned\ major_required,
\ \ \ \ unsigned\ minor_required);;
\f[]
.fi
.SS Managing library behavior:
.IP
.nf
\f[C]
void\ pmemcto_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[]
.fi
.SS Error handling:
.IP
.nf
\f[C]
const\ char\ *pmemcto_errormsg(void);
\f[]
.fi
.SS Other library functions:
.PP
A description of other \f[B]libpmemcto\f[] functions can be found on the
following manual pages:
.PP
\f[B]pmemcto_aligned_alloc\f[](3), \f[B]pmemcto_malloc\f[](3),
\f[B]pmemcto_malloc_usable_size\f[](3), \f[B]pmemcto_open\f[](3),
\f[B]pmemcto_set_root_pointer\f[](3), \f[B]pmemcto_stats_print\f[](3),
\f[B]pmemcto_strdup\f[](3), \f[B]pmemcto_wcsdup\f[](3)
.SH DESCRIPTION
.PP
\f[B]libpmemcto\f[] is a \f[I]persistent memory\f[] allocator with no
overhead imposed by run\-time flushing or transactional updates:
.IP \[bu] 2
It runs at traditional \f[B]volatile\f[] memory allocator speeds \-
there is no flushing or consistency check at run\-time.
.IP \[bu] 2
An overhead imposed only when program exits normally and have to flush
the file.
.IP \[bu] 2
The program flushes the pool contents when it exits, and then rebuilds
the pool on the next run.
.IP \[bu] 2
If the program crashes before flushing the file (or if flushing fails),
the pool is in an inconsistent state causing subsequent pool opening to
fail.
.PP
\f[B]libpmemcto\f[] provides common \f[I]malloc\-like\f[] interfaces to
persistent memory pools built on memory\-mapped files.
\f[B]libpmemcto\f[] uses the \f[B]mmap\f[](2) system call to create a
pool of persistent memory.
The library is intended for applications using \f[I]Direct Access\f[]
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\-like\f[] interfaces on top of those pools.
\f[B]libpmemcto\f[] is one of a collection of persistent memory
libraries available, the others are:
.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.
.IP \[bu] 2
\f[B]libpmem\f[](7), low\-level persistent memory support.
.PP
Under normal usage, \f[B]libpmemcto\f[] will never print messages or
intentionally cause the process to exit.
Exceptions to this are prints caused by calls to
\f[B]pmemcto_stats_print\f[](3), or by enabling debugging as described
under \f[B]DEBUGGING AND ERROR HANDLING\f[] below.
The library uses \f[B]pthreads\f[](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 \f[B]select\f[]() or \f[B]poll\f[]().
.PP
The system memory allocation routines like \f[B]malloc\f[]() and
\f[B]free\f[]() are used by \f[B]libpmemcto\f[] for managing a small
amount of run\-time state, but applications are allowed to override
these calls if necessary (see \f[B]pmemcto_set_funcs\f[]()).
.PP
This library builds on the low\-level pmem support provided by
\f[B]libpmem\f[](7).
.PP
To use close\-to\-open persistence supplied by \f[B]libpmemcto\f[], a
\f[I]memory pool\f[] is first created using the
\f[B]pmemcto_create\f[]() function described in
\f[B]pmemcto_open\f[](3).
The other \f[B]libpmemcto\f[] functions operate on the resulting block
memory pool using the opaque handle, of type \f[I]PMEMctopool*\f[], that
is returned by \f[B]pmemcto_create\f[]() or \f[B]pmemcto_open\f[]().
Internally, \f[B]libpmemcto\f[] will use either \f[B]pmem_persist\f[](3)
or \f[B]msync\f[](2) when it needs to flush changes, depending on
whether the memory pool appears to be persistent memory or a regular
file (see the \f[B]pmem_is_pmem\f[](3) function in \f[B]libpmem\f[](7)
for more information).
There is no need for applications to flush changes directly when using
the close\-to\-open persistence memory API provided by
\f[B]libpmemcto\f[].
.SH CAVEATS
.PP
\f[B]libpmemcto\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]pmemcto_check_version\f[]() function is used to determine
whether the installed \f[B]libpmemcto\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]<ibpmemcto.h>\f[], like this:
.IP
.nf
\f[C]
reason\ =\ pmemcto_check_version(PMEMCTO_MAJOR_VERSION,
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PMEMCTO_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]pmemcto_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]pmemcto_check_version\f[]() must not be
modified or freed.
.SH MANAGING LIBRARY BEHAVIOR
.PP
The \f[B]pmemcto_set_funcs\f[]() function allows an application to
override memory allocation calls used internally by \f[B]libpmemcto\f[].
Passing in NULL for any of the handlers will cause the
\f[B]libpmemcto\f[] default function to be used.
The library does not make heavy use of the system malloc functions, but
it does allocate approximately 4\-8 kilobytes for each memory pool in
use.
.SH DEBUGGING AND ERROR HANDLING
.PP
The \f[B]pmemcto_errormsg\f[]() 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]libpmemcto\f[] function indicated an error, or if
\f[I]errno\f[] 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]libpmemcto\f[] are typically available on a
development system.
The normal version, accessed when a program is linked using the
\f[B]\-lpmemcto\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.
If an error is detected in a call to \f[B]libpmemcto\f[], the error
message describing the failure may be retrieved with
\f[B]pmemcto_errormsg\f[]() as described above.
.PP
A second version of \f[B]libpmemcto\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
\f[B]LD_LIBRARY_PATH\f[] environment variable 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.
.IP \[bu] 2
\f[B]PMEMCTO_LOG_LEVEL\f[]
.PP
The value of \f[B]PMEMCTO_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]PMEMCTO_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]pmemcto_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]libpmemcto\f[] developers.
.PP
Unless \f[B]PMEMCTO_LOG_FILE\f[] is set, debugging output is written to
\f[I]stderr\f[].
.IP \[bu] 2
\f[B]PMEMCTO_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 "\-", 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]PMEMCTO_LOG_FILE\f[] is not set, the logging output is written
to \f[I]stderr\f[].
.PP
See also \f[B]libpmem\f[](7) for information on other environment
variables that may affect \f[B]libpmemcto\f[] behavior.
.SH EXAMPLE
.PP
The following example creates a memory pool, allocates some memory to
contain the string "hello, world", and then frees that memory.
.IP
.nf
\f[C]
#include\ <stdio.h>
#include\ <fcntl.h>
#include\ <errno.h>
#include\ <stdlib.h>
#include\ <unistd.h>
#include\ <string.h>
#include\ <libpmemcto.h>
/*\ size\ of\ the\ pmemcto\ pool\ \-\-\ 1\ GB\ */
#define\ POOL_SIZE\ ((size_t)(1\ <<\ 30))
/*\ name\ of\ our\ layout\ in\ the\ pool\ */
#define\ LAYOUT_NAME\ "example_layout"
struct\ root\ {
\ \ \ \ char\ *str;
\ \ \ \ char\ *data;
};
int
main(int\ argc,\ char\ *argv[])
{
\ \ \ \ const\ char\ path[]\ =\ "/pmem\-fs/myfile";
\ \ \ \ PMEMctopool\ *pcp;
\ \ \ \ /*\ create\ the\ pmemcto\ pool\ or\ open\ it\ if\ already\ exists\ */
\ \ \ \ pcp\ =\ pmemcto_create(path,\ LAYOUT_NAME,\ POOL_SIZE,\ 0666);
\ \ \ \ if\ (pcp\ ==\ NULL)
\ \ \ \ \ \ \ \ pcp\ =\ pmemcto_open(path,\ LAYOUT_NAME);
\ \ \ \ if\ (pcp\ ==\ NULL)\ {
\ \ \ \ \ \ \ \ perror(path);
\ \ \ \ \ \ \ \ exit(1);
\ \ \ \ }
\ \ \ \ /*\ get\ the\ root\ object\ pointer\ */
\ \ \ \ struct\ root\ *rootp\ =\ pmemcto_get_root_pointer(pcp);
\ \ \ \ if\ (rootp\ ==\ NULL)\ {
\ \ \ \ \ \ \ \ /*\ allocate\ root\ object\ */
\ \ \ \ \ \ \ \ rootp\ =\ pmemcto_malloc(pcp,\ sizeof(*rootp));
\ \ \ \ \ \ \ \ if\ (rootp\ ==\ NULL)\ {
\ \ \ \ \ \ \ \ \ \ \ \ perror(pmemcto_errormsg());
\ \ \ \ \ \ \ \ \ \ \ \ exit(1);
\ \ \ \ \ \ \ \ }
\ \ \ \ \ \ \ \ /*\ save\ the\ root\ object\ pointer\ */
\ \ \ \ \ \ \ \ pmemcto_set_root_pointer(pcp,\ rootp);
\ \ \ \ \ \ \ \ rootp\->str\ =\ pmemcto_strdup(pcp,\ "Hello\ World!");
\ \ \ \ \ \ \ \ rootp\->data\ =\ NULL;
\ \ \ \ }
\ \ \ \ /*\ ...\ */
\ \ \ \ pmemcto_close(pcp);
}
\f[]
.fi
.PP
See <http://pmem.io/pmdk/libpmemcto> for more examples using the
\f[B]libpmemcto\f[] API.
.SH BUGS
.PP
Unlike \f[B]libpmemobj\f[](3), data replication is not supported in
\f[B]libpmemcto\f[].
Thus, it is not allowed to specify replica sections in pool set files.
.SH NOTES
.PP
Unlike the normal \f[B]malloc\f[](), which asks the system for
additional memory when it runs out, \f[B]libpmemcto\f[] allocates the
size it is told to and never attempts to grow or shrink that memory
pool.
.SH AVAILABILITY
.PP
\f[B]libpmemcto\f[] is part of the PMDK since version 1.4 and is
available from <https://github.com/pmem/pmdk>
.SH ACKNOWLEDGEMENTS
.PP
\f[B]libpmemcto\f[] depends on jemalloc, written by Jason Evans, to do
the heavy lifting of managing dynamic memory allocation.
See: <http://www.canonware.com/jemalloc>
.PP
\f[B]libpmemcto\f[] builds on the persistent memory programming model
recommended by the SNIA NVM Programming Technical Work Group:
<http://snia.org/nvmp>
.SH SEE ALSO
.PP
\f[B]ndctl\-create\-namespace\f[](1), \f[B]dlclose\f[](2),
\f[B]mmap\f[](2), \f[B]jemalloc\f[](3), \f[B]malloc\f[](3),
\f[B]pmemcto_aligned_alloc\f[](3), \f[B]pmemcto_errormsg\f[](3),
\f[B]pmemcto_malloc\f[](3), \f[B]pmemcto_malloc_usable_size\f[](3),
\f[B]pmemcto_open\f[](3), \f[B]pmemcto_set_root_pointer\f[](3),
\f[B]pmemcto_stats_print\f[](3), \f[B]pmemcto_strdup\f[](3),
\f[B]pmemcto_wcsdup\f[](3), \f[B]libpmem\f[](7), \f[B]libpmemblk\f[](7),
\f[B]libpmemlog\f[](7), \f[B]libpmemobj\f[](7) and
\f[B]<http://pmem.io>\f[]