From patchwork Thu Mar 30 13:18:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 77175 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1133161vqo; Thu, 30 Mar 2023 06:36:53 -0700 (PDT) X-Google-Smtp-Source: AKy350a42iaHOYzN/Ebyq852mobNtjzwMNGLRyOI2Q5xOU4ER9Q5zAH4yrJYsHT6sH/2EkcsuhPD X-Received: by 2002:a17:906:5293:b0:92c:6fbf:28 with SMTP id c19-20020a170906529300b0092c6fbf0028mr25108772ejm.64.1680183413498; Thu, 30 Mar 2023 06:36:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680183413; cv=none; d=google.com; s=arc-20160816; b=YjKYrKnwiKJdz4752dcoQcLgOPb4MxoZr2VVwYtyF2cNR5+24Dv1DlgFk2v37GtzSe J3A3Vc41ZeSApkzj5RIenJlwK+JjZNRHnnUajbu04vaLdtazQnek4AyMewnimgKwwJeC C74nm6bmXzIBL+E4CgVs8o4op+mnKUlrAmRNjdW/ZZYFYlqcWnwhbr3qzIPy66wlMsU+ r3ODP1PCJza3DiGheuvdDEJ4mqrKd8MTK63KHNWyUaj662ZDOwttIp+az3kQ0qb3CK/7 N+YtFqJBDMoLXx1Rsm6BWlDEhFLcs264RpyXVMcEc5dXG0yBhCEGylhpKQ59+r1Tx9hp YfjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=y+p3eDIt36scYfT2NhI1rHpeR0QJ8+JkYD79UFXLd8o=; b=urvMERZy/+xQ7vZB5VBtl3LAdnCZ5EIOf3Xpx6Y3AxbI4WdkvsvFKKp+pD07KbefWb JYAbCUzVdRGr8vz5WkY+1xA5+pW34qK3wnrsp6wHhOJKyoM2fS6Cd1YUls2/mvIYtrQX 8gBVf4pFnC6VbvN+0ebeFs//D2CA2YfK3DwCcMituL+cO26p3t81t7RnFr4EkCgJSHcf CXQA5XkZb0tXWJmIbWuSM7fN2fh1HRAyaC7qoIMu0POaOV3W071Nj5bxGLtqdTCf+DlJ cUIDM5o7h2J7uQ+wZT2qe20EPovALFWXHj3/D2KUJkHDIEYkMZc7k16j4oPmnw5yM/tp w/Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hGaCkWXJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i20-20020a170906115400b008b17cc2e38asi184859eja.582.2023.03.30.06.36.27; Thu, 30 Mar 2023 06:36:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=hGaCkWXJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjC3NSz (ORCPT + 99 others); Thu, 30 Mar 2023 09:18:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231790AbjC3NSx (ORCPT ); Thu, 30 Mar 2023 09:18:53 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8052185; Thu, 30 Mar 2023 06:18:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680182332; x=1711718332; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3EksB4AadDf7H69GDgoB9EW6O1V5EwTQwlm8Fpj1ozk=; b=hGaCkWXJrdqreGDknqnlomTXGPpH+3+Gli6JH8xm1aSa9o5Ttt/5+wbv xkgCZZg6S5hvtTmhAViBB9DyUKmR9ojASIgiElB/pzXPuyjOfgREwCxEH cqgo4ffaDyIJcR7D+MmbClNJAdWVyvwvbFvIQRKFFnBYa4JrC5Kep98Mc 2zf0xaPNZmAi4HwMQHDXJRxM280dpdAxtIRv1KCrm3BS0v8XkKNAFbpxo wcYoUsNCOjGr8XOgn2n1gm+jwe/m0bD6r7SuN8RwsJIsl0o3z3Kv6L6EB EpLEJwU4mfWzDtLtU/4pz+vcWR7NWwkVGxcEk7truk3ia7qu6HiTQoEp4 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10665"; a="427440763" X-IronPort-AV: E=Sophos;i="5.98,303,1673942400"; d="scan'208";a="427440763" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 06:18:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10665"; a="678185543" X-IronPort-AV: E=Sophos;i="5.98,303,1673942400"; d="scan'208";a="678185543" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.249.32.122]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Mar 2023 06:18:48 -0700 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , Andi Kleen , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH 1/2] perf top: Add --branch-history option Date: Thu, 30 Mar 2023 16:18:32 +0300 Message-Id: <20230330131833.12864-2-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330131833.12864-1-adrian.hunter@intel.com> References: <20230330131833.12864-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761799890580084254?= X-GMAIL-MSGID: =?utf-8?q?1761800003101265723?= Add --branch-history option, to act the same as that option does for perf report. Example: $ cat tcallf.c volatile a = 10000, b = 100000, c; __attribute__((noinline)) f2() { c = a / b; } __attribute__((noinline)) f1() { f2(); f2(); } main() { while (1) f1(); } $ gcc -w -g -o tcallf tcallf.c $ ./tcallf & [1] 29409 $ perf top -e cycles:u -t $(pidof tcallf) --stdio --no-children --branch-history PerfTop: 3819 irqs/sec kernel: 0.0% exact: 0.0% lost: 0/0 drop: 0/0 [4000Hz cycles:u], (target_tid: 29409) -------------------------------------------------------------------------------------------------------------------- 49.01% tcallf.c:5 [.] f2 tcallf | |--24.91%--f2 tcallf.c:4 | | | |--17.14%--f1 tcallf.c:11 (cycles:1) | | f1 tcallf.c:11 | | f2 tcallf.c:6 (cycles:3) | | f2 tcallf.c:4 | | f1 tcallf.c:10 (cycles:2) | | f1 tcallf.c:9 | | main tcallf.c:16 (cycles:1) | | main tcallf.c:16 | | main tcallf.c:16 (cycles:1) | | main tcallf.c:16 | | f1 tcallf.c:12 (cycles:1) | | f1 tcallf.c:12 | | f2 tcallf.c:6 (cycles:3) | | f2 tcallf.c:4 | | f1 tcallf.c:11 (cycles:1 iter:1 avg_cycles:12) | | f1 tcallf.c:11 | | f2 tcallf.c:6 (cycles:3 iter:1 avg_cycles:12) | | f2 tcallf.c:4 | | f1 tcallf.c:10 (cycles:2 iter:1 avg_cycles:12) | | | --7.78%--f1 tcallf.c:10 (cycles:2) | f1 tcallf.c:9 | main tcallf.c:16 (cycles:1) | main tcallf.c:16 | main tcallf.c:16 (cycles:1) | main tcallf.c:16 | f1 tcallf.c:12 (cycles:1) | f1 tcallf.c:12 | f2 tcallf.c:6 (cycles:3) | f2 tcallf.c:4 | f1 tcallf.c:11 (cycles:1) | f1 tcallf.c:11 | f2 tcallf.c:6 (cycles:3) | f2 tcallf.c:4 | f1 tcallf.c:10 (cycles:2 iter:1 avg_cycles:12) | f1 tcallf.c:9 | main tcallf.c:16 (cycles:1 iter:1 avg_cycles:12) | main tcallf.c:16 | main tcallf.c:16 (cycles:1 iter:1 avg_cycles:12) ... $ pkill tcallf [1]+ Terminated ./tcallf Signed-off-by: Adrian Hunter --- tools/perf/Documentation/perf-top.txt | 4 ++++ tools/perf/builtin-top.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt index 619cc8143ad5..3c202ec080ba 100644 --- a/tools/perf/Documentation/perf-top.txt +++ b/tools/perf/Documentation/perf-top.txt @@ -254,6 +254,10 @@ Default is to monitor all CPUS. The various filters must be specified as a comma separated list: --branch-filter any_ret,u,k Note that this feature may not be available on all processors. +--branch-history:: + Add the addresses of sampled taken branches to the callstack. + This allows to examine the path the program took to each sample. + --raw-trace:: When displaying traceevent output, do not use print fmt or plugins. diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 82c6c065830d..2c985cfea517 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -1437,6 +1437,7 @@ int cmd_top(int argc, const char **argv) .max_stack = sysctl__max_stack(), .nr_threads_synthesize = UINT_MAX, }; + bool branch_call_mode = false; struct record_opts *opts = &top.record_opts; struct target *target = &opts->target; const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; @@ -1551,6 +1552,8 @@ int cmd_top(int argc, const char **argv) OPT_CALLBACK('j', "branch-filter", &opts->branch_stack, "branch filter mask", "branch stack filter modes", parse_branch_stack), + OPT_BOOLEAN(0, "branch-history", &branch_call_mode, + "add last branch records to call history"), OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace, "Show raw trace event output (do not use print fmt or plugins)"), OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy, @@ -1677,6 +1680,20 @@ int cmd_top(int argc, const char **argv) goto out_delete_evlist; } + if (branch_call_mode) { + if (!opts->branch_stack) + opts->branch_stack = PERF_SAMPLE_BRANCH_ANY; + symbol_conf.use_callchain = true; + callchain_param.key = CCKEY_ADDRESS; + callchain_param.branch_callstack = true; + callchain_param.enabled = true; + if (callchain_param.record_mode == CALLCHAIN_NONE) + callchain_param.record_mode = CALLCHAIN_FP; + callchain_register_param(&callchain_param); + if (!sort_order) + sort_order = "srcline,symbol,dso"; + } + if (opts->branch_stack && callchain_param.enabled) symbol_conf.show_branchflag_count = true;