Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp747740pxb; Mon, 25 Oct 2021 18:03:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/76x4n9UCM/YPr5pUaX8FQN2AvmewOnVtSPn6/SAvNXeefQhYEpExJZlW3Uw+dtW/2lym X-Received: by 2002:a17:906:c186:: with SMTP id g6mr26114756ejz.259.1635210194395; Mon, 25 Oct 2021 18:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635210194; cv=none; d=google.com; s=arc-20160816; b=MkmRaqvkDgCVIpI5R8i+ZOZhUf9oGup6MnHzdpbQeIzJX7TJ/I5fVbVqt19R/fO7T6 2Lob4zLLTpDNC4+BdcERBgwuVD2u0xkXfq4zvbpgSDzdIFA3qvaP3XH8Wr+89cHbFzeB fD7dj93k1KLglDmmqkkWgHw7Z0B1UHa3gyVlP7wgY0zO5V5m0oB5OOchfdcwQ8dkEZxV t6EY49DgFC2cK8cRWF2LyLAZfy8GIOb3keviPezWsE1hH6OCDm8dpHff57U2W/aqTOim W82sSxi712tfNgZAYluM0IZJ6b8dBIg6zu1FV3f83TvQs57ZCiMbvFwPjOV89rTV2lpL XPxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:cc:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=ycQjNiSMtT2r+Nmugkr+45lU69ayqIpN5p1Bdj1Qt2c=; b=SUHfh9Ut80TwXEHNNkw/xQGvgxhaYEyxMAc4TXMRKHYGm9m8YAE3unr3mdqAWg1Q0F lMxDVN0QF9EF9TRG0SMaSBgiI3OcWPcjsKv4wcsF5uRH4/1+gWwXrnIVNaYYAVISE1Ia 3d2+1WIaX6w2LVk03b1+MQ9mgz519t9nHhA4YegjawRK60+5NPA1pGyLJh29rW33HHhn LEBsSOrg61u1ceYWlgVyTx91BKtI7OixOPFRjW3E1hh/jzHfiSl2jYqeCRClK8yCl1h9 bcUODDgVmgqvg26JdUIv7Ud4IypDrmC74VWt+mE2nUxAc9BV1PoxijxBoQlgJ57HlTR5 vntA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aHqeuuil; 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 jr21si1374237ejb.14.2021.10.25.18.02.46; Mon, 25 Oct 2021 18:03:14 -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=aHqeuuil; 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 S234122AbhJYU0O (ORCPT + 99 others); Mon, 25 Oct 2021 16:26:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238036AbhJYUZ6 (ORCPT ); Mon, 25 Oct 2021 16:25:58 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07D04C04A429 for ; Mon, 25 Oct 2021 13:10:59 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id w4-20020a1709029a8400b00138e222b06aso4242331plp.12 for ; Mon, 25 Oct 2021 13:10:59 -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:cc; bh=ycQjNiSMtT2r+Nmugkr+45lU69ayqIpN5p1Bdj1Qt2c=; b=aHqeuuil44JjI5IyPl326WS7uYjIPv/RTESFRvJ8eJPKCTZ9lxhYUaAYiJ+Fy/WHxn lyoQs83YyCWL7l7MPfW4D1VwVyjWBkTs1SkonBbq11kLheT3tU6trVfUwc6ddfF/LZiM mhRWys2x+SIuPykGQVgvbfbvgGt0BWRTaiMymAcBpOXbk43cxrlwxGpezYliMyRESHQm NHstJvCXsI7RvQ3gBeJh8qg8iBfNjl7/P94LDup9KQuSlkwFsuI+lmkv2HFzxKZCrtgB PL3k0hySc8NmomxDEnAjyq0UZ2OHLh1smdkVnlY/kRNx4XkLd8siIXSYeJ9YSnl74ZRp JMXQ== 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:cc; bh=ycQjNiSMtT2r+Nmugkr+45lU69ayqIpN5p1Bdj1Qt2c=; b=UQCVix5ugS9QMCcxNXwVF3Sw7nIFuFMn8QxkbWUOpjHF0aeB3TeFyfw+kq5R2BJpKa o0WIcQ8v24h1EhdRbwywUk5agC8ZG6GfyXLGXpXW9UBnYwGdoXr529V/kpGxmXFD+4T9 EakWuNaZQ1Is1Sla5544rdbgU2+qcs1b/q82QR5G0zDycZSTek6XnqDMrHJl+Q6eU6CN B41yKhB6XG1noymDkVn9y9umo5SeeX0Ou8gu5/9Ugtx1DvXvaLb5hw6+J9duqwYHLcPr a3mXouzLcpmP1KRA9l1u3zXOB1oYott4bhsytEoFYMoFjgjnZD+Qi23i2methFiBHck+ FMuw== X-Gm-Message-State: AOAM530BccuJ0ZZjvNivExPpHeGXhcctLQbdru5b4jLNPpWG87wdTAdP rGmCjh9gzn0dMB9EQSKf/TfSNhoZccNoQPpSPA== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:b783:5702:523e:d435]) (user=kaleshsingh job=sendgmr) by 2002:a17:90b:60d:: with SMTP id gb13mr154155pjb.0.1635192657999; Mon, 25 Oct 2021 13:10:57 -0700 (PDT) Date: Mon, 25 Oct 2021 13:08:37 -0700 In-Reply-To: <20211025200852.3002369-1-kaleshsingh@google.com> Message-Id: <20211025200852.3002369-6-kaleshsingh@google.com> Mime-Version: 1.0 References: <20211025200852.3002369-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v4 5/8] tracing/histogram: Covert expr to const if both operands are constants From: Kalesh Singh Cc: surenb@google.com, hridya@google.com, namhyung@kernel.org, kernel-team@android.com, Kalesh Singh , Steven Rostedt , Jonathan Corbet , Ingo Molnar , Shuah Khan , Masami Hiramatsu , Tom Zanussi , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If both operands of a hist trigger expression are constants, convert the expression to a constant. This optimization avoids having to perform the same calculation multiple times and also saves on memory since the merged constants are represented by a single struct hist_field instead or multiple. Signed-off-by: Kalesh Singh Suggested-by: Steven Rostedt --- kernel/trace/trace_events_hist.c | 104 ++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 34aba07d23f8..db28bcf976f4 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -2411,9 +2411,15 @@ static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, return ERR_PTR(ret); } +/* + * If the operands are var refs, return pointers the + * variable(s) referenced in var1 and var2, else NULL. + */ static int check_expr_operands(struct trace_array *tr, struct hist_field *operand1, - struct hist_field *operand2) + struct hist_field *operand2, + struct hist_field **var1, + struct hist_field **var2) { unsigned long operand1_flags = operand1->flags; unsigned long operand2_flags = operand2->flags; @@ -2426,6 +2432,7 @@ static int check_expr_operands(struct trace_array *tr, if (!var) return -EINVAL; operand1_flags = var->flags; + *var1 = var; } if ((operand2_flags & HIST_FIELD_FL_VAR_REF) || @@ -2436,6 +2443,7 @@ static int check_expr_operands(struct trace_array *tr, if (!var) return -EINVAL; operand2_flags = var->flags; + *var2 = var; } if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) != @@ -2453,9 +2461,12 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, char *var_name, unsigned int *n_subexprs) { struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL; - unsigned long operand_flags; + struct hist_field *var1 = NULL, *var2 = NULL; + unsigned long operand_flags, operand2_flags; int field_op, ret = -EINVAL; char *sep, *operand1_str; + hist_field_fn_t op_fn; + bool combine_consts; if (*n_subexprs > 3) { hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); @@ -2512,11 +2523,38 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, goto free; } - ret = check_expr_operands(file->tr, operand1, operand2); + switch (field_op) { + case FIELD_OP_MINUS: + op_fn = hist_field_minus; + break; + case FIELD_OP_PLUS: + op_fn = hist_field_plus; + break; + case FIELD_OP_DIV: + op_fn = hist_field_div; + break; + case FIELD_OP_MULT: + op_fn = hist_field_mult; + break; + default: + ret = -EINVAL; + goto free; + } + + ret = check_expr_operands(file->tr, operand1, operand2, &var1, &var2); if (ret) goto free; - flags |= HIST_FIELD_FL_EXPR; + operand_flags = var1 ? var1->flags : operand1->flags; + operand2_flags = var2 ? var2->flags : operand2->flags; + + /* + * If both operands are constant, the expression can be + * collapsed to a single constant. + */ + combine_consts = operand_flags & operand2_flags & HIST_FIELD_FL_CONST; + + flags |= combine_consts ? HIST_FIELD_FL_CONST : HIST_FIELD_FL_EXPR; flags |= operand1->flags & (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); @@ -2533,37 +2571,43 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, expr->operands[0] = operand1; expr->operands[1] = operand2; - /* The operand sizes should be the same, so just pick one */ - expr->size = operand1->size; + if (combine_consts) { + if (var1) + expr->operands[0] = var1; + if (var2) + expr->operands[1] = var2; - expr->operator = field_op; - expr->name = expr_str(expr, 0); - expr->type = kstrdup_const(operand1->type, GFP_KERNEL); - if (!expr->type) { - ret = -ENOMEM; - goto free; - } + expr->constant = op_fn(expr, NULL, NULL, NULL, NULL); - switch (field_op) { - case FIELD_OP_MINUS: - expr->fn = hist_field_minus; - break; - case FIELD_OP_PLUS: - expr->fn = hist_field_plus; - break; - case FIELD_OP_DIV: - expr->fn = hist_field_div; - break; - case FIELD_OP_MULT: - expr->fn = hist_field_mult; - break; - default: - ret = -EINVAL; - goto free; + expr->operands[0] = NULL; + expr->operands[1] = NULL; + + /* + * var refs won't be destroyed immediately + * See: destroy_hist_field() + */ + destroy_hist_field(operand2, 0); + destroy_hist_field(operand1, 0); + + expr->name = expr_str(expr, 0); + } else { + expr->fn = op_fn; + + /* The operand sizes should be the same, so just pick one */ + expr->size = operand1->size; + + expr->operator = field_op; + expr->type = kstrdup_const(operand1->type, GFP_KERNEL); + if (!expr->type) { + ret = -ENOMEM; + goto free; + } + + expr->name = expr_str(expr, 0); } return expr; - free: +free: destroy_hist_field(operand1, 0); destroy_hist_field(operand2, 0); destroy_hist_field(expr, 0); -- 2.33.0.1079.g6e70778dc9-goog