218
40 // flushing from user space
41 void flush(const void *addr, size_t len) {
42
uintptr_t flush_align = 64, uptr;
43
for (uptr = (uintptr_t)addr & ~(flush_align - 1);
44
uptr < (uintptr_t)addr + len;
45
uptr += flush_align)
46
_mm_clflush((char *)uptr);
47 }
48
49 int main(int argc, char *argv[]) {
50
int fd, *data;
51
52
// open the file and allocate space for one
53
// integer
54
fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);
55
posix_fallocate(fd, 0, sizeof(int));
56
57
// map the file and register it with VALGRIND
58
data = (int *)mmap(NULL, sizeof(int),
59
PROT_READ | PROT_WRITE,
60
MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0);
61
VALGRIND_PMC_REGISTER_PMEM_MAPPING(data,
62
sizeof(int));
63
64
// write and flush
65
*data = 1234;
66
flush((void *)data, sizeof(int));
67
68
// unmap and un-register
69
munmap(data, sizeof(int));
70
VALGRIND_PMC_REMOVE_PMEM_MAPPING(data,
71
sizeof(int));
72
return 0;
73 }
Chapter 12 Debugging persistent MeMory appliCations
40 // flushing from user space
41 void flush(const void *addr, size_t len) {
42
uintptr_t flush_align = 64, uptr;
43
for (uptr = (uintptr_t)addr & ~(flush_align - 1);
44
uptr < (uintptr_t)addr + len;
45
uptr += flush_align)
46
_mm_clflush((char *)uptr);
47 }
48
49 int main(int argc, char *argv[]) {
50
int fd, *data;
51
52
// open the file and allocate space for one
53
// integer
54
fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);
55
posix_fallocate(fd, 0, sizeof(int));
56
57
// map the file and register it with VALGRIND
58
data = (int *)mmap(NULL, sizeof(int),
59
PROT_READ | PROT_WRITE,
60
MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0);
61
VALGRIND_PMC_REGISTER_PMEM_MAPPING(data,
62
sizeof(int));
63
64
// write and flush
65
*data = 1234;
66
flush((void *)data, sizeof(int));
67
68
// unmap and un-register
69
munmap(data, sizeof(int));
70
VALGRIND_PMC_REMOVE_PMEM_MAPPING(data,
71
sizeof(int));
72
return 0;
73 }
Chapter 12 Debugging persistent MeMory appliCations
