| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <pthread.h> |
| 4 |
#include <sched.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <sys/resource.h> |
| 7 |
#include <sys/wait.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-bus.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "bus-internal.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "tests.h" |
| 16 |
#include "time-util.h" |
| 17 |
|
| 18 |
#define MAX_SIZE (2*1024*1024) |
| 19 |
|
| 20 |
static usec_t arg_loop_usec = 100 * USEC_PER_MSEC; |
| 21 |
|
| 22 |
typedef enum Type { |
| 23 |
TYPE_LEGACY, |
| 24 |
TYPE_DIRECT, |
| 25 |
} Type; |
| 26 |
|
| 27 |
static void server(sd_bus *b, size_t *result) { |
| 28 |
int r; |
| 29 |
|
| 30 |
assert(result); |
| 31 |
|
| 32 |
for (;;) { |
| 33 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 34 |
|
| 35 |
r = sd_bus_process(b, &m); |
| 36 |
assert_se(r >= 0); |
| 37 |
|
| 38 |
if (r == 0) |
| 39 |
assert_se(sd_bus_wait(b, USEC_INFINITY) >= 0); |
| 40 |
if (!m) |
| 41 |
continue; |
| 42 |
|
| 43 |
if (sd_bus_message_is_method_call(m, "benchmark.server", "Ping")) |
| 44 |
assert_se(sd_bus_reply_method_return(m, NULL) >= 0); |
| 45 |
else if (sd_bus_message_is_method_call(m, "benchmark.server", "Work")) { |
| 46 |
const void *p; |
| 47 |
size_t sz; |
| 48 |
|
| 49 |
/* Make sure the mmap is mapped */ |
| 50 |
assert_se(sd_bus_message_read_array(m, 'y', &p, &sz) > 0); |
| 51 |
|
| 52 |
r = sd_bus_reply_method_return(m, NULL); |
| 53 |
assert_se(r >= 0); |
| 54 |
} else if (sd_bus_message_is_method_call(m, "benchmark.server", "Exit")) { |
| 55 |
uint64_t res; |
| 56 |
assert_se(sd_bus_message_read(m, "t", &res) > 0); |
| 57 |
|
| 58 |
*result = res; |
| 59 |
return; |
| 60 |
|
| 61 |
} else if (!sd_bus_message_is_signal(m, NULL, NULL)) |
| 62 |
assert_not_reached(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static void transaction(sd_bus *b, size_t sz, const char *server_name) { |
| 67 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 68 |
uint8_t *p; |
| 69 |
|
| 70 |
assert_se(sd_bus_message_new_method_call(b, &m, server_name, "/", "benchmark.server", "Work") >= 0); |
| 71 |
assert_se(sd_bus_message_append_array_space(m, 'y', sz, (void**) &p) >= 0); |
| 72 |
|
| 73 |
memset(p, 0x80, sz); |
| 74 |
|
| 75 |
assert_se(sd_bus_call(b, m, 0, NULL, &reply) >= 0); |
| 76 |
} |
| 77 |
|
| 78 |
static void client_bisect(const char *address, const char *server_name) { |
| 79 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *x = NULL; |
| 80 |
size_t lsize, rsize, csize; |
| 81 |
sd_bus *b; |
| 82 |
int r; |
| 83 |
|
| 84 |
r = sd_bus_new(&b); |
| 85 |
assert_se(r >= 0); |
| 86 |
|
| 87 |
r = sd_bus_set_address(b, address); |
| 88 |
assert_se(r >= 0); |
| 89 |
|
| 90 |
r = sd_bus_start(b); |
| 91 |
assert_se(r >= 0); |
| 92 |
|
| 93 |
r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL); |
| 94 |
assert_se(r >= 0); |
| 95 |
|
| 96 |
lsize = 1; |
| 97 |
rsize = MAX_SIZE; |
| 98 |
|
| 99 |
printf("SIZE\tCOPY\tMEMFD\n"); |
| 100 |
|
| 101 |
for (;;) { |
| 102 |
usec_t t; |
| 103 |
unsigned n_copying, n_memfd; |
| 104 |
|
| 105 |
csize = (lsize + rsize) / 2; |
| 106 |
|
| 107 |
if (csize <= lsize) |
| 108 |
break; |
| 109 |
|
| 110 |
if (csize <= 0) |
| 111 |
break; |
| 112 |
|
| 113 |
printf("%zu\t", csize); |
| 114 |
|
| 115 |
b->use_memfd = 0; |
| 116 |
|
| 117 |
t = now(CLOCK_MONOTONIC); |
| 118 |
for (n_copying = 0;; n_copying++) { |
| 119 |
transaction(b, csize, server_name); |
| 120 |
if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) |
| 121 |
break; |
| 122 |
} |
| 123 |
printf("%u\t", (unsigned) ((n_copying * USEC_PER_SEC) / arg_loop_usec)); |
| 124 |
|
| 125 |
b->use_memfd = -1; |
| 126 |
|
| 127 |
t = now(CLOCK_MONOTONIC); |
| 128 |
for (n_memfd = 0;; n_memfd++) { |
| 129 |
transaction(b, csize, server_name); |
| 130 |
if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) |
| 131 |
break; |
| 132 |
} |
| 133 |
printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec)); |
| 134 |
|
| 135 |
if (n_copying == n_memfd) |
| 136 |
break; |
| 137 |
|
| 138 |
if (n_copying > n_memfd) |
| 139 |
lsize = csize; |
| 140 |
else |
| 141 |
rsize = csize; |
| 142 |
} |
| 143 |
|
| 144 |
b->use_memfd = 1; |
| 145 |
assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0); |
| 146 |
assert_se(sd_bus_message_append(x, "t", csize) >= 0); |
| 147 |
assert_se(sd_bus_send(b, x, NULL) >= 0); |
| 148 |
|
| 149 |
sd_bus_unref(b); |
| 150 |
} |
| 151 |
|
| 152 |
static void client_chart(Type type, const char *address, const char *server_name, int fd) { |
| 153 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *x = NULL; |
| 154 |
size_t csize; |
| 155 |
sd_bus *b; |
| 156 |
int r; |
| 157 |
|
| 158 |
r = sd_bus_new(&b); |
| 159 |
assert_se(r >= 0); |
| 160 |
|
| 161 |
if (type == TYPE_DIRECT) { |
| 162 |
r = sd_bus_set_fd(b, fd, fd); |
| 163 |
assert_se(r >= 0); |
| 164 |
} else { |
| 165 |
r = sd_bus_set_address(b, address); |
| 166 |
assert_se(r >= 0); |
| 167 |
|
| 168 |
r = sd_bus_set_bus_client(b, true); |
| 169 |
assert_se(r >= 0); |
| 170 |
} |
| 171 |
|
| 172 |
r = sd_bus_start(b); |
| 173 |
assert_se(r >= 0); |
| 174 |
|
| 175 |
r = sd_bus_call_method(b, server_name, "/", "benchmark.server", "Ping", NULL, NULL, NULL); |
| 176 |
assert_se(r >= 0); |
| 177 |
|
| 178 |
switch (type) { |
| 179 |
case TYPE_LEGACY: |
| 180 |
printf("SIZE\tLEGACY\n"); |
| 181 |
break; |
| 182 |
case TYPE_DIRECT: |
| 183 |
printf("SIZE\tDIRECT\n"); |
| 184 |
break; |
| 185 |
} |
| 186 |
|
| 187 |
for (csize = 1; csize <= MAX_SIZE; csize *= 2) { |
| 188 |
usec_t t; |
| 189 |
unsigned n_memfd; |
| 190 |
|
| 191 |
printf("%zu\t", csize); |
| 192 |
|
| 193 |
t = now(CLOCK_MONOTONIC); |
| 194 |
for (n_memfd = 0;; n_memfd++) { |
| 195 |
transaction(b, csize, server_name); |
| 196 |
if (now(CLOCK_MONOTONIC) >= t + arg_loop_usec) |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
printf("%u\n", (unsigned) ((n_memfd * USEC_PER_SEC) / arg_loop_usec)); |
| 201 |
} |
| 202 |
|
| 203 |
b->use_memfd = 1; |
| 204 |
assert_se(sd_bus_message_new_method_call(b, &x, server_name, "/", "benchmark.server", "Exit") >= 0); |
| 205 |
assert_se(sd_bus_message_append(x, "t", csize) >= 0); |
| 206 |
assert_se(sd_bus_send(b, x, NULL) >= 0); |
| 207 |
|
| 208 |
sd_bus_unref(b); |
| 209 |
} |
| 210 |
|
| 211 |
int main(int argc, char *argv[]) { |
| 212 |
enum { |
| 213 |
MODE_BISECT, |
| 214 |
MODE_CHART, |
| 215 |
} mode = MODE_BISECT; |
| 216 |
Type type = TYPE_LEGACY; |
| 217 |
int i, pair[2] = EBADF_PAIR; |
| 218 |
_cleanup_free_ char *address = NULL, *server_name = NULL; |
| 219 |
_cleanup_close_ int bus_ref = -EBADF; |
| 220 |
const char *unique; |
| 221 |
cpu_set_t cpuset; |
| 222 |
size_t result; |
| 223 |
sd_bus *b; |
| 224 |
pid_t pid; |
| 225 |
int r; |
| 226 |
|
| 227 |
test_setup_logging(LOG_DEBUG); |
| 228 |
|
| 229 |
for (i = 1; i < argc; i++) { |
| 230 |
if (streq(argv[i], "chart")) { |
| 231 |
mode = MODE_CHART; |
| 232 |
continue; |
| 233 |
} else if (streq(argv[i], "legacy")) { |
| 234 |
type = TYPE_LEGACY; |
| 235 |
continue; |
| 236 |
} else if (streq(argv[i], "direct")) { |
| 237 |
type = TYPE_DIRECT; |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
assert_se(parse_sec(argv[i], &arg_loop_usec) >= 0); |
| 242 |
} |
| 243 |
|
| 244 |
assert_se(arg_loop_usec > 0); |
| 245 |
|
| 246 |
if (type == TYPE_LEGACY) { |
| 247 |
const char *e; |
| 248 |
|
| 249 |
e = secure_getenv("DBUS_SESSION_BUS_ADDRESS"); |
| 250 |
assert_se(e); |
| 251 |
|
| 252 |
address = strdup(e); |
| 253 |
assert_se(address); |
| 254 |
} |
| 255 |
|
| 256 |
r = sd_bus_new(&b); |
| 257 |
assert_se(r >= 0); |
| 258 |
|
| 259 |
if (type == TYPE_DIRECT) { |
| 260 |
assert_se(socketpair(AF_UNIX, SOCK_STREAM, 0, pair) >= 0); |
| 261 |
|
| 262 |
r = sd_bus_set_fd(b, pair[0], pair[0]); |
| 263 |
assert_se(r >= 0); |
| 264 |
|
| 265 |
r = sd_bus_set_server(b, true, SD_ID128_NULL); |
| 266 |
assert_se(r >= 0); |
| 267 |
} else { |
| 268 |
r = sd_bus_set_address(b, address); |
| 269 |
assert_se(r >= 0); |
| 270 |
|
| 271 |
r = sd_bus_set_bus_client(b, true); |
| 272 |
assert_se(r >= 0); |
| 273 |
} |
| 274 |
|
| 275 |
r = sd_bus_start(b); |
| 276 |
assert_se(r >= 0); |
| 277 |
|
| 278 |
if (type != TYPE_DIRECT) { |
| 279 |
r = sd_bus_get_unique_name(b, &unique); |
| 280 |
assert_se(r >= 0); |
| 281 |
|
| 282 |
server_name = strdup(unique); |
| 283 |
assert_se(server_name); |
| 284 |
} |
| 285 |
|
| 286 |
sync(); |
| 287 |
setpriority(PRIO_PROCESS, 0, -19); |
| 288 |
|
| 289 |
pid = fork(); |
| 290 |
assert_se(pid >= 0); |
| 291 |
|
| 292 |
if (pid == 0) { |
| 293 |
CPU_ZERO(&cpuset); |
| 294 |
CPU_SET(0, &cpuset); |
| 295 |
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); |
| 296 |
|
| 297 |
safe_close(bus_ref); |
| 298 |
sd_bus_unref(b); |
| 299 |
|
| 300 |
switch (mode) { |
| 301 |
case MODE_BISECT: |
| 302 |
client_bisect(address, server_name); |
| 303 |
break; |
| 304 |
|
| 305 |
case MODE_CHART: |
| 306 |
client_chart(type, address, server_name, pair[1]); |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
_exit(EXIT_SUCCESS); |
| 311 |
} |
| 312 |
|
| 313 |
CPU_ZERO(&cpuset); |
| 314 |
CPU_SET(1, &cpuset); |
| 315 |
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); |
| 316 |
|
| 317 |
server(b, &result); |
| 318 |
|
| 319 |
if (mode == MODE_BISECT) |
| 320 |
printf("Copying/memfd are equally fast at %zu bytes\n", result); |
| 321 |
|
| 322 |
assert_se(waitpid(pid, NULL, 0) == pid); |
| 323 |
|
| 324 |
safe_close(pair[1]); |
| 325 |
sd_bus_unref(b); |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |