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