PMDK libpmemobj 例程3 宏定义操作

前两篇文章说了如何操作persist memory的读写和事务操作,但在函数调用方面,并不方便,需要考虑偏移量和内存大小等问题。所以本节介绍一些简化函数调用的宏定义:
(1)定义PMEMobjpool持久性内存池的名称,通过POBJ_LAYOUT_BEGIN、POBJ_LAYOUT_ROOT、POBJ_LAYOUT_END进行定义,然后通过POBJ_LAYOUT_NAME调用该名称;
POBJ_LAYOUT_BEGIN(string_store);
POBJ_LAYOUT_ROOT(string_store, struct my_root);
POBJ_LAYOUT_END(string_store);
  POBJ_LAYOUT_NAME(string_store)
(2)POBJ_ROOT() 宏替代了 pmemobj_root() 函数,应用等于内存池的根节点;
(3)TOID 宏替代 PMEMoids 的指针操作;
(4)TX_MEMCPY 宏替代 pmemobj_tx_add_range_direct() 函数进行事务操作
(5)D_RWD_RO 更直接写入或读取 TOID 定义的根节点。

writer.c

#include 
#include 
#include 

#define MAX_BUF_LEN 10

POBJ_LAYOUT_BEGIN(string_store);
POBJ_LAYOUT_ROOT(string_store, struct my_root);
POBJ_LAYOUT_END(string_store);

struct my_root {
    char buf[MAX_BUF_LEN];
};

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("usage: %s file-name\n", argv[0]);
        return 1;
    }

    PMEMobjpool *pop = pmemobj_create(argv[1], POBJ_LAYOUT_NAME(string_store), PMEMOBJ_MIN_POOL, 0666);
    if (pop == NULL) {
        perror("pmemobj_create");
        return 1;
    }

    char buf[MAX_BUF_LEN] = {0};
    int num = scanf("%9s", buf);
    if (num == EOF) {
        fprintf(stderr, "EOF\n");
        return 1;
    }

    TOID(struct my_root) root = POBJ_ROOT(pop, struct my_root);

    TX_BEGIN(pop) {
        TX_MEMCPY(D_RW(root)->buf, buf, strlen(buf));
        //pmemobj_tx_abort(EINVAL);
    } TX_END

    pmemobj_close(pop);
    return 0;
}

reader.c

#include 
#include 
#include 

#define MAX_BUF_LEN 10

POBJ_LAYOUT_BEGIN(string_store);
POBJ_LAYOUT_ROOT(string_store, struct my_root);
POBJ_LAYOUT_END(string_store);

struct my_root {
    char buf[MAX_BUF_LEN];
};

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("usage: %s file-name\n", argv[0]);
        return 1;
    }

    PMEMobjpool *pop = pmemobj_open(argv[1], POBJ_LAYOUT_NAME(string_store));
    if (pop == NULL) {
        perror("pmemobj_open");
        return 1;
    }

    TOID(struct my_root) root = POBJ_ROOT(pop, struct my_root);

    printf("%s\n", D_RO(root)->buf);

    pmemobj_close(pop);
    return 0;
}

先将pmemobj_tx_abort()函数注销,所以拷贝操作会成功,如果不注销,则事务操作会回滚,意思就是说没有数据会写入内存池,运行结果如下:

$ ./writer pmem.001
123456
$ ./reader pmem.001
123456

$ ./writer pmem.002
123456
$ ./reader pmem.002 

通过pmempool工具查看如下:

$ ./pmempool info -d /home/hostname/pmem.001 -o
Part file:
path                     : /home/hostname/pmem.001
type                     : regular file
size                     : 8388608

POOL Header:
Signature                : PMEMOBJ
Major                    : 4
Mandatory features       : 0x0
Not mandatory features   : 0x0
Forced RO                : 0x0
Pool set UUID            : a3642b53-9d3e-42fd-a0f4-d4b9fd163676
UUID                     : b5903a59-c03d-4a68-8a75-2d5fb22058a2
Previous part UUID       : b5903a59-c03d-4a68-8a75-2d5fb22058a2
Next part UUID           : b5903a59-c03d-4a68-8a75-2d5fb22058a2
Previous replica UUID    : b5903a59-c03d-4a68-8a75-2d5fb22058a2
Next replica UUID        : b5903a59-c03d-4a68-8a75-2d5fb22058a2
Creation Time            : Tue Aug 28 2018 08:48:49
Alignment Descriptor     : 0x000007f737777310[OK]
Class                    : ELF64
Data                     : 2's complement, little endian
Machine                  : AMD X86-64
Checksum                 : 0x36af8ef0feaf4fe5 [OK]

PMEM OBJ Header:
Layout                   : string_store
Lanes offset             : 0x2000
Number of lanes          : 1024
Heap offset              : 0x302000
Heap size                : 5234688
Checksum                 : 0xc7d69baa42c46f55 [OK]
Root offset              : 0x3c0550

Root object:
Offset                   : 0x00000000003c0550
Size                     : 10
003c0550  31 32 33 34 35 36 00 00  00 00 00 00 00 00 00 00  |123456..........|
003c0560  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
003c05c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
------------------------------------------------------------------------------

$ ./pmempool info -d /home/hostname/pmem.002 -o
Part file:
path                     : /home/hostname/pmem.002
type                     : regular file
size                     : 8388608

POOL Header:
Signature                : PMEMOBJ
Major                    : 4
Mandatory features       : 0x0
Not mandatory features   : 0x0
Forced RO                : 0x0
Pool set UUID            : e88d6514-2a97-42d3-b60a-f51e96e19d34
UUID                     : 580ee76f-7398-448e-9aa4-24d61cc2e862
Previous part UUID       : 580ee76f-7398-448e-9aa4-24d61cc2e862
Next part UUID           : 580ee76f-7398-448e-9aa4-24d61cc2e862
Previous replica UUID    : 580ee76f-7398-448e-9aa4-24d61cc2e862
Next replica UUID        : 580ee76f-7398-448e-9aa4-24d61cc2e862
Creation Time            : Tue Aug 28 2018 08:49:21
Alignment Descriptor     : 0x000007f737777310[OK]
Class                    : ELF64
Data                     : 2's complement, little endian
Machine                  : AMD X86-64
Checksum                 : 0xe44583ef8c12ec65 [OK]

PMEM OBJ Header:
Layout                   : string_store
Lanes offset             : 0x2000
Number of lanes          : 1024
Heap offset              : 0x302000
Heap size                : 5234688
Checksum                 : 0xc7d69baa42c46f55 [OK]
Root offset              : 0x3c0550

Root object:
Offset                   : 0x00000000003c0550
Size                     : 10
003c0550  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
003c05c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
------------------------------------------------------------------------------





你可能感兴趣的:(Persistent,Memory)