Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3540667ybz; Mon, 20 Apr 2020 04:59:21 -0700 (PDT) X-Google-Smtp-Source: APiQypI7nTIHPOw9DStqxS8yBRF3blIKmzdMwPFApfLg9ty19CMuFbydNXMmrKqV3LnmhBrAgdUG X-Received: by 2002:aa7:d4d3:: with SMTP id t19mr13355439edr.144.1587383961042; Mon, 20 Apr 2020 04:59:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587383961; cv=none; d=google.com; s=arc-20160816; b=Of7WXzxPWUkisHYuXHOOGCSE7iHVIdwEBV6oVhRfXoIdEo0B3wzB6pDFAQm5scjKS8 Sj/WHzNUGCjkkar7DcOotY5D2+mBITHKw8Gf26nG6X/IrcnkSGl9QocSAuUZSlgcePYZ g9Sr9mBDmGuoU7XZW8q+FdiIldDunThRu23hgsPmM7CcPJngiljOi7zg7bXu2VGCvyBH 9vIZi/TROmdk+kxzCooc1Vje2bQQjlDEXcxF9W8RjMAPyZQhYkBm1VBGphoDKWy5F/HH vAtqU1HzosBhAQyXdpzMwiqTWrVT/bjwky4Y4vprzNbg1jVSbB0dUqE9ru62dhw8s1Ps 699Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jbciz631Tg90OsTQQ0erDyslwFcmwLt6WxXSuc2vE0M=; b=n5kMzkZF1LCwJP9228zeqBscwj/Q5f6CgaHwMq/KfTtvFp/k2S1W3LSNh8tPv9KG8B sTbB44tKFho42pGUPvpZMGRN4n2Qiai9q7tgrx678ZcvzDOFSngMz88lho2AmqF8CIFt WVebW0F9u8PIr0EqEu85syasRKK1CIBF0Jgm/dmTZTF5KYUND8EOMt+n7Se+fv0Fyt9Q L7xxIfksZEsOOPoWA2I4S8XrvED69gFKyrgfMsIN6ePk0SHIC9gUBR7EhhkTrZh9bQlV 731p8y3i6+NCU4CwVTKI2RK/uyVsNJmsSguIT5r3dXcFkp59TqznfX5vgRYvaSjGzjKv SI/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=S1S4f1RT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id di17si384491edb.593.2020.04.20.04.58.58; Mon, 20 Apr 2020 04:59:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=S1S4f1RT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726381AbgDTL5G (ORCPT + 99 others); Mon, 20 Apr 2020 07:57:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:41586 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726402AbgDTL5E (ORCPT ); Mon, 20 Apr 2020 07:57:04 -0400 Received: from quaco.ghostprotocols.net (unknown [179.97.37.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0F61D20857; Mon, 20 Apr 2020 11:56:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587383823; bh=sEL+5jzjb+p4Og2B/stXdQpoK2rzusgjEidnB5s45d0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S1S4f1RTgHK44k/eoOcKAWoOt7N/mds5IUGGrDR/XbLPSRSOb40FMSoLCntYKvd6J EAn9IngsRYbPx/saQxH17xH0rys0NFKp9A7XKwC7JsqfefcDFIXpTKDZe/TLnnqgkp 0SgMnMFXWmeUu7ubyBGbJcFs+cQu8wGPlStpLpUc= From: Arnaldo Carvalho de Melo To: Ingo Molnar , Thomas Gleixner Cc: Jiri Olsa , Namhyung Kim , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Kan Liang , Andi Kleen , Jiri Olsa , Adrian Hunter , Alexey Budankov , Mathieu Poirier , Michael Ellerman , Pavel Gerasimov , Peter Zijlstra , Ravi Bangoria , Stephane Eranian , Vitaly Slobodskoy , Arnaldo Carvalho de Melo Subject: [PATCH 49/60] perf machine: Refine the function for LBR call stack reconstruction Date: Mon, 20 Apr 2020 08:53:05 -0300 Message-Id: <20200420115316.18781-50-acme@kernel.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200420115316.18781-1-acme@kernel.org> References: <20200420115316.18781-1-acme@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang LBR only collect the user call stack. To reconstruct a call stack, both kernel call stack and user call stack are required. The function resolve_lbr_callchain_sample() mix the kernel call stack and user call stack. Now, with the help of HW idx, perf tool can reconstruct a more complete call stack by adding some user call stack from previous sample. However, current implementation is hard to be extended to support it. Current code path for resolve_lbr_callchain_sample() for (j = 0; j < mix_chain_nr; j++) { if (ORDER_CALLEE) { if (kernel callchain) Fill callchain info else if (LBR callchain) Fill callchain info } else { if (LBR callchain) Fill callchain info else if (kernel callchain) Fill callchain info } add_callchain_ip(); } With the patch, if (ORDER_CALLEE) { for (j = 0; j < NUM of kernel callchain) { Fill callchain info add_callchain_ip(); } for (; j < mix_chain_nr) { Fill callchain info add_callchain_ip(); } } else { for (; j < NUM of LBR callchain) { Fill callchain info add_callchain_ip(); } for (j = 0; j < mix_chain_nr) { Fill callchain info add_callchain_ip(); } } No functional changes. Signed-off-by: Kan Liang Reviewed-by: Andi Kleen Acked-by: Jiri Olsa Cc: Adrian Hunter Cc: Alexey Budankov Cc: Mathieu Poirier Cc: Michael Ellerman Cc: Namhyung Kim Cc: Pavel Gerasimov Cc: Peter Zijlstra Cc: Ravi Bangoria Cc: Stephane Eranian Cc: Vitaly Slobodskoy Link: http://lore.kernel.org/lkml/20200319202517.23423-7-kan.liang@linux.intel.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/machine.c | 111 ++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 35 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index be1bd9277471..0da540e6f803 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -2214,6 +2214,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread, bool branch; struct branch_flags *flags; int mix_chain_nr; + int err; for (i = 0; i < chain_nr; i++) { if (chain->ips[i] == PERF_CONTEXT_USER) @@ -2239,50 +2240,90 @@ static int resolve_lbr_callchain_sample(struct thread *thread, */ mix_chain_nr = i + 1 + lbr_nr + 1; - for (j = 0; j < mix_chain_nr; j++) { - int err; - - branch = false; - flags = NULL; - - if (callchain_param.order == ORDER_CALLEE) { - if (j < i + 1) - ip = chain->ips[j]; - else if (j > i + 1) { - k = j - i - 2; - ip = entries[k].from; - branch = true; - flags = &entries[k].flags; - } else { - ip = entries[0].to; - branch = true; - flags = &entries[0].flags; - branch_from = entries[0].from; - } - } else { - if (j < lbr_nr) { - k = lbr_nr - j - 1; - ip = entries[k].from; - branch = true; - flags = &entries[k].flags; - } else if (j > lbr_nr) - ip = chain->ips[i + 1 - (j - lbr_nr)]; - else { - ip = entries[0].to; - branch = true; - flags = &entries[0].flags; - branch_from = entries[0].from; - } + if (callchain_param.order == ORDER_CALLEE) { + /* Add kernel ip */ + for (j = 0; j < i + 1; j++) { + ip = chain->ips[j]; + branch = false; + flags = NULL; + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + branch, flags, NULL, + branch_from); + if (err) + goto error; } + /* Add LBR ip from first entries.to */ + ip = entries[0].to; + branch = true; + flags = &entries[0].flags; + branch_from = entries[0].from; + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + branch, flags, NULL, + branch_from); + if (err) + goto error; + /* Add LBR ip from entries.from one by one. */ + for (j = i + 2; j < mix_chain_nr; j++) { + k = j - i - 2; + ip = entries[k].from; + branch = true; + flags = &entries[k].flags; + + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + branch, flags, NULL, + branch_from); + if (err) + goto error; + } + } else { + /* Add LBR ip from entries.from one by one. */ + for (j = 0; j < lbr_nr; j++) { + k = lbr_nr - j - 1; + ip = entries[k].from; + branch = true; + flags = &entries[k].flags; + + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + branch, flags, NULL, + branch_from); + if (err) + goto error; + } + + /* Add LBR ip from first entries.to */ + ip = entries[0].to; + branch = true; + flags = &entries[0].flags; + branch_from = entries[0].from; err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip, branch, flags, NULL, branch_from); if (err) - return (err < 0) ? err : 0; + goto error; + + /* Add kernel ip */ + for (j = lbr_nr + 1; j < mix_chain_nr; j++) { + ip = chain->ips[i + 1 - (j - lbr_nr)]; + branch = false; + flags = NULL; + err = add_callchain_ip(thread, cursor, parent, + root_al, &cpumode, ip, + branch, flags, NULL, + branch_from); + if (err) + goto error; + } } return 1; + +error: + return (err < 0) ? err : 0; } static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread, -- 2.21.1