Optane非易失性內(nèi)存代碼實(shí)測:
使用PMDK中的obj封裝好的庫進(jìn)行:
#include <stdio.h>
#include <string.h>
#include <libpmemobj.h>
#include <fstream>
#include <iostream>
#include <math.h>
#include <mutex>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <libpmem.h>
using namespace std;
POBJ_LAYOUT_BEGIN(lastfm_dataset);
POBJ_LAYOUT_ROOT(lastfm_dataset, struct MY_ROOT); //必須要定義才能夠使用
// POBJ_LAYOUT_TOID(lastfm_dataset, LN) // 如何需要用其他結(jié)構(gòu)體它呀,需要在這里定義好
POBJ_LAYOUT_END(lastfm_dataset);
#define MAX_BUF_LEN 10
struct MY_ROOT {
int a;
int buf[MAX_BUF_LEN];
};
static inline int file_exists(char const *file) { return access(file, F_OK); }
int main(int argc, char *argv[])
{
char* persistent_path = "/mnt/pmem/recNVM/data/lastfm";
PMEMobjpool *pop; // 預(yù)先分配好空間
if (file_exists(persistent_path) != 0) {
pop = pmemobj_create(persistent_path, "lastfm_dataset", 8000000000,
0666); // 創(chuàng)建NVM的池子
if (pop == NULL)
{
perror("pmemobj_create failed\n");
exit(0);
}
} else {
pop = pmemobj_open(persistent_path, "lastfm_dataset"); //如果已經(jīng)創(chuàng)建則直接打開
}
TOID(struct MY_ROOT) root = POBJ_ROOT(pop, struct MY_ROOT); // 創(chuàng)建胖指針
int buf[10];
for(int i=0; i<10; i++)
buf[i] = (i);
TX_BEGIN(pop) {
TX_MEMCPY(D_RW(root)->buf, buf, sizeof(float) * 10); //調(diào)用事務(wù)涡戳,原子性地將數(shù)據(jù)寫入翅阵,其中D_RW將胖指針root轉(zhuǎn)成常用瘦指針印衔,不然不能操作
} TX_END
for(int i=0; i<10; i++){
cout<<D_RO(root)->buf[i]<<" "; // D_RO表示讀數(shù)據(jù)
}
pmemobj_close(pop);
return 0;
}