forked from google/neper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_stream.c
353 lines (336 loc) · 13 KB
/
tcp_stream.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
/*
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <math.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <unistd.h>
#include "common.h"
#include "flow.h"
#include "interval.h"
#include "lib.h"
#include "logging.h"
#include "sample.h"
#include "thread.h"
static inline uint32_t epoll_events(struct options *opts)
{
uint32_t events = 0;
if (opts->enable_write)
events |= EPOLLOUT;
if (opts->enable_read)
events |= EPOLLIN;
if (opts->edge_trigger)
events |= EPOLLET;
return events;
}
/**
* The function expects @fd_listen is in a "ready" state in the @epfd
* epoll set, and directly calls accept() on @fd_listen. The readiness
* should guarantee that the accept() doesn't block.
*
* After a client socket fd is obtained, a new flow is created as part
* of the thread @t.
*/
static void server_accept(int fd_listen, int epfd, struct thread *t)
{
struct options *opts = t->opts;
struct callbacks *cb = t->cb;
struct sockaddr_storage cli_addr;
socklen_t cli_len;
struct flow *flow;
int client;
cli_len = sizeof(cli_addr);
client = accept(fd_listen, (struct sockaddr *)&cli_addr, &cli_len);
if (client == -1) {
if (errno == EINTR || errno == ECONNABORTED)
return;
NP_PLOG_ERROR(cb, "accept");
return;
}
flow = addflow(t->index, epfd, client, t->next_flow_id++,
epoll_events(opts), opts, cb);
flow->itv = interval_create(opts->interval, t);
}
static void process_events(struct thread *t, int epfd,
struct epoll_event *events, int nfds, int fd_listen,
char *buf)
{
struct options *opts = t->opts;
struct callbacks *cb = t->cb;
struct timespec ts;
ssize_t num_bytes;
int i;
for (i = 0; i < nfds; i++) {
struct flow *flow = events[i].data.ptr;
if (flow->fd == t->stop_efd) {
t->stop = 1;
break;
}
if (flow->fd == fd_listen) {
server_accept(fd_listen, epfd, t);
continue;
}
if (events[i].events & EPOLLRDHUP) {
delflow(t->index, epfd, flow, cb);
continue;
}
if (opts->enable_read && (events[i].events & EPOLLIN)) {
read_again:
num_bytes = read(flow->fd, buf, opts->buffer_size);
if (num_bytes == -1) {
if (errno != EAGAIN)
NP_PLOG_ERROR(cb, "read");
continue;
}
if (num_bytes == 0) {
delflow(t->index, epfd, flow, cb);
continue;
}
flow->bytes_read += num_bytes;
flow->transactions++;
interval_collect(flow, t);
if (opts->edge_trigger)
goto read_again;
}
if (opts->enable_write && (events[i].events & EPOLLOUT)) {
write_again:
num_bytes = write(flow->fd, buf, opts->buffer_size);
if (num_bytes == -1) {
if (errno != EAGAIN)
NP_PLOG_ERROR(cb, "write");
continue;
}
if (opts->delay) {
ts.tv_sec = opts->delay / (1000*1000*1000);
ts.tv_nsec = opts->delay % (1000*1000*1000);
nanosleep(&ts, NULL);
}
if (opts->edge_trigger)
goto write_again;
}
}
}
static void client_connect(int flow_id, int epfd, struct thread *t, int j)
{
struct options *opts = t->opts;
struct callbacks *cb = t->cb;
struct addrinfo *ai = t->ai[j];
struct flow *flow;
int fd;
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd == -1)
NP_PLOG_FATAL(cb, "socket");
if (opts->min_rto)
set_min_rto(fd, opts->min_rto, cb);
if (opts->debug)
set_debug(fd, 1, cb);
if (opts->local_host)
set_local_host(fd, opts, cb);
if (do_connect(fd, ai->ai_addr, ai->ai_addrlen))
NP_PLOG_FATAL(cb, "do_connect");
flow = addflow(t->index, epfd, fd, flow_id, epoll_events(opts), opts,
cb);
flow->itv = interval_create(opts->interval, t);
}
static void run_client(struct thread *t)
{
struct options *opts = t->opts;
const int flows_in_this_thread = flows_in_thread(opts->num_flows,
opts->num_threads,
t->index);
struct callbacks *cb = t->cb;
struct epoll_event *events;
int epfd, i, j;
char *buf;
NP_LOG_INFO(cb, "flows_in_this_thread=%d", flows_in_this_thread);
epfd = epoll_create1(0);
if (epfd == -1)
NP_PLOG_FATAL(cb, "epoll_create1");
NP_LOG_INFO(cb, "t->stop_efd=%d", t->stop_efd);
epoll_add_or_die(epfd, t->stop_efd, EPOLLIN, cb);
j = t->index % t->num_hosts;
for (i = 0; i < flows_in_this_thread; i++) {
client_connect(i, epfd, t, j);
if (j < t->num_hosts - 1) {
j++;
} else {
j = 0;
}
}
events = calloc(opts->maxevents, sizeof(struct epoll_event));
buf = malloc(opts->buffer_size);
if (!buf)
NP_PLOG_FATAL(cb, "malloc");
if (opts->enable_write)
fill_random(buf, opts->buffer_size);
pthread_barrier_wait(t->ready);
while (!t->stop) {
int ms = opts->nonblocking ? 10 /* milliseconds */ : -1;
int nfds = epoll_wait(epfd, events, opts->maxevents, ms);
if (nfds == -1) {
if (errno == EINTR)
continue;
NP_PLOG_FATAL(cb, "epoll_wait");
}
process_events(t, epfd, events, nfds, -1, buf);
}
free(buf);
free(events);
do_close(epfd);
}
static void run_server(struct thread *t)
{
struct options *opts = t->opts;
struct callbacks *cb = t->cb;
struct addrinfo *ai = t->ai[0];
struct epoll_event *events;
int fd_listen, epfd;
char *buf;
fd_listen = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd_listen == -1)
NP_PLOG_FATAL(cb, "socket");
set_reuseport(fd_listen, cb);
set_reuseaddr(fd_listen, 1, cb);
if (bind(fd_listen, ai->ai_addr, ai->ai_addrlen))
NP_PLOG_FATAL(cb, "bind");
if (opts->min_rto)
set_min_rto(fd_listen, opts->min_rto, cb);
if (listen(fd_listen, opts->listen_backlog))
NP_PLOG_FATAL(cb, "listen");
epfd = epoll_create1(0);
if (epfd == -1)
NP_PLOG_FATAL(cb, "epoll_create1");
epoll_add_or_die(epfd, t->stop_efd, EPOLLIN, cb);
epoll_add_or_die(epfd, fd_listen, EPOLLIN, cb);
events = calloc(opts->maxevents, sizeof(struct epoll_event));
buf = malloc(opts->buffer_size);
if (!buf)
NP_PLOG_FATAL(cb, "malloc");
if (opts->enable_write)
fill_random(buf, opts->buffer_size);
pthread_barrier_wait(t->ready);
while (!t->stop) {
int ms = opts->nonblocking ? 10 /* milliseconds */ : -1;
int nfds = epoll_wait(epfd, events, opts->maxevents, ms);
if (nfds == -1) {
if (errno == EINTR)
continue;
NP_PLOG_FATAL(cb, "epoll_wait");
}
process_events(t, epfd, events, nfds, fd_listen, buf);
}
free(buf);
free(events);
do_close(epfd);
}
static void *worker_thread(void *arg)
{
struct thread *t = arg;
if (t->opts->client) {
int i = 0;
for (i = 0; i < t->num_hosts; i++) {
reset_port(t->ai[i], atoi(t->opts->host[i].data_port), t->cb);
}
run_client(t);
} else {
reset_port(t->ai[0], atoi(t->opts->port), t->cb);
run_server(t);
}
return NULL;
}
static void report_stats(struct thread *tinfo)
{
struct timespec *start_time;
struct sample *p, *samples;
int num_samples, i, j, tid, flow_id, start_index, end_index;
ssize_t start_total, current_total, **per_flow;
double duration, total_bytes, throughput, correlation_coefficient,
sum_xy = 0, sum_xx = 0, sum_yy = 0;
struct options *opts = tinfo[0].opts;
struct callbacks *cb = tinfo[0].cb;
num_samples = 0;
for (i = 0; i < opts->num_threads; i++)
for (p = tinfo[i].samples; p; p = p->next)
num_samples++;
if (num_samples == 0) {
NP_LOG_WARN(cb, "no sample collected");
return;
}
samples = calloc(num_samples, sizeof(struct sample));
j = 0;
for (i = 0; i < opts->num_threads; i++)
for (p = tinfo[i].samples; p; p = p->next)
samples[j++] = *p;
qsort(samples, num_samples, sizeof(samples[0]), compare_samples);
if (opts->all_samples)
print_samples(0, samples, num_samples, opts->all_samples, cb);
start_index = 0;
end_index = num_samples - 1;
PRINT(cb, "start_index", "%d", start_index);
PRINT(cb, "end_index", "%d", end_index);
PRINT(cb, "num_samples", "%d", num_samples);
if (start_index >= end_index) {
NP_LOG_WARN(cb, "insufficient number of samples");
return;
}
start_time = &samples[start_index].timestamp;
start_total = samples[start_index].bytes_read;
current_total = start_total;
per_flow = calloc(opts->num_threads, sizeof(ssize_t *));
for (i = 0; i < opts->num_threads; i++) {
int max_flow_id = 0;
for (p = tinfo[i].samples; p; p = p->next) {
if (p->flow_id > max_flow_id)
max_flow_id = p->flow_id;
}
per_flow[i] = calloc(max_flow_id + 1, sizeof(ssize_t));
}
tid = samples[start_index].tid;
flow_id = samples[start_index].flow_id;
per_flow[tid][flow_id] = start_total;
for (j = start_index + 1; j <= end_index; j++) {
tid = samples[j].tid;
flow_id = samples[j].flow_id;
current_total -= per_flow[tid][flow_id];
per_flow[tid][flow_id] = samples[j].bytes_read;
current_total += per_flow[tid][flow_id];
duration = seconds_between(start_time, &samples[j].timestamp);
total_bytes = current_total - start_total;
sum_xy += duration * total_bytes;
sum_xx += duration * duration;
sum_yy += total_bytes * total_bytes;
}
throughput = total_bytes / duration;
correlation_coefficient = sum_xy / sqrt(sum_xx * sum_yy);
PRINT(cb, "throughput_Mbps", "%.2f", throughput * 8 / 1e6);
PRINT(cb, "correlation_coefficient", "%.2f", correlation_coefficient);
for (i = 0; i < opts->num_threads; i++)
free(per_flow[i]);
free(per_flow);
PRINT(cb, "time_end", "%ld.%09ld", samples[num_samples-1].timestamp.tv_sec,
samples[num_samples-1].timestamp.tv_nsec);
free(samples);
}
int tcp_stream(struct options *opts, struct callbacks *cb)
{
if (opts->delay)
prctl(PR_SET_TIMERSLACK, 1UL);
return run_main_thread(opts, cb, worker_thread, report_stats);
}