4 * (c)Copyright 2012 Antonio Huete Jimenez <tuxillo@quantumachine.net>,
5 * this code is hereby placed in the public domain.
7 * As a safety the directory 'tmpfiles/' must exist. This program will
8 * create 500 x 8MB files in tmpfiles/*, memory map the files MAP_SHARED,
9 * R+W, make random modifications, and msync() in a loop.
11 * The purpose is to stress the VM system.
23 static void randomfill(int fd);
26 main(int argc, char *argv[])
33 struct stat st[NFILES];
37 for (i = 0; i < NFILES; i++) {
38 snprintf(name, 128, "tmpfiles/file%d", i);
39 if ((fd[i] = open(name, O_RDWR)) < 1) {
40 if ((fd[i] = open(name, O_RDWR | O_CREAT, 0644)) < 1)
44 if ((fstat(fd[i], &st[i])) == -1)
46 fprintf(stdout, "\rFile creation, random data filled [%d/%d] ", i+1, NFILES);
52 for (i = 0; i < NFILES; i++) {
54 fprintf(stdout, "\rDoing mmap() + msync() [%d/%d] ", i+1, NFILES);
56 p[i] = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
57 if (p[i] == MAP_FAILED)
60 for (jump = 0; jump < size; jump += 65535) {
61 p[i][jump] = jump + i;
65 * This is a funny bug. MS_SYNC and 0 are reversed (i.e.
66 * the msync() call wasn't written correctly), but the
67 * broken msync() leads to a heavier VM load as a veritible
68 * ton of dirty file-backed pages wind up accumulating in
71 * So we leave it as is for now.
73 if ((msync(*p, MS_SYNC, 0)) == -1) {
74 printf("%s: %d %p\n", name, i, *p);
89 for (i = 0; i < 32768; ++i)
91 for (i = 0; i < 8192; i += 32) /* 8MB */
92 write(fd, buf, 32768);