Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1357051ybj; Thu, 7 May 2020 22:39:20 -0700 (PDT) X-Google-Smtp-Source: APiQypI4AUGBJvU4Qn5D0LKzI8m4mtOnQsVhX4ViB/puA9JC5vqC7Ya5YTFzZ1y6+PHcXkbLDFix X-Received: by 2002:a05:6402:688:: with SMTP id f8mr723901edy.233.1588916360422; Thu, 07 May 2020 22:39:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588916360; cv=none; d=google.com; s=arc-20160816; b=rFFd3ibjq8Eu8Tq1ereQjp6mgqca98kV5jfE8igtl6rMSEJ2wug8d2/kOXubZ7VTWa eDqb6U1wHhBl7kjs2Qi8kTWILT3ZdqGfym7hWqWDsL9SPuhDQh0vbTAQ9W+cfhmOBDwg Qs/86FawmvNPT6FoC3Ca3HwrDx96tG8atJ1LVoRCZepw57YWvTix1FqpdI8SUmlFJN6M EjakpY8n5WOD8a4GM4iQq5GxF0dWS74n5mSG2tCLNB7ccBRxAo/lYzaSspFHRvDqezCL 6lRklMX2vGvWKiWJMQHa5X7URDA6qjFZGqF4SmhpHi3p66BaDA6QwKsXXp4QN5CgQvt4 7Krw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=NI47XF21ZdHZtTwg9U3Xu5hLQ+KgyT9MTAu+sQz/TqI=; b=grOZ21GYMXdt3f8beqtW8bKzHHgs1fJvVQ9ny2eTqHciPhxOqd0Y8w8qpHSmwfZBfS i5jJtQfG6/oRp42CRa9/5qZWUafxzsAjDnOfpVLZpynUiwAWcX725nZ/P9EhWfFHoDG7 W7uQk4AP9Ny376b4J4YF+5q3y/+SzJO1qNYXUBdSaYlMGF1EKQS9bqKgxGaRdoXqW5Hg M1dSJJwI8i9oboIlDZRO63JQbY2YBmAJFsE7+Kl4n0lluV5xhBL9mE9VmcZN5XAKYhvT NMjNfBAILjQmj5zDm3LOmpuDmhzuPpkjcQXBe6bw3aibgf+1TTmrQvHywRfTq641pOtl igYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dzEo1npF; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w21si342835edx.313.2020.05.07.22.38.58; Thu, 07 May 2020 22:39:20 -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=@google.com header.s=20161025 header.b=dzEo1npF; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727893AbgEHFhS (ORCPT + 99 others); Fri, 8 May 2020 01:37:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727828AbgEHFg6 (ORCPT ); Fri, 8 May 2020 01:36:58 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88712C05BD43 for ; Thu, 7 May 2020 22:36:58 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id z11so807926ybk.2 for ; Thu, 07 May 2020 22:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=NI47XF21ZdHZtTwg9U3Xu5hLQ+KgyT9MTAu+sQz/TqI=; b=dzEo1npFQYDAAAa2R9qUj9LJDb4+qu9189mY1rhyIeoq+5C1/fZvUF3sIkiIKlpd4D GvmaBTO9offX6hAyEYsH5GfCUOW6d+FIXSo3oCT5I39Q4so7fbqZxDSx34LhVMhWUD1I ZUMOOkM26v0UdIT4X9X0u0dsJ+4roOqFBZ2h0/NAV4CaENVLyJUM7ZGH64kbWPerxbqb rkcXtvmAJa3gFY7sNrJtRoIEsoA5vqJT2dxWOjtDdLNn9RdqGGowTqycTeZBNlJMh7Zu cKt1DlKWYZIwKi7saZZvLxUZ8KCr/sPOXXRzhT3R30Wn0nEpamC77Jk79Sp/qqPDMNjY 2tCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=NI47XF21ZdHZtTwg9U3Xu5hLQ+KgyT9MTAu+sQz/TqI=; b=eMmhLePkVPxN84r2igNeC+hbz3njzNCEmEAM5nvOhXUSJmbw0zytxnkTiUVL6QHhbx Oa5EMBkAOf/r2COow9CFxl6h17GmJRrDi5I22/8jMDVWn9G1Oj8xbNpoqrZ6cPfVOL/P NY9S4VuqcjGbQEsvmrD3l+TVYTXJ9+ISNTJBaE760/fgSblyezLTMILbhZbl+q+Pu5/0 FC+tjTUN3T6wDdUkjhcXMB2sCWyHVKxtAOYNlu1cd39daN5V5QVG6p4sDEs1J1eB2AgE ejGze5t8y7J+qa560GuRanECLOCsVqzCopDyvQd9rfODREOiW31OeG9Bhzu1V7MLX2AX +roQ== X-Gm-Message-State: AGi0PuaJJTbveqTbZYJ6fnSK2O82daRqBeNk1z0BfjQe+wXYLzZVqEAN cmaTH/608RfN9aX7IT5FnyplLymq/zj8 X-Received: by 2002:a25:d408:: with SMTP id m8mr1938204ybf.336.1588916217716; Thu, 07 May 2020 22:36:57 -0700 (PDT) Date: Thu, 7 May 2020 22:36:28 -0700 In-Reply-To: <20200508053629.210324-1-irogers@google.com> Message-Id: <20200508053629.210324-14-irogers@google.com> Mime-Version: 1.0 References: <20200508053629.210324-1-irogers@google.com> X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [RFC PATCH v3 13/14] perf metricgroup: remove duped metric group events From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , Kajol Jain , Andi Kleen , John Garry , Jin Yao , Kan Liang , Cong Wang , Kim Phillips , linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, Vince Weaver , Stephane Eranian , Ian Rogers Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A metric group contains multiple metrics. These metrics may use the same events. If metrics use separate events then it leads to more multiplexing and overall metric counts fail to sum to 100%. Modify how metrics are associated with events so that if the events in an earlier group satisfy the current metric, the same events are used. A record of used events is kept and at the end of processing unnecessary events are eliminated. Before: $ perf stat -a -M TopDownL1 sleep 1 Performance counter stats for 'system wide': 920,211,343 uops_issued.any # 0.5 Backend_Bound (16.56%) 1,977,733,128 idq_uops_not_delivered.core (16.56%) 51,668,510 int_misc.recovery_cycles (16.56%) 732,305,692 uops_retired.retire_slots (16.56%) 1,497,621,849 cycles (16.56%) 721,098,274 uops_issued.any # 0.1 Bad_Speculation (16.79%) 1,332,681,791 cycles (16.79%) 552,475,482 uops_retired.retire_slots (16.79%) 47,708,340 int_misc.recovery_cycles (16.79%) 1,383,713,292 cycles # 0.4 Frontend_Bound (16.76%) 2,013,757,701 idq_uops_not_delivered.core (16.76%) 1,373,363,790 cycles # 0.1 Retiring (33.54%) 577,302,589 uops_retired.retire_slots (33.54%) 392,766,987 inst_retired.any # 0.3 IPC (50.24%) 1,351,873,350 cpu_clk_unhalted.thread (50.24%) 1,332,510,318 cycles # 5330041272.0 SLOTS (49.90%) 1.006336145 seconds time elapsed After: $ perf stat -a -M TopDownL1 sleep 1 Performance counter stats for 'system wide': 765,949,145 uops_issued.any # 0.1 Bad_Speculation # 0.5 Backend_Bound (50.09%) 1,883,830,591 idq_uops_not_delivered.core # 0.3 Frontend_Bound (50.09%) 48,237,080 int_misc.recovery_cycles (50.09%) 581,798,385 uops_retired.retire_slots # 0.1 Retiring (50.09%) 1,361,628,527 cycles # 5446514108.0 SLOTS (50.09%) 391,415,714 inst_retired.any # 0.3 IPC (49.91%) 1,336,486,781 cpu_clk_unhalted.thread (49.91%) 1.005469298 seconds time elapsed Note: Bad_Speculation + Backend_Bound + Frontend_Bound + Retiring = 100% after, where as before it is 110%. After there are 2 groups, whereas before there are 6. After the cycles event appears once, before it appeared 5 times. Signed-off-by: Ian Rogers --- tools/perf/util/metricgroup.c | 96 ++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c index 69fbff47089f..c97dc87c2a31 100644 --- a/tools/perf/util/metricgroup.c +++ b/tools/perf/util/metricgroup.c @@ -93,45 +93,72 @@ struct egroup { bool has_constraint; }; +/** + * Find a group of events in perf_evlist that correpond to those from a parsed + * metric expression. + * @perf_evlist: a list of events something like: {metric1 leader, metric1 + * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling, + * metric2 sibling}:W,duration_time + * @pctx: the parse context for the metric expression. + * @has_constraint: is there a contraint on the group of events? In which case + * the events won't be grouped. + * @metric_events: out argument, null terminated array of evsel's associated + * with the metric. + * @evlist_used: in/out argument, bitmap tracking which evlist events are used. + * @return the first metric event or NULL on failure. + */ static struct evsel *find_evsel_group(struct evlist *perf_evlist, struct expr_parse_ctx *pctx, + bool has_constraint, struct evsel **metric_events, unsigned long *evlist_used) { - struct evsel *ev; - bool leader_found; - const size_t idnum = hashmap__size(&pctx->ids); - size_t i = 0; - int j = 0; + struct evsel *ev, *current_leader = NULL; double *val_ptr; + int i = 0, matched_events = 0, events_to_match; + const int idnum = (int)hashmap__size(&pctx->ids); + + /* duration_time is grouped separately. */ + if (!has_constraint && + hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr)) + events_to_match = idnum - 1; + else + events_to_match = idnum; evlist__for_each_entry (perf_evlist, ev) { - if (test_bit(j++, evlist_used)) + /* + * Events with a constraint aren't grouped and match the first + * events available. + */ + if (has_constraint && ev->weak_group) continue; - if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) { - if (!metric_events[i]) - metric_events[i] = ev; - i++; - if (i == idnum) - break; - } else { - /* Discard the whole match and start again */ - i = 0; + if (!has_constraint && ev->leader != current_leader) { + /* + * Start of a new group, discard the whole match and + * start again. + */ + matched_events = 0; memset(metric_events, 0, sizeof(struct evsel *) * idnum); + current_leader = ev->leader; + } + if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) + metric_events[matched_events++] = ev; + if (matched_events == events_to_match) + break; + } - if (hashmap__find(&pctx->ids, ev->name, - (void **)&val_ptr)) { - if (!metric_events[i]) - metric_events[i] = ev; - i++; - if (i == idnum) - break; + if (events_to_match != idnum) { + /* Add the first duration_time. */ + evlist__for_each_entry(perf_evlist, ev) { + if (!strcmp(ev->name, "duration_time")) { + metric_events[matched_events++] = ev; + break; } } } - if (i != idnum) { + if (matched_events != idnum) { /* Not whole match */ return NULL; } @@ -139,18 +166,8 @@ static struct evsel *find_evsel_group(struct evlist *perf_evlist, metric_events[idnum] = NULL; for (i = 0; i < idnum; i++) { - leader_found = false; - evlist__for_each_entry(perf_evlist, ev) { - if (!leader_found && (ev == metric_events[i])) - leader_found = true; - - if (leader_found && - !strcmp(ev->name, metric_events[i]->name)) { - ev->metric_leader = metric_events[i]; - } - j++; - } ev = metric_events[i]; + ev->metric_leader = ev; set_bit(ev->idx, evlist_used); } @@ -166,7 +183,7 @@ static int metricgroup__setup_events(struct list_head *groups, int i = 0; int ret = 0; struct egroup *eg; - struct evsel *evsel; + struct evsel *evsel, *tmp; unsigned long *evlist_used; evlist_used = bitmap_alloc(perf_evlist->core.nr_entries); @@ -182,7 +199,8 @@ static int metricgroup__setup_events(struct list_head *groups, ret = -ENOMEM; break; } - evsel = find_evsel_group(perf_evlist, &eg->pctx, metric_events, + evsel = find_evsel_group(perf_evlist, &eg->pctx, + eg->has_constraint, metric_events, evlist_used); if (!evsel) { pr_debug("Cannot resolve %s: %s\n", @@ -212,6 +230,12 @@ static int metricgroup__setup_events(struct list_head *groups, list_add(&expr->nd, &me->head); } + evlist__for_each_entry_safe(perf_evlist, tmp, evsel) { + if (!test_bit(evsel->idx, evlist_used)) { + evlist__remove(perf_evlist, evsel); + evsel__delete(evsel); + } + } bitmap_free(evlist_used); return ret; -- 2.26.2.645.ge9eca65c58-goog