1LIBPMEMLOG(7) PMDK Programmer's Manual LIBPMEMLOG(7)
2
3
4
6 libpmemlog -- persistent memory resident log file
7
9 #include <libpmemlog.h>
10 cc ... -lpmemlog -lpmem
11
12 Library API versioning:
13 const char *pmemlog_check_version(
14 unsigned major_required,
15 unsigned minor_required);
16
17 Managing library behavior:
18 void pmemlog_set_funcs(
19 void *(*malloc_func)(size_t size),
20 void (*free_func)(void *ptr),
21 void *(*realloc_func)(void *ptr, size_t size),
22 char *(*strdup_func)(const char *s));
23
24 Error handling:
25 int pmemlog_check(const char *path);
26
27 Other library functions:
28 A description of other libpmemlog functions can be found on the follow‐
29 ing manual pages:
30
31 pmemlog_create(3), pmemlog_nbyte(3), pmemlog_append(3), pmemlog_tell(3)
32
34 libpmemlog provides a log file in persistent memory (pmem) such that
35 additions to the log are appended atomically. This library is intended
36 for applications using direct access storage (DAX), which is storage
37 that supports load/store access without paging blocks from a block
38 storage device. Some types of non-volatile memory DIMMs (NVDIMMs) pro‐
39 vide this type of byte addressable access to storage. A persistent
40 memory aware file system is typically used to expose the direct access
41 to applications. Memory mapping a file from this type of file system
42 results in the load/store, non-paged access to pmem. libpmemlog builds
43 on thistype of memory mapped file.
44
45 This library is for applications that need a persistent log file updat‐
46 ed atomically (the updates cannot be torn by program interruption such
47 as power failures). This library builds on the low-level pmem support
48 provided by libpmem(7), handling the transactional update of the log,
49 flushing to persistence, and recovery for the application.
50
51 libpmemlog is one of a collection of persistent memory libraries avail‐
52 able. The others are:
53
54 · libpmemobj(7), a general use persistent memory API, providing memory
55 allocation and transactional operations on variable-sized objects.
56
57 · libpmemblk(7), providing pmem-resident arrays of fixed-sized blocks
58 with atomic updates.
59
60 · libpmemcto(7), providing close-to-open persistence.
61
62 · libpmem(7), low-level persistent memory support.
63
64 Under normal usage, libpmemlog will never print messages or intention‐
65 ally cause the process to exit. The only exception to this is the de‐
66 bugging information, when enabled, as described under DEBUGGING AND ER‐
67 ROR HANDLING below.
68
69 To use the pmem-resident log file provided by libpmemlog, a memory pool
70 is first created. This is done with the pmemlog_create(3) function.
71 The other functions mentioned above in SYNOPSIS section then operate on
72 the resulting log memory pool.
73
74 Once created, the memory pool is represented by an opaque handle, of
75 type PMEMlogpool*, which is passed to most of the other functions from
76 libpmemlog. Internally, libpmemlog will use either pmem_persist(3) or
77 msync(2) when it needs to flush changes, depending on whether the memo‐
78 ry pool appears to be persistent memory or a regular file (see the
79 pmem_is_pmem(3) function in libpmem(7) for more information). There is
80 no need for applications to flush changes directly when using the log
81 memory API provided by libpmemlog.
82
84 libpmemlog relies on the library destructor being called from the main
85 thread. For this reason, all functions that might trigger destruction
86 (e.g. dlclose(3)) should be called in the main thread. Otherwise some
87 of the resources associated with that thread might not be cleaned up
88 properly.
89
91 This section describes how the library API is versioned, allowing ap‐
92 plications to work with an evolving API.
93
94 The pmemlog_check_version() function is used to determine whether the
95 installed libpmemlog supports the version of the library API required
96 by an application. The easiest way to do this is for the application
97 to supply the compile-time version information provided by defines in
98 <libpmemlog.h>, like this:
99
100 reason = pmemlog_check_version(PMEMLOG_MAJOR_VERSION,
101 PMEMLOG_MINOR_VERSION);
102 if (reason != NULL) {
103 /* version check failed, reason string tells you why */
104 }
105
106 Any mismatch in the major version number is considered a failure, but a
107 library with a newer minor version number will pass this check since
108 increasing minor versions imply backwards compatibility.
109
110 An application can also check specifically for the existence of an in‐
111 terface by checking for the version where that interface was intro‐
112 duced. These versions are documented in this man page as follows: un‐
113 less otherwise specified, all interfaces described here are available
114 in version 1.0 of the library. Interfaces added after version 1.0 will
115 contain the text introduced in version x.y in the section of this manu‐
116 al describing the feature.
117
118 On success, pmemlog_check_version() returns NULL. Otherwise, the re‐
119 turn value is a static string describing the reason the version check
120 failed. The string returned by pmemlog_check_version() must not be
121 modified or freed.
122
124 The pmemlog_set_funcs() function allows an application to override mem‐
125 ory allocation calls used internally by libpmemlog. Passing in NULL
126 for any of the handlers will cause the libpmemlog default function to
127 be used. The library does not make heavy use of the system malloc
128 functions, but it does allocate approximately 4-8 kilobytes for each
129 memory pool in use.
130
132 The pmemlog_errormsg() function returns a pointer to a static buffer
133 containing the last error message logged for the current thread. If
134 errno was set, the error message may include a description of the cor‐
135 responding error code as returned by strerror(3). The error message
136 buffer is thread-local; errors encountered in one thread do not affect
137 its value in other threads. The buffer is never cleared by any library
138 function; its content is significant only when the return value of the
139 immediately preceding call to a libpmemlog function indicated an error,
140 or if errno was set. The application must not modify or free the error
141 message string, but it may be modified by subsequent calls to other li‐
142 brary functions.
143
144 Two versions of libpmemlog are typically available on a development
145 system. The normal version, accessed when a program is linked using
146 the -lpmemlog option, is optimized for performance. That version skips
147 checks that impact performance and never logs any trace information or
148 performs any run-time assertions.
149
150 A second version of libpmemlog, accessed when a program uses the li‐
151 braries under /usr/lib/pmdk_debug, contains run-time assertions and
152 trace points. The typical way to access the debug version is to set
153 the environment variable LD_LIBRARY_PATH to /usr/lib/pmdk_debug or
154 /usr/lib64/pmdk_debug, as appropriate. Debugging output is controlled
155 using the following environment variables. These variables have no ef‐
156 fect on the non-debug version of the library.
157
158 · PMEMLOG_LOG_LEVEL
159
160 The value of PMEMLOG_LOG_LEVEL enables trace points in the debug ver‐
161 sion of the library, as follows:
162
163 · 0 - This is the default level when PMEMLOG_LOG_LEVEL is not set. No
164 log messages are emitted at this level.
165
166 · 1 - Additional details on any errors detected are logged, in addition
167 to returning the errno-based errors as usual. The same information
168 may be retrieved using pmemlog_errormsg().
169
170 · 2 - A trace of basic operations is logged.
171
172 · 3 - Enables a very verbose amount of function call tracing in the li‐
173 brary.
174
175 · 4 - Enables voluminous and fairly obscure tracing information that is
176 likely only useful to the libpmemlog developers.
177
178 Unless PMEMLOG_LOG_FILE is set, debugging output is written to stderr.
179
180 · PMEMLOG_LOG_FILE
181
182 Specifies the name of a file name where all logging information should
183 be written. If the last character in the name is "-", the PID of the
184 current process will be appended to the file name when the log file is
185 created. If PMEMLOG_LOG_FILE is not set, logging output is written to
186 stderr.
187
188 See also libpmem(7) for information about other environment variables
189 affecting libpmemlog behavior.
190
192 The following example illustrates how the libpmemlog API is used.
193
194 #include <stdio.h>
195 #include <fcntl.h>
196 #include <errno.h>
197 #include <stdlib.h>
198 #include <unistd.h>
199 #include <string.h>
200 #include <libpmemlog.h>
201
202 /* size of the pmemlog pool -- 1 GB */
203 #define POOL_SIZE ((size_t)(1 << 30))
204
205 /*
206 * printit -- log processing callback for use with pmemlog_walk()
207 */
208 int
209 printit(const void *buf, size_t len, void *arg)
210 {
211 fwrite(buf, len, 1, stdout);
212 return 0;
213 }
214
215 int
216 main(int argc, char *argv[])
217 {
218 const char path[] = "/pmem-fs/myfile";
219 PMEMlogpool *plp;
220 size_t nbyte;
221 char *str;
222
223 /* create the pmemlog pool or open it if it already exists */
224 plp = pmemlog_create(path, POOL_SIZE, 0666);
225
226 if (plp == NULL)
227 plp = pmemlog_open(path);
228
229 if (plp == NULL) {
230 perror(path);
231 exit(1);
232 }
233
234 /* how many bytes does the log hold? */
235 nbyte = pmemlog_nbyte(plp);
236 printf("log holds %zu bytes", nbyte);
237
238 /* append to the log... */
239 str = "This is the first string appended";
240 if (pmemlog_append(plp, str, strlen(str)) < 0) {
241 perror("pmemlog_append");
242 exit(1);
243 }
244 str = "This is the second string appended";
245 if (pmemlog_append(plp, str, strlen(str)) < 0) {
246 perror("pmemlog_append");
247 exit(1);
248 }
249
250 /* print the log contents */
251 printf("log contains:");
252 pmemlog_walk(plp, 0, printit, NULL);
253
254 pmemlog_close(plp);
255 }
256
257 See <http://pmem.io/pmdk/libpmemlog> for more examples using the libp‐
258 memlog API.
259
261 Unlike libpmemobj(7), data replication is not supported in libpmemlog.
262 Thus, specifying replica sections in pool set files is not allowed.
263
265 libpmemlog builds on the persistent memory programming model recommend‐
266 ed by the SNIA NVM Programming Technical Work Group:
267 <http://snia.org/nvmp>
268
270 msync(2), pmemlog_append(3), pmemlog_create(3), pmemlog_nbyte(3), pmem‐
271 log_tell(3), strerror(3), libpmem(7), libpmemblk(7), libpmemcto(7),
272 libpmemobj(7) and <http://pmem.io>
273
274
275
276PMDK - pmemlog API version 1.1 2018-03-13 LIBPMEMLOG(7)