Linux 6.14-rc1
[linux.git] / tools / testing / vsock / vsock_perf.c
blob75971ac708c9a3df9d126c3c02ffda9c321680a5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vsock_perf - benchmark utility for vsock.
5 * Copyright (C) 2022 SberDevices.
7 * Author: Arseniy Krasnov <AVKrasnov@sberdevices.ru>
8 */
9 #include <getopt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <time.h>
17 #include <stdint.h>
18 #include <poll.h>
19 #include <sys/socket.h>
20 #include <linux/vm_sockets.h>
21 #include <sys/mman.h>
23 #include "msg_zerocopy_common.h"
25 #define DEFAULT_BUF_SIZE_BYTES (128 * 1024)
26 #define DEFAULT_TO_SEND_BYTES (64 * 1024)
27 #define DEFAULT_VSOCK_BUF_BYTES (256 * 1024)
28 #define DEFAULT_RCVLOWAT_BYTES 1
29 #define DEFAULT_PORT 1234
31 #define BYTES_PER_GB (1024 * 1024 * 1024ULL)
32 #define NSEC_PER_SEC (1000000000ULL)
34 static unsigned int port = DEFAULT_PORT;
35 static unsigned long buf_size_bytes = DEFAULT_BUF_SIZE_BYTES;
36 static unsigned long long vsock_buf_bytes = DEFAULT_VSOCK_BUF_BYTES;
37 static bool zerocopy;
39 static void error(const char *s)
41 perror(s);
42 exit(EXIT_FAILURE);
45 static time_t current_nsec(void)
47 struct timespec ts;
49 if (clock_gettime(CLOCK_REALTIME, &ts))
50 error("clock_gettime");
52 return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
55 /* From lib/cmdline.c. */
56 static unsigned long memparse(const char *ptr)
58 char *endptr;
60 unsigned long long ret = strtoull(ptr, &endptr, 0);
62 switch (*endptr) {
63 case 'E':
64 case 'e':
65 ret <<= 10;
66 case 'P':
67 case 'p':
68 ret <<= 10;
69 case 'T':
70 case 't':
71 ret <<= 10;
72 case 'G':
73 case 'g':
74 ret <<= 10;
75 case 'M':
76 case 'm':
77 ret <<= 10;
78 case 'K':
79 case 'k':
80 ret <<= 10;
81 endptr++;
82 default:
83 break;
86 return ret;
89 static void vsock_increase_buf_size(int fd)
91 if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_MAX_SIZE,
92 &vsock_buf_bytes, sizeof(vsock_buf_bytes)))
93 error("setsockopt(SO_VM_SOCKETS_BUFFER_MAX_SIZE)");
95 if (setsockopt(fd, AF_VSOCK, SO_VM_SOCKETS_BUFFER_SIZE,
96 &vsock_buf_bytes, sizeof(vsock_buf_bytes)))
97 error("setsockopt(SO_VM_SOCKETS_BUFFER_SIZE)");
100 static int vsock_connect(unsigned int cid, unsigned int port)
102 union {
103 struct sockaddr sa;
104 struct sockaddr_vm svm;
105 } addr = {
106 .svm = {
107 .svm_family = AF_VSOCK,
108 .svm_port = port,
109 .svm_cid = cid,
112 int fd;
114 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
116 if (fd < 0) {
117 perror("socket");
118 return -1;
121 if (connect(fd, &addr.sa, sizeof(addr.svm)) < 0) {
122 perror("connect");
123 close(fd);
124 return -1;
127 return fd;
130 static float get_gbps(unsigned long bits, time_t ns_delta)
132 return ((float)bits / 1000000000ULL) /
133 ((float)ns_delta / NSEC_PER_SEC);
136 static void run_receiver(int rcvlowat_bytes)
138 unsigned int read_cnt;
139 time_t rx_begin_ns;
140 time_t in_read_ns;
141 size_t total_recv;
142 int client_fd;
143 char *data;
144 int fd;
145 union {
146 struct sockaddr sa;
147 struct sockaddr_vm svm;
148 } addr = {
149 .svm = {
150 .svm_family = AF_VSOCK,
151 .svm_port = port,
152 .svm_cid = VMADDR_CID_ANY,
155 union {
156 struct sockaddr sa;
157 struct sockaddr_vm svm;
158 } clientaddr;
160 socklen_t clientaddr_len = sizeof(clientaddr.svm);
162 printf("Run as receiver\n");
163 printf("Listen port %u\n", port);
164 printf("RX buffer %lu bytes\n", buf_size_bytes);
165 printf("vsock buffer %llu bytes\n", vsock_buf_bytes);
166 printf("SO_RCVLOWAT %d bytes\n", rcvlowat_bytes);
168 fd = socket(AF_VSOCK, SOCK_STREAM, 0);
170 if (fd < 0)
171 error("socket");
173 if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0)
174 error("bind");
176 if (listen(fd, 1) < 0)
177 error("listen");
179 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
181 if (client_fd < 0)
182 error("accept");
184 vsock_increase_buf_size(client_fd);
186 if (setsockopt(client_fd, SOL_SOCKET, SO_RCVLOWAT,
187 &rcvlowat_bytes,
188 sizeof(rcvlowat_bytes)))
189 error("setsockopt(SO_RCVLOWAT)");
191 data = malloc(buf_size_bytes);
193 if (!data) {
194 fprintf(stderr, "'malloc()' failed\n");
195 exit(EXIT_FAILURE);
198 read_cnt = 0;
199 in_read_ns = 0;
200 total_recv = 0;
201 rx_begin_ns = current_nsec();
203 while (1) {
204 struct pollfd fds = { 0 };
206 fds.fd = client_fd;
207 fds.events = POLLIN | POLLERR |
208 POLLHUP | POLLRDHUP;
210 if (poll(&fds, 1, -1) < 0)
211 error("poll");
213 if (fds.revents & POLLERR) {
214 fprintf(stderr, "'poll()' error\n");
215 exit(EXIT_FAILURE);
218 if (fds.revents & POLLIN) {
219 ssize_t bytes_read;
220 time_t t;
222 t = current_nsec();
223 bytes_read = read(fds.fd, data, buf_size_bytes);
224 in_read_ns += (current_nsec() - t);
225 read_cnt++;
227 if (!bytes_read)
228 break;
230 if (bytes_read < 0) {
231 perror("read");
232 exit(EXIT_FAILURE);
235 total_recv += bytes_read;
238 if (fds.revents & (POLLHUP | POLLRDHUP))
239 break;
242 printf("total bytes received: %zu\n", total_recv);
243 printf("rx performance: %f Gbits/s\n",
244 get_gbps(total_recv * 8, current_nsec() - rx_begin_ns));
245 printf("total time in 'read()': %f sec\n", (float)in_read_ns / NSEC_PER_SEC);
246 printf("average time in 'read()': %f ns\n", (float)in_read_ns / read_cnt);
247 printf("POLLIN wakeups: %i\n", read_cnt);
249 free(data);
250 close(client_fd);
251 close(fd);
254 static void enable_so_zerocopy(int fd)
256 int val = 1;
258 if (setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val))) {
259 perror("setsockopt");
260 exit(EXIT_FAILURE);
264 static void run_sender(int peer_cid, unsigned long to_send_bytes)
266 time_t tx_begin_ns;
267 time_t tx_total_ns;
268 size_t total_send;
269 time_t time_in_send;
270 void *data;
271 int fd;
273 if (zerocopy)
274 printf("Run as sender MSG_ZEROCOPY\n");
275 else
276 printf("Run as sender\n");
278 printf("Connect to %i:%u\n", peer_cid, port);
279 printf("Send %lu bytes\n", to_send_bytes);
280 printf("TX buffer %lu bytes\n", buf_size_bytes);
282 fd = vsock_connect(peer_cid, port);
284 if (fd < 0)
285 exit(EXIT_FAILURE);
287 if (zerocopy) {
288 enable_so_zerocopy(fd);
290 data = mmap(NULL, buf_size_bytes, PROT_READ | PROT_WRITE,
291 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
292 if (data == MAP_FAILED) {
293 perror("mmap");
294 exit(EXIT_FAILURE);
296 } else {
297 data = malloc(buf_size_bytes);
299 if (!data) {
300 fprintf(stderr, "'malloc()' failed\n");
301 exit(EXIT_FAILURE);
305 memset(data, 0, buf_size_bytes);
306 total_send = 0;
307 time_in_send = 0;
308 tx_begin_ns = current_nsec();
310 while (total_send < to_send_bytes) {
311 ssize_t sent;
312 size_t rest_bytes;
313 time_t before;
315 rest_bytes = to_send_bytes - total_send;
317 before = current_nsec();
318 sent = send(fd, data, (rest_bytes > buf_size_bytes) ?
319 buf_size_bytes : rest_bytes,
320 zerocopy ? MSG_ZEROCOPY : 0);
321 time_in_send += (current_nsec() - before);
323 if (sent <= 0)
324 error("write");
326 total_send += sent;
328 if (zerocopy) {
329 struct pollfd fds = { 0 };
331 fds.fd = fd;
333 if (poll(&fds, 1, -1) < 0) {
334 perror("poll");
335 exit(EXIT_FAILURE);
338 if (!(fds.revents & POLLERR)) {
339 fprintf(stderr, "POLLERR expected\n");
340 exit(EXIT_FAILURE);
343 vsock_recv_completion(fd, NULL);
347 tx_total_ns = current_nsec() - tx_begin_ns;
349 printf("total bytes sent: %zu\n", total_send);
350 printf("tx performance: %f Gbits/s\n",
351 get_gbps(total_send * 8, time_in_send));
352 printf("total time in tx loop: %f sec\n",
353 (float)tx_total_ns / NSEC_PER_SEC);
354 printf("time in 'send()': %f sec\n",
355 (float)time_in_send / NSEC_PER_SEC);
357 close(fd);
359 if (zerocopy)
360 munmap(data, buf_size_bytes);
361 else
362 free(data);
365 static const char optstring[] = "";
366 static const struct option longopts[] = {
368 .name = "help",
369 .has_arg = no_argument,
370 .val = 'H',
373 .name = "sender",
374 .has_arg = required_argument,
375 .val = 'S',
378 .name = "port",
379 .has_arg = required_argument,
380 .val = 'P',
383 .name = "bytes",
384 .has_arg = required_argument,
385 .val = 'M',
388 .name = "buf-size",
389 .has_arg = required_argument,
390 .val = 'B',
393 .name = "vsk-size",
394 .has_arg = required_argument,
395 .val = 'V',
398 .name = "rcvlowat",
399 .has_arg = required_argument,
400 .val = 'R',
403 .name = "zerocopy",
404 .has_arg = no_argument,
405 .val = 'Z',
410 static void usage(void)
412 printf("Usage: ./vsock_perf [--help] [options]\n"
413 "\n"
414 "This is benchmarking utility, to test vsock performance.\n"
415 "It runs in two modes: sender or receiver. In sender mode, it\n"
416 "connects to the specified CID and starts data transmission.\n"
417 "\n"
418 "Options:\n"
419 " --help This message\n"
420 " --sender <cid> Sender mode (receiver default)\n"
421 " <cid> of the receiver to connect to\n"
422 " --zerocopy Enable zerocopy (for sender mode only)\n"
423 " --port <port> Port (default %d)\n"
424 " --bytes <bytes>KMG Bytes to send (default %d)\n"
425 " --buf-size <bytes>KMG Data buffer size (default %d). In sender mode\n"
426 " it is the buffer size, passed to 'write()'. In\n"
427 " receiver mode it is the buffer size passed to 'read()'.\n"
428 " --vsk-size <bytes>KMG Socket buffer size (default %d)\n"
429 " --rcvlowat <bytes>KMG SO_RCVLOWAT value (default %d)\n"
430 "\n", DEFAULT_PORT, DEFAULT_TO_SEND_BYTES,
431 DEFAULT_BUF_SIZE_BYTES, DEFAULT_VSOCK_BUF_BYTES,
432 DEFAULT_RCVLOWAT_BYTES);
433 exit(EXIT_FAILURE);
436 static long strtolx(const char *arg)
438 long value;
439 char *end;
441 value = strtol(arg, &end, 10);
443 if (end != arg + strlen(arg))
444 usage();
446 return value;
449 int main(int argc, char **argv)
451 unsigned long to_send_bytes = DEFAULT_TO_SEND_BYTES;
452 int rcvlowat_bytes = DEFAULT_RCVLOWAT_BYTES;
453 int peer_cid = -1;
454 bool sender = false;
456 while (1) {
457 int opt = getopt_long(argc, argv, optstring, longopts, NULL);
459 if (opt == -1)
460 break;
462 switch (opt) {
463 case 'V': /* Peer buffer size. */
464 vsock_buf_bytes = memparse(optarg);
465 break;
466 case 'R': /* SO_RCVLOWAT value. */
467 rcvlowat_bytes = memparse(optarg);
468 break;
469 case 'P': /* Port to connect to. */
470 port = strtolx(optarg);
471 break;
472 case 'M': /* Bytes to send. */
473 to_send_bytes = memparse(optarg);
474 break;
475 case 'B': /* Size of rx/tx buffer. */
476 buf_size_bytes = memparse(optarg);
477 break;
478 case 'S': /* Sender mode. CID to connect to. */
479 peer_cid = strtolx(optarg);
480 sender = true;
481 break;
482 case 'H': /* Help. */
483 usage();
484 break;
485 case 'Z': /* Zerocopy. */
486 zerocopy = true;
487 break;
488 default:
489 usage();
493 if (!sender)
494 run_receiver(rcvlowat_bytes);
495 else
496 run_sender(peer_cid, to_send_bytes);
498 return 0;