DragonFlyBSD Kernel Audit
DF-2879 / hog.c
← back to finding ↓ download raw
/* DF-2879: anonymous-memory hog. Touch (and keep re-dirtying) more
 * memory than the guest has RAM so the pageout daemon must swap anon
 * pages, exposing whatever the swap registration advertised. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

int main(int argc, char **argv) {
    size_t mb = (argc > 1) ? (size_t)atoll(argv[1]) : 64;
    size_t sz = mb << 20;
    char *p = mmap(NULL, sz, PROT_READ|PROT_WRITE,
                   MAP_ANON|MAP_PRIVATE, -1, 0);
    if (p == MAP_FAILED) { perror("mmap"); return 1; }
    for (size_t i = 0; i < sz; i += 4096)
        p[i] = (char)(i / 4096);
    fprintf(stderr, "HOG: touched %zu MB\n", mb);
    for (int round = 0; round < 600; round++) {
        for (size_t i = 0; i < sz; i += 4096)
            p[i] = (char)(i / 4096 + round);
        usleep(200000);
    }
    return 0;
}