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