Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp1639321ybs; Mon, 25 May 2020 23:13:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSh9IhmjgWy4XseTNCXTUKWfZi4rd5Jf4fEnnAUjRgpdWUflAugi1+4cdUB7pdgxkimiNC X-Received: by 2002:aa7:d8c2:: with SMTP id k2mr18642086eds.145.1590473614664; Mon, 25 May 2020 23:13:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590473614; cv=none; d=google.com; s=arc-20160816; b=M3hnbm6rQoq515VfK/VkplPZIuyWdOhfUHJGoDOniDo550IBxEh9V3axxsEvJn9kYx LcrUGcXWrNBftVPjCUT2ZWSiBIK8aL/8SoIMp2hPJopEatpQLldELksPqy/JjQyJm4sE 9Rc7Vu4kycJxZrGWzv+FHHSSqVlyHokTewBl2MJiE3IKL2Dj42XkgOZYZkGRLIp7vpVh akMpjtACg9PQcdxlZcYsmgOeyWsaTAdLI+Y6ngzJ1zlm51JBemsDQ8KAtKLdT9e+7L71 ptAaSE+9sHCgNfPgkMrdKoeTnJGe2AP1leXUop9X09YmtWk8NoVXNk2dTbK3b1uycrAc em1A== 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:ironport-sdr:ironport-sdr; bh=3YV+6iTTI2UO7+cTGGGXyXYJwWvBnTcAYV155oyXil0=; b=lOEfFvFuypVRDQsoSHkXnfAM9gRfaLWJqlXNfrHr9rQ7ECfioja0obVr//kejaNe52 ukwAPsv+IUvWYUfB8JHHDJbMNU20+1yn5m2W4nChFLhY+Jg0EO66MikIBjqwaaNXetwI n/4ieV54GO/4Kv/gd9rAtwv2yM67m0j0gWnV430VBCAJVXklwcDcKPVYdO7o527bgSQ6 cXWMRd/Who2QNre6RJLg1LO8puolQ5vV+PDpaORE74/xUlwzJZL7UKZZHxAZQ1yUX6cv a54SoG9qFqQNhpZmEP93iLo8zObCx0Yj+0/SJhRQhuCennw45hJZRVonJ4nAI2vkdqwk d+Dw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a7si10193568edb.445.2020.05.25.23.13.12; Mon, 25 May 2020 23:13:34 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727088AbgEZGL3 (ORCPT + 99 others); Tue, 26 May 2020 02:11:29 -0400 Received: from mga06.intel.com ([134.134.136.31]:16985 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726207AbgEZGL2 (ORCPT ); Tue, 26 May 2020 02:11:28 -0400 IronPort-SDR: tokKTa/nPBQ08BmOTp0I76cQsaRHm8q9ZUnlJ0sIALhx/yjJnpr52SDGtjgSzIWefWt8Nv5DY1 1IKNddepQEsA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2020 23:11:28 -0700 IronPort-SDR: 6is0HbyNULswCI42e7TELqib1CrF2IGpkWI7iqkFeAoQ2YsshGl4Y6HpUDvF5IG5p95DkWeqgO 9z/RWXEC9TfA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,436,1583222400"; d="scan'208";a="291068646" Received: from kbl-ppc.sh.intel.com ([10.239.159.118]) by fmsmga004.fm.intel.com with ESMTP; 25 May 2020 23:11:25 -0700 From: Jin Yao To: acme@kernel.org, jolsa@kernel.org, peterz@infradead.org, mingo@redhat.com, alexander.shishkin@linux.intel.com Cc: Linux-kernel@vger.kernel.org, ak@linux.intel.com, kan.liang@intel.com, yao.jin@intel.com, Jin Yao Subject: [PATCH v4 1/7] perf util: Create streams Date: Tue, 26 May 2020 14:09:14 +0800 Message-Id: <20200526060920.26490-2-yao.jin@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200526060920.26490-1-yao.jin@linux.intel.com> References: <20200526060920.26490-1-yao.jin@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We define the stream is the branch history which is aggregated by the branch records from perf samples. For example, the callchains aggregated from the branch records. By browsing the hot stream, we can understand the hot code path. Now we only support the callchain. So for measuring the hot level for a stream, we use the callchain_node->hit, higher is hotter. There may be many callchains sampled so we only focus on top N hottest callchains. N is a user defined parameter or predefined default value (nr_streams_max). This patch creates an evsel_streams array per event, and saves top N hottest streams in a stream array. So now we can get the per-event top N hottest streams. Signed-off-by: Jin Yao --- tools/perf/util/Build | 1 + tools/perf/util/stream.c | 152 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/stream.h | 30 ++++++++ 3 files changed, 183 insertions(+) create mode 100644 tools/perf/util/stream.c create mode 100644 tools/perf/util/stream.h diff --git a/tools/perf/util/Build b/tools/perf/util/Build index 880fcdd1ab11..3bfcdcb3fed2 100644 --- a/tools/perf/util/Build +++ b/tools/perf/util/Build @@ -101,6 +101,7 @@ perf-y += call-path.o perf-y += rwsem.o perf-y += thread-stack.o perf-y += spark.o +perf-y += stream.o perf-$(CONFIG_AUXTRACE) += auxtrace.o perf-$(CONFIG_AUXTRACE) += intel-pt-decoder/ perf-$(CONFIG_AUXTRACE) += intel-pt.o diff --git a/tools/perf/util/stream.c b/tools/perf/util/stream.c new file mode 100644 index 000000000000..327a00c27ae2 --- /dev/null +++ b/tools/perf/util/stream.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Compare and figure out the top N hottest streams + * Copyright (c) 2020, Intel Corporation. + * Author: Jin Yao + */ + +#include +#include +#include +#include "debug.h" +#include "hist.h" +#include "sort.h" +#include "stream.h" +#include "evlist.h" + +static void free_evsel_streams(struct evsel_streams *es, int nr_evsel) +{ + for (int i = 0; i < nr_evsel; i++) + zfree(&es[i].streams); + + free(es); +} + +static struct evsel_streams *create_evsel_streams(int nr_evsel, + int nr_streams_max) +{ + struct evsel_streams *es; + + es = calloc(nr_evsel, sizeof(struct evsel_streams)); + if (!es) + return NULL; + + for (int i = 0; i < nr_evsel; i++) { + struct evsel_streams *s = &es[i]; + + s->streams = calloc(nr_streams_max, sizeof(struct stream)); + if (!s->streams) + goto err; + + s->nr_streams_max = nr_streams_max; + s->evsel_idx = -1; + } + + return es; + +err: + free_evsel_streams(es, nr_evsel); + return NULL; +} + +/* + * The cnodes with high hit number are hot callchains. + */ +static void set_hot_cnode(struct evsel_streams *es, + struct callchain_node *cnode) +{ + int i, idx = 0; + u64 hit; + + if (es->nr_streams < es->nr_streams_max) { + i = es->nr_streams; + es->streams[i].cnode = cnode; + es->nr_streams++; + return; + } + + /* + * Considering a few number of hot streams, only use simple + * way to find the cnode with smallest hit number and replace. + */ + hit = (es->streams[0].cnode)->hit; + for (i = 1; i < es->nr_streams; i++) { + if ((es->streams[i].cnode)->hit < hit) { + hit = (es->streams[i].cnode)->hit; + idx = i; + } + } + + if (cnode->hit > hit) + es->streams[idx].cnode = cnode; +} + +static void update_hot_callchain(struct hist_entry *he, + struct evsel_streams *es) +{ + struct rb_root *root = &he->sorted_chain; + struct rb_node *rb_node = rb_first(root); + struct callchain_node *cnode; + + while (rb_node) { + cnode = rb_entry(rb_node, struct callchain_node, rb_node); + set_hot_cnode(es, cnode); + rb_node = rb_next(rb_node); + } +} + +static void init_hot_callchain(struct hists *hists, struct evsel_streams *es) +{ + struct rb_node *next = rb_first_cached(&hists->entries); + + while (next) { + struct hist_entry *he; + + he = rb_entry(next, struct hist_entry, rb_node); + update_hot_callchain(he, es); + next = rb_next(&he->rb_node); + } +} + +static int evlist_init_callchain_streams(struct evlist *evlist, + struct evsel_streams *es, int nr_es) +{ + struct evsel *pos; + int i = 0; + + BUG_ON(nr_es < evlist->core.nr_entries); + + evlist__for_each_entry(evlist, pos) { + struct hists *hists = evsel__hists(pos); + + hists__output_resort(hists, NULL); + init_hot_callchain(hists, &es[i]); + es[i].evsel_idx = pos->idx; + es[i].stream_type = STREAM_CALLCHAIN; + i++; + } + + return 0; +} + +struct evsel_streams *perf_evlist__create_streams(struct evlist *evlist, + int nr_streams_max, + enum stream_type type) +{ + struct evsel_streams *es; + int nr_evsel = evlist->core.nr_entries, ret = -1; + + es = create_evsel_streams(nr_evsel, nr_streams_max); + if (!es) + return NULL; + + if (type == STREAM_CALLCHAIN) + ret = evlist_init_callchain_streams(evlist, es, nr_evsel); + + if (ret) { + free_evsel_streams(es, nr_evsel); + return NULL; + } + + return es; +} diff --git a/tools/perf/util/stream.h b/tools/perf/util/stream.h new file mode 100644 index 000000000000..a8a0172b4d13 --- /dev/null +++ b/tools/perf/util/stream.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __PERF_STREAM_H +#define __PERF_STREAM_H + +#include "callchain.h" + +enum stream_type { + STREAM_NONE = 0, + STREAM_CALLCHAIN +}; + +struct stream { + struct callchain_node *cnode; +}; + +struct evsel_streams { + struct stream *streams; + enum stream_type stream_type; + int nr_streams_max; + int nr_streams; + int evsel_idx; +}; + +struct evlist; + +struct evsel_streams *perf_evlist__create_streams(struct evlist *evlist, + int nr_streams_max, + enum stream_type type); + +#endif /* __PERF_STREAM_H */ -- 2.17.1