⬢ DragonFlyBSD Kernel Audit
DF-5812 / churn.c
← back to finding ↓ download raw
/*
 * DF-5812 trigger assist: anonymous memory churn to force the pageout
 * daemon to allocate swap blocks in blist order (lowest-first), driving
 * allocations into the phantom tail published by swaponvp() when the
 * swap device's page count is not a multiple of SWB_DMMAX (64 pages).
 *
 * Usage: ./churn <megabytes>
 * Allocates and touches <megabytes> of anonymous memory, holds it for
 * a few seconds so the pageout daemon can evict earlier pages to swap,
 * then exits.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	size_t mb, i;
	size_t pages;
	volatile unsigned char *p;

	if (argc != 2) {
		fprintf(stderr, "usage: %s <megabytes>\n", argv[0]);
		return (2);
	}
	mb = (size_t)strtoul(argv[1], NULL, 0);
	pages = mb * 1024 * 1024 / 4096;

	p = malloc(mb * 1024 * 1024);
	if (p == NULL) {
		perror("malloc");
		return (1);
	}
	for (i = 0; i < pages; i++)
		((unsigned char *)p)[i * 4096] = (unsigned char)i;

	printf("churn: touched %zu MB (%zu pages), holding 5s\n",
	       mb, pages);
	fflush(stdout);
	sleep(5);
	free((void *)p);
	printf("churn: done\n");
	return (0);
}