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/>.
27 #include "time-util.h"
30 #include "bus-message.h"
31 #include "bus-error.h"
32 #include "bus-kernel.h"
33 #include "bus-internal.h"
35 #define MAX_SIZE (1*1024*1024)
37 static usec_t arg_loop_usec = 10 * USEC_PER_SEC;
39 static void server(sd_bus *b, size_t *result) {
43 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
45 r = sd_bus_process(b, &m);
49 assert_se(sd_bus_wait(b, (usec_t) -1) >= 0);
53 if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping"))
54 assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
55 else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) {
59 /* Make sure the mmap is mapped */
60 assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0);
62 assert_se(sd_bus_reply_method_return(b, m, NULL) >= 0);
63 } else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) {
65 assert_se(sd_bus_message_read(m, "t", &res) > 0);
71 assert_not_reached("Unknown method");
75 static void transaction(sd_bus *b, size_t sz) {
76 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
80 assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Work", &m) >= 0);
81 assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0);
83 /* Touch every page */
84 /* psz = page_size(); */
85 /* for (i = 0; i < sz; i += psz) */
88 assert_se(sd_bus_send_with_reply_and_block(b, m, 0, NULL, &reply) >= 0);
91 static void client(const char *address) {
92 _cleanup_bus_message_unref_ sd_bus_message *x = NULL;
93 size_t lsize, rsize, csize;
100 r = sd_bus_set_address(b, address);
106 assert_se(sd_bus_call_method(b, ":1.1", "/", "benchmark.server", "Ping", NULL, NULL, NULL) >= 0);
113 unsigned n_copying, n_memfd;
115 csize = (lsize + rsize) / 2;
117 log_info("Trying size=%zu", csize);
125 log_info("copying...");
128 t = now(CLOCK_MONOTONIC);
129 for (n_copying = 0;; n_copying++) {
130 transaction(b, csize);
131 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
135 log_info("%u copy transactions per second", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec));
137 log_info("sending memfd...");
140 t = now(CLOCK_MONOTONIC);
141 for (n_memfd = 0;; n_memfd++) {
142 transaction(b, csize);
143 if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec)
147 log_info("%u memfd transactions per second", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec));
149 if (n_copying == n_memfd)
152 if (n_copying > n_memfd)
158 assert_se(sd_bus_message_new_method_call(b, ":1.1", "/", "benchmark.server", "Exit", &x) >= 0);
159 assert_se(sd_bus_message_append(x, "t", csize) >= 0);
160 assert_se(sd_bus_send(b, x, NULL) >= 0);
165 int main(int argc, char *argv[]) {
166 _cleanup_free_ char *bus_name = NULL, *address = NULL;
167 _cleanup_close_ int bus_ref = -1;
174 log_set_max_level(LOG_DEBUG);
177 assert_se(parse_sec(argv[1], &arg_loop_usec) >= 0);
179 assert_se(arg_loop_usec > 0);
181 bus_ref = bus_kernel_create("deine-mutter", &bus_name);
182 if (bus_ref == -ENOENT)
183 exit(EXIT_TEST_SKIP);
185 assert_se(bus_ref >= 0);
187 address = strappend("kernel:path=", bus_name);
193 r = sd_bus_set_address(b, address);
200 setpriority(PRIO_PROCESS, 0, -19);
208 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
210 close_nointr_nofail(bus_ref);
219 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
223 log_info("Copying/memfd are equally fast at %zu", result);
225 assert_se(waitpid(pid, NULL, 0) == pid);