xref: /freebsd/tools/regression/gaithrstress/gaithrstress.c (revision a8089ea5aee578e08acab2438e82fc9a9ae50ed8)
1 /*-
2  * Copyright (c) 2004 Brian Fundakowski Feldman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30 
31 #include <netinet/in.h>
32 
33 #include <err.h>
34 #include <netdb.h>
35 #include <pthread.h>
36 #include <resolv.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 /* Per-thread struct containing all important data. */
44 struct worker {
45 	pthread_t w_thread;			     /* self */
46 	uintmax_t w_lookup_success, w_lookup_failure;   /* getaddrinfo stats */
47 	struct timespec w_max_lookup_time;
48 };
49 
50 static volatile int workers_stop = 0;
51 static double max_random_sleep = 1.0;
52 static char **randwords;
53 static size_t nrandwords;
54 static const struct addrinfo *hints, hintipv4only = { .ai_family = AF_INET };
55 
56 /*
57  * We don't have good random(3)-type functions that are thread-safe,
58  * unfortunately.
59  */
60 static u_int32_t
61 my_arc4random_r(void)
62 {
63 	static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
64 	u_int32_t ret;
65 
66 	(void)pthread_mutex_lock(&mymutex);
67 	ret = arc4random();
68 	(void)pthread_mutex_unlock(&mymutex);
69 	return (ret);
70 }
71 
72 static void
73 randomsleep(double max_sleep_sec)
74 {
75 	struct timespec slptime = { 0, 0 };
76 	double rndsleep;
77 
78 	rndsleep = (double)my_arc4random_r() / 4294967296.0 * max_sleep_sec;
79 	while (rndsleep >= 1.0) {
80 		slptime.tv_sec++;
81 		rndsleep -= 1.0;
82 	}
83 	slptime.tv_nsec = rndsleep * 1e9;
84 	(void)nanosleep(&slptime, NULL);
85 }
86 
87 /*
88  * Start looking up arbitrary hostnames and record the successes/failures.
89  * Between lookups, sleep a random amount of time to make sure threads
90  * stay well out of synchronization.
91  *
92  * Host name:	part		probability
93  *		----		-----------
94  *		www.		1/2
95  *		random word	always, equal
96  *		random word	1/3, equal
97  *		.(net|com|org)	equal
98  */
99 static void *
100 work(void *arg)
101 {
102 	struct worker *w = arg;
103 
104 	/* Turn off domain name list searching as much as possible. */
105 	if (_res.options & RES_INIT || res_init() == 0)
106 		_res.options &= ~RES_DNSRCH;
107 	do {
108 		const char *suffixes[] = { "net", "com", "org" };
109 		const size_t nsuffixes = sizeof(suffixes) / sizeof(suffixes[0]);
110 		struct timespec ts_begintime, ts_total;
111 		struct addrinfo *res;
112 		char *hostname;
113 		int error;
114 
115 		randomsleep(max_random_sleep);
116 		if (asprintf(&hostname, "%s%s%s.%s",
117 		    (my_arc4random_r() % 2) == 0 ? "www." : "",
118 		    randwords[my_arc4random_r() % nrandwords],
119 		    (my_arc4random_r() % 3) == 0 ?
120 		    randwords[my_arc4random_r() % nrandwords] : "",
121 		    suffixes[my_arc4random_r() % nsuffixes]) == -1)
122 			continue;
123 		(void)clock_gettime(CLOCK_REALTIME, &ts_begintime);
124 		error = getaddrinfo(hostname, NULL, hints, &res);
125 		(void)clock_gettime(CLOCK_REALTIME, &ts_total);
126 		ts_total.tv_sec -= ts_begintime.tv_sec;
127 		ts_total.tv_nsec -= ts_begintime.tv_nsec;
128 		if (ts_total.tv_nsec < 0) {
129 			ts_total.tv_sec--;
130 			ts_total.tv_nsec += 1000000000;
131 		}
132 		if (ts_total.tv_sec > w->w_max_lookup_time.tv_sec ||
133 		    (ts_total.tv_sec == w->w_max_lookup_time.tv_sec &&
134 		    ts_total.tv_nsec > w->w_max_lookup_time.tv_sec))
135 			w->w_max_lookup_time = ts_total;
136 		free(hostname);
137 		if (error == 0) {
138 			w->w_lookup_success++;
139 			freeaddrinfo(res);
140 		} else {
141 			w->w_lookup_failure++;
142 		}
143 	} while (!workers_stop);
144 
145 	pthread_exit(NULL);
146 }
147 
148 int
149 dowordfile(const char *fname)
150 {
151 	FILE *fp;
152 	char newword[64];
153 	size_t n;
154 
155 	fp = fopen(fname, "r");
156 	if (fp == NULL)
157 		return (-1);
158 	nrandwords = 0;
159 	while (fgets(newword, sizeof(newword), fp) != NULL)
160 		nrandwords++;
161 	if (ferror(fp) || fseek(fp, 0, SEEK_SET) != 0)
162 		goto fail;
163 	randwords = calloc(nrandwords, sizeof(char *));
164 	if (randwords == NULL)
165 		goto fail;
166 	n = nrandwords;
167 	nrandwords = 0;
168 	while (fgets(newword, sizeof(newword), fp) != NULL) {
169 		newword[strcspn(newword, "\r\n")] = '\0';
170 		randwords[nrandwords] = strdup(newword);
171 		if (randwords[nrandwords] == NULL)
172 			err(1, "reading words file");
173 		if (++nrandwords == n)
174 			break;
175 	}
176 	nrandwords = n;
177 	fclose(fp);
178 	return (0);
179 fail:
180 	fclose(fp);
181 	return (-1);
182 }
183 
184 int
185 main(int argc, char **argv) {
186 	unsigned long nworkers = 1;
187 	struct worker *workers;
188 	size_t i;
189 	char waiting[3], *send, *wordfile = "/usr/share/dict/words";
190 	int ch;
191 
192 	if (getprogname() == NULL)
193 		setprogname(argv[0]);
194 	printf("%s: threaded stress-tester for getaddrinfo(3)\n",
195 	    getprogname());
196 	printf("(c) 2004 Brian Feldman <green@FreeBSD.org>\n");
197 	while ((ch = getopt(argc, argv, "4s:t:w:")) != -1) {
198 		switch (ch) {
199 		case '4':
200 			hints = &hintipv4only;
201 			break;
202 		case 's':
203 			max_random_sleep = strtod(optarg, &send);
204 			if (*send != '\0')
205 				goto usage;
206 			break;
207 		case 't':
208 			nworkers = strtoul(optarg, &send, 0);
209 			if (*send != '\0')
210 				goto usage;
211 			break;
212 		case 'w':
213 			wordfile = optarg;
214 			break;
215 		default:
216 usage:
217 			fprintf(stderr, "usage: %s [-4] [-s sleep] "
218 			    "[-t threads] [-w wordfile]\n", getprogname());
219 			exit(2);
220 		}
221 	}
222 	argc -= optind;
223 	argv += optind;
224 
225 	if (nworkers < 1 || nworkers != (size_t)nworkers)
226 		goto usage;
227 	if (dowordfile(wordfile) == -1)
228 		err(1, "reading word file %s", wordfile);
229 	if (nrandwords < 1)
230 		errx(1, "word file %s did not have >0 words", wordfile);
231 	printf("Read %zu random words from %s.\n", nrandwords, wordfile);
232 	workers = calloc(nworkers, sizeof(*workers));
233 	if (workers == NULL)
234 		err(1, "allocating workers");
235 	printf("Intra-query delay time is from 0 to %g seconds (random).\n",
236 	    max_random_sleep);
237 
238 	printf("Starting %lu worker%.*s: ", nworkers, nworkers > 1, "s");
239 	fflush(stdout);
240 	for (i = 0; i < nworkers; i++) {
241 		if (pthread_create(&workers[i].w_thread, NULL, work,
242 		    &workers[i]) != 0)
243 			err(1, "creating worker %zu", i);
244 		printf("%zu%s", i, i == nworkers - 1 ? ".\n" : ", ");
245 		fflush(stdout);
246 	}
247 
248 	printf("<Press enter key to end test.>\n");
249 	(void)fgets(waiting, sizeof(waiting), stdin);
250 	workers_stop = 1;
251 
252 	printf("Stopping %lu worker%.*s: ", nworkers, nworkers > 1, "s");
253 	fflush(stdout);
254 	for (i = 0; i < nworkers; i++) {
255 		pthread_join(workers[i].w_thread, NULL);
256 		printf("%zu%s", i, i == nworkers - 1 ? ".\n" : ", ");
257 		fflush(stdout);
258 	}
259 
260 	printf("%-10s%-20s%-20s%-29s\n", "Worker", "Successful GAI",
261 	    "Failed GAI", "Max resolution time (M:SS*)");
262 	printf("%-10s%-20s%-20s%-29s\n", "------", "--------------",
263 	    "----------", "---------------------------");
264 	for (i = 0; i < nworkers; i++) {
265 		printf("%-10zu%-20ju%-20ju%ld:%s%.2f\n", i,
266 		    workers[i].w_lookup_success, workers[i].w_lookup_failure,
267 		    workers[i].w_max_lookup_time.tv_sec / 60,
268 		    workers[i].w_max_lookup_time.tv_sec % 60 < 10 ? "0" : "",
269 		    (double)(workers[i].w_max_lookup_time.tv_sec % 60) +
270 		    (double)workers[i].w_max_lookup_time.tv_nsec / 1e9);
271 	}
272 
273 	exit(0);
274 }
275