Skip to content

Commit 52cfb37

Browse files
Kalesh Singhrostedt
authored andcommitted
tracing: Add support for creating hist trigger variables from literal
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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kalesh Singh <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 25b9513 commit 52cfb37

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

kernel/trace/trace_events_hist.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@
6666
C(EMPTY_SORT_FIELD, "Empty sort field"), \
6767
C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \
6868
C(INVALID_SORT_FIELD, "Sort field must be a key or a val"), \
69-
C(INVALID_STR_OPERAND, "String type can not be an operand in expression"),
69+
C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), \
70+
C(EXPECT_NUMBER, "Expecting numeric literal"),
7071

7172
#undef C
7273
#define C(a, b) HIST_ERR_##a
@@ -89,6 +90,7 @@ typedef u64 (*hist_field_fn_t) (struct hist_field *field,
8990
#define HIST_FIELD_OPERANDS_MAX 2
9091
#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
9192
#define HIST_ACTIONS_MAX 8
93+
#define HIST_CONST_DIGITS_MAX 21
9294

9395
enum field_op_id {
9496
FIELD_OP_NONE,
@@ -152,6 +154,9 @@ struct hist_field {
152154
bool read_once;
153155

154156
unsigned int var_str_idx;
157+
158+
/* Numeric literals are represented as u64 */
159+
u64 constant;
155160
};
156161

157162
static u64 hist_field_none(struct hist_field *field,
@@ -163,6 +168,15 @@ static u64 hist_field_none(struct hist_field *field,
163168
return 0;
164169
}
165170

171+
static u64 hist_field_const(struct hist_field *field,
172+
struct tracing_map_elt *elt,
173+
struct trace_buffer *buffer,
174+
struct ring_buffer_event *rbe,
175+
void *event)
176+
{
177+
return field->constant;
178+
}
179+
166180
static u64 hist_field_counter(struct hist_field *field,
167181
struct tracing_map_elt *elt,
168182
struct trace_buffer *buffer,
@@ -341,6 +355,7 @@ enum hist_field_flags {
341355
HIST_FIELD_FL_CPU = 1 << 15,
342356
HIST_FIELD_FL_ALIAS = 1 << 16,
343357
HIST_FIELD_FL_BUCKET = 1 << 17,
358+
HIST_FIELD_FL_CONST = 1 << 18,
344359
};
345360

346361
struct var_defs {
@@ -1516,6 +1531,12 @@ static void expr_field_str(struct hist_field *field, char *expr)
15161531
{
15171532
if (field->flags & HIST_FIELD_FL_VAR_REF)
15181533
strcat(expr, "$");
1534+
else if (field->flags & HIST_FIELD_FL_CONST) {
1535+
char str[HIST_CONST_DIGITS_MAX];
1536+
1537+
snprintf(str, HIST_CONST_DIGITS_MAX, "%llu", field->constant);
1538+
strcat(expr, str);
1539+
}
15191540

15201541
strcat(expr, hist_field_name(field, 0));
15211542

@@ -1689,6 +1710,15 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
16891710
goto out;
16901711
}
16911712

1713+
if (flags & HIST_FIELD_FL_CONST) {
1714+
hist_field->fn = hist_field_const;
1715+
hist_field->size = sizeof(u64);
1716+
hist_field->type = kstrdup("u64", GFP_KERNEL);
1717+
if (!hist_field->type)
1718+
goto free;
1719+
goto out;
1720+
}
1721+
16921722
if (flags & HIST_FIELD_FL_STACKTRACE) {
16931723
hist_field->fn = hist_field_none;
16941724
goto out;
@@ -2090,6 +2120,29 @@ static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
20902120
return alias;
20912121
}
20922122

2123+
static struct hist_field *parse_const(struct hist_trigger_data *hist_data,
2124+
char *str, char *var_name,
2125+
unsigned long *flags)
2126+
{
2127+
struct trace_array *tr = hist_data->event_file->tr;
2128+
struct hist_field *field = NULL;
2129+
u64 constant;
2130+
2131+
if (kstrtoull(str, 0, &constant)) {
2132+
hist_err(tr, HIST_ERR_EXPECT_NUMBER, errpos(str));
2133+
return NULL;
2134+
}
2135+
2136+
*flags |= HIST_FIELD_FL_CONST;
2137+
field = create_hist_field(hist_data, NULL, *flags, var_name);
2138+
if (!field)
2139+
return NULL;
2140+
2141+
field->constant = constant;
2142+
2143+
return field;
2144+
}
2145+
20932146
static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
20942147
struct trace_event_file *file, char *str,
20952148
unsigned long *flags, char *var_name)
@@ -2100,6 +2153,15 @@ static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
21002153
unsigned long buckets = 0;
21012154
int ret = 0;
21022155

2156+
if (isdigit(str[0])) {
2157+
hist_field = parse_const(hist_data, str, var_name, flags);
2158+
if (!hist_field) {
2159+
ret = -EINVAL;
2160+
goto out;
2161+
}
2162+
return hist_field;
2163+
}
2164+
21032165
s = strchr(str, '.');
21042166
if (s) {
21052167
s = strchr(++s, '.');
@@ -4945,6 +5007,8 @@ static void hist_field_debug_show_flags(struct seq_file *m,
49455007

49465008
if (flags & HIST_FIELD_FL_ALIAS)
49475009
seq_puts(m, " HIST_FIELD_FL_ALIAS\n");
5010+
else if (flags & HIST_FIELD_FL_CONST)
5011+
seq_puts(m, " HIST_FIELD_FL_CONST\n");
49485012
}
49495013

49505014
static int hist_field_debug_show(struct seq_file *m,
@@ -4966,6 +5030,9 @@ static int hist_field_debug_show(struct seq_file *m,
49665030
field->var.idx);
49675031
}
49685032

5033+
if (field->flags & HIST_FIELD_FL_CONST)
5034+
seq_printf(m, " constant: %llu\n", field->constant);
5035+
49695036
if (field->flags & HIST_FIELD_FL_ALIAS)
49705037
seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n",
49715038
field->var_ref_idx);
@@ -5208,6 +5275,8 @@ static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
52085275

52095276
if (hist_field->flags & HIST_FIELD_FL_CPU)
52105277
seq_puts(m, "common_cpu");
5278+
else if (hist_field->flags & HIST_FIELD_FL_CONST)
5279+
seq_printf(m, "%llu", hist_field->constant);
52115280
else if (field_name) {
52125281
if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
52135282
hist_field->flags & HIST_FIELD_FL_ALIAS)

0 commit comments

Comments
 (0)