1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * builtin-stat.c 4 * 5 * Builtin stat command: Give a precise performance counters summary 6 * overview about any workload, CPU or specific PID. 7 * 8 * Sample output: 9 10 $ perf stat ./hackbench 10 11 12 Time: 0.118 13 14 Performance counter stats for './hackbench 10': 15 16 1708.761321 task-clock # 11.037 CPUs utilized 17 41,190 context-switches # 0.024 M/sec 18 6,735 CPU-migrations # 0.004 M/sec 19 17,318 page-faults # 0.010 M/sec 20 5,205,202,243 cycles # 3.046 GHz 21 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle 22 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle 23 2,603,501,247 instructions # 0.50 insns per cycle 24 # 1.48 stalled cycles per insn 25 484,357,498 branches # 283.455 M/sec 26 6,388,934 branch-misses # 1.32% of all branches 27 28 0.154822978 seconds time elapsed 29 30 * 31 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com> 32 * 33 * Improvements and fixes by: 34 * 35 * Arjan van de Ven <arjan@linux.intel.com> 36 * Yanmin Zhang <yanmin.zhang@intel.com> 37 * Wu Fengguang <fengguang.wu@intel.com> 38 * Mike Galbraith <efault@gmx.de> 39 * Paul Mackerras <paulus@samba.org> 40 * Jaswinder Singh Rajput <jaswinder@kernel.org> 41 */ 42 43 #include "builtin.h" 44 #include "perf.h" 45 #include "util/cgroup.h" 46 #include <subcmd/parse-options.h> 47 #include "util/parse-events.h" 48 #include "util/pmu.h" 49 #include "util/event.h" 50 #include "util/evlist.h" 51 #include "util/evlist-hybrid.h" 52 #include "util/evsel.h" 53 #include "util/debug.h" 54 #include "util/color.h" 55 #include "util/stat.h" 56 #include "util/header.h" 57 #include "util/cpumap.h" 58 #include "util/thread_map.h" 59 #include "util/counts.h" 60 #include "util/topdown.h" 61 #include "util/session.h" 62 #include "util/tool.h" 63 #include "util/string2.h" 64 #include "util/metricgroup.h" 65 #include "util/synthetic-events.h" 66 #include "util/target.h" 67 #include "util/time-utils.h" 68 #include "util/top.h" 69 #include "util/affinity.h" 70 #include "util/pfm.h" 71 #include "util/bpf_counter.h" 72 #include "util/iostat.h" 73 #include "util/pmu-hybrid.h" 74 #include "asm/bug.h" 75 76 #include <linux/time64.h> 77 #include <linux/zalloc.h> 78 #include <api/fs/fs.h> 79 #include <errno.h> 80 #include <signal.h> 81 #include <stdlib.h> 82 #include <sys/prctl.h> 83 #include <inttypes.h> 84 #include <locale.h> 85 #include <math.h> 86 #include <sys/types.h> 87 #include <sys/stat.h> 88 #include <sys/wait.h> 89 #include <unistd.h> 90 #include <sys/time.h> 91 #include <sys/resource.h> 92 #include <linux/err.h> 93 94 #include <linux/ctype.h> 95 #include <perf/evlist.h> 96 97 #define DEFAULT_SEPARATOR " " 98 #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi" 99 100 static void print_counters(struct timespec *ts, int argc, const char **argv); 101 102 /* Default events used for perf stat -T */ 103 static const char *transaction_attrs = { 104 "task-clock," 105 "{" 106 "instructions," 107 "cycles," 108 "cpu/cycles-t/," 109 "cpu/tx-start/," 110 "cpu/el-start/," 111 "cpu/cycles-ct/" 112 "}" 113 }; 114 115 /* More limited version when the CPU does not have all events. */ 116 static const char * transaction_limited_attrs = { 117 "task-clock," 118 "{" 119 "instructions," 120 "cycles," 121 "cpu/cycles-t/," 122 "cpu/tx-start/" 123 "}" 124 }; 125 126 static const char * topdown_attrs[] = { 127 "topdown-total-slots", 128 "topdown-slots-retired", 129 "topdown-recovery-bubbles", 130 "topdown-fetch-bubbles", 131 "topdown-slots-issued", 132 NULL, 133 }; 134 135 static const char *topdown_metric_attrs[] = { 136 "slots", 137 "topdown-retiring", 138 "topdown-bad-spec", 139 "topdown-fe-bound", 140 "topdown-be-bound", 141 NULL, 142 }; 143 144 static const char *topdown_metric_L2_attrs[] = { 145 "slots", 146 "topdown-retiring", 147 "topdown-bad-spec", 148 "topdown-fe-bound", 149 "topdown-be-bound", 150 "topdown-heavy-ops", 151 "topdown-br-mispredict", 152 "topdown-fetch-lat", 153 "topdown-mem-bound", 154 NULL, 155 }; 156 157 #define TOPDOWN_MAX_LEVEL 2 158 159 static const char *smi_cost_attrs = { 160 "{" 161 "msr/aperf/," 162 "msr/smi/," 163 "cycles" 164 "}" 165 }; 166 167 static struct evlist *evsel_list; 168 static bool all_counters_use_bpf = true; 169 170 static struct target target = { 171 .uid = UINT_MAX, 172 }; 173 174 #define METRIC_ONLY_LEN 20 175 176 static volatile sig_atomic_t child_pid = -1; 177 static int detailed_run = 0; 178 static bool transaction_run; 179 static bool topdown_run = false; 180 static bool smi_cost = false; 181 static bool smi_reset = false; 182 static int big_num_opt = -1; 183 static bool group = false; 184 static const char *pre_cmd = NULL; 185 static const char *post_cmd = NULL; 186 static bool sync_run = false; 187 static bool forever = false; 188 static bool force_metric_only = false; 189 static struct timespec ref_time; 190 static bool append_file; 191 static bool interval_count; 192 static const char *output_name; 193 static int output_fd; 194 static char *metrics; 195 196 struct perf_stat { 197 bool record; 198 struct perf_data data; 199 struct perf_session *session; 200 u64 bytes_written; 201 struct perf_tool tool; 202 bool maps_allocated; 203 struct perf_cpu_map *cpus; 204 struct perf_thread_map *threads; 205 enum aggr_mode aggr_mode; 206 }; 207 208 static struct perf_stat perf_stat; 209 #define STAT_RECORD perf_stat.record 210 211 static volatile sig_atomic_t done = 0; 212 213 static struct perf_stat_config stat_config = { 214 .aggr_mode = AGGR_GLOBAL, 215 .scale = true, 216 .unit_width = 4, /* strlen("unit") */ 217 .run_count = 1, 218 .metric_only_len = METRIC_ONLY_LEN, 219 .walltime_nsecs_stats = &walltime_nsecs_stats, 220 .ru_stats = &ru_stats, 221 .big_num = true, 222 .ctl_fd = -1, 223 .ctl_fd_ack = -1, 224 .iostat_run = false, 225 }; 226 227 static bool cpus_map_matched(struct evsel *a, struct evsel *b) 228 { 229 if (!a->core.cpus && !b->core.cpus) 230 return true; 231 232 if (!a->core.cpus || !b->core.cpus) 233 return false; 234 235 if (perf_cpu_map__nr(a->core.cpus) != perf_cpu_map__nr(b->core.cpus)) 236 return false; 237 238 for (int i = 0; i < perf_cpu_map__nr(a->core.cpus); i++) { 239 if (perf_cpu_map__cpu(a->core.cpus, i).cpu != 240 perf_cpu_map__cpu(b->core.cpus, i).cpu) 241 return false; 242 } 243 244 return true; 245 } 246 247 static void evlist__check_cpu_maps(struct evlist *evlist) 248 { 249 struct evsel *evsel, *pos, *leader; 250 char buf[1024]; 251 252 if (evlist__has_hybrid(evlist)) 253 evlist__warn_hybrid_group(evlist); 254 255 evlist__for_each_entry(evlist, evsel) { 256 leader = evsel__leader(evsel); 257 258 /* Check that leader matches cpus with each member. */ 259 if (leader == evsel) 260 continue; 261 if (cpus_map_matched(leader, evsel)) 262 continue; 263 264 /* If there's mismatch disable the group and warn user. */ 265 WARN_ONCE(1, "WARNING: grouped events cpus do not match, disabling group:\n"); 266 evsel__group_desc(leader, buf, sizeof(buf)); 267 pr_warning(" %s\n", buf); 268 269 if (verbose) { 270 cpu_map__snprint(leader->core.cpus, buf, sizeof(buf)); 271 pr_warning(" %s: %s\n", leader->name, buf); 272 cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf)); 273 pr_warning(" %s: %s\n", evsel->name, buf); 274 } 275 276 for_each_group_evsel(pos, leader) 277 evsel__remove_from_group(pos, leader); 278 } 279 } 280 281 static inline void diff_timespec(struct timespec *r, struct timespec *a, 282 struct timespec *b) 283 { 284 r->tv_sec = a->tv_sec - b->tv_sec; 285 if (a->tv_nsec < b->tv_nsec) { 286 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec; 287 r->tv_sec--; 288 } else { 289 r->tv_nsec = a->tv_nsec - b->tv_nsec ; 290 } 291 } 292 293 static void perf_stat__reset_stats(void) 294 { 295 evlist__reset_stats(evsel_list); 296 perf_stat__reset_shadow_stats(); 297 } 298 299 static int process_synthesized_event(struct perf_tool *tool __maybe_unused, 300 union perf_event *event, 301 struct perf_sample *sample __maybe_unused, 302 struct machine *machine __maybe_unused) 303 { 304 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) { 305 pr_err("failed to write perf data, error: %m\n"); 306 return -1; 307 } 308 309 perf_stat.bytes_written += event->header.size; 310 return 0; 311 } 312 313 static int write_stat_round_event(u64 tm, u64 type) 314 { 315 return perf_event__synthesize_stat_round(NULL, tm, type, 316 process_synthesized_event, 317 NULL); 318 } 319 320 #define WRITE_STAT_ROUND_EVENT(time, interval) \ 321 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval) 322 323 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 324 325 static int evsel__write_stat_event(struct evsel *counter, int cpu_map_idx, u32 thread, 326 struct perf_counts_values *count) 327 { 328 struct perf_sample_id *sid = SID(counter, cpu_map_idx, thread); 329 struct perf_cpu cpu = perf_cpu_map__cpu(evsel__cpus(counter), cpu_map_idx); 330 331 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count, 332 process_synthesized_event, NULL); 333 } 334 335 static int read_single_counter(struct evsel *counter, int cpu_map_idx, 336 int thread, struct timespec *rs) 337 { 338 switch(counter->tool_event) { 339 case PERF_TOOL_DURATION_TIME: { 340 u64 val = rs->tv_nsec + rs->tv_sec*1000000000ULL; 341 struct perf_counts_values *count = 342 perf_counts(counter->counts, cpu_map_idx, thread); 343 count->ena = count->run = val; 344 count->val = val; 345 return 0; 346 } 347 case PERF_TOOL_USER_TIME: 348 case PERF_TOOL_SYSTEM_TIME: { 349 u64 val; 350 struct perf_counts_values *count = 351 perf_counts(counter->counts, cpu_map_idx, thread); 352 if (counter->tool_event == PERF_TOOL_USER_TIME) 353 val = ru_stats.ru_utime_usec_stat.mean; 354 else 355 val = ru_stats.ru_stime_usec_stat.mean; 356 count->ena = count->run = val; 357 count->val = val; 358 return 0; 359 } 360 default: 361 case PERF_TOOL_NONE: 362 return evsel__read_counter(counter, cpu_map_idx, thread); 363 case PERF_TOOL_MAX: 364 /* This should never be reached */ 365 return 0; 366 } 367 } 368 369 /* 370 * Read out the results of a single counter: 371 * do not aggregate counts across CPUs in system-wide mode 372 */ 373 static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu_map_idx) 374 { 375 int nthreads = perf_thread_map__nr(evsel_list->core.threads); 376 int thread; 377 378 if (!counter->supported) 379 return -ENOENT; 380 381 for (thread = 0; thread < nthreads; thread++) { 382 struct perf_counts_values *count; 383 384 count = perf_counts(counter->counts, cpu_map_idx, thread); 385 386 /* 387 * The leader's group read loads data into its group members 388 * (via evsel__read_counter()) and sets their count->loaded. 389 */ 390 if (!perf_counts__is_loaded(counter->counts, cpu_map_idx, thread) && 391 read_single_counter(counter, cpu_map_idx, thread, rs)) { 392 counter->counts->scaled = -1; 393 perf_counts(counter->counts, cpu_map_idx, thread)->ena = 0; 394 perf_counts(counter->counts, cpu_map_idx, thread)->run = 0; 395 return -1; 396 } 397 398 perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, false); 399 400 if (STAT_RECORD) { 401 if (evsel__write_stat_event(counter, cpu_map_idx, thread, count)) { 402 pr_err("failed to write stat event\n"); 403 return -1; 404 } 405 } 406 407 if (verbose > 1) { 408 fprintf(stat_config.output, 409 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", 410 evsel__name(counter), 411 perf_cpu_map__cpu(evsel__cpus(counter), 412 cpu_map_idx).cpu, 413 count->val, count->ena, count->run); 414 } 415 } 416 417 return 0; 418 } 419 420 static int read_affinity_counters(struct timespec *rs) 421 { 422 struct evlist_cpu_iterator evlist_cpu_itr; 423 struct affinity saved_affinity, *affinity; 424 425 if (all_counters_use_bpf) 426 return 0; 427 428 if (!target__has_cpu(&target) || target__has_per_thread(&target)) 429 affinity = NULL; 430 else if (affinity__setup(&saved_affinity) < 0) 431 return -1; 432 else 433 affinity = &saved_affinity; 434 435 evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) { 436 struct evsel *counter = evlist_cpu_itr.evsel; 437 438 if (evsel__is_bpf(counter)) 439 continue; 440 441 if (!counter->err) { 442 counter->err = read_counter_cpu(counter, rs, 443 evlist_cpu_itr.cpu_map_idx); 444 } 445 } 446 if (affinity) 447 affinity__cleanup(&saved_affinity); 448 449 return 0; 450 } 451 452 static int read_bpf_map_counters(void) 453 { 454 struct evsel *counter; 455 int err; 456 457 evlist__for_each_entry(evsel_list, counter) { 458 if (!evsel__is_bpf(counter)) 459 continue; 460 461 err = bpf_counter__read(counter); 462 if (err) 463 return err; 464 } 465 return 0; 466 } 467 468 static int read_counters(struct timespec *rs) 469 { 470 if (!stat_config.stop_read_counter) { 471 if (read_bpf_map_counters() || 472 read_affinity_counters(rs)) 473 return -1; 474 } 475 return 0; 476 } 477 478 static void process_counters(void) 479 { 480 struct evsel *counter; 481 482 evlist__for_each_entry(evsel_list, counter) { 483 if (counter->err) 484 pr_debug("failed to read counter %s\n", counter->name); 485 if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter)) 486 pr_warning("failed to process counter %s\n", counter->name); 487 counter->err = 0; 488 } 489 490 perf_stat_merge_counters(&stat_config, evsel_list); 491 perf_stat_process_percore(&stat_config, evsel_list); 492 perf_stat_process_shadow_stats(&stat_config, evsel_list); 493 } 494 495 static void process_interval(void) 496 { 497 struct timespec ts, rs; 498 499 clock_gettime(CLOCK_MONOTONIC, &ts); 500 diff_timespec(&rs, &ts, &ref_time); 501 502 perf_stat__reset_shadow_per_stat(&rt_stat); 503 evlist__reset_aggr_stats(evsel_list); 504 505 if (read_counters(&rs) == 0) 506 process_counters(); 507 508 if (STAT_RECORD) { 509 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL)) 510 pr_err("failed to write stat round event\n"); 511 } 512 513 init_stats(&walltime_nsecs_stats); 514 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL); 515 print_counters(&rs, 0, NULL); 516 } 517 518 static bool handle_interval(unsigned int interval, int *times) 519 { 520 if (interval) { 521 process_interval(); 522 if (interval_count && !(--(*times))) 523 return true; 524 } 525 return false; 526 } 527 528 static int enable_counters(void) 529 { 530 struct evsel *evsel; 531 int err; 532 533 evlist__for_each_entry(evsel_list, evsel) { 534 if (!evsel__is_bpf(evsel)) 535 continue; 536 537 err = bpf_counter__enable(evsel); 538 if (err) 539 return err; 540 } 541 542 if (stat_config.initial_delay < 0) { 543 pr_info(EVLIST_DISABLED_MSG); 544 return 0; 545 } 546 547 if (stat_config.initial_delay > 0) { 548 pr_info(EVLIST_DISABLED_MSG); 549 usleep(stat_config.initial_delay * USEC_PER_MSEC); 550 } 551 552 /* 553 * We need to enable counters only if: 554 * - we don't have tracee (attaching to task or cpu) 555 * - we have initial delay configured 556 */ 557 if (!target__none(&target) || stat_config.initial_delay) { 558 if (!all_counters_use_bpf) 559 evlist__enable(evsel_list); 560 if (stat_config.initial_delay > 0) 561 pr_info(EVLIST_ENABLED_MSG); 562 } 563 return 0; 564 } 565 566 static void disable_counters(void) 567 { 568 struct evsel *counter; 569 570 /* 571 * If we don't have tracee (attaching to task or cpu), counters may 572 * still be running. To get accurate group ratios, we must stop groups 573 * from counting before reading their constituent counters. 574 */ 575 if (!target__none(&target)) { 576 evlist__for_each_entry(evsel_list, counter) 577 bpf_counter__disable(counter); 578 if (!all_counters_use_bpf) 579 evlist__disable(evsel_list); 580 } 581 } 582 583 static volatile sig_atomic_t workload_exec_errno; 584 585 /* 586 * evlist__prepare_workload will send a SIGUSR1 587 * if the fork fails, since we asked by setting its 588 * want_signal to true. 589 */ 590 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, 591 void *ucontext __maybe_unused) 592 { 593 workload_exec_errno = info->si_value.sival_int; 594 } 595 596 static bool evsel__should_store_id(struct evsel *counter) 597 { 598 return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID; 599 } 600 601 static bool is_target_alive(struct target *_target, 602 struct perf_thread_map *threads) 603 { 604 struct stat st; 605 int i; 606 607 if (!target__has_task(_target)) 608 return true; 609 610 for (i = 0; i < threads->nr; i++) { 611 char path[PATH_MAX]; 612 613 scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(), 614 threads->map[i].pid); 615 616 if (!stat(path, &st)) 617 return true; 618 } 619 620 return false; 621 } 622 623 static void process_evlist(struct evlist *evlist, unsigned int interval) 624 { 625 enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED; 626 627 if (evlist__ctlfd_process(evlist, &cmd) > 0) { 628 switch (cmd) { 629 case EVLIST_CTL_CMD_ENABLE: 630 __fallthrough; 631 case EVLIST_CTL_CMD_DISABLE: 632 if (interval) 633 process_interval(); 634 break; 635 case EVLIST_CTL_CMD_SNAPSHOT: 636 case EVLIST_CTL_CMD_ACK: 637 case EVLIST_CTL_CMD_UNSUPPORTED: 638 case EVLIST_CTL_CMD_EVLIST: 639 case EVLIST_CTL_CMD_STOP: 640 case EVLIST_CTL_CMD_PING: 641 default: 642 break; 643 } 644 } 645 } 646 647 static void compute_tts(struct timespec *time_start, struct timespec *time_stop, 648 int *time_to_sleep) 649 { 650 int tts = *time_to_sleep; 651 struct timespec time_diff; 652 653 diff_timespec(&time_diff, time_stop, time_start); 654 655 tts -= time_diff.tv_sec * MSEC_PER_SEC + 656 time_diff.tv_nsec / NSEC_PER_MSEC; 657 658 if (tts < 0) 659 tts = 0; 660 661 *time_to_sleep = tts; 662 } 663 664 static int dispatch_events(bool forks, int timeout, int interval, int *times) 665 { 666 int child_exited = 0, status = 0; 667 int time_to_sleep, sleep_time; 668 struct timespec time_start, time_stop; 669 670 if (interval) 671 sleep_time = interval; 672 else if (timeout) 673 sleep_time = timeout; 674 else 675 sleep_time = 1000; 676 677 time_to_sleep = sleep_time; 678 679 while (!done) { 680 if (forks) 681 child_exited = waitpid(child_pid, &status, WNOHANG); 682 else 683 child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0; 684 685 if (child_exited) 686 break; 687 688 clock_gettime(CLOCK_MONOTONIC, &time_start); 689 if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */ 690 if (timeout || handle_interval(interval, times)) 691 break; 692 time_to_sleep = sleep_time; 693 } else { /* fd revent */ 694 process_evlist(evsel_list, interval); 695 clock_gettime(CLOCK_MONOTONIC, &time_stop); 696 compute_tts(&time_start, &time_stop, &time_to_sleep); 697 } 698 } 699 700 return status; 701 } 702 703 enum counter_recovery { 704 COUNTER_SKIP, 705 COUNTER_RETRY, 706 COUNTER_FATAL, 707 }; 708 709 static enum counter_recovery stat_handle_error(struct evsel *counter) 710 { 711 char msg[BUFSIZ]; 712 /* 713 * PPC returns ENXIO for HW counters until 2.6.37 714 * (behavior changed with commit b0a873e). 715 */ 716 if (errno == EINVAL || errno == ENOSYS || 717 errno == ENOENT || errno == EOPNOTSUPP || 718 errno == ENXIO) { 719 if (verbose > 0) 720 ui__warning("%s event is not supported by the kernel.\n", 721 evsel__name(counter)); 722 counter->supported = false; 723 /* 724 * errored is a sticky flag that means one of the counter's 725 * cpu event had a problem and needs to be reexamined. 726 */ 727 counter->errored = true; 728 729 if ((evsel__leader(counter) != counter) || 730 !(counter->core.leader->nr_members > 1)) 731 return COUNTER_SKIP; 732 } else if (evsel__fallback(counter, errno, msg, sizeof(msg))) { 733 if (verbose > 0) 734 ui__warning("%s\n", msg); 735 return COUNTER_RETRY; 736 } else if (target__has_per_thread(&target) && 737 evsel_list->core.threads && 738 evsel_list->core.threads->err_thread != -1) { 739 /* 740 * For global --per-thread case, skip current 741 * error thread. 742 */ 743 if (!thread_map__remove(evsel_list->core.threads, 744 evsel_list->core.threads->err_thread)) { 745 evsel_list->core.threads->err_thread = -1; 746 return COUNTER_RETRY; 747 } 748 } 749 750 evsel__open_strerror(counter, &target, errno, msg, sizeof(msg)); 751 ui__error("%s\n", msg); 752 753 if (child_pid != -1) 754 kill(child_pid, SIGTERM); 755 return COUNTER_FATAL; 756 } 757 758 static int __run_perf_stat(int argc, const char **argv, int run_idx) 759 { 760 int interval = stat_config.interval; 761 int times = stat_config.times; 762 int timeout = stat_config.timeout; 763 char msg[BUFSIZ]; 764 unsigned long long t0, t1; 765 struct evsel *counter; 766 size_t l; 767 int status = 0; 768 const bool forks = (argc > 0); 769 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false; 770 struct evlist_cpu_iterator evlist_cpu_itr; 771 struct affinity saved_affinity, *affinity = NULL; 772 int err; 773 bool second_pass = false; 774 775 if (forks) { 776 if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) { 777 perror("failed to prepare workload"); 778 return -1; 779 } 780 child_pid = evsel_list->workload.pid; 781 } 782 783 if (group) 784 evlist__set_leader(evsel_list); 785 786 if (!cpu_map__is_dummy(evsel_list->core.user_requested_cpus)) { 787 if (affinity__setup(&saved_affinity) < 0) 788 return -1; 789 affinity = &saved_affinity; 790 } 791 792 evlist__for_each_entry(evsel_list, counter) { 793 counter->reset_group = false; 794 if (bpf_counter__load(counter, &target)) 795 return -1; 796 if (!evsel__is_bpf(counter)) 797 all_counters_use_bpf = false; 798 } 799 800 evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) { 801 counter = evlist_cpu_itr.evsel; 802 803 /* 804 * bperf calls evsel__open_per_cpu() in bperf__load(), so 805 * no need to call it again here. 806 */ 807 if (target.use_bpf) 808 break; 809 810 if (counter->reset_group || counter->errored) 811 continue; 812 if (evsel__is_bpf(counter)) 813 continue; 814 try_again: 815 if (create_perf_stat_counter(counter, &stat_config, &target, 816 evlist_cpu_itr.cpu_map_idx) < 0) { 817 818 /* 819 * Weak group failed. We cannot just undo this here 820 * because earlier CPUs might be in group mode, and the kernel 821 * doesn't support mixing group and non group reads. Defer 822 * it to later. 823 * Don't close here because we're in the wrong affinity. 824 */ 825 if ((errno == EINVAL || errno == EBADF) && 826 evsel__leader(counter) != counter && 827 counter->weak_group) { 828 evlist__reset_weak_group(evsel_list, counter, false); 829 assert(counter->reset_group); 830 second_pass = true; 831 continue; 832 } 833 834 switch (stat_handle_error(counter)) { 835 case COUNTER_FATAL: 836 return -1; 837 case COUNTER_RETRY: 838 goto try_again; 839 case COUNTER_SKIP: 840 continue; 841 default: 842 break; 843 } 844 845 } 846 counter->supported = true; 847 } 848 849 if (second_pass) { 850 /* 851 * Now redo all the weak group after closing them, 852 * and also close errored counters. 853 */ 854 855 /* First close errored or weak retry */ 856 evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) { 857 counter = evlist_cpu_itr.evsel; 858 859 if (!counter->reset_group && !counter->errored) 860 continue; 861 862 perf_evsel__close_cpu(&counter->core, evlist_cpu_itr.cpu_map_idx); 863 } 864 /* Now reopen weak */ 865 evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) { 866 counter = evlist_cpu_itr.evsel; 867 868 if (!counter->reset_group) 869 continue; 870 try_again_reset: 871 pr_debug2("reopening weak %s\n", evsel__name(counter)); 872 if (create_perf_stat_counter(counter, &stat_config, &target, 873 evlist_cpu_itr.cpu_map_idx) < 0) { 874 875 switch (stat_handle_error(counter)) { 876 case COUNTER_FATAL: 877 return -1; 878 case COUNTER_RETRY: 879 goto try_again_reset; 880 case COUNTER_SKIP: 881 continue; 882 default: 883 break; 884 } 885 } 886 counter->supported = true; 887 } 888 } 889 affinity__cleanup(affinity); 890 891 evlist__for_each_entry(evsel_list, counter) { 892 if (!counter->supported) { 893 perf_evsel__free_fd(&counter->core); 894 continue; 895 } 896 897 l = strlen(counter->unit); 898 if (l > stat_config.unit_width) 899 stat_config.unit_width = l; 900 901 if (evsel__should_store_id(counter) && 902 evsel__store_ids(counter, evsel_list)) 903 return -1; 904 } 905 906 if (evlist__apply_filters(evsel_list, &counter)) { 907 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 908 counter->filter, evsel__name(counter), errno, 909 str_error_r(errno, msg, sizeof(msg))); 910 return -1; 911 } 912 913 if (STAT_RECORD) { 914 int fd = perf_data__fd(&perf_stat.data); 915 916 if (is_pipe) { 917 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data)); 918 } else { 919 err = perf_session__write_header(perf_stat.session, evsel_list, 920 fd, false); 921 } 922 923 if (err < 0) 924 return err; 925 926 err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list, 927 process_synthesized_event, is_pipe); 928 if (err < 0) 929 return err; 930 } 931 932 err = enable_counters(); 933 if (err) 934 return -1; 935 936 /* Exec the command, if any */ 937 if (forks) 938 evlist__start_workload(evsel_list); 939 940 t0 = rdclock(); 941 clock_gettime(CLOCK_MONOTONIC, &ref_time); 942 943 if (forks) { 944 if (interval || timeout || evlist__ctlfd_initialized(evsel_list)) 945 status = dispatch_events(forks, timeout, interval, ×); 946 if (child_pid != -1) { 947 if (timeout) 948 kill(child_pid, SIGTERM); 949 wait4(child_pid, &status, 0, &stat_config.ru_data); 950 } 951 952 if (workload_exec_errno) { 953 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 954 pr_err("Workload failed: %s\n", emsg); 955 return -1; 956 } 957 958 if (WIFSIGNALED(status)) 959 psignal(WTERMSIG(status), argv[0]); 960 } else { 961 status = dispatch_events(forks, timeout, interval, ×); 962 } 963 964 disable_counters(); 965 966 t1 = rdclock(); 967 968 if (stat_config.walltime_run_table) 969 stat_config.walltime_run[run_idx] = t1 - t0; 970 971 if (interval && stat_config.summary) { 972 stat_config.interval = 0; 973 stat_config.stop_read_counter = true; 974 init_stats(&walltime_nsecs_stats); 975 update_stats(&walltime_nsecs_stats, t1 - t0); 976 977 evlist__copy_prev_raw_counts(evsel_list); 978 evlist__reset_prev_raw_counts(evsel_list); 979 evlist__reset_aggr_stats(evsel_list); 980 perf_stat__reset_shadow_per_stat(&rt_stat); 981 } else { 982 update_stats(&walltime_nsecs_stats, t1 - t0); 983 update_rusage_stats(&ru_stats, &stat_config.ru_data); 984 } 985 986 /* 987 * Closing a group leader splits the group, and as we only disable 988 * group leaders, results in remaining events becoming enabled. To 989 * avoid arbitrary skew, we must read all counters before closing any 990 * group leaders. 991 */ 992 if (read_counters(&(struct timespec) { .tv_nsec = t1-t0 }) == 0) 993 process_counters(); 994 995 /* 996 * We need to keep evsel_list alive, because it's processed 997 * later the evsel_list will be closed after. 998 */ 999 if (!STAT_RECORD) 1000 evlist__close(evsel_list); 1001 1002 return WEXITSTATUS(status); 1003 } 1004 1005 static int run_perf_stat(int argc, const char **argv, int run_idx) 1006 { 1007 int ret; 1008 1009 if (pre_cmd) { 1010 ret = system(pre_cmd); 1011 if (ret) 1012 return ret; 1013 } 1014 1015 if (sync_run) 1016 sync(); 1017 1018 ret = __run_perf_stat(argc, argv, run_idx); 1019 if (ret) 1020 return ret; 1021 1022 if (post_cmd) { 1023 ret = system(post_cmd); 1024 if (ret) 1025 return ret; 1026 } 1027 1028 return ret; 1029 } 1030 1031 static void print_counters(struct timespec *ts, int argc, const char **argv) 1032 { 1033 /* Do not print anything if we record to the pipe. */ 1034 if (STAT_RECORD && perf_stat.data.is_pipe) 1035 return; 1036 if (quiet) 1037 return; 1038 1039 evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv); 1040 } 1041 1042 static volatile sig_atomic_t signr = -1; 1043 1044 static void skip_signal(int signo) 1045 { 1046 if ((child_pid == -1) || stat_config.interval) 1047 done = 1; 1048 1049 signr = signo; 1050 /* 1051 * render child_pid harmless 1052 * won't send SIGTERM to a random 1053 * process in case of race condition 1054 * and fast PID recycling 1055 */ 1056 child_pid = -1; 1057 } 1058 1059 static void sig_atexit(void) 1060 { 1061 sigset_t set, oset; 1062 1063 /* 1064 * avoid race condition with SIGCHLD handler 1065 * in skip_signal() which is modifying child_pid 1066 * goal is to avoid send SIGTERM to a random 1067 * process 1068 */ 1069 sigemptyset(&set); 1070 sigaddset(&set, SIGCHLD); 1071 sigprocmask(SIG_BLOCK, &set, &oset); 1072 1073 if (child_pid != -1) 1074 kill(child_pid, SIGTERM); 1075 1076 sigprocmask(SIG_SETMASK, &oset, NULL); 1077 1078 if (signr == -1) 1079 return; 1080 1081 signal(signr, SIG_DFL); 1082 kill(getpid(), signr); 1083 } 1084 1085 void perf_stat__set_big_num(int set) 1086 { 1087 stat_config.big_num = (set != 0); 1088 } 1089 1090 void perf_stat__set_no_csv_summary(int set) 1091 { 1092 stat_config.no_csv_summary = (set != 0); 1093 } 1094 1095 static int stat__set_big_num(const struct option *opt __maybe_unused, 1096 const char *s __maybe_unused, int unset) 1097 { 1098 big_num_opt = unset ? 0 : 1; 1099 perf_stat__set_big_num(!unset); 1100 return 0; 1101 } 1102 1103 static int enable_metric_only(const struct option *opt __maybe_unused, 1104 const char *s __maybe_unused, int unset) 1105 { 1106 force_metric_only = true; 1107 stat_config.metric_only = !unset; 1108 return 0; 1109 } 1110 1111 static int append_metric_groups(const struct option *opt __maybe_unused, 1112 const char *str, 1113 int unset __maybe_unused) 1114 { 1115 if (metrics) { 1116 char *tmp; 1117 1118 if (asprintf(&tmp, "%s,%s", metrics, str) < 0) 1119 return -ENOMEM; 1120 free(metrics); 1121 metrics = tmp; 1122 } else { 1123 metrics = strdup(str); 1124 if (!metrics) 1125 return -ENOMEM; 1126 } 1127 return 0; 1128 } 1129 1130 static int parse_control_option(const struct option *opt, 1131 const char *str, 1132 int unset __maybe_unused) 1133 { 1134 struct perf_stat_config *config = opt->value; 1135 1136 return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close); 1137 } 1138 1139 static int parse_stat_cgroups(const struct option *opt, 1140 const char *str, int unset) 1141 { 1142 if (stat_config.cgroup_list) { 1143 pr_err("--cgroup and --for-each-cgroup cannot be used together\n"); 1144 return -1; 1145 } 1146 1147 return parse_cgroups(opt, str, unset); 1148 } 1149 1150 static int parse_hybrid_type(const struct option *opt, 1151 const char *str, 1152 int unset __maybe_unused) 1153 { 1154 struct evlist *evlist = *(struct evlist **)opt->value; 1155 1156 if (!list_empty(&evlist->core.entries)) { 1157 fprintf(stderr, "Must define cputype before events/metrics\n"); 1158 return -1; 1159 } 1160 1161 evlist->hybrid_pmu_name = perf_pmu__hybrid_type_to_pmu(str); 1162 if (!evlist->hybrid_pmu_name) { 1163 fprintf(stderr, "--cputype %s is not supported!\n", str); 1164 return -1; 1165 } 1166 1167 return 0; 1168 } 1169 1170 static struct option stat_options[] = { 1171 OPT_BOOLEAN('T', "transaction", &transaction_run, 1172 "hardware transaction statistics"), 1173 OPT_CALLBACK('e', "event", &evsel_list, "event", 1174 "event selector. use 'perf list' to list available events", 1175 parse_events_option), 1176 OPT_CALLBACK(0, "filter", &evsel_list, "filter", 1177 "event filter", parse_filter), 1178 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit, 1179 "child tasks do not inherit counters"), 1180 OPT_STRING('p', "pid", &target.pid, "pid", 1181 "stat events on existing process id"), 1182 OPT_STRING('t', "tid", &target.tid, "tid", 1183 "stat events on existing thread id"), 1184 #ifdef HAVE_BPF_SKEL 1185 OPT_STRING('b', "bpf-prog", &target.bpf_str, "bpf-prog-id", 1186 "stat events on existing bpf program id"), 1187 OPT_BOOLEAN(0, "bpf-counters", &target.use_bpf, 1188 "use bpf program to count events"), 1189 OPT_STRING(0, "bpf-attr-map", &target.attr_map, "attr-map-path", 1190 "path to perf_event_attr map"), 1191 #endif 1192 OPT_BOOLEAN('a', "all-cpus", &target.system_wide, 1193 "system-wide collection from all CPUs"), 1194 OPT_BOOLEAN('g', "group", &group, 1195 "put the counters into a counter group"), 1196 OPT_BOOLEAN(0, "scale", &stat_config.scale, 1197 "Use --no-scale to disable counter scaling for multiplexing"), 1198 OPT_INCR('v', "verbose", &verbose, 1199 "be more verbose (show counter open errors, etc)"), 1200 OPT_INTEGER('r', "repeat", &stat_config.run_count, 1201 "repeat command and print average + stddev (max: 100, forever: 0)"), 1202 OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table, 1203 "display details about each run (only with -r option)"), 1204 OPT_BOOLEAN('n', "null", &stat_config.null_run, 1205 "null run - dont start any counters"), 1206 OPT_INCR('d', "detailed", &detailed_run, 1207 "detailed run - start a lot of events"), 1208 OPT_BOOLEAN('S', "sync", &sync_run, 1209 "call sync() before starting a run"), 1210 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, 1211 "print large numbers with thousands\' separators", 1212 stat__set_big_num), 1213 OPT_STRING('C', "cpu", &target.cpu_list, "cpu", 1214 "list of cpus to monitor in system-wide"), 1215 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, 1216 "disable CPU count aggregation", AGGR_NONE), 1217 OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"), 1218 OPT_BOOLEAN(0, "hybrid-merge", &stat_config.hybrid_merge, 1219 "Merge identical named hybrid events"), 1220 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator", 1221 "print counts with custom separator"), 1222 OPT_BOOLEAN('j', "json-output", &stat_config.json_output, 1223 "print counts in JSON format"), 1224 OPT_CALLBACK('G', "cgroup", &evsel_list, "name", 1225 "monitor event in cgroup name only", parse_stat_cgroups), 1226 OPT_STRING(0, "for-each-cgroup", &stat_config.cgroup_list, "name", 1227 "expand events for each cgroup"), 1228 OPT_STRING('o', "output", &output_name, "file", "output file name"), 1229 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), 1230 OPT_INTEGER(0, "log-fd", &output_fd, 1231 "log output to fd, instead of stderr"), 1232 OPT_STRING(0, "pre", &pre_cmd, "command", 1233 "command to run prior to the measured command"), 1234 OPT_STRING(0, "post", &post_cmd, "command", 1235 "command to run after to the measured command"), 1236 OPT_UINTEGER('I', "interval-print", &stat_config.interval, 1237 "print counts at regular interval in ms " 1238 "(overhead is possible for values <= 100ms)"), 1239 OPT_INTEGER(0, "interval-count", &stat_config.times, 1240 "print counts for fixed number of times"), 1241 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear, 1242 "clear screen in between new interval"), 1243 OPT_UINTEGER(0, "timeout", &stat_config.timeout, 1244 "stop workload and print counts after a timeout period in ms (>= 10ms)"), 1245 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, 1246 "aggregate counts per processor socket", AGGR_SOCKET), 1247 OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode, 1248 "aggregate counts per processor die", AGGR_DIE), 1249 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, 1250 "aggregate counts per physical processor core", AGGR_CORE), 1251 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, 1252 "aggregate counts per thread", AGGR_THREAD), 1253 OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode, 1254 "aggregate counts per numa node", AGGR_NODE), 1255 OPT_INTEGER('D', "delay", &stat_config.initial_delay, 1256 "ms to wait before starting measurement after program start (-1: start with events disabled)"), 1257 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL, 1258 "Only print computed metrics. No raw values", enable_metric_only), 1259 OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group, 1260 "don't group metric events, impacts multiplexing"), 1261 OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge, 1262 "don't try to share events between metrics in a group"), 1263 OPT_BOOLEAN(0, "topdown", &topdown_run, 1264 "measure top-down statistics"), 1265 OPT_UINTEGER(0, "td-level", &stat_config.topdown_level, 1266 "Set the metrics level for the top-down statistics (0: max level)"), 1267 OPT_BOOLEAN(0, "smi-cost", &smi_cost, 1268 "measure SMI cost"), 1269 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list", 1270 "monitor specified metrics or metric groups (separated by ,)", 1271 append_metric_groups), 1272 OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel, 1273 "Configure all used events to run in kernel space.", 1274 PARSE_OPT_EXCLUSIVE), 1275 OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user, 1276 "Configure all used events to run in user space.", 1277 PARSE_OPT_EXCLUSIVE), 1278 OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread, 1279 "Use with 'percore' event qualifier to show the event " 1280 "counts of one hardware thread by sum up total hardware " 1281 "threads of same physical core"), 1282 OPT_BOOLEAN(0, "summary", &stat_config.summary, 1283 "print summary for interval mode"), 1284 OPT_BOOLEAN(0, "no-csv-summary", &stat_config.no_csv_summary, 1285 "don't print 'summary' for CSV summary output"), 1286 OPT_BOOLEAN(0, "quiet", &quiet, 1287 "don't print any output, messages or warnings (useful with record)"), 1288 OPT_CALLBACK(0, "cputype", &evsel_list, "hybrid cpu type", 1289 "Only enable events on applying cpu with this type " 1290 "for hybrid platform (e.g. core or atom)", 1291 parse_hybrid_type), 1292 #ifdef HAVE_LIBPFM 1293 OPT_CALLBACK(0, "pfm-events", &evsel_list, "event", 1294 "libpfm4 event selector. use 'perf list' to list available events", 1295 parse_libpfm_events_option), 1296 #endif 1297 OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]", 1298 "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n" 1299 "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n" 1300 "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.", 1301 parse_control_option), 1302 OPT_CALLBACK_OPTARG(0, "iostat", &evsel_list, &stat_config, "default", 1303 "measure I/O performance metrics provided by arch/platform", 1304 iostat_parse), 1305 OPT_END() 1306 }; 1307 1308 static const char *const aggr_mode__string[] = { 1309 [AGGR_CORE] = "core", 1310 [AGGR_DIE] = "die", 1311 [AGGR_GLOBAL] = "global", 1312 [AGGR_NODE] = "node", 1313 [AGGR_NONE] = "none", 1314 [AGGR_SOCKET] = "socket", 1315 [AGGR_THREAD] = "thread", 1316 [AGGR_UNSET] = "unset", 1317 }; 1318 1319 static struct aggr_cpu_id perf_stat__get_socket(struct perf_stat_config *config __maybe_unused, 1320 struct perf_cpu cpu) 1321 { 1322 return aggr_cpu_id__socket(cpu, /*data=*/NULL); 1323 } 1324 1325 static struct aggr_cpu_id perf_stat__get_die(struct perf_stat_config *config __maybe_unused, 1326 struct perf_cpu cpu) 1327 { 1328 return aggr_cpu_id__die(cpu, /*data=*/NULL); 1329 } 1330 1331 static struct aggr_cpu_id perf_stat__get_core(struct perf_stat_config *config __maybe_unused, 1332 struct perf_cpu cpu) 1333 { 1334 return aggr_cpu_id__core(cpu, /*data=*/NULL); 1335 } 1336 1337 static struct aggr_cpu_id perf_stat__get_node(struct perf_stat_config *config __maybe_unused, 1338 struct perf_cpu cpu) 1339 { 1340 return aggr_cpu_id__node(cpu, /*data=*/NULL); 1341 } 1342 1343 static struct aggr_cpu_id perf_stat__get_global(struct perf_stat_config *config __maybe_unused, 1344 struct perf_cpu cpu) 1345 { 1346 return aggr_cpu_id__global(cpu, /*data=*/NULL); 1347 } 1348 1349 static struct aggr_cpu_id perf_stat__get_cpu(struct perf_stat_config *config __maybe_unused, 1350 struct perf_cpu cpu) 1351 { 1352 return aggr_cpu_id__cpu(cpu, /*data=*/NULL); 1353 } 1354 1355 static struct aggr_cpu_id perf_stat__get_aggr(struct perf_stat_config *config, 1356 aggr_get_id_t get_id, struct perf_cpu cpu) 1357 { 1358 struct aggr_cpu_id id; 1359 1360 /* per-process mode - should use global aggr mode */ 1361 if (cpu.cpu == -1) 1362 return get_id(config, cpu); 1363 1364 if (aggr_cpu_id__is_empty(&config->cpus_aggr_map->map[cpu.cpu])) 1365 config->cpus_aggr_map->map[cpu.cpu] = get_id(config, cpu); 1366 1367 id = config->cpus_aggr_map->map[cpu.cpu]; 1368 return id; 1369 } 1370 1371 static struct aggr_cpu_id perf_stat__get_socket_cached(struct perf_stat_config *config, 1372 struct perf_cpu cpu) 1373 { 1374 return perf_stat__get_aggr(config, perf_stat__get_socket, cpu); 1375 } 1376 1377 static struct aggr_cpu_id perf_stat__get_die_cached(struct perf_stat_config *config, 1378 struct perf_cpu cpu) 1379 { 1380 return perf_stat__get_aggr(config, perf_stat__get_die, cpu); 1381 } 1382 1383 static struct aggr_cpu_id perf_stat__get_core_cached(struct perf_stat_config *config, 1384 struct perf_cpu cpu) 1385 { 1386 return perf_stat__get_aggr(config, perf_stat__get_core, cpu); 1387 } 1388 1389 static struct aggr_cpu_id perf_stat__get_node_cached(struct perf_stat_config *config, 1390 struct perf_cpu cpu) 1391 { 1392 return perf_stat__get_aggr(config, perf_stat__get_node, cpu); 1393 } 1394 1395 static struct aggr_cpu_id perf_stat__get_global_cached(struct perf_stat_config *config, 1396 struct perf_cpu cpu) 1397 { 1398 return perf_stat__get_aggr(config, perf_stat__get_global, cpu); 1399 } 1400 1401 static struct aggr_cpu_id perf_stat__get_cpu_cached(struct perf_stat_config *config, 1402 struct perf_cpu cpu) 1403 { 1404 return perf_stat__get_aggr(config, perf_stat__get_cpu, cpu); 1405 } 1406 1407 static aggr_cpu_id_get_t aggr_mode__get_aggr(enum aggr_mode aggr_mode) 1408 { 1409 switch (aggr_mode) { 1410 case AGGR_SOCKET: 1411 return aggr_cpu_id__socket; 1412 case AGGR_DIE: 1413 return aggr_cpu_id__die; 1414 case AGGR_CORE: 1415 return aggr_cpu_id__core; 1416 case AGGR_NODE: 1417 return aggr_cpu_id__node; 1418 case AGGR_NONE: 1419 return aggr_cpu_id__cpu; 1420 case AGGR_GLOBAL: 1421 return aggr_cpu_id__global; 1422 case AGGR_THREAD: 1423 case AGGR_UNSET: 1424 case AGGR_MAX: 1425 default: 1426 return NULL; 1427 } 1428 } 1429 1430 static aggr_get_id_t aggr_mode__get_id(enum aggr_mode aggr_mode) 1431 { 1432 switch (aggr_mode) { 1433 case AGGR_SOCKET: 1434 return perf_stat__get_socket_cached; 1435 case AGGR_DIE: 1436 return perf_stat__get_die_cached; 1437 case AGGR_CORE: 1438 return perf_stat__get_core_cached; 1439 case AGGR_NODE: 1440 return perf_stat__get_node_cached; 1441 case AGGR_NONE: 1442 return perf_stat__get_cpu_cached; 1443 case AGGR_GLOBAL: 1444 return perf_stat__get_global_cached; 1445 case AGGR_THREAD: 1446 case AGGR_UNSET: 1447 case AGGR_MAX: 1448 default: 1449 return NULL; 1450 } 1451 } 1452 1453 static int perf_stat_init_aggr_mode(void) 1454 { 1455 int nr; 1456 aggr_cpu_id_get_t get_id = aggr_mode__get_aggr(stat_config.aggr_mode); 1457 1458 if (get_id) { 1459 bool needs_sort = stat_config.aggr_mode != AGGR_NONE; 1460 stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus, 1461 get_id, /*data=*/NULL, needs_sort); 1462 if (!stat_config.aggr_map) { 1463 pr_err("cannot build %s map", aggr_mode__string[stat_config.aggr_mode]); 1464 return -1; 1465 } 1466 stat_config.aggr_get_id = aggr_mode__get_id(stat_config.aggr_mode); 1467 } 1468 1469 if (stat_config.aggr_mode == AGGR_THREAD) { 1470 nr = perf_thread_map__nr(evsel_list->core.threads); 1471 stat_config.aggr_map = cpu_aggr_map__empty_new(nr); 1472 if (stat_config.aggr_map == NULL) 1473 return -ENOMEM; 1474 1475 for (int s = 0; s < nr; s++) { 1476 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1477 1478 id.thread_idx = s; 1479 stat_config.aggr_map->map[s] = id; 1480 } 1481 return 0; 1482 } 1483 1484 /* 1485 * The evsel_list->cpus is the base we operate on, 1486 * taking the highest cpu number to be the size of 1487 * the aggregation translate cpumap. 1488 */ 1489 if (evsel_list->core.user_requested_cpus) 1490 nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu; 1491 else 1492 nr = 0; 1493 stat_config.cpus_aggr_map = cpu_aggr_map__empty_new(nr + 1); 1494 return stat_config.cpus_aggr_map ? 0 : -ENOMEM; 1495 } 1496 1497 static void cpu_aggr_map__delete(struct cpu_aggr_map *map) 1498 { 1499 if (map) { 1500 WARN_ONCE(refcount_read(&map->refcnt) != 0, 1501 "cpu_aggr_map refcnt unbalanced\n"); 1502 free(map); 1503 } 1504 } 1505 1506 static void cpu_aggr_map__put(struct cpu_aggr_map *map) 1507 { 1508 if (map && refcount_dec_and_test(&map->refcnt)) 1509 cpu_aggr_map__delete(map); 1510 } 1511 1512 static void perf_stat__exit_aggr_mode(void) 1513 { 1514 cpu_aggr_map__put(stat_config.aggr_map); 1515 cpu_aggr_map__put(stat_config.cpus_aggr_map); 1516 stat_config.aggr_map = NULL; 1517 stat_config.cpus_aggr_map = NULL; 1518 } 1519 1520 static struct aggr_cpu_id perf_env__get_socket_aggr_by_cpu(struct perf_cpu cpu, void *data) 1521 { 1522 struct perf_env *env = data; 1523 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1524 1525 if (cpu.cpu != -1) 1526 id.socket = env->cpu[cpu.cpu].socket_id; 1527 1528 return id; 1529 } 1530 1531 static struct aggr_cpu_id perf_env__get_die_aggr_by_cpu(struct perf_cpu cpu, void *data) 1532 { 1533 struct perf_env *env = data; 1534 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1535 1536 if (cpu.cpu != -1) { 1537 /* 1538 * die_id is relative to socket, so start 1539 * with the socket ID and then add die to 1540 * make a unique ID. 1541 */ 1542 id.socket = env->cpu[cpu.cpu].socket_id; 1543 id.die = env->cpu[cpu.cpu].die_id; 1544 } 1545 1546 return id; 1547 } 1548 1549 static struct aggr_cpu_id perf_env__get_core_aggr_by_cpu(struct perf_cpu cpu, void *data) 1550 { 1551 struct perf_env *env = data; 1552 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1553 1554 if (cpu.cpu != -1) { 1555 /* 1556 * core_id is relative to socket and die, 1557 * we need a global id. So we set 1558 * socket, die id and core id 1559 */ 1560 id.socket = env->cpu[cpu.cpu].socket_id; 1561 id.die = env->cpu[cpu.cpu].die_id; 1562 id.core = env->cpu[cpu.cpu].core_id; 1563 } 1564 1565 return id; 1566 } 1567 1568 static struct aggr_cpu_id perf_env__get_cpu_aggr_by_cpu(struct perf_cpu cpu, void *data) 1569 { 1570 struct perf_env *env = data; 1571 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1572 1573 if (cpu.cpu != -1) { 1574 /* 1575 * core_id is relative to socket and die, 1576 * we need a global id. So we set 1577 * socket, die id and core id 1578 */ 1579 id.socket = env->cpu[cpu.cpu].socket_id; 1580 id.die = env->cpu[cpu.cpu].die_id; 1581 id.core = env->cpu[cpu.cpu].core_id; 1582 id.cpu = cpu; 1583 } 1584 1585 return id; 1586 } 1587 1588 static struct aggr_cpu_id perf_env__get_node_aggr_by_cpu(struct perf_cpu cpu, void *data) 1589 { 1590 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1591 1592 id.node = perf_env__numa_node(data, cpu); 1593 return id; 1594 } 1595 1596 static struct aggr_cpu_id perf_env__get_global_aggr_by_cpu(struct perf_cpu cpu __maybe_unused, 1597 void *data __maybe_unused) 1598 { 1599 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1600 1601 /* it always aggregates to the cpu 0 */ 1602 id.cpu = (struct perf_cpu){ .cpu = 0 }; 1603 return id; 1604 } 1605 1606 static struct aggr_cpu_id perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused, 1607 struct perf_cpu cpu) 1608 { 1609 return perf_env__get_socket_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1610 } 1611 static struct aggr_cpu_id perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused, 1612 struct perf_cpu cpu) 1613 { 1614 return perf_env__get_die_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1615 } 1616 1617 static struct aggr_cpu_id perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused, 1618 struct perf_cpu cpu) 1619 { 1620 return perf_env__get_core_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1621 } 1622 1623 static struct aggr_cpu_id perf_stat__get_cpu_file(struct perf_stat_config *config __maybe_unused, 1624 struct perf_cpu cpu) 1625 { 1626 return perf_env__get_cpu_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1627 } 1628 1629 static struct aggr_cpu_id perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused, 1630 struct perf_cpu cpu) 1631 { 1632 return perf_env__get_node_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1633 } 1634 1635 static struct aggr_cpu_id perf_stat__get_global_file(struct perf_stat_config *config __maybe_unused, 1636 struct perf_cpu cpu) 1637 { 1638 return perf_env__get_global_aggr_by_cpu(cpu, &perf_stat.session->header.env); 1639 } 1640 1641 static aggr_cpu_id_get_t aggr_mode__get_aggr_file(enum aggr_mode aggr_mode) 1642 { 1643 switch (aggr_mode) { 1644 case AGGR_SOCKET: 1645 return perf_env__get_socket_aggr_by_cpu; 1646 case AGGR_DIE: 1647 return perf_env__get_die_aggr_by_cpu; 1648 case AGGR_CORE: 1649 return perf_env__get_core_aggr_by_cpu; 1650 case AGGR_NODE: 1651 return perf_env__get_node_aggr_by_cpu; 1652 case AGGR_GLOBAL: 1653 return perf_env__get_global_aggr_by_cpu; 1654 case AGGR_NONE: 1655 return perf_env__get_cpu_aggr_by_cpu; 1656 case AGGR_THREAD: 1657 case AGGR_UNSET: 1658 case AGGR_MAX: 1659 default: 1660 return NULL; 1661 } 1662 } 1663 1664 static aggr_get_id_t aggr_mode__get_id_file(enum aggr_mode aggr_mode) 1665 { 1666 switch (aggr_mode) { 1667 case AGGR_SOCKET: 1668 return perf_stat__get_socket_file; 1669 case AGGR_DIE: 1670 return perf_stat__get_die_file; 1671 case AGGR_CORE: 1672 return perf_stat__get_core_file; 1673 case AGGR_NODE: 1674 return perf_stat__get_node_file; 1675 case AGGR_GLOBAL: 1676 return perf_stat__get_global_file; 1677 case AGGR_NONE: 1678 return perf_stat__get_cpu_file; 1679 case AGGR_THREAD: 1680 case AGGR_UNSET: 1681 case AGGR_MAX: 1682 default: 1683 return NULL; 1684 } 1685 } 1686 1687 static int perf_stat_init_aggr_mode_file(struct perf_stat *st) 1688 { 1689 struct perf_env *env = &st->session->header.env; 1690 aggr_cpu_id_get_t get_id = aggr_mode__get_aggr_file(stat_config.aggr_mode); 1691 bool needs_sort = stat_config.aggr_mode != AGGR_NONE; 1692 1693 if (stat_config.aggr_mode == AGGR_THREAD) { 1694 int nr = perf_thread_map__nr(evsel_list->core.threads); 1695 1696 stat_config.aggr_map = cpu_aggr_map__empty_new(nr); 1697 if (stat_config.aggr_map == NULL) 1698 return -ENOMEM; 1699 1700 for (int s = 0; s < nr; s++) { 1701 struct aggr_cpu_id id = aggr_cpu_id__empty(); 1702 1703 id.thread_idx = s; 1704 stat_config.aggr_map->map[s] = id; 1705 } 1706 return 0; 1707 } 1708 1709 if (!get_id) 1710 return 0; 1711 1712 stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus, 1713 get_id, env, needs_sort); 1714 if (!stat_config.aggr_map) { 1715 pr_err("cannot build %s map", aggr_mode__string[stat_config.aggr_mode]); 1716 return -1; 1717 } 1718 stat_config.aggr_get_id = aggr_mode__get_id_file(stat_config.aggr_mode); 1719 return 0; 1720 } 1721 1722 /* 1723 * Add default attributes, if there were no attributes specified or 1724 * if -d/--detailed, -d -d or -d -d -d is used: 1725 */ 1726 static int add_default_attributes(void) 1727 { 1728 int err; 1729 struct perf_event_attr default_attrs0[] = { 1730 1731 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, 1732 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, 1733 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, 1734 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, 1735 1736 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, 1737 }; 1738 struct perf_event_attr frontend_attrs[] = { 1739 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, 1740 }; 1741 struct perf_event_attr backend_attrs[] = { 1742 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, 1743 }; 1744 struct perf_event_attr default_attrs1[] = { 1745 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, 1746 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, 1747 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, 1748 1749 }; 1750 1751 /* 1752 * Detailed stats (-d), covering the L1 and last level data caches: 1753 */ 1754 struct perf_event_attr detailed_attrs[] = { 1755 1756 { .type = PERF_TYPE_HW_CACHE, 1757 .config = 1758 PERF_COUNT_HW_CACHE_L1D << 0 | 1759 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1760 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1761 1762 { .type = PERF_TYPE_HW_CACHE, 1763 .config = 1764 PERF_COUNT_HW_CACHE_L1D << 0 | 1765 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1766 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1767 1768 { .type = PERF_TYPE_HW_CACHE, 1769 .config = 1770 PERF_COUNT_HW_CACHE_LL << 0 | 1771 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1772 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1773 1774 { .type = PERF_TYPE_HW_CACHE, 1775 .config = 1776 PERF_COUNT_HW_CACHE_LL << 0 | 1777 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1778 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1779 }; 1780 1781 /* 1782 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: 1783 */ 1784 struct perf_event_attr very_detailed_attrs[] = { 1785 1786 { .type = PERF_TYPE_HW_CACHE, 1787 .config = 1788 PERF_COUNT_HW_CACHE_L1I << 0 | 1789 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1790 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1791 1792 { .type = PERF_TYPE_HW_CACHE, 1793 .config = 1794 PERF_COUNT_HW_CACHE_L1I << 0 | 1795 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1796 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1797 1798 { .type = PERF_TYPE_HW_CACHE, 1799 .config = 1800 PERF_COUNT_HW_CACHE_DTLB << 0 | 1801 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1802 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1803 1804 { .type = PERF_TYPE_HW_CACHE, 1805 .config = 1806 PERF_COUNT_HW_CACHE_DTLB << 0 | 1807 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1808 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1809 1810 { .type = PERF_TYPE_HW_CACHE, 1811 .config = 1812 PERF_COUNT_HW_CACHE_ITLB << 0 | 1813 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1814 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1815 1816 { .type = PERF_TYPE_HW_CACHE, 1817 .config = 1818 PERF_COUNT_HW_CACHE_ITLB << 0 | 1819 (PERF_COUNT_HW_CACHE_OP_READ << 8) | 1820 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1821 1822 }; 1823 1824 /* 1825 * Very, very detailed stats (-d -d -d), adding prefetch events: 1826 */ 1827 struct perf_event_attr very_very_detailed_attrs[] = { 1828 1829 { .type = PERF_TYPE_HW_CACHE, 1830 .config = 1831 PERF_COUNT_HW_CACHE_L1D << 0 | 1832 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | 1833 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, 1834 1835 { .type = PERF_TYPE_HW_CACHE, 1836 .config = 1837 PERF_COUNT_HW_CACHE_L1D << 0 | 1838 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | 1839 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, 1840 }; 1841 1842 struct perf_event_attr default_null_attrs[] = {}; 1843 1844 /* Set attrs if no event is selected and !null_run: */ 1845 if (stat_config.null_run) 1846 return 0; 1847 1848 if (transaction_run) { 1849 struct parse_events_error errinfo; 1850 /* Handle -T as -M transaction. Once platform specific metrics 1851 * support has been added to the json files, all architectures 1852 * will use this approach. To determine transaction support 1853 * on an architecture test for such a metric name. 1854 */ 1855 if (metricgroup__has_metric("transaction")) { 1856 return metricgroup__parse_groups(evsel_list, "transaction", 1857 stat_config.metric_no_group, 1858 stat_config.metric_no_merge, 1859 stat_config.user_requested_cpu_list, 1860 stat_config.system_wide, 1861 &stat_config.metric_events); 1862 } 1863 1864 parse_events_error__init(&errinfo); 1865 if (pmu_have_event("cpu", "cycles-ct") && 1866 pmu_have_event("cpu", "el-start")) 1867 err = parse_events(evsel_list, transaction_attrs, 1868 &errinfo); 1869 else 1870 err = parse_events(evsel_list, 1871 transaction_limited_attrs, 1872 &errinfo); 1873 if (err) { 1874 fprintf(stderr, "Cannot set up transaction events\n"); 1875 parse_events_error__print(&errinfo, transaction_attrs); 1876 } 1877 parse_events_error__exit(&errinfo); 1878 return err ? -1 : 0; 1879 } 1880 1881 if (smi_cost) { 1882 struct parse_events_error errinfo; 1883 int smi; 1884 1885 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) { 1886 fprintf(stderr, "freeze_on_smi is not supported.\n"); 1887 return -1; 1888 } 1889 1890 if (!smi) { 1891 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) { 1892 fprintf(stderr, "Failed to set freeze_on_smi.\n"); 1893 return -1; 1894 } 1895 smi_reset = true; 1896 } 1897 1898 if (!pmu_have_event("msr", "aperf") || 1899 !pmu_have_event("msr", "smi")) { 1900 fprintf(stderr, "To measure SMI cost, it needs " 1901 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n"); 1902 return -1; 1903 } 1904 if (!force_metric_only) 1905 stat_config.metric_only = true; 1906 1907 parse_events_error__init(&errinfo); 1908 err = parse_events(evsel_list, smi_cost_attrs, &errinfo); 1909 if (err) { 1910 parse_events_error__print(&errinfo, smi_cost_attrs); 1911 fprintf(stderr, "Cannot set up SMI cost events\n"); 1912 } 1913 parse_events_error__exit(&errinfo); 1914 return err ? -1 : 0; 1915 } 1916 1917 if (topdown_run) { 1918 const char **metric_attrs = topdown_metric_attrs; 1919 unsigned int max_level = 1; 1920 char *str = NULL; 1921 bool warn = false; 1922 const char *pmu_name = arch_get_topdown_pmu_name(evsel_list, true); 1923 1924 if (!force_metric_only) 1925 stat_config.metric_only = true; 1926 1927 if (pmu_have_event(pmu_name, topdown_metric_L2_attrs[5])) { 1928 metric_attrs = topdown_metric_L2_attrs; 1929 max_level = 2; 1930 } 1931 1932 if (stat_config.topdown_level > max_level) { 1933 pr_err("Invalid top-down metrics level. The max level is %u.\n", max_level); 1934 return -1; 1935 } else if (!stat_config.topdown_level) 1936 stat_config.topdown_level = max_level; 1937 1938 if (topdown_filter_events(metric_attrs, &str, 1, pmu_name) < 0) { 1939 pr_err("Out of memory\n"); 1940 return -1; 1941 } 1942 1943 if (metric_attrs[0] && str) { 1944 if (!stat_config.interval && !stat_config.metric_only) { 1945 fprintf(stat_config.output, 1946 "Topdown accuracy may decrease when measuring long periods.\n" 1947 "Please print the result regularly, e.g. -I1000\n"); 1948 } 1949 goto setup_metrics; 1950 } 1951 1952 zfree(&str); 1953 1954 if (stat_config.aggr_mode != AGGR_GLOBAL && 1955 stat_config.aggr_mode != AGGR_CORE) { 1956 pr_err("top down event configuration requires --per-core mode\n"); 1957 return -1; 1958 } 1959 stat_config.aggr_mode = AGGR_CORE; 1960 if (nr_cgroups || !target__has_cpu(&target)) { 1961 pr_err("top down event configuration requires system-wide mode (-a)\n"); 1962 return -1; 1963 } 1964 1965 if (topdown_filter_events(topdown_attrs, &str, 1966 arch_topdown_check_group(&warn), 1967 pmu_name) < 0) { 1968 pr_err("Out of memory\n"); 1969 return -1; 1970 } 1971 1972 if (topdown_attrs[0] && str) { 1973 struct parse_events_error errinfo; 1974 if (warn) 1975 arch_topdown_group_warn(); 1976 setup_metrics: 1977 parse_events_error__init(&errinfo); 1978 err = parse_events(evsel_list, str, &errinfo); 1979 if (err) { 1980 fprintf(stderr, 1981 "Cannot set up top down events %s: %d\n", 1982 str, err); 1983 parse_events_error__print(&errinfo, str); 1984 parse_events_error__exit(&errinfo); 1985 free(str); 1986 return -1; 1987 } 1988 parse_events_error__exit(&errinfo); 1989 } else { 1990 fprintf(stderr, "System does not support topdown\n"); 1991 return -1; 1992 } 1993 free(str); 1994 } 1995 1996 if (!stat_config.topdown_level) 1997 stat_config.topdown_level = TOPDOWN_MAX_LEVEL; 1998 1999 if (!evsel_list->core.nr_entries) { 2000 if (target__has_cpu(&target)) 2001 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK; 2002 2003 if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0) 2004 return -1; 2005 if (pmu_have_event("cpu", "stalled-cycles-frontend")) { 2006 if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0) 2007 return -1; 2008 } 2009 if (pmu_have_event("cpu", "stalled-cycles-backend")) { 2010 if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0) 2011 return -1; 2012 } 2013 if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0) 2014 return -1; 2015 /* Platform specific attrs */ 2016 if (evlist__add_default_attrs(evsel_list, default_null_attrs) < 0) 2017 return -1; 2018 } 2019 2020 /* Detailed events get appended to the event list: */ 2021 2022 if (detailed_run < 1) 2023 return 0; 2024 2025 /* Append detailed run extra attributes: */ 2026 if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) 2027 return -1; 2028 2029 if (detailed_run < 2) 2030 return 0; 2031 2032 /* Append very detailed run extra attributes: */ 2033 if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) 2034 return -1; 2035 2036 if (detailed_run < 3) 2037 return 0; 2038 2039 /* Append very, very detailed run extra attributes: */ 2040 return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); 2041 } 2042 2043 static const char * const stat_record_usage[] = { 2044 "perf stat record [<options>]", 2045 NULL, 2046 }; 2047 2048 static void init_features(struct perf_session *session) 2049 { 2050 int feat; 2051 2052 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 2053 perf_header__set_feat(&session->header, feat); 2054 2055 perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT); 2056 perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 2057 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 2058 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 2059 perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 2060 } 2061 2062 static int __cmd_record(int argc, const char **argv) 2063 { 2064 struct perf_session *session; 2065 struct perf_data *data = &perf_stat.data; 2066 2067 argc = parse_options(argc, argv, stat_options, stat_record_usage, 2068 PARSE_OPT_STOP_AT_NON_OPTION); 2069 2070 if (output_name) 2071 data->path = output_name; 2072 2073 if (stat_config.run_count != 1 || forever) { 2074 pr_err("Cannot use -r option with perf stat record.\n"); 2075 return -1; 2076 } 2077 2078 session = perf_session__new(data, NULL); 2079 if (IS_ERR(session)) { 2080 pr_err("Perf session creation failed\n"); 2081 return PTR_ERR(session); 2082 } 2083 2084 init_features(session); 2085 2086 session->evlist = evsel_list; 2087 perf_stat.session = session; 2088 perf_stat.record = true; 2089 return argc; 2090 } 2091 2092 static int process_stat_round_event(struct perf_session *session, 2093 union perf_event *event) 2094 { 2095 struct perf_record_stat_round *stat_round = &event->stat_round; 2096 struct timespec tsh, *ts = NULL; 2097 const char **argv = session->header.env.cmdline_argv; 2098 int argc = session->header.env.nr_cmdline; 2099 2100 process_counters(); 2101 2102 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL) 2103 update_stats(&walltime_nsecs_stats, stat_round->time); 2104 2105 if (stat_config.interval && stat_round->time) { 2106 tsh.tv_sec = stat_round->time / NSEC_PER_SEC; 2107 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC; 2108 ts = &tsh; 2109 } 2110 2111 print_counters(ts, argc, argv); 2112 return 0; 2113 } 2114 2115 static 2116 int process_stat_config_event(struct perf_session *session, 2117 union perf_event *event) 2118 { 2119 struct perf_tool *tool = session->tool; 2120 struct perf_stat *st = container_of(tool, struct perf_stat, tool); 2121 2122 perf_event__read_stat_config(&stat_config, &event->stat_config); 2123 2124 if (perf_cpu_map__empty(st->cpus)) { 2125 if (st->aggr_mode != AGGR_UNSET) 2126 pr_warning("warning: processing task data, aggregation mode not set\n"); 2127 } else if (st->aggr_mode != AGGR_UNSET) { 2128 stat_config.aggr_mode = st->aggr_mode; 2129 } 2130 2131 if (perf_stat.data.is_pipe) 2132 perf_stat_init_aggr_mode(); 2133 else 2134 perf_stat_init_aggr_mode_file(st); 2135 2136 if (stat_config.aggr_map) { 2137 int nr_aggr = stat_config.aggr_map->nr; 2138 2139 if (evlist__alloc_aggr_stats(session->evlist, nr_aggr) < 0) { 2140 pr_err("cannot allocate aggr counts\n"); 2141 return -1; 2142 } 2143 } 2144 return 0; 2145 } 2146 2147 static int set_maps(struct perf_stat *st) 2148 { 2149 if (!st->cpus || !st->threads) 2150 return 0; 2151 2152 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n")) 2153 return -EINVAL; 2154 2155 perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads); 2156 2157 if (evlist__alloc_stats(&stat_config, evsel_list, /*alloc_raw=*/true)) 2158 return -ENOMEM; 2159 2160 st->maps_allocated = true; 2161 return 0; 2162 } 2163 2164 static 2165 int process_thread_map_event(struct perf_session *session, 2166 union perf_event *event) 2167 { 2168 struct perf_tool *tool = session->tool; 2169 struct perf_stat *st = container_of(tool, struct perf_stat, tool); 2170 2171 if (st->threads) { 2172 pr_warning("Extra thread map event, ignoring.\n"); 2173 return 0; 2174 } 2175 2176 st->threads = thread_map__new_event(&event->thread_map); 2177 if (!st->threads) 2178 return -ENOMEM; 2179 2180 return set_maps(st); 2181 } 2182 2183 static 2184 int process_cpu_map_event(struct perf_session *session, 2185 union perf_event *event) 2186 { 2187 struct perf_tool *tool = session->tool; 2188 struct perf_stat *st = container_of(tool, struct perf_stat, tool); 2189 struct perf_cpu_map *cpus; 2190 2191 if (st->cpus) { 2192 pr_warning("Extra cpu map event, ignoring.\n"); 2193 return 0; 2194 } 2195 2196 cpus = cpu_map__new_data(&event->cpu_map.data); 2197 if (!cpus) 2198 return -ENOMEM; 2199 2200 st->cpus = cpus; 2201 return set_maps(st); 2202 } 2203 2204 static const char * const stat_report_usage[] = { 2205 "perf stat report [<options>]", 2206 NULL, 2207 }; 2208 2209 static struct perf_stat perf_stat = { 2210 .tool = { 2211 .attr = perf_event__process_attr, 2212 .event_update = perf_event__process_event_update, 2213 .thread_map = process_thread_map_event, 2214 .cpu_map = process_cpu_map_event, 2215 .stat_config = process_stat_config_event, 2216 .stat = perf_event__process_stat_event, 2217 .stat_round = process_stat_round_event, 2218 }, 2219 .aggr_mode = AGGR_UNSET, 2220 }; 2221 2222 static int __cmd_report(int argc, const char **argv) 2223 { 2224 struct perf_session *session; 2225 const struct option options[] = { 2226 OPT_STRING('i', "input", &input_name, "file", "input file name"), 2227 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode, 2228 "aggregate counts per processor socket", AGGR_SOCKET), 2229 OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode, 2230 "aggregate counts per processor die", AGGR_DIE), 2231 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode, 2232 "aggregate counts per physical processor core", AGGR_CORE), 2233 OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode, 2234 "aggregate counts per numa node", AGGR_NODE), 2235 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode, 2236 "disable CPU count aggregation", AGGR_NONE), 2237 OPT_END() 2238 }; 2239 struct stat st; 2240 int ret; 2241 2242 argc = parse_options(argc, argv, options, stat_report_usage, 0); 2243 2244 if (!input_name || !strlen(input_name)) { 2245 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 2246 input_name = "-"; 2247 else 2248 input_name = "perf.data"; 2249 } 2250 2251 perf_stat__init_shadow_stats(); 2252 2253 perf_stat.data.path = input_name; 2254 perf_stat.data.mode = PERF_DATA_MODE_READ; 2255 2256 session = perf_session__new(&perf_stat.data, &perf_stat.tool); 2257 if (IS_ERR(session)) 2258 return PTR_ERR(session); 2259 2260 perf_stat.session = session; 2261 stat_config.output = stderr; 2262 evsel_list = session->evlist; 2263 2264 ret = perf_session__process_events(session); 2265 if (ret) 2266 return ret; 2267 2268 perf_session__delete(session); 2269 return 0; 2270 } 2271 2272 static void setup_system_wide(int forks) 2273 { 2274 /* 2275 * Make system wide (-a) the default target if 2276 * no target was specified and one of following 2277 * conditions is met: 2278 * 2279 * - there's no workload specified 2280 * - there is workload specified but all requested 2281 * events are system wide events 2282 */ 2283 if (!target__none(&target)) 2284 return; 2285 2286 if (!forks) 2287 target.system_wide = true; 2288 else { 2289 struct evsel *counter; 2290 2291 evlist__for_each_entry(evsel_list, counter) { 2292 if (!counter->core.requires_cpu && 2293 strcmp(counter->name, "duration_time")) { 2294 return; 2295 } 2296 } 2297 2298 if (evsel_list->core.nr_entries) 2299 target.system_wide = true; 2300 } 2301 } 2302 2303 int cmd_stat(int argc, const char **argv) 2304 { 2305 const char * const stat_usage[] = { 2306 "perf stat [<options>] [<command>]", 2307 NULL 2308 }; 2309 int status = -EINVAL, run_idx, err; 2310 const char *mode; 2311 FILE *output = stderr; 2312 unsigned int interval, timeout; 2313 const char * const stat_subcommands[] = { "record", "report" }; 2314 char errbuf[BUFSIZ]; 2315 2316 setlocale(LC_ALL, ""); 2317 2318 evsel_list = evlist__new(); 2319 if (evsel_list == NULL) 2320 return -ENOMEM; 2321 2322 parse_events__shrink_config_terms(); 2323 2324 /* String-parsing callback-based options would segfault when negated */ 2325 set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG); 2326 set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG); 2327 set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG); 2328 2329 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, 2330 (const char **) stat_usage, 2331 PARSE_OPT_STOP_AT_NON_OPTION); 2332 2333 if (stat_config.csv_sep) { 2334 stat_config.csv_output = true; 2335 if (!strcmp(stat_config.csv_sep, "\\t")) 2336 stat_config.csv_sep = "\t"; 2337 } else 2338 stat_config.csv_sep = DEFAULT_SEPARATOR; 2339 2340 if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) { 2341 argc = __cmd_record(argc, argv); 2342 if (argc < 0) 2343 return -1; 2344 } else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0])) 2345 return __cmd_report(argc, argv); 2346 2347 interval = stat_config.interval; 2348 timeout = stat_config.timeout; 2349 2350 /* 2351 * For record command the -o is already taken care of. 2352 */ 2353 if (!STAT_RECORD && output_name && strcmp(output_name, "-")) 2354 output = NULL; 2355 2356 if (output_name && output_fd) { 2357 fprintf(stderr, "cannot use both --output and --log-fd\n"); 2358 parse_options_usage(stat_usage, stat_options, "o", 1); 2359 parse_options_usage(NULL, stat_options, "log-fd", 0); 2360 goto out; 2361 } 2362 2363 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) { 2364 fprintf(stderr, "--metric-only is not supported with --per-thread\n"); 2365 goto out; 2366 } 2367 2368 if (stat_config.metric_only && stat_config.run_count > 1) { 2369 fprintf(stderr, "--metric-only is not supported with -r\n"); 2370 goto out; 2371 } 2372 2373 if (stat_config.walltime_run_table && stat_config.run_count <= 1) { 2374 fprintf(stderr, "--table is only supported with -r\n"); 2375 parse_options_usage(stat_usage, stat_options, "r", 1); 2376 parse_options_usage(NULL, stat_options, "table", 0); 2377 goto out; 2378 } 2379 2380 if (output_fd < 0) { 2381 fprintf(stderr, "argument to --log-fd must be a > 0\n"); 2382 parse_options_usage(stat_usage, stat_options, "log-fd", 0); 2383 goto out; 2384 } 2385 2386 if (!output && !quiet) { 2387 struct timespec tm; 2388 mode = append_file ? "a" : "w"; 2389 2390 output = fopen(output_name, mode); 2391 if (!output) { 2392 perror("failed to create output file"); 2393 return -1; 2394 } 2395 clock_gettime(CLOCK_REALTIME, &tm); 2396 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); 2397 } else if (output_fd > 0) { 2398 mode = append_file ? "a" : "w"; 2399 output = fdopen(output_fd, mode); 2400 if (!output) { 2401 perror("Failed opening logfd"); 2402 return -errno; 2403 } 2404 } 2405 2406 if (stat_config.interval_clear && !isatty(fileno(output))) { 2407 fprintf(stderr, "--interval-clear does not work with output\n"); 2408 parse_options_usage(stat_usage, stat_options, "o", 1); 2409 parse_options_usage(NULL, stat_options, "log-fd", 0); 2410 parse_options_usage(NULL, stat_options, "interval-clear", 0); 2411 return -1; 2412 } 2413 2414 stat_config.output = output; 2415 2416 /* 2417 * let the spreadsheet do the pretty-printing 2418 */ 2419 if (stat_config.csv_output) { 2420 /* User explicitly passed -B? */ 2421 if (big_num_opt == 1) { 2422 fprintf(stderr, "-B option not supported with -x\n"); 2423 parse_options_usage(stat_usage, stat_options, "B", 1); 2424 parse_options_usage(NULL, stat_options, "x", 1); 2425 goto out; 2426 } else /* Nope, so disable big number formatting */ 2427 stat_config.big_num = false; 2428 } else if (big_num_opt == 0) /* User passed --no-big-num */ 2429 stat_config.big_num = false; 2430 2431 err = target__validate(&target); 2432 if (err) { 2433 target__strerror(&target, err, errbuf, BUFSIZ); 2434 pr_warning("%s\n", errbuf); 2435 } 2436 2437 setup_system_wide(argc); 2438 2439 /* 2440 * Display user/system times only for single 2441 * run and when there's specified tracee. 2442 */ 2443 if ((stat_config.run_count == 1) && target__none(&target)) 2444 stat_config.ru_display = true; 2445 2446 if (stat_config.run_count < 0) { 2447 pr_err("Run count must be a positive number\n"); 2448 parse_options_usage(stat_usage, stat_options, "r", 1); 2449 goto out; 2450 } else if (stat_config.run_count == 0) { 2451 forever = true; 2452 stat_config.run_count = 1; 2453 } 2454 2455 if (stat_config.walltime_run_table) { 2456 stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0])); 2457 if (!stat_config.walltime_run) { 2458 pr_err("failed to setup -r option"); 2459 goto out; 2460 } 2461 } 2462 2463 if ((stat_config.aggr_mode == AGGR_THREAD) && 2464 !target__has_task(&target)) { 2465 if (!target.system_wide || target.cpu_list) { 2466 fprintf(stderr, "The --per-thread option is only " 2467 "available when monitoring via -p -t -a " 2468 "options or only --per-thread.\n"); 2469 parse_options_usage(NULL, stat_options, "p", 1); 2470 parse_options_usage(NULL, stat_options, "t", 1); 2471 goto out; 2472 } 2473 } 2474 2475 /* 2476 * no_aggr, cgroup are for system-wide only 2477 * --per-thread is aggregated per thread, we dont mix it with cpu mode 2478 */ 2479 if (((stat_config.aggr_mode != AGGR_GLOBAL && 2480 stat_config.aggr_mode != AGGR_THREAD) || 2481 (nr_cgroups || stat_config.cgroup_list)) && 2482 !target__has_cpu(&target)) { 2483 fprintf(stderr, "both cgroup and no-aggregation " 2484 "modes only available in system-wide mode\n"); 2485 2486 parse_options_usage(stat_usage, stat_options, "G", 1); 2487 parse_options_usage(NULL, stat_options, "A", 1); 2488 parse_options_usage(NULL, stat_options, "a", 1); 2489 parse_options_usage(NULL, stat_options, "for-each-cgroup", 0); 2490 goto out; 2491 } 2492 2493 if (stat_config.iostat_run) { 2494 status = iostat_prepare(evsel_list, &stat_config); 2495 if (status) 2496 goto out; 2497 if (iostat_mode == IOSTAT_LIST) { 2498 iostat_list(evsel_list, &stat_config); 2499 goto out; 2500 } else if (verbose) 2501 iostat_list(evsel_list, &stat_config); 2502 if (iostat_mode == IOSTAT_RUN && !target__has_cpu(&target)) 2503 target.system_wide = true; 2504 } 2505 2506 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide)) 2507 target.per_thread = true; 2508 2509 stat_config.system_wide = target.system_wide; 2510 if (target.cpu_list) { 2511 stat_config.user_requested_cpu_list = strdup(target.cpu_list); 2512 if (!stat_config.user_requested_cpu_list) { 2513 status = -ENOMEM; 2514 goto out; 2515 } 2516 } 2517 2518 /* 2519 * Metric parsing needs to be delayed as metrics may optimize events 2520 * knowing the target is system-wide. 2521 */ 2522 if (metrics) { 2523 metricgroup__parse_groups(evsel_list, metrics, 2524 stat_config.metric_no_group, 2525 stat_config.metric_no_merge, 2526 stat_config.user_requested_cpu_list, 2527 stat_config.system_wide, 2528 &stat_config.metric_events); 2529 zfree(&metrics); 2530 } 2531 perf_stat__collect_metric_expr(evsel_list); 2532 perf_stat__init_shadow_stats(); 2533 2534 if (add_default_attributes()) 2535 goto out; 2536 2537 if (stat_config.cgroup_list) { 2538 if (nr_cgroups > 0) { 2539 pr_err("--cgroup and --for-each-cgroup cannot be used together\n"); 2540 parse_options_usage(stat_usage, stat_options, "G", 1); 2541 parse_options_usage(NULL, stat_options, "for-each-cgroup", 0); 2542 goto out; 2543 } 2544 2545 if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list, 2546 &stat_config.metric_events, true) < 0) { 2547 parse_options_usage(stat_usage, stat_options, 2548 "for-each-cgroup", 0); 2549 goto out; 2550 } 2551 } 2552 2553 if (evlist__fix_hybrid_cpus(evsel_list, target.cpu_list)) { 2554 pr_err("failed to use cpu list %s\n", target.cpu_list); 2555 goto out; 2556 } 2557 2558 target.hybrid = perf_pmu__has_hybrid(); 2559 if (evlist__create_maps(evsel_list, &target) < 0) { 2560 if (target__has_task(&target)) { 2561 pr_err("Problems finding threads of monitor\n"); 2562 parse_options_usage(stat_usage, stat_options, "p", 1); 2563 parse_options_usage(NULL, stat_options, "t", 1); 2564 } else if (target__has_cpu(&target)) { 2565 perror("failed to parse CPUs map"); 2566 parse_options_usage(stat_usage, stat_options, "C", 1); 2567 parse_options_usage(NULL, stat_options, "a", 1); 2568 } 2569 goto out; 2570 } 2571 2572 evlist__check_cpu_maps(evsel_list); 2573 2574 /* 2575 * Initialize thread_map with comm names, 2576 * so we could print it out on output. 2577 */ 2578 if (stat_config.aggr_mode == AGGR_THREAD) { 2579 thread_map__read_comms(evsel_list->core.threads); 2580 } 2581 2582 if (stat_config.aggr_mode == AGGR_NODE) 2583 cpu__setup_cpunode_map(); 2584 2585 if (stat_config.times && interval) 2586 interval_count = true; 2587 else if (stat_config.times && !interval) { 2588 pr_err("interval-count option should be used together with " 2589 "interval-print.\n"); 2590 parse_options_usage(stat_usage, stat_options, "interval-count", 0); 2591 parse_options_usage(stat_usage, stat_options, "I", 1); 2592 goto out; 2593 } 2594 2595 if (timeout && timeout < 100) { 2596 if (timeout < 10) { 2597 pr_err("timeout must be >= 10ms.\n"); 2598 parse_options_usage(stat_usage, stat_options, "timeout", 0); 2599 goto out; 2600 } else 2601 pr_warning("timeout < 100ms. " 2602 "The overhead percentage could be high in some cases. " 2603 "Please proceed with caution.\n"); 2604 } 2605 if (timeout && interval) { 2606 pr_err("timeout option is not supported with interval-print.\n"); 2607 parse_options_usage(stat_usage, stat_options, "timeout", 0); 2608 parse_options_usage(stat_usage, stat_options, "I", 1); 2609 goto out; 2610 } 2611 2612 if (perf_stat_init_aggr_mode()) 2613 goto out; 2614 2615 if (evlist__alloc_stats(&stat_config, evsel_list, interval)) 2616 goto out; 2617 2618 /* 2619 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless 2620 * while avoiding that older tools show confusing messages. 2621 * 2622 * However for pipe sessions we need to keep it zero, 2623 * because script's perf_evsel__check_attr is triggered 2624 * by attr->sample_type != 0, and we can't run it on 2625 * stat sessions. 2626 */ 2627 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe); 2628 2629 /* 2630 * We dont want to block the signals - that would cause 2631 * child tasks to inherit that and Ctrl-C would not work. 2632 * What we want is for Ctrl-C to work in the exec()-ed 2633 * task, but being ignored by perf stat itself: 2634 */ 2635 atexit(sig_atexit); 2636 if (!forever) 2637 signal(SIGINT, skip_signal); 2638 signal(SIGCHLD, skip_signal); 2639 signal(SIGALRM, skip_signal); 2640 signal(SIGABRT, skip_signal); 2641 2642 if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack)) 2643 goto out; 2644 2645 /* Enable ignoring missing threads when -p option is defined. */ 2646 evlist__first(evsel_list)->ignore_missing_thread = target.pid; 2647 status = 0; 2648 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) { 2649 if (stat_config.run_count != 1 && verbose > 0) 2650 fprintf(output, "[ perf stat: executing run #%d ... ]\n", 2651 run_idx + 1); 2652 2653 if (run_idx != 0) 2654 evlist__reset_prev_raw_counts(evsel_list); 2655 2656 status = run_perf_stat(argc, argv, run_idx); 2657 if (forever && status != -1 && !interval) { 2658 print_counters(NULL, argc, argv); 2659 perf_stat__reset_stats(); 2660 } 2661 } 2662 2663 if (!forever && status != -1 && (!interval || stat_config.summary)) 2664 print_counters(NULL, argc, argv); 2665 2666 evlist__finalize_ctlfd(evsel_list); 2667 2668 if (STAT_RECORD) { 2669 /* 2670 * We synthesize the kernel mmap record just so that older tools 2671 * don't emit warnings about not being able to resolve symbols 2672 * due to /proc/sys/kernel/kptr_restrict settings and instead provide 2673 * a saner message about no samples being in the perf.data file. 2674 * 2675 * This also serves to suppress a warning about f_header.data.size == 0 2676 * in header.c at the moment 'perf stat record' gets introduced, which 2677 * is not really needed once we start adding the stat specific PERF_RECORD_ 2678 * records, but the need to suppress the kptr_restrict messages in older 2679 * tools remain -acme 2680 */ 2681 int fd = perf_data__fd(&perf_stat.data); 2682 2683 err = perf_event__synthesize_kernel_mmap((void *)&perf_stat, 2684 process_synthesized_event, 2685 &perf_stat.session->machines.host); 2686 if (err) { 2687 pr_warning("Couldn't synthesize the kernel mmap record, harmless, " 2688 "older tools may produce warnings about this file\n."); 2689 } 2690 2691 if (!interval) { 2692 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL)) 2693 pr_err("failed to write stat round event\n"); 2694 } 2695 2696 if (!perf_stat.data.is_pipe) { 2697 perf_stat.session->header.data_size += perf_stat.bytes_written; 2698 perf_session__write_header(perf_stat.session, evsel_list, fd, true); 2699 } 2700 2701 evlist__close(evsel_list); 2702 perf_session__delete(perf_stat.session); 2703 } 2704 2705 perf_stat__exit_aggr_mode(); 2706 evlist__free_stats(evsel_list); 2707 out: 2708 if (stat_config.iostat_run) 2709 iostat_release(evsel_list); 2710 2711 zfree(&stat_config.walltime_run); 2712 zfree(&stat_config.user_requested_cpu_list); 2713 2714 if (smi_cost && smi_reset) 2715 sysfs__write_int(FREEZE_ON_SMI_PATH, 0); 2716 2717 evlist__delete(evsel_list); 2718 2719 metricgroup__rblist_exit(&stat_config.metric_events); 2720 evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close); 2721 2722 return status; 2723 } 2724