1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "time-util.h"
28 #include "bus-kernel.h"
29 #include "bus-internal.h"
32 #define MAX_SIZE (2*1024*1024)
34 static usec_t arg_loop_usec = 100 * USEC_PER_MSEC;
36 static void server(sd_bus *b, size_t *result) {
40 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
42 r = sd_bus_process(b, &m);
46 assert_se(sd_bus_wait(b, USEC_INFINITY) >= 0);
50 if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
51 assert_se(sd_bus_reply_method_return(m, NULL) >= 0);
52 else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
56 /* Make sure the mmap is mapped */
57 assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
59 r = sd_bus_reply_method_return(m, NULL);
61 } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
63 assert_se(sd_bus_message_read(m, "t", &res) > 0);
69 assert_not_reached("Unknown method");
73 static void transaction(sd_bus *b, size_t sz, const char *server_name) {
74 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
77 assert_se(sd_bus_message_new_method_call(b, &m, server_name, "/", "benchmark.server", "Work") >= 0);
78 assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0);
82 assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0);
85 static void client_bisect(const char *address, const char *server_name) {
86 _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
87 size_t lsize, rsize, csize;
94 r = sd_bus_set_address(b, address);
100 r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL);
106 printf("SIZE\tCOPY\tMEMFD\n");
110 unsigned n_copying, n_memfd;
112 csize = (lsize + rsize) / 2;
120 printf("%zu\t", csize);
124 t = now(CLOCK_MONOTONIC);
125 for (n_copying = 0;; n_copying++) {
126 transaction(b, csize, server_name);
127 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
130 printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
134 t = now(CLOCK_MONOTONIC);
135 for (n_memfd = 0;; n_memfd++) {
136 transaction(b, csize, server_name);
137 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
140 printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
142 if (n_copying == n_memfd)
145 if (n_copying > n_memfd)
152 assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
153 assert_se(sd_bus_message_append(x, "t", csize) >= 0);
154 assert_se(sd_bus_send(b, x, NULL) >= 0);
159 static void client_chart(const char *address, const char *server_name) {
160 _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
168 r = sd_bus_set_address(b, address);
174 assert_se(sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0);
176 printf("SIZE\tCOPY\tMEMFD\n");
178 for (csize = 1; csize <= MAX_SIZE; csize *= 2) {
180 unsigned n_copying, n_memfd;
182 printf("%zu\t", csize);
186 t = now(CLOCK_MONOTONIC);
187 for (n_copying = 0;; n_copying++) {
188 transaction(b, csize, server_name);
189 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
193 printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
197 t = now(CLOCK_MONOTONIC);
198 for (n_memfd = 0;; n_memfd++) {
199 transaction(b, csize, server_name);
200 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
204 printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
208 assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0);
209 assert_se(sd_bus_message_append(x, "t", csize) >= 0);
210 assert_se(sd_bus_send(b, x, NULL) >= 0);
215 int main(int argc, char *argv[]) {
219 } mode = MODE_BISECT;
221 _cleanup_free_ char *name = NULL, *bus_name = NULL, *address = NULL, *server_name = NULL;
222 _cleanup_close_ int bus_ref = -1;
230 for (i = 1; i < argc; i++) {
231 if (streq(argv[i], "chart")) {
236 assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0);
239 assert_se(arg_loop_usec > 0);
241 assert_se(asprintf(&name, "deine-mutter-%u", (unsigned) getpid()) >= 0);
243 bus_ref = bus_kernel_create_bus(name, false, &bus_name);
244 if (bus_ref == -ENOENT)
245 exit(EXIT_TEST_SKIP);
247 assert_se(bus_ref >= 0);
249 address = strappend("kernel:path=", bus_name);
255 r = sd_bus_set_address(b, address);
261 r = sd_bus_get_unique_name(b, &unique);
264 server_name = strdup(unique);
265 assert_se(server_name);
268 setpriority(PRIO_PROCESS, 0, -19);
276 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
283 client_bisect(address, server_name);
287 client_chart(address, server_name);
296 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
300 if (mode == MODE_BISECT)
301 printf("Copying/memfd are equally fast at %zu bytes\n", result);
303 assert_se(waitpid(pid, NULL, 0) == pid);