PMDK libpmemblk 例程1 读写持久化内存块

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

使用libpmemblk能直接访问storage(DAX),支持 load/store access,不需要 paging blocks。
libpmemblk API说明:http://pmem.io/pmdk/manpages/linux/master/libpmemblk/libpmemblk.7.html

Key:
pmemblk_create()  // 创建pmem的内存块
pmemblk_open()  // 打开已创建的内存块
pmemblk_nblock()  // 获得某内存块内分片元素数量
pmemblk_write()  // 将数据写入某分片元素
pmemblk_read()  // 从某分片元素中读出数据
pmemblk_set_zero()  // 将某分片元素设置成0
pmemblk_close()  // 关闭内存块
pmemblk_set_funcs()  // 允许用户覆盖libpmemblk内的malloc、free等函数,实现自定义malloc、free等
例子:

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

#include <libpmemblk.h>

#define POOL_SIZE ((size_t)(1024 * 1024 * 1024))  /* size of the pmemblk pool -- 1 GB */
#define ELEMENT_SIZE 1024  /* size of each element in the pmem pool */

int main(int argc, char *argv[])
{
    const char path[] = "/root/hostnme/pmemblk.002";
    char buf[ELEMENT_SIZE];
    char rcv[ELEMENT_SIZE];

    /* create the pmemblk pool or open it if it already exists */
    PMEMblkpool *pbp = pmemblk_create(path, ELEMENT_SIZE, POOL_SIZE, 0666);

    if (pbp == NULL)
        pbp = pmemblk_open(path, ELEMENT_SIZE);

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

    /* how many elements fit into the file? */
    size_t nelements = pmemblk_nblock(pbp);
    printf("file holds %zu elements\n", nelements);

    /* store a block at index 5 */
    strcpy(buf, "hello, world");
    printf("buf: %s\n", buf);
    if (pmemblk_write(pbp, buf, 0) < 0 ||
        pmemblk_write(pbp, buf, 5) < 0 ||
        pmemblk_write(pbp, buf, 6) < 0) {
        perror("pmemblk_write");
        exit(1);
    }

    /* read the block at index 5 */
    if (pmemblk_read(pbp, rcv, 5) < 0) {
        perror("pmemblk_read");
        exit(1);
    }
    printf("rcv: %s\n", rcv);

    /* read the block at index 10 (reads as zeros initially) */
    if (pmemblk_read(pbp, rcv, 10) < 0) {
        perror("pmemblk_read");
        exit(1);
    }
    printf("rcv: %s\n", rcv);

    /* zero out the block at index 5 */
    if (pmemblk_set_zero(pbp, 5) < 0) {
        perror("pmemblk_set_zero");
        exit(1);
    }

    /* read the block at index 5 */
    if (pmemblk_read(pbp, rcv, 5) < 0) {
        perror("pmemblk_read");
        exit(1);
    }
    printf("rcv: %s\n", rcv);

    pmemblk_close(pbp);

    return 0;

运行结果如下:POOL_SIZE 除以 ELEMENT_SIZE 为 1024*1024,但结果为1044204,说明pmemblk创建时占用了部分字节,需根据pmemblk_nblock()得到pmemblk_create()创建得到的实际block数量。

$ ./manpage 
file holds 1044204 elements
buf: hello, world
rcv: hello, world
rcv: 
rcv:

通过pmempool查看结果,每一个block(0x000037f0减0x00003400等于1009),说明实际每个block中并没有1024的element,注意一下hello, world总共12个字符(中间有空格)对应与前面的12个字节68 65 6c 6c 6f 2c 20 77 6f 72 6c 64,中间无分隔符:

$ ./pmempool info -d /root/hostname/pmemblk.002 --skip-zeros 
Part file:
path                     : /root/hostname/pmemblk.002
type                     : regular file
size                     : 1073741824

POOL Header:
Signature                : PMEMBLK
Major                    : 1
Mandatory features       : 0x0
Not mandatory features   : 0x0
Forced RO                : 0x0
Pool set UUID            : 6aa96390-7748-4991-bccf-9c535261f909
UUID                     : c5af766e-054f-4b09-93ee-44efd89a0c40
Previous part UUID       : c5af766e-054f-4b09-93ee-44efd89a0c40
Next part UUID           : c5af766e-054f-4b09-93ee-44efd89a0c40
Previous replica UUID    : c5af766e-054f-4b09-93ee-44efd89a0c40
Next replica UUID        : c5af766e-054f-4b09-93ee-44efd89a0c40
Creation Time            : Wed Aug 22 2018 21:31:40
Alignment Descriptor     : 0x000007f737777310[OK]
Class                    : 64
Data                     : 2's complement, little endian
Machine                  : AMD X86-64
Last shutdown            : clean
Checksum                 : 0xdc5e837e32c6a106 [OK]

PMEM BLK Header:
Block size               : 1024
Is zeroed                : true

[ARENA 0]
PMEM BLK BTT Info Header:
Signature                : BTT_ARENA_INFO
UUID of container        : 6aa96390-7748-4991-bccf-9c535261f909
Flags                    : 0x0
Major                    : 1
Minor                    : 1
External LBA size        : 1024
External LBA count       : 1044204
Internal LBA size        : 1024
Internal LBA count       : 1044460
Free blocks              : 256
Info block size          : 4096
Next arena offset        : 0x0
Arena data offset        : 0x1000
Area map offset          : 0x3fbfd000
Area flog offset         : 0x3fff9000
Info block backup offset : 0x3fffd000
Checksum                 : 0x39e2a520dc291ae3 [OK]

PMEM BLK blocks data:
Block          0: offset: 0x00000001 state: normal
00003400  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 00 00 00 00  |hello, world....|
00003410  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
*
00003430  04 00 00 00 00 00 00 00  28 4d 7b 09 2c 7f 00 00  |........(M{.,...|
00003440  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00003450  0d 39 8a 92 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |.9........#.,...|
00003460  e1 e5 49 92 02 80 ff ff  28 4d 7b 09 2c 7f 00 00  |..I.....(M{.,...|
00003470  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00003480  57 db 93 1c 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |W.........#.,...|
00003490  b1 e5 49 92 02 80 ff ff  10 1a b6 6d fd 7f 00 00  |..I........m....|
000034a0  d0 37 7b 09 2c 7f 00 00  68 9d 7b 09 2c 7f 00 00  |.7{.,...h.{.,...|
000034b0  20 1b b6 6d fd 7f 00 00  10 1b b6 6d fd 7f 00 00  | ..m.......m....|
000034c0  17 00 00 00 00 00 00 00  15 cf 89 09 2c 7f 00 00  |............,...|
000034d0  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
000034e0  18 fa 44 0a 2c 7f 00 00  00 4f b7 09 2c 7f 00 00  |..D.,....O..,...|
000034f0  03 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00003500  31 6e d5 35 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |1n.5......#.,...|
00003510  30 57 65 01 00 00 00 00  90 1a b6 6d fd 7f 00 00  |0We........m....|
00003520  ec 4c b7 09 2c 7f 00 00  d8 61 b7 09 2c 7f 00 00  |.L..,....a..,...|
00003530  a0 1b b6 6d fd 7f 00 00  90 1b b6 6d fd 7f 00 00  |...m.......m....|
00003540  31 00 00 00 2c 7f 00 00  c0 1a b6 6d fd 7f 00 00  |1...,......m....|
00003550  00 00 00 00 00 00 00 00  30 59 43 0a 2c 7f 00 00  |........0YC.,...|
00003560  50 f5 44 0a 2c 7f 00 00  55 a9 ff 09 2c 7f 00 00  |P.D.,...U...,...|
00003570  50 6e b7 09 2c 7f 00 00  98 95 ff 09 2c 7f 00 00  |Pn..,.......,...|
00003580  00 00 00 00 01 00 00 00  c9 00 00 00 01 00 00 00  |................|
00003590  18 fa 44 0a 2c 7f 00 00  58 1c b6 6d fd 7f 00 00  |..D.,...X..m....|
000035a0  30 1c b6 6d fd 7f 00 00  01 00 00 00 00 00 00 00  |0..m............|
000035b0  30 59 43 0a 2c 7f 00 00  20 0a 45 0a 2c 7f 00 00  |0YC.,... .E.,...|
000035c0  c8 06 45 0a 2c 7f 00 00  df 9f 23 0a 2c 7f 00 00  |..E.,.....#.,...|
000035d0  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
000035e0  01 00 00 00 2c 7f 00 00  28 4d 7b 09 2c 7f 00 00  |....,...(M{.,...|
000035f0  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00003600  2e 4e 3d f6 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |.N=.......#.,...|
00003610  01 00 00 00 2c 7f 00 00  90 1b b6 6d fd 7f 00 00  |....,......m....|
00003620  30 4b 7b 09 2c 7f 00 00  a8 11 7c 09 2c 7f 00 00  |0K{.,.....|.,...|
00003630  a0 1c b6 6d fd 7f 00 00  90 1c b6 6d fd 7f 00 00  |...m.......m....|
00003640  2e 00 00 00 00 00 00 00  55 a9 ff 09 2c 7f 00 00  |........U...,...|
00003650  00 00 00 00 00 00 00 00  40 58 43 0a 2c 7f 00 00  |........@XC.,...|
00003660  18 fa 44 0a 2c 7f 00 00  d1 05 40 00 00 00 00 00  |..D.,.....@.....|
00003670  78 1d 7c 09 2c 7f 00 00  60 03 40 00 00 00 00 00  |x.|.,...`.@.....|
00003680  00 00 00 00 01 00 00 00  30 08 00 00 01 00 00 00  |........0.......|
00003690  d0 19 7c 09 2c 7f 00 00  58 1d b6 6d fd 7f 00 00  |..|.,...X..m....|
000036a0  30 1d b6 6d fd 7f 00 00  01 00 00 00 00 00 00 00  |0..m............|
000036b0  40 58 43 0a 2c 7f 00 00  a8 34 45 0a 2c 7f 00 00  |@XC.,....4E.,...|
000036c0  50 31 45 0a 2c 7f 00 00  df 9f 23 0a 2c 7f 00 00  |P1E.,.....#.,...|
000036d0  00 00 00 00 00 00 00 00  40 58 43 0a 2c 7f 00 00  |........@XC.,...|
000036e0  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000036f0  01 00 00 00 fd 7f 00 00  50 31 45 0a 2c 7f 00 00  |........P1E.,...|
00003700  01 00 00 00 fd 7f 00 00  00 00 00 00 00 00 00 00  |................|
00003710  01 00 00 00 00 00 00 00  d8 61 b7 09 2c 7f 00 00  |.........a..,...|
00003720  00 00 00 00 00 00 00 00  a8 34 45 0a 2c 7f 00 00  |.........4E.,...|
00003730  a0 1c b6 6d fd 7f 00 00  90 1c b6 6d fd 7f 00 00  |...m.......m....|
00003740  2e 4e 3d f6 00 00 00 00  d1 05 40 00 00 00 00 00  |.N=.......@.....|
00003750  ff ff ff ff 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00003760  a8 11 7c 09 2c 7f 00 00  18 fa 44 0a 2c 7f 00 00  |..|.,.....D.,...|
00003770  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00003780  72 65 67 69 6f 6e 00 53  74 61 72 74 20 74 72 61  |region.Start tra|
00003790  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000037b0  72 65 67 69 6f 6e 00 53  74 61 72 74 20 74 72 61  |region.Start tra|
000037c0  00 00 00 00 00 00 00 00  38 20 60 00 00 00 00 00  |........8 `.....|
000037d0  c0 08 40 00 00 00 00 00  90 1e b6 6d fd 7f 00 00  |[email protected]....|
000037e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000037f0  00 00 00 00 00 00 00 00  1e e9 23 0a 2c 7f 00 00  |..........#.,...|
------------------------------------------------------------------------------
Block          6: offset: 0x00000006 state: normal
00004800  68 65 6c 6c 6f 2c 20 77  6f 72 6c 64 00 00 00 00  |hello, world....|
00004810  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
*
00004830  04 00 00 00 00 00 00 00  28 4d 7b 09 2c 7f 00 00  |........(M{.,...|
00004840  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00004850  0d 39 8a 92 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |.9........#.,...|
00004860  e1 e5 49 92 02 80 ff ff  28 4d 7b 09 2c 7f 00 00  |..I.....(M{.,...|
00004870  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00004880  57 db 93 1c 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |W.........#.,...|
00004890  b1 e5 49 92 02 80 ff ff  10 1a b6 6d fd 7f 00 00  |..I........m....|
000048a0  d0 37 7b 09 2c 7f 00 00  68 9d 7b 09 2c 7f 00 00  |.7{.,...h.{.,...|
000048b0  20 1b b6 6d fd 7f 00 00  10 1b b6 6d fd 7f 00 00  | ..m.......m....|
000048c0  17 00 00 00 00 00 00 00  15 cf 89 09 2c 7f 00 00  |............,...|
000048d0  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
000048e0  18 fa 44 0a 2c 7f 00 00  00 4f b7 09 2c 7f 00 00  |..D.,....O..,...|
000048f0  03 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00004900  31 6e d5 35 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |1n.5......#.,...|
00004910  30 57 65 01 00 00 00 00  90 1a b6 6d fd 7f 00 00  |0We........m....|
00004920  ec 4c b7 09 2c 7f 00 00  d8 61 b7 09 2c 7f 00 00  |.L..,....a..,...|
00004930  a0 1b b6 6d fd 7f 00 00  90 1b b6 6d fd 7f 00 00  |...m.......m....|
00004940  31 00 00 00 2c 7f 00 00  c0 1a b6 6d fd 7f 00 00  |1...,......m....|
00004950  00 00 00 00 00 00 00 00  30 59 43 0a 2c 7f 00 00  |........0YC.,...|
00004960  50 f5 44 0a 2c 7f 00 00  55 a9 ff 09 2c 7f 00 00  |P.D.,...U...,...|
00004970  50 6e b7 09 2c 7f 00 00  98 95 ff 09 2c 7f 00 00  |Pn..,.......,...|
00004980  00 00 00 00 01 00 00 00  c9 00 00 00 01 00 00 00  |................|
00004990  18 fa 44 0a 2c 7f 00 00  58 1c b6 6d fd 7f 00 00  |..D.,...X..m....|
000049a0  30 1c b6 6d fd 7f 00 00  01 00 00 00 00 00 00 00  |0..m............|
000049b0  30 59 43 0a 2c 7f 00 00  20 0a 45 0a 2c 7f 00 00  |0YC.,... .E.,...|
000049c0  c8 06 45 0a 2c 7f 00 00  df 9f 23 0a 2c 7f 00 00  |..E.,.....#.,...|
000049d0  00 00 00 00 00 00 00 00  c4 95 23 0a 2c 7f 00 00  |..........#.,...|
000049e0  01 00 00 00 2c 7f 00 00  28 4d 7b 09 2c 7f 00 00  |....,...(M{.,...|
000049f0  04 00 00 00 00 00 00 00  16 00 00 00 00 00 00 00  |................|
00004a00  2e 4e 3d f6 00 00 00 00  fb 9c 23 0a 2c 7f 00 00  |.N=.......#.,...|
00004a10  01 00 00 00 2c 7f 00 00  90 1b b6 6d fd 7f 00 00  |....,......m....|
00004a20  30 4b 7b 09 2c 7f 00 00  a8 11 7c 09 2c 7f 00 00  |0K{.,.....|.,...|
00004a30  a0 1c b6 6d fd 7f 00 00  90 1c b6 6d fd 7f 00 00  |...m.......m....|
00004a40  2e 00 00 00 00 00 00 00  55 a9 ff 09 2c 7f 00 00  |........U...,...|
00004a50  00 00 00 00 00 00 00 00  40 58 43 0a 2c 7f 00 00  |........@XC.,...|
00004a60  18 fa 44 0a 2c 7f 00 00  d1 05 40 00 00 00 00 00  |..D.,.....@.....|
00004a70  78 1d 7c 09 2c 7f 00 00  60 03 40 00 00 00 00 00  |x.|.,...`.@.....|
00004a80  00 00 00 00 01 00 00 00  30 08 00 00 01 00 00 00  |........0.......|
00004a90  d0 19 7c 09 2c 7f 00 00  58 1d b6 6d fd 7f 00 00  |..|.,...X..m....|
00004aa0  30 1d b6 6d fd 7f 00 00  01 00 00 00 00 00 00 00  |0..m............|
00004ab0  40 58 43 0a 2c 7f 00 00  a8 34 45 0a 2c 7f 00 00  |@XC.,....4E.,...|
00004ac0  50 31 45 0a 2c 7f 00 00  df 9f 23 0a 2c 7f 00 00  |P1E.,.....#.,...|
00004ad0  00 00 00 00 00 00 00 00  40 58 43 0a 2c 7f 00 00  |........@XC.,...|
00004ae0  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00004af0  01 00 00 00 fd 7f 00 00  50 31 45 0a 2c 7f 00 00  |........P1E.,...|
00004b00  01 00 00 00 fd 7f 00 00  00 00 00 00 00 00 00 00  |................|
00004b10  01 00 00 00 00 00 00 00  d8 61 b7 09 2c 7f 00 00  |.........a..,...|
00004b20  00 00 00 00 00 00 00 00  a8 34 45 0a 2c 7f 00 00  |.........4E.,...|
00004b30  a0 1c b6 6d fd 7f 00 00  90 1c b6 6d fd 7f 00 00  |...m.......m....|
00004b40  2e 4e 3d f6 00 00 00 00  d1 05 40 00 00 00 00 00  |.N=.......@.....|
00004b50  ff ff ff ff 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00004b60  a8 11 7c 09 2c 7f 00 00  18 fa 44 0a 2c 7f 00 00  |..|.,.....D.,...|
00004b70  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00004b80  72 65 67 69 6f 6e 00 53  74 61 72 74 20 74 72 61  |region.Start tra|
00004b90  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004bb0  72 65 67 69 6f 6e 00 53  74 61 72 74 20 74 72 61  |region.Start tra|
00004bc0  00 00 00 00 00 00 00 00  38 20 60 00 00 00 00 00  |........8 `.....|
00004bd0  c0 08 40 00 00 00 00 00  90 1e b6 6d fd 7f 00 00  |[email protected]....|
00004be0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00004bf0  00 00 00 00 00 00 00 00  1e e9 23 0a 2c 7f 00 00  |..........#.,...|
------------------------------------------------------------------------------

猜你喜欢

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