PMDK libpmemlog 例程1 持久化log记录

版权声明: https://blog.csdn.net/qccz123456/article/details/81908424

libpmemlog用于持久化记录log文件,采用append的方法记录。
libpmemlog API说明:http://pmem.io/pmdk/manpages/linux/master/libpmemlog/libpmemlog.7.html

Key:
pmemlog_create()  // 创建持久化的log内存
pmemlog_open()  // 打开已创建的log内存
pmemlog_nbyte()  // 获得某log内存内可以添加多少位的数据,而非多少条记录
pmemlog_append()  // 向log内存中append方式写入log记录
pmemlog_walk()  // 查看log内存中的数据
pmemlog_close()  // 关闭log内存

例子:

#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <libpmemlog.h>


#define POOL_SIZE ((size_t)(1 << 30))       /* size of the pmemlog pool -- 1 GB */

// log processing callback for use with pmemlog_walk()
int printit(const void *buf, size_t len, void *arg)
{
    fwrite(buf, len, 1, stdout);
    return 0;
}

int main(int argc, char *argv[])
{
    const char path[] = "/root/hostname/pmemlog.001";

    /* create the pmemlog pool or open it if it already exists */
    PMEMlogpool *plp = pmemlog_create(path, POOL_SIZE, 0666);

    if (plp == NULL)
        plp = pmemlog_open(path);

    if (plp == NULL) {
        perror(path);
        exit(1);
    }

    /* how many bytes does the log hold? */
    size_t nbyte = pmemlog_nbyte(plp);
    printf("log holds %zu bytes", nbyte);

    /* append to the log... */
    char *str = "This is the first string appended";
    if (pmemlog_append(plp, str, strlen(str)) < 0) {
        perror("pmemlog_append");
        exit(1);
    }

    str = "This is the second string appended";
    if (pmemlog_append(plp, str, strlen(str)) < 0) {
        perror("pmemlog_append");
        exit(1);
    }

    /* print the log contents */
    printf("log contains:");
    pmemlog_walk(plp, 0, printit, NULL);

    pmemlog_close(plp);
}

运行结果如下,1<30等于1073741824,大致等于如下的1073733632 字节数,多余的字节存放其他数据结构:

$ ./manpage 
log holds 1073733632 bytes
log contains:
This is the first string appended
This is the second string appended

通过pmempool查看结果,可以发现pmemlog是以0a这个换行符做为结束符结束一条记录的:

$ ./pmempool info -d /root/hostname/pmemlog.001
Part file:
path                     : /root/hostname/pmemlog.001
type                     : regular file
size                     : 1073741824

POOL Header:
Signature                : PMEMLOG
Major                    : 1
Mandatory features       : 0x0
Not mandatory features   : 0x0
Forced RO                : 0x0
Pool set UUID            : a307be6a-b300-497b-b123-01b17eda7a6a
UUID                     : fb3b6ba0-45a6-4f07-aea9-52a4ba8107ec
Previous part UUID       : fb3b6ba0-45a6-4f07-aea9-52a4ba8107ec
Next part UUID           : fb3b6ba0-45a6-4f07-aea9-52a4ba8107ec
Previous replica UUID    : fb3b6ba0-45a6-4f07-aea9-52a4ba8107ec
Next replica UUID        : fb3b6ba0-45a6-4f07-aea9-52a4ba8107ec
Creation Time            : Wed Aug 22 2018 21:17:45
Alignment Descriptor     : 0x000007f737777310[OK]
Class                    : 64
Data                     : 2's complement, little endian
Machine                  : AMD X86-64
Last shutdown            : clean
Checksum                 : 0xcba5bfdb2dcf3a1e [OK]

PMEM LOG Header:
Start offset             : 0x2000
Write offset             : 0x2045 [OK]
End offset               : 0x40000000

PMEMLOG data:
00002000  54 68 69 73 20 69 73 20  74 68 65 20 66 69 72 73  |This is the firs|
00002010  74 20 73 74 72 69 6e 67  20 61 70 70 65 6e 64 65  |t string appende|
00002020  64 0a 54 68 69 73 20 69  73 20 74 68 65 20 73 65  |d.This is the se|
00002030  63 6f 6e 64 20 73 74 72  69 6e 67 20 61 70 70 65  |cond string appe|
00002040  6e 64 65 64 0a                                    |nded.           |
------------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/qccz123456/article/details/81908424