diff options
author | John Fastabend <john.fastabend@gmail.com> | 2018-01-22 13:36:19 -0500 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-01-24 04:46:59 -0500 |
commit | 66fdd1a3cdf9bcee9790bc6b6322f0cafaf0d3f2 (patch) | |
tree | c8e415febc82b0fb5f39f8ee1da1d979b04b828e | |
parent | d7d6437acf9b4ddbbebe0a9029a30c23be141683 (diff) |
bpf: sockmap sample, report bytes/sec
Report bytes/sec sent as well as total bytes. Useful to get rough
idea how different configurations and usage patterns perform with
sockmap.
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r-- | samples/sockmap/sockmap_user.c | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/samples/sockmap/sockmap_user.c b/samples/sockmap/sockmap_user.c index 6ab3ec233bdf..661ea7e4a350 100644 --- a/samples/sockmap/sockmap_user.c +++ b/samples/sockmap/sockmap_user.c | |||
@@ -24,6 +24,7 @@ | |||
24 | #include <signal.h> | 24 | #include <signal.h> |
25 | #include <fcntl.h> | 25 | #include <fcntl.h> |
26 | #include <sys/wait.h> | 26 | #include <sys/wait.h> |
27 | #include <time.h> | ||
27 | 28 | ||
28 | #include <sys/time.h> | 29 | #include <sys/time.h> |
29 | #include <sys/types.h> | 30 | #include <sys/types.h> |
@@ -189,14 +190,16 @@ static int sockmap_init_sockets(void) | |||
189 | struct msg_stats { | 190 | struct msg_stats { |
190 | size_t bytes_sent; | 191 | size_t bytes_sent; |
191 | size_t bytes_recvd; | 192 | size_t bytes_recvd; |
193 | struct timespec start; | ||
194 | struct timespec end; | ||
192 | }; | 195 | }; |
193 | 196 | ||
194 | static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | 197 | static int msg_loop(int fd, int iov_count, int iov_length, int cnt, |
195 | struct msg_stats *s, bool tx) | 198 | struct msg_stats *s, bool tx) |
196 | { | 199 | { |
197 | struct msghdr msg = {0}; | 200 | struct msghdr msg = {0}; |
201 | int err, i, flags = MSG_NOSIGNAL; | ||
198 | struct iovec *iov; | 202 | struct iovec *iov; |
199 | int i, flags = MSG_NOSIGNAL; | ||
200 | 203 | ||
201 | iov = calloc(iov_count, sizeof(struct iovec)); | 204 | iov = calloc(iov_count, sizeof(struct iovec)); |
202 | if (!iov) | 205 | if (!iov) |
@@ -217,6 +220,7 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | |||
217 | msg.msg_iovlen = iov_count; | 220 | msg.msg_iovlen = iov_count; |
218 | 221 | ||
219 | if (tx) { | 222 | if (tx) { |
223 | clock_gettime(CLOCK_MONOTONIC, &s->start); | ||
220 | for (i = 0; i < cnt; i++) { | 224 | for (i = 0; i < cnt; i++) { |
221 | int sent = sendmsg(fd, &msg, flags); | 225 | int sent = sendmsg(fd, &msg, flags); |
222 | 226 | ||
@@ -226,6 +230,7 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | |||
226 | } | 230 | } |
227 | s->bytes_sent += sent; | 231 | s->bytes_sent += sent; |
228 | } | 232 | } |
233 | clock_gettime(CLOCK_MONOTONIC, &s->end); | ||
229 | } else { | 234 | } else { |
230 | int slct, recv, max_fd = fd; | 235 | int slct, recv, max_fd = fd; |
231 | struct timeval timeout; | 236 | struct timeval timeout; |
@@ -233,6 +238,9 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | |||
233 | fd_set w; | 238 | fd_set w; |
234 | 239 | ||
235 | total_bytes = (float)iov_count * (float)iov_length * (float)cnt; | 240 | total_bytes = (float)iov_count * (float)iov_length * (float)cnt; |
241 | err = clock_gettime(CLOCK_MONOTONIC, &s->start); | ||
242 | if (err < 0) | ||
243 | perror("recv start time: "); | ||
236 | while (s->bytes_recvd < total_bytes) { | 244 | while (s->bytes_recvd < total_bytes) { |
237 | timeout.tv_sec = 1; | 245 | timeout.tv_sec = 1; |
238 | timeout.tv_usec = 0; | 246 | timeout.tv_usec = 0; |
@@ -244,16 +252,19 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | |||
244 | slct = select(max_fd + 1, &w, NULL, NULL, &timeout); | 252 | slct = select(max_fd + 1, &w, NULL, NULL, &timeout); |
245 | if (slct == -1) { | 253 | if (slct == -1) { |
246 | perror("select()"); | 254 | perror("select()"); |
255 | clock_gettime(CLOCK_MONOTONIC, &s->end); | ||
247 | goto out_errno; | 256 | goto out_errno; |
248 | } else if (!slct) { | 257 | } else if (!slct) { |
249 | fprintf(stderr, "unexpected timeout\n"); | 258 | fprintf(stderr, "unexpected timeout\n"); |
250 | errno = -EIO; | 259 | errno = -EIO; |
260 | clock_gettime(CLOCK_MONOTONIC, &s->end); | ||
251 | goto out_errno; | 261 | goto out_errno; |
252 | } | 262 | } |
253 | 263 | ||
254 | recv = recvmsg(fd, &msg, flags); | 264 | recv = recvmsg(fd, &msg, flags); |
255 | if (recv < 0) { | 265 | if (recv < 0) { |
256 | if (errno != EWOULDBLOCK) { | 266 | if (errno != EWOULDBLOCK) { |
267 | clock_gettime(CLOCK_MONOTONIC, &s->end); | ||
257 | perror("recv failed()\n"); | 268 | perror("recv failed()\n"); |
258 | goto out_errno; | 269 | goto out_errno; |
259 | } | 270 | } |
@@ -261,6 +272,7 @@ static int msg_loop(int fd, int iov_count, int iov_length, int cnt, | |||
261 | 272 | ||
262 | s->bytes_recvd += recv; | 273 | s->bytes_recvd += recv; |
263 | } | 274 | } |
275 | clock_gettime(CLOCK_MONOTONIC, &s->end); | ||
264 | } | 276 | } |
265 | 277 | ||
266 | for (i = 0; i < iov_count; i++) | 278 | for (i = 0; i < iov_count; i++) |
@@ -274,11 +286,24 @@ out_errno: | |||
274 | return errno; | 286 | return errno; |
275 | } | 287 | } |
276 | 288 | ||
289 | static float giga = 1000000000; | ||
290 | |||
291 | static inline float sentBps(struct msg_stats s) | ||
292 | { | ||
293 | return s.bytes_sent / (s.end.tv_sec - s.start.tv_sec); | ||
294 | } | ||
295 | |||
296 | static inline float recvdBps(struct msg_stats s) | ||
297 | { | ||
298 | return s.bytes_recvd / (s.end.tv_sec - s.start.tv_sec); | ||
299 | } | ||
300 | |||
277 | static int sendmsg_test(int iov_count, int iov_buf, int cnt, int verbose) | 301 | static int sendmsg_test(int iov_count, int iov_buf, int cnt, int verbose) |
278 | { | 302 | { |
279 | int txpid, rxpid, err = 0; | 303 | int txpid, rxpid, err = 0; |
280 | struct msg_stats s = {0}; | 304 | struct msg_stats s = {0}; |
281 | int status; | 305 | int status; |
306 | float sent_Bps = 0, recvd_Bps = 0; | ||
282 | 307 | ||
283 | errno = 0; | 308 | errno = 0; |
284 | 309 | ||
@@ -289,10 +314,16 @@ static int sendmsg_test(int iov_count, int iov_buf, int cnt, int verbose) | |||
289 | fprintf(stderr, | 314 | fprintf(stderr, |
290 | "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n", | 315 | "msg_loop_rx: iov_count %i iov_buf %i cnt %i err %i\n", |
291 | iov_count, iov_buf, cnt, err); | 316 | iov_count, iov_buf, cnt, err); |
292 | fprintf(stdout, "rx_sendmsg: TX_bytes %zu RX_bytes %zu\n", | ||
293 | s.bytes_sent, s.bytes_recvd); | ||
294 | shutdown(p2, SHUT_RDWR); | 317 | shutdown(p2, SHUT_RDWR); |
295 | shutdown(p1, SHUT_RDWR); | 318 | shutdown(p1, SHUT_RDWR); |
319 | if (s.end.tv_sec - s.start.tv_sec) { | ||
320 | sent_Bps = sentBps(s); | ||
321 | recvd_Bps = recvdBps(s); | ||
322 | } | ||
323 | fprintf(stdout, | ||
324 | "rx_sendmsg: TX: %zuB %fB/s %fGB/s RX: %zuB %fB/s %fGB/s\n", | ||
325 | s.bytes_sent, sent_Bps, sent_Bps/giga, | ||
326 | s.bytes_recvd, recvd_Bps, recvd_Bps/giga); | ||
296 | exit(1); | 327 | exit(1); |
297 | } else if (rxpid == -1) { | 328 | } else if (rxpid == -1) { |
298 | perror("msg_loop_rx: "); | 329 | perror("msg_loop_rx: "); |
@@ -306,9 +337,15 @@ static int sendmsg_test(int iov_count, int iov_buf, int cnt, int verbose) | |||
306 | fprintf(stderr, | 337 | fprintf(stderr, |
307 | "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n", | 338 | "msg_loop_tx: iov_count %i iov_buf %i cnt %i err %i\n", |
308 | iov_count, iov_buf, cnt, err); | 339 | iov_count, iov_buf, cnt, err); |
309 | fprintf(stdout, "tx_sendmsg: TX_bytes %zu RX_bytes %zu\n", | ||
310 | s.bytes_sent, s.bytes_recvd); | ||
311 | shutdown(c1, SHUT_RDWR); | 340 | shutdown(c1, SHUT_RDWR); |
341 | if (s.end.tv_sec - s.start.tv_sec) { | ||
342 | sent_Bps = sentBps(s); | ||
343 | recvd_Bps = recvdBps(s); | ||
344 | } | ||
345 | fprintf(stdout, | ||
346 | "tx_sendmsg: TX: %zuB %fB/s %f GB/s RX: %zuB %fB/s %fGB/s\n", | ||
347 | s.bytes_sent, sent_Bps, sent_Bps/giga, | ||
348 | s.bytes_recvd, recvd_Bps, recvd_Bps/giga); | ||
312 | exit(1); | 349 | exit(1); |
313 | } else if (txpid == -1) { | 350 | } else if (txpid == -1) { |
314 | perror("msg_loop_tx: "); | 351 | perror("msg_loop_tx: "); |