1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020 Facebook */ 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 <sys/resource.h> 12 #include <signal.h> 13 #include "bench.h" 14 #include "testing_helpers.h" 15 16 struct env env = { 17 .warmup_sec = 1, 18 .duration_sec = 5, 19 .affinity = false, 20 .consumer_cnt = 1, 21 .producer_cnt = 1, 22 }; 23 24 static int libbpf_print_fn(enum libbpf_print_level level, 25 const char *format, va_list args) 26 { 27 if (level == LIBBPF_DEBUG && !env.verbose) 28 return 0; 29 return vfprintf(stderr, format, args); 30 } 31 32 static int bump_memlock_rlimit(void) 33 { 34 struct rlimit rlim_new = { 35 .rlim_cur = RLIM_INFINITY, 36 .rlim_max = RLIM_INFINITY, 37 }; 38 39 return setrlimit(RLIMIT_MEMLOCK, &rlim_new); 40 } 41 42 void setup_libbpf() 43 { 44 int err; 45 46 libbpf_set_print(libbpf_print_fn); 47 48 err = bump_memlock_rlimit(); 49 if (err) 50 fprintf(stderr, "failed to increase RLIMIT_MEMLOCK: %d", err); 51 } 52 53 void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns) 54 { 55 double hits_per_sec, drops_per_sec; 56 double hits_per_prod; 57 58 hits_per_sec = res->hits / 1000000.0 / (delta_ns / 1000000000.0); 59 hits_per_prod = hits_per_sec / env.producer_cnt; 60 drops_per_sec = res->drops / 1000000.0 / (delta_ns / 1000000000.0); 61 62 printf("Iter %3d (%7.3lfus): ", 63 iter, (delta_ns - 1000000000) / 1000.0); 64 65 printf("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s\n", 66 hits_per_sec, hits_per_prod, drops_per_sec); 67 } 68 69 void hits_drops_report_final(struct bench_res res[], int res_cnt) 70 { 71 int i; 72 double hits_mean = 0.0, drops_mean = 0.0; 73 double hits_stddev = 0.0, drops_stddev = 0.0; 74 75 for (i = 0; i < res_cnt; i++) { 76 hits_mean += res[i].hits / 1000000.0 / (0.0 + res_cnt); 77 drops_mean += res[i].drops / 1000000.0 / (0.0 + res_cnt); 78 } 79 80 if (res_cnt > 1) { 81 for (i = 0; i < res_cnt; i++) { 82 hits_stddev += (hits_mean - res[i].hits / 1000000.0) * 83 (hits_mean - res[i].hits / 1000000.0) / 84 (res_cnt - 1.0); 85 drops_stddev += (drops_mean - res[i].drops / 1000000.0) * 86 (drops_mean - res[i].drops / 1000000.0) / 87 (res_cnt - 1.0); 88 } 89 hits_stddev = sqrt(hits_stddev); 90 drops_stddev = sqrt(drops_stddev); 91 } 92 printf("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), ", 93 hits_mean, hits_stddev, hits_mean / env.producer_cnt); 94 printf("drops %8.3lf \u00B1 %5.3lfM/s\n", 95 drops_mean, drops_stddev); 96 } 97 98 const char *argp_program_version = "benchmark"; 99 const char *argp_program_bug_address = "<bpf@vger.kernel.org>"; 100 const char argp_program_doc[] = 101 "benchmark Generic benchmarking framework.\n" 102 "\n" 103 "This tool runs benchmarks.\n" 104 "\n" 105 "USAGE: benchmark <bench-name>\n" 106 "\n" 107 "EXAMPLES:\n" 108 " # run 'count-local' benchmark with 1 producer and 1 consumer\n" 109 " benchmark count-local\n" 110 " # run 'count-local' with 16 producer and 8 consumer thread, pinned to CPUs\n" 111 " benchmark -p16 -c8 -a count-local\n"; 112 113 enum { 114 ARG_PROD_AFFINITY_SET = 1000, 115 ARG_CONS_AFFINITY_SET = 1001, 116 }; 117 118 static const struct argp_option opts[] = { 119 { "list", 'l', NULL, 0, "List available benchmarks"}, 120 { "duration", 'd', "SEC", 0, "Duration of benchmark, seconds"}, 121 { "warmup", 'w', "SEC", 0, "Warm-up period, seconds"}, 122 { "producers", 'p', "NUM", 0, "Number of producer threads"}, 123 { "consumers", 'c', "NUM", 0, "Number of consumer threads"}, 124 { "verbose", 'v', NULL, 0, "Verbose debug output"}, 125 { "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"}, 126 { "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0, 127 "Set of CPUs for producer threads; implies --affinity"}, 128 { "cons-affinity", ARG_CONS_AFFINITY_SET, "CPUSET", 0, 129 "Set of CPUs for consumer threads; implies --affinity"}, 130 {}, 131 }; 132 133 static error_t parse_arg(int key, char *arg, struct argp_state *state) 134 { 135 static int pos_args; 136 137 switch (key) { 138 case 'v': 139 env.verbose = true; 140 break; 141 case 'l': 142 env.list = true; 143 break; 144 case 'd': 145 env.duration_sec = strtol(arg, NULL, 10); 146 if (env.duration_sec <= 0) { 147 fprintf(stderr, "Invalid duration: %s\n", arg); 148 argp_usage(state); 149 } 150 break; 151 case 'w': 152 env.warmup_sec = strtol(arg, NULL, 10); 153 if (env.warmup_sec <= 0) { 154 fprintf(stderr, "Invalid warm-up duration: %s\n", arg); 155 argp_usage(state); 156 } 157 break; 158 case 'p': 159 env.producer_cnt = strtol(arg, NULL, 10); 160 if (env.producer_cnt <= 0) { 161 fprintf(stderr, "Invalid producer count: %s\n", arg); 162 argp_usage(state); 163 } 164 break; 165 case 'c': 166 env.consumer_cnt = strtol(arg, NULL, 10); 167 if (env.consumer_cnt <= 0) { 168 fprintf(stderr, "Invalid consumer count: %s\n", arg); 169 argp_usage(state); 170 } 171 break; 172 case 'a': 173 env.affinity = true; 174 break; 175 case ARG_PROD_AFFINITY_SET: 176 env.affinity = true; 177 if (parse_num_list(arg, &env.prod_cpus.cpus, 178 &env.prod_cpus.cpus_len)) { 179 fprintf(stderr, "Invalid format of CPU set for producers."); 180 argp_usage(state); 181 } 182 break; 183 case ARG_CONS_AFFINITY_SET: 184 env.affinity = true; 185 if (parse_num_list(arg, &env.cons_cpus.cpus, 186 &env.cons_cpus.cpus_len)) { 187 fprintf(stderr, "Invalid format of CPU set for consumers."); 188 argp_usage(state); 189 } 190 break; 191 case ARGP_KEY_ARG: 192 if (pos_args++) { 193 fprintf(stderr, 194 "Unrecognized positional argument: %s\n", arg); 195 argp_usage(state); 196 } 197 env.bench_name = strdup(arg); 198 break; 199 default: 200 return ARGP_ERR_UNKNOWN; 201 } 202 return 0; 203 } 204 205 static void parse_cmdline_args(int argc, char **argv) 206 { 207 static const struct argp argp = { 208 .options = opts, 209 .parser = parse_arg, 210 .doc = argp_program_doc, 211 }; 212 if (argp_parse(&argp, argc, argv, 0, NULL, NULL)) 213 exit(1); 214 if (!env.list && !env.bench_name) { 215 argp_help(&argp, stderr, ARGP_HELP_DOC, "bench"); 216 exit(1); 217 } 218 } 219 220 static void collect_measurements(long delta_ns); 221 222 static __u64 last_time_ns; 223 static void sigalarm_handler(int signo) 224 { 225 long new_time_ns = get_time_ns(); 226 long delta_ns = new_time_ns - last_time_ns; 227 228 collect_measurements(delta_ns); 229 230 last_time_ns = new_time_ns; 231 } 232 233 /* set up periodic 1-second timer */ 234 static void setup_timer() 235 { 236 static struct sigaction sigalarm_action = { 237 .sa_handler = sigalarm_handler, 238 }; 239 struct itimerval timer_settings = {}; 240 int err; 241 242 last_time_ns = get_time_ns(); 243 err = sigaction(SIGALRM, &sigalarm_action, NULL); 244 if (err < 0) { 245 fprintf(stderr, "failed to install SIGALRM handler: %d\n", -errno); 246 exit(1); 247 } 248 timer_settings.it_interval.tv_sec = 1; 249 timer_settings.it_value.tv_sec = 1; 250 err = setitimer(ITIMER_REAL, &timer_settings, NULL); 251 if (err < 0) { 252 fprintf(stderr, "failed to arm interval timer: %d\n", -errno); 253 exit(1); 254 } 255 } 256 257 static void set_thread_affinity(pthread_t thread, int cpu) 258 { 259 cpu_set_t cpuset; 260 261 CPU_ZERO(&cpuset); 262 CPU_SET(cpu, &cpuset); 263 if (pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset)) { 264 fprintf(stderr, "setting affinity to CPU #%d failed: %d\n", 265 cpu, errno); 266 exit(1); 267 } 268 } 269 270 static int next_cpu(struct cpu_set *cpu_set) 271 { 272 if (cpu_set->cpus) { 273 int i; 274 275 /* find next available CPU */ 276 for (i = cpu_set->next_cpu; i < cpu_set->cpus_len; i++) { 277 if (cpu_set->cpus[i]) { 278 cpu_set->next_cpu = i + 1; 279 return i; 280 } 281 } 282 fprintf(stderr, "Not enough CPUs specified, need CPU #%d or higher.\n", i); 283 exit(1); 284 } 285 286 return cpu_set->next_cpu++; 287 } 288 289 static struct bench_state { 290 int res_cnt; 291 struct bench_res *results; 292 pthread_t *consumers; 293 pthread_t *producers; 294 } state; 295 296 const struct bench *bench = NULL; 297 298 extern const struct bench bench_count_global; 299 extern const struct bench bench_count_local; 300 extern const struct bench bench_rename_base; 301 extern const struct bench bench_rename_kprobe; 302 extern const struct bench bench_rename_kretprobe; 303 extern const struct bench bench_rename_rawtp; 304 extern const struct bench bench_rename_fentry; 305 extern const struct bench bench_rename_fexit; 306 extern const struct bench bench_rename_fmodret; 307 extern const struct bench bench_trig_base; 308 extern const struct bench bench_trig_tp; 309 extern const struct bench bench_trig_rawtp; 310 extern const struct bench bench_trig_kprobe; 311 extern const struct bench bench_trig_fentry; 312 extern const struct bench bench_trig_fmodret; 313 314 static const struct bench *benchs[] = { 315 &bench_count_global, 316 &bench_count_local, 317 &bench_rename_base, 318 &bench_rename_kprobe, 319 &bench_rename_kretprobe, 320 &bench_rename_rawtp, 321 &bench_rename_fentry, 322 &bench_rename_fexit, 323 &bench_rename_fmodret, 324 &bench_trig_base, 325 &bench_trig_tp, 326 &bench_trig_rawtp, 327 &bench_trig_kprobe, 328 &bench_trig_fentry, 329 &bench_trig_fmodret, 330 }; 331 332 static void setup_benchmark() 333 { 334 int i, err; 335 336 if (!env.bench_name) { 337 fprintf(stderr, "benchmark name is not specified\n"); 338 exit(1); 339 } 340 341 for (i = 0; i < ARRAY_SIZE(benchs); i++) { 342 if (strcmp(benchs[i]->name, env.bench_name) == 0) { 343 bench = benchs[i]; 344 break; 345 } 346 } 347 if (!bench) { 348 fprintf(stderr, "benchmark '%s' not found\n", env.bench_name); 349 exit(1); 350 } 351 352 printf("Setting up benchmark '%s'...\n", bench->name); 353 354 state.producers = calloc(env.producer_cnt, sizeof(*state.producers)); 355 state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers)); 356 state.results = calloc(env.duration_sec + env.warmup_sec + 2, 357 sizeof(*state.results)); 358 if (!state.producers || !state.consumers || !state.results) 359 exit(1); 360 361 if (bench->validate) 362 bench->validate(); 363 if (bench->setup) 364 bench->setup(); 365 366 for (i = 0; i < env.consumer_cnt; i++) { 367 err = pthread_create(&state.consumers[i], NULL, 368 bench->consumer_thread, (void *)(long)i); 369 if (err) { 370 fprintf(stderr, "failed to create consumer thread #%d: %d\n", 371 i, -errno); 372 exit(1); 373 } 374 if (env.affinity) 375 set_thread_affinity(state.consumers[i], 376 next_cpu(&env.cons_cpus)); 377 } 378 379 /* unless explicit producer CPU list is specified, continue after 380 * last consumer CPU 381 */ 382 if (!env.prod_cpus.cpus) 383 env.prod_cpus.next_cpu = env.cons_cpus.next_cpu; 384 385 for (i = 0; i < env.producer_cnt; i++) { 386 err = pthread_create(&state.producers[i], NULL, 387 bench->producer_thread, (void *)(long)i); 388 if (err) { 389 fprintf(stderr, "failed to create producer thread #%d: %d\n", 390 i, -errno); 391 exit(1); 392 } 393 if (env.affinity) 394 set_thread_affinity(state.producers[i], 395 next_cpu(&env.prod_cpus)); 396 } 397 398 printf("Benchmark '%s' started.\n", bench->name); 399 } 400 401 static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER; 402 static pthread_cond_t bench_done = PTHREAD_COND_INITIALIZER; 403 404 static void collect_measurements(long delta_ns) { 405 int iter = state.res_cnt++; 406 struct bench_res *res = &state.results[iter]; 407 408 bench->measure(res); 409 410 if (bench->report_progress) 411 bench->report_progress(iter, res, delta_ns); 412 413 if (iter == env.duration_sec + env.warmup_sec) { 414 pthread_mutex_lock(&bench_done_mtx); 415 pthread_cond_signal(&bench_done); 416 pthread_mutex_unlock(&bench_done_mtx); 417 } 418 } 419 420 int main(int argc, char **argv) 421 { 422 parse_cmdline_args(argc, argv); 423 424 if (env.list) { 425 int i; 426 427 printf("Available benchmarks:\n"); 428 for (i = 0; i < ARRAY_SIZE(benchs); i++) { 429 printf("- %s\n", benchs[i]->name); 430 } 431 return 0; 432 } 433 434 setup_benchmark(); 435 436 setup_timer(); 437 438 pthread_mutex_lock(&bench_done_mtx); 439 pthread_cond_wait(&bench_done, &bench_done_mtx); 440 pthread_mutex_unlock(&bench_done_mtx); 441 442 if (bench->report_final) 443 /* skip first sample */ 444 bench->report_final(state.results + env.warmup_sec, 445 state.res_cnt - env.warmup_sec); 446 447 return 0; 448 } 449 450