1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2019 Facebook */ 4 5 #include <assert.h> 6 #include <limits.h> 7 #include <unistd.h> 8 #include <sys/file.h> 9 #include <sys/resource.h> 10 #include <sys/time.h> 11 #include <linux/err.h> 12 #include <linux/list.h> 13 #include <linux/zalloc.h> 14 #include <api/fs/fs.h> 15 #include <bpf/bpf.h> 16 #include <bpf/btf.h> 17 #include <perf/bpf_perf.h> 18 19 #include "bpf_counter.h" 20 #include "bpf-utils.h" 21 #include "counts.h" 22 #include "debug.h" 23 #include "evsel.h" 24 #include "evlist.h" 25 #include "target.h" 26 #include "cgroup.h" 27 #include "cpumap.h" 28 #include "thread_map.h" 29 30 #include "bpf_skel/bpf_prog_profiler.skel.h" 31 #include "bpf_skel/bperf_u.h" 32 #include "bpf_skel/bperf_leader.skel.h" 33 #include "bpf_skel/bperf_follower.skel.h" 34 35 struct bpf_counter { 36 void *skel; 37 struct list_head list; 38 }; 39 40 #define ATTR_MAP_SIZE 16 41 42 static void *u64_to_ptr(__u64 ptr) 43 { 44 return (void *)(unsigned long)ptr; 45 } 46 47 48 void set_max_rlimit(void) 49 { 50 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY }; 51 52 setrlimit(RLIMIT_MEMLOCK, &rinf); 53 } 54 55 static __u32 bpf_link_get_id(int fd) 56 { 57 struct bpf_link_info link_info = { .id = 0, }; 58 __u32 link_info_len = sizeof(link_info); 59 60 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len); 61 return link_info.id; 62 } 63 64 static __u32 bpf_link_get_prog_id(int fd) 65 { 66 struct bpf_link_info link_info = { .id = 0, }; 67 __u32 link_info_len = sizeof(link_info); 68 69 bpf_obj_get_info_by_fd(fd, &link_info, &link_info_len); 70 return link_info.prog_id; 71 } 72 73 static __u32 bpf_map_get_id(int fd) 74 { 75 struct bpf_map_info map_info = { .id = 0, }; 76 __u32 map_info_len = sizeof(map_info); 77 78 bpf_obj_get_info_by_fd(fd, &map_info, &map_info_len); 79 return map_info.id; 80 } 81 82 /* trigger the leader program on a cpu */ 83 int bperf_trigger_reading(int prog_fd, int cpu) 84 { 85 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, 86 .ctx_in = NULL, 87 .ctx_size_in = 0, 88 .flags = BPF_F_TEST_RUN_ON_CPU, 89 .cpu = cpu, 90 .retval = 0, 91 ); 92 93 return bpf_prog_test_run_opts(prog_fd, &opts); 94 } 95 96 static struct bpf_counter *bpf_counter_alloc(void) 97 { 98 struct bpf_counter *counter; 99 100 counter = zalloc(sizeof(*counter)); 101 if (counter) 102 INIT_LIST_HEAD(&counter->list); 103 return counter; 104 } 105 106 static int bpf_program_profiler__destroy(struct evsel *evsel) 107 { 108 struct bpf_counter *counter, *tmp; 109 110 list_for_each_entry_safe(counter, tmp, 111 &evsel->bpf_counter_list, list) { 112 list_del_init(&counter->list); 113 bpf_prog_profiler_bpf__destroy(counter->skel); 114 free(counter); 115 } 116 assert(list_empty(&evsel->bpf_counter_list)); 117 118 return 0; 119 } 120 121 static char *bpf_target_prog_name(int tgt_fd) 122 { 123 struct bpf_func_info *func_info; 124 struct perf_bpil *info_linear; 125 const struct btf_type *t; 126 struct btf *btf = NULL; 127 char *name = NULL; 128 129 info_linear = get_bpf_prog_info_linear(tgt_fd, 1UL << PERF_BPIL_FUNC_INFO); 130 if (IS_ERR_OR_NULL(info_linear)) { 131 pr_debug("failed to get info_linear for prog FD %d\n", tgt_fd); 132 return NULL; 133 } 134 135 if (info_linear->info.btf_id == 0) { 136 pr_debug("prog FD %d doesn't have valid btf\n", tgt_fd); 137 goto out; 138 } 139 140 btf = btf__load_from_kernel_by_id(info_linear->info.btf_id); 141 if (libbpf_get_error(btf)) { 142 pr_debug("failed to load btf for prog FD %d\n", tgt_fd); 143 goto out; 144 } 145 146 func_info = u64_to_ptr(info_linear->info.func_info); 147 t = btf__type_by_id(btf, func_info[0].type_id); 148 if (!t) { 149 pr_debug("btf %d doesn't have type %d\n", 150 info_linear->info.btf_id, func_info[0].type_id); 151 goto out; 152 } 153 name = strdup(btf__name_by_offset(btf, t->name_off)); 154 out: 155 btf__free(btf); 156 free(info_linear); 157 return name; 158 } 159 160 static int bpf_program_profiler_load_one(struct evsel *evsel, u32 prog_id) 161 { 162 struct bpf_prog_profiler_bpf *skel; 163 struct bpf_counter *counter; 164 struct bpf_program *prog; 165 char *prog_name = NULL; 166 int prog_fd; 167 int err; 168 169 prog_fd = bpf_prog_get_fd_by_id(prog_id); 170 if (prog_fd < 0) { 171 pr_err("Failed to open fd for bpf prog %u\n", prog_id); 172 return -1; 173 } 174 counter = bpf_counter_alloc(); 175 if (!counter) { 176 close(prog_fd); 177 return -1; 178 } 179 180 skel = bpf_prog_profiler_bpf__open(); 181 if (!skel) { 182 pr_err("Failed to open bpf skeleton\n"); 183 goto err_out; 184 } 185 186 skel->rodata->num_cpu = evsel__nr_cpus(evsel); 187 188 bpf_map__set_max_entries(skel->maps.events, evsel__nr_cpus(evsel)); 189 bpf_map__set_max_entries(skel->maps.fentry_readings, 1); 190 bpf_map__set_max_entries(skel->maps.accum_readings, 1); 191 192 prog_name = bpf_target_prog_name(prog_fd); 193 if (!prog_name) { 194 pr_err("Failed to get program name for bpf prog %u. Does it have BTF?\n", prog_id); 195 goto err_out; 196 } 197 198 bpf_object__for_each_program(prog, skel->obj) { 199 err = bpf_program__set_attach_target(prog, prog_fd, prog_name); 200 if (err) { 201 pr_err("bpf_program__set_attach_target failed.\n" 202 "Does bpf prog %u have BTF?\n", prog_id); 203 goto err_out; 204 } 205 } 206 set_max_rlimit(); 207 err = bpf_prog_profiler_bpf__load(skel); 208 if (err) { 209 pr_err("bpf_prog_profiler_bpf__load failed\n"); 210 goto err_out; 211 } 212 213 assert(skel != NULL); 214 counter->skel = skel; 215 list_add(&counter->list, &evsel->bpf_counter_list); 216 free(prog_name); 217 close(prog_fd); 218 return 0; 219 err_out: 220 bpf_prog_profiler_bpf__destroy(skel); 221 free(prog_name); 222 free(counter); 223 close(prog_fd); 224 return -1; 225 } 226 227 static int bpf_program_profiler__load(struct evsel *evsel, struct target *target) 228 { 229 char *bpf_str, *bpf_str_, *tok, *saveptr = NULL, *p; 230 u32 prog_id; 231 int ret; 232 233 bpf_str_ = bpf_str = strdup(target->bpf_str); 234 if (!bpf_str) 235 return -1; 236 237 while ((tok = strtok_r(bpf_str, ",", &saveptr)) != NULL) { 238 prog_id = strtoul(tok, &p, 10); 239 if (prog_id == 0 || prog_id == UINT_MAX || 240 (*p != '\0' && *p != ',')) { 241 pr_err("Failed to parse bpf prog ids %s\n", 242 target->bpf_str); 243 free(bpf_str_); 244 return -1; 245 } 246 247 ret = bpf_program_profiler_load_one(evsel, prog_id); 248 if (ret) { 249 bpf_program_profiler__destroy(evsel); 250 free(bpf_str_); 251 return -1; 252 } 253 bpf_str = NULL; 254 } 255 free(bpf_str_); 256 return 0; 257 } 258 259 static int bpf_program_profiler__enable(struct evsel *evsel) 260 { 261 struct bpf_counter *counter; 262 int ret; 263 264 list_for_each_entry(counter, &evsel->bpf_counter_list, list) { 265 assert(counter->skel != NULL); 266 ret = bpf_prog_profiler_bpf__attach(counter->skel); 267 if (ret) { 268 bpf_program_profiler__destroy(evsel); 269 return ret; 270 } 271 } 272 return 0; 273 } 274 275 static int bpf_program_profiler__disable(struct evsel *evsel) 276 { 277 struct bpf_counter *counter; 278 279 list_for_each_entry(counter, &evsel->bpf_counter_list, list) { 280 assert(counter->skel != NULL); 281 bpf_prog_profiler_bpf__detach(counter->skel); 282 } 283 return 0; 284 } 285 286 static int bpf_program_profiler__read(struct evsel *evsel) 287 { 288 // BPF_MAP_TYPE_PERCPU_ARRAY uses /sys/devices/system/cpu/possible 289 // Sometimes possible > online, like on a Ryzen 3900X that has 24 290 // threads but its possible showed 0-31 -acme 291 int num_cpu_bpf = libbpf_num_possible_cpus(); 292 struct bpf_perf_event_value values[num_cpu_bpf]; 293 struct bpf_counter *counter; 294 struct perf_counts_values *counts; 295 int reading_map_fd; 296 __u32 key = 0; 297 int err, bpf_cpu; 298 unsigned int idx; 299 300 if (list_empty(&evsel->bpf_counter_list)) 301 return -EAGAIN; 302 303 perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) { 304 counts = perf_counts(evsel->counts, idx, 0); 305 counts->val = 0; 306 counts->ena = 0; 307 counts->run = 0; 308 } 309 list_for_each_entry(counter, &evsel->bpf_counter_list, list) { 310 struct bpf_prog_profiler_bpf *skel = counter->skel; 311 312 assert(skel != NULL); 313 reading_map_fd = bpf_map__fd(skel->maps.accum_readings); 314 315 err = bpf_map_lookup_elem(reading_map_fd, &key, values); 316 if (err) { 317 pr_err("failed to read value\n"); 318 return err; 319 } 320 321 for (bpf_cpu = 0; bpf_cpu < num_cpu_bpf; bpf_cpu++) { 322 int i = perf_cpu_map__idx(evsel__cpus(evsel), 323 (struct perf_cpu){.cpu = bpf_cpu}); 324 325 if (i == -1) 326 continue; 327 counts = perf_counts(evsel->counts, i, 0); 328 counts->val += values[bpf_cpu].counter; 329 counts->ena += values[bpf_cpu].enabled; 330 counts->run += values[bpf_cpu].running; 331 } 332 } 333 return 0; 334 } 335 336 static int bpf_program_profiler__install_pe(struct evsel *evsel, int cpu_map_idx, 337 int fd) 338 { 339 struct bpf_prog_profiler_bpf *skel; 340 struct bpf_counter *counter; 341 int cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx).cpu; 342 int ret; 343 344 list_for_each_entry(counter, &evsel->bpf_counter_list, list) { 345 skel = counter->skel; 346 assert(skel != NULL); 347 348 ret = bpf_map_update_elem(bpf_map__fd(skel->maps.events), 349 &cpu, &fd, BPF_ANY); 350 if (ret) 351 return ret; 352 } 353 return 0; 354 } 355 356 static struct bpf_counter_ops bpf_program_profiler_ops = { 357 .load = bpf_program_profiler__load, 358 .enable = bpf_program_profiler__enable, 359 .disable = bpf_program_profiler__disable, 360 .read = bpf_program_profiler__read, 361 .destroy = bpf_program_profiler__destroy, 362 .install_pe = bpf_program_profiler__install_pe, 363 }; 364 365 static bool bperf_attr_map_compatible(int attr_map_fd) 366 { 367 struct bpf_map_info map_info = {0}; 368 __u32 map_info_len = sizeof(map_info); 369 int err; 370 371 err = bpf_obj_get_info_by_fd(attr_map_fd, &map_info, &map_info_len); 372 373 if (err) 374 return false; 375 return (map_info.key_size == sizeof(struct perf_event_attr)) && 376 (map_info.value_size == sizeof(struct perf_event_attr_map_entry)); 377 } 378 379 static int bperf_lock_attr_map(struct target *target) 380 { 381 char path[PATH_MAX]; 382 int map_fd, err; 383 384 if (target->attr_map) { 385 scnprintf(path, PATH_MAX, "%s", target->attr_map); 386 } else { 387 scnprintf(path, PATH_MAX, "%s/fs/bpf/%s", sysfs__mountpoint(), 388 BPF_PERF_DEFAULT_ATTR_MAP_PATH); 389 } 390 391 if (access(path, F_OK)) { 392 map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, 393 sizeof(struct perf_event_attr), 394 sizeof(struct perf_event_attr_map_entry), 395 ATTR_MAP_SIZE, NULL); 396 if (map_fd < 0) 397 return -1; 398 399 err = bpf_obj_pin(map_fd, path); 400 if (err) { 401 /* someone pinned the map in parallel? */ 402 close(map_fd); 403 map_fd = bpf_obj_get(path); 404 if (map_fd < 0) 405 return -1; 406 } 407 } else { 408 map_fd = bpf_obj_get(path); 409 if (map_fd < 0) 410 return -1; 411 } 412 413 if (!bperf_attr_map_compatible(map_fd)) { 414 close(map_fd); 415 return -1; 416 417 } 418 err = flock(map_fd, LOCK_EX); 419 if (err) { 420 close(map_fd); 421 return -1; 422 } 423 return map_fd; 424 } 425 426 static int bperf_check_target(struct evsel *evsel, 427 struct target *target, 428 enum bperf_filter_type *filter_type, 429 __u32 *filter_entry_cnt) 430 { 431 if (evsel->core.leader->nr_members > 1) { 432 pr_err("bpf managed perf events do not yet support groups.\n"); 433 return -1; 434 } 435 436 /* determine filter type based on target */ 437 if (target->system_wide) { 438 *filter_type = BPERF_FILTER_GLOBAL; 439 *filter_entry_cnt = 1; 440 } else if (target->cpu_list) { 441 *filter_type = BPERF_FILTER_CPU; 442 *filter_entry_cnt = perf_cpu_map__nr(evsel__cpus(evsel)); 443 } else if (target->tid) { 444 *filter_type = BPERF_FILTER_PID; 445 *filter_entry_cnt = perf_thread_map__nr(evsel->core.threads); 446 } else if (target->pid || evsel->evlist->workload.pid != -1) { 447 *filter_type = BPERF_FILTER_TGID; 448 *filter_entry_cnt = perf_thread_map__nr(evsel->core.threads); 449 } else { 450 pr_err("bpf managed perf events do not yet support these targets.\n"); 451 return -1; 452 } 453 454 return 0; 455 } 456 457 static __u32 filter_entry_cnt; 458 459 static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd, 460 struct perf_event_attr_map_entry *entry) 461 { 462 struct bperf_leader_bpf *skel = bperf_leader_bpf__open(); 463 int link_fd, diff_map_fd, err; 464 struct bpf_link *link = NULL; 465 struct perf_thread_map *threads; 466 467 if (!skel) { 468 pr_err("Failed to open leader skeleton\n"); 469 return -1; 470 } 471 472 bpf_map__set_max_entries(skel->maps.events, libbpf_num_possible_cpus()); 473 err = bperf_leader_bpf__load(skel); 474 if (err) { 475 pr_err("Failed to load leader skeleton\n"); 476 goto out; 477 } 478 479 link = bpf_program__attach(skel->progs.on_switch); 480 if (IS_ERR(link)) { 481 pr_err("Failed to attach leader program\n"); 482 err = PTR_ERR(link); 483 goto out; 484 } 485 486 link_fd = bpf_link__fd(link); 487 diff_map_fd = bpf_map__fd(skel->maps.diff_readings); 488 entry->link_id = bpf_link_get_id(link_fd); 489 entry->diff_map_id = bpf_map_get_id(diff_map_fd); 490 err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, entry, BPF_ANY); 491 assert(err == 0); 492 493 evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry->link_id); 494 assert(evsel->bperf_leader_link_fd >= 0); 495 496 /* 497 * save leader_skel for install_pe, which is called within 498 * following evsel__open_per_cpu call 499 */ 500 evsel->leader_skel = skel; 501 assert(!perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus)); 502 /* Always open system wide. */ 503 threads = thread_map__new_by_tid(-1); 504 evsel__open(evsel, evsel->core.cpus, threads); 505 perf_thread_map__put(threads); 506 507 out: 508 bperf_leader_bpf__destroy(skel); 509 bpf_link__destroy(link); 510 return err; 511 } 512 513 static int bperf_attach_follower_program(struct bperf_follower_bpf *skel, 514 enum bperf_filter_type filter_type, 515 bool inherit) 516 { 517 struct bpf_link *link; 518 int err = 0; 519 520 if ((filter_type == BPERF_FILTER_PID || 521 filter_type == BPERF_FILTER_TGID) && inherit) 522 /* attach all follower bpf progs to enable event inheritance */ 523 err = bperf_follower_bpf__attach(skel); 524 else { 525 link = bpf_program__attach(skel->progs.fexit_XXX); 526 if (IS_ERR(link)) 527 err = PTR_ERR(link); 528 } 529 530 return err; 531 } 532 533 static int bperf__load(struct evsel *evsel, struct target *target) 534 { 535 struct perf_event_attr_map_entry entry = {0xffffffff, 0xffffffff}; 536 int attr_map_fd, diff_map_fd = -1, err; 537 enum bperf_filter_type filter_type; 538 __u32 i; 539 540 if (bperf_check_target(evsel, target, &filter_type, &filter_entry_cnt)) 541 return -1; 542 543 evsel->bperf_leader_prog_fd = -1; 544 evsel->bperf_leader_link_fd = -1; 545 546 /* 547 * Step 1: hold a fd on the leader program and the bpf_link, if 548 * the program is not already gone, reload the program. 549 * Use flock() to ensure exclusive access to the perf_event_attr 550 * map. 551 */ 552 attr_map_fd = bperf_lock_attr_map(target); 553 if (attr_map_fd < 0) { 554 pr_err("Failed to lock perf_event_attr map\n"); 555 return -1; 556 } 557 558 err = bpf_map_lookup_elem(attr_map_fd, &evsel->core.attr, &entry); 559 if (err) { 560 err = bpf_map_update_elem(attr_map_fd, &evsel->core.attr, &entry, BPF_ANY); 561 if (err) 562 goto out; 563 } 564 565 evsel->bperf_leader_link_fd = bpf_link_get_fd_by_id(entry.link_id); 566 if (evsel->bperf_leader_link_fd < 0 && 567 bperf_reload_leader_program(evsel, attr_map_fd, &entry)) { 568 err = -1; 569 goto out; 570 } 571 /* 572 * The bpf_link holds reference to the leader program, and the 573 * leader program holds reference to the maps. Therefore, if 574 * link_id is valid, diff_map_id should also be valid. 575 */ 576 evsel->bperf_leader_prog_fd = bpf_prog_get_fd_by_id( 577 bpf_link_get_prog_id(evsel->bperf_leader_link_fd)); 578 assert(evsel->bperf_leader_prog_fd >= 0); 579 580 diff_map_fd = bpf_map_get_fd_by_id(entry.diff_map_id); 581 assert(diff_map_fd >= 0); 582 583 /* 584 * bperf uses BPF_PROG_TEST_RUN to get accurate reading. Check 585 * whether the kernel support it 586 */ 587 err = bperf_trigger_reading(evsel->bperf_leader_prog_fd, 0); 588 if (err) { 589 pr_err("The kernel does not support test_run for raw_tp BPF programs.\n" 590 "Therefore, --use-bpf might show inaccurate readings\n"); 591 goto out; 592 } 593 594 /* Step 2: load the follower skeleton */ 595 evsel->follower_skel = bperf_follower_bpf__open(); 596 if (!evsel->follower_skel) { 597 err = -1; 598 pr_err("Failed to open follower skeleton\n"); 599 goto out; 600 } 601 602 /* attach fexit program to the leader program */ 603 bpf_program__set_attach_target(evsel->follower_skel->progs.fexit_XXX, 604 evsel->bperf_leader_prog_fd, "on_switch"); 605 606 /* connect to leader diff_reading map */ 607 bpf_map__reuse_fd(evsel->follower_skel->maps.diff_readings, diff_map_fd); 608 609 /* set up reading map */ 610 bpf_map__set_max_entries(evsel->follower_skel->maps.accum_readings, 611 filter_entry_cnt); 612 err = bperf_follower_bpf__load(evsel->follower_skel); 613 if (err) { 614 pr_err("Failed to load follower skeleton\n"); 615 bperf_follower_bpf__destroy(evsel->follower_skel); 616 evsel->follower_skel = NULL; 617 goto out; 618 } 619 620 for (i = 0; i < filter_entry_cnt; i++) { 621 int filter_map_fd; 622 __u32 key; 623 struct bperf_filter_value fval = { i, 0 }; 624 625 if (filter_type == BPERF_FILTER_PID || 626 filter_type == BPERF_FILTER_TGID) 627 key = perf_thread_map__pid(evsel->core.threads, i); 628 else if (filter_type == BPERF_FILTER_CPU) 629 key = perf_cpu_map__cpu(evsel->core.cpus, i).cpu; 630 else 631 break; 632 633 filter_map_fd = bpf_map__fd(evsel->follower_skel->maps.filter); 634 bpf_map_update_elem(filter_map_fd, &key, &fval, BPF_ANY); 635 } 636 637 evsel->follower_skel->bss->type = filter_type; 638 evsel->follower_skel->bss->inherit = target->inherit; 639 640 err = bperf_attach_follower_program(evsel->follower_skel, filter_type, 641 target->inherit); 642 643 out: 644 if (err && evsel->bperf_leader_link_fd >= 0) 645 close(evsel->bperf_leader_link_fd); 646 if (err && evsel->bperf_leader_prog_fd >= 0) 647 close(evsel->bperf_leader_prog_fd); 648 if (diff_map_fd >= 0) 649 close(diff_map_fd); 650 651 flock(attr_map_fd, LOCK_UN); 652 close(attr_map_fd); 653 654 return err; 655 } 656 657 static int bperf__install_pe(struct evsel *evsel, int cpu_map_idx, int fd) 658 { 659 struct bperf_leader_bpf *skel = evsel->leader_skel; 660 int cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx).cpu; 661 662 return bpf_map_update_elem(bpf_map__fd(skel->maps.events), 663 &cpu, &fd, BPF_ANY); 664 } 665 666 /* 667 * trigger the leader prog on each cpu, so the accum_reading map could get 668 * the latest readings. 669 */ 670 static int bperf_sync_counters(struct evsel *evsel) 671 { 672 struct perf_cpu cpu; 673 unsigned int idx; 674 675 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) 676 bperf_trigger_reading(evsel->bperf_leader_prog_fd, cpu.cpu); 677 678 return 0; 679 } 680 681 static int bperf__enable(struct evsel *evsel) 682 { 683 evsel->follower_skel->bss->enabled = 1; 684 return 0; 685 } 686 687 static int bperf__disable(struct evsel *evsel) 688 { 689 evsel->follower_skel->bss->enabled = 0; 690 return 0; 691 } 692 693 static int bperf__read(struct evsel *evsel) 694 { 695 struct bperf_follower_bpf *skel = evsel->follower_skel; 696 __u32 num_cpu_bpf = cpu__max_cpu().cpu; 697 struct bpf_perf_event_value values[num_cpu_bpf]; 698 struct perf_counts_values *counts; 699 int reading_map_fd, err = 0; 700 701 bperf_sync_counters(evsel); 702 reading_map_fd = bpf_map__fd(skel->maps.accum_readings); 703 704 for (__u32 i = 0; i < filter_entry_cnt; i++) { 705 struct perf_cpu entry; 706 __u32 cpu; 707 708 err = bpf_map_lookup_elem(reading_map_fd, &i, values); 709 if (err) 710 goto out; 711 switch (evsel->follower_skel->bss->type) { 712 case BPERF_FILTER_GLOBAL: { 713 unsigned int j; 714 715 assert(i == 0); 716 perf_cpu_map__for_each_cpu(entry, j, evsel__cpus(evsel)) { 717 counts = perf_counts(evsel->counts, j, 0); 718 counts->val = values[entry.cpu].counter; 719 counts->ena = values[entry.cpu].enabled; 720 counts->run = values[entry.cpu].running; 721 } 722 break; 723 } 724 case BPERF_FILTER_CPU: 725 cpu = perf_cpu_map__cpu(evsel__cpus(evsel), i).cpu; 726 assert(cpu >= 0); 727 counts = perf_counts(evsel->counts, i, 0); 728 counts->val = values[cpu].counter; 729 counts->ena = values[cpu].enabled; 730 counts->run = values[cpu].running; 731 break; 732 case BPERF_FILTER_PID: 733 case BPERF_FILTER_TGID: 734 counts = perf_counts(evsel->counts, 0, i); 735 counts->val = 0; 736 counts->ena = 0; 737 counts->run = 0; 738 739 for (cpu = 0; cpu < num_cpu_bpf; cpu++) { 740 counts->val += values[cpu].counter; 741 counts->ena += values[cpu].enabled; 742 counts->run += values[cpu].running; 743 } 744 break; 745 default: 746 break; 747 } 748 } 749 out: 750 return err; 751 } 752 753 static int bperf__destroy(struct evsel *evsel) 754 { 755 bperf_follower_bpf__destroy(evsel->follower_skel); 756 close(evsel->bperf_leader_prog_fd); 757 close(evsel->bperf_leader_link_fd); 758 return 0; 759 } 760 761 /* 762 * bperf: share hardware PMCs with BPF 763 * 764 * perf uses performance monitoring counters (PMC) to monitor system 765 * performance. The PMCs are limited hardware resources. For example, 766 * Intel CPUs have 3x fixed PMCs and 4x programmable PMCs per cpu. 767 * 768 * Modern data center systems use these PMCs in many different ways: 769 * system level monitoring, (maybe nested) container level monitoring, per 770 * process monitoring, profiling (in sample mode), etc. In some cases, 771 * there are more active perf_events than available hardware PMCs. To allow 772 * all perf_events to have a chance to run, it is necessary to do expensive 773 * time multiplexing of events. 774 * 775 * On the other hand, many monitoring tools count the common metrics 776 * (cycles, instructions). It is a waste to have multiple tools create 777 * multiple perf_events of "cycles" and occupy multiple PMCs. 778 * 779 * bperf tries to reduce such wastes by allowing multiple perf_events of 780 * "cycles" or "instructions" (at different scopes) to share PMUs. Instead 781 * of having each perf-stat session to read its own perf_events, bperf uses 782 * BPF programs to read the perf_events and aggregate readings to BPF maps. 783 * Then, the perf-stat session(s) reads the values from these BPF maps. 784 * 785 * || 786 * shared progs and maps <- || -> per session progs and maps 787 * || 788 * --------------- || 789 * | perf_events | || 790 * --------------- fexit || ----------------- 791 * | --------||----> | follower prog | 792 * --------------- / || --- ----------------- 793 * cs -> | leader prog |/ ||/ | | 794 * --> --------------- /|| -------------- ------------------ 795 * / | | / || | filter map | | accum_readings | 796 * / ------------ ------------ || -------------- ------------------ 797 * | | prev map | | diff map | || | 798 * | ------------ ------------ || | 799 * \ || | 800 * = \ ==================================================== | ============ 801 * \ / user space 802 * \ / 803 * \ / 804 * BPF_PROG_TEST_RUN BPF_MAP_LOOKUP_ELEM 805 * \ / 806 * \ / 807 * \------ perf-stat ----------------------/ 808 * 809 * The figure above shows the architecture of bperf. Note that the figure 810 * is divided into 3 regions: shared progs and maps (top left), per session 811 * progs and maps (top right), and user space (bottom). 812 * 813 * The leader prog is triggered on each context switch (cs). The leader 814 * prog reads perf_events and stores the difference (current_reading - 815 * previous_reading) to the diff map. For the same metric, e.g. "cycles", 816 * multiple perf-stat sessions share the same leader prog. 817 * 818 * Each perf-stat session creates a follower prog as fexit program to the 819 * leader prog. It is possible to attach up to BPF_MAX_TRAMP_PROGS (38) 820 * follower progs to the same leader prog. The follower prog checks current 821 * task and processor ID to decide whether to add the value from the diff 822 * map to its accumulated reading map (accum_readings). 823 * 824 * Finally, perf-stat user space reads the value from accum_reading map. 825 * 826 * Besides context switch, it is also necessary to trigger the leader prog 827 * before perf-stat reads the value. Otherwise, the accum_reading map may 828 * not have the latest reading from the perf_events. This is achieved by 829 * triggering the event via sys_bpf(BPF_PROG_TEST_RUN) to each CPU. 830 * 831 * Comment before the definition of struct perf_event_attr_map_entry 832 * describes how different sessions of perf-stat share information about 833 * the leader prog. 834 */ 835 836 static struct bpf_counter_ops bperf_ops = { 837 .load = bperf__load, 838 .enable = bperf__enable, 839 .disable = bperf__disable, 840 .read = bperf__read, 841 .install_pe = bperf__install_pe, 842 .destroy = bperf__destroy, 843 }; 844 845 extern struct bpf_counter_ops bperf_cgrp_ops; 846 847 static bool bpf_counter_skip(struct evsel *evsel) 848 { 849 return evsel->bpf_counter_ops == NULL; 850 } 851 852 int bpf_counter__install_pe(struct evsel *evsel, int cpu_map_idx, int fd) 853 { 854 if (bpf_counter_skip(evsel)) 855 return 0; 856 return evsel->bpf_counter_ops->install_pe(evsel, cpu_map_idx, fd); 857 } 858 859 int bpf_counter__load(struct evsel *evsel, struct target *target) 860 { 861 if (target->bpf_str) 862 evsel->bpf_counter_ops = &bpf_program_profiler_ops; 863 else if (cgrp_event_expanded && target->use_bpf) 864 evsel->bpf_counter_ops = &bperf_cgrp_ops; 865 else if (target->use_bpf || evsel->bpf_counter || 866 evsel__match_bpf_counter_events(evsel->name)) 867 evsel->bpf_counter_ops = &bperf_ops; 868 869 if (evsel->bpf_counter_ops) 870 return evsel->bpf_counter_ops->load(evsel, target); 871 return 0; 872 } 873 874 int bpf_counter__enable(struct evsel *evsel) 875 { 876 if (bpf_counter_skip(evsel)) 877 return 0; 878 return evsel->bpf_counter_ops->enable(evsel); 879 } 880 881 int bpf_counter__disable(struct evsel *evsel) 882 { 883 if (bpf_counter_skip(evsel)) 884 return 0; 885 return evsel->bpf_counter_ops->disable(evsel); 886 } 887 888 int bpf_counter__read(struct evsel *evsel) 889 { 890 if (bpf_counter_skip(evsel)) 891 return -EAGAIN; 892 return evsel->bpf_counter_ops->read(evsel); 893 } 894 895 void bpf_counter__destroy(struct evsel *evsel) 896 { 897 if (bpf_counter_skip(evsel)) 898 return; 899 evsel->bpf_counter_ops->destroy(evsel); 900 evsel->bpf_counter_ops = NULL; 901 evsel->bpf_skel = NULL; 902 } 903