| 1 | |
| 2 |
|
| 3 | #define _GNU_SOURCE
|
| 4 | #include <argp.h>
|
| 5 | #include <linux/compiler.h>
|
| 6 | #include <sys/time.h>
|
| 7 | #include <sched.h>
|
| 8 | #include <fcntl.h>
|
| 9 | #include <pthread.h>
|
| 10 | #include <sys/sysinfo.h>
|
| 11 | #include <signal.h>
|
| 12 | #include "bench.h"
|
| 13 | #include "bpf_util.h"
|
| 14 | #include "testing_helpers.h"
|
| 15 |
|
| 16 | struct env env = {
|
| 17 | .warmup_sec = 1,
|
| 18 | .duration_sec = 5,
|
| 19 | .affinity = false,
|
| 20 | .quiet = false,
|
| 21 | .consumer_cnt = 0,
|
| 22 | .producer_cnt = 1,
|
| 23 | };
|
| 24 |
|
| 25 | static int libbpf_print_fn(enum libbpf_print_level level,
|
| 26 | const char *format, va_list args)
|
| 27 | {
|
| 28 | if (level == LIBBPF_DEBUG && !env.verbose)
|
| 29 | return 0;
|
| 30 | return vfprintf(stderr, format, args);
|
| 31 | }
|
| 32 |
|
| 33 | void setup_libbpf(void)
|
| 34 | {
|
| 35 | libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
|
| 36 | libbpf_set_print(libbpf_print_fn);
|
| 37 | }
|
| 38 |
|
| 39 | void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns)
|
| 40 | {
|
| 41 | long total = res->false_hits + res->hits + res->drops;
|
| 42 |
|
| 43 | printf("Iter %3d (%7.3lfus): ",
|
| 44 | iter, (delta_ns - 1000000000) / 1000.0);
|
| 45 |
|
| 46 | printf("%ld false hits of %ld total operations. Percentage = %2.2f %%\n",
|
| 47 | res->false_hits, total, ((float)res->false_hits / total) * 100);
|
| 48 | }
|
| 49 |
|
| 50 | void false_hits_report_final(struct bench_res res[], int res_cnt)
|
| 51 | {
|
| 52 | long total_hits = 0, total_drops = 0, total_false_hits = 0, total_ops = 0;
|
| 53 | int i;
|
| 54 |
|
| 55 | for (i = 0; i < res_cnt; i++) {
|
| 56 | total_hits += res[i].hits;
|
| 57 | total_false_hits += res[i].false_hits;
|
| 58 | total_drops += res[i].drops;
|
| 59 | }
|
| 60 | total_ops = total_hits + total_false_hits + total_drops;
|
| 61 |
|
| 62 | printf("Summary: %ld false hits of %ld total operations. ",
|
| 63 | total_false_hits, total_ops);
|
| 64 | printf("Percentage = %2.2f %%\n",
|
| 65 | ((float)total_false_hits / total_ops) * 100);
|
| 66 | }
|
| 67 |
|
| 68 | void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)
|
| 69 | {
|
| 70 | double hits_per_sec, drops_per_sec;
|
| 71 | double hits_per_prod;
|
| 72 |
|
| 73 | hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0);
|
| 74 | hits_per_prod = hits_per_sec / env.producer_cnt;
|
| 75 | drops_per_sec = res->drops / 1000000.0 / (delta_ns / 1000000000.0);
|
| 76 |
|
| 77 | printf("Iter %3d (%7.3lfus): ",
|
| 78 | iter, (delta_ns - 1000000000) / 1000.0);
|
| 79 |
|
| 80 | printf("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s, total operations %8.3lfM/s\n",
|
| 81 | hits_per_sec, hits_per_prod, drops_per_sec, hits_per_sec + drops_per_sec);
|
| 82 | }
|
| 83 |
|
| 84 | void
|
| 85 | grace_period_latency_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
|
| 86 | {
|
| 87 | int i;
|
| 88 |
|
| 89 | memset(gp_stat, 0, sizeof(struct basic_stats));
|
| 90 |
|
| 91 | for (i = 0; i < res_cnt; i++)
|
| 92 | gp_stat->mean += res[i].gp_ns / 1000.0 / (double)res[i].gp_ct / (0.0 + res_cnt);
|
| 93 |
|
| 94 | #define IT_MEAN_DIFF (res[i].gp_ns / 1000.0 / (double)res[i].gp_ct - gp_stat->mean)
|
| 95 | if (res_cnt > 1) {
|
| 96 | for (i = 0; i < res_cnt; i++)
|
| 97 | gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
|
| 98 | }
|
| 99 | gp_stat->stddev = sqrt(gp_stat->stddev);
|
| 100 | #undef IT_MEAN_DIFF
|
| 101 | }
|
| 102 |
|
| 103 | void
|
| 104 | grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt, struct basic_stats *gp_stat)
|
| 105 | {
|
| 106 | int i;
|
| 107 |
|
| 108 | memset(gp_stat, 0, sizeof(struct basic_stats));
|
| 109 | for (i = 0; i < res_cnt; i++)
|
| 110 | gp_stat->mean += res[i].stime / (double)res[i].gp_ct / (0.0 + res_cnt);
|
| 111 |
|
| 112 | #define IT_MEAN_DIFF (res[i].stime / (double)res[i].gp_ct - gp_stat->mean)
|
| 113 | if (res_cnt > 1) {
|
| 114 | for (i = 0; i < res_cnt; i++)
|
| 115 | gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
|
| 116 | }
|
| 117 | gp_stat->stddev = sqrt(gp_stat->stddev);
|
| 118 | #undef IT_MEAN_DIFF
|
| 119 | }
|
| 120 |
|
| 121 | void hits_drops_report_final(struct bench_res res[], int res_cnt)
|
| 122 | {
|
| 123 | int i;
|
| 124 | double hits_mean = 0.0, drops_mean = 0.0, total_ops_mean = 0.0;
|
| 125 | double hits_stddev = 0.0, drops_stddev = 0.0, total_ops_stddev = 0.0;
|
| 126 | double total_ops;
|
| 127 |
|
| 128 | for (i = 0; i < res_cnt; i++) {
|
| 129 | hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
|
| 130 | drops_mean += res[i].drops / 1000000.0 / (0.0 + res_cnt);
|
| 131 | }
|
| 132 | total_ops_mean = hits_mean + drops_mean;
|
| 133 |
|
| 134 | if (res_cnt > 1) {
|
| 135 | for (i = 0; i < res_cnt; i++) {
|
| 136 | hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
|
| 137 | (hits_mean - res[i].hits / 1000000.0) /
|
| 138 | (res_cnt - 1.0);
|
| 139 | drops_stddev += (drops_mean - res[i].drops / 1000000.0) *
|
| 140 | (drops_mean - res[i].drops / 1000000.0) /
|
| 141 | (res_cnt - 1.0);
|
| 142 | total_ops = res[i].hits + res[i].drops;
|
| 143 | total_ops_stddev += (total_ops_mean - total_ops / 1000000.0) *
|
| 144 | (total_ops_mean - total_ops / 1000000.0) /
|
| 145 | (res_cnt - 1.0);
|
| 146 | }
|
| 147 | hits_stddev = sqrt(hits_stddev);
|
| 148 | drops_stddev = sqrt(drops_stddev);
|
| 149 | total_ops_stddev = sqrt(total_ops_stddev);
|
| 150 | }
|
| 151 | printf("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), ",
|
| 152 | hits_mean, hits_stddev, hits_mean / env.producer_cnt);
|
| 153 | printf("drops %8.3lf \u00B1 %5.3lfM/s, ",
|
| 154 | drops_mean, drops_stddev);
|
| 155 | printf("total operations %8.3lf \u00B1 %5.3lfM/s\n",
|
| 156 | total_ops_mean, total_ops_stddev);
|
| 157 | }
|
| 158 |
|
| 159 | void ops_report_progress(int iter, struct bench_res *res, long delta_ns)
|
| 160 | {
|
| 161 | double hits_per_sec, hits_per_prod;
|
| 162 |
|
| 163 | hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0);
|
| 164 | hits_per_prod = hits_per_sec / env.producer_cnt;
|
| 165 |
|
| 166 | printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
|
| 167 |
|
| 168 | printf("hits %8.3lfM/s (%7.3lfM/prod)\n", hits_per_sec, hits_per_prod);
|
| 169 | }
|
| 170 |
|
| 171 | void ops_report_final(struct bench_res res[], int res_cnt)
|
| 172 | {
|
| 173 | double hits_mean = 0.0, hits_stddev = 0.0;
|
| 174 | int i;
|
| 175 |
|
| 176 | for (i = 0; i < res_cnt; i++)
|
| 177 | hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
|
| 178 |
|
| 179 | if (res_cnt > 1) {
|
| 180 | for (i = 0; i < res_cnt; i++)
|
| 181 | hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
|
| 182 | (hits_mean - res[i].hits / 1000000.0) /
|
| 183 | (res_cnt - 1.0);
|
| 184 |
|
| 185 | hits_stddev = sqrt(hits_stddev);
|
| 186 | }
|
| 187 | printf("Summary: throughput %8.3lf \u00B1 %5.3lf M ops/s (%7.3lfM ops/prod), ",
|
| 188 | hits_mean, hits_stddev, hits_mean / env.producer_cnt);
|
| 189 | printf("latency %8.3lf ns/op\n", 1000.0 / hits_mean * env.producer_cnt);
|
| 190 | }
|
| 191 |
|
| 192 | void local_storage_report_progress(int iter, struct bench_res *res,
|
| 193 | long delta_ns)
|
| 194 | {
|
| 195 | double important_hits_per_sec, hits_per_sec;
|
| 196 | double delta_sec = delta_ns / 1000000000.0;
|
| 197 |
|
| 198 | hits_per_sec = res->hits / 1000000.0 / delta_sec;
|
| 199 | important_hits_per_sec = res->important_hits / 1000000.0 / delta_sec;
|
| 200 |
|
| 201 | printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
|
| 202 |
|
| 203 | printf("hits %8.3lfM/s ", hits_per_sec);
|
| 204 | printf("important_hits %8.3lfM/s\n", important_hits_per_sec);
|
| 205 | }
|
| 206 |
|
| 207 | void local_storage_report_final(struct bench_res res[], int res_cnt)
|
| 208 | {
|
| 209 | double important_hits_mean = 0.0, important_hits_stddev = 0.0;
|
| 210 | double hits_mean = 0.0, hits_stddev = 0.0;
|
| 211 | int i;
|
| 212 |
|
| 213 | for (i = 0; i < res_cnt; i++) {
|
| 214 | hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt);
|
| 215 | important_hits_mean += res[i].important_hits / 1000000.0 / (0.0 + res_cnt);
|
| 216 | }
|
| 217 |
|
| 218 | if (res_cnt > 1) {
|
| 219 | for (i = 0; i < res_cnt; i++) {
|
| 220 | hits_stddev += (hits_mean - res[i].hits / 1000000.0) *
|
| 221 | (hits_mean - res[i].hits / 1000000.0) /
|
| 222 | (res_cnt - 1.0);
|
| 223 | important_hits_stddev +=
|
| 224 | (important_hits_mean - res[i].important_hits / 1000000.0) *
|
| 225 | (important_hits_mean - res[i].important_hits / 1000000.0) /
|
| 226 | (res_cnt - 1.0);
|
| 227 | }
|
| 228 |
|
| 229 | hits_stddev = sqrt(hits_stddev);
|
| 230 | important_hits_stddev = sqrt(important_hits_stddev);
|
| 231 | }
|
| 232 | printf("Summary: hits throughput %8.3lf \u00B1 %5.3lf M ops/s, ",
|
| 233 | hits_mean, hits_stddev);
|
| 234 | printf("hits latency %8.3lf ns/op, ", 1000.0 / hits_mean);
|
| 235 | printf("important_hits throughput %8.3lf \u00B1 %5.3lf M ops/s\n",
|
| 236 | important_hits_mean, important_hits_stddev);
|
| 237 | }
|
| 238 |
|
| 239 | const char *argp_program_version = "benchmark";
|
| 240 | const char *argp_program_bug_address = "<[email protected]>";
|
| 241 | const char argp_program_doc[] =
|
| 242 | "benchmark Generic benchmarking framework.\n"
|
| 243 | "\n"
|
| 244 | "This tool runs benchmarks.\n"
|
| 245 | "\n"
|
| 246 | "USAGE: benchmark <bench-name>\n"
|
| 247 | "\n"
|
| 248 | "EXAMPLES:\n"
|
| 249 | " # run 'count-local' benchmark with 1 producer and 1 consumer\n"
|
| 250 | " benchmark count-local\n"
|
| 251 | " # run 'count-local' with 16 producer and 8 consumer thread, pinned to CPUs\n"
|
| 252 | " benchmark -p16 -c8 -a count-local\n";
|
| 253 |
|
| 254 | enum {
|
| 255 | ARG_PROD_AFFINITY_SET = 1000,
|
| 256 | ARG_CONS_AFFINITY_SET = 1001,
|
| 257 | };
|
| 258 |
|
| 259 | static const struct argp_option opts[] = {
|
| 260 | { "list", 'l', NULL, 0, "List available benchmarks"},
|
| 261 | { "duration", 'd', "SEC", 0, "Duration of benchmark, seconds"},
|
| 262 | { "warmup", 'w', "SEC", 0, "Warm-up period, seconds"},
|
| 263 | { "producers", 'p', "NUM", 0, "Number of producer threads"},
|
| 264 | { "consumers", 'c', "NUM", 0, "Number of consumer threads"},
|
| 265 | { "verbose", 'v', NULL, 0, "Verbose debug output"},
|
| 266 | { "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"},
|
| 267 | { "quiet", 'q', NULL, 0, "Be more quiet"},
|
| 268 | { "stacktrace", 's', NULL, 0, "Get stack trace"},
|
| 269 | { "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0,
|
| 270 | "Set of CPUs for producer threads; implies --affinity"},
|
| 271 | { "cons-affinity", ARG_CONS_AFFINITY_SET, "CPUSET", 0,
|
| 272 | "Set of CPUs for consumer threads; implies --affinity"},
|
| 273 | {},
|
| 274 | };
|
| 275 |
|
| 276 | extern struct argp bench_ringbufs_argp;
|
| 277 | extern struct argp bench_bloom_map_argp;
|
| 278 | extern struct argp bench_bpf_loop_argp;
|
| 279 | extern struct argp bench_bpf_for_argp;
|
| 280 | extern struct argp bench_local_storage_argp;
|
| 281 | extern struct argp bench_local_storage_rcu_tasks_trace_argp;
|
| 282 | extern struct argp bench_strncmp_argp;
|
| 283 | extern struct argp bench_hashmap_lookup_argp;
|
| 284 | extern struct argp bench_local_storage_create_argp;
|
| 285 | extern struct argp bench_htab_mem_argp;
|
| 286 | extern struct argp bench_trigger_batch_argp;
|
| 287 | extern struct argp bench_crypto_argp;
|
| 288 | extern struct argp bench_sockmap_argp;
|
| 289 | extern struct argp bench_lpm_trie_map_argp;
|
| 290 | extern struct argp bench_xdp_lb_argp;
|
| 291 |
|
| 292 | static const struct argp_child bench_parsers[] = {
|
| 293 | { &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 },
|
| 294 | { &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 },
|
| 295 | { &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 },
|
| 296 | { &bench_bpf_for_argp, 0, "bpf_for loop benchmark", 0 },
|
| 297 | { &bench_local_storage_argp, 0, "local_storage benchmark", 0 },
|
| 298 | { &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 },
|
| 299 | { &bench_local_storage_rcu_tasks_trace_argp, 0,
|
| 300 | "local_storage RCU Tasks Trace slowdown benchmark", 0 },
|
| 301 | { &bench_hashmap_lookup_argp, 0, "Hashmap lookup benchmark", 0 },
|
| 302 | { &bench_local_storage_create_argp, 0, "local-storage-create benchmark", 0 },
|
| 303 | { &bench_htab_mem_argp, 0, "hash map memory benchmark", 0 },
|
| 304 | { &bench_trigger_batch_argp, 0, "BPF triggering benchmark", 0 },
|
| 305 | { &bench_crypto_argp, 0, "bpf crypto benchmark", 0 },
|
| 306 | { &bench_sockmap_argp, 0, "bpf sockmap benchmark", 0 },
|
| 307 | { &bench_lpm_trie_map_argp, 0, "LPM trie map benchmark", 0 },
|
| 308 | { &bench_xdp_lb_argp, 0, "XDP load-balancer benchmark", 0 },
|
| 309 | {},
|
| 310 | };
|
| 311 |
|
| 312 |
|
| 313 | static int pos_args;
|
| 314 |
|
| 315 | static error_t parse_arg(int key, char *arg, struct argp_state *state)
|
| 316 | {
|
| 317 | switch (key) {
|
| 318 | case 'v':
|
| 319 | env.verbose = true;
|
| 320 | break;
|
| 321 | case 'l':
|
| 322 | env.list = true;
|
| 323 | break;
|
| 324 | case 'd':
|
| 325 | env.duration_sec = strtol(arg, NULL, 10);
|
| 326 | if (env.duration_sec <= 0) {
|
| 327 | fprintf(stderr, "Invalid duration: %s\n", arg);
|
| 328 | argp_usage(state);
|
| 329 | }
|
| 330 | break;
|
| 331 | case 'w':
|
| 332 | env.warmup_sec = strtol(arg, NULL, 10);
|
| 333 | if (env.warmup_sec <= 0) {
|
| 334 | fprintf(stderr, "Invalid warm-up duration: %s\n", arg);
|
| 335 | argp_usage(state);
|
| 336 | }
|
| 337 | break;
|
| 338 | case 'p':
|
| 339 | env.producer_cnt = strtol(arg, NULL, 10);
|
| 340 | if (env.producer_cnt < 0) {
|
| 341 | fprintf(stderr, "Invalid producer count: %s\n", arg);
|
| 342 | argp_usage(state);
|
| 343 | }
|
| 344 | break;
|
| 345 | case 'c':
|
| 346 | env.consumer_cnt = strtol(arg, NULL, 10);
|
| 347 | if (env.consumer_cnt < 0) {
|
| 348 | fprintf(stderr, "Invalid consumer count: %s\n", arg);
|
| 349 | argp_usage(state);
|
| 350 | }
|
| 351 | break;
|
| 352 | case 'a':
|
| 353 | env.affinity = true;
|
| 354 | break;
|
| 355 | case 'q':
|
| 356 | env.quiet = true;
|
| 357 | break;
|
| 358 | case 's':
|
| 359 | env.stacktrace = true;
|
| 360 | break;
|
| 361 | case ARG_PROD_AFFINITY_SET:
|
| 362 | env.affinity = true;
|
| 363 | if (parse_num_list(arg, &env.prod_cpus.cpus,
|
| 364 | &env.prod_cpus.cpus_len)) {
|
| 365 | fprintf(stderr, "Invalid format of CPU set for producers.");
|
| 366 | argp_usage(state);
|
| 367 | }
|
| 368 | break;
|
| 369 | case ARG_CONS_AFFINITY_SET:
|
| 370 | env.affinity = true;
|
| 371 | if (parse_num_list(arg, &env.cons_cpus.cpus,
|
| 372 | &env.cons_cpus.cpus_len)) {
|
| 373 | fprintf(stderr, "Invalid format of CPU set for consumers.");
|
| 374 | argp_usage(state);
|
| 375 | }
|
| 376 | break;
|
| 377 | case ARGP_KEY_ARG:
|
| 378 | if (pos_args++) {
|
| 379 | fprintf(stderr,
|
| 380 | "Unrecognized positional argument: %s\n", arg);
|
| 381 | argp_usage(state);
|
| 382 | }
|
| 383 | env.bench_name = strdup(arg);
|
| 384 | break;
|
| 385 | default:
|
| 386 | return ARGP_ERR_UNKNOWN;
|
| 387 | }
|
| 388 | return 0;
|
| 389 | }
|
| 390 |
|
| 391 | static void parse_cmdline_args_init(int argc, char **argv)
|
| 392 | {
|
| 393 | static const struct argp argp = {
|
| 394 | .options = opts,
|
| 395 | .parser = parse_arg,
|
| 396 | .doc = argp_program_doc,
|
| 397 | .children = bench_parsers,
|
| 398 | };
|
| 399 | if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
|
| 400 | exit(1);
|
| 401 | }
|
| 402 |
|
| 403 | static void parse_cmdline_args_final(int argc, char **argv)
|
| 404 | {
|
| 405 | struct argp_child bench_parsers[2] = {};
|
| 406 | const struct argp argp = {
|
| 407 | .options = opts,
|
| 408 | .parser = parse_arg,
|
| 409 | .doc = argp_program_doc,
|
| 410 | .children = bench_parsers,
|
| 411 | };
|
| 412 |
|
| 413 |
|
| 414 | if (bench->argp) {
|
| 415 | bench_parsers[0].argp = bench->argp;
|
| 416 | bench_parsers[0].header = bench->name;
|
| 417 | pos_args = 0;
|
| 418 | if (argp_parse(&argp, argc, argv, 0, NULL, NULL))
|
| 419 | exit(1);
|
| 420 | }
|
| 421 | }
|
| 422 |
|
| 423 | static void collect_measurements(long delta_ns);
|
| 424 |
|
| 425 | static __u64 last_time_ns;
|
| 426 | static void sigalarm_handler(int signo)
|
| 427 | {
|
| 428 | long new_time_ns = get_time_ns();
|
| 429 | long delta_ns = new_time_ns - last_time_ns;
|
| 430 |
|
| 431 | collect_measurements(delta_ns);
|
| 432 |
|
| 433 | last_time_ns = new_time_ns;
|
| 434 | }
|
| 435 |
|
| 436 |
|
| 437 | static void setup_timer()
|
| 438 | {
|
| 439 | static struct sigaction sigalarm_action = {
|
| 440 | .sa_handler = sigalarm_handler,
|
| 441 | };
|
| 442 | struct itimerval timer_settings = {};
|
| 443 | int err;
|
| 444 |
|
| 445 | last_time_ns = get_time_ns();
|
| 446 | err = sigaction(SIGALRM, &sigalarm_action, NULL);
|
| 447 | if (err < 0) {
|
| 448 | fprintf(stderr, "failed to install SIGALRM handler: %d\n", -errno);
|
| 449 | exit(1);
|
| 450 | }
|
| 451 | timer_settings.it_interval.tv_sec = 1;
|
| 452 | timer_settings.it_value.tv_sec = 1;
|
| 453 | err = setitimer(ITIMER_REAL, &timer_settings, NULL);
|
| 454 | if (err < 0) {
|
| 455 | fprintf(stderr, "failed to arm interval timer: %d\n", -errno);
|
| 456 | exit(1);
|
| 457 | }
|
| 458 | }
|
| 459 |
|
| 460 | static void set_thread_affinity(pthread_t thread, int cpu)
|
| 461 | {
|
| 462 | cpu_set_t cpuset;
|
| 463 | int err;
|
| 464 |
|
| 465 | CPU_ZERO(&cpuset);
|
| 466 | CPU_SET(cpu, &cpuset);
|
| 467 | err = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
|
| 468 | if (err) {
|
| 469 | fprintf(stderr, "setting affinity to CPU #%d failed: %d\n",
|
| 470 | cpu, -err);
|
| 471 | exit(1);
|
| 472 | }
|
| 473 | }
|
| 474 |
|
| 475 | static int next_cpu(struct cpu_set *cpu_set)
|
| 476 | {
|
| 477 | if (cpu_set->cpus) {
|
| 478 | int i;
|
| 479 |
|
| 480 |
|
| 481 | for (i = cpu_set->next_cpu; i < cpu_set->cpus_len; i++) {
|
| 482 | if (cpu_set->cpus[i]) {
|
| 483 | cpu_set->next_cpu = i + 1;
|
| 484 | return i;
|
| 485 | }
|
| 486 | }
|
| 487 | fprintf(stderr, "Not enough CPUs specified, need CPU #%d or higher.\n", i);
|
| 488 | exit(1);
|
| 489 | }
|
| 490 |
|
| 491 | return cpu_set->next_cpu++ % env.nr_cpus;
|
| 492 | }
|
| 493 |
|
| 494 | static struct bench_state {
|
| 495 | int res_cnt;
|
| 496 | struct bench_res *results;
|
| 497 | pthread_t *consumers;
|
| 498 | pthread_t *producers;
|
| 499 | } state;
|
| 500 |
|
| 501 | const struct bench *bench = NULL;
|
| 502 |
|
| 503 | extern const struct bench bench_count_global;
|
| 504 | extern const struct bench bench_count_local;
|
| 505 | extern const struct bench bench_rename_base;
|
| 506 | extern const struct bench bench_rename_kprobe;
|
| 507 | extern const struct bench bench_rename_kretprobe;
|
| 508 | extern const struct bench bench_rename_rawtp;
|
| 509 | extern const struct bench bench_rename_fentry;
|
| 510 | extern const struct bench bench_rename_fexit;
|
| 511 |
|
| 512 |
|
| 513 | extern const struct bench bench_trig_usermode_count;
|
| 514 | extern const struct bench bench_trig_syscall_count;
|
| 515 | extern const struct bench bench_trig_kernel_count;
|
| 516 |
|
| 517 |
|
| 518 | extern const struct bench bench_trig_kprobe;
|
| 519 | extern const struct bench bench_trig_kretprobe;
|
| 520 | extern const struct bench bench_trig_kprobe_multi;
|
| 521 | extern const struct bench bench_trig_kretprobe_multi;
|
| 522 | extern const struct bench bench_trig_fentry;
|
| 523 | extern const struct bench bench_trig_kprobe_multi_all;
|
| 524 | extern const struct bench bench_trig_kretprobe_multi_all;
|
| 525 | extern const struct bench bench_trig_fexit;
|
| 526 | extern const struct bench bench_trig_fmodret;
|
| 527 | extern const struct bench bench_trig_tp;
|
| 528 | extern const struct bench bench_trig_rawtp;
|
| 529 |
|
| 530 |
|
| 531 | extern const struct bench bench_trig_uprobe_nop;
|
| 532 | extern const struct bench bench_trig_uretprobe_nop;
|
| 533 | extern const struct bench bench_trig_uprobe_push;
|
| 534 | extern const struct bench bench_trig_uretprobe_push;
|
| 535 | extern const struct bench bench_trig_uprobe_ret;
|
| 536 | extern const struct bench bench_trig_uretprobe_ret;
|
| 537 | extern const struct bench bench_trig_uprobe_multi_nop;
|
| 538 | extern const struct bench bench_trig_uretprobe_multi_nop;
|
| 539 | extern const struct bench bench_trig_uprobe_multi_push;
|
| 540 | extern const struct bench bench_trig_uretprobe_multi_push;
|
| 541 | extern const struct bench bench_trig_uprobe_multi_ret;
|
| 542 | extern const struct bench bench_trig_uretprobe_multi_ret;
|
| 543 | #ifdef __x86_64__
|
| 544 | extern const struct bench bench_trig_uprobe_nop10;
|
| 545 | extern const struct bench bench_trig_uretprobe_nop10;
|
| 546 | extern const struct bench bench_trig_uprobe_multi_nop10;
|
| 547 | extern const struct bench bench_trig_uretprobe_multi_nop10;
|
| 548 | extern const struct bench bench_trig_usdt_nop;
|
| 549 | extern const struct bench bench_trig_usdt_nop10;
|
| 550 | #endif
|
| 551 |
|
| 552 | extern const struct bench bench_rb_libbpf;
|
| 553 | extern const struct bench bench_rb_custom;
|
| 554 | extern const struct bench bench_pb_libbpf;
|
| 555 | extern const struct bench bench_pb_custom;
|
| 556 | extern const struct bench bench_bloom_lookup;
|
| 557 | extern const struct bench bench_bloom_update;
|
| 558 | extern const struct bench bench_bloom_false_positive;
|
| 559 | extern const struct bench bench_hashmap_without_bloom;
|
| 560 | extern const struct bench bench_hashmap_with_bloom;
|
| 561 | extern const struct bench bench_bpf_loop;
|
| 562 | extern const struct bench bench_bpf_for;
|
| 563 | extern const struct bench bench_strncmp_no_helper;
|
| 564 | extern const struct bench bench_strncmp_helper;
|
| 565 | extern const struct bench bench_bpf_hashmap_full_update;
|
| 566 | extern const struct bench bench_bpf_rhashmap_full_update;
|
| 567 | extern const struct bench bench_local_storage_cache_seq_get;
|
| 568 | extern const struct bench bench_local_storage_cache_interleaved_get;
|
| 569 | extern const struct bench bench_local_storage_cache_hashmap_control;
|
| 570 | extern const struct bench bench_local_storage_tasks_trace;
|
| 571 | extern const struct bench bench_bpf_hashmap_lookup;
|
| 572 | extern const struct bench bench_bpf_rhashmap_lookup;
|
| 573 | extern const struct bench bench_local_storage_create;
|
| 574 | extern const struct bench bench_htab_mem;
|
| 575 | extern const struct bench bench_rhtab_mem;
|
| 576 | extern const struct bench bench_crypto_encrypt;
|
| 577 | extern const struct bench bench_crypto_decrypt;
|
| 578 | extern const struct bench bench_sockmap;
|
| 579 | extern const struct bench bench_lpm_trie_noop;
|
| 580 | extern const struct bench bench_lpm_trie_baseline;
|
| 581 | extern const struct bench bench_lpm_trie_lookup;
|
| 582 | extern const struct bench bench_lpm_trie_insert;
|
| 583 | extern const struct bench bench_lpm_trie_update;
|
| 584 | extern const struct bench bench_lpm_trie_delete;
|
| 585 | extern const struct bench bench_lpm_trie_free;
|
| 586 | extern const struct bench bench_bpf_nop;
|
| 587 | extern const struct bench bench_xdp_lb;
|
| 588 |
|
| 589 | static const struct bench *benchs[] = {
|
| 590 | &bench_count_global,
|
| 591 | &bench_count_local,
|
| 592 | &bench_rename_base,
|
| 593 | &bench_rename_kprobe,
|
| 594 | &bench_rename_kretprobe,
|
| 595 | &bench_rename_rawtp,
|
| 596 | &bench_rename_fentry,
|
| 597 | &bench_rename_fexit,
|
| 598 |
|
| 599 | &bench_trig_usermode_count,
|
| 600 | &bench_trig_kernel_count,
|
| 601 | &bench_trig_syscall_count,
|
| 602 |
|
| 603 | &bench_trig_kprobe,
|
| 604 | &bench_trig_kretprobe,
|
| 605 | &bench_trig_kprobe_multi,
|
| 606 | &bench_trig_kretprobe_multi,
|
| 607 | &bench_trig_fentry,
|
| 608 | &bench_trig_kprobe_multi_all,
|
| 609 | &bench_trig_kretprobe_multi_all,
|
| 610 | &bench_trig_fexit,
|
| 611 | &bench_trig_fmodret,
|
| 612 | &bench_trig_tp,
|
| 613 | &bench_trig_rawtp,
|
| 614 |
|
| 615 | &bench_trig_uprobe_nop,
|
| 616 | &bench_trig_uretprobe_nop,
|
| 617 | &bench_trig_uprobe_push,
|
| 618 | &bench_trig_uretprobe_push,
|
| 619 | &bench_trig_uprobe_ret,
|
| 620 | &bench_trig_uretprobe_ret,
|
| 621 | &bench_trig_uprobe_multi_nop,
|
| 622 | &bench_trig_uretprobe_multi_nop,
|
| 623 | &bench_trig_uprobe_multi_push,
|
| 624 | &bench_trig_uretprobe_multi_push,
|
| 625 | &bench_trig_uprobe_multi_ret,
|
| 626 | &bench_trig_uretprobe_multi_ret,
|
| 627 | #ifdef __x86_64__
|
| 628 | &bench_trig_uprobe_nop10,
|
| 629 | &bench_trig_uretprobe_nop10,
|
| 630 | &bench_trig_uprobe_multi_nop10,
|
| 631 | &bench_trig_uretprobe_multi_nop10,
|
| 632 | &bench_trig_usdt_nop,
|
| 633 | &bench_trig_usdt_nop10,
|
| 634 | #endif
|
| 635 |
|
| 636 | &bench_rb_libbpf,
|
| 637 | &bench_rb_custom,
|
| 638 | &bench_pb_libbpf,
|
| 639 | &bench_pb_custom,
|
| 640 | &bench_bloom_lookup,
|
| 641 | &bench_bloom_update,
|
| 642 | &bench_bloom_false_positive,
|
| 643 | &bench_hashmap_without_bloom,
|
| 644 | &bench_hashmap_with_bloom,
|
| 645 | &bench_bpf_loop,
|
| 646 | &bench_bpf_for,
|
| 647 | &bench_strncmp_no_helper,
|
| 648 | &bench_strncmp_helper,
|
| 649 | &bench_bpf_hashmap_full_update,
|
| 650 | &bench_bpf_rhashmap_full_update,
|
| 651 | &bench_local_storage_cache_seq_get,
|
| 652 | &bench_local_storage_cache_interleaved_get,
|
| 653 | &bench_local_storage_cache_hashmap_control,
|
| 654 | &bench_local_storage_tasks_trace,
|
| 655 | &bench_bpf_hashmap_lookup,
|
| 656 | &bench_bpf_rhashmap_lookup,
|
| 657 | &bench_local_storage_create,
|
| 658 | &bench_htab_mem,
|
| 659 | &bench_rhtab_mem,
|
| 660 | &bench_crypto_encrypt,
|
| 661 | &bench_crypto_decrypt,
|
| 662 | &bench_sockmap,
|
| 663 | &bench_lpm_trie_noop,
|
| 664 | &bench_lpm_trie_baseline,
|
| 665 | &bench_lpm_trie_lookup,
|
| 666 | &bench_lpm_trie_insert,
|
| 667 | &bench_lpm_trie_update,
|
| 668 | &bench_lpm_trie_delete,
|
| 669 | &bench_lpm_trie_free,
|
| 670 | &bench_bpf_nop,
|
| 671 | &bench_xdp_lb,
|
| 672 | };
|
| 673 |
|
| 674 | static void find_benchmark(void)
|
| 675 | {
|
| 676 | int i;
|
| 677 |
|
| 678 | if (!env.bench_name) {
|
| 679 | fprintf(stderr, "benchmark name is not specified\n");
|
| 680 | exit(1);
|
| 681 | }
|
| 682 | for (i = 0; i < ARRAY_SIZE(benchs); i++) {
|
| 683 | if (strcmp(benchs[i]->name, env.bench_name) == 0) {
|
| 684 | bench = benchs[i];
|
| 685 | break;
|
| 686 | }
|
| 687 | }
|
| 688 | if (!bench) {
|
| 689 | fprintf(stderr, "benchmark '%s' not found\n", env.bench_name);
|
| 690 | exit(1);
|
| 691 | }
|
| 692 | }
|
| 693 |
|
| 694 | static void setup_benchmark(void)
|
| 695 | {
|
| 696 | int i, err;
|
| 697 |
|
| 698 | if (!env.quiet)
|
| 699 | printf("Setting up benchmark '%s'...\n", bench->name);
|
| 700 |
|
| 701 | state.producers = calloc(env.producer_cnt, sizeof(*state.producers));
|
| 702 | state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers));
|
| 703 | state.results = calloc(env.duration_sec + env.warmup_sec + 2,
|
| 704 | sizeof(*state.results));
|
| 705 | if (!state.producers || !state.consumers || !state.results)
|
| 706 | exit(1);
|
| 707 |
|
| 708 | if (bench->validate)
|
| 709 | bench->validate();
|
| 710 | if (bench->setup)
|
| 711 | bench->setup();
|
| 712 |
|
| 713 | for (i = 0; i < env.consumer_cnt; i++) {
|
| 714 | if (!bench->consumer_thread) {
|
| 715 | fprintf(stderr, "benchmark doesn't support consumers!\n");
|
| 716 | exit(1);
|
| 717 | }
|
| 718 | err = pthread_create(&state.consumers[i], NULL,
|
| 719 | bench->consumer_thread, (void *)(long)i);
|
| 720 | if (err) {
|
| 721 | fprintf(stderr, "failed to create consumer thread #%d: %d\n",
|
| 722 | i, -err);
|
| 723 | exit(1);
|
| 724 | }
|
| 725 | if (env.affinity)
|
| 726 | set_thread_affinity(state.consumers[i],
|
| 727 | next_cpu(&env.cons_cpus));
|
| 728 | }
|
| 729 |
|
| 730 | |
| 731 | * last consumer CPU
|
| 732 |
|
| 733 | if (!env.prod_cpus.cpus)
|
| 734 | env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
|
| 735 |
|
| 736 | for (i = 0; i < env.producer_cnt; i++) {
|
| 737 | if (!bench->producer_thread) {
|
| 738 | fprintf(stderr, "benchmark doesn't support producers!\n");
|
| 739 | exit(1);
|
| 740 | }
|
| 741 | err = pthread_create(&state.producers[i], NULL,
|
| 742 | bench->producer_thread, (void *)(long)i);
|
| 743 | if (err) {
|
| 744 | fprintf(stderr, "failed to create producer thread #%d: %d\n",
|
| 745 | i, -err);
|
| 746 | exit(1);
|
| 747 | }
|
| 748 | if (env.affinity)
|
| 749 | set_thread_affinity(state.producers[i],
|
| 750 | next_cpu(&env.prod_cpus));
|
| 751 | }
|
| 752 |
|
| 753 | if (!env.quiet)
|
| 754 | printf("Benchmark '%s' started.\n", bench->name);
|
| 755 | }
|
| 756 |
|
| 757 | static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER;
|
| 758 | static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER;
|
| 759 |
|
| 760 | void bench_force_done(void)
|
| 761 | {
|
| 762 | pthread_mutex_lock(&bench_done_mtx);
|
| 763 | pthread_cond_signal(&bench_done);
|
| 764 | pthread_mutex_unlock(&bench_done_mtx);
|
| 765 | }
|
| 766 |
|
| 767 | static void collect_measurements(long delta_ns) {
|
| 768 | int iter = state.res_cnt++;
|
| 769 | struct bench_res *res = &state.results[iter];
|
| 770 |
|
| 771 | bench->measure(res);
|
| 772 |
|
| 773 | if (bench->report_progress)
|
| 774 | bench->report_progress(iter, res, delta_ns);
|
| 775 |
|
| 776 | if (iter == env.duration_sec + env.warmup_sec)
|
| 777 | bench_force_done();
|
| 778 | }
|
| 779 |
|
| 780 | int main(int argc, char **argv)
|
| 781 | {
|
| 782 | env.nr_cpus = get_nprocs();
|
| 783 | parse_cmdline_args_init(argc, argv);
|
| 784 |
|
| 785 | if (env.list) {
|
| 786 | int i;
|
| 787 |
|
| 788 | printf("Available benchmarks:\n");
|
| 789 | for (i = 0; i < ARRAY_SIZE(benchs); i++) {
|
| 790 | printf("- %s\n", benchs[i]->name);
|
| 791 | }
|
| 792 | return 0;
|
| 793 | }
|
| 794 |
|
| 795 | find_benchmark();
|
| 796 | parse_cmdline_args_final(argc, argv);
|
| 797 |
|
| 798 | setup_benchmark();
|
| 799 |
|
| 800 | setup_timer();
|
| 801 |
|
| 802 | pthread_mutex_lock(&bench_done_mtx);
|
| 803 | pthread_cond_wait(&bench_done, &bench_done_mtx);
|
| 804 | pthread_mutex_unlock(&bench_done_mtx);
|
| 805 |
|
| 806 | if (bench->report_final)
|
| 807 |
|
| 808 | bench->report_final(state.results + env.warmup_sec,
|
| 809 | state.res_cnt - env.warmup_sec);
|
| 810 |
|
| 811 | return 0;
|
| 812 | }
|