summaryrefslogtreecommitdiff
path: root/tests/udp_modes.c
blob: 654cfd8d9a27807631ca9fdf7ee7adbf428fee65 (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
/* A receiver with the udp arrangements we are choosing between, so they can
   be compared without any of them being built into the tracker first.

   It echoes what it is sent, so tests/udp_load.c drives it unchanged. There
   is no tracker work here on purpose: with nothing else happening, whatever
   the kernel's receive path costs is the whole figure, which is the clearest
   view of it. Pass a work time to dilute that back towards realism.

     cc -O2 -pthread -o udp_modes tests/udp_modes.c
     ./udp_modes 6969 4 shared 1
     ./udp_modes 6969 4 shared 32
     ./udp_modes 6969 4 per-thread 1
     ./udp_modes 6969 4 per-thread 32

   Arguments: port, threads, shared|per-thread, datagrams per syscall,
   and optionally nanoseconds of pretend work per datagram. */

#define _GNU_SOURCE /* struct mmsghdr on glibc */

#include <errno.h>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>

#if defined(__linux__) || defined(__FreeBSD__)
#define HAVE_MMSG 1
#endif

#define MAX_BATCH   256
#define BUFFER_SIZE 2048

static int         g_port;
static long        g_threads;
static long        g_batch;
static long        g_work_ns;
static int         g_shared_socket = -1; /* -1 means one socket per thread */
static _Atomic int g_running       = 1;

static void        stop(int sig) {
  (void)sig;
  atomic_store(&g_running, 0);
}

/* Which option the platform offers for spreading datagrams over a group of
   sockets bound to one address. They are not the same thing: FreeBSD's plain
   SO_REUSEPORT permits the bind without spreading anything, and Darwin's
   hands every datagram to whichever socket bound last. */
static const char *share_the_port(int sock) {
  int one = 1;

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
#ifdef SO_REUSEPORT_LB
  if (!setsockopt(sock, SOL_SOCKET, SO_REUSEPORT_LB, &one, sizeof(one)))
    return "SO_REUSEPORT_LB";
#endif
#ifdef SO_REUSEPORT
  if (!setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)))
    return "SO_REUSEPORT";
#endif
  return "neither";
}

static int bind_one(int share, const char **option) {
  struct sockaddr_in address;
  int                sock = socket(AF_INET, SOCK_DGRAM, 0);

  if (sock < 0)
    return -1;
  *option = share ? share_the_port(sock) : "exclusive";

  memset(&address, 0, sizeof(address));
  address.sin_family      = AF_INET;
  address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  address.sin_port        = htons((uint16_t)g_port);
  if (bind(sock, (struct sockaddr *)&address, sizeof(address)) < 0) {
    close(sock);
    return -1;
  }
  return sock;
}

static void pretend_to_work(void) {
  struct timespec started, now;
  long            waited = 0;

  if (!g_work_ns)
    return;
  clock_gettime(CLOCK_MONOTONIC, &started);
  while (waited < g_work_ns) {
    clock_gettime(CLOCK_MONOTONIC, &now);
    waited = (now.tv_sec - started.tv_sec) * 1000000000L + (now.tv_nsec - started.tv_nsec);
  }
}

static void one_at_a_time(int sock) {
  char               buffer[BUFFER_SIZE];
  struct sockaddr_in from;
  socklen_t          length;
  ssize_t            got;

  while (atomic_load_explicit(&g_running, memory_order_relaxed)) {
    length = sizeof(from);
    got    = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&from, &length);
    if (got < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return;
    }
    pretend_to_work();
    sendto(sock, buffer, (size_t)got, 0, (struct sockaddr *)&from, length);
  }
}

#ifdef HAVE_MMSG
static void in_batches(int sock) {
  static _Thread_local char       buffers[MAX_BATCH][BUFFER_SIZE];
  static _Thread_local struct     sockaddr_in froms[MAX_BATCH];
  static _Thread_local struct     iovec iovecs[MAX_BATCH];
  static _Thread_local struct     mmsghdr messages[MAX_BATCH];
  int                             got, i;

  while (atomic_load_explicit(&g_running, memory_order_relaxed)) {
    for (i = 0; i < g_batch; ++i) {
      iovecs[i].iov_base             = buffers[i];
      iovecs[i].iov_len              = BUFFER_SIZE;
      memset(messages + i, 0, sizeof(messages[i]));
      messages[i].msg_hdr.msg_name    = froms + i;
      messages[i].msg_hdr.msg_namelen = sizeof(froms[i]);
      messages[i].msg_hdr.msg_iov     = iovecs + i;
      messages[i].msg_hdr.msg_iovlen  = 1;
    }

    /* WAITFORONE so a quiet receiver behaves like the unbatched one rather
       than holding the first datagram until the batch fills */
    got = recvmmsg(sock, messages, (unsigned)g_batch, MSG_WAITFORONE, NULL);
    if (got < 0) {
      if (errno == EINTR || errno == EAGAIN)
        continue;
      return;
    }

    for (i = 0; i < got; ++i) {
      pretend_to_work();
      iovecs[i].iov_len              = messages[i].msg_len;
      messages[i].msg_hdr.msg_namelen = sizeof(froms[i]);
    }
    sendmmsg(sock, messages, (unsigned)got, 0);
  }
}
#endif

static void *worker(void *args) {
  const char *option = "";
  int         sock   = g_shared_socket;

  (void)args;
  if (sock < 0) {
    sock = bind_one(1, &option);
    if (sock < 0) {
      fprintf(stderr, "  a worker could not bind: %s\n", strerror(errno));
      return NULL;
    }
  }

#ifdef HAVE_MMSG
  if (g_batch > 1)
    in_batches(sock);
  else
#endif
    one_at_a_time(sock);

  if (g_shared_socket < 0)
    close(sock);
  return NULL;
}

int main(int argc, char **argv) {
  const char *how     = argc > 3 ? argv[3] : "shared";
  const char *option  = "";
  pthread_t  *threads;
  long        i;

  g_port    = argc > 1 ? atoi(argv[1]) : 6969;
  g_threads = argc > 2 ? atol(argv[2]) : 1;
  g_batch   = argc > 4 ? atol(argv[4]) : 1;
  g_work_ns = argc > 5 ? atol(argv[5]) : 0;
  if (g_batch < 1)
    g_batch = 1;
  if (g_batch > MAX_BATCH)
    g_batch = MAX_BATCH;

#ifndef HAVE_MMSG
  if (g_batch > 1) {
    fprintf(stderr, "  no recvmmsg on this platform; batching unavailable\n");
    return 2;
  }
#endif

  signal(SIGINT, stop);
  signal(SIGTERM, stop);

  if (!strcmp(how, "shared")) {
    g_shared_socket = bind_one(0, &option);
    if (g_shared_socket < 0) {
      fprintf(stderr, "  bind failed: %s\n", strerror(errno));
      return 1;
    }
  } else {
    /* Probe once so the mode line can say what the platform gave us */
    int probe = bind_one(1, &option);
    if (probe < 0) {
      fprintf(stderr, "  bind failed: %s\n", strerror(errno));
      return 1;
    }
    close(probe);
  }

  printf("  port %d, %ld threads, %s socket%s (%s), %ld per syscall, %ld ns work\n",
         g_port, g_threads, strcmp(how, "shared") ? "one per thread" : "one shared",
         strcmp(how, "shared") ? "s" : "", option, g_batch, g_work_ns);
  fflush(stdout);

  threads = calloc((size_t)g_threads, sizeof(pthread_t));
  for (i = 0; i < g_threads; ++i)
    pthread_create(threads + i, NULL, worker, NULL);
  for (i = 0; i < g_threads; ++i)
    pthread_join(threads[i], NULL);

  if (g_shared_socket >= 0)
    close(g_shared_socket);
  free(threads);
  return 0;
}