-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathperf_client.c
408 lines (346 loc) · 10.4 KB
/
perf_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */
/*
* perf_client.c -- Implements the "perf" client, see
* https://tools.ietf.org/html/draft-banks-quic-performance-00
*/
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#include <fcntl.h>
#else
#include "vc_compat.h"
#include "getopt.h"
#endif
#include <event2/event.h>
#include "lsquic.h"
#include "test_common.h"
#include "prog.h"
#include "../src/liblsquic/lsquic_logger.h"
#include "../src/liblsquic/lsquic_int_types.h"
#include "../src/liblsquic/lsquic_byteswap.h"
struct scenario
{
STAILQ_ENTRY(scenario) next;
uint64_t bytes_to_request;
uint64_t bytes_to_send; /* After the 8-byte header */
};
/* Assume all connections use the same list of scenarios, so store it in
* a global variable.
*/
static STAILQ_HEAD(, scenario) s_scenarios
= STAILQ_HEAD_INITIALIZER(s_scenarios);
static unsigned s_n_scenarios;
static unsigned s_n_conns;
struct prog s_prog;
struct lsquic_conn_ctx
{
/* Once a connection runs out of scenarios, no new streams are created
* and the connection is closed when all streams are closed.
*/
const struct scenario *next_scenario;
unsigned n_scenarios_left;
unsigned n_streams;
};
static bool
perf_create_streams (struct lsquic_conn *conn, struct lsquic_conn_ctx *conn_ctx)
{
if (conn_ctx->n_scenarios_left)
{
--conn_ctx->n_scenarios_left;
lsquic_conn_make_stream(conn);
return true;
}
else
return false;
}
static lsquic_conn_ctx_t *
perf_client_on_new_conn (void *stream_if_ctx, struct lsquic_conn *conn)
{
struct lsquic_conn_ctx *conn_ctx;
if (s_n_scenarios)
{
conn_ctx = calloc(1, sizeof(*conn_ctx));
conn_ctx->next_scenario = STAILQ_FIRST(&s_scenarios);
conn_ctx->n_scenarios_left = s_n_scenarios;
perf_create_streams(conn, conn_ctx);
++s_n_conns;
return conn_ctx;
}
else
{
lsquic_conn_close(conn);
return NULL;
}
}
static void
perf_client_on_conn_closed (struct lsquic_conn *conn)
{
struct lsquic_conn_ctx *conn_ctx;
LSQ_NOTICE("Connection closed");
conn_ctx = lsquic_conn_get_ctx(conn);
lsquic_conn_set_ctx(conn, NULL);
free(conn_ctx);
--s_n_conns;
if (0 == s_n_conns)
prog_stop(&s_prog);
}
struct lsquic_stream_ctx
{
const struct scenario *scenario;
struct {
uint64_t header; /* Big-endian */
unsigned n_h; /* Number of header bytes written */
uint64_t n_written; /* Number of non-header bytes written */
} write_state;
struct {
uint64_t n_read;
} read_state;
};
static struct lsquic_stream_ctx *
perf_client_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
{
struct lsquic_conn_ctx *conn_ctx;
struct lsquic_conn *conn;
conn = lsquic_stream_conn(stream);
conn_ctx = lsquic_conn_get_ctx(conn);
if (!stream)
{
LSQ_NOTICE("%s: got null stream: no more streams possible", __func__);
lsquic_conn_close(conn);
return NULL;
}
assert(conn_ctx->next_scenario);
struct lsquic_stream_ctx *stream_ctx = calloc(1, sizeof(*stream_ctx));
stream_ctx->scenario = conn_ctx->next_scenario;
conn_ctx->next_scenario = STAILQ_NEXT(conn_ctx->next_scenario, next);
#if __BYTE_ORDER == __LITTLE_ENDIAN
stream_ctx->write_state.header
= bswap_64(stream_ctx->scenario->bytes_to_request);
#else
stream_ctx->write_state.header = stream_ctx->scenario->bytes_to_request;
#endif
lsquic_stream_wantwrite(stream, 1);
return stream_ctx;
}
static size_t
buffer_size (void *lsqr_ctx)
{
struct lsquic_stream_ctx *const stream_ctx = lsqr_ctx;
return stream_ctx->scenario->bytes_to_send
- stream_ctx->write_state.n_written;
}
static size_t
buffer_read (void *lsqr_ctx, void *buf, size_t count)
{
struct lsquic_stream_ctx *const stream_ctx = lsqr_ctx;
size_t left;
left = buffer_size(stream_ctx);
if (count > left)
count = left;
memset(buf, 0, count);
stream_ctx->write_state.n_written += count;
return count;
}
static size_t
header_size (void *lsqr_ctx)
{
struct lsquic_stream_ctx *const stream_ctx = lsqr_ctx;
return sizeof(uint64_t) - stream_ctx->write_state.n_h;
}
static size_t
header_read (void *lsqr_ctx, void *buf, size_t count)
{
struct lsquic_stream_ctx *const stream_ctx = lsqr_ctx;
const unsigned char *src;
size_t left;
left = header_size(stream_ctx);
if (count < left)
count = left;
src = (unsigned char *) &stream_ctx->write_state.header
+ sizeof(uint64_t) - left;
memcpy(buf, src, count);
stream_ctx->write_state.n_h += count;
return count;
}
static void
perf_client_on_write (struct lsquic_stream *stream,
struct lsquic_stream_ctx *stream_ctx)
{
struct lsquic_reader reader;
ssize_t nw;
if (stream_ctx->write_state.n_h >= sizeof(uint64_t))
reader = (struct lsquic_reader) {
buffer_read,
buffer_size,
stream_ctx,
};
else
reader = (struct lsquic_reader) {
header_read,
header_size,
stream_ctx,
};
nw = lsquic_stream_writef(stream, &reader);
if (nw >= 0)
LSQ_DEBUG("%s: wrote %zd bytes", __func__, nw);
else
LSQ_WARN("%s: cannot write to stream: %s", __func__, strerror(errno));
if (reader.lsqr_size(stream_ctx) == 0
&& (reader.lsqr_size == buffer_size || buffer_size(stream_ctx) == 0))
{
lsquic_stream_shutdown(stream, 1);
lsquic_stream_wantread(stream, 1);
}
}
static size_t
perf_read_and_discard (void *user_data, const unsigned char *buf,
size_t count, int fin)
{
return count;
}
static void
perf_client_on_read (struct lsquic_stream *stream,
struct lsquic_stream_ctx *stream_ctx)
{
ssize_t nr;
nr = lsquic_stream_readf(stream, perf_read_and_discard, NULL);
if (nr >= 0)
{
stream_ctx->read_state.n_read += nr;
if (nr == 0)
{
LSQ_DEBUG("reached fin after reading %"PRIu64" bytes from server",
stream_ctx->read_state.n_read);
lsquic_stream_shutdown(stream, 0);
}
}
else
{
LSQ_WARN("error reading from stream: %s, abort connection",
strerror(errno));
lsquic_stream_close(stream);
lsquic_conn_abort(lsquic_stream_conn(stream));
}
}
static void
perf_client_on_close (struct lsquic_stream *stream,
struct lsquic_stream_ctx *stream_ctx)
{
struct lsquic_conn_ctx *conn_ctx;
struct lsquic_conn *conn;
conn = lsquic_stream_conn(stream);
conn_ctx = lsquic_conn_get_ctx(conn);
if (!perf_create_streams(conn, conn_ctx))
{
LSQ_DEBUG("out of scenarios, will close connection");
lsquic_conn_close(conn);
}
free(stream_ctx);
}
const struct lsquic_stream_if perf_stream_if = {
.on_new_conn = perf_client_on_new_conn,
.on_conn_closed = perf_client_on_conn_closed,
.on_new_stream = perf_client_on_new_stream,
.on_read = perf_client_on_read,
.on_write = perf_client_on_write,
.on_close = perf_client_on_close,
};
static void
usage (const char *prog)
{
const char *const slash = strrchr(prog, '/');
if (slash)
prog = slash + 1;
printf(
"Usage: %s [opts]\n"
"\n"
"Options:\n"
" -p NREQ:NSEND Request NREQ bytes from server and, in addition, send\n"
" NSEND bytes to server. May be specified many times\n"
" and must be specified at least once.\n"
" -T FILE Print stats to FILE. If FILE is -, print stats to stdout.\n"
, prog);
}
int
main (int argc, char **argv)
{
char *p;
int opt, s;
struct sport_head sports;
struct scenario *scenario;
TAILQ_INIT(&sports);
prog_init(&s_prog, 0, &sports, &perf_stream_if, NULL);
s_prog.prog_api.ea_alpn = "perf";
s_prog.prog_settings.es_delay_onclose = 1;
while (-1 != (opt = getopt(argc, argv, PROG_OPTS "hp:T:")))
{
switch (opt) {
case 'p':
scenario = calloc(1, sizeof(*scenario));
if (!scenario)
{
perror("calloc");
exit(EXIT_FAILURE);
}
++s_n_scenarios;
STAILQ_INSERT_TAIL(&s_scenarios, scenario, next);
scenario->bytes_to_request = strtoull(optarg, &p, 10);
if (*p != ':')
{
fprintf(stderr, "invalid scenario `%s'\n", optarg);
exit(EXIT_FAILURE);
}
scenario->bytes_to_send = strtoull(p + 1, NULL, 10);
break;
case 'T':
if (0 == strcmp(optarg, "-"))
s_prog.prog_api.ea_stats_fh = stdout;
else
{
s_prog.prog_api.ea_stats_fh = fopen(optarg, "w");
if (!s_prog.prog_api.ea_stats_fh)
{
perror("fopen");
exit(1);
}
}
break;
case 'h':
usage(argv[0]);
prog_print_common_options(&s_prog, stdout);
exit(0);
default:
if (0 != prog_set_opt(&s_prog, opt, optarg))
exit(1);
}
}
if (STAILQ_EMPTY(&s_scenarios))
{
fprintf(stderr, "please specify one of more requests using -p\n");
exit(1);
}
if (0 != prog_prep(&s_prog))
{
LSQ_ERROR("could not prep");
exit(EXIT_FAILURE);
}
if (0 != prog_connect(&s_prog, NULL, 0))
{
LSQ_ERROR("could not connect");
exit(EXIT_FAILURE);
}
LSQ_DEBUG("entering event loop");
s = prog_run(&s_prog);
prog_cleanup(&s_prog);
exit(0 == s ? EXIT_SUCCESS : EXIT_FAILURE);
}