Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp339929pxb; Thu, 23 Sep 2021 00:50:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEV7tqJK5NY3gAwoUPWeWwm6KcKfzo+skl9XZw+c8WpDOOXiSQHvTA4pbTI2Wn5gPE9mGb X-Received: by 2002:a05:6402:411:: with SMTP id q17mr3942977edv.35.1632383456654; Thu, 23 Sep 2021 00:50:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632383456; cv=none; d=google.com; s=arc-20160816; b=CS7Sj+/t9WJ4D53sTsk+Nj2qw2ZXm6pKzNbQwp3KlgeYnBrgPNk174Tqzbc9yQ9ghj PXVPCmqgi3JpGdf9po6/AwVNhUPAXNQOaKbBfbzW8cbqruCetvfFH9wn4qw29NK8XUKk j5lD709sAqhgegeWAKA7aBkRLT/PVtvqVVonbljLE7P16OYqb+gn/9hcibuX5mdbn/xC HcP7bB7qZZTuWUvC8+S8zUh27TfStgQ0vu19aozmW+kux4mzSVD1xg7a+PQDE5kYkEVc 92sA+Yxv5ngGPLBVZpdNZIQxFXntOT62JKFDxvEkyebNQ2QqXohUX++ezkVoomHCteEp Ki6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=pMaOfLt+vkRQx0RPjtfU4NwNaK63mYgla1MP1EllA8c=; b=IE+tIL6ql68+XYzqmkwAGMH6SouJScbxca2Q1f4EOcJCAjIdXw7cyhEGvUdFpUL/zp frJOPf2kA6cq/nwVcC6qokmVoIOhltA8dNKs2L3Zlcdk6Rb9IaOm0yRDnpa2PyllzYN7 PWG5clBCpNtO4t1QAXg4Vj0y57LMew2nKCHqBUbVTcgjxfe1GAsdpsu+YLrCpwdww5G8 E99sGFPUoixD3c6E6jRn7IR3tmWkmouFrBGO7ba5WTi+i5jg9alNuopf4qCuHj0H5IcD SPD9gxvRV8Gnu3ptbKonJJS/CEpxwcEyz18Esn1oaG03A5TgsjaqR89oOSIJfmrEPq9m GUJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rHUFtH7+; 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 dg13si4787132edb.604.2021.09.23.00.50.33; Thu, 23 Sep 2021 00:50:56 -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=20210112 header.b=rHUFtH7+; 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 S239835AbhIWHtZ (ORCPT + 99 others); Thu, 23 Sep 2021 03:49:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239929AbhIWHss (ORCPT ); Thu, 23 Sep 2021 03:48:48 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6830AC0613E3 for ; Thu, 23 Sep 2021 00:47:06 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id d20-20020ac81194000000b002a53ffbd04dso15872489qtj.12 for ; Thu, 23 Sep 2021 00:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=pMaOfLt+vkRQx0RPjtfU4NwNaK63mYgla1MP1EllA8c=; b=rHUFtH7+fh8D4nXJQx30emGYbrI/3qe6050g6PPZZPoA6l3zvxLSLjfKMKJNgugfIq ZmkwYHUdx8FnjnyTxIe2M2cmo2O5UQ9rR4pQYPLLdj8JXdfVTta8F2d222y5xheWWqx4 fLY9znZVpFFHPRjW/QVYUf2uUfD92qtwnP0VoF3mk662kd7OIiA2sJi9h0Ckr/S9sgEC Q79W2IWaorHUFJYKscFxVbcXcJeIbcgwNP1eswCfpylMczjZJjMbA4b8ZEU8hVaecD8Z 1BcOsi12mdXzySlarolIVADLIzIFwEHQVAan/U92/FphPBeIjaB9il5Mi49bQNZEMp7t Tjlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=pMaOfLt+vkRQx0RPjtfU4NwNaK63mYgla1MP1EllA8c=; b=NdtBzIzUUX83YtGZjCz4AdieFEKG1OZP4DdGC+NnDzYXYmSvkQUZb6cXF+UuKljFXn ARixQo5EdPgU8LID4jU4FUlNtxF5NU7sk0NcRXG46JLdl3AFwHfH9s8h1/dR9QrnWLzX qYrQxISUxG9tOY5wG8Sxo3al56DYD9zbNC/lxPOj4PCPGPgo5jU820RPOfdsJPXKBDtU NGy7HldMWgCYycB9aqJ2oZtQVd/iApwYNgJt4dv8A5PThDvLC+5hzOxHIaZ1QHsKExEt LHTRMt9MT22c+42etuapv2WwaUuvtKUEjenXxMacvNmQ02kqsRbTxAqY/secgru5sPGw mytA== X-Gm-Message-State: AOAM530rxiBnr0vQ8WB5dtoI7ByDlqVzuSPOn/23jAnXuM8e5RMxAf6m w120TpZUX7UEu68aaZiNk2rRKTrvTnJJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:d3ff:e8f7:11f4:c738]) (user=irogers job=sendgmr) by 2002:a05:6902:1504:: with SMTP id q4mr3970375ybu.219.1632383225560; Thu, 23 Sep 2021 00:47:05 -0700 (PDT) Date: Thu, 23 Sep 2021 00:46:16 -0700 In-Reply-To: <20210923074616.674826-1-irogers@google.com> Message-Id: <20210923074616.674826-14-irogers@google.com> Mime-Version: 1.0 References: <20210923074616.674826-1-irogers@google.com> X-Mailer: git-send-email 2.33.0.464.g1972c5931b-goog Subject: [PATCH v9 13/13] perf metric: Avoid events for an 'if' constant result From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org, Andi Kleen , Jin Yao , John Garry , Paul Clarke , kajoljain , linux-perf-users@vger.kernel.org Cc: Stephane Eranian , Sandeep Dasgupta , Ian Rogers Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For a metric like: CONST if expr else CONST if the values of CONST are identical then expr doesn't need evaluating, and events, in order to compute a result. Signed-off-by: Ian Rogers --- tools/perf/tests/expr.c | 7 +++++++ tools/perf/util/expr.y | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c index 287989321d2a..f1d8411fce12 100644 --- a/tools/perf/tests/expr.c +++ b/tools/perf/tests/expr.c @@ -143,6 +143,13 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused) smt_on() ? "EVENT1" : "EVENT2", (void **)&val_ptr)); + /* The expression is a constant 1.0 without needing to evaluate EVENT1. */ + expr__ctx_clear(ctx); + TEST_ASSERT_VAL("find ids", + expr__find_ids("1.0 if EVENT1 > 100.0 else 1.0", + NULL, ctx, 0) == 0); + TEST_ASSERT_VAL("find ids", hashmap__size(ctx->ids) == 0); + expr__ctx_free(ctx); return 0; diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y index 5b878f044f22..ba7d3b667fcb 100644 --- a/tools/perf/util/expr.y +++ b/tools/perf/util/expr.y @@ -142,6 +142,16 @@ if_expr: expr IF expr ELSE expr $$.ids = $1.ids; ids__free($3.ids); ids__free($5.ids); + } else if ($1.val == $5.val) { + /* + * LHS == RHS, so both are an identical constant. No need to + * evaluate any events. + */ + $$.val = $1.val; + $$.ids = NULL; + ids__free($1.ids); + ids__free($3.ids); + ids__free($5.ids); } else { /* * Value is either the LHS or RHS and we need the IF expression -- 2.33.0.464.g1972c5931b-goog