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