Skip to content

Instantly share code, notes, and snippets.

@dvdhrm
Created March 24, 2015 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvdhrm/3ac4339bf94fadc13b98 to your computer and use it in GitHub Desktop.
Save dvdhrm/3ac4339bf94fadc13b98 to your computer and use it in GitHub Desktop.
diff --git a/test/Makefile b/test/Makefile
index 9a84bdb..c10b939 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -36,6 +36,7 @@ OBJS= \
test-metadata-ns.o \
test-monitor.o \
test-names.o \
+ test-perf.o \
test-policy.o \
test-policy-ns.o \
test-policy-priv.o \
diff --git a/test/kdbus-test.c b/test/kdbus-test.c
index a43674c..74177c1 100644
--- a/test/kdbus-test.c
+++ b/test/kdbus-test.c
@@ -257,6 +257,12 @@ static const struct kdbus_test tests[] = {
.flags = TEST_CREATE_BUS | TEST_CREATE_CONN,
},
{
+ .name = "perf",
+ .desc = "performance",
+ .func = kdbus_test_perf,
+ .flags = TEST_CREATE_BUS,
+ },
+ {
.name = "benchmark",
.desc = "benchmark",
.func = kdbus_test_benchmark,
diff --git a/test/kdbus-test.h b/test/kdbus-test.h
index 6473318..9af4d9d 100644
--- a/test/kdbus-test.h
+++ b/test/kdbus-test.h
@@ -74,6 +74,7 @@ int kdbus_test_monitor(struct kdbus_test_env *env);
int kdbus_test_name_basic(struct kdbus_test_env *env);
int kdbus_test_name_conflict(struct kdbus_test_env *env);
int kdbus_test_name_queue(struct kdbus_test_env *env);
+int kdbus_test_perf(struct kdbus_test_env *env);
int kdbus_test_policy(struct kdbus_test_env *env);
int kdbus_test_policy_ns(struct kdbus_test_env *env);
int kdbus_test_policy_priv(struct kdbus_test_env *env);
diff --git a/test/test-perf.c b/test/test-perf.c
new file mode 100644
index 0000000..ee99c3b
--- /dev/null
+++ b/test/test-perf.c
@@ -0,0 +1,155 @@
+#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"
+
+static const char payload_out[8192];
+static char payload_in[8192];
+
+static int setup_kdbus_msg(struct kdbus_msg **msg_out)
+{
+ struct kdbus_item *item;
+ struct kdbus_msg *msg;
+ uint64_t size;
+
+ size = sizeof(struct kdbus_msg);
+ size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
+
+ msg = calloc(1, size);
+ if (!msg)
+ return -ENOMEM;
+
+ msg->size = size;
+ 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) payload_out;
+ item->vec.size = sizeof(payload_out);
+
+ *msg_out = msg;
+ return 0;
+}
+
+static int send_kdbus(struct kdbus_conn *c, uint64_t dst, struct kdbus_msg *m)
+{
+ struct kdbus_cmd_send cmd = {};
+
+ m->dst_id = dst;
+
+ cmd.size = sizeof(cmd);
+ cmd.msg_address = (uintptr_t)m;
+
+ return kdbus_cmd_send(c->fd, &cmd);
+}
+
+static int recv_kdbus(struct kdbus_conn *c)
+{
+ struct kdbus_cmd_recv recv = { .size = sizeof(recv) };
+ int ret;
+
+ ret = kdbus_cmd_recv(c->fd, &recv);
+ if (ret < 0)
+ return ret;
+
+ ret = kdbus_free(c, recv.msg.offset);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+int kdbus_test_perf(struct kdbus_test_env *env)
+{
+ struct kdbus_conn *conn_a, *conn_b;
+ struct kdbus_msg *kdbus_msg;
+ size_t i;
+ int ret, uds[2];
+
+ /* 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_conn_update_attach_flags(conn_a, _KDBUS_ATTACH_ALL, 0);
+ ASSERT_RETURN(ret == 0);
+ ret = kdbus_conn_update_attach_flags(conn_b, _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 messages */
+
+ if (0) {
+ /*ret = setup_memfd_kdbus_msg(conn_b, conn_a->id,
+ &memfd_cached_offset,
+ &kdbus_msg);
+ ASSERT_RETURN(ret == 0);*/
+ } else {
+ ret = setup_kdbus_msg(&kdbus_msg);
+ ASSERT_RETURN(ret == 0);
+ }
+
+ /* run roundtrip */
+
+ for (i = 0; i < 1; ++i) {
+ struct pollfd fds[2] = {};
+
+ /* run kdbus */
+
+ fds[0].fd = conn_a->fd;
+ fds[0].events = POLLIN;
+ fds[1].fd = conn_b->fd;
+ fds[1].events = POLLIN;
+
+ ret = send_kdbus(conn_a, conn_b->id, kdbus_msg);
+ ASSERT_RETURN(ret == 0);
+
+ ret = poll(fds, 2, -1);
+ ASSERT_RETURN(ret >= 0);
+
+ ASSERT_RETURN(fds[1].revents & POLLIN);
+ ret = recv_kdbus(conn_b);
+ ASSERT_RETURN(ret == 0);
+
+ /* run UDS */
+
+ fds[0].fd = uds[0];
+ fds[1].fd = uds[1];
+
+ ret = write(uds[0], payload_out, sizeof(payload_out));
+ ASSERT_RETURN(ret == sizeof(payload_out));
+
+ ret = poll(fds, 2, -1);
+ ASSERT_RETURN(ret >= 0);
+
+ ASSERT_RETURN(fds[1].revents & POLLIN);
+ ret = read(uds[1], payload_in, sizeof(payload_in));
+ ASSERT_RETURN(ret == sizeof(payload_in));
+ }
+
+ return 0;
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment