Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3539823ybz; Mon, 20 Apr 2020 04:58:17 -0700 (PDT) X-Google-Smtp-Source: APiQypIJsQr6Xk5GIVq0u1jkBAG1h67tqJ+9I2qMOfs9J3zTqfucyF5hw6WVm1/VZkEMhoZF9OUC X-Received: by 2002:a50:ed8f:: with SMTP id h15mr14295213edr.331.1587383897230; Mon, 20 Apr 2020 04:58:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587383897; cv=none; d=google.com; s=arc-20160816; b=tWjZIZo35Q+jlcjJhGnwW8aOECSrT1UTHZt0V5hbjloYQhJbOdzW3CfDeA8yrOViPv xPIGJI6qySprH7c8VbrxpLNqoisioitMPcrL+YA1keZyC9mEIgGAYSghJaSqssoCYtZx dtbilPMnAvsque5cQg7uF+0U1QkNB4PSjw6+urKAwssmWrbndVigI5CJ09o86MVdsIIg Erd9dUzNx5bV3yIhyBAizfz7isMEQrcGBaqP9BIofUpIpl9O8HkTXQ0M87hWrSeqvlVZ MDcl34uOgVGrCWTS4SEErUOS/b6xNy+61qTJjOvXWNQ+JNeEydxdY+yhjgIdlROj4P/t eaAQ== 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=fDr5YRjulXOTgUEIUOg41hpIxraEH5CT1FTT5W+ABiA=; b=rKrUMFjq1xeAmbTRPFjTJlHkdrc98smtv+9fTo71PSUc2hhJMJnw/1s5nCzpyfQSIS UP4o1YghqSvXi5kPK+sGmzwzJ5S6Y6Ia3dh+3m6hfzcYtmqxyZeez+zSaR5u9NCLdGpV K/nRPPOFPZbt9+fFd8n8OwQf7UoqvmGM98alqy0rXO3dP9+mUt8He/gujZG9LArsXOh3 W1s2OVpgkrVoHgmhtvz77TdeCZDRj3lGbbVm6vZ1eelbS09u1Rd27QTNJSr7m6AMKonz ArJiz4jzXwklp8y/JycLUIf5fgsl7+nkZ8cySpiAdfaTd9Fw6x3mr9d4h8al9GYXOVco tXvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=aPSBj6AY; 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 ca25si383568edb.389.2020.04.20.04.57.54; Mon, 20 Apr 2020 04:58:17 -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=aPSBj6AY; 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 S1726999AbgDTL4D (ORCPT + 99 others); Mon, 20 Apr 2020 07:56:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:40274 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726995AbgDTL4A (ORCPT ); Mon, 20 Apr 2020 07:56:00 -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 5B9B822209; Mon, 20 Apr 2020 11:55:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587383760; bh=/LdjocWRJnI0zIamhfwcQJvo4tMtmun37qnKuIK3AC8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aPSBj6AY4WymkgofpKGLafK5xg4vGjucGJy5g1aV2lDZSkVmCEacZtKNpnbrui0+T v3i4u6A62KjAhm+48jB4bQpW4HXkfBRjZhvb+qFwW4nvJyCiOk+GpyXaym4P3iHXRa 5PtVTIdN5prVLDZdLNVJWlKj2Jo+DIuT/JdPDhiA= 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, Adrian Hunter , Andi Kleen , Jiri Olsa , Arnaldo Carvalho de Melo Subject: [PATCH 33/60] perf thread-stack: Add thread_stack__sample_late() Date: Mon, 20 Apr 2020 08:52:49 -0300 Message-Id: <20200420115316.18781-34-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: Adrian Hunter Add a thread stack function to create a call chain for hardware events where the sample records get created some time after the event occurred. Signed-off-by: Adrian Hunter Cc: Andi Kleen Cc: Jiri Olsa Link: http://lore.kernel.org/lkml/20200401101613.6201-10-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/thread-stack.c | 57 ++++++++++++++++++++++++++++++++++ tools/perf/util/thread-stack.h | 3 ++ 2 files changed, 60 insertions(+) diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c index 0885967d5bc3..83f6c83f5617 100644 --- a/tools/perf/util/thread-stack.c +++ b/tools/perf/util/thread-stack.c @@ -497,6 +497,63 @@ void thread_stack__sample(struct thread *thread, int cpu, chain->nr = i; } +/* + * Hardware sample records, created some time after the event occurred, need to + * have subsequent addresses removed from the call chain. + */ +void thread_stack__sample_late(struct thread *thread, int cpu, + struct ip_callchain *chain, size_t sz, + u64 sample_ip, u64 kernel_start) +{ + struct thread_stack *ts = thread__stack(thread, cpu); + u64 sample_context = callchain_context(sample_ip, kernel_start); + u64 last_context, context, ip; + size_t nr = 0, j; + + if (sz < 2) { + chain->nr = 0; + return; + } + + if (!ts) + goto out; + + /* + * When tracing kernel space, kernel addresses occur at the top of the + * call chain after the event occurred but before tracing stopped. + * Skip them. + */ + for (j = 1; j <= ts->cnt; j++) { + ip = ts->stack[ts->cnt - j].ret_addr; + context = callchain_context(ip, kernel_start); + if (context == PERF_CONTEXT_USER || + (context == sample_context && ip == sample_ip)) + break; + } + + last_context = sample_ip; /* Use sample_ip as an invalid context */ + + for (; nr < sz && j <= ts->cnt; nr++, j++) { + ip = ts->stack[ts->cnt - j].ret_addr; + context = callchain_context(ip, kernel_start); + if (context != last_context) { + if (nr >= sz - 1) + break; + chain->ips[nr++] = context; + last_context = context; + } + chain->ips[nr] = ip; + } +out: + if (nr) { + chain->nr = nr; + } else { + chain->ips[0] = sample_context; + chain->ips[1] = sample_ip; + chain->nr = 2; + } +} + struct call_return_processor * call_return_processor__new(int (*process)(struct call_return *cr, u64 *parent_db_id, void *data), void *data) diff --git a/tools/perf/util/thread-stack.h b/tools/perf/util/thread-stack.h index e1ec5a58f1b2..8962ddc4e1ab 100644 --- a/tools/perf/util/thread-stack.h +++ b/tools/perf/util/thread-stack.h @@ -85,6 +85,9 @@ int thread_stack__event(struct thread *thread, int cpu, u32 flags, u64 from_ip, void thread_stack__set_trace_nr(struct thread *thread, int cpu, u64 trace_nr); void thread_stack__sample(struct thread *thread, int cpu, struct ip_callchain *chain, size_t sz, u64 ip, u64 kernel_start); +void thread_stack__sample_late(struct thread *thread, int cpu, + struct ip_callchain *chain, size_t sz, u64 ip, + u64 kernel_start); int thread_stack__flush(struct thread *thread); void thread_stack__free(struct thread *thread); size_t thread_stack__depth(struct thread *thread, int cpu); -- 2.21.1