summaryrefslogtreecommitdiffhomepage
path: root/test/perf/linux/send_recv_benchmark.cc
blob: d73e49523f18ca74b26164d46f0d962be037159f (plain)
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
// Copyright 2020 The gVisor Authors.
//
// 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 <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <cstring>

#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "benchmark/benchmark.h"
#include "test/syscalls/linux/socket_test_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/logging.h"
#include "test/util/posix_error.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"

namespace gvisor {
namespace testing {

namespace {

constexpr ssize_t kMessageSize = 1024;

class Message {
 public:
  explicit Message(int byte = 0) : Message(byte, kMessageSize, 0) {}

  explicit Message(int byte, int sz) : Message(byte, sz, 0) {}

  explicit Message(int byte, int sz, int cmsg_sz)
      : buffer_(sz, byte), cmsg_buffer_(cmsg_sz, 0) {
    iov_.iov_base = buffer_.data();
    iov_.iov_len = sz;
    hdr_.msg_iov = &iov_;
    hdr_.msg_iovlen = 1;
    hdr_.msg_control = cmsg_buffer_.data();
    hdr_.msg_controllen = cmsg_sz;
  }

  struct msghdr* header() {
    return &hdr_;
  }

 private:
  std::vector<char> buffer_;
  std::vector<char> cmsg_buffer_;
  struct iovec iov_ = {};
  struct msghdr hdr_ = {};
};

void BM_Recvmsg(benchmark::State& state) {
  int sockets[2];
  TEST_CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
  FileDescriptor send_socket(sockets[0]), recv_socket(sockets[1]);
  absl::Notification notification;
  Message send_msg('a'), recv_msg;

  ScopedThread t([&send_msg, &send_socket, &notification] {
    while (!notification.HasBeenNotified()) {
      sendmsg(send_socket.get(), send_msg.header(), 0);
    }
  });

  int64_t bytes_received = 0;
  for (auto ignored : state) {
    int n = recvmsg(recv_socket.get(), recv_msg.header(), 0);
    TEST_CHECK(n > 0);
    bytes_received += n;
  }

  notification.Notify();
  recv_socket.reset();

  state.SetBytesProcessed(bytes_received);
}

BENCHMARK(BM_Recvmsg)->UseRealTime();

void BM_Sendmsg(benchmark::State& state) {
  int sockets[2];
  TEST_CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
  FileDescriptor send_socket(sockets[0]), recv_socket(sockets[1]);
  absl::Notification notification;
  Message send_msg('a'), recv_msg;

  ScopedThread t([&recv_msg, &recv_socket, &notification] {
    while (!notification.HasBeenNotified()) {
      recvmsg(recv_socket.get(), recv_msg.header(), 0);
    }
  });

  int64_t bytes_sent = 0;
  for (auto ignored : state) {
    int n = sendmsg(send_socket.get(), send_msg.header(), 0);
    TEST_CHECK(n > 0);
    bytes_sent += n;
  }

  notification.Notify();
  send_socket.reset();

  state.SetBytesProcessed(bytes_sent);
}

BENCHMARK(BM_Sendmsg)->UseRealTime();

void BM_Recvfrom(benchmark::State& state) {
  int sockets[2];
  TEST_CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
  FileDescriptor send_socket(sockets[0]), recv_socket(sockets[1]);
  absl::Notification notification;
  char send_buffer[kMessageSize], recv_buffer[kMessageSize];

  ScopedThread t([&send_socket, &send_buffer, &notification] {
    while (!notification.HasBeenNotified()) {
      sendto(send_socket.get(), send_buffer, kMessageSize, 0, nullptr, 0);
    }
  });

  int bytes_received = 0;
  for (auto ignored : state) {
    int n = recvfrom(recv_socket.get(), recv_buffer, kMessageSize, 0, nullptr,
                     nullptr);
    TEST_CHECK(n > 0);
    bytes_received += n;
  }

  notification.Notify();
  recv_socket.reset();

  state.SetBytesProcessed(bytes_received);
}

BENCHMARK(BM_Recvfrom)->UseRealTime();

void BM_Sendto(benchmark::State& state) {
  int sockets[2];
  TEST_CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
  FileDescriptor send_socket(sockets[0]), recv_socket(sockets[1]);
  absl::Notification notification;
  char send_buffer[kMessageSize], recv_buffer[kMessageSize];

  ScopedThread t([&recv_socket, &recv_buffer, &notification] {
    while (!notification.HasBeenNotified()) {
      recvfrom(recv_socket.get(), recv_buffer, kMessageSize, 0, nullptr,
               nullptr);
    }
  });

  int64_t bytes_sent = 0;
  for (auto ignored : state) {
    int n = sendto(send_socket.get(), send_buffer, kMessageSize, 0, nullptr, 0);
    TEST_CHECK(n > 0);
    bytes_sent += n;
  }

  notification.Notify();
  send_socket.reset();

  state.SetBytesProcessed(bytes_sent);
}

BENCHMARK(BM_Sendto)->UseRealTime();

PosixErrorOr<sockaddr_storage> InetLoopbackAddr(int family) {
  struct sockaddr_storage addr;
  memset(&addr, 0, sizeof(addr));
  addr.ss_family = family;
  switch (family) {
    case AF_INET:
      reinterpret_cast<struct sockaddr_in*>(&addr)->sin_addr.s_addr =
          htonl(INADDR_LOOPBACK);
      break;
    case AF_INET6:
      reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_addr =
          in6addr_loopback;
      break;
    default:
      return PosixError(EINVAL,
                        absl::StrCat("unknown socket family: ", family));
  }
  return addr;
}

// BM_RecvmsgWithControlBuf measures the performance of recvmsg when we allocate
// space for control messages. Note that we do not expect to receive any.
void BM_RecvmsgWithControlBuf(benchmark::State& state) {
  auto listen_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP));

  // Initialize address to the loopback one.
  sockaddr_storage addr = ASSERT_NO_ERRNO_AND_VALUE(InetLoopbackAddr(AF_INET6));
  socklen_t addrlen = sizeof(addr);

  // Bind to some port then start listening.
  ASSERT_THAT(bind(listen_socket.get(),
                   reinterpret_cast<struct sockaddr*>(&addr), addrlen),
              SyscallSucceeds());

  ASSERT_THAT(listen(listen_socket.get(), SOMAXCONN), SyscallSucceeds());

  // Get the address we're listening on, then connect to it. We need to do this
  // because we're allowing the stack to pick a port for us.
  ASSERT_THAT(getsockname(listen_socket.get(),
                          reinterpret_cast<struct sockaddr*>(&addr), &addrlen),
              SyscallSucceeds());

  auto send_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP));

  ASSERT_THAT(
      RetryEINTR(connect)(send_socket.get(),
                          reinterpret_cast<struct sockaddr*>(&addr), addrlen),
      SyscallSucceeds());

  // Accept the connection.
  auto recv_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Accept(listen_socket.get(), nullptr, nullptr));

  absl::Notification notification;
  Message send_msg('a');
  // Create a msghdr with a buffer allocated for control messages.
  Message recv_msg(0, kMessageSize, /*cmsg_sz=*/24);

  ScopedThread t([&send_msg, &send_socket, &notification] {
    while (!notification.HasBeenNotified()) {
      sendmsg(send_socket.get(), send_msg.header(), 0);
    }
  });

  int64_t bytes_received = 0;
  for (auto ignored : state) {
    int n = recvmsg(recv_socket.get(), recv_msg.header(), 0);
    TEST_CHECK(n > 0);
    bytes_received += n;
  }

  notification.Notify();
  recv_socket.reset();

  state.SetBytesProcessed(bytes_received);
}

BENCHMARK(BM_RecvmsgWithControlBuf)->UseRealTime();

// BM_SendmsgTCP measures the sendmsg throughput with varying payload sizes.
//
// state.Args[0] indicates whether the underlying socket should be blocking or
// non-blocking w/ 0 indicating non-blocking and 1 to indicate blocking.
// state.Args[1] is the size of the payload to be used per sendmsg call.
void BM_SendmsgTCP(benchmark::State& state) {
  auto listen_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));

  // Initialize address to the loopback one.
  sockaddr_storage addr = ASSERT_NO_ERRNO_AND_VALUE(InetLoopbackAddr(AF_INET));
  socklen_t addrlen = sizeof(addr);

  // Bind to some port then start listening.
  ASSERT_THAT(bind(listen_socket.get(),
                   reinterpret_cast<struct sockaddr*>(&addr), addrlen),
              SyscallSucceeds());

  ASSERT_THAT(listen(listen_socket.get(), SOMAXCONN), SyscallSucceeds());

  // Get the address we're listening on, then connect to it. We need to do this
  // because we're allowing the stack to pick a port for us.
  ASSERT_THAT(getsockname(listen_socket.get(),
                          reinterpret_cast<struct sockaddr*>(&addr), &addrlen),
              SyscallSucceeds());

  auto send_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));

  ASSERT_THAT(
      RetryEINTR(connect)(send_socket.get(),
                          reinterpret_cast<struct sockaddr*>(&addr), addrlen),
      SyscallSucceeds());

  // Accept the connection.
  auto recv_socket =
      ASSERT_NO_ERRNO_AND_VALUE(Accept(listen_socket.get(), nullptr, nullptr));

  // Check if we want to run the test w/ a blocking send socket
  // or non-blocking.
  const int blocking = state.range(0);
  if (!blocking) {
    // Set the send FD to O_NONBLOCK.
    int opts;
    ASSERT_THAT(opts = fcntl(send_socket.get(), F_GETFL), SyscallSucceeds());
    opts |= O_NONBLOCK;
    ASSERT_THAT(fcntl(send_socket.get(), F_SETFL, opts), SyscallSucceeds());
  }

  absl::Notification notification;

  // Get the buffer size we should use for this iteration of the test.
  const int buf_size = state.range(1);
  Message send_msg('a', buf_size), recv_msg(0, buf_size);

  ScopedThread t([&recv_msg, &recv_socket, &notification] {
    while (!notification.HasBeenNotified()) {
      TEST_CHECK(recvmsg(recv_socket.get(), recv_msg.header(), 0) >= 0);
    }
  });

  int64_t bytes_sent = 0;
  int ncalls = 0;
  for (auto ignored : state) {
    int sent = 0;
    while (true) {
      struct msghdr hdr = {};
      struct iovec iov = {};
      struct msghdr* snd_header = send_msg.header();
      iov.iov_base = static_cast<char*>(snd_header->msg_iov->iov_base) + sent;
      iov.iov_len = snd_header->msg_iov->iov_len - sent;
      hdr.msg_iov = &iov;
      hdr.msg_iovlen = 1;
      int n = RetryEINTR(sendmsg)(send_socket.get(), &hdr, 0);
      ncalls++;
      if (n > 0) {
        sent += n;
        if (sent == buf_size) {
          break;
        }
        // n can be > 0 but less than requested size. In which case we don't
        // poll.
        continue;
      }
      // Poll the fd for it to become writable.
      struct pollfd poll_fd = {send_socket.get(), POLL_OUT, 0};
      EXPECT_THAT(RetryEINTR(poll)(&poll_fd, 1, 10),
                  SyscallSucceedsWithValue(0));
    }
    bytes_sent += static_cast<int64_t>(sent);
  }

  notification.Notify();
  send_socket.reset();
  state.SetBytesProcessed(bytes_sent);
}

void Args(benchmark::internal::Benchmark* benchmark) {
  for (int blocking = 0; blocking < 2; blocking++) {
    for (int buf_size = 1024; buf_size <= 256 << 20; buf_size *= 2) {
      benchmark->Args({blocking, buf_size});
    }
  }
}

BENCHMARK(BM_SendmsgTCP)->Apply(&Args)->UseRealTime();

}  // namespace

}  // namespace testing
}  // namespace gvisor