1/*
2 * File: test_waitqlocktry_12053360.c
3 * Test Description: This is a load test for wait queues in the kernel. It is designed to excercise the locking of threads and
4 * wait queues in the face of timer expirations. The overall runtime is limited to 90 secs.
5 * In case of inconsistency we have found to be panic'ing within the first 15 secs.
6 * Radar: <rdar://problem/12053360> <rdar://problem/12323652>
7 */
8
9#include <unistd.h>
10#include <stdio.h>
11#include <mach/semaphore.h>
12#include <time.h>
13#include <stdlib.h>
14#include <pthread.h>
15
16#define MAX_TEST_RUN_TIME  90
17uint32_t test_usleep_max;
18
19void*
20test_thread(void *arg __unused)
21{
22	while (1) {
23		usleep(random() % test_usleep_max);
24	}
25
26	return NULL;
27}
28
29
30int
31main(int argc, const char **argv)
32{
33	pthread_t *threads;
34	uint32_t nthreads, i;
35	int tmp, result;
36
37	if (argc != 3) {
38		printf("Usage: %s <max sleep in usecs> <nthreads>\n", argv[0]);
39		printf("Currently defaulting to 100us and 100 threads\n");
40		test_usleep_max = 100;
41		nthreads = 100;
42	}else {
43
44		tmp = atoi(argv[1]);
45		if (tmp < 0) {
46			printf("Sleep time must be > 0.\n");
47			exit(1);
48		}
49
50		test_usleep_max = (uint32_t)tmp;
51
52		tmp = atoi(argv[2]);
53		if (tmp < 0) {
54			printf("Num threads must be > 0.\n");
55			exit(1);
56		}
57		nthreads = (uint32_t)tmp;
58	}
59	threads = (pthread_t*)malloc(nthreads * sizeof(pthread_t));
60	if (threads == NULL) {
61		printf("Failed to allocate thread array.\n");
62		exit(1);
63	}
64
65	printf("Creating %u threads with a max sleep time of %uusec.\n", nthreads, test_usleep_max);
66	srand(time(NULL));
67	for (i = 0; i < nthreads; i++) {
68		result = pthread_create(&threads[i], NULL, test_thread, NULL);
69		if (result != 0) {
70			printf("Failed to allocate thread.\n");
71			exit(1);
72		}
73	}
74
75	printf("Main thread sleeping for %d secs\n", MAX_TEST_RUN_TIME);
76	sleep(MAX_TEST_RUN_TIME);
77	printf("Success. Exiting..\n");
78	return 0;
79}
80