summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kdbus/test-benchmark.c
blob: 8a9744b0050841f1c93feaf24669afcdd21952de (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <locale.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <math.h>

#include "kdbus-api.h"
#include "kdbus-test.h"
#include "kdbus-util.h"
#include "kdbus-enum.h"

#define SERVICE_NAME "foo.bar.echo"

/*
 * To have a banchmark comparison with unix socket, set:
 * user_memfd	= false;
 * compare_uds	= true;
 * attach_none	= true;		do not attached metadata
 */

static bool use_memfd = true;		/* transmit memfd? */
static bool compare_uds = false;		/* unix-socket comparison? */
static bool attach_none = false;		/* clear attach-flags? */
static char stress_payload[8192];

struct stats {
	uint64_t count;
	uint64_t latency_acc;
	uint64_t latency_low;
	uint64_t latency_high;
	uint64_t latency_avg;
	uint64_t latency_ssquares;
};

static struct stats stats;

static void reset_stats(void)
{
	stats.count = 0;
	stats.latency_acc = 0;
	stats.latency_low = UINT64_MAX;
	stats.latency_high = 0;
	stats.latency_avg = 0;
	stats.latency_ssquares = 0;
}

static void dump_stats(bool is_uds)
{
	if (stats.count > 0) {
		kdbus_printf("stats %s: %'llu packets processed, latency (nsecs) min/max/avg/dev %'7llu // %'7llu // %'7llu // %'7.f\n",
			     is_uds ? " (UNIX)" : "(KDBUS)",
			     (unsigned long long) stats.count,
			     (unsigned long long) stats.latency_low,
			     (unsigned long long) stats.latency_high,
			     (unsigned long long) stats.latency_avg,
			     sqrt(stats.latency_ssquares / stats.count));
	} else {
		kdbus_printf("*** no packets received. bus stuck?\n");
	}
}

static void add_stats(uint64_t prev)
{
	uint64_t diff, latency_avg_prev;

	diff = now(CLOCK_THREAD_CPUTIME_ID) - prev;

	stats.count++;
	stats.latency_acc += diff;

	/* see Welford62 */
	latency_avg_prev = stats.latency_avg;
	stats.latency_avg = stats.latency_acc / stats.count;
	stats.latency_ssquares += (diff - latency_avg_prev) * (diff - stats.latency_avg);

	if (stats.latency_low > diff)
		stats.latency_low = diff;

	if (stats.latency_high < diff)
		stats.latency_high = diff;
}

static int setup_simple_kdbus_msg(struct kdbus_conn *conn,
				  uint64_t dst_id,
				  struct kdbus_msg **msg_out)
{
	struct kdbus_msg *msg;
	struct kdbus_item *item;
	uint64_t size;

	size = sizeof(struct kdbus_msg);
	size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));

	msg = malloc(size);
	ASSERT_RETURN_VAL(msg, -ENOMEM);

	memset(msg, 0, size);
	msg->size = size;
	msg->src_id = conn->id;
	msg->dst_id = dst_id;
	msg->payload_type = KDBUS_PAYLOAD_DBUS;

	item = msg->items;

	item->type = KDBUS_ITEM_PAYLOAD_VEC;
	item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);
	item->vec.address = (uintptr_t) stress_payload;
	item->vec.size = sizeof(stress_payload);
	item = KDBUS_ITEM_NEXT(item);

	*msg_out = msg;

	return 0;
}

static int setup_memfd_kdbus_msg(struct kdbus_conn *conn,
				 uint64_t dst_id,
				 off_t *memfd_item_offset,
				 struct kdbus_msg **msg_out)
{
	struct kdbus_msg *msg;
	struct kdbus_item *item;
	uint64_t size;

	size = sizeof(struct kdbus_msg);
	size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
	size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));

	msg = malloc(size);
	ASSERT_RETURN_VAL(msg, -ENOMEM);

	memset(msg, 0, size);
	msg->size = size;
	msg->src_id = conn->id;
	msg->dst_id = dst_id;
	msg->payload_type = KDBUS_PAYLOAD_DBUS;

	item = msg->items;

	item->type = KDBUS_ITEM_PAYLOAD_VEC;
	item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_vec);
	item->vec.address = (uintptr_t) stress_payload;
	item->vec.size = sizeof(stress_payload);
	item = KDBUS_ITEM_NEXT(item);

	item->type = KDBUS_ITEM_PAYLOAD_MEMFD;
	item->size = KDBUS_ITEM_HEADER_SIZE + sizeof(struct kdbus_memfd);
	item->memfd.size = sizeof(uint64_t);

	*memfd_item_offset = (unsigned char *)item - (unsigned char *)msg;
	*msg_out = msg;

	return 0;
}

static int
send_echo_request(struct kdbus_conn *conn, uint64_t dst_id,
		  void *kdbus_msg, off_t memfd_item_offset)
{
	struct kdbus_cmd_send cmd = {};
	int memfd = -1;
	int ret;

	if (use_memfd) {
		uint64_t now_ns = now(CLOCK_THREAD_CPUTIME_ID);
		struct kdbus_item *item = memfd_item_offset + kdbus_msg;
		memfd = sys_memfd_create("memfd-name", 0);
		ASSERT_RETURN_VAL(memfd >= 0, memfd);

		ret = write(memfd, &now_ns, sizeof(now_ns));
		ASSERT_RETURN_VAL(ret == sizeof(now_ns), -EAGAIN);

		ret = sys_memfd_seal_set(memfd);
		ASSERT_RETURN_VAL(ret == 0, -errno);

		item->memfd.fd = memfd;
	}

	cmd.size = sizeof(cmd);
	cmd.msg_address = (uintptr_t)kdbus_msg;

	ret = kdbus_cmd_send(conn->fd, &cmd);
	ASSERT_RETURN_VAL(ret == 0, ret);

	close(memfd);

	return 0;
}

static int
handle_echo_reply(struct kdbus_conn *conn, uint64_t send_ns)
{
	int ret;
	struct kdbus_cmd_recv recv = { .size = sizeof(recv) };
	struct kdbus_msg *msg;
	const struct kdbus_item *item;
	bool has_memfd = false;

	ret = kdbus_cmd_recv(conn->fd, &recv);
	if (ret == -EAGAIN)
		return ret;

	ASSERT_RETURN_VAL(ret == 0, ret);

	if (!use_memfd)
		goto out;

	msg = (struct kdbus_msg *)(conn->buf + recv.msg.offset);

	KDBUS_ITEM_FOREACH(item, msg, items) {
		switch (item->type) {
		case KDBUS_ITEM_PAYLOAD_MEMFD: {
			char *buf;

			buf = mmap(NULL, item->memfd.size, PROT_READ,
				   MAP_PRIVATE, item->memfd.fd, 0);
			ASSERT_RETURN_VAL(buf != MAP_FAILED, -EINVAL);
			ASSERT_RETURN_VAL(item->memfd.size == sizeof(uint64_t),
					  -EINVAL);

			add_stats(*(uint64_t*)buf);
			munmap(buf, item->memfd.size);
			close(item->memfd.fd);
			has_memfd = true;
			break;
		}

		case KDBUS_ITEM_PAYLOAD_OFF:
			/* ignore */
			break;
		}
	}

out:
	if (!has_memfd)
		add_stats(send_ns);

	ret = kdbus_free(conn, recv.msg.offset);
	ASSERT_RETURN_VAL(ret == 0, -errno);

	return 0;
}

static int benchmark(struct kdbus_test_env *env)
{
	static char buf[sizeof(stress_payload)];
	struct kdbus_msg *kdbus_msg = NULL;
	off_t memfd_cached_offset = 0;
	int ret;
	struct kdbus_conn *conn_a, *conn_b;
	struct pollfd fds[2];
	uint64_t start, send_ns, now_ns, diff;
	unsigned int i;
	int uds[2];

	setlocale(LC_ALL, "");

	for (i = 0; i < sizeof(stress_payload); i++)
		stress_payload[i] = i;

	/* setup kdbus pair */

	conn_a = kdbus_hello(env->buspath, 0, NULL, 0);
	conn_b = kdbus_hello(env->buspath, 0, NULL, 0);
	ASSERT_RETURN(conn_a && conn_b);

	ret = kdbus_add_match_empty(conn_a);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_add_match_empty(conn_b);
	ASSERT_RETURN(ret == 0);

	ret = kdbus_name_acquire(conn_a, SERVICE_NAME, NULL);
	ASSERT_RETURN(ret == 0);

	if (attach_none) {
		ret = kdbus_conn_update_attach_flags(conn_a,
						     _KDBUS_ATTACH_ALL,
						     0);
		ASSERT_RETURN(ret == 0);
	}

	/* setup UDS pair */

	ret = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0, uds);
	ASSERT_RETURN(ret == 0);

	/* setup a kdbus msg now */
	if (use_memfd) {
		ret = setup_memfd_kdbus_msg(conn_b, conn_a->id,
					    &memfd_cached_offset,
					    &kdbus_msg);
		ASSERT_RETURN(ret == 0);
	} else {
		ret = setup_simple_kdbus_msg(conn_b, conn_a->id, &kdbus_msg);
		ASSERT_RETURN(ret == 0);
	}

	/* start benchmark */

	kdbus_printf("-- entering poll loop ...\n");

	do {
		/* run kdbus benchmark */
		fds[0].fd = conn_a->fd;
		fds[1].fd = conn_b->fd;

		/* cancel any pending message */
		handle_echo_reply(conn_a, 0);

		start = now(CLOCK_THREAD_CPUTIME_ID);
		reset_stats();

		send_ns = now(CLOCK_THREAD_CPUTIME_ID);
		ret = send_echo_request(conn_b, conn_a->id,
					kdbus_msg, memfd_cached_offset);
		ASSERT_RETURN(ret == 0);

		while (1) {
			unsigned int nfds = sizeof(fds) / sizeof(fds[0]);
			unsigned int i;

			for (i = 0; i < nfds; i++) {
				fds[i].events = POLLIN | POLLPRI | POLLHUP;
				fds[i].revents = 0;
			}

			ret = poll(fds, nfds, 10);
			if (ret < 0)
				break;

			if (fds[0].revents & POLLIN) {
				ret = handle_echo_reply(conn_a, send_ns);
				ASSERT_RETURN(ret == 0);

				send_ns = now(CLOCK_THREAD_CPUTIME_ID);
				ret = send_echo_request(conn_b, conn_a->id,
							kdbus_msg,
							memfd_cached_offset);
				ASSERT_RETURN(ret == 0);
			}

			now_ns = now(CLOCK_THREAD_CPUTIME_ID);
			diff = now_ns - start;
			if (diff > 1000000000ULL) {
				start = now_ns;

				dump_stats(false);
				break;
			}
		}

		if (!compare_uds)
			continue;

		/* run unix-socket benchmark as comparison */

		fds[0].fd = uds[0];
		fds[1].fd = uds[1];

		/* cancel any pendign message */
		read(uds[1], buf, sizeof(buf));

		start = now(CLOCK_THREAD_CPUTIME_ID);
		reset_stats();

		send_ns = now(CLOCK_THREAD_CPUTIME_ID);
		ret = write(uds[0], stress_payload, sizeof(stress_payload));
		ASSERT_RETURN(ret == sizeof(stress_payload));

		while (1) {
			unsigned int nfds = sizeof(fds) / sizeof(fds[0]);
			unsigned int i;

			for (i = 0; i < nfds; i++) {
				fds[i].events = POLLIN | POLLPRI | POLLHUP;
				fds[i].revents = 0;
			}

			ret = poll(fds, nfds, 10);
			if (ret < 0)
				break;

			if (fds[1].revents & POLLIN) {
				ret = read(uds[1], buf, sizeof(buf));
				ASSERT_RETURN(ret == sizeof(buf));

				add_stats(send_ns);

				send_ns = now(CLOCK_THREAD_CPUTIME_ID);
				ret = write(uds[0], buf, sizeof(buf));
				ASSERT_RETURN(ret == sizeof(buf));
			}

			now_ns = now(CLOCK_THREAD_CPUTIME_ID);
			diff = now_ns - start;
			if (diff > 1000000000ULL) {
				start = now_ns;

				dump_stats(true);
				break;
			}
		}

	} while (kdbus_util_verbose);

	kdbus_printf("-- closing bus connections\n");

	free(kdbus_msg);

	kdbus_conn_free(conn_a);
	kdbus_conn_free(conn_b);

	return (stats.count > 1) ? TEST_OK : TEST_ERR;
}

int kdbus_test_benchmark(struct kdbus_test_env *env)
{
	use_memfd = true;
	attach_none = false;
	compare_uds = false;
	return benchmark(env);
}

int kdbus_test_benchmark_nomemfds(struct kdbus_test_env *env)
{
	use_memfd = false;
	attach_none = false;
	compare_uds = false;
	return benchmark(env);
}

int kdbus_test_benchmark_uds(struct kdbus_test_env *env)
{
	use_memfd = false;
	attach_none = true;
	compare_uds = true;
	return benchmark(env);
}