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