PMDK libpmem 例程3 统一flush

将数据统一收集后,最后才flush到持久化文件中
Key:
pmem_memcpy_nodrain() // 若数据地址是持久化数据,将数据先导入pmem的缓存中,并不会flush到持久化文件中
pmem_drain() // 若数据地址是持久化数据,统一将数据flush到文件中
memcpy() // 若数据地址是普通易失性内存数据,通过memcpy缓存
pmem_msync() // 若数据地址是普通易失性内存数据,通过pmem_msync统一刷入持久性文件。

// do_copy_to_pmem -- copy to pmem, postponing drain step until the end
void do_copy_to_pmem(char *pmemaddr, int srcfd, off_t len)
{
    char buf[BUF_LEN];
    int cc;

    /* copy the file, saving the last flush step to the end */
    while ((cc = read(srcfd, buf, BUF_LEN)) > 0) {
        pmem_memcpy_nodrain(pmemaddr, buf, cc);
        pmemaddr += cc;
    }

    if (cc < 0) {
        perror("read");
        exit(1);
    }

    /* perform final flush step */
    pmem_drain();
}


// do_copy_to_non_pmem -- copy to a non-pmem memory mapped file
void do_copy_to_non_pmem(char *addr, int srcfd, off_t len)
{
    char *startaddr = addr;
    char buf[BUF_LEN];
    int cc;

    /* copy the file, saving the last flush step to the end */
    while ((cc = read(srcfd, buf, BUF_LEN)) > 0) {
        memcpy(addr, buf, cc);
        addr += cc;
    }

    if (cc < 0) {
        perror("read");
        exit(1);
    }

    /* flush it */
    if (pmem_msync(startaddr, len) < 0) {
        perror("pmem_msync");
        exit(1);
    }
}

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