| Commit | Line | Data |
|---|---|---|
| d921454f MD |
1 | /* |
| 2 | * TUXLOAD.C | |
| 3 | * | |
| 4 | * (c)Copyright 2012 Antonio Huete Jimenez <tuxillo@quantumachine.net>, | |
| 5 | * this code is hereby placed in the public domain. | |
| 6 | * | |
| 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. | |
| 10 | * | |
| 11 | * The purpose is to stress the VM system. | |
| 12 | */ | |
| 13 | #include <stdio.h> | |
| 14 | #include <unistd.h> | |
| 15 | #include <fcntl.h> | |
| 16 | #include <stdlib.h> | |
| 17 | #include <sys/mman.h> | |
| 18 | #include <sys/stat.h> | |
| 19 | #include <err.h> | |
| 20 | ||
| 21 | #define NFILES 500 | |
| 22 | ||
| 23 | static void randomfill(int fd); | |
| 24 | ||
| 25 | int | |
| 26 | main(int argc, char *argv[]) | |
| 27 | { | |
| 28 | ||
| 29 | int i; | |
| 30 | size_t size; | |
| 31 | long jump; | |
| 32 | int fd[NFILES]; | |
| 33 | struct stat st[NFILES]; | |
| 34 | char name[128]; | |
| 35 | char *p[NFILES]; | |
| 36 | ||
| 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) | |
| 41 | err(1, "open"); | |
| 42 | randomfill(fd[i]); | |
| 43 | } | |
| 44 | ||
| 45 | if ((fstat(fd[i], &st[i])) == -1) | |
| 46 | err(1, "fstat"); | |
| 47 | ||
| 48 | size = st[i].st_size; | |
| 49 | p[i] = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0); | |
| 50 | if (p[i] == MAP_FAILED) | |
| 51 | err(1, "mmap"); | |
| 52 | ||
| 53 | for (jump = 0; jump < size; jump += 65535) { | |
| 54 | p[i][jump] = jump + i; | |
| 55 | } | |
| 45c97380 MD |
56 | |
| 57 | /* | |
| 58 | * This is a funny bug. MS_SYNC and 0 are reversed (i.e. | |
| 59 | * the msync() call wasn't written correctly), but the | |
| 60 | * broken msync() leads to a heavier VM load as a veritible | |
| 61 | * ton of dirty file-backed pages wind up accumulating in | |
| 62 | * the memory maps. | |
| 63 | * | |
| 64 | * So we leave it as is for now. | |
| 65 | */ | |
| d921454f MD |
66 | if ((msync(*p, MS_SYNC, 0)) == -1) { |
| 67 | printf("%s: %d %p\n", name, i, *p); | |
| 68 | err(1, "msync"); | |
| 69 | ||
| 70 | } | |
| 71 | } | |
| d921454f | 72 | return 0; |
| d921454f MD |
73 | } |
| 74 | ||
| 75 | static void | |
| 76 | randomfill(int fd) | |
| 77 | { | |
| 78 | char buf[32768]; | |
| 79 | int i; | |
| 80 | ||
| 81 | srandomdev(); | |
| 82 | for (i = 0; i < 32768; ++i) | |
| 83 | buf[i] = random(); | |
| 45c97380 | 84 | for (i = 0; i < 8192; i += 32) /* 8MB */ |
| d921454f MD |
85 | write(fd, buf, 32768); |
| 86 | fsync(fd); | |
| 87 | lseek(fd, 0L, 0); | |
| 88 | } |