Debian Codegraph @

c/src/reactor/io/windows/write_pipeline.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 /*
23 * A simple write buffer pool. Each socket has a dedicated "primary"
24 * buffer and can borrow from a shared pool with limited size tuning.
25 * Could enhance e.g. with separate pools per network interface and fancier
26 * memory tuning based on interface speed, system resources, and
27 * number of connections, etc.
28 */
29
30 #ifndef _WIN32_WINNT
31 #define _WIN32_WINNT 0x0501
32 #endif
33 #if _WIN32_WINNT < 0x0501
34 #error "Proton requires Windows API support for XP or later."
35 #endif
36 #include <winsock2.h>
37 #include <Ws2tcpip.h>
38
39 #include "reactor/io.h"
40 #include "reactor/selector.h"
41 #include "reactor/selectable.h"
42
43 #include "iocp.h"
44 #include "core/util.h"
45
46 #include <proton/error.h>
47 #include <proton/object.h>
48
49 #include <assert.h>
50
51 // Max overlapped writes per socket
52 #define IOCP_MAX_OWRITES 16
53 // Write buffer size
54 #define IOCP_WBUFSIZE 16384
55
56 static void pipeline_log(const char *fmt, ...)
57 {
58 va_list ap;
59 va_start(ap, fmt);
60 vfprintf(stderr, fmt, ap);
61 va_end(ap);
62 fflush(stderr);
63 }
64
65 void pni_shared_pool_create(iocp_t *iocp)
66 {
67 // TODO: more pools (or larger one) when using multiple non-loopback interfaces
68 iocp->shared_pool_size = 16;
69 char *env = getenv("PNI_WRITE_BUFFERS"); // Internal: for debugging
70 if (env) {
71 int sz = atoi(env);
72 if (sz >= 0 && sz < 256) {
73 iocp->shared_pool_size = sz;
74 }
75 }
76 iocp->loopback_bufsize = 0;
77 env = getenv("PNI_LB_BUFSIZE"); // Internal: for debugging
78 if (env) {
79 int sz = atoi(env);
80 if (sz >= 0 && sz <= 128 * 1024) {
81 iocp->loopback_bufsize = sz;
82 }
83 }
84
85 if (iocp->shared_pool_size) {
86 iocp->shared_pool_memory = (char *) VirtualAlloc(NULL, IOCP_WBUFSIZE * iocp->shared_pool_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
87 HRESULT status = GetLastError();
88 if (!iocp->shared_pool_memory) {
89 perror("Proton write buffer pool allocation failure\n");
90 iocp->shared_pool_size = 0;
91 iocp->shared_available_count = 0;
92 return;
93 }
94
95 iocp->shared_results = (write_result_t **) malloc(iocp->shared_pool_size * sizeof(write_result_t *));
96 iocp->available_results = (write_result_t **) malloc(iocp->shared_pool_size * sizeof(write_result_t *));
97 iocp->shared_available_count = iocp->shared_pool_size;
98 char *mem = iocp->shared_pool_memory;
99 for (unsigned i = 0; i < iocp->shared_pool_size; i++) {
100 iocp->shared_results[i] = iocp->available_results[i] = pni_write_result(NULL, mem, IOCP_WBUFSIZE);
101 mem += IOCP_WBUFSIZE;
102 }
103 }
104 }
105
106 void pni_shared_pool_free(iocp_t *iocp)
107 {
108 for (unsigned i = 0; i < iocp->shared_pool_size; i++) {
109 write_result_t *result = iocp->shared_results[i];
110 if (result->in_use)
111 pipeline_log("Proton buffer pool leak\n");
112 else
113 free(result);
114 }
115 if (iocp->shared_pool_size) {
116 free(iocp->shared_results);
117 free(iocp->available_results);
118 if (iocp->shared_pool_memory) {
119 if (!VirtualFree(iocp->shared_pool_memory, 0, MEM_RELEASE)) {
120 perror("write buffers release failed");
121 }
122 iocp->shared_pool_memory = NULL;
123 }
124 }
125 }
126
127 static void shared_pool_push(write_result_t *result)
128 {
129 iocp_t *iocp = result->base.iocpd->iocp;
130 assert(iocp->shared_available_count < iocp->shared_pool_size);
131 iocp->available_results[iocp->shared_available_count++] = result;
132 }
133
134 static write_result_t *shared_pool_pop(iocp_t *iocp)
135 {
136 return iocp->shared_available_count ? iocp->available_results[--iocp->shared_available_count] : NULL;
137 }
138
139 struct write_pipeline_t {
140 iocpdesc_t *iocpd;
141 size_t pending_count;
142 write_result_t *primary;
143 size_t reserved_count;
144 size_t next_primary_index;
145 size_t depth;
146 bool is_writer;
147 };
148
149 #define CID_write_pipeline CID_pn_void
150 #define write_pipeline_compare NULL
151 #define write_pipeline_inspect NULL
152 #define write_pipeline_hashcode NULL
153
154 static void write_pipeline_initialize(void *object)
155 {
156 write_pipeline_t *pl = (write_pipeline_t *) object;
157 pl->pending_count = 0;
158 const char *pribuf = (const char *) malloc(IOCP_WBUFSIZE);
159 pl->primary = pni_write_result(NULL, pribuf, IOCP_WBUFSIZE);
160 pl->depth = 0;
161 pl->is_writer = false;
162 }
163
164 static void write_pipeline_finalize(void *object)
165 {
166 write_pipeline_t *pl = (write_pipeline_t *) object;
167 free((void *)pl->primary->buffer.start);
168 free(pl->primary);
169 }
170
171 write_pipeline_t *pni_write_pipeline(iocpdesc_t *iocpd)
172 {
173 static const pn_class_t clazz = PN_CLASS(write_pipeline);
174 write_pipeline_t *pipeline = (write_pipeline_t *) pn_class_new(&clazz, sizeof(write_pipeline_t));
175 pipeline->iocpd = iocpd;
176 pipeline->primary->base.iocpd = iocpd;
177 return pipeline;
178 }
179
180 static void confirm_as_writer(write_pipeline_t *pl)
181 {
182 if (!pl->is_writer) {
183 iocp_t *iocp = pl->iocpd->iocp;
184 iocp->writer_count++;
185 pl->is_writer = true;
186 }
187 }
188
189 static void remove_as_writer(write_pipeline_t *pl)
190 {
191 if (!pl->is_writer)
192 return;
193 iocp_t *iocp = pl->iocpd->iocp;
194 assert(iocp->writer_count);
195 pl->is_writer = false;
196 iocp->writer_count--;
197 }
198
199 /*
200 * Optimal depth will depend on properties of the NIC, server, and driver. For now,
201 * just distinguish between loopback interfaces and the rest. Optimizations in the
202 * loopback stack allow decent performance with depth 1 and actually cause major
203 * performance hiccups if set to large values.
204 */
205 static void set_depth(write_pipeline_t *pl)
206 {
207 pl->depth = 1;
208 struct sockaddr_storage sa;
209 socklen_t salen = sizeof(sa);
210 char buf[INET6_ADDRSTRLEN];
211 DWORD buflen = sizeof(buf);
212
213 if (getsockname(pl->iocpd->socket,(struct sockaddr*) &sa, &salen) == 0 &&
214 getnameinfo((struct sockaddr*) &sa, salen, buf, buflen, NULL, 0, NI_NUMERICHOST) == 0) {
215 if ((sa.ss_family == AF_INET6 && strcmp(buf, "::1")) ||
216 (sa.ss_family == AF_INET && strncmp(buf, "127.", 4))) {
217 // not loopback
218 pl->depth = IOCP_MAX_OWRITES;
219 } else {
220 iocp_t *iocp = pl->iocpd->iocp;
221 if (iocp->loopback_bufsize) {
222 const char *p = (const char *) realloc((void *) pl->primary->buffer.start, iocp->loopback_bufsize);
223 if (p) {
224 pl->primary->buffer.start = p;
225 pl->primary->buffer.size = iocp->loopback_bufsize;
226 }
227 }
228 }
229 }
230 }
231
232 // Reserve as many buffers as possible for count bytes.
233 size_t pni_write_pipeline_reserve(write_pipeline_t *pl, size_t count)
234 {
235 if (pl->primary->in_use)
236 return 0; // I.e. io->wouldblock
237 if (!pl->depth)
238 set_depth(pl);
239 if (pl->depth == 1) {
240 // always use the primary
241 pl->reserved_count = 1;
242 pl->next_primary_index = 0;
243 return 1;
244 }
245
246 iocp_t *iocp = pl->iocpd->iocp;
247 confirm_as_writer(pl);
248 size_t wanted = (count / IOCP_WBUFSIZE);
249 if (count % IOCP_WBUFSIZE)
250 wanted++;
251 size_t pending = pl->pending_count;
252 assert(pending < pl->depth);
253 size_t bufs = pn_min(wanted, pl->depth - pending);
254 // Can draw from shared pool or the primary... but share with others.
255 size_t writers = iocp->writer_count;
256 size_t shared_count = (iocp->shared_available_count + writers - 1) / writers;
257 bufs = pn_min(bufs, shared_count + 1);
258 pl->reserved_count = pending + bufs;
259
260 if (bufs == wanted &&
261 pl->reserved_count < (pl->depth / 2) &&
262 iocp->shared_available_count > (2 * writers + bufs)) {
263 // No shortage: keep the primary as spare for future use
264 pl->next_primary_index = pl->reserved_count;
265 } else if (bufs == 1) {
266 pl->next_primary_index = pending;
267 } else {
268 // let approx 1/3 drain before replenishing
269 pl->next_primary_index = ((pl->reserved_count + 2) / 3) - 1;
270 if (pl->next_primary_index < pending)
271 pl->next_primary_index = pending;
272 }
273 return bufs;
274 }
275
276 write_result_t *pni_write_pipeline_next(write_pipeline_t *pl)
277 {
278 size_t sz = pl->pending_count;
279 if (sz >= pl->reserved_count)
280 return NULL;
281 write_result_t *result;
282 if (sz == pl->next_primary_index) {
283 result = pl->primary;
284 } else {
285 assert(pl->iocpd->iocp->shared_available_count > 0);
286 result = shared_pool_pop(pl->iocpd->iocp);
287 }
288
289 result->in_use = true;
290 pl->pending_count++;
291 return result;
292 }
293
294 void pni_write_pipeline_return(write_pipeline_t *pl, write_result_t *result)
295 {
296 result->in_use = false;
297 pl->pending_count--;
298 pl->reserved_count = 0;
299 if (result != pl->primary)
300 shared_pool_push(result);
301 if (pl->pending_count == 0)
302 remove_as_writer(pl);
303 }
304
305 bool pni_write_pipeline_writable(write_pipeline_t *pl)
306 {
307 // Only writable if not full and we can guarantee a buffer:
308 return pl->pending_count < pl->depth && !pl->primary->in_use;
309 }
310
311 size_t pni_write_pipeline_size(write_pipeline_t *pl)
312 {
313 return pl->pending_count;
314 }