File: | lib/lwan-thread.c |
Warning: | line 710, column 9 Assigned value is garbage or undefined |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * lwan - simple web server | |||
3 | * Copyright (c) 2012, 2013 Leandro A. F. Pereira <leandro@hardinfo.org> | |||
4 | * | |||
5 | * This program is free software; you can redistribute it and/or | |||
6 | * modify it under the terms of the GNU General Public License | |||
7 | * as published by the Free Software Foundation; either version 2 | |||
8 | * of the License, or any later version. | |||
9 | * | |||
10 | * This program is distributed in the hope that it will be useful, | |||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
13 | * GNU General Public License for more details. | |||
14 | * | |||
15 | * You should have received a copy of the GNU General Public License | |||
16 | * along with this program; if not, write to the Free Software | |||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |||
18 | * USA. | |||
19 | */ | |||
20 | ||||
21 | #define _GNU_SOURCE | |||
22 | #include <assert.h> | |||
23 | #include <errno(*__errno_location ()).h> | |||
24 | #include <fcntl.h> | |||
25 | #include <pthread.h> | |||
26 | #include <sched.h> | |||
27 | #include <stdlib.h> | |||
28 | #include <string.h> | |||
29 | #include <sys/epoll.h> | |||
30 | #include <sys/ioctl.h> | |||
31 | #include <sys/socket.h> | |||
32 | #include <unistd.h> | |||
33 | ||||
34 | #if defined(HAVE_EVENTFD) | |||
35 | #include <sys/eventfd.h> | |||
36 | #endif | |||
37 | ||||
38 | #include "lwan-private.h" | |||
39 | #include "lwan-tq.h" | |||
40 | #include "list.h" | |||
41 | ||||
42 | static void lwan_strbuf_free_defer(void *data) | |||
43 | { | |||
44 | lwan_strbuf_free((struct lwan_strbuf *)data); | |||
45 | } | |||
46 | ||||
47 | static void graceful_close(struct lwan *l, | |||
48 | struct lwan_connection *conn, | |||
49 | char buffer[static DEFAULT_BUFFER_SIZE4096]) | |||
50 | { | |||
51 | int fd = lwan_connection_get_fd(l, conn); | |||
52 | ||||
53 | while (TIOCOUTQ0x5411) { | |||
54 | /* This ioctl isn't probably doing what it says on the tin; the details | |||
55 | * are subtle, but it seems to do the trick to allow gracefully closing | |||
56 | * the connection in some cases with minimal system calls. */ | |||
57 | int bytes_waiting; | |||
58 | int r = ioctl(fd, TIOCOUTQ0x5411, &bytes_waiting); | |||
59 | ||||
60 | if (!r && !bytes_waiting) /* See note about close(2) below. */ | |||
61 | return; | |||
62 | if (r < 0 && errno(*__errno_location ()) == EINTR4) | |||
63 | continue; | |||
64 | ||||
65 | break; | |||
66 | } | |||
67 | ||||
68 | if (UNLIKELY(shutdown(fd, SHUT_WR) < 0)__builtin_expect(((shutdown(fd, SHUT_WR) < 0)), (0))) { | |||
69 | if (UNLIKELY(errno == ENOTCONN)__builtin_expect((((*__errno_location ()) == 107)), (0))) | |||
70 | return; | |||
71 | } | |||
72 | ||||
73 | for (int tries = 0; tries < 20; tries++) { | |||
74 | ssize_t r = read(fd, buffer, DEFAULT_BUFFER_SIZE4096); | |||
75 | ||||
76 | if (!r) | |||
77 | break; | |||
78 | ||||
79 | if (r < 0) { | |||
80 | switch (errno(*__errno_location ())) { | |||
81 | case EAGAIN11: | |||
82 | break; | |||
83 | case EINTR4: | |||
84 | continue; | |||
85 | default: | |||
86 | return; | |||
87 | } | |||
88 | } | |||
89 | ||||
90 | coro_yield(conn->coro, CONN_CORO_WANT_READ); | |||
91 | } | |||
92 | ||||
93 | /* close(2) will be called when the coroutine yields with CONN_CORO_ABORT */ | |||
94 | } | |||
95 | ||||
96 | __attribute__((noreturn)) static int process_request_coro(struct coro *coro, | |||
97 | void *data) | |||
98 | { | |||
99 | /* NOTE: This function should not return; coro_yield should be used | |||
100 | * instead. This ensures the storage for `strbuf` is alive when the | |||
101 | * coroutine ends and lwan_strbuf_free() is called. */ | |||
102 | struct lwan_connection *conn = data; | |||
103 | struct lwan *lwan = conn->thread->lwan; | |||
104 | int fd = lwan_connection_get_fd(lwan, conn); | |||
105 | enum lwan_request_flags flags = lwan->config.request_flags; | |||
106 | struct lwan_strbuf strbuf = LWAN_STRBUF_STATIC_INIT(struct lwan_strbuf) { .buffer = "" }; | |||
107 | char request_buffer[DEFAULT_BUFFER_SIZE4096]; | |||
108 | struct lwan_value buffer = {.value = request_buffer, .len = 0}; | |||
109 | char *next_request = NULL((void*)0); | |||
110 | char *header_start[N_HEADER_START64]; | |||
111 | struct lwan_proxy proxy; | |||
112 | const int error_when_n_packets = lwan_calculate_n_packets(DEFAULT_BUFFER_SIZE4096); | |||
113 | ||||
114 | coro_defer(coro, lwan_strbuf_free_defer, &strbuf); | |||
115 | ||||
116 | const size_t init_gen = 1; /* 1 call to coro_defer() */ | |||
117 | assert(init_gen == coro_deferred_get_generation(coro))((void) sizeof ((init_gen == coro_deferred_get_generation(coro )) ? 1 : 0), __extension__ ({ if (init_gen == coro_deferred_get_generation (coro)) ; else __assert_fail ("init_gen == coro_deferred_get_generation(coro)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 117, __extension__ __PRETTY_FUNCTION__); })); | |||
118 | ||||
119 | while (true1) { | |||
120 | struct lwan_request_parser_helper helper = { | |||
121 | .buffer = &buffer, | |||
122 | .next_request = next_request, | |||
123 | .error_when_n_packets = error_when_n_packets, | |||
124 | .header_start = header_start, | |||
125 | }; | |||
126 | struct lwan_request request = {.conn = conn, | |||
127 | .global_response_headers = &lwan->headers, | |||
128 | .fd = fd, | |||
129 | .response = {.buffer = &strbuf}, | |||
130 | .flags = flags, | |||
131 | .proxy = &proxy, | |||
132 | .helper = &helper}; | |||
133 | ||||
134 | lwan_process_request(lwan, &request); | |||
135 | ||||
136 | /* Run the deferred instructions now (except those used to initialize | |||
137 | * the coroutine), so that if the connection is gracefully closed, | |||
138 | * the storage for ``helper'' is still there. */ | |||
139 | coro_deferred_run(coro, init_gen); | |||
140 | ||||
141 | if (UNLIKELY(!(conn->flags & CONN_IS_KEEP_ALIVE))__builtin_expect(((!(conn->flags & CONN_IS_KEEP_ALIVE) )), (0))) { | |||
142 | graceful_close(lwan, conn, request_buffer); | |||
143 | break; | |||
144 | } | |||
145 | ||||
146 | if (next_request && *next_request) { | |||
147 | conn->flags |= CONN_CORK; | |||
148 | ||||
149 | if (!(conn->flags & CONN_EVENTS_WRITE)) | |||
150 | coro_yield(coro, CONN_CORO_WANT_WRITE); | |||
151 | } else { | |||
152 | conn->flags &= ~CONN_CORK; | |||
153 | coro_yield(coro, CONN_CORO_WANT_READ); | |||
154 | } | |||
155 | ||||
156 | /* Ensure string buffer is reset between requests, and that the backing | |||
157 | * store isn't over 2KB. */ | |||
158 | lwan_strbuf_reset_trim(&strbuf, 2048); | |||
159 | ||||
160 | /* Only allow flags from config. */ | |||
161 | flags = request.flags & (REQUEST_PROXIED | REQUEST_ALLOW_CORS); | |||
162 | next_request = helper.next_request; | |||
163 | } | |||
164 | ||||
165 | coro_yield(coro, CONN_CORO_ABORT); | |||
166 | __builtin_unreachable(); | |||
167 | } | |||
168 | ||||
169 | static ALWAYS_INLINEinline __attribute__((always_inline)) uint32_t | |||
170 | conn_flags_to_epoll_events(enum lwan_connection_flags flags) | |||
171 | { | |||
172 | static const uint32_t map[CONN_EVENTS_MASK + 1] = { | |||
173 | [0 /* Suspended (timer or await) */] = EPOLLRDHUPEPOLLRDHUP, | |||
174 | [CONN_EVENTS_WRITE] = EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP, | |||
175 | [CONN_EVENTS_READ] = EPOLLINEPOLLIN | EPOLLRDHUPEPOLLRDHUP, | |||
176 | [CONN_EVENTS_READ_WRITE] = EPOLLINEPOLLIN | EPOLLOUTEPOLLOUT | EPOLLRDHUPEPOLLRDHUP, | |||
177 | }; | |||
178 | ||||
179 | return map[flags & CONN_EVENTS_MASK]; | |||
180 | } | |||
181 | ||||
182 | static void update_epoll_flags(int fd, | |||
183 | struct lwan_connection *conn, | |||
184 | int epoll_fd, | |||
185 | enum lwan_connection_coro_yield yield_result) | |||
186 | { | |||
187 | static const enum lwan_connection_flags or_mask[CONN_CORO_MAX] = { | |||
188 | [CONN_CORO_YIELD] = 0, | |||
189 | ||||
190 | [CONN_CORO_WANT_READ_WRITE] = CONN_EVENTS_READ_WRITE, | |||
191 | [CONN_CORO_WANT_READ] = CONN_EVENTS_READ, | |||
192 | [CONN_CORO_WANT_WRITE] = CONN_EVENTS_WRITE, | |||
193 | ||||
194 | /* While the coro is suspended, we're not interested in either EPOLLIN | |||
195 | * or EPOLLOUT events. We still want to track this fd in epoll, though, | |||
196 | * so unset both so that only EPOLLRDHUP (plus the implicitly-set ones) | |||
197 | * are set. */ | |||
198 | [CONN_CORO_SUSPEND_TIMER] = CONN_SUSPENDED_TIMER, | |||
199 | [CONN_CORO_SUSPEND_ASYNC_AWAIT] = CONN_SUSPENDED_ASYNC_AWAIT, | |||
200 | ||||
201 | /* Ideally, when suspending a coroutine, the current flags&CONN_EVENTS_MASK | |||
202 | * would have to be stored and restored -- however, resuming as if the | |||
203 | * client coroutine is interested in a write event always guarantees that | |||
204 | * they'll be resumed as they're TCP sockets. There's a good chance that | |||
205 | * trying to read from a socket after resuming a coroutine will succeed, | |||
206 | * but if it doesn't because read() returns -EAGAIN, the I/O wrappers will | |||
207 | * yield with CONN_CORO_WANT_READ anyway. */ | |||
208 | [CONN_CORO_RESUME] = CONN_EVENTS_WRITE, | |||
209 | }; | |||
210 | static const enum lwan_connection_flags and_mask[CONN_CORO_MAX] = { | |||
211 | [CONN_CORO_YIELD] = ~0, | |||
212 | ||||
213 | [CONN_CORO_WANT_READ_WRITE] = ~0, | |||
214 | [CONN_CORO_WANT_READ] = ~CONN_EVENTS_WRITE, | |||
215 | [CONN_CORO_WANT_WRITE] = ~CONN_EVENTS_READ, | |||
216 | ||||
217 | [CONN_CORO_SUSPEND_TIMER] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_ASYNC_AWAIT), | |||
218 | [CONN_CORO_SUSPEND_ASYNC_AWAIT] = ~(CONN_EVENTS_READ_WRITE | CONN_SUSPENDED_TIMER), | |||
219 | [CONN_CORO_RESUME] = ~CONN_SUSPENDED, | |||
220 | }; | |||
221 | enum lwan_connection_flags prev_flags = conn->flags; | |||
222 | ||||
223 | conn->flags |= or_mask[yield_result]; | |||
224 | conn->flags &= and_mask[yield_result]; | |||
225 | ||||
226 | if (conn->flags == prev_flags) | |||
227 | return; | |||
228 | ||||
229 | struct epoll_event event = { | |||
230 | .events = conn_flags_to_epoll_events(conn->flags), | |||
231 | .data.ptr = conn, | |||
232 | }; | |||
233 | ||||
234 | if (UNLIKELY(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0)__builtin_expect(((epoll_ctl(epoll_fd, 3, fd, &event) < 0)), (0))) | |||
235 | lwan_status_perror("epoll_ctl")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 235, __FUNCTION__, "epoll_ctl"); | |||
236 | } | |||
237 | ||||
238 | static void clear_async_await_flag(void *data) | |||
239 | { | |||
240 | struct lwan_connection *async_fd_conn = data; | |||
241 | ||||
242 | async_fd_conn->flags &= ~CONN_ASYNC_AWAIT; | |||
243 | } | |||
244 | ||||
245 | static enum lwan_connection_coro_yield | |||
246 | resume_async(struct timeout_queue *tq, | |||
247 | enum lwan_connection_coro_yield yield_result, | |||
248 | int64_t from_coro, | |||
249 | struct lwan_connection *conn, | |||
250 | int epoll_fd) | |||
251 | { | |||
252 | static const enum lwan_connection_flags to_connection_flags[] = { | |||
253 | [CONN_CORO_ASYNC_AWAIT_READ] = CONN_EVENTS_READ, | |||
254 | [CONN_CORO_ASYNC_AWAIT_WRITE] = CONN_EVENTS_WRITE, | |||
255 | [CONN_CORO_ASYNC_AWAIT_READ_WRITE] = CONN_EVENTS_READ_WRITE, | |||
256 | }; | |||
257 | int await_fd = (int)((uint64_t)from_coro >> 32); | |||
258 | enum lwan_connection_flags flags; | |||
259 | int op; | |||
260 | ||||
261 | assert(await_fd >= 0)((void) sizeof ((await_fd >= 0) ? 1 : 0), __extension__ ({ if (await_fd >= 0) ; else __assert_fail ("await_fd >= 0" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 261, __extension__ __PRETTY_FUNCTION__); })); | |||
262 | assert(yield_result >= CONN_CORO_ASYNC_AWAIT_READ &&((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 263, __extension__ __PRETTY_FUNCTION__); })) | |||
263 | yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE)((void) sizeof ((yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ? 1 : 0), __extension__ ({ if (yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE ) ; else __assert_fail ("yield_result >= CONN_CORO_ASYNC_AWAIT_READ && yield_result <= CONN_CORO_ASYNC_AWAIT_READ_WRITE" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 263, __extension__ __PRETTY_FUNCTION__); })); | |||
264 | ||||
265 | flags = to_connection_flags[yield_result]; | |||
266 | ||||
267 | struct lwan_connection *await_fd_conn = &tq->lwan->conns[await_fd]; | |||
268 | if (LIKELY(await_fd_conn->flags & CONN_ASYNC_AWAIT)__builtin_expect((!!(await_fd_conn->flags & CONN_ASYNC_AWAIT )), (1))) { | |||
269 | if (LIKELY((await_fd_conn->flags & CONN_EVENTS_MASK) == flags)__builtin_expect((!!((await_fd_conn->flags & CONN_EVENTS_MASK ) == flags)), (1))) | |||
270 | return CONN_CORO_SUSPEND_ASYNC_AWAIT; | |||
271 | ||||
272 | op = EPOLL_CTL_MOD3; | |||
273 | } else { | |||
274 | op = EPOLL_CTL_ADD1; | |||
275 | flags |= CONN_ASYNC_AWAIT; | |||
276 | coro_defer(conn->coro, clear_async_await_flag, await_fd_conn); | |||
277 | } | |||
278 | ||||
279 | struct epoll_event event = {.events = conn_flags_to_epoll_events(flags), | |||
280 | .data.ptr = conn}; | |||
281 | if (LIKELY(!epoll_ctl(epoll_fd, op, await_fd, &event))__builtin_expect((!!(!epoll_ctl(epoll_fd, op, await_fd, & event))), (1))) { | |||
282 | await_fd_conn->flags &= ~CONN_EVENTS_MASK; | |||
283 | await_fd_conn->flags |= flags; | |||
284 | return CONN_CORO_SUSPEND_ASYNC_AWAIT; | |||
285 | } | |||
286 | ||||
287 | return CONN_CORO_ABORT; | |||
288 | } | |||
289 | ||||
290 | static ALWAYS_INLINEinline __attribute__((always_inline)) void resume_coro(struct timeout_queue *tq, | |||
291 | struct lwan_connection *conn, | |||
292 | int epoll_fd) | |||
293 | { | |||
294 | assert(conn->coro)((void) sizeof ((conn->coro) ? 1 : 0), __extension__ ({ if (conn->coro) ; else __assert_fail ("conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 294, __extension__ __PRETTY_FUNCTION__); })); | |||
295 | ||||
296 | int64_t from_coro = coro_resume(conn->coro); | |||
297 | enum lwan_connection_coro_yield yield_result = from_coro & 0xffffffff; | |||
298 | ||||
299 | if (UNLIKELY(yield_result >= CONN_CORO_ASYNC)__builtin_expect(((yield_result >= CONN_CORO_ASYNC)), (0))) | |||
300 | yield_result = resume_async(tq, yield_result, from_coro, conn, epoll_fd); | |||
301 | ||||
302 | if (UNLIKELY(yield_result == CONN_CORO_ABORT)__builtin_expect(((yield_result == CONN_CORO_ABORT)), (0))) | |||
303 | return timeout_queue_expire(tq, conn); | |||
304 | ||||
305 | return update_epoll_flags(lwan_connection_get_fd(tq->lwan, conn), conn, | |||
306 | epoll_fd, yield_result); | |||
307 | } | |||
308 | ||||
309 | static void update_date_cache(struct lwan_thread *thread) | |||
310 | { | |||
311 | time_t now = time(NULL((void*)0)); | |||
312 | ||||
313 | lwan_format_rfc_time(now, thread->date.date); | |||
314 | lwan_format_rfc_time(now + (time_t)thread->lwan->config.expires, | |||
315 | thread->date.expires); | |||
316 | } | |||
317 | ||||
318 | static ALWAYS_INLINEinline __attribute__((always_inline)) void spawn_coro(struct lwan_connection *conn, | |||
319 | struct coro_switcher *switcher, | |||
320 | struct timeout_queue *tq) | |||
321 | { | |||
322 | struct lwan_thread *t = conn->thread; | |||
323 | ||||
324 | assert(!conn->coro)((void) sizeof ((!conn->coro) ? 1 : 0), __extension__ ({ if (!conn->coro) ; else __assert_fail ("!conn->coro", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 324, __extension__ __PRETTY_FUNCTION__); })); | |||
325 | assert(t)((void) sizeof ((t) ? 1 : 0), __extension__ ({ if (t) ; else __assert_fail ("t", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 325, __extension__ __PRETTY_FUNCTION__); })); | |||
326 | assert((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads)((void) sizeof (((uintptr_t)t >= (uintptr_t)tq->lwan-> thread.threads) ? 1 : 0), __extension__ ({ if ((uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads) ; else __assert_fail ("(uintptr_t)t >= (uintptr_t)tq->lwan->thread.threads" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 326, __extension__ __PRETTY_FUNCTION__); })); | |||
327 | assert((uintptr_t)t <((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan-> thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__ ({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread. threads + tq->lwan->thread.count)) ; else __assert_fail ("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 328, __extension__ __PRETTY_FUNCTION__); })) | |||
328 | (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count))((void) sizeof (((uintptr_t)t < (uintptr_t)(tq->lwan-> thread.threads + tq->lwan->thread.count)) ? 1 : 0), __extension__ ({ if ((uintptr_t)t < (uintptr_t)(tq->lwan->thread. threads + tq->lwan->thread.count)) ; else __assert_fail ("(uintptr_t)t < (uintptr_t)(tq->lwan->thread.threads + tq->lwan->thread.count)" , "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 328, __extension__ __PRETTY_FUNCTION__); })); | |||
329 | ||||
330 | *conn = (struct lwan_connection) { | |||
331 | .coro = coro_new(switcher, process_request_coro, conn), | |||
332 | .flags = CONN_EVENTS_READ, | |||
333 | .time_to_expire = tq->current_time + tq->move_to_last_bump, | |||
334 | .thread = t, | |||
335 | }; | |||
336 | if (UNLIKELY(!conn->coro)__builtin_expect(((!conn->coro)), (0))) { | |||
337 | /* FIXME: send a "busy" response to this client? we don't have a coroutine | |||
338 | * at this point, can't use lwan_send() here */ | |||
339 | lwan_status_error("Could not create coroutine, dropping connection")lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 339, __FUNCTION__, "Could not create coroutine, dropping connection" ); | |||
340 | ||||
341 | conn->flags = 0; | |||
342 | ||||
343 | int fd = lwan_connection_get_fd(tq->lwan, conn); | |||
344 | shutdown(fd, SHUT_RDWRSHUT_RDWR); | |||
345 | close(fd); | |||
346 | ||||
347 | return; | |||
348 | } | |||
349 | ||||
350 | timeout_queue_insert(tq, conn); | |||
351 | } | |||
352 | ||||
353 | static void accept_nudge(int pipe_fd, | |||
354 | struct lwan_thread *t, | |||
355 | struct lwan_connection *conns, | |||
356 | struct timeout_queue *tq, | |||
357 | struct coro_switcher *switcher, | |||
358 | int epoll_fd) | |||
359 | { | |||
360 | uint64_t event; | |||
361 | int new_fd; | |||
362 | ||||
363 | /* Errors are ignored here as pipe_fd serves just as a way to wake the | |||
364 | * thread from epoll_wait(). It's fine to consume the queue at this | |||
365 | * point, regardless of the error type. */ | |||
366 | (void)read(pipe_fd, &event, sizeof(event)); | |||
367 | ||||
368 | while (spsc_queue_pop(&t->pending_fds, &new_fd)) { | |||
369 | struct lwan_connection *conn = &conns[new_fd]; | |||
370 | struct epoll_event ev = { | |||
371 | .data.ptr = conn, | |||
372 | .events = conn_flags_to_epoll_events(CONN_EVENTS_READ), | |||
373 | }; | |||
374 | ||||
375 | if (LIKELY(!epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_fd, &ev))__builtin_expect((!!(!epoll_ctl(epoll_fd, 1, new_fd, &ev) )), (1))) | |||
376 | spawn_coro(conn, switcher, tq); | |||
377 | } | |||
378 | ||||
379 | timeouts_add(t->wheel, &tq->timeout, 1000); | |||
380 | } | |||
381 | ||||
382 | static bool_Bool process_pending_timers(struct timeout_queue *tq, | |||
383 | struct lwan_thread *t, | |||
384 | int epoll_fd) | |||
385 | { | |||
386 | struct timeout *timeout; | |||
387 | bool_Bool should_expire_timers = false0; | |||
388 | ||||
389 | while ((timeout = timeouts_get(t->wheel))) { | |||
390 | struct lwan_request *request; | |||
391 | ||||
392 | if (timeout == &tq->timeout) { | |||
393 | should_expire_timers = true1; | |||
394 | continue; | |||
395 | } | |||
396 | ||||
397 | request = container_of(timeout, struct lwan_request, timeout)((struct lwan_request *) ((char *)(timeout) - __builtin_offsetof (struct lwan_request, timeout)) + ((typeof(*(timeout)) *)0 != (typeof(((struct lwan_request *)0)->timeout) *)0)); | |||
398 | ||||
399 | update_epoll_flags(request->fd, request->conn, epoll_fd, | |||
400 | CONN_CORO_RESUME); | |||
401 | } | |||
402 | ||||
403 | if (should_expire_timers) { | |||
404 | timeout_queue_expire_waiting(tq); | |||
405 | ||||
406 | /* tq timeout expires every 1000ms if there are connections, so | |||
407 | * update the date cache at this point as well. */ | |||
408 | update_date_cache(t); | |||
409 | ||||
410 | if (!timeout_queue_empty(tq)) { | |||
411 | timeouts_add(t->wheel, &tq->timeout, 1000); | |||
412 | return true1; | |||
413 | } | |||
414 | ||||
415 | timeouts_del(t->wheel, &tq->timeout); | |||
416 | } | |||
417 | ||||
418 | return false0; | |||
419 | } | |||
420 | ||||
421 | static int | |||
422 | turn_timer_wheel(struct timeout_queue *tq, struct lwan_thread *t, int epoll_fd) | |||
423 | { | |||
424 | timeout_t wheel_timeout; | |||
425 | struct timespec now; | |||
426 | ||||
427 | if (UNLIKELY(clock_gettime(monotonic_clock_id, &now) < 0)__builtin_expect(((clock_gettime(monotonic_clock_id, &now ) < 0)), (0))) | |||
428 | lwan_status_critical("Could not get monotonic time")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 428, __FUNCTION__, "Could not get monotonic time"); | |||
429 | ||||
430 | timeouts_update(t->wheel, | |||
431 | (timeout_t)(now.tv_sec * 1000 + now.tv_nsec / 1000000)); | |||
432 | ||||
433 | wheel_timeout = timeouts_timeout(t->wheel); | |||
434 | if (UNLIKELY((int64_t)wheel_timeout < 0)__builtin_expect((((int64_t)wheel_timeout < 0)), (0))) | |||
435 | goto infinite_timeout; | |||
436 | ||||
437 | if (wheel_timeout == 0) { | |||
438 | if (!process_pending_timers(tq, t, epoll_fd)) | |||
439 | goto infinite_timeout; | |||
440 | ||||
441 | wheel_timeout = timeouts_timeout(t->wheel); | |||
442 | if (wheel_timeout == 0) | |||
443 | goto infinite_timeout; | |||
444 | } | |||
445 | ||||
446 | return (int)wheel_timeout; | |||
447 | ||||
448 | infinite_timeout: | |||
449 | return -1; | |||
450 | } | |||
451 | ||||
452 | static void *thread_io_loop(void *data) | |||
453 | { | |||
454 | struct lwan_thread *t = data; | |||
455 | int epoll_fd = t->epoll_fd; | |||
456 | const int read_pipe_fd = t->pipe_fd[0]; | |||
457 | const int max_events = LWAN_MIN((int)t->lwan->thread.max_fd, 1024)({ const __typeof__(((int)t->lwan->thread.max_fd) + 0) lwan_tmp_id6 = ((int)t->lwan->thread.max_fd); const __typeof__((1024 ) + 0) lwan_tmp_id7 = (1024); lwan_tmp_id6 > lwan_tmp_id7 ? lwan_tmp_id7 : lwan_tmp_id6; }); | |||
458 | struct lwan *lwan = t->lwan; | |||
459 | struct epoll_event *events; | |||
460 | struct coro_switcher switcher; | |||
461 | struct timeout_queue tq; | |||
462 | ||||
463 | lwan_status_debug("Worker thread #%zd starting",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 464, __FUNCTION__, "Worker thread #%zd starting", t - t-> lwan->thread.threads + 1) | |||
464 | t - t->lwan->thread.threads + 1)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 464, __FUNCTION__, "Worker thread #%zd starting", t - t-> lwan->thread.threads + 1); | |||
465 | lwan_set_thread_name("worker"); | |||
466 | ||||
467 | events = calloc((size_t)max_events, sizeof(*events)); | |||
468 | if (UNLIKELY(!events)__builtin_expect(((!events)), (0))) | |||
469 | lwan_status_critical("Could not allocate memory for events")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 469, __FUNCTION__, "Could not allocate memory for events"); | |||
470 | ||||
471 | update_date_cache(t); | |||
472 | ||||
473 | timeout_queue_init(&tq, lwan); | |||
474 | ||||
475 | pthread_barrier_wait(&lwan->thread.barrier); | |||
476 | ||||
477 | for (;;) { | |||
478 | int timeout = turn_timer_wheel(&tq, t, epoll_fd); | |||
479 | int n_fds = epoll_wait(epoll_fd, events, max_events, timeout); | |||
480 | ||||
481 | if (UNLIKELY(n_fds < 0)__builtin_expect(((n_fds < 0)), (0))) { | |||
482 | if (errno(*__errno_location ()) == EBADF9 || errno(*__errno_location ()) == EINVAL22) | |||
483 | break; | |||
484 | continue; | |||
485 | } | |||
486 | ||||
487 | for (struct epoll_event *event = events; n_fds--; event++) { | |||
488 | struct lwan_connection *conn; | |||
489 | ||||
490 | if (UNLIKELY(!event->data.ptr)__builtin_expect(((!event->data.ptr)), (0))) { | |||
491 | accept_nudge(read_pipe_fd, t, lwan->conns, &tq, &switcher, | |||
492 | epoll_fd); | |||
493 | continue; | |||
494 | } | |||
495 | ||||
496 | conn = event->data.ptr; | |||
497 | ||||
498 | if (UNLIKELY(event->events & (EPOLLRDHUP | EPOLLHUP))__builtin_expect(((event->events & (EPOLLRDHUP | EPOLLHUP ))), (0))) { | |||
499 | timeout_queue_expire(&tq, conn); | |||
500 | continue; | |||
501 | } | |||
502 | ||||
503 | resume_coro(&tq, conn, epoll_fd); | |||
504 | timeout_queue_move_to_last(&tq, conn); | |||
505 | } | |||
506 | } | |||
507 | ||||
508 | pthread_barrier_wait(&lwan->thread.barrier); | |||
509 | ||||
510 | timeout_queue_expire_all(&tq); | |||
511 | free(events); | |||
512 | ||||
513 | return NULL((void*)0); | |||
514 | } | |||
515 | ||||
516 | static void create_thread(struct lwan *l, struct lwan_thread *thread, | |||
517 | const size_t n_queue_fds) | |||
518 | { | |||
519 | int ignore; | |||
520 | pthread_attr_t attr; | |||
521 | ||||
522 | memset(thread, 0, sizeof(*thread)); | |||
523 | thread->lwan = l; | |||
524 | ||||
525 | thread->wheel = timeouts_open(&ignore); | |||
526 | if (!thread->wheel) | |||
527 | lwan_status_critical("Could not create timer wheel")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 527, __FUNCTION__, "Could not create timer wheel"); | |||
528 | ||||
529 | if ((thread->epoll_fd = epoll_create1(EPOLL_CLOEXECEPOLL_CLOEXEC)) < 0) | |||
530 | lwan_status_critical_perror("epoll_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 530, __FUNCTION__, "epoll_create"); | |||
531 | ||||
532 | if (pthread_attr_init(&attr)) | |||
533 | lwan_status_critical_perror("pthread_attr_init")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 533, __FUNCTION__, "pthread_attr_init"); | |||
534 | ||||
535 | if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEMPTHREAD_SCOPE_SYSTEM)) | |||
536 | lwan_status_critical_perror("pthread_attr_setscope")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 536, __FUNCTION__, "pthread_attr_setscope"); | |||
537 | ||||
538 | if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLEPTHREAD_CREATE_JOINABLE)) | |||
539 | lwan_status_critical_perror("pthread_attr_setdetachstate")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 539, __FUNCTION__, "pthread_attr_setdetachstate"); | |||
540 | ||||
541 | #if defined(HAVE_EVENTFD) | |||
542 | int efd = eventfd(0, EFD_NONBLOCKEFD_NONBLOCK | EFD_SEMAPHOREEFD_SEMAPHORE | EFD_CLOEXECEFD_CLOEXEC); | |||
543 | if (efd < 0) | |||
544 | lwan_status_critical_perror("eventfd")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 544, __FUNCTION__, "eventfd"); | |||
545 | ||||
546 | thread->pipe_fd[0] = thread->pipe_fd[1] = efd; | |||
547 | #else | |||
548 | if (pipe2(thread->pipe_fd, O_NONBLOCK04000 | O_CLOEXEC02000000) < 0) | |||
549 | lwan_status_critical_perror("pipe")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 549, __FUNCTION__, "pipe"); | |||
550 | #endif | |||
551 | ||||
552 | struct epoll_event event = { .events = EPOLLINEPOLLIN, .data.ptr = NULL((void*)0) }; | |||
553 | if (epoll_ctl(thread->epoll_fd, EPOLL_CTL_ADD1, thread->pipe_fd[0], &event) < 0) | |||
554 | lwan_status_critical_perror("epoll_ctl")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 554, __FUNCTION__, "epoll_ctl"); | |||
555 | ||||
556 | if (pthread_create(&thread->self, &attr, thread_io_loop, thread)) | |||
557 | lwan_status_critical_perror("pthread_create")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 557, __FUNCTION__, "pthread_create"); | |||
558 | ||||
559 | if (pthread_attr_destroy(&attr)) | |||
560 | lwan_status_critical_perror("pthread_attr_destroy")lwan_status_critical_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 560, __FUNCTION__, "pthread_attr_destroy"); | |||
561 | ||||
562 | if (spsc_queue_init(&thread->pending_fds, n_queue_fds) < 0) { | |||
563 | lwan_status_critical("Could not initialize pending fd "lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 564, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements" , n_queue_fds) | |||
564 | "queue width %zu elements", n_queue_fds)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 564, __FUNCTION__, "Could not initialize pending fd " "queue width %zu elements" , n_queue_fds); | |||
565 | } | |||
566 | } | |||
567 | ||||
568 | void lwan_thread_nudge(struct lwan_thread *t) | |||
569 | { | |||
570 | uint64_t event = 1; | |||
571 | ||||
572 | if (UNLIKELY(write(t->pipe_fd[1], &event, sizeof(event)) < 0)__builtin_expect(((write(t->pipe_fd[1], &event, sizeof (event)) < 0)), (0))) | |||
573 | lwan_status_perror("write")lwan_status_perror_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 573, __FUNCTION__, "write"); | |||
574 | } | |||
575 | ||||
576 | void lwan_thread_add_client(struct lwan_thread *t, int fd) | |||
577 | { | |||
578 | for (int i = 0; i < 10; i++) { | |||
579 | bool_Bool pushed = spsc_queue_push(&t->pending_fds, fd); | |||
580 | ||||
581 | if (LIKELY(pushed)__builtin_expect((!!(pushed)), (1))) | |||
582 | return; | |||
583 | ||||
584 | /* Queue is full; nudge the thread to consume it. */ | |||
585 | lwan_thread_nudge(t); | |||
586 | } | |||
587 | ||||
588 | lwan_status_error("Dropping connection %d", fd)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 588, __FUNCTION__, "Dropping connection %d", fd); | |||
589 | /* FIXME: send "busy" response now, even without receiving request? */ | |||
590 | close(fd); | |||
591 | } | |||
592 | ||||
593 | #if defined(__linux__1) && defined(__x86_64__1) | |||
594 | static bool_Bool read_cpu_topology(struct lwan *l, uint32_t siblings[]) | |||
595 | { | |||
596 | char path[PATH_MAX4096]; | |||
597 | ||||
598 | for (uint32_t i = 0; i < l->available_cpus; i++) | |||
599 | siblings[i] = 0xbebacafe; | |||
600 | ||||
601 | for (unsigned int i = 0; i < l->available_cpus; i++) { | |||
602 | FILE *sib; | |||
603 | uint32_t id, sibling; | |||
604 | char separator; | |||
605 | ||||
606 | snprintf(path, sizeof(path), | |||
607 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", | |||
608 | i); | |||
609 | ||||
610 | sib = fopen(path, "re"); | |||
611 | if (!sib) { | |||
612 | lwan_status_warning("Could not open `%s` to determine CPU topology",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 613, __FUNCTION__, "Could not open `%s` to determine CPU topology" , path) | |||
613 | path)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 613, __FUNCTION__, "Could not open `%s` to determine CPU topology" , path); | |||
614 | return false0; | |||
615 | } | |||
616 | ||||
617 | switch (fscanf(sib, "%u%c%u", &id, &separator, &sibling)) { | |||
618 | case 2: /* No SMT */ | |||
619 | siblings[i] = id; | |||
620 | break; | |||
621 | case 3: /* SMT */ | |||
622 | if (!(separator == ',' || separator == '-')) { | |||
623 | lwan_status_critical("Expecting either ',' or '-' for sibling separator")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 623, __FUNCTION__, "Expecting either ',' or '-' for sibling separator" ); | |||
624 | __builtin_unreachable(); | |||
625 | } | |||
626 | ||||
627 | siblings[i] = sibling; | |||
628 | break; | |||
629 | default: | |||
630 | lwan_status_critical("%s has invalid format", path)lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 630, __FUNCTION__, "%s has invalid format", path); | |||
631 | __builtin_unreachable(); | |||
632 | } | |||
633 | ||||
634 | fclose(sib); | |||
635 | } | |||
636 | ||||
637 | /* Perform a sanity check here, as some systems seem to filter out the | |||
638 | * result of sysconf() to obtain the number of configured and online | |||
639 | * CPUs but don't bother changing what's available through sysfs as far | |||
640 | * as the CPU topology information goes. It's better to fall back to a | |||
641 | * possibly non-optimal setup than just crash during startup while | |||
642 | * trying to perform an out-of-bounds array access. */ | |||
643 | for (unsigned int i = 0; i < l->available_cpus; i++) { | |||
644 | if (siblings[i] == 0xbebacafe) { | |||
645 | lwan_status_warning("Could not determine sibling for CPU %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 645, __FUNCTION__, "Could not determine sibling for CPU %d" , i); | |||
646 | return false0; | |||
647 | } | |||
648 | ||||
649 | if (siblings[i] >= l->available_cpus) { | |||
650 | lwan_status_warning("CPU information topology says CPU %d exists, "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 653, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
651 | "but max available CPUs is %d (online CPUs: %d). "lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 653, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
652 | "Is Lwan running in a (broken) container?",lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 653, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus) | |||
653 | siblings[i], l->available_cpus, l->online_cpus)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 653, __FUNCTION__, "CPU information topology says CPU %d exists, " "but max available CPUs is %d (online CPUs: %d). " "Is Lwan running in a (broken) container?" , siblings[i], l->available_cpus, l->online_cpus); | |||
654 | return false0; | |||
655 | } | |||
656 | } | |||
657 | ||||
658 | return true1; | |||
659 | } | |||
660 | ||||
661 | static void | |||
662 | siblings_to_schedtbl(struct lwan *l, uint32_t siblings[], uint32_t schedtbl[]) | |||
663 | { | |||
664 | int *seen = alloca(l->available_cpus * sizeof(int))__builtin_alloca (l->available_cpus * sizeof(int)); | |||
665 | unsigned int n_schedtbl = 0; | |||
666 | ||||
667 | for (uint32_t i = 0; i < l->available_cpus; i++) | |||
668 | seen[i] = -1; | |||
669 | ||||
670 | for (uint32_t i = 0; i < l->available_cpus; i++) { | |||
671 | if (seen[siblings[i]] < 0) { | |||
672 | seen[siblings[i]] = (int)i; | |||
673 | } else { | |||
674 | schedtbl[n_schedtbl++] = (uint32_t)seen[siblings[i]]; | |||
675 | schedtbl[n_schedtbl++] = i; | |||
676 | } | |||
677 | } | |||
678 | ||||
679 | if (n_schedtbl != l->available_cpus) | |||
680 | memcpy(schedtbl, seen, l->available_cpus * sizeof(int)); | |||
681 | } | |||
682 | ||||
683 | static bool_Bool | |||
684 | topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads) | |||
685 | { | |||
686 | uint32_t *siblings = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t)); | |||
687 | ||||
688 | if (read_cpu_topology(l, siblings)) { | |||
689 | uint32_t *affinity = alloca(l->available_cpus * sizeof(uint32_t))__builtin_alloca (l->available_cpus * sizeof(uint32_t)); | |||
690 | ||||
691 | siblings_to_schedtbl(l, siblings, affinity); | |||
692 | ||||
693 | for (uint32_t i = 0; i < n_threads; i++) | |||
694 | schedtbl[i] = affinity[i % l->available_cpus]; | |||
695 | return true1; | |||
696 | } | |||
697 | ||||
698 | for (uint32_t i = 0; i < n_threads; i++) | |||
699 | schedtbl[i] = (i / 2) % l->thread.count; | |||
700 | return false0; | |||
701 | } | |||
702 | ||||
703 | static void | |||
704 | adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t mask) | |||
705 | { | |||
706 | for (uint32_t i = 0; i < l->thread.count; i++) { | |||
707 | cpu_set_t set; | |||
708 | ||||
709 | CPU_ZERO(&set)do __builtin_memset (&set, '\0', sizeof (cpu_set_t)); while (0); | |||
710 | CPU_SET(schedtbl[i & mask], &set)(__extension__ ({ size_t __cpu = (schedtbl[i & mask]); __cpu / 8 < (sizeof (cpu_set_t)) ? (((__cpu_mask *) ((&set) ->__bits))[((__cpu) / (8 * sizeof (__cpu_mask)))] |= ((__cpu_mask ) 1 << ((__cpu) % (8 * sizeof (__cpu_mask))))) : 0; })); | |||
| ||||
711 | ||||
712 | if (pthread_setaffinity_np(l->thread.threads[i].self, sizeof(set), | |||
713 | &set)) | |||
714 | lwan_status_warning("Could not set affinity for thread %d", i)lwan_status_warning_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 714, __FUNCTION__, "Could not set affinity for thread %d", i ); | |||
715 | } | |||
716 | } | |||
717 | #elif defined(__x86_64__1) | |||
718 | static bool_Bool | |||
719 | topology_to_schedtbl(struct lwan *l, uint32_t schedtbl[], uint32_t n_threads) | |||
720 | { | |||
721 | for (uint32_t i = 0; i < n_threads; i++) | |||
722 | schedtbl[i] = (i / 2) % l->thread.count; | |||
723 | return false0; | |||
724 | } | |||
725 | ||||
726 | static void | |||
727 | adjust_threads_affinity(struct lwan *l, uint32_t *schedtbl, uint32_t n) | |||
728 | { | |||
729 | } | |||
730 | #endif | |||
731 | ||||
732 | void lwan_thread_init(struct lwan *l) | |||
733 | { | |||
734 | if (pthread_barrier_init(&l->thread.barrier, NULL((void*)0), | |||
| ||||
735 | (unsigned)l->thread.count + 1)) | |||
736 | lwan_status_critical("Could not create barrier")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 736, __FUNCTION__, "Could not create barrier"); | |||
737 | ||||
738 | lwan_status_debug("Initializing threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 738, __FUNCTION__, "Initializing threads"); | |||
739 | ||||
740 | l->thread.threads = | |||
741 | calloc((size_t)l->thread.count, sizeof(struct lwan_thread)); | |||
742 | if (!l->thread.threads) | |||
743 | lwan_status_critical("Could not allocate memory for threads")lwan_status_critical_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 743, __FUNCTION__, "Could not allocate memory for threads"); | |||
744 | ||||
745 | const size_t n_queue_fds = LWAN_MIN(l->thread.max_fd / l->thread.count,({ const __typeof__((l->thread.max_fd / l->thread.count ) + 0) lwan_tmp_id8 = (l->thread.max_fd / l->thread.count ); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size ())) + 0) lwan_tmp_id9 = ((size_t)(2 * lwan_socket_get_backlog_size ())); lwan_tmp_id8 > lwan_tmp_id9 ? lwan_tmp_id9 : lwan_tmp_id8 ; }) | |||
746 | (size_t)(2 * lwan_socket_get_backlog_size()))({ const __typeof__((l->thread.max_fd / l->thread.count ) + 0) lwan_tmp_id8 = (l->thread.max_fd / l->thread.count ); const __typeof__(((size_t)(2 * lwan_socket_get_backlog_size ())) + 0) lwan_tmp_id9 = ((size_t)(2 * lwan_socket_get_backlog_size ())); lwan_tmp_id8 > lwan_tmp_id9 ? lwan_tmp_id9 : lwan_tmp_id8 ; }); | |||
747 | lwan_status_debug("Pending client file descriptor queue has %zu items", n_queue_fds)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 747, __FUNCTION__, "Pending client file descriptor queue has %zu items" , n_queue_fds); | |||
748 | for (unsigned int i = 0; i
| |||
749 | create_thread(l, &l->thread.threads[i], n_queue_fds); | |||
750 | ||||
751 | const unsigned int total_conns = l->thread.max_fd * l->thread.count; | |||
752 | #ifdef __x86_64__1 | |||
753 | static_assert(sizeof(struct lwan_connection) == 32,extern int (*__Static_assert_function (void)) [!!sizeof (struct { int __error_if_negative: (sizeof(struct lwan_connection) == 32) ? 2 : -1; })] | |||
754 | "Two connections per cache line")extern int (*__Static_assert_function (void)) [!!sizeof (struct { int __error_if_negative: (sizeof(struct lwan_connection) == 32) ? 2 : -1; })]; | |||
755 | ||||
756 | lwan_status_debug("%d CPUs of %d are online. "lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 758, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus) | |||
757 | "Reading topology to pre-schedule clients",lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 758, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus) | |||
758 | l->online_cpus, l->available_cpus)lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 758, __FUNCTION__, "%d CPUs of %d are online. " "Reading topology to pre-schedule clients" , l->online_cpus, l->available_cpus); | |||
759 | ||||
760 | /* | |||
761 | * Pre-schedule each file descriptor, to reduce some operations in the | |||
762 | * fast path. | |||
763 | * | |||
764 | * Since struct lwan_connection is guaranteed to be 32-byte long, two of | |||
765 | * them can fill up a cache line. Assume siblings share cache lines and | |||
766 | * use the CPU topology to group two connections per cache line in such | |||
767 | * a way that false sharing is avoided. | |||
768 | */ | |||
769 | uint32_t n_threads = (uint32_t)lwan_nextpow2((size_t)((l->thread.count - 1) * 2)); | |||
770 | uint32_t *schedtbl = alloca(n_threads * sizeof(uint32_t))__builtin_alloca (n_threads * sizeof(uint32_t)); | |||
771 | ||||
772 | bool_Bool adj_affinity = topology_to_schedtbl(l, schedtbl, n_threads); | |||
773 | ||||
774 | n_threads--; /* Transform count into mask for AND below */ | |||
775 | ||||
776 | if (adj_affinity
| |||
777 | adjust_threads_affinity(l, schedtbl, n_threads); | |||
778 | ||||
779 | for (unsigned int i = 0; i < total_conns; i++) | |||
780 | l->conns[i].thread = &l->thread.threads[schedtbl[i & n_threads]]; | |||
781 | #else | |||
782 | for (unsigned int i = 0; i < total_conns; i++) | |||
783 | l->conns[i].thread = &l->thread.threads[i % l->thread.count]; | |||
784 | #endif | |||
785 | ||||
786 | pthread_barrier_wait(&l->thread.barrier); | |||
787 | ||||
788 | lwan_status_debug("Worker threads created and ready to serve")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 788, __FUNCTION__, "Worker threads created and ready to serve" ); | |||
789 | } | |||
790 | ||||
791 | void lwan_thread_shutdown(struct lwan *l) | |||
792 | { | |||
793 | lwan_status_debug("Shutting down threads")lwan_status_debug_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-thread.c" , 793, __FUNCTION__, "Shutting down threads"); | |||
794 | ||||
795 | for (unsigned int i = 0; i < l->thread.count; i++) { | |||
796 | struct lwan_thread *t = &l->thread.threads[i]; | |||
797 | ||||
798 | close(t->epoll_fd); | |||
799 | lwan_thread_nudge(t); | |||
800 | } | |||
801 | ||||
802 | pthread_barrier_wait(&l->thread.barrier); | |||
803 | pthread_barrier_destroy(&l->thread.barrier); | |||
804 | ||||
805 | for (unsigned int i = 0; i < l->thread.count; i++) { | |||
806 | struct lwan_thread *t = &l->thread.threads[i]; | |||
807 | ||||
808 | close(t->pipe_fd[0]); | |||
809 | #if !defined(HAVE_EVENTFD) | |||
810 | close(t->pipe_fd[1]); | |||
811 | #endif | |||
812 | ||||
813 | pthread_join(l->thread.threads[i].self, NULL((void*)0)); | |||
814 | spsc_queue_free(&t->pending_fds); | |||
815 | timeouts_close(t->wheel); | |||
816 | } | |||
817 | ||||
818 | free(l->thread.threads); | |||
819 | } |