Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp4126589iob; Tue, 17 May 2022 14:46:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0axMiTnjBQVpm/6R4OLrL9wxw8xJcyasog63s5NwJFr9Y/4qYU6WKCHHrwSaU1tMhFhuK X-Received: by 2002:a63:dc4e:0:b0:3f2:6da7:4cd3 with SMTP id f14-20020a63dc4e000000b003f26da74cd3mr10138157pgj.411.1652824014396; Tue, 17 May 2022 14:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652824014; cv=none; d=google.com; s=arc-20160816; b=Mi6SJo4wFfL5e89AaE0KMCQJ50pw6+1yaEwoRKzUdoHQ/qp7iqQL51Mi5+yf4HHXiM 9y87G6JESrXiUakeT/k9nzIPdMLrnwRZf3lYtKB61d1Muc3hx4zDdSBJOrAF9YuJbKs0 r0y+UZDow7+XVKshJdGdJi7te4xOiTlFhn48+HSpHRU5tCNM8VTS0IkLraEHWWQ20xHU xF7vDRSW0B6c3fctca3DQvSRCKokqb4fGfKfdr1Ey4bGUo1MovTFcF6n+rrEC1jGY8VO OdoJw4G/vB3Tyd0eGSrTr8W+MIdotgkr9ZZvyd8sE8gh7D/oonMwx9ierIhR8zaj+pnL voJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3a9HyiIRBM4wCH9QPbga5zq5KSGL3bFe4oIwKIdI5E4=; b=itgkKm4bIMIJinRqCwTEySjOJPGdhyN+s/YIp7LGvQicFYO6fKg4vJeYotGlsX0d4T o7PJKYo00pSADm85SO0wyj0j+Xbw2vxyQ8a0N7dY3qcdsWWWsUU2CfCEICLY7isEHbr7 tY0srXowaHwRd+Czvwb5W8DEjZnfedpFD/hoJNB+hM7wRMUEHPCHlrVuiGiff3iV6PoL nYBp5KyubVgDY6FAyx0R3lNILLsJbUO2SuNb15XbUzT5oRooPgmomc9KxUivX160etyB 4Qy3Mm8clApnXflWjkHFR7aUJdMKPs2FuBiatySXXmwFbzMWumSJBhBsz+NcadtTfCVe DBFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GYr55paZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q1-20020a170902eb8100b00156bdbdedf4si264784plg.269.2022.05.17.14.46.42; Tue, 17 May 2022 14:46:54 -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=@gmail.com header.s=20210112 header.b=GYr55paZ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238485AbiEQB5q (ORCPT + 99 others); Mon, 16 May 2022 21:57:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238542AbiEQB5o (ORCPT ); Mon, 16 May 2022 21:57:44 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66FFC27B38; Mon, 16 May 2022 18:57:43 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id n8so16101009plh.1; Mon, 16 May 2022 18:57:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3a9HyiIRBM4wCH9QPbga5zq5KSGL3bFe4oIwKIdI5E4=; b=GYr55paZ8TIT7tq6qQkePhatMRmdh7D+PtqLx6vy02JCPGjQAUiwkQIRJwwfdm9Uye jSnG4k791tnDD2QEDT+k4Iql0Oy4c9wwvVTfaVFUSrSIeDIsDMt5AOrEUARxg7LF/qmW EtXnCmCD5a5LH69Wc2+osbJrPVZ9kxuD92c4m+yA8cwBZC478wEWTeqpqlKJgzD3IAxy 4VegSWsTfFuhUgCtMdpFJKg6WNwk0QaX/HO4zdpO34GdhCE2ONBLbv9jAbi1FqLzRObi P7cxJJs7r6lUvmunhrgU4abcu09L5ONJeGByGM/IbxYKH4lzH3FrwATnMCQocpvVpr+e lzRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3a9HyiIRBM4wCH9QPbga5zq5KSGL3bFe4oIwKIdI5E4=; b=v3a0tge5410n52I1aYjujzzpk/SifbArQQ1CkfjpErx2H1yNZQzR5S4M2YP90V0OQh 8+yMdSH1yLeCrdx1mzXvZ09NVojRI8En0AOo2oCXMq2N54yb4vjEe5Qjd0lItexDOlIW 4LVgYM4sH6m5i6z+QAbxEZiLKMje+3J5ZPLf+A79sS2yISZ5QYXyuL2v4tGVfTQwtxQz 19RtZO+m3xQBIsnjG+pk4C1QWLWW23OZQlx6Juqhj9TMuKCPypvbzHzhQU5Lh9IEBIJr Kf7L5gvSQPEw0H1fwnUJjd88po2A18fUvXlWcKAW8xUvJKwaSe1ptGfnUbm3RkIoSQ/w 5XgQ== X-Gm-Message-State: AOAM533LJM1tkxe0yyxrWQGM4ajLlUUg1PiCrjAfbABe+GP4UuAGbgJX oYOL0gLyb0QB5pUzK3ROkjM= X-Received: by 2002:a17:903:496:b0:161:6377:8024 with SMTP id jj22-20020a170903049600b0016163778024mr10803104plb.90.1652752662587; Mon, 16 May 2022 18:57:42 -0700 (PDT) Received: from CHENGDONGLI-MB0.localdomain ([203.205.141.21]) by smtp.gmail.com with ESMTPSA id z4-20020a62d104000000b0050dc7628184sm7708267pfg.94.2022.05.16.18.57.40 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 16 May 2022 18:57:41 -0700 (PDT) From: Chengdong Li X-Google-Original-From: Chengdong Li Received: by CHENGDONGLI-MB0.localdomain (Postfix, from userid 501) id 8C14F808B887; Tue, 17 May 2022 09:57:34 +0800 (CST) To: alexey.v.bayduraev@linux.intel.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, rickyman7@gmail.com, adrian.hunter@intel.com, irogers@google.com, german.gomez@arm.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: ak@linux.intel.com, likexu@tencent.com, chengdongli@tencent.com Subject: [RESEND PATCH v2] perf tools: fix callstack entries and nr print message Date: Tue, 17 May 2022 09:57:26 +0800 Message-Id: <20220517015726.96131-1-chengdongli@tencent.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Chengdong Li when generating callstack information from branch_stack(Intel LBR), the actual number of callstack entry should be bigger than the number of branch_stack, for example: branch_stack records: B() -> C() A() -> B() converted callstack records should be: C() B() A() though, the number of callstack equals to the number of branch stack plus 1. This patch fixes above issue in branch_stack__printf(). For example, # echo 'scale=2000; 4*a(1)' > cmd # perf record --call-graph lbr bc -l < cmd Before applying this patch, `perf script -D` output: 1220022677386876 0x2a40 [0xd8]: PERF_RECORD_SAMPLE(IP, 0x4002): 17990/17990: 0x40a6d6 period: 894172 addr: 0 ... LBR call chain: nr:8 ..... 0: fffffffffffffe00 ..... 1: 000000000040a410 ..... 2: 000000000040573c ..... 3: 0000000000408650 ..... 4: 00000000004022f2 ..... 5: 00000000004015f5 ..... 6: 00007f5ed6dcb553 ..... 7: 0000000000401698 ... FP chain: nr:2 ..... 0: fffffffffffffe00 ..... 1: 000000000040a6d8 ... branch callstack: nr:6 # which is not consistent with LBR records. ..... 0: 000000000040a410 ..... 1: 0000000000408650 # ditto ..... 2: 00000000004022f2 ..... 3: 00000000004015f5 ..... 4: 00007f5ed6dcb553 ..... 5: 0000000000401698 ... thread: bc:17990 ...... dso: /usr/bin/bc bc 17990 1220022.677386: 894172 cycles: 40a410 [unknown] (/usr/bin/bc) 40573c [unknown] (/usr/bin/bc) 408650 [unknown] (/usr/bin/bc) 4022f2 [unknown] (/usr/bin/bc) 4015f5 [unknown] (/usr/bin/bc) 7f5ed6dcb553 __libc_start_main+0xf3 (/usr/lib64/libc-2.17.so) 401698 [unknown] (/usr/bin/bc) After applied: 1220022677386876 0x2a40 [0xd8]: PERF_RECORD_SAMPLE(IP, 0x4002): 17990/17990: 0x40a6d6 period: 894172 addr: 0 ... LBR call chain: nr:8 ..... 0: fffffffffffffe00 ..... 1: 000000000040a410 ..... 2: 000000000040573c ..... 3: 0000000000408650 ..... 4: 00000000004022f2 ..... 5: 00000000004015f5 ..... 6: 00007f5ed6dcb553 ..... 7: 0000000000401698 ... FP chain: nr:2 ..... 0: fffffffffffffe00 ..... 1: 000000000040a6d8 ... branch callstack: nr:7 ..... 0: 000000000040a410 ..... 1: 000000000040573c ..... 2: 0000000000408650 ..... 3: 00000000004022f2 ..... 4: 00000000004015f5 ..... 5: 00007f5ed6dcb553 ..... 6: 0000000000401698 ... thread: bc:17990 ...... dso: /usr/bin/bc bc 17990 1220022.677386: 894172 cycles: 40a410 [unknown] (/usr/bin/bc) 40573c [unknown] (/usr/bin/bc) 408650 [unknown] (/usr/bin/bc) 4022f2 [unknown] (/usr/bin/bc) 4015f5 [unknown] (/usr/bin/bc) 7f5ed6dcb553 __libc_start_main+0xf3 (/usr/lib64/libc-2.17.so) 401698 [unknown] (/usr/bin/bc) Change from v1: - refined code style according to Jiri's review comments. Signed-off-by: Chengdong Li --- tools/perf/util/session.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index f9a320694b85..a7f93f5a1ac8 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1151,9 +1151,20 @@ static void branch_stack__printf(struct perf_sample *sample, bool callstack) struct branch_entry *entries = perf_sample__branch_entries(sample); uint64_t i; - printf("%s: nr:%" PRIu64 "\n", - !callstack ? "... branch stack" : "... branch callstack", - sample->branch_stack->nr); + if (!callstack) { + printf("%s: nr:%" PRIu64 "\n", "... branch stack", sample->branch_stack->nr); + } else { + /* the reason of adding 1 to nr is because after expanding + * branch stack it generates nr + 1 callstack records. e.g., + * B()->C() + * A()->B() + * the final callstack should be: + * C() + * B() + * A() + */ + printf("%s: nr:%" PRIu64 "\n", "... branch callstack", sample->branch_stack->nr+1); + } for (i = 0; i < sample->branch_stack->nr; i++) { struct branch_entry *e = &entries[i]; @@ -1169,8 +1180,13 @@ static void branch_stack__printf(struct perf_sample *sample, bool callstack) (unsigned)e->flags.reserved, e->flags.type ? branch_type_name(e->flags.type) : ""); } else { - printf("..... %2"PRIu64": %016" PRIx64 "\n", - i, i > 0 ? e->from : e->to); + if (i == 0) { + printf("..... %2"PRIu64": %016" PRIx64 "\n" + "..... %2"PRIu64": %016" PRIx64 "\n", + i, e->to, i+1, e->from); + } else { + printf("..... %2"PRIu64": %016" PRIx64 "\n", i+1, e->from); + } } } } -- 2.27.0