1 /* $NetBSD: h_stresscli.c,v 1.9 2011/06/26 13:17:36 christos Exp $ */
4 #include <sys/atomic.h>
5 #include <sys/sysctl.h>
7 #include <sys/socket.h>
9 #include <netinet/in.h>
20 #include <rump/rump_syscalls.h>
21 #include <rump/rumpclient.h>
23 static unsigned int syscalls
, bindcalls
;
25 static volatile sig_atomic_t doquit
;
34 static const int hostnamemib
[] = { CTL_KERN
, KERN_HOSTNAME
};
35 static char hostnamebuf
[128];
36 #define HOSTNAMEBASE "rumpclient"
44 struct sockaddr_in sin
;
46 int port
= (int)(uintptr_t)arg
;
49 memset(&sin
, 0, sizeof(sin
));
50 sin
.sin_family
= AF_INET
;
51 sin
.sin_len
= sizeof(sin
);
52 sin
.sin_port
= htons(port
);
57 s
= rump_sys_socket(PF_INET
, SOCK_STREAM
, 0);
60 atomic_inc_uint(&syscalls
);
62 fd
= rump_sys_open("/dev/null", O_RDWR
);
63 atomic_inc_uint(&syscalls
);
69 if (rump_sys_setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
,
74 * we don't really know when the kernel handles our disconnect,
75 * so be soft about about the failure in case of a killer client
77 if (rump_sys_bind(s
, (struct sockaddr
*)&sin
, sizeof(sin
))==-1) {
79 err(1, "bind to port %d failed",
82 atomic_inc_uint(&bindcalls
);
84 atomic_inc_uint(&syscalls
);
89 if (rump_sys___sysctl(hostnamemib
, __arraycount(hostnamemib
),
90 buf
, &blen
, NULL
, 0) == -1)
92 if (strncmp(buf
, hostnamebuf
, sizeof(HOSTNAMEBASE
)-1) != 0)
93 errx(1, "hostname (%s/%s) mismatch", buf
, hostnamebuf
);
94 atomic_inc_uint(&syscalls
);
99 pidi
= rump_sys_getpid();
103 errx(1, "mypid mismatch");
104 atomic_inc_uint(&syscalls
);
109 if (rump_sys_write(fd
, buf
, 16) != 16)
110 err(1, "write /dev/null");
111 atomic_inc_uint(&syscalls
);
115 atomic_inc_uint(&syscalls
);
117 atomic_inc_uint(&syscalls
);
123 /* Stress with max 32 clients, 8 threads each (256 concurrent threads) */
128 main(int argc
, char *argv
[])
130 pthread_t pt
[NTHR
-1];
138 if (argc
!= 2 && argc
!= 3)
139 errx(1, "need roundcount");
142 if (strcmp(argv
[2], "kill") != 0)
143 errx(1, "optional 3rd param must be kill");
150 signal(SIGUSR1
, signaali
);
152 memset(clis
, 0, sizeof(clis
));
153 for (rounds
= 1; rounds
< atoi(argv
[1])*10; rounds
++) {
154 while (ncli
< NCLI
) {
155 switch ((apid
= fork())) {
157 err(1, "fork failed");
159 if (rumpclient_init() == -1)
160 err(1, "rumpclient init");
162 mypid
= rump_sys_getpid();
163 sprintf(hostnamebuf
, HOSTNAMEBASE
"%d", mypid
);
164 if (rump_sys___sysctl(hostnamemib
,
165 __arraycount(hostnamemib
), NULL
, NULL
,
166 hostnamebuf
, strlen(hostnamebuf
)+1) == -1)
167 err(1, "sethostname");
169 for (j
= 0; j
< NTHR
-1; j
++) {
170 myport
= i
*NCLI
+ j
+2;
171 if (pthread_create(&pt
[j
], NULL
,
173 (void*)(uintptr_t)myport
) !=0 )
174 err(1, "pthread create");
177 client((void *)(uintptr_t)myport
);
178 for (j
= 0; j
< NTHR
-1; j
++)
179 pthread_join(pt
[j
], NULL
);
181 fprintf(stderr
, "done %d\n", syscalls
);
194 kill(clis
[i
], thesig
);
196 apid
= wait(&status
);
198 errx(1, "wanted pid %d, got %d\n", clis
[i
], apid
);
201 if (thesig
== SIGUSR1
) {
202 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
203 fprintf(stderr
, "child died with 0x%x\n",
208 if (!WIFSIGNALED(status
) || WTERMSIG(status
) != thesig
){
209 fprintf(stderr
, "child died with 0x%x\n",
216 for (i
= 0; i
< NCLI
; i
++)
218 kill(clis
[i
], SIGKILL
);