select.c revision 1.2 1 1.2 ad /* $NetBSD: select.c,v 1.2 2008/03/21 16:03:33 ad 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.1 yamt
33 1.1 yamt #include <errno.h>
34 1.1 yamt #include <fcntl.h>
35 1.1 yamt #include <pthread.h>
36 1.1 yamt #include <stdio.h>
37 1.1 yamt #include <stdlib.h>
38 1.1 yamt #include <string.h>
39 1.1 yamt #include <unistd.h>
40 1.1 yamt
41 1.1 yamt #define NPIPE 128
42 1.1 yamt #define NTHREAD 64
43 1.1 yamt #define VERBOSE 0
44 1.1 yamt
45 1.1 yamt #if !defined(RANDOM_MAX)
46 1.1 yamt #define RANDOM_MAX ((1UL << 31) - 1)
47 1.1 yamt #endif
48 1.1 yamt
49 1.1 yamt int fds[NPIPE][2];
50 1.1 yamt
51 1.2 ad unsigned count;
52 1.2 ad
53 1.2 ad pthread_barrier_t barrier;
54 1.1 yamt
55 1.1 yamt static void
56 1.1 yamt dowrite(void)
57 1.1 yamt {
58 1.1 yamt char buf[1];
59 1.1 yamt int fd;
60 1.1 yamt int i;
61 1.1 yamt
62 1.1 yamt i = random() % NPIPE;
63 1.1 yamt fd = fds[i][1];
64 1.1 yamt #if VERBOSE
65 1.1 yamt printf("[%p] write %d\n", (void *)pthread_self(), fd);
66 1.1 yamt #endif
67 1.1 yamt if (write(fd, buf, sizeof(buf)) == -1) {
68 1.1 yamt perror("write");
69 1.1 yamt abort();
70 1.1 yamt }
71 1.1 yamt }
72 1.1 yamt
73 1.1 yamt static void *
74 1.1 yamt f(void *dummy)
75 1.1 yamt {
76 1.1 yamt
77 1.2 ad pthread_barrier_wait(&barrier);
78 1.2 ad
79 1.1 yamt for (;;) {
80 1.1 yamt struct timeval to;
81 1.1 yamt fd_set oset;
82 1.1 yamt fd_set set;
83 1.1 yamt int maxfd = -1;
84 1.1 yamt int nfd = 0;
85 1.1 yamt int ret;
86 1.1 yamt int fd;
87 1.1 yamt int i;
88 1.1 yamt
89 1.1 yamt FD_ZERO(&set);
90 1.1 yamt do {
91 1.1 yamt for (i = 0; i < NPIPE; i++) {
92 1.1 yamt fd = fds[i][0];
93 1.1 yamt if (fd > FD_SETSIZE) {
94 1.1 yamt fprintf(stderr,
95 1.1 yamt "fd(%d) > FD_SETSIZE(%d)\n",
96 1.1 yamt fd, FD_SETSIZE);
97 1.1 yamt abort();
98 1.1 yamt }
99 1.1 yamt if (random() & 1) {
100 1.1 yamt FD_SET(fd, &set);
101 1.1 yamt if (fd > maxfd) {
102 1.1 yamt maxfd = fd;
103 1.1 yamt nfd++;
104 1.1 yamt }
105 1.1 yamt }
106 1.1 yamt }
107 1.1 yamt } while (nfd == 0);
108 1.1 yamt memcpy(&oset, &set, sizeof(oset));
109 1.1 yamt memset(&to, 0, sizeof(to));
110 1.1 yamt to.tv_sec = random() % 10;
111 1.1 yamt to.tv_usec = random() % 1000000;
112 1.1 yamt #if VERBOSE
113 1.1 yamt printf("[%p] select start to=%lu\n", (void *)pthread_self(),
114 1.1 yamt (unsigned long)to.tv_sec);
115 1.1 yamt #endif
116 1.1 yamt ret = select(maxfd + 1, &set, NULL, NULL, &to);
117 1.1 yamt #if VERBOSE
118 1.1 yamt printf("[%p] select done ret=%d\n",
119 1.1 yamt (void *)pthread_self(), ret);
120 1.1 yamt #endif
121 1.1 yamt if (ret == -1) {
122 1.1 yamt perror("select");
123 1.1 yamt abort();
124 1.1 yamt }
125 1.1 yamt if (ret > nfd) {
126 1.1 yamt fprintf(stderr, "[%p] unexpected return value %d\n",
127 1.1 yamt (void *)pthread_self(), ret);
128 1.1 yamt abort();
129 1.1 yamt }
130 1.1 yamt nfd = 0;
131 1.1 yamt for (fd = 0; fd <= maxfd; fd++) {
132 1.1 yamt if (FD_ISSET(fd, &set)) {
133 1.1 yamt char buf[1];
134 1.1 yamt
135 1.1 yamt #if VERBOSE
136 1.1 yamt printf("[%p] read %d\n",
137 1.1 yamt (void *)pthread_self(), fd);
138 1.1 yamt #endif
139 1.1 yamt if (!FD_ISSET(fd, &oset)) {
140 1.1 yamt fprintf(stderr, "[%p] unexpected\n",
141 1.1 yamt (void *)pthread_self());
142 1.1 yamt abort();
143 1.1 yamt }
144 1.1 yamt if (read(fd, buf, sizeof(buf)) == -1) {
145 1.1 yamt if (errno != EAGAIN) {
146 1.1 yamt perror("read");
147 1.1 yamt abort();
148 1.1 yamt }
149 1.1 yamt } else {
150 1.1 yamt dowrite();
151 1.2 ad atomic_inc_uint(&count);
152 1.1 yamt }
153 1.1 yamt nfd++;
154 1.1 yamt }
155 1.1 yamt }
156 1.1 yamt if (ret != nfd) {
157 1.1 yamt fprintf(stderr, "[%p] ret(%d) != nfd(%d)\n",
158 1.1 yamt (void *)pthread_self(), ret, nfd);
159 1.1 yamt abort();
160 1.1 yamt }
161 1.1 yamt }
162 1.1 yamt }
163 1.1 yamt
164 1.1 yamt int
165 1.1 yamt main(int argc, char *argv[])
166 1.1 yamt {
167 1.1 yamt pthread_t pt[NTHREAD];
168 1.1 yamt int i;
169 1.1 yamt unsigned int secs;
170 1.1 yamt
171 1.1 yamt secs = atoi(argv[1]);
172 1.1 yamt
173 1.1 yamt for (i = 0; i < NPIPE; i++) {
174 1.1 yamt if (pipe(fds[i])) {
175 1.1 yamt perror("pipe");
176 1.1 yamt abort();
177 1.1 yamt }
178 1.1 yamt if (fcntl(fds[i][0], F_SETFL, O_NONBLOCK) == -1) {
179 1.1 yamt perror("fcntl");
180 1.1 yamt abort();
181 1.1 yamt }
182 1.1 yamt }
183 1.2 ad pthread_barrier_init(&barrier, NULL, NTHREAD + 1);
184 1.1 yamt for (i = 0; i < NTHREAD; i++) {
185 1.1 yamt int error = pthread_create(&pt[i], NULL, f, NULL);
186 1.1 yamt if (error) {
187 1.1 yamt errno = error;
188 1.1 yamt perror("pthread_create");
189 1.1 yamt abort();
190 1.1 yamt }
191 1.1 yamt }
192 1.2 ad pthread_barrier_wait(&barrier);
193 1.1 yamt dowrite();
194 1.1 yamt dowrite();
195 1.1 yamt dowrite();
196 1.1 yamt dowrite();
197 1.1 yamt
198 1.1 yamt sleep(secs);
199 1.1 yamt printf("%u / %u = %lf\n", count, secs, (double)count / secs);
200 1.1 yamt exit(EXIT_SUCCESS);
201 1.1 yamt }
202