240
Memory Overwrites
When multiple modifications to the same persistent memory location occur before
the location is made persistent (that is, flushed), a memory overwrite occurs. This is
a potential data corruption source if a program crashes because the final value of the
persistent variable can be any of the values written between the last flush and the crash.
It is important to know that this may not be an issue if it is in the code by design. We
recommend using volatile variables for short-lived data and only write to persistent
variables when you want to persist data.
Consider the code in Listing 12-31, which writes twice to the data variable inside the
main() function (lines 62 and 63) before we call flush() on line 64.
Listing 12-31. Example of persistent memory overwriting – variable data –
before flushing
33 #include
34 #include
35 #include
36 #include
37 #include
38 #include
39
40 void flush(const void *addr, size_t len) {
41
uintptr_t flush_align = 64, uptr;
42
for (uptr = (uintptr_t)addr & ~(flush_align - 1);
43
uptr < (uintptr_t)addr + len;
44
uptr += flush_align)
45
_mm_clflush((char *)uptr);
46 }
47
48 int main(int argc, char *argv[]) {
49
int fd, *data;
50
51
fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);
52
posix_fallocate(fd, 0, sizeof(int));
53
Chapter 12 Debugging persistent MeMory appliCations
Précédent

- 264/457

Suivant