DragonFlyBSD Kernel Audit
DF-2721 / hammer.c
← back to finding ↓ download raw
/*
 * hammer.c - userspace driver for the slabstrand KLD.
 *
 * usage: hammer <cpu> <p|c> <batch> <seconds>
 *
 * pins itself to <cpu> with usched_set(2) and then hammers
 * kern.slabstrand.push (p, allocator cpu) or kern.slabstrand.pop
 * (c, freeing cpu) as fast as possible.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/usched.h>

int
main(int ac, char **av)
{
	int cpu, batch, secs;
	char mode;
	int val;
	size_t len;
	long ops = 0;

	if (ac != 5) {
		fprintf(stderr, "usage: %s cpu p|c batch seconds\n", av[0]);
		exit(1);
	}
	cpu = atoi(av[1]);
	mode = av[2][0];
	batch = atoi(av[3]);
	secs = atoi(av[4]);

	if (usched_set(0, USCHED_SET_CPU, &cpu, sizeof(cpu)) < 0) {
		perror("usched_set");
		exit(1);
	}
	printf("hammer cpu=%d mode=%c batch=%d secs=%d pid=%d\n",
	       cpu, mode, batch, secs, getpid());
	fflush(stdout);

	while (secs-- > 0) {
		int i;
		for (i = 0; i < 200; ++i) {
			val = batch;
			len = 0;
			if (sysctlbyname(mode == 'p' ?
					 "kern.slabstrand.push" :
					 "kern.slabstrand.pop",
					 NULL, &len, &val, sizeof(val)) < 0) {
				perror("sysctlbyname");
				exit(1);
			}
			ops += batch;
		}
		sleep(1);
	}
	printf("hammer cpu=%d mode=%c done ops~%ld\n", cpu, mode, ops);
	return (0);
}