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);
24 static int mmap01(void *);
25 static int mmap02(void *);
26 static int build_files(void);
39 static const struct test {
41 int (*testfn)(void *);
43 { "mmap01 - Massive mmap / msync (flushing all pages)", mmap01 },
44 { "mmap02 - Massive mmap / msync (flushing only specified pages)", mmap02 },
56 if ((build_files()) != 0)
57 err(1, "Failed to create the files");
62 for (i = 0; i < opt_nofiles; i++) {
66 fprintf(stdout, "\rDoing mmap() + msync() [%d/%d] ", i+1, opt_nofiles);
69 pp[i] = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
70 if (pp[i] == MAP_FAILED)
73 for (jump = 0; jump < size; jump += 65535) {
74 pp[i][jump] = jump + i;
82 if ((msync(pp[i], len, MS_SYNC)) == -1) {
83 printf("fd %d %p\n", fd[i], pp[i]);
106 const struct test *tp;
108 printf("tuxload: [-v] [-c count] [-m megs_per_file] [-n no_of_files] [-t test_to_run] \n"
109 "Available tests: \n");
111 for (tp = testlist; tp->testfn != NULL; tp++)
112 printf("\t%s\n", tp->testdesc);
117 main(int argc, char *argv[])
122 opt_verbose = opt_testno = 0;
129 while ((c = getopt(argc, argv, "n:t:m:c:v")) != -1) {
135 opt_nofiles = (int)strtol(optarg, NULL, 0);
138 opt_testno = (int)strtol(optarg, NULL, 0);
142 opt_mbfile = (int)strtol(optarg, NULL, 0);
145 opt_count = (int)strtol(optarg, NULL, 0);
160 st = malloc(opt_nofiles * sizeof(*st));
161 fd = malloc(opt_nofiles * sizeof(*fd));
162 pp = malloc(opt_nofiles * sizeof(*pp));
164 while (opt_count-- || forever)
165 testlist[opt_testno].testfn(0);
177 for (i = 0, error = 0; i < opt_nofiles; i++) {
178 snprintf(name, 128, "tmpfiles/file%d", i);
179 if ((fd[i] = open(name, O_RDWR)) < 1) {
180 if ((fd[i] = open(name, O_RDWR | O_CREAT, 0644)) < 1) {
186 if ((fstat(fd[i], &st[i])) == -1) {
191 fprintf(stdout, "\rFile creation, random data filled [%d/%d] ", i+1, opt_nofiles);
207 tot = opt_mbfile * 1024L;
208 for (i = 0; i < 32768; ++i)
210 for (i = 0; i < tot; i += 32) /* 8MB by default */
211 write(fd, buf, 32768);