Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1015763pxb; Tue, 19 Oct 2021 18:36:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzTjUvj908EdPOjnRtIBc8I1cSmtKz8ZhzPRV5HPtQPA8KNJd6+606oMPCnXfdBbB7AhJX1 X-Received: by 2002:a17:906:1706:: with SMTP id c6mr44203289eje.343.1634693816323; Tue, 19 Oct 2021 18:36:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634693816; cv=none; d=google.com; s=arc-20160816; b=tlBKK5xdxSgSMVgx8fQsBtGQp5Ryfneq5+Zx2WEZvjrwxwwmhR4kmywXFzb79/V2mh t+EDClyz3/rQ4NE2YEO0sBTUuRgPjOcoPPItnjy4entt+hxU7f7dYfDM7PdvBPsf7ieS 2LTPlDQF58fBVZiKhlNf3if6CfJkBtsbOB/cOURkjY9unE68b9Nf0RLLDSW47PlG3XWs 9JMGczltou5tD6XF3SAlLGLAbyJKBNVBCPxL4wXfBTYfbsYN7JoeHkgbuTmf1t6Ur8Kb ZC7bxYabg+vV7lC8/WKn/Uvm7GIAb4VUB3YrUFiX1JUOkGENez7GXR2QU3leIHHUUme2 lUiQ== 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=QoyL/j4Wj5xtQouIJDIdg2CMTweye3z9i1beCqJqXYs=; b=hlgs/1g+0ALMP31wUk0f362aP1Gvu/5Zo4lNP0RkRatVTpq2oZ6qRpKH4fX9w1egXK n3/GN45SNF9K5Ce+MlJB4FsbnyLM9E2KTs0742o+XBAEkmsNY/WDZvNn/bjw5pPLai8T 6FulaRofrHOszOdH4oZgi/+JZZtPODKBAtBxIg2XC+mfGdo1BhYk2Ab5Kch4HQCT9WUo 42bYNgdSPPdJloi/DLSPBxJQgxqsUAfn9VLdxVfD7LdZicjfoXQ65u90t8rKx2Lm/Byf YXzrA+nQ0RjHcQs1mysd3iCgL6YdhphXUcb6ohlNAwuBI4DXRjSDXH710g2R1as/G4H8 ++MA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Zw3jTItR; 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 p4si1360757edj.221.2021.10.19.18.36.32; Tue, 19 Oct 2021 18:36: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=Zw3jTItR; 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 S229846AbhJTBg1 (ORCPT + 99 others); Tue, 19 Oct 2021 21:36:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhJTBgX (ORCPT ); Tue, 19 Oct 2021 21:36:23 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68ABC061746 for ; Tue, 19 Oct 2021 18:34:09 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id z130-20020a256588000000b005b6b4594129so28151201ybb.15 for ; Tue, 19 Oct 2021 18:34:09 -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=QoyL/j4Wj5xtQouIJDIdg2CMTweye3z9i1beCqJqXYs=; b=Zw3jTItRnv/CJli7JslBJen12pbdK99RkpUXc0nkc0zUSAySAtJbYkRKEI8joNJr59 cBZqPCZX3zMuHXTJADhzSz/ru2z+OsZoqvnKok1CLc27jw0jAWWi8zDpMz3O5b/wDU5f UwFb1/Iqj0Foq0RC2L4ntvG+WBje7P2yca6VG/6tl4FHPyxxpeuYKGSr0/+x8dDagRPP nKl2dh/WItLL0Brnetrw3YCGNd6lueLntINApGPWW+iUK+sfkbXgNfeZKhNjBFtQwSfx r3gCve22ppGU0Z99Eh+UYIqYCAWgXOiL1K3dsSOi3Pxsk77v2jBb/U9jQrLwTuAsYaMw x7Tw== 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=QoyL/j4Wj5xtQouIJDIdg2CMTweye3z9i1beCqJqXYs=; b=IHkDeM0VIpMVsoMILWO5svcgk7eIFN9/C6+qBC1O2k+l5xiVZbJhjzpSLPIl+3FhXP FhT/JQK6gg57fL4W6fa2/jpwzJv4l4xdwYUjjmOrI+Ox+qfayPeMGTkucDLeZNvWJCJm Wj5/D/vPlOMZv+YAj1QNMwKuw8E92M55W5wJbS4dGfsA2EPLLq1PrqubxcGDIYErj8Yo D0tcnvaZ4eFKE0htJMCn2w2kfV0zgYsjQagtvIAO6mE2877+3ORjFMbe/DCZ6VCklniu c/aaakU2nJAYAkFhCKXU8/Uaue8ikRp/eItalpSTHSRrYzc84L18BN8Kadro7MrwJw7C 1YxQ== X-Gm-Message-State: AOAM531Sk/qKYUTYiiIJ0kSkLliuVgy6Uaz8XRnClNVKlvD5K3jsHwVk r6xaTOlklrEP2MhlN2cAbd58yPecOJXwA2UXgQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:1953:b886:7a6c:bf0]) (user=kaleshsingh job=sendgmr) by 2002:a25:c696:: with SMTP id k144mr37146969ybf.296.1634693649086; Tue, 19 Oct 2021 18:34:09 -0700 (PDT) Date: Tue, 19 Oct 2021 18:31:39 -0700 In-Reply-To: <20211020013153.4106001-1-kaleshsingh@google.com> Message-Id: <20211020013153.4106001-3-kaleshsingh@google.com> Mime-Version: 1.0 References: <20211020013153.4106001-1-kaleshsingh@google.com> X-Mailer: git-send-email 2.33.0.1079.g6e70778dc9-goog Subject: [PATCH v2 2/5] tracing: Add division and multiplication support for hist triggers From: Kalesh Singh Cc: surenb@google.com, hridya@google.com, namhyung@kernel.org, kernel-team@android.com, Kalesh Singh , Jonathan Corbet , Steven Rostedt , 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 Adds basic support for division and multiplication operations for hist trigger variable expressions. For simplicity this patch only supports, division and multiplication for a single operation expression (e.g. x=$a/$b), as currently expressions are always evaluated right to left. This can lead to some incorrect results: e.g. echo 'hist:keys=common_pid:x=8-4-2' >> event/trigger 8-4-2 should evaluate to 2 i.e. (8-4)-2 but currently x evaluate to 6 i.e. 8-(4-2) Multiplication and division in sub-expressions will work correctly, once correct operator precedence support is added (See next patch in this series). For the undefined case of division by 0, the histogram expression evaluates to (u64)(-1). Since this cannot be detected when the expression is created, it is the responsibility of the user to be aware and account for this possibility. Examples: echo 'hist:keys=common_pid:a=8,b=4,x=$a/$b' \ >> event/trigger echo 'hist:keys=common_pid:y=5*$b' \ >> event/trigger Signed-off-by: Kalesh Singh --- Changes in v2: - Use div64 helper in hist_field_div() to avoid faults on x86 32-bit machines, per Steven Rostedt kernel/trace/trace_events_hist.c | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 8563a2d51f65..9415ee65acc0 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -99,6 +99,8 @@ enum field_op_id { FIELD_OP_PLUS, FIELD_OP_MINUS, FIELD_OP_UNARY_MINUS, + FIELD_OP_DIV, + FIELD_OP_MULT, }; /* @@ -287,6 +289,40 @@ static u64 hist_field_minus(struct hist_field *hist_field, return val1 - val2; } +static u64 hist_field_div(struct hist_field *hist_field, + struct tracing_map_elt *elt, + struct trace_buffer *buffer, + struct ring_buffer_event *rbe, + void *event) +{ + struct hist_field *operand1 = hist_field->operands[0]; + struct hist_field *operand2 = hist_field->operands[1]; + + u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event); + u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event); + + /* Return -1 for the undefined case */ + if (!val2) + return -1; + + return div64_u64(val1, val2); +} + +static u64 hist_field_mult(struct hist_field *hist_field, + struct tracing_map_elt *elt, + struct trace_buffer *buffer, + struct ring_buffer_event *rbe, + void *event) +{ + struct hist_field *operand1 = hist_field->operands[0]; + struct hist_field *operand2 = hist_field->operands[1]; + + u64 val1 = operand1->fn(operand1, elt, buffer, rbe, event); + u64 val2 = operand2->fn(operand2, elt, buffer, rbe, event); + + return val1 * val2; +} + static u64 hist_field_unary_minus(struct hist_field *hist_field, struct tracing_map_elt *elt, struct trace_buffer *buffer, @@ -1595,6 +1631,12 @@ static char *expr_str(struct hist_field *field, unsigned int level) case FIELD_OP_PLUS: strcat(expr, "+"); break; + case FIELD_OP_DIV: + strcat(expr, "/"); + break; + case FIELD_OP_MULT: + strcat(expr, "*"); + break; default: kfree(expr); return NULL; @@ -1610,7 +1652,7 @@ static int contains_operator(char *str) enum field_op_id field_op = FIELD_OP_NONE; char *op; - op = strpbrk(str, "+-"); + op = strpbrk(str, "+-/*"); if (!op) return FIELD_OP_NONE; @@ -1631,6 +1673,12 @@ static int contains_operator(char *str) case '+': field_op = FIELD_OP_PLUS; break; + case '/': + field_op = FIELD_OP_DIV; + break; + case '*': + field_op = FIELD_OP_MULT; + break; default: break; } @@ -2370,10 +2418,26 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, case FIELD_OP_PLUS: sep = "+"; break; + case FIELD_OP_DIV: + sep = "/"; + break; + case FIELD_OP_MULT: + sep = "*"; + break; default: goto free; } + /* + * Multiplication and division are only supported in single operator + * expressions, since the expression is always evaluated from right + * to left. + */ + if ((field_op == FIELD_OP_DIV || field_op == FIELD_OP_MULT) && level > 0) { + hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); + return ERR_PTR(-EINVAL); + } + operand1_str = strsep(&str, sep); if (!operand1_str || !str) goto free; @@ -2445,6 +2509,12 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, 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; -- 2.33.0.1079.g6e70778dc9-goog