/*
 * DF-2980 regression sanity: normal profil(2) profiling must still work
 * after the fix (counters must accumulate).
 */
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

static void
spin_300ms(void)
{
	struct timeval a, b;

	gettimeofday(&a, NULL);
	do {
		gettimeofday(&b, NULL);
	} while ((b.tv_sec - a.tv_sec) * 1000000 +
	    (b.tv_usec - a.tv_usec) < 300000);
}

int
main(void)
{
	unsigned short buf[65536];
	unsigned long sum = 0;
	int i;

	memset(buf, 0, sizeof(buf));
	/* window: [main, main+64K) covers main/spin_300ms text */
	if (syscall(SYS_profil, buf, sizeof(buf),
	    (u_long)(uintptr_t)&main, 0x10000) != 0) {
		perror("profil");
		return (1);
	}
	printf("spinning 300ms of user time...\n");
	spin_300ms();
	syscall(SYS_profil, NULL, 0, 0, 0);
	for (i = 0; i < 65536; i++)
		sum += buf[i];
	printf("sample sum = %lu (expect > 0: profiling still works)\n", sum);
	return (sum > 0 ? 0 : 1);
}
