select.c revision 1.3 1 1.3 yamt /* $NetBSD: select.c,v 1.3 2011/11/02 16:49:12 yamt Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2008 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt #define FD_SETSIZE 65536
30 1.1 yamt #include <sys/select.h>
31 1.2 ad #include <sys/atomic.h>
32 1.3 yamt #include <sys/time.h>
33 1.1 yamt
34 1.3 yamt #include <assert.h>
35 1.1 yamt #include <errno.h>
36 1.1 yamt #include <fcntl.h>
37 1.1 yamt #include <pthread.h>
38 1.3 yamt #include <stdint.h>
39 1.1 yamt #include <stdio.h>
40 1.1 yamt #include <stdlib.h>
41 1.1 yamt #include <string.h>
42 1.1 yamt #include <unistd.h>
43 1.1 yamt
44 1.1 yamt #define NPIPE 128
45 1.1 yamt #define NTHREAD 64
46 1.3 yamt #define NBALLS 5
47 1.1 yamt #define VERBOSE 0
48 1.1 yamt
49 1.1 yamt #if !defined(RANDOM_MAX)
50 1.1 yamt #define RANDOM_MAX ((1UL << 31) - 1)
51 1.1 yamt #endif
52 1.1 yamt
53 1.1 yamt int fds[NPIPE][2];
54 1.1 yamt
55 1.3 yamt volatile unsigned count;
56 1.2 ad
57 1.2 ad pthread_barrier_t barrier;
58 1.1 yamt
59 1.1 yamt static void
60 1.1 yamt dowrite(void)
61 1.1 yamt {
62 1.1 yamt char buf[1];
63 1.1 yamt int fd;
64 1.1 yamt int i;
65 1.1 yamt
66 1.1 yamt i = random() % NPIPE;
67 1.1 yamt fd = fds[i][1];
68 1.1 yamt #if VERBOSE
69 1.1 yamt printf("[%p] write %d\n", (void *)pthread_self(), fd);
70 1.1 yamt #endif
71 1.1 yamt if (write(fd, buf, sizeof(buf)) == -1) {
72 1.1 yamt perror("write");
73 1.1 yamt abort();
74 1.1 yamt }
75 1.1 yamt }
76 1.1 yamt
77 1.1 yamt static void *
78 1.1 yamt f(void *dummy)
79 1.1 yamt {
80 1.1 yamt
81 1.2 ad pthread_barrier_wait(&barrier);
82 1.2 ad
83 1.1 yamt for (;;) {
84 1.1 yamt struct timeval to;
85 1.1 yamt fd_set oset;
86 1.1 yamt fd_set set;
87 1.1 yamt int maxfd = -1;
88 1.1 yamt int nfd = 0;
89 1.1 yamt int ret;
90 1.1 yamt int fd;
91 1.1 yamt int i;
92 1.1 yamt
93 1.1 yamt FD_ZERO(&set);
94 1.1 yamt do {
95 1.1 yamt for (i = 0; i < NPIPE; i++) {
96 1.1 yamt fd = fds[i][0];
97 1.1 yamt if (fd > FD_SETSIZE) {
98 1.1 yamt fprintf(stderr,
99 1.1 yamt "fd(%d) > FD_SETSIZE(%d)\n",
100 1.1 yamt fd, FD_SETSIZE);
101 1.1 yamt abort();
102 1.1 yamt }
103 1.1 yamt if (random() & 1) {
104 1.3 yamt assert(!FD_ISSET(fd, &set));
105 1.1 yamt FD_SET(fd, &set);
106 1.3 yamt nfd++;
107 1.1 yamt if (fd > maxfd) {
108 1.1 yamt maxfd = fd;
109 1.1 yamt }
110 1.1 yamt }
111 1.1 yamt }
112 1.1 yamt } while (nfd == 0);
113 1.1 yamt memcpy(&oset, &set, sizeof(oset));
114 1.1 yamt memset(&to, 0, sizeof(to));
115 1.1 yamt to.tv_sec = random() % 10;
116 1.1 yamt to.tv_usec = random() % 1000000;
117 1.1 yamt #if VERBOSE
118 1.1 yamt printf("[%p] select start to=%lu\n", (void *)pthread_self(),
119 1.1 yamt (unsigned long)to.tv_sec);
120 1.1 yamt #endif
121 1.1 yamt ret = select(maxfd + 1, &set, NULL, NULL, &to);
122 1.1 yamt #if VERBOSE
123 1.1 yamt printf("[%p] select done ret=%d\n",
124 1.1 yamt (void *)pthread_self(), ret);
125 1.1 yamt #endif
126 1.1 yamt if (ret == -1) {
127 1.1 yamt perror("select");
128 1.1 yamt abort();
129 1.1 yamt }
130 1.1 yamt if (ret > nfd) {
131 1.1 yamt fprintf(stderr, "[%p] unexpected return value %d\n",
132 1.1 yamt (void *)pthread_self(), ret);
133 1.1 yamt abort();
134 1.1 yamt }
135 1.3 yamt if (ret > NBALLS) {
136 1.3 yamt fprintf(stderr, "[%p] unexpected return value %d"
137 1.3 yamt " > NBALLS\n",
138 1.3 yamt (void *)pthread_self(), ret);
139 1.3 yamt abort();
140 1.3 yamt }
141 1.1 yamt nfd = 0;
142 1.1 yamt for (fd = 0; fd <= maxfd; fd++) {
143 1.1 yamt if (FD_ISSET(fd, &set)) {
144 1.1 yamt char buf[1];
145 1.1 yamt
146 1.1 yamt #if VERBOSE
147 1.1 yamt printf("[%p] read %d\n",
148 1.1 yamt (void *)pthread_self(), fd);
149 1.1 yamt #endif
150 1.1 yamt if (!FD_ISSET(fd, &oset)) {
151 1.1 yamt fprintf(stderr, "[%p] unexpected\n",
152 1.1 yamt (void *)pthread_self());
153 1.1 yamt abort();
154 1.1 yamt }
155 1.1 yamt if (read(fd, buf, sizeof(buf)) == -1) {
156 1.1 yamt if (errno != EAGAIN) {
157 1.1 yamt perror("read");
158 1.1 yamt abort();
159 1.1 yamt }
160 1.1 yamt } else {
161 1.1 yamt dowrite();
162 1.2 ad atomic_inc_uint(&count);
163 1.1 yamt }
164 1.1 yamt nfd++;
165 1.1 yamt }
166 1.1 yamt }
167 1.1 yamt if (ret != nfd) {
168 1.1 yamt fprintf(stderr, "[%p] ret(%d) != nfd(%d)\n",
169 1.1 yamt (void *)pthread_self(), ret, nfd);
170 1.1 yamt abort();
171 1.1 yamt }
172 1.1 yamt }
173 1.1 yamt }
174 1.1 yamt
175 1.1 yamt int
176 1.1 yamt main(int argc, char *argv[])
177 1.1 yamt {
178 1.1 yamt pthread_t pt[NTHREAD];
179 1.1 yamt int i;
180 1.1 yamt unsigned int secs;
181 1.3 yamt struct timeval start_tv;
182 1.3 yamt struct timeval end_tv;
183 1.3 yamt uint64_t usecs;
184 1.3 yamt unsigned int result;
185 1.1 yamt
186 1.1 yamt secs = atoi(argv[1]);
187 1.1 yamt
188 1.1 yamt for (i = 0; i < NPIPE; i++) {
189 1.1 yamt if (pipe(fds[i])) {
190 1.1 yamt perror("pipe");
191 1.1 yamt abort();
192 1.1 yamt }
193 1.1 yamt if (fcntl(fds[i][0], F_SETFL, O_NONBLOCK) == -1) {
194 1.1 yamt perror("fcntl");
195 1.1 yamt abort();
196 1.1 yamt }
197 1.1 yamt }
198 1.2 ad pthread_barrier_init(&barrier, NULL, NTHREAD + 1);
199 1.1 yamt for (i = 0; i < NTHREAD; i++) {
200 1.1 yamt int error = pthread_create(&pt[i], NULL, f, NULL);
201 1.1 yamt if (error) {
202 1.1 yamt errno = error;
203 1.1 yamt perror("pthread_create");
204 1.1 yamt abort();
205 1.1 yamt }
206 1.1 yamt }
207 1.2 ad pthread_barrier_wait(&barrier);
208 1.3 yamt gettimeofday(&start_tv, NULL);
209 1.3 yamt assert(count == 0);
210 1.3 yamt for (i = 0; i < NBALLS; i++) {
211 1.3 yamt dowrite();
212 1.3 yamt }
213 1.1 yamt sleep(secs);
214 1.3 yamt gettimeofday(&end_tv, NULL);
215 1.3 yamt result = count;
216 1.3 yamt usecs = (end_tv.tv_sec - start_tv.tv_sec) * 1000000
217 1.3 yamt + end_tv.tv_usec - start_tv.tv_usec;
218 1.3 yamt printf("%u / %f = %f\n", result, (double)usecs / 1000000,
219 1.3 yamt (double)result / usecs * 1000000);
220 1.1 yamt exit(EXIT_SUCCESS);
221 1.1 yamt }
222