Skip to content

Instantly share code, notes, and snippets.

@macdice
Created May 8, 2018 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macdice/325318a77a2e4c7a515c8786faad1dfc to your computer and use it in GitHub Desktop.
Save macdice/325318a77a2e4c7a515c8786faad1dfc to your computer and use it in GitHub Desktop.
POSIX_FADV_DONTNEED troubles
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define BUFFER_SIZE 8192
int
main(int argc, char **argv)
{
char *mapped;
char *buffer;
int fd;
int rc;
char pagevector[1];
/* get some clean data into the page cache */
buffer = malloc(BUFFER_SIZE);
assert(buffer != NULL);
fd = open("my_file", O_RDWR | O_CREAT, 0664);
assert(fd >= 0);
memset(buffer, 0, BUFFER_SIZE);
rc = write(fd, buffer, BUFFER_SIZE);
assert(rc == BUFFER_SIZE);
rc = fsync(fd);
assert(rc == 0);
rc = lseek(fd, 0, SEEK_SET);
assert(rc == 0);
rc = read(fd, buffer, BUFFER_SIZE);
assert(rc == BUFFER_SIZE);
mapped = mmap(NULL, BUFFER_SIZE, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
assert(mapped != NULL);
rc = mincore(mapped, 4096, pagevector);
assert(rc == 0);
assert(pagevector[0] & MINCORE_INCORE);
/* kick it out of the page cache */
rc = posix_fadvise(fd, 0, 8192, POSIX_FADV_DONTNEED);
assert(rc == 0);
rc = mincore(mapped, 4096, pagevector);
assert(rc == 0);
assert(!(pagevector[0] & MINCORE_INCORE)); /* <-- fails. why? */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment