From patchwork Mon Jan 15 18:13:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Poirier X-Patchwork-Id: 124551 Delivered-To: patch@linaro.org Received: by 10.46.64.148 with SMTP id r20csp752000lje; Mon, 15 Jan 2018 10:14:15 -0800 (PST) X-Google-Smtp-Source: ACJfBoteRrDMexkRJ02FykkHVQiifYUZfQvm0b1BOaqcrc0YWeZ4rrxXd6dZ7iEjG0UZOCCJfS4P X-Received: by 10.84.248.68 with SMTP id e4mr36103707pln.296.1516040055014; Mon, 15 Jan 2018 10:14:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516040055; cv=none; d=google.com; s=arc-20160816; b=jIm5vSGX5x8nGttx4fudpDxgnqUbkur88S+aT1l/Yj8M5PDfw1/s8prKC/U4zBtM5z 5nKFQ0IIR63FSVDH11ir5SEaz5EgaBsUZnwBBOZvrQtANjmc9madSy/Vf0vA9Sj/733v rwJi1Sj1nHrDpTSCCO9oLxWwzByixicWAJsq8zC3ID68C2D/sn8SUz+qS8Jclhrrg61T LeJq5xjkXBotOj3rrXTj8tRKIe0u55FEhPwqxeikysTWzuSYv5uZy0dfrvIJzrAWeYLn 5ZyiitU1RvxW9YaS9ClPMXNxYBfQfBkNgSwc303N86YfHfKqQKUIjOkn/8CddPOwo84B tP3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=A6W1XtJQdjoZR9NsZdSVjX0dAYL8NIBU+4g7IS13xkg=; b=TIER86s3k+tG66RKr82lEoZQLx3IsQfH5BHkOG9u09WRTmgfZKFiPKHs5l3RBF/zGO F2OB8m4AGYTzvuphzo+7s9/ATDWDwKi2tGeVXaxcTKtcl/RjB9d2H/JwQSnMFQSz6cUr fk+Zc8JyeQLbRqxOBARNWRTvsogg+pT9AqMbfGvmOmRBLGl625H2BbW8Sy2I9r/OXAl9 vLUBnZ+5VlHjA0zuMT96MtAW441QiQdKt3cFNexndOAdCuN1/v62tK2wuUTyGH14buL5 weFg7Lp/pcJ6W5b4YBeRK5aeS7qYR7bmUXYFTuKuz6GsyvcZwvm71E7kZwHVJba2mLmb /lBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ezer++od; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v8si135030pgt.503.2018.01.15.10.14.14; Mon, 15 Jan 2018 10:14:14 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ezer++od; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751398AbeAOSON (ORCPT + 28 others); Mon, 15 Jan 2018 13:14:13 -0500 Received: from mail-io0-f175.google.com ([209.85.223.175]:41938 "EHLO mail-io0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750798AbeAOSNi (ORCPT ); Mon, 15 Jan 2018 13:13:38 -0500 Received: by mail-io0-f175.google.com with SMTP id f6so13955544ioh.8 for ; Mon, 15 Jan 2018 10:13:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=A6W1XtJQdjoZR9NsZdSVjX0dAYL8NIBU+4g7IS13xkg=; b=Ezer++odgTbn2GqX1jtm4s6zxTmcBO0Uvtz+2B8tZKPzmBrc7vTkJUAubUupn+HsTI JvHIIWKfb9KEtHMNb6LOOPw1+dAz7WSBUplVJggDsOjU/Ydax2623LceRmHUBwDJLrEZ Wkj3W0sNLpq3KYi7nVfCtOjB1BG0LmfE2TotY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=A6W1XtJQdjoZR9NsZdSVjX0dAYL8NIBU+4g7IS13xkg=; b=sYjv4pTOggDIrvK0sjQNDrlupZEpUuGX55uiMdzUwlxS4ZfhOY/HqgbLsRIX3H13hq 0YwyXx84EolB6m1Cq3FXKxdVYTcBJEw8R0L5UY1fYyWvC9fACVRrb06a17nopnoPc9iG ajG5itkI5KEAmGC2AaWnpMaUVKiii+FMsufZsuJxgA+MUiWP+yEHet4ECVp5HrRSolN0 qT7xXxFiJTTZVlOErs7xUXgOf3cyZZqJsPgJoESX5KZ8wHpeB5oCdofZXwff5j1p6HqA aETITeKLPEMgnY1UB9yqgrBNDEbJeqlmM5X2ABntFNX+czVer1qbGnsTRyrCUys1+r77 jPQA== X-Gm-Message-State: AKwxytelTByThKEIx+K3ywkF7RhcREjINHBJQrKw2PtLuFODKeHCiwi3 r3VLbqJxcRRuU+Wr2EQfbew12w== X-Received: by 10.107.8.25 with SMTP id 25mr4586839ioi.167.1516040017556; Mon, 15 Jan 2018 10:13:37 -0800 (PST) Received: from xps15.cg.shawcable.net (S0106002369de4dac.cg.shawcable.net. [68.147.8.254]) by smtp.gmail.com with ESMTPSA id w142sm231480ita.25.2018.01.15.10.13.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Jan 2018 10:13:36 -0800 (PST) From: Mathieu Poirier To: acme@kernel.org Cc: peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com, namhyung@kernel.org, adrian.hunter@intel.com, mike.leach@arm.com, suzuki.poulosi@arm.com, jolsa@redhat.com, kim.phillips@arm.com, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v2 09/10] perf tools: Add mechanic to synthesise CoreSight trace packets Date: Mon, 15 Jan 2018 11:13:13 -0700 Message-Id: <1516039994-314-10-git-send-email-mathieu.poirier@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1516039994-314-1-git-send-email-mathieu.poirier@linaro.org> References: <1516039994-314-1-git-send-email-mathieu.poirier@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Once decoded from trace packets information on trace range needs to be communicated to the perf synthesis infrastructure so that it is available to the perf tools built-in rendering tools and scripts. Co-authored-by: Tor Jeremiassen Signed-off-by: Mathieu Poirier --- tools/perf/util/cs-etm.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) -- 2.7.4 diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 407095af1456..b9f0a53dfa65 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -453,6 +453,157 @@ static void cs_etm__set_pid_tid_cpu(struct cs_etm_auxtrace *etm, } } +/* + * The cs etm packet encodes an instruction range between a branch target + * and the next taken branch. Generate sample accordingly. + */ +static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, + struct cs_etm_packet *packet) +{ + int ret = 0; + struct cs_etm_auxtrace *etm = etmq->etm; + struct perf_sample sample = {.ip = 0,}; + union perf_event *event = etmq->event_buf; + u64 start_addr = packet->start_addr; + u64 end_addr = packet->end_addr; + + event->sample.header.type = PERF_RECORD_SAMPLE; + event->sample.header.misc = PERF_RECORD_MISC_USER; + event->sample.header.size = sizeof(struct perf_event_header); + + sample.ip = start_addr; + sample.pid = etmq->pid; + sample.tid = etmq->tid; + sample.addr = end_addr; + sample.id = etmq->etm->branches_id; + sample.stream_id = etmq->etm->branches_id; + sample.period = 1; + sample.cpu = packet->cpu; + sample.flags = 0; + sample.cpumode = PERF_RECORD_MISC_USER; + + ret = perf_session__deliver_synth_event(etm->session, event, &sample); + + if (ret) + pr_err( + "CS ETM Trace: failed to deliver instruction event, error %d\n", + ret); + + return ret; +} + +struct cs_etm_synth { + struct perf_tool dummy_tool; + struct perf_session *session; +}; + +static int cs_etm__event_synth(struct perf_tool *tool, + union perf_event *event, + struct perf_sample *sample __maybe_unused, + struct machine *machine __maybe_unused) +{ + struct cs_etm_synth *cs_etm_synth = + container_of(tool, struct cs_etm_synth, dummy_tool); + + return perf_session__deliver_synth_event(cs_etm_synth->session, + event, NULL); +} + +static int cs_etm__synth_event(struct perf_session *session, + struct perf_event_attr *attr, u64 id) +{ + struct cs_etm_synth cs_etm_synth; + + memset(&cs_etm_synth, 0, sizeof(struct cs_etm_synth)); + cs_etm_synth.session = session; + + return perf_event__synthesize_attr(&cs_etm_synth.dummy_tool, attr, 1, + &id, cs_etm__event_synth); +} + +static int cs_etm__synth_events(struct cs_etm_auxtrace *etm, + struct perf_session *session) +{ + struct perf_evlist *evlist = session->evlist; + struct perf_evsel *evsel; + struct perf_event_attr attr; + bool found = false; + u64 id; + int err; + + evlist__for_each_entry(evlist, evsel) { + if (evsel->attr.type == etm->pmu_type) { + found = true; + break; + } + } + + if (!found) { + pr_debug("No selected events with CoreSight Trace data\n"); + return 0; + } + + memset(&attr, 0, sizeof(struct perf_event_attr)); + attr.size = sizeof(struct perf_event_attr); + attr.type = PERF_TYPE_HARDWARE; + attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK; + attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | + PERF_SAMPLE_PERIOD; + if (etm->timeless_decoding) + attr.sample_type &= ~(u64)PERF_SAMPLE_TIME; + else + attr.sample_type |= PERF_SAMPLE_TIME; + + attr.exclude_user = evsel->attr.exclude_user; + attr.exclude_kernel = evsel->attr.exclude_kernel; + attr.exclude_hv = evsel->attr.exclude_hv; + attr.exclude_host = evsel->attr.exclude_host; + attr.exclude_guest = evsel->attr.exclude_guest; + attr.sample_id_all = evsel->attr.sample_id_all; + attr.read_format = evsel->attr.read_format; + + /* create new id val to be a fixed offset from evsel id */ + id = evsel->id[0] + 1000000000; + + if (!id) + id = 1; + + if (etm->synth_opts.branches) { + attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; + attr.sample_period = 1; + attr.sample_type |= PERF_SAMPLE_ADDR; + err = cs_etm__synth_event(session, &attr, id); + if (err) + return err; + etm->sample_branches = true; + etm->branches_sample_type = attr.sample_type; + etm->branches_id = id; + } + + return 0; +} + +static int cs_etm__sample(struct cs_etm_queue *etmq) +{ + int ret; + struct cs_etm_packet packet; + + while (1) { + ret = cs_etm_decoder__get_packet(etmq->decoder, &packet); + if (ret <= 0) + return ret; + + /* + * If the packet contains an instruction range, generate an + * instruction sequence event. + */ + if (packet.sample_type & CS_ETM_RANGE) + cs_etm__synth_branch_sample(etmq, &packet); + } + + return 0; +} + static int cs_etm__run_decoder(struct cs_etm_queue *etmq) { struct cs_etm_auxtrace *etm = etmq->etm; @@ -494,6 +645,12 @@ static int cs_etm__run_decoder(struct cs_etm_queue *etmq) etmq->offset += processed; buffer_used += processed; + + /* + * Nothing to do with an error condition, let's hope the next + * chunk will be better. + */ + err = cs_etm__sample(etmq); } while (buffer.len > buffer_used); goto more; @@ -828,6 +985,17 @@ int cs_etm__process_auxtrace_info(union perf_event *event, return 0; } + if (session->itrace_synth_opts && session->itrace_synth_opts->set) { + etm->synth_opts = *session->itrace_synth_opts; + } else { + itrace_synth_opts__set_default(&etm->synth_opts); + etm->synth_opts.callchain = false; + } + + err = cs_etm__synth_events(etm, session); + if (err) + goto err_free_queues; + err = auxtrace_queues__process_index(&etm->queues, session); if (err) goto err_free_queues;