xref: /minix3/tests/rump/rumpkern/h_client/h_stresscli.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: h_stresscli.c,v 1.9 2011/06/26 13:17:36 christos Exp $	*/
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc #include <sys/types.h>
4*11be35a1SLionel Sambuc #include <sys/atomic.h>
5*11be35a1SLionel Sambuc #include <sys/sysctl.h>
6*11be35a1SLionel Sambuc #include <sys/wait.h>
7*11be35a1SLionel Sambuc #include <sys/socket.h>
8*11be35a1SLionel Sambuc 
9*11be35a1SLionel Sambuc #include <netinet/in.h>
10*11be35a1SLionel Sambuc 
11*11be35a1SLionel Sambuc #include <err.h>
12*11be35a1SLionel Sambuc #include <fcntl.h>
13*11be35a1SLionel Sambuc #include <pthread.h>
14*11be35a1SLionel Sambuc #include <stdio.h>
15*11be35a1SLionel Sambuc #include <stdlib.h>
16*11be35a1SLionel Sambuc #include <string.h>
17*11be35a1SLionel Sambuc #include <unistd.h>
18*11be35a1SLionel Sambuc #include <signal.h>
19*11be35a1SLionel Sambuc 
20*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
21*11be35a1SLionel Sambuc #include <rump/rumpclient.h>
22*11be35a1SLionel Sambuc 
23*11be35a1SLionel Sambuc static unsigned int syscalls, bindcalls;
24*11be35a1SLionel Sambuc static pid_t mypid;
25*11be35a1SLionel Sambuc static volatile sig_atomic_t doquit;
26*11be35a1SLionel Sambuc 
27*11be35a1SLionel Sambuc static void
signaali(int sig)28*11be35a1SLionel Sambuc signaali(int sig)
29*11be35a1SLionel Sambuc {
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc 	doquit = 1;
32*11be35a1SLionel Sambuc }
33*11be35a1SLionel Sambuc 
34*11be35a1SLionel Sambuc static const int hostnamemib[] = { CTL_KERN, KERN_HOSTNAME };
35*11be35a1SLionel Sambuc static char hostnamebuf[128];
36*11be35a1SLionel Sambuc #define HOSTNAMEBASE "rumpclient"
37*11be35a1SLionel Sambuc 
38*11be35a1SLionel Sambuc static int iskiller;
39*11be35a1SLionel Sambuc 
40*11be35a1SLionel Sambuc static void *
client(void * arg)41*11be35a1SLionel Sambuc client(void *arg)
42*11be35a1SLionel Sambuc {
43*11be35a1SLionel Sambuc 	char buf[256];
44*11be35a1SLionel Sambuc 	struct sockaddr_in sin;
45*11be35a1SLionel Sambuc 	size_t blen;
46*11be35a1SLionel Sambuc 	int port = (int)(uintptr_t)arg;
47*11be35a1SLionel Sambuc 	int s, fd, x;
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc 	memset(&sin, 0, sizeof(sin));
50*11be35a1SLionel Sambuc 	sin.sin_family = AF_INET;
51*11be35a1SLionel Sambuc 	sin.sin_len = sizeof(sin);
52*11be35a1SLionel Sambuc 	sin.sin_port = htons(port);
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc 	while (!doquit) {
55*11be35a1SLionel Sambuc 		pid_t pidi;
56*11be35a1SLionel Sambuc 		blen = sizeof(buf);
57*11be35a1SLionel Sambuc 		s = rump_sys_socket(PF_INET, SOCK_STREAM, 0);
58*11be35a1SLionel Sambuc 		if (s == -1)
59*11be35a1SLionel Sambuc 			err(1, "socket");
60*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
61*11be35a1SLionel Sambuc 
62*11be35a1SLionel Sambuc 		fd = rump_sys_open("/dev/null", O_RDWR);
63*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc 		if (doquit)
66*11be35a1SLionel Sambuc 			goto out;
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 		x = 1;
69*11be35a1SLionel Sambuc 		if (rump_sys_setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
70*11be35a1SLionel Sambuc 		    &x, sizeof(x)) == -1)
71*11be35a1SLionel Sambuc 			err(1, "reuseaddr");
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 		/*
74*11be35a1SLionel Sambuc 		 * we don't really know when the kernel handles our disconnect,
75*11be35a1SLionel Sambuc 		 * so be soft about about the failure in case of a killer client
76*11be35a1SLionel Sambuc 		 */
77*11be35a1SLionel Sambuc 		if (rump_sys_bind(s, (struct sockaddr*)&sin, sizeof(sin))==-1) {
78*11be35a1SLionel Sambuc 			if (!iskiller)
79*11be35a1SLionel Sambuc 				err(1, "bind to port %d failed",
80*11be35a1SLionel Sambuc 				    ntohs(sin.sin_port));
81*11be35a1SLionel Sambuc 		} else {
82*11be35a1SLionel Sambuc 			atomic_inc_uint(&bindcalls);
83*11be35a1SLionel Sambuc 		}
84*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
85*11be35a1SLionel Sambuc 
86*11be35a1SLionel Sambuc 		if (doquit)
87*11be35a1SLionel Sambuc 			goto out;
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc 		if (rump_sys___sysctl(hostnamemib, __arraycount(hostnamemib),
90*11be35a1SLionel Sambuc 		    buf, &blen, NULL, 0) == -1)
91*11be35a1SLionel Sambuc 			err(1, "sysctl");
92*11be35a1SLionel Sambuc 		if (strncmp(buf, hostnamebuf, sizeof(HOSTNAMEBASE)-1) != 0)
93*11be35a1SLionel Sambuc 			errx(1, "hostname (%s/%s) mismatch", buf, hostnamebuf);
94*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
95*11be35a1SLionel Sambuc 
96*11be35a1SLionel Sambuc 		if (doquit)
97*11be35a1SLionel Sambuc 			goto out;
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc 		pidi = rump_sys_getpid();
100*11be35a1SLionel Sambuc 		if (pidi == -1)
101*11be35a1SLionel Sambuc 			err(1, "getpid");
102*11be35a1SLionel Sambuc 		if (pidi != mypid)
103*11be35a1SLionel Sambuc 			errx(1, "mypid mismatch");
104*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 		if (doquit)
107*11be35a1SLionel Sambuc 			goto out;
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 		if (rump_sys_write(fd, buf, 16) != 16)
110*11be35a1SLionel Sambuc 			err(1, "write /dev/null");
111*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
112*11be35a1SLionel Sambuc 
113*11be35a1SLionel Sambuc  out:
114*11be35a1SLionel Sambuc 		rump_sys_close(fd);
115*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
116*11be35a1SLionel Sambuc 		rump_sys_close(s);
117*11be35a1SLionel Sambuc 		atomic_inc_uint(&syscalls);
118*11be35a1SLionel Sambuc 	}
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 	return NULL;
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc /* Stress with max 32 clients, 8 threads each (256 concurrent threads) */
124*11be35a1SLionel Sambuc #define NCLI 32
125*11be35a1SLionel Sambuc #define NTHR 8
126*11be35a1SLionel Sambuc 
127*11be35a1SLionel Sambuc int
main(int argc,char * argv[])128*11be35a1SLionel Sambuc main(int argc, char *argv[])
129*11be35a1SLionel Sambuc {
130*11be35a1SLionel Sambuc 	pthread_t pt[NTHR-1];
131*11be35a1SLionel Sambuc 	pid_t clis[NCLI];
132*11be35a1SLionel Sambuc 	pid_t apid;
133*11be35a1SLionel Sambuc 	int ncli = 0;
134*11be35a1SLionel Sambuc 	int i = 0, j;
135*11be35a1SLionel Sambuc 	int status, thesig;
136*11be35a1SLionel Sambuc 	int rounds, myport;
137*11be35a1SLionel Sambuc 
138*11be35a1SLionel Sambuc 	if (argc != 2 && argc != 3)
139*11be35a1SLionel Sambuc 		errx(1, "need roundcount");
140*11be35a1SLionel Sambuc 
141*11be35a1SLionel Sambuc 	if (argc == 3) {
142*11be35a1SLionel Sambuc 		if (strcmp(argv[2], "kill") != 0)
143*11be35a1SLionel Sambuc 			errx(1, "optional 3rd param must be kill");
144*11be35a1SLionel Sambuc 		thesig = SIGKILL;
145*11be35a1SLionel Sambuc 		iskiller = 1;
146*11be35a1SLionel Sambuc 	} else {
147*11be35a1SLionel Sambuc 		thesig = SIGUSR1;
148*11be35a1SLionel Sambuc 	}
149*11be35a1SLionel Sambuc 
150*11be35a1SLionel Sambuc 	signal(SIGUSR1, signaali);
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc 	memset(clis, 0, sizeof(clis));
153*11be35a1SLionel Sambuc 	for (rounds = 1; rounds < atoi(argv[1])*10; rounds++) {
154*11be35a1SLionel Sambuc 		while (ncli < NCLI) {
155*11be35a1SLionel Sambuc 			switch ((apid = fork())) {
156*11be35a1SLionel Sambuc 			case -1:
157*11be35a1SLionel Sambuc 				err(1, "fork failed");
158*11be35a1SLionel Sambuc 			case 0:
159*11be35a1SLionel Sambuc 				if (rumpclient_init() == -1)
160*11be35a1SLionel Sambuc 					err(1, "rumpclient init");
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc 				mypid = rump_sys_getpid();
163*11be35a1SLionel Sambuc 				sprintf(hostnamebuf, HOSTNAMEBASE "%d", mypid);
164*11be35a1SLionel Sambuc 				if (rump_sys___sysctl(hostnamemib,
165*11be35a1SLionel Sambuc 				    __arraycount(hostnamemib), NULL, NULL,
166*11be35a1SLionel Sambuc 				    hostnamebuf, strlen(hostnamebuf)+1) == -1)
167*11be35a1SLionel Sambuc 					err(1, "sethostname");
168*11be35a1SLionel Sambuc 
169*11be35a1SLionel Sambuc 				for (j = 0; j < NTHR-1; j++) {
170*11be35a1SLionel Sambuc 					myport = i*NCLI + j+2;
171*11be35a1SLionel Sambuc 					if (pthread_create(&pt[j], NULL,
172*11be35a1SLionel Sambuc 					    client,
173*11be35a1SLionel Sambuc 					    (void*)(uintptr_t)myport) !=0 )
174*11be35a1SLionel Sambuc 						err(1, "pthread create");
175*11be35a1SLionel Sambuc 				}
176*11be35a1SLionel Sambuc 				myport = i*NCLI+1;
177*11be35a1SLionel Sambuc 				client((void *)(uintptr_t)myport);
178*11be35a1SLionel Sambuc 				for (j = 0; j < NTHR-1; j++)
179*11be35a1SLionel Sambuc 					pthread_join(pt[j], NULL);
180*11be35a1SLionel Sambuc 				membar_consumer();
181*11be35a1SLionel Sambuc 				fprintf(stderr, "done %d\n", syscalls);
182*11be35a1SLionel Sambuc 				exit(0);
183*11be35a1SLionel Sambuc 				/* NOTREACHED */
184*11be35a1SLionel Sambuc 			default:
185*11be35a1SLionel Sambuc 				ncli++;
186*11be35a1SLionel Sambuc 				clis[i] = apid;
187*11be35a1SLionel Sambuc 				break;
188*11be35a1SLionel Sambuc 			}
189*11be35a1SLionel Sambuc 
190*11be35a1SLionel Sambuc 			i = (i + 1) % NCLI;
191*11be35a1SLionel Sambuc 		}
192*11be35a1SLionel Sambuc 
193*11be35a1SLionel Sambuc 		usleep(100000);
194*11be35a1SLionel Sambuc 		kill(clis[i], thesig);
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc 		apid = wait(&status);
197*11be35a1SLionel Sambuc 		if (apid != clis[i])
198*11be35a1SLionel Sambuc 			errx(1, "wanted pid %d, got %d\n", clis[i], apid);
199*11be35a1SLionel Sambuc 		clis[i] = 0;
200*11be35a1SLionel Sambuc 		ncli--;
201*11be35a1SLionel Sambuc 		if (thesig == SIGUSR1) {
202*11be35a1SLionel Sambuc 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
203*11be35a1SLionel Sambuc 				fprintf(stderr, "child died with 0x%x\n",
204*11be35a1SLionel Sambuc 				    status);
205*11be35a1SLionel Sambuc 				exit(1);
206*11be35a1SLionel Sambuc 			}
207*11be35a1SLionel Sambuc 		} else {
208*11be35a1SLionel Sambuc 			if (!WIFSIGNALED(status) || WTERMSIG(status) != thesig){
209*11be35a1SLionel Sambuc 				fprintf(stderr, "child died with 0x%x\n",
210*11be35a1SLionel Sambuc 				    status);
211*11be35a1SLionel Sambuc 				exit(1);
212*11be35a1SLionel Sambuc 			}
213*11be35a1SLionel Sambuc 		}
214*11be35a1SLionel Sambuc 	}
215*11be35a1SLionel Sambuc 
216*11be35a1SLionel Sambuc 	for (i = 0; i < NCLI; i++)
217*11be35a1SLionel Sambuc 		if (clis[i])
218*11be35a1SLionel Sambuc 			kill(clis[i], SIGKILL);
219*11be35a1SLionel Sambuc }
220