/*
 * dockrace.c -- DF-4823 racer: tight kldload(2)/kldunload(2) loop against the
 * deferred AcpiOsExecute(acpi_dock_attach_later) work queued by every
 * acpi_dock insert (96 fake _EJD dependents from ssdt_dock.dsl).
 *
 * Build:  cc -O2 -o dockrace dockrace.c
 * Run:    ./dockrace [iterations]
 * Exit:   0 = survived; !=0/panic = hit (kernel usually panics first).
 */
#include <sys/param.h>
#include <sys/linker.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	long n = (argc > 1) ? strtol(argv[1], NULL, 10) : 2000;
	long i;
	int id;

	for (i = 0; i < n; i++) {
		id = kldload("acpi_dock");
		if (id >= 0)
			kldunload(id);
		else
			usleep(1000);	/* module wedged? brief backoff */
		if ((i % 100) == 0) {
			printf("iter %ld\n", i);
			fflush(stdout);
		}
	}
	printf("dockrace: survived %ld kldload/kldunload cycles\n", n);
	return (0);
}
