From patchwork Thu Mar 2 17:17:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 63503 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp5848172dyk; Thu, 2 Mar 2023 09:19:22 -0800 (PST) X-Google-Smtp-Source: AK7set/F9TT33ciDBoJ0idrbDmB1EveAHR7Zxe8docSNwaoOpITU2fpQesUk87csTgerz2FvE5uC X-Received: by 2002:a17:90a:1917:b0:237:659a:a453 with SMTP id 23-20020a17090a191700b00237659aa453mr12413368pjg.16.1677777562069; Thu, 02 Mar 2023 09:19:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677777562; cv=none; d=google.com; s=arc-20160816; b=0jDyF0/io8CjKw6Go7CMmI28Px2UuBtWF5yTyqmb7oExITiCm6lVao8OvZzx5qVBQ+ uQjDO7tsrZSlPmaZrW8Ru76JrKabKtyqyLrMZ92pTs24B6S7Yz9+u0kHZadbd2rm0Tg2 rFaa2pgbMw8wO6rK0OSOlFKEKy0pzUoxYLMzLXm9n77w2b2z3HfdMlBRMUaQAZOj2Zqf HFmz/RYFkO+OiCyPKpc4gqXRi4mmX4UB01FfYwbRPHL2p1S4KCLCgzmVg3VFLlTnQTtG 5NhO0Mb7FaqeGa4rv2UuXvjw+2CW0SoFR8/e0iCpIlBJteq4X7DUOrW7udxjr0tBl3ea emeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=dFwyX5fCRCzWJmR23HYHFZHQHmrn3rL22R9ZNcUhtkU=; b=U5vivvGMDj+wKFjVBpHPYfFpzLgrABBmRAN0YSyzrtzAiJdbyJyvMEs8eGAvGtouiG rkZat8bDpuXXyNTpoQWAe3SG+uuaFWsoNGvnTeNCd/oISegmRWygYpmI06fxtoxUnuaE K5sxl3g1qglXr0GwowZi8rOSa/aKIR0/6c+07XYVK4yxQKjsEA/Xk1iMTxUM2hLOEglR w22b/LfXswNmluW+TOQRx3fvLGDOkcqCI+6lKJCUJJnP8fSTOAldoLa6ZPLsI7yD7688 Qrlwoupuvlnl0FPyl5C4u+OSTZ1chHwMxLmQFHn1A9dPzkQ1Mr9UQKhRz+zHDvnsR9Kz Aa7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t22-20020a17090ad51600b00233f0f06b4asi100137pju.52.2023.03.02.09.19.08; Thu, 02 Mar 2023 09:19:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229623AbjCBRSG (ORCPT + 99 others); Thu, 2 Mar 2023 12:18:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229547AbjCBRSD (ORCPT ); Thu, 2 Mar 2023 12:18:03 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 64C1E61A4; Thu, 2 Mar 2023 09:18:02 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 61C942F4; Thu, 2 Mar 2023 09:18:45 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 41E473F67D; Thu, 2 Mar 2023 09:18:01 -0800 (PST) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: linux-trace-kernel@vger.kernel.org, mark.rutland@arm.com, mhiramat@kernel.org, rostedt@goodmis.org, zanussi@kernel.org Subject: [PATCH 1/2] tracing/hist: simplify contains_operator() Date: Thu, 2 Mar 2023 17:17:54 +0000 Message-Id: <20230302171755.1821653-2-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230302171755.1821653-1-mark.rutland@arm.com> References: <20230302171755.1821653-1-mark.rutland@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759277284892145765?= X-GMAIL-MSGID: =?utf-8?q?1759277284892145765?= In a subsequent patch we'll add additional operators for histogram expressions. In preparation for adding additional operators, this patch refactors contains_operator() to consider each operator within a precedence group independently by using the 'sep' pointer as the current rightmost operator, and removing the separate op pointers. Within each precedence group, this allows operators to be checked independently with a consistent pattern: op = strrchr(str, $OP_CHAR); if (op > *sep) { *sep = op; field_op = $FIELD_OP_TYPE; } This makes it easy to add new operators of the same precedence without needing to check multiple pointers, and without needing a final switch statement to recover the relevant pointer. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland Cc: Masami Hiramatsu Cc: Steven Rostedt (Google) Cc: Tom Zanussi Cc: linux-trace-kernel@vger.kernel.org --- kernel/trace/trace_events_hist.c | 80 ++++++++++++-------------------- 1 file changed, 30 insertions(+), 50 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index 10d36f751fcd..a308da2cde2f 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -1813,13 +1813,15 @@ static char *expr_str(struct hist_field *field, unsigned int level) static int contains_operator(char *str, char **sep) { enum field_op_id field_op = FIELD_OP_NONE; - char *minus_op, *plus_op, *div_op, *mult_op; + char *op; + *sep = NULL; /* - * Report the last occurrence of the operators first, so that the - * expression is evaluated left to right. This is important since - * subtraction and division are not associative. + * For operators of the same precedence report the last occurrence of + * the operators first, so that the expression is evaluated left to + * right. This is important since subtraction and division are not + * associative. * * e.g * 64/8/4/2 is 1, i.e 64/8/4/2 = ((64/8)/4)/2 @@ -1830,68 +1832,46 @@ static int contains_operator(char *str, char **sep) * First, find lower precedence addition and subtraction * since the expression will be evaluated recursively. */ - minus_op = strrchr(str, '-'); - if (minus_op) { + op = strrchr(str, '-'); + if (op > *sep) { + *sep = op; + /* * Unary minus is not supported in sub-expressions. If * present, it is always the next root operator. */ - if (minus_op == str) { - field_op = FIELD_OP_UNARY_MINUS; - goto out; - } + if (op == str) + return FIELD_OP_UNARY_MINUS; field_op = FIELD_OP_MINUS; } - plus_op = strrchr(str, '+'); - if (plus_op || minus_op) { - /* - * For operators of the same precedence use to rightmost as the - * root, so that the expression is evaluated left to right. - */ - if (plus_op > minus_op) - field_op = FIELD_OP_PLUS; - goto out; + op = strrchr(str, '+'); + if (op > *sep) { + *sep = op; + field_op = FIELD_OP_PLUS; } /* - * Multiplication and division have higher precedence than addition and - * subtraction. + * If we've found a low-precedence operator, we're done. */ - div_op = strrchr(str, '/'); - if (div_op) - field_op = FIELD_OP_DIV; + if (*sep) + return field_op; - mult_op = strrchr(str, '*'); /* - * For operators of the same precedence use to rightmost as the - * root, so that the expression is evaluated left to right. + * Second, consider the higher precedence multiplication and division + * operators. */ - if (mult_op > div_op) - field_op = FIELD_OP_MULT; + op = strrchr(str, '/'); + if (op > *sep) { + *sep = op; + field_op = FIELD_OP_DIV; + } -out: - if (sep) { - switch (field_op) { - case FIELD_OP_UNARY_MINUS: - case FIELD_OP_MINUS: - *sep = minus_op; - break; - case FIELD_OP_PLUS: - *sep = plus_op; - break; - case FIELD_OP_DIV: - *sep = div_op; - break; - case FIELD_OP_MULT: - *sep = mult_op; - break; - case FIELD_OP_NONE: - default: - *sep = NULL; - break; - } + op = strrchr(str, '*'); + if (op > *sep) { + *sep = op; + field_op = FIELD_OP_MULT; } return field_op; From patchwork Thu Mar 2 17:17:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 63505 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp5849646dyk; Thu, 2 Mar 2023 09:21:38 -0800 (PST) X-Google-Smtp-Source: AK7set98hNNX5tg+sQY7UkyTcmBDzO9ABzaBRfqzCTAau/u1fguV23jFiTUtvqdmM3fcpM2a6gfH X-Received: by 2002:a17:902:ec85:b0:199:2b9f:f369 with SMTP id x5-20020a170902ec8500b001992b9ff369mr11899819plg.32.1677777698249; Thu, 02 Mar 2023 09:21:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677777698; cv=none; d=google.com; s=arc-20160816; b=gYM4N+yA+dBAI/I9tRqQ02GmFacxGWYVAbUtiW+EvJh/asg2PCnzJccKV76ZZi0DdQ enggOaHIv50GY3CrpyI8jocMKQLGd0qDesiSv3O63bn3nqAvkrK96UKP5HqeEdr5UOrA ixuFV5sOKv5EfJhh1hPgQBI/nKc6YouQR6cJX5WPmAoJ3l/KPhH/ojKjhPtdPFxhPSuP IQb/SRAur5ZgpYI2DK9zzyWpmOB30cCjYO3ywiFO6m+TOiuKdCQD1GAL4vGgpEKN/BCG 2R63w0ezgpVWZhCmfo1qgelhcRk6IR7dBW+dH6B1UKBybs91cfwjNZx5tAZjkbXyxHfL TVDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=xH6WrdxjBPMifoXbPfPm78dwWRwFi1B6Uzz6gRf2t3Y=; b=Rp25Lxcmj0fSlTTM6z03HLfYU1kxMiFYkiBFbSqcCT/i3pxRfCXntWmG2I96lL96nn YVybjB+lZu6e5m5wI75YGAjYMymCRepmh8YmmkG2HZRZwc5kn++tloMP5J/oeaDXpChP gfvDgIlOVSh9CDJVpAS74fjQCfWDA84V75cEWI3XMgpyJ6oIfw3sMTfxdAbe/Eadln7I qHnb3t+Y1ajcRS0NtuBxnU40FX2ZsbXjSfv8CkxEIYssGkLzsaxJ/7Wwb3TavocpB5jQ tdKwI4D5XoluaYMzEHS/7XU5cdktqseJM8O1CXlRxWyw/joXBbvBQ3nFNhBy/Iqr+M8v cQ6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020a17090282c700b0019ad22c40c7si14856530plz.132.2023.03.02.09.20.58; Thu, 02 Mar 2023 09:21:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229754AbjCBRSJ (ORCPT + 99 others); Thu, 2 Mar 2023 12:18:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229626AbjCBRSF (ORCPT ); Thu, 2 Mar 2023 12:18:05 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D5BD0CA26; Thu, 2 Mar 2023 09:18:03 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DD251169E; Thu, 2 Mar 2023 09:18:46 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id BE54D3F67D; Thu, 2 Mar 2023 09:18:02 -0800 (PST) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: linux-trace-kernel@vger.kernel.org, mark.rutland@arm.com, mhiramat@kernel.org, rostedt@goodmis.org, zanussi@kernel.org Subject: [PATCH 2/2] tracing/hist: add modulus operator Date: Thu, 2 Mar 2023 17:17:55 +0000 Message-Id: <20230302171755.1821653-3-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230302171755.1821653-1-mark.rutland@arm.com> References: <20230302171755.1821653-1-mark.rutland@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759277427611472266?= X-GMAIL-MSGID: =?utf-8?q?1759277427611472266?= Currently historgram field expressions can use addition ('+'), substraction ('-'), division ('/'), and multiplication ('*') operators. It would be helpful to also have a modulus ('%') operator. This is helpful for capturing the alignment of pointers. For example, on arm64 with CONFIG_KPROBE_EVENTS_ON_NOTRACE=y, we can record the size and alignment of copies to user with: | # echo 'p:copy_to_user __arch_copy_to_user to=$arg1 from=$arg2 n=$arg3' >> /sys/kernel/tracing/kprobe_events | # echo 'hist keys=n,to%8:vals=hitcount:sort=n,to%8' > /sys/kernel/tracing/events/kprobes/copy_to_user/trigger | # cat /sys/kernel/tracing/events/kprobes/copy_to_user/hist | # event histogram | # | # trigger info: hist:keys=n,to%8:vals=hitcount:sort=n,to%8:size=2048 [active] | # | | { n: 1, to%8: 1 } hitcount: 5 | { n: 8, to%8: 0 } hitcount: 3 | { n: 16, to%8: 0 } hitcount: 2 | { n: 32, to%8: 0 } hitcount: 1 | { n: 36, to%8: 0 } hitcount: 1 | { n: 128, to%8: 0 } hitcount: 4 | { n: 336, to%8: 0 } hitcount: 1 | { n: 832, to%8: 0 } hitcount: 3 | | Totals: | Hits: 20 | Entries: 8 | Dropped: 0 Add a modulus operator, with the same precedence as multiplication and division, matching C's operator precedence. Signed-off-by: Mark Rutland Cc: Masami Hiramatsu Cc: Steven Rostedt (Google) Cc: Tom Zanussi Cc: linux-trace-kernel@vger.kernel.org --- Documentation/trace/histogram.rst | 4 ++-- kernel/trace/trace_events_hist.c | 35 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Documentation/trace/histogram.rst b/Documentation/trace/histogram.rst index f95459aa984f..534fb190ebe0 100644 --- a/Documentation/trace/histogram.rst +++ b/Documentation/trace/histogram.rst @@ -1771,8 +1771,8 @@ using the same key and variable from yet another event:: # echo 'hist:key=pid:wakeupswitch_lat=$wakeup_lat+$switchtime_lat ...' >> event3/trigger -Expressions support the use of addition, subtraction, multiplication and -division operators (+-\*/). +Expressions support the use of addition, subtraction, multiplication, +division, modulus operators (+-\*/%). Note if division by zero cannot be detected at parse time (i.e. the divisor is not a constant), the result will be -1. diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index a308da2cde2f..629896aaed54 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -103,6 +103,7 @@ enum field_op_id { FIELD_OP_UNARY_MINUS, FIELD_OP_DIV, FIELD_OP_MULT, + FIELD_OP_MOD, }; enum hist_field_fn { @@ -131,6 +132,7 @@ enum hist_field_fn { HIST_FIELD_FN_PLUS, HIST_FIELD_FN_DIV, HIST_FIELD_FN_MULT, + HIST_FIELD_FN_MOD, HIST_FIELD_FN_DIV_POWER2, HIST_FIELD_FN_DIV_NOT_POWER2, HIST_FIELD_FN_DIV_MULT_SHIFT, @@ -436,6 +438,21 @@ static u64 hist_field_mult(struct hist_field *hist_field, return val1 * val2; } +static u64 hist_field_mod(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 = hist_fn_call(operand1, elt, buffer, rbe, event); + u64 val2 = hist_fn_call(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, @@ -1796,6 +1813,9 @@ static char *expr_str(struct hist_field *field, unsigned int level) case FIELD_OP_MULT: strcat(expr, "*"); break; + case FIELD_OP_MOD: + strcat(expr, "%"); + break; default: kfree(expr); return NULL; @@ -1859,8 +1879,8 @@ static int contains_operator(char *str, char **sep) return field_op; /* - * Second, consider the higher precedence multiplication and division - * operators. + * Second, consider the higher precedence multiplication, division, and + * modulus operators. */ op = strrchr(str, '/'); if (op > *sep) { @@ -1874,6 +1894,12 @@ static int contains_operator(char *str, char **sep) field_op = FIELD_OP_MULT; } + op = strrchr(str, '%'); + if (op > *sep) { + *sep = op; + field_op = FIELD_OP_MOD; + } + return field_op; } @@ -2698,6 +2724,9 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, case FIELD_OP_MULT: op_fn = HIST_FIELD_FN_MULT; break; + case FIELD_OP_MOD: + op_fn = HIST_FIELD_FN_MOD; + break; default: ret = -EINVAL; goto free_operands; @@ -4289,6 +4318,8 @@ static u64 hist_fn_call(struct hist_field *hist_field, return hist_field_div(hist_field, elt, buffer, rbe, event); case HIST_FIELD_FN_MULT: return hist_field_mult(hist_field, elt, buffer, rbe, event); + case HIST_FIELD_FN_MOD: + return hist_field_mod(hist_field, elt, buffer, rbe, event); case HIST_FIELD_FN_DIV_POWER2: return div_by_power_of_two(hist_field, elt, buffer, rbe, event); case HIST_FIELD_FN_DIV_NOT_POWER2: