#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libpmem.h>
#include <sys/time.h>
//编译运行
//g++ DRAM_NVM_Bandwidth.cpp -o DRAM_NVM_Bandwidth -lpmem
//NOTE:
//每次运行后,去PATH路径下删除生成的文件,才可以运行第二次。
//详情:
//https://pmem.io/pmdk/
/* using 4k of pmem for this example */
// 1GB
#define PMEM_LEN 1073741824
// #define PMEM_LEN 16384
#define PATH "/mnt/pmem/test_lz"
int main(int argc, char *argv[])
{
struct timeval tvs,tve;
char *pmemaddr1; //这个就是NVM上的地址
char *pmemaddr2; //这个就是NVM上的地址
size_t mapped_len; //NVM文件的总大小
int is_pmem1; //是否识别为持久性内存,1 是 ,0 否。 假如为0的话表示PATH这个地址现在不是在NVM上(要重新挂载一下) -命令: mount -o dax,noatime /dev/pmem0 /mnt/pmem/
int is_pmem2;
char *dramaddr1;
char *dramaddr2;
dramaddr1 = (char *)malloc(PMEM_LEN);
dramaddr2 = (char *)malloc(PMEM_LEN);
/* create a pmem file and memory map it */
if ((pmemaddr1 = (char *)pmem_map_file(PATH, PMEM_LEN, PMEM_FILE_CREATE,
0666, &mapped_len, &is_pmem1)) == NULL)
{
perror("pmem_map_file");
exit(1);
}
if ((pmemaddr2 = (char *)pmem_map_file(PATH, PMEM_LEN, PMEM_FILE_CREATE,
0666, &mapped_len, &is_pmem2)) == NULL)
{
perror("pmem_map_file");
exit(1);
}
printf("%d %d\n", is_pmem1,is_pmem2); //假如为 1 ,后面就可以用这个指针了
/* store a string to the persistent memory */
// pmem_memset_persist(pmemaddr1, '1', PMEM_LEN);
/*
***************************************************************************
************************* DRAM -> NVM *************************************
***************************************************************************
*/
memset(dramaddr1, 'b', PMEM_LEN);
gettimeofday(&tvs,NULL);
// memcpy(pmemaddr1,dramaddr,PMEM_LEN);
pmem_memcpy(pmemaddr1,dramaddr1,PMEM_LEN,0);
// pmem_memcpy_nodrain(pmemaddr1,dramaddr,PMEM_LEN);
gettimeofday(&tve,NULL);
// second
double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
printf("DRAM -> NVM time: %.4f\n",span);
printf("The DRAM to NVM bandwidth is: %.4f GB/s\n",PMEM_LEN/1024/1024/1024/span);
// printf("%c\n",pmemaddr1[0]);
/*
***************************************************************************
************************* NVM -> DRAM *************************************
***************************************************************************
*/
// pmem_memset_persist(pmemaddr, '1', PMEM_LEN);
memset(pmemaddr2, 'c', PMEM_LEN);
gettimeofday(&tvs,NULL);
pmem_memcpy(dramaddr2,pmemaddr2,PMEM_LEN,0);
gettimeofday(&tve,NULL);
// second
span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;
printf("NVM -> DRAM time: %.4f\n",span);
printf("The NVM to DRAM bandwidth is: %.4f GB/s\n",PMEM_LEN/1024/1024/1024/span);
printf("%c\n",dramaddr2[0]);
}
网友评论