Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1015265pxb; Tue, 19 Oct 2021 18:36:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzf+SHhakNoWV8SOyqfLYs5FgWLNbr08d7tWN4Bh0VzRq4fhlrT11GzhRhkTWwVRaT9i5nb X-Received: by 2002:a05:6402:370:: with SMTP id s16mr54899511edw.1.1634693769488; Tue, 19 Oct 2021 18:36:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634693769; cv=none; d=google.com; s=arc-20160816; b=Ushzi+1RwnyTHJpVfwAgt23JarXO3IUzpHoAeIwEVOidaP7Si1XNLzJQbS5/3Usj5x M1ZTNNjaqWOuLsYip7vJGJ66bZs5qIQTqsKm0IyBijAtQ6kBMsPAHugg18Wo1903x0U2 yHh91IH+tGrHJ6EUj3YJ9QwH9iIr2rkZ7sdgziQ01QkHK3iMakeeRe03oxnu9SezjlNJ 4y5Dgjp5C9RoPG0uIRFfnVwj4zEQjSwGoxwsTTW13na6Dxz7nQpL5qbC1BDtlQ42h3fz PdapFamU25hIp+ucKql2vvUAIKklbWj1rFbud2h8egqK6Nj53lLZh9a4nUFio7iM9iyN IW3A== 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=KnOr7VusvEl2nOBWpkm4SdPaotgSdz121uuu2iozEYg=; b=oxkmMf7wv0Bww8iZP5y1aIcCZqiFfgRabLEz2X3eDpsujt7Zs5mfrG9E1jUkoAS6RL eJfdP8uvotAtqNO1k57zNFkVDK08jxadCJ7F9W6PH2KN/AhVspHVg1RmNio7v9P+ywpM FG5b5j79LCD5MB/oABOPoFHHrs9RqTHcQ6yqIskFU0tJtmOH8Rf26tJWnyzq8O7n9rK/ Pt6BS8KcW9aYOPYtfQGbJXi0af57H4tf0p2Vw5T0IVfSKHxl93wxB70lJEkb7T6M9eeh t8bN/SYppPiE73LS8dtcnqz6x0DGjeTXuSLlZ32v/24CITiwWkRz7+ic9clkbHZfNV5O PpVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="f1+R/8BH"; 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 g13si1330342edz.599.2021.10.19.18.35.45; Tue, 19 Oct 2021 18:36:09 -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="f1+R/8BH"; 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 S229663AbhJTBgJ (ORCPT + 99 others); Tue, 19 Oct 2021 21:36:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229728AbhJTBgI (ORCPT ); Tue, 19 Oct 2021 21:36:08 -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 9A25AC061749 for ; Tue, 19 Oct 2021 18:33:54 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id b195-20020a25cbcc000000b005be6b18fb7fso5885979ybg.7 for ; Tue, 19 Oct 2021 18:33:54 -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=KnOr7VusvEl2nOBWpkm4SdPaotgSdz121uuu2iozEYg=; b=f1+R/8BH6N9Ykke7x90tzzZZZ8+VSZ/GM8LQg7CwgILrCr8q0G8vwYrNAuasCMowbD rrDO+5wYSRhnm658/7ubdw9KldX27XDi9HQO8nHLcEvy0X+Qyw9uZhTEQoPXSmIH6jKa CprT61BHVH5h4gd4uCJuFx8HAm4P/S/rUv0mYAcLaPxrnD0TXdw8T06+rZEeZehyAYNV xh1eRGDn2wJP3yc1kxm3iqKsvBgIL+FRtc3/3WL43nHQP9qRmoKOZNMZ2Lb3do/Pxzgp XLShggZ0n+1ItEn1AyB/wrWgDAOFBS/r3UpVE6+FTydWoXyiiFw7bQ4VlJQ95BPnn6rL 5D9A== 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=KnOr7VusvEl2nOBWpkm4SdPaotgSdz121uuu2iozEYg=; b=i63fClHPg+hX0DjyWejjpmUieiaI6a9G+/P0skJruGdO09QICMmiOH9RCTyunIbaNw T0qZXXl9HrAE4qlc2Ne+k4kG65LEO8SUW6BLq+MCjpmVfTOPsWzGUydC7qaEioNOc16B uuXfmMNIbXnaqG/3csYvFs7UM1TOARGZ+yvyxN9xjBJaIi3x3M1vV9y02o6mijNzJdrQ AB8EnBu98ct90u6h07bBy3ON8fi3lFFPz0BiuKX+hqpapP8TDcshh/cMnKrUkk0BYHB1 qX+UKCOsvP3YaM9VpxbrpY/xRR5gZbkj7ILPGlFXNGsCli5SWyEisr8NHgEnjsywIghp 5A5w== X-Gm-Message-State: AOAM5324fyGtds8A8alO03svWzw7fDfJvyn5sPcjIMvujUaKjFqh7dbI 6fXeOg2nA6vVnyZGGulV+xJ61rdDLTWYmmM4uQ== X-Received: from kaleshsingh.mtv.corp.google.com ([2620:15c:211:200:1953:b886:7a6c:bf0]) (user=kaleshsingh job=sendgmr) by 2002:a25:ac11:: with SMTP id w17mr43291157ybi.282.1634693633829; Tue, 19 Oct 2021 18:33:53 -0700 (PDT) Date: Tue, 19 Oct 2021 18:31:38 -0700 In-Reply-To: <20211020013153.4106001-1-kaleshsingh@google.com> Message-Id: <20211020013153.4106001-2-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 1/5] tracing: Add support for creating hist trigger variables from literal 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 Currently hist trigger expressions don't support the use of numeric literals: e.g. echo 'hist:keys=common_pid:x=$y-1234' --> is not valid expression syntax Having the ability to use numeric constants in hist triggers supports a wider range of expressions for creating variables. Add support for creating trace event histogram variables from numeric literals. e.g. echo 'hist:keys=common_pid:x=1234,y=size-1024' >> event/trigger A negative numeric constant is created, using unary minus operator (parentheses are required). e.g. echo 'hist:keys=common_pid:z=-(2)' >> event/trigger Constants can be used with division/multiplication (added in the next patch in this series) to implement granularity filters for frequent trace events. For instance we can limit emitting the rss_stat trace event to when there is a 512KB cross over in the rss size: # Create a synthetic event to monitor instead of the high frequency # rss_stat event echo 'rss_stat_throttled unsigned int mm_id; unsigned int curr; int member; long size' >> tracing/synthetic_events # Create a hist trigger that emits the synthetic rss_stat_throttled # event only when the rss size crosses a 512KB boundary. echo 'hist:keys=keys=mm_id,member:bucket=size/0x80000:onchange($bucket) .rss_stat_throttled(mm_id,curr,member,size)' >> events/kmem/rss_stat/trigger A use case for using constants with addition/subtraction is not yet known, but for completeness the use of constants are supported for all operators. Signed-off-by: Kalesh Singh Reviewed-by: Namhyung Kim --- Changes in v2: - Add description of use case for constants in arithmetic operations, per Steven Rostedt - Add Namhyung's Reviewed-by kernel/trace/trace_events_hist.c | 80 +++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index f01e442716e2..8563a2d51f65 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -66,7 +66,9 @@ C(EMPTY_SORT_FIELD, "Empty sort field"), \ C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \ C(INVALID_SORT_FIELD, "Sort field must be a key or a val"), \ - C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), + C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), \ + C(TOO_MANY_CONSTS, "Too many constants defined"), \ + C(EXPECT_NUMBER, "Expecting numeric literal"), #undef C #define C(a, b) HIST_ERR_##a @@ -89,6 +91,8 @@ typedef u64 (*hist_field_fn_t) (struct hist_field *field, #define HIST_FIELD_OPERANDS_MAX 2 #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX) #define HIST_ACTIONS_MAX 8 +#define HIST_CONST_MAX 4 +#define HIST_CONST_DIGITS_MAX 21 enum field_op_id { FIELD_OP_NONE, @@ -152,6 +156,9 @@ struct hist_field { bool read_once; unsigned int var_str_idx; + + /* Numeric literals are represented as u64 */ + u64 constant; }; static u64 hist_field_none(struct hist_field *field, @@ -163,6 +170,15 @@ static u64 hist_field_none(struct hist_field *field, return 0; } +static u64 hist_field_const(struct hist_field *field, + struct tracing_map_elt *elt, + struct trace_buffer *buffer, + struct ring_buffer_event *rbe, + void *event) +{ + return field->constant; +} + static u64 hist_field_counter(struct hist_field *field, struct tracing_map_elt *elt, struct trace_buffer *buffer, @@ -341,6 +357,7 @@ enum hist_field_flags { HIST_FIELD_FL_CPU = 1 << 15, HIST_FIELD_FL_ALIAS = 1 << 16, HIST_FIELD_FL_BUCKET = 1 << 17, + HIST_FIELD_FL_CONST = 1 << 18, }; struct var_defs { @@ -410,6 +427,7 @@ struct hist_trigger_data { struct field_var *save_vars[SYNTH_FIELDS_MAX]; unsigned int n_save_vars; unsigned int n_save_var_str; + unsigned int n_constants; }; struct action_data; @@ -1516,6 +1534,12 @@ static void expr_field_str(struct hist_field *field, char *expr) { if (field->flags & HIST_FIELD_FL_VAR_REF) strcat(expr, "$"); + else if (field->flags & HIST_FIELD_FL_CONST) { + char str[HIST_CONST_DIGITS_MAX]; + + snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant); + strcat(expr, str); + } strcat(expr, hist_field_name(field, 0)); @@ -1689,6 +1713,15 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, goto out; } + if (flags & HIST_FIELD_FL_CONST) { + hist_field->fn = hist_field_const; + hist_field->size = sizeof(u64); + hist_field->type = kstrdup("u64", GFP_KERNEL); + if (!hist_field->type) + goto free; + goto out; + } + if (flags & HIST_FIELD_FL_STACKTRACE) { hist_field->fn = hist_field_none; goto out; @@ -2090,6 +2123,35 @@ static struct hist_field *create_alias(struct hist_trigger_data *hist_data, return alias; } +static struct hist_field *parse_const(struct hist_trigger_data *hist_data, + char *str, char *var_name, + unsigned long *flags) +{ + struct trace_array *tr = hist_data->event_file->tr; + struct hist_field *field = NULL; + u64 constant; + + if (hist_data->n_constants >= HIST_CONST_MAX) { + hist_err(tr, HIST_ERR_TOO_MANY_CONSTS, errpos(str)); + return NULL; + } + + if (kstrtoull(str, 0, &constant)) { + hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str)); + return NULL; + } + + *flags |= HIST_FIELD_FL_CONST; + field = create_hist_field(hist_data, NULL, *flags, var_name); + if (!field) + return NULL; + + field->constant = constant; + hist_data->n_constants++; + + return field; +} + static struct hist_field *parse_atom(struct hist_trigger_data *hist_data, struct trace_event_file *file, char *str, unsigned long *flags, char *var_name) @@ -2100,6 +2162,15 @@ static struct hist_field *parse_atom(struct hist_trigger_data *hist_data, unsigned long buckets = 0; int ret = 0; + if (isdigit(str[0])) { + hist_field = parse_const(hist_data, str, var_name, flags); + if (!hist_field) { + ret = -EINVAL; + goto out; + } + return hist_field; + } + s = strchr(str, '.'); if (s) { s = strchr(++s, '.'); @@ -4950,6 +5021,8 @@ static void hist_field_debug_show_flags(struct seq_file *m, if (flags & HIST_FIELD_FL_ALIAS) seq_puts(m, " HIST_FIELD_FL_ALIAS\n"); + else if (flags & HIST_FIELD_FL_CONST) + seq_puts(m, " HIST_FIELD_FL_CONST\n"); } static int hist_field_debug_show(struct seq_file *m, @@ -4971,6 +5044,9 @@ static int hist_field_debug_show(struct seq_file *m, field->var.idx); } + if (field->flags & HIST_FIELD_FL_CONST) + seq_printf(m, " constant: %llu\n", field->constant); + if (field->flags & HIST_FIELD_FL_ALIAS) seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n", field->var_ref_idx); @@ -5213,6 +5289,8 @@ static void hist_field_print(struct seq_file *m, struct hist_field *hist_field) if (hist_field->flags & HIST_FIELD_FL_CPU) seq_puts(m, "common_cpu"); + else if (hist_field->flags & HIST_FIELD_FL_CONST) + seq_printf(m, "%llu", hist_field->constant); else if (field_name) { if (hist_field->flags & HIST_FIELD_FL_VAR_REF || hist_field->flags & HIST_FIELD_FL_ALIAS) -- 2.33.0.1079.g6e70778dc9-goog