Skip to content

Instantly share code, notes, and snippets.

@tony612
Last active May 28, 2020 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tony612/f5c6d227a3c0d4eec43ee933b5f192a3 to your computer and use it in GitHub Desktop.
Save tony612/f5c6d227a3c0d4eec43ee933b5f192a3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
int main(void) {
size_t pagesize = getpagesize();
printf("System page size: %zu bytes\n", pagesize);
for (size_t i = 0; i < 1000; i++) {
char *region = mmap(
NULL,
pagesize * (i + 1),
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, // to a private block of hardware memory
-1,
0
);
if (region == MAP_FAILED) {
perror("Could not mmap");
return 1;
}
// Trigger page fault
for (size_t j = 0; j < pagesize * (i + 1); j++) {
region[j] = 'a';
}
int unmap_result = munmap(region, pagesize * (i + 1));
if (unmap_result == -1) {
perror("Could not munmap");
return 1;
}
sleep(1);
}
return 0;
}
pidstat -r 1 -e ./a.out
Linux 4.15.0-1044-aws (xxx) 08/12/19 _x86_64_ (16 CPU)
System page size: 4096 bytes
06:42:23 UID PID minflt/s majflt/s VSZ RSS %MEM Command
06:42:24 1000 1192 33.00 0.00 4508 1472 0.00 a.out
06:42:25 1000 1192 2.00 0.00 4508 1536 0.00 a.out
06:42:26 1000 1192 3.00 0.00 4508 1536 0.00 a.out
06:42:27 1000 1192 4.00 0.00 4508 1536 0.00 a.out
06:42:28 1000 1192 5.00 0.00 4508 1536 0.00 a.out
06:42:29 1000 1192 6.00 0.00 4508 1536 0.00 a.out
06:42:30 1000 1192 7.00 0.00 4508 1536 0.00 a.out
06:42:31 1000 1192 8.00 0.00 4508 1536 0.00 a.out
06:42:32 1000 1192 9.00 0.00 4508 1536 0.00 a.out
06:42:33 1000 1192 10.00 0.00 4508 1536 0.00 a.out
06:42:34 1000 1192 11.00 0.00 4508 1536 0.00 a.out
06:42:35 1000 1192 12.00 0.00 4508 1536 0.00 a.out
06:42:36 1000 1192 13.00 0.00 4508 1536 0.00 a.out
06:42:37 1000 1192 14.00 0.00 4508 1536 0.00 a.out
06:42:38 1000 1192 15.00 0.00 4508 1536 0.00 a.out
06:42:39 1000 1192 16.00 0.00 4508 1536 0.00 a.out
06:42:40 1000 1192 17.00 0.00 4508 1536 0.00 a.out
06:42:41 1000 1192 18.00 0.00 4508 1536 0.00 a.out
06:42:42 1000 1192 19.00 0.00 4508 1536 0.00 a.out
06:42:43 1000 1192 20.00 0.00 4508 1536 0.00 a.out
06:42:44 1000 1192 21.00 0.00 4508 1536 0.00 a.out
06:42:45 1000 1192 22.00 0.00 4508 1536 0.00 a.out
06:42:46 1000 1192 23.00 0.00 4508 1536 0.00 a.out
06:42:47 1000 1192 24.00 0.00 4508 1536 0.00 a.out
06:42:48 1000 1192 25.00 0.00 4508 1536 0.00 a.out
06:42:49 1000 1192 26.00 0.00 4508 1536 0.00 a.out
06:42:50 1000 1192 27.00 0.00 4508 1536 0.00 a.out
06:42:51 1000 1192 28.00 0.00 4508 1536 0.00 a.out
06:42:52 1000 1192 29.00 0.00 4508 1536 0.00 a.out
06:42:53 1000 1192 30.00 0.00 4508 1536 0.00 a.out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment