1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * builtin-bench.c 4 * 5 * General benchmarking collections provided by perf 6 * 7 * Copyright (C) 2009, Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> 8 */ 9 10 /* 11 * Available benchmark collection list: 12 * 13 * sched ... scheduler and IPC performance 14 * syscall ... System call performance 15 * mem ... memory access performance 16 * numa ... NUMA scheduling and MM performance 17 * atomic ... Atomic operation performance 18 * futex ... Futex performance 19 * epoll ... Event poll performance 20 */ 21 #include <subcmd/parse-options.h> 22 #include "builtin.h" 23 #include "bench/bench.h" 24 25 #include <locale.h> 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 #include <sys/prctl.h> 30 #include <linux/zalloc.h> 31 32 typedef int (*bench_fn_t)(int argc, const char **argv); 33 34 struct bench { 35 const char *name; 36 const char *summary; 37 bench_fn_t fn; 38 }; 39 40 #ifdef HAVE_LIBNUMA_SUPPORT 41 static const struct bench numa_benchmarks[] = { 42 { "mem", "Benchmark for NUMA workloads", bench_numa }, 43 { "all", "Run all NUMA benchmarks", NULL }, 44 { NULL, NULL, NULL } 45 }; 46 #endif 47 48 static const struct bench sched_benchmarks[] = { 49 { "messaging", "Benchmark for scheduling and IPC", bench_sched_messaging }, 50 { "pipe", "Benchmark for pipe() between two processes", bench_sched_pipe }, 51 { "seccomp-notify", "Benchmark for seccomp user notify", bench_sched_seccomp_notify}, 52 { "all", "Run all scheduler benchmarks", NULL }, 53 { NULL, NULL, NULL } 54 }; 55 56 static const struct bench syscall_benchmarks[] = { 57 { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic }, 58 { "getpgid", "Benchmark for getpgid(2) calls", bench_syscall_getpgid }, 59 { "fork", "Benchmark for fork(2) calls", bench_syscall_fork }, 60 { "execve", "Benchmark for execve(2) calls", bench_syscall_execve }, 61 { "all", "Run all syscall benchmarks", NULL }, 62 { NULL, NULL, NULL }, 63 }; 64 65 static const struct bench mem_benchmarks[] = { 66 { "memcpy", "Benchmark for memcpy() functions", bench_mem_memcpy }, 67 { "memset", "Benchmark for memset() functions", bench_mem_memset }, 68 { "find_bit", "Benchmark for find_bit() functions", bench_mem_find_bit }, 69 { "mmap", "Benchmark for mmap() mappings", bench_mem_mmap }, 70 { "all", "Run all memory access benchmarks", NULL }, 71 { NULL, NULL, NULL } 72 }; 73 74 static const struct bench futex_benchmarks[] = { 75 { "hash", "Benchmark for futex hash table", bench_futex_hash }, 76 { "wake", "Benchmark for futex wake calls", bench_futex_wake }, 77 { "wake-parallel", "Benchmark for parallel futex wake calls", bench_futex_wake_parallel }, 78 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue }, 79 /* pi-futexes */ 80 { "lock-pi", "Benchmark for futex lock_pi calls", bench_futex_lock_pi }, 81 { "all", "Run all futex benchmarks", NULL }, 82 { NULL, NULL, NULL } 83 }; 84 85 #ifdef HAVE_EVENTFD_SUPPORT 86 static const struct bench epoll_benchmarks[] = { 87 { "wait", "Benchmark epoll concurrent epoll_waits", bench_epoll_wait }, 88 { "ctl", "Benchmark epoll concurrent epoll_ctls", bench_epoll_ctl }, 89 { "all", "Run all futex benchmarks", NULL }, 90 { NULL, NULL, NULL } 91 }; 92 #endif // HAVE_EVENTFD_SUPPORT 93 94 static const struct bench internals_benchmarks[] = { 95 { "synthesize", "Benchmark perf event synthesis", bench_synthesize }, 96 { "kallsyms-parse", "Benchmark kallsyms parsing", bench_kallsyms_parse }, 97 { "inject-build-id", "Benchmark build-id injection", bench_inject_build_id }, 98 { "evlist-open-close", "Benchmark evlist open and close", bench_evlist_open_close }, 99 { "pmu-scan", "Benchmark sysfs PMU info scanning", bench_pmu_scan }, 100 { NULL, NULL, NULL } 101 }; 102 103 static const struct bench breakpoint_benchmarks[] = { 104 { "thread", "Benchmark thread start/finish with breakpoints", bench_breakpoint_thread}, 105 { "enable", "Benchmark breakpoint enable/disable", bench_breakpoint_enable}, 106 { "all", "Run all breakpoint benchmarks", NULL}, 107 { NULL, NULL, NULL }, 108 }; 109 110 static const struct bench uprobe_benchmarks[] = { 111 { "baseline", "Baseline libc usleep(1000) call", bench_uprobe_baseline, }, 112 { "empty", "Attach empty BPF prog to uprobe on usleep, system wide", bench_uprobe_empty, }, 113 { "trace_printk", "Attach trace_printk BPF prog to uprobe on usleep syswide", bench_uprobe_trace_printk, }, 114 { "empty_ret", "Attach empty BPF prog to uretprobe on usleep, system wide", bench_uprobe_empty_ret, }, 115 { "trace_printk_ret", "Attach trace_printk BPF prog to uretprobe on usleep syswide", bench_uprobe_trace_printk_ret,}, 116 { NULL, NULL, NULL }, 117 }; 118 119 static const struct bench atomic_benchmarks[] = { 120 { "cas", "Benchmark CAS (compare-and-swap) operations", bench_atomic_cas }, 121 { "inc", "Benchmark atomic fetch-add operations", bench_atomic_inc }, 122 { "all", "Run all atomic benchmarks", NULL }, 123 { NULL, NULL, NULL } 124 }; 125 126 struct collection { 127 const char *name; 128 const char *summary; 129 const struct bench *benchmarks; 130 }; 131 132 static const struct collection collections[] = { 133 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks }, 134 { "syscall", "System call benchmarks", syscall_benchmarks }, 135 { "mem", "Memory access benchmarks", mem_benchmarks }, 136 #ifdef HAVE_LIBNUMA_SUPPORT 137 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, 138 #endif 139 { "atomic", "Atomic operation benchmarks", atomic_benchmarks }, 140 {"futex", "Futex stressing benchmarks", futex_benchmarks }, 141 #ifdef HAVE_EVENTFD_SUPPORT 142 {"epoll", "Epoll stressing benchmarks", epoll_benchmarks }, 143 #endif 144 { "internals", "Perf-internals benchmarks", internals_benchmarks }, 145 { "breakpoint", "Breakpoint benchmarks", breakpoint_benchmarks }, 146 { "uprobe", "uprobe benchmarks", uprobe_benchmarks }, 147 { "all", "All benchmarks", NULL }, 148 { NULL, NULL, NULL } 149 }; 150 151 /* Iterate over all benchmark collections: */ 152 #define for_each_collection(coll) \ 153 for (coll = collections; coll->name; coll++) 154 155 /* Iterate over all benchmarks within a collection: */ 156 #define for_each_bench(coll, bench) \ 157 for (bench = coll->benchmarks; bench && bench->name; bench++) 158 159 static void dump_benchmarks(const struct collection *coll) 160 { 161 const struct bench *bench; 162 163 printf("\n # List of available benchmarks for collection '%s':\n\n", coll->name); 164 165 for_each_bench(coll, bench) 166 printf("%14s: %s\n", bench->name, bench->summary); 167 168 printf("\n"); 169 } 170 171 static const char *bench_format_str; 172 173 /* Output/formatting style, exported to benchmark modules: */ 174 int bench_format = BENCH_FORMAT_DEFAULT; 175 unsigned int bench_repeat = 10; /* default number of times to repeat the run */ 176 177 static const struct option bench_options[] = { 178 OPT_STRING('f', "format", &bench_format_str, "default|simple", "Specify the output formatting style"), 179 OPT_UINTEGER('r', "repeat", &bench_repeat, "Specify number of times to repeat the run"), 180 OPT_END() 181 }; 182 183 static const char * const bench_usage[] = { 184 "perf bench [<common options>] <collection> <benchmark> [<options>]", 185 NULL 186 }; 187 188 static void print_usage(void) 189 { 190 const struct collection *coll; 191 int i; 192 193 printf("Usage: \n"); 194 for (i = 0; bench_usage[i]; i++) 195 printf("\t%s\n", bench_usage[i]); 196 printf("\n"); 197 198 printf(" # List of all available benchmark collections:\n\n"); 199 200 for_each_collection(coll) 201 printf("%14s: %s\n", coll->name, coll->summary); 202 printf("\n"); 203 } 204 205 static int bench_str2int(const char *str) 206 { 207 if (!str) 208 return BENCH_FORMAT_DEFAULT; 209 210 if (!strcmp(str, BENCH_FORMAT_DEFAULT_STR)) 211 return BENCH_FORMAT_DEFAULT; 212 else if (!strcmp(str, BENCH_FORMAT_SIMPLE_STR)) 213 return BENCH_FORMAT_SIMPLE; 214 215 return BENCH_FORMAT_UNKNOWN; 216 } 217 218 /* 219 * Run a specific benchmark but first rename the running task's ->comm[] 220 * to something meaningful: 221 */ 222 static int run_bench(const char *coll_name, const char *bench_name, bench_fn_t fn, 223 int argc, const char **argv) 224 { 225 int size; 226 char *name; 227 int ret; 228 229 size = strlen(coll_name) + 1 + strlen(bench_name) + 1; 230 231 name = zalloc(size); 232 BUG_ON(!name); 233 234 scnprintf(name, size, "%s-%s", coll_name, bench_name); 235 236 prctl(PR_SET_NAME, name); 237 argv[0] = name; 238 239 ret = fn(argc, argv); 240 241 free(name); 242 243 return ret; 244 } 245 246 static void run_collection(const struct collection *coll) 247 { 248 const struct bench *bench; 249 const char *argv[2]; 250 251 argv[1] = NULL; 252 /* 253 * TODO: 254 * 255 * Preparing preset parameters for 256 * embedded, ordinary PC, HPC, etc... 257 * would be helpful. 258 */ 259 for_each_bench(coll, bench) { 260 if (!bench->fn) 261 break; 262 printf("# Running %s/%s benchmark...\n", coll->name, bench->name); 263 264 argv[1] = bench->name; 265 run_bench(coll->name, bench->name, bench->fn, 1, argv); 266 printf("\n"); 267 } 268 } 269 270 static void run_all_collections(void) 271 { 272 const struct collection *coll; 273 274 for_each_collection(coll) 275 run_collection(coll); 276 } 277 278 int cmd_bench(int argc, const char **argv) 279 { 280 const struct collection *coll; 281 int ret = 0; 282 283 /* Unbuffered output */ 284 setvbuf(stdout, NULL, _IONBF, 0); 285 setlocale(LC_ALL, ""); 286 287 if (argc < 2) { 288 /* No collection specified. */ 289 print_usage(); 290 goto end; 291 } 292 293 argc = parse_options(argc, argv, bench_options, bench_usage, 294 PARSE_OPT_STOP_AT_NON_OPTION); 295 296 bench_format = bench_str2int(bench_format_str); 297 if (bench_format == BENCH_FORMAT_UNKNOWN) { 298 printf("Unknown format descriptor: '%s'\n", bench_format_str); 299 goto end; 300 } 301 302 if (bench_repeat == 0) { 303 printf("Invalid repeat option: Must specify a positive value\n"); 304 goto end; 305 } 306 307 if (argc < 1) { 308 print_usage(); 309 goto end; 310 } 311 312 if (!strcmp(argv[0], "all")) { 313 run_all_collections(); 314 goto end; 315 } 316 317 for_each_collection(coll) { 318 const struct bench *bench; 319 320 if (strcmp(coll->name, argv[0])) 321 continue; 322 323 if (argc < 2) { 324 /* No bench specified. */ 325 dump_benchmarks(coll); 326 goto end; 327 } 328 329 if (!strcmp(argv[1], "all")) { 330 run_collection(coll); 331 goto end; 332 } 333 334 for_each_bench(coll, bench) { 335 if (strcmp(bench->name, argv[1])) 336 continue; 337 338 if (bench_format == BENCH_FORMAT_DEFAULT) 339 printf("# Running '%s/%s' benchmark:\n", coll->name, bench->name); 340 ret = run_bench(coll->name, bench->name, bench->fn, argc-1, argv+1); 341 goto end; 342 } 343 344 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { 345 dump_benchmarks(coll); 346 goto end; 347 } 348 349 printf("Unknown benchmark: '%s' for collection '%s'\n", argv[1], argv[0]); 350 ret = 1; 351 goto end; 352 } 353 354 printf("Unknown collection: '%s'\n", argv[0]); 355 ret = 1; 356 357 end: 358 return ret; 359 } 360