pmdk icon indicating copy to clipboard operation
pmdk copied to clipboard

The Question to use pmem_map_file to map existing file in PM

Open pyhundan opened this issue 2 years ago • 3 comments

QUESTION: The Question to use pmem_map_file to map existing file in PM

Details

Hi guys, I have a problem when running programs on Optane DCPMM, please help check the following code:

`#include <libpmem.h>
#include<iostream>
#include <errno.h>

/* using 1k of pmem for this example */
#define PMEM_LEN 1024

#define PATH "/mnt/ramdisk/test.txt"  // like "/home/hostname/Desktop/pmem.001"

int main(int argc, char *argv[])
{
    void *pmemaddr;
    size_t mapped_len=1024;
    int is_pmem;

    if ((pmemaddr = pmem_map_file(PATH, PMEM_LEN, 0,
                0666, &mapped_len, &is_pmem)) == NULL) {
        perror("pmem_map_file");
        std::cout<<"failed"<<std::endl;
        exit(1);
    }
    else{
        
        std::cout<<"success"<<std::endl;
    }
    
    pmem_unmap(pmemaddr, mapped_len);

}`

I just want to how to use pmem_map_file to map existing files. But the way my code is written doesn't map files on PM to memory, and I don't know there's an error there. And when pmem_map_file's flag use PMEM_FILE_CREATE, the program will work successfully.

Can anyone ask my question? Maybe it is a simple problem.

pyhundan avatar May 12 '22 09:05 pyhundan

Try enabling debug logs, and it should tell you what's the error. You can also use pmem_errormsg().

But, in general, if adding PMEM_FILE_CREATE helps, then that means the file likely doesn't exist.

pbalcer avatar May 12 '22 09:05 pbalcer

Hi, @pbalcer I tried to set pmem_map_file the flag to PMEM_FILE_CREATE , then create a file on PM, and then modify the program to set the flag to 0, but still error, the error message is pmem_map_file: Invalid argument

If I want to map the existing PM file, is the code like I did to set flags to 0

pyhundan avatar May 12 '22 14:05 pyhundan

Enabling debug logs (https://pmem.io/pmdk/manpages/linux/master/libpmem/libpmem.7/) will help you decipher that error. Looking more closely, I think your problem is trying to map 1/4 of a page. If PMEM_FILE_CREATE is not specified in flags, the entire existing file path is mapped, len must be zero, and mode is ignored. (from: https://pmem.io/pmdk/manpages/linux/master/libpmem/pmem_is_pmem.3/)

pbalcer avatar May 12 '22 14:05 pbalcer

This issue seems inactive now, if you still have some questions, please re-open it.

lukaszstolarczuk avatar Mar 13 '23 11:03 lukaszstolarczuk