天天看點

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)) > ) {
        pmem_memcpy_nodrain(pmemaddr, buf, cc);
        pmemaddr += cc;
    }

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

    /* 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)) > ) {
        memcpy(addr, buf, cc);
        addr += cc;
    }

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

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