Created
March 15, 2017 11:04
-
-
Save primiano/34937e18fb2577f2f1e79d1e089f5c59 to your computer and use it in GitHub Desktop.
proof that mlock doesn't prevent dirty writeback
This file contains hidden or 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
$cat test.cc | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define CHECK(x) do { if (!(x)) { perror(#x); abort(); }} while (0) | |
int main() { | |
const size_t SZ = 100ul * 1024 * 1024; | |
int fd = open("test.bin", O_CREAT | O_RDWR, 0666); | |
CHECK(fd > 0); | |
CHECK(ftruncate(fd, SZ) == 0); | |
void* m = mmap(0, SZ, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0); | |
CHECK(m); | |
CHECK(mlock(m, SZ) == 0); | |
memset(m, 0x42, SZ); | |
for(;;) { | |
char cmd[64]; | |
sprintf(cmd,"cat /proc/%d/smaps | grep test.bin -A8 | grep Private_; echo", getpid()); | |
system(cmd); | |
sleep(5); | |
} | |
} | |
$ make test && ./test | |
g++ test.cc -o test | |
Private_Clean: 0 kB | |
Private_Dirty: 102400 kB | |
Private_Clean: 0 kB | |
Private_Dirty: 102400 kB | |
Private_Clean: 102400 kB | |
Private_Dirty: 0 kB # Writeback happened at this point |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment