-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to use PMDK lib with C style CMAKE configuration and also …
…created example with test code from pmdk guide
- Loading branch information
1 parent
c959ef2
commit 1eb4268
Showing
2 changed files
with
97 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
project(PMEM_mmap_IO C) | ||
|
||
set(CMAKE_C_STANDARD 11) | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2") | ||
set(CMAKE_VERBOSE_MAKEFILE ON) | ||
|
||
add_executable(PMEM_mmap_IO src/main.c) | ||
# Include source content | ||
file (GLOB_RECURSE sourceFiles CONFIGURE_DEPENDS "src/*.c") | ||
file (GLOB_RECURSE headerFiles CONFIGURE_DEPENDS "src/*.h") | ||
|
||
set (includeDirs "") | ||
foreach (_headerFile ${headerFiles}) | ||
get_filename_component(_dir ${_headerFile} PATH) | ||
list (APPEND includeDirs ${_dir}) | ||
endforeach() | ||
list (REMOVE_DUPLICATES includeDirs) | ||
|
||
# Mark executable | ||
add_executable(PMEM_mmap_IO ${sourceFiles}) | ||
target_include_directories(PMEM_mmap_IO PRIVATE ${includeDirs}) | ||
|
||
set(PMDK_INCLUDE "C:/Users/DEG3NERAT3_/vcpkg/packages/pmdk_x64-windows/include") | ||
set(PMDK_LIBRARIES "C:/Users/DEG3NERAT3_/vcpkg/packages/pmdk_x64-windows/lib") | ||
target_include_directories(PMEM_mmap_IO PRIVATE ${PMDK_INCLUDE}) | ||
target_link_libraries(PMEM_mmap_IO PRIVATE ${PMDK_LIBRARIES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,89 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
#include <sys/mman.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <libpmem.h> | ||
|
||
int main(){ | ||
/* using 1k of pmem for this example */ | ||
#define PMEM_LEN 1024 | ||
|
||
int arraySize = 5; | ||
// Maximum length of our buffer | ||
#define MAX_BUF_LEN 30 | ||
|
||
int *ptr = mmap( | ||
NULL, | ||
arraySize * sizeof(int), | ||
PROT_READ | PROT_WRITE, | ||
MAP_SHARED | MAP_ANONYMOUS, | ||
0,0 | ||
); | ||
/**************************** | ||
* This function writes the "Hello..." string to persistent-memory. | ||
*****************************/ | ||
void write_hello_string (char *buf, char *path) { | ||
char *pmemaddr; | ||
size_t mapped_len; | ||
int is_pmem; | ||
|
||
if (ptr == MAP_FAILED) { | ||
printf("Could not map file\n"); | ||
return 1; | ||
/* create a pmem file and memory map it */ | ||
if ((pmemaddr = (char *) pmem_map_file( | ||
path, | ||
PMEM_LEN, | ||
PMEM_FILE_CREATE, | ||
0666, | ||
&mapped_len, | ||
&is_pmem | ||
)) == NULL) { | ||
perror("pmem_map_file"); | ||
exit(1); | ||
} | ||
/* store a string to the persistent memory */ | ||
strcpy(pmemaddr, buf); | ||
|
||
for (int i = 0; i < arraySize; i++) { | ||
ptr[i] = i + 1; | ||
} | ||
/* flush above strcpy to persistence */ | ||
if (is_pmem) | ||
pmem_persist(pmemaddr, mapped_len); | ||
else | ||
pmem_msync(pmemaddr, mapped_len); | ||
|
||
printf("Initial array: "); | ||
for (int i = 0; i < arraySize; i++ ){ | ||
printf("%d ", ptr[i] ); | ||
} | ||
printf("\n"); | ||
|
||
pid_t child_pid = fork(); | ||
|
||
if (child_pid == 0) { | ||
//child | ||
for (int i = 0; i < N; i++){ | ||
ptr[i] = ptr[i] * 10; | ||
} | ||
} else{ | ||
//parent | ||
waitpid(child_pid, NULL, 0); | ||
printf("\nParent:\n"); | ||
|
||
printf("Updated array: "); | ||
for (int i = 0; i < arraySize; i++){ | ||
printf("%d ", ptr[i] ); | ||
} | ||
printf("\n"); | ||
/* output a string from the persistent memory to console */ | ||
printf("\nWrite the (%s) string to persistent memory.\n",pmemaddr); | ||
} | ||
|
||
/**************************** | ||
* This function reads the "Hello..." string from persistent-memory. | ||
*****************************/ | ||
void read_hello_string(char *path) { | ||
char *pmemaddr; | ||
size_t mapped_len; | ||
int is_pmem; | ||
|
||
/* open the pmem file to read back the data */ | ||
if ((pmemaddr = (char *) pmem_map_file( | ||
path, | ||
PMEM_LEN, | ||
PMEM_FILE_CREATE, | ||
0666, | ||
&mapped_len, | ||
&is_pmem | ||
)) == NULL) { | ||
perror("pmem_map_file"); | ||
exit(1); | ||
} | ||
/* Reading the string from persistent-memory and write to console */ | ||
printf("\nRead the (%s) string from persistent memory.\n",pmemaddr); | ||
} | ||
|
||
/**************************** | ||
* This main function gather from the command line and call the appropriate | ||
* functions to perform read and write persistently to memory. | ||
*****************************/ | ||
int main(int argc, char *argv[]) { | ||
char *path = argv[2]; | ||
|
||
int err = munmap(ptr, arraySize * sizeof(int)); | ||
// Create the string to save to persistent memory | ||
char buf[MAX_BUF_LEN] = "Hello Persistent Memory!!!"; | ||
|
||
if (err != 0) { | ||
printf("Could not unmap file\n"); | ||
return 1; | ||
if (strcmp (argv[1], "-w") == 0) { | ||
write_hello_string (buf, path); | ||
} else if (strcmp (argv[1], "-r") == 0) { | ||
read_hello_string(path); | ||
} else { | ||
fprintf(stderr, "Usage: %s <-w/-r> <filename>\n", argv[0]); | ||
exit(1); | ||
} | ||
return 0; | ||
|
||
} |