Skip to content

Commit 17ac338

Browse files
mrpreKernel Patches Daemon
authored andcommitted
selftests/bpf: add splice option to sockmap benchmark
Add --splice option to bench_sockmap that uses splice(2) instead of read(2) in the consumer path. A global pipe is created once during setup and reused across iterations to avoid per-call pipe creation overhead. When --splice is enabled, the consumer splices data from the socket into the pipe, then reads from the pipe into the user buffer. The socket is set to O_NONBLOCK to prevent tcp_splice_read() from blocking indefinitely, as it only checks sock->file->f_flags for non-blocking mode, ignoring SPLICE_F_NONBLOCK. Also increase SO_RCVBUF to 16MB to avoid sk_psock_backlog being throttled by the default sk_rcvbuf limit, and add --verify option to optionally enable data correctness checking (disabled by default for benchmark accuracy). Benchmark results with rx-verdict-ingress mode (loopback, 8 CPUs): read(2): ~4292 MB/s splice(2) + zero-copy: ~4270 MB/s splice(2) + always-copy: ~2770 MB/s Zero-copy splice achieves near-parity with read(2), while the always-copy fallback is ~35% slower. Usage: # Steer softirqs to CPU 7 to avoid contending with the producer CPU echo 80 > /sys/class/net/lo/queues/rx-0/rps_cpus # Raise the receive buffer ceiling so the benchmark can set 16MB rcvbuf sysctl -w net.core.rmem_max=16777216 # Run the benchmark ./bench sockmap --rx-verdict-ingress --splice -c 2 -p 1 -a -d 30 Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
1 parent d24b0f2 commit 17ac338

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

tools/testing/selftests/bpf/benchs/bench_sockmap.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <sys/sendfile.h>
88
#include <arpa/inet.h>
99
#include <fcntl.h>
10+
#include <unistd.h>
11+
#include <sched.h>
12+
#include <sys/syscall.h>
1013
#include <argp.h>
1114
#include "bench.h"
1215
#include "bench_sockmap_prog.skel.h"
@@ -46,6 +49,8 @@ enum SOCKMAP_ARG_FLAG {
4649
ARG_CTL_RX_STRP,
4750
ARG_CONSUMER_DELAY_TIME,
4851
ARG_PRODUCER_DURATION,
52+
ARG_CTL_SPLICE,
53+
ARG_CTL_VERIFY,
4954
};
5055

5156
#define TXMODE_NORMAL() \
@@ -110,6 +115,9 @@ static struct socmap_ctx {
110115
int delay_consumer;
111116
int prod_run_time;
112117
int strp_size;
118+
bool use_splice;
119+
bool verify;
120+
int pipefd[2];
113121
} ctx = {
114122
.prod_send = 0,
115123
.user_read = 0,
@@ -119,6 +127,9 @@ static struct socmap_ctx {
119127
.delay_consumer = 0,
120128
.prod_run_time = 0,
121129
.strp_size = 0,
130+
.use_splice = false,
131+
.verify = false,
132+
.pipefd = {-1, -1},
122133
};
123134

124135
static void bench_sockmap_prog_destroy(void)
@@ -130,6 +141,11 @@ static void bench_sockmap_prog_destroy(void)
130141
close(ctx.fds[i]);
131142
}
132143

144+
if (ctx.pipefd[0] >= 0)
145+
close(ctx.pipefd[0]);
146+
if (ctx.pipefd[1] >= 0)
147+
close(ctx.pipefd[1]);
148+
133149
bench_sockmap_prog__destroy(ctx.skel);
134150
}
135151

@@ -320,6 +336,7 @@ static int setup_tx_sockmap(void)
320336

321337
static void setup(void)
322338
{
339+
int rcvbuf = 16 * 1024 * 1024;
323340
int err;
324341

325342
ctx.skel = bench_sockmap_prog__open_and_load();
@@ -350,6 +367,18 @@ static void setup(void)
350367
goto err;
351368
}
352369

370+
if (ctx.use_splice) {
371+
if (pipe(ctx.pipefd)) {
372+
fprintf(stderr, "pipe error:%d\n", errno);
373+
goto err;
374+
}
375+
}
376+
377+
setsockopt(ctx.c2, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
378+
379+
if (ctx.use_splice)
380+
set_non_block(ctx.c2, true);
381+
353382
return;
354383

355384
err:
@@ -368,6 +397,8 @@ static void measure(struct bench_res *res)
368397

369398
static void verify_data(int *check_pos, char *buf, int rcv)
370399
{
400+
if (!ctx.verify)
401+
return;
371402
for (int i = 0 ; i < rcv; i++) {
372403
if (buf[i] != snd_data[(*check_pos) % DATA_REPEAT_SIZE]) {
373404
fprintf(stderr, "verify data fail");
@@ -388,6 +419,9 @@ static void *consumer(void *input)
388419
char *buf = malloc(recv_buf_size);
389420
int delay_read = ctx.delay_consumer;
390421

422+
printf("cons[%d] started, tid=%ld cpu=%d\n",
423+
tid, syscall(SYS_gettid), sched_getcpu());
424+
391425
if (!buf) {
392426
fprintf(stderr, "fail to init read buffer");
393427
return NULL;
@@ -419,7 +453,15 @@ static void *consumer(void *input)
419453
}
420454
/* read real endpoint by consumer 0 */
421455
atomic_inc(&ctx.read_calls);
422-
rcv = read(ctx.c2, buf, recv_buf_size);
456+
if (ctx.use_splice) {
457+
rcv = splice(ctx.c2, NULL, ctx.pipefd[1],
458+
NULL, recv_buf_size,
459+
SPLICE_F_NONBLOCK);
460+
if (rcv > 0)
461+
rcv = read(ctx.pipefd[0], buf, rcv);
462+
} else {
463+
rcv = read(ctx.c2, buf, recv_buf_size);
464+
}
423465
if (rcv < 0 && errno != EAGAIN) {
424466
fprintf(stderr, "%s fail to read c2 %d\n", __func__, errno);
425467
return NULL;
@@ -440,6 +482,9 @@ static void *producer(void *input)
440482
int target;
441483
FILE *file;
442484

485+
printf("prod started, tid=%ld cpu=%d\n",
486+
syscall(SYS_gettid), sched_getcpu());
487+
443488
file = tmpfile();
444489
if (!file) {
445490
fprintf(stderr, "create file for sendfile");
@@ -554,6 +599,10 @@ static const struct argp_option opts[] = {
554599
"delay consumer start"},
555600
{ "producer-duration", ARG_PRODUCER_DURATION, "SEC", 0,
556601
"producer duration"},
602+
{ "splice", ARG_CTL_SPLICE, NULL, 0,
603+
"use splice instead of read for consumer"},
604+
{ "verify", ARG_CTL_VERIFY, NULL, 0,
605+
"verify received data correctness"},
557606
{},
558607
};
559608

@@ -572,6 +621,12 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
572621
case ARG_CTL_RX_STRP:
573622
ctx.strp_size = strtol(arg, NULL, 10);
574623
break;
624+
case ARG_CTL_SPLICE:
625+
ctx.use_splice = true;
626+
break;
627+
case ARG_CTL_VERIFY:
628+
ctx.verify = true;
629+
break;
575630
default:
576631
return ARGP_ERR_UNKNOWN;
577632
}

0 commit comments

Comments
 (0)