tracing: Allow execnames to be passed as args for synthetic events
Steven Rostedt
maintainerPATCH v2 1/1 tracing: Allow execnames to be passed as args for synthetic events
From: "Steven Rostedt (VMware)" <[email protected]>
Allow common_pid.execname to be saved in a variable in one histogram to be passed to another histogram that can pass it as a parameter to a synthetic event.
Quoted 1 line
# echo 'hist:keys=pid:__arg__1=common_timestamp.usecs:arg2=common_pid.execname' \
> events/sched/sched_waking/trigger
Quoted 2 lines
# echo 'wakeup_lat s32 pid; u64 delta; char wake_comm[]' > synthetic_events # echo 'hist:keys=next_pid:pid=next_pid,delta=common_timestamp.usecs-$__arg__1,exec=$arg2'\
':onmatch(sched.sched_waking).trace(wakeup_lat,$pid,$delta,$exec)' \
Quoted 1 line
events/sched/sched_switch/trigger
The above is a wake up latency synthetic event setup that passes the execname of the common_pid that woke the task to the scheduling of that task, which triggers a synthetic event that passes the original execname as a parameter to display it.
Quoted 2 lines
# echo 1 > events/synthetic/enable # cat trace
<idle>-0 [006] d..4 186.863801: wakeup_lat: pid=1306 delta=65 wake_comm=kworker/u16:3
<idle>-0 [000] d..4 186.863858: wakeup_lat: pid=163 delta=27 wake_comm=<idle>
<idle>-0 [001] d..4 186.863903: wakeup_lat: pid=1307 delta=36 wake_comm=kworker/u16:4
<idle>-0 [000] d..4 186.863927: wakeup_lat: pid=163 delta=5 wake_comm=<idle>
<idle>-0 [006] d..4 186.863957: wakeup_lat: pid=1306 delta=24 wake_comm=kworker/u16:3
sshd-1306 [006] d..4 186.864051: wakeup_lat: pid=61 delta=62 wake_comm=<idle>
<idle>-0 [000] d..4 186.965030: wakeup_lat: pid=609 delta=18 wake_comm=<idle>
<idle>-0 [006] d..4 186.987582: wakeup_lat: pid=1306 delta=65 wake_comm=kworker/u16:3
<idle>-0 [000] d..4 186.987639: wakeup_lat: pid=163 delta=27 wake_comm=<idle>---
Changes since v1:
- Hit a bug on freeing the histogram, found that I used "char[]" for the type, and it expects to be freed. Freed the old "type" and still use "char[]" but have the normal freeing use kfree_const() on type.
kernel/trace/trace_events_hist.c | 49 ++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-)
kernel/trace/trace_events_hist.c +44 -5
@@ -1395,17 +1395,17 @@ static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)struct hist_trigger_data *hist_data = elt->map->private_data;unsigned int size =TASK_COMM_LEN ;struct hist_elt_data *elt_data; -struct hist_field *key_field; +struct hist_field *hist_field;unsigned int i, n_str; elt_data =kzalloc (sizeof (*elt_data),GFP_KERNEL );if (!elt_data)return -ENOMEM ; -for_each_hist_key_field (i, hist_data) { - key_field = hist_data->fields[i]; +for_each_hist_field (i, hist_data) { + hist_field = hist_data->fields[i]; -if (key_field->flags &HIST_FIELD_FL_EXECNAME ) { +if (hist_field->flags &HIST_FIELD_FL_EXECNAME ) { elt_data->comm =kzalloc (size,GFP_KERNEL );if (!elt_data->comm) {kfree (elt_data);
@@ -1589,7 +1589,9 @@ static void __destroy_hist_field(struct hist_field *hist_field)kfree (hist_field->var.name);kfree (hist_field->name); -kfree (hist_field->type); + +/* execname vars use a constant type */ +kfree_const (hist_field->type);kfree (hist_field->system);kfree (hist_field->event_name);
@@ -3707,6 +3709,40 @@ static int create_val_field(struct hist_trigger_data *hist_data,return __create_val_field (hist_data, val_idx, file,NULL , field_str,0 ); } +static const char *no_comm ="(no comm)" ; + +static u64 hist_field_execname (struct hist_field *hist_field, +struct tracing_map_elt *elt, +struct trace_buffer *buffer, +struct ring_buffer_event *rbe, +void *event) +{ +struct hist_elt_data *elt_data; + +if (WARN_ON_ONCE (!elt)) +return (u64 )(unsigned long )no_comm; + + elt_data = elt->private_data; + +if (WARN_ON_ONCE (!elt_data->comm)) +return (u64 )(unsigned long )no_comm; + +return (u64 )(unsigned long )(elt_data->comm); +} + +/* Convert a var that points to common_pid.execname to a string */ +static void update_var_execname (struct hist_field *hist_field) +{ + hist_field->flags =HIST_FIELD_FL_STRING |HIST_FIELD_FL_VAR | +HIST_FIELD_FL_EXECNAME ; + hist_field->size =MAX_FILTER_STR_VAL ; + hist_field->is_signed =0 ; +kfree (hist_field->type); +/* This uses kfree_const() to free */ + hist_field->type ="char[]" ; + hist_field->fn = hist_field_execname; +} + static int create_var_field(struct hist_trigger_data *hist_data, unsigned int val_idx, struct trace_event_file *file,
@@ -3731,6 +3767,9 @@ static int create_var_field(struct hist_trigger_data *hist_data, ret =__create_val_field (hist_data, val_idx, file, var_name, expr_str, flags); +if (!ret && hist_data->fields[val_idx]->flags &HIST_FIELD_FL_EXECNAME ) +update_var_execname (hist_data->fields[val_idx]); +if (!ret && hist_data->fields[val_idx]->flags &HIST_FIELD_FL_STRING ) hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;
Signature · 1 line
2.31.1
Masami Hiramatsu
maintainerQuoted 20 lines · On Wed, 21 Jul 2021 21:47:02 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow common_pid.execname to be saved in a variable in one histogram to be passed to another histogram that can pass it as a parameter to a synthetic event.
Quoted 1 line
# echo 'hist:keys=pid:__arg__1=common_timestamp.usecs:arg2=common_pid.execname' \> events/sched/sched_waking/trigger
Quoted 2 lines
# echo 'wakeup_lat s32 pid; u64 delta; char wake_comm[]' > synthetic_events # echo 'hist:keys=next_pid:pid=next_pid,delta=common_timestamp.usecs-$__arg__1,exec=$arg2'\':onmatch(sched.sched_waking).trace(wakeup_lat,$pid,$delta,$exec)' \
Quoted 1 line
events/sched/sched_switch/trigger
The above is a wake up latency synthetic event setup that passes the execname of the
common_pidthat woke the task to the scheduling of that task, which triggers a synthetic event that passes the original execname as a parameter to display it.Quoted 2 lines
# echo 1 > events/synthetic/enable # cat trace
Quoted 10 lines
<idle>-0 [006] d..4 186.863801: wakeup_lat: pid=1306 delta=65 wake_comm=kworker/u16:3
<idle>-0 [000] d..4 186.863858: wakeup_lat: pid=163 delta=27 wake_comm=<idle>
<idle>-0 [001] d..4 186.863903: wakeup_lat: pid=1307 delta=36 wake_comm=kworker/u16:4
<idle>-0 [000] d..4 186.863927: wakeup_lat: pid=163 delta=5 wake_comm=<idle>
<idle>-0 [006] d..4 186.863957: wakeup_lat: pid=1306 delta=24 wake_comm=kworker/u16:3
sshd-1306 [006] d..4 186.864051: wakeup_lat: pid=61 delta=62 wake_comm=<idle>
<idle>-0 [000] d..4 186.965030: wakeup_lat: pid=609 delta=18 wake_comm=<idle>
<idle>-0 [006] d..4 186.987582: wakeup_lat: pid=1306 delta=65 wake_comm=kworker/u16:3
<idle>-0 [000] d..4 186.987639: wakeup_lat: pid=163 delta=27 wake_comm=<idle>This looks an interesting improvement. If we can do this, can we also filter the event by 'comm' (or execname)?
Quoted 6 lines
Signed-off-by Steven Rostedt (VMware) <[email protected]>---
Changes since v1:
- Hit a bug on freeing the histogram, found that I used "char[]" for the type, and it expects to be freed. Freed the old "type" and still use "char[]" but have the normal freeing usekfree_const()on type.
BTW, if we can use this technic on the hist_field->type, we can cleanup other type = strdup("<any const type>", GFP_KERNEL) with type = "<any const type>" ?
Anyway, that will be done in another patch (I think that should be done first, then we can avoid an odd kfree() in update_var_execname().)
Thank you,
Quoted 95 lines
kernel/trace/trace_events_hist.c | 49 ++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-)kernel/trace/trace_events_hist.c +44
-5@@ -1395,17 +1395,17 @@ static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)struct hist_trigger_data *hist_data = elt->map->private_data;unsigned int size =TASK_COMM_LEN ;struct hist_elt_data *elt_data; -struct hist_field *key_field; +struct hist_field *hist_field;unsigned int i, n_str; elt_data =kzalloc (sizeof (*elt_data),GFP_KERNEL );if (!elt_data)return -ENOMEM ; -for_each_hist_key_field (i, hist_data) { - key_field = hist_data->fields[i]; +for_each_hist_field (i, hist_data) { + hist_field = hist_data->fields[i]; -if (key_field->flags &HIST_FIELD_FL_EXECNAME ) { +if (hist_field->flags &HIST_FIELD_FL_EXECNAME ) { elt_data->comm =kzalloc (size,GFP_KERNEL );if (!elt_data->comm) {kfree (elt_data);@@ -1589,7 +1589,9 @@ static void __destroy_hist_field(struct hist_field *hist_field)kfree (hist_field->var.name);kfree (hist_field->name); -kfree (hist_field->type); + +/* execname vars use a constant type */ +kfree_const (hist_field->type);kfree (hist_field->system);kfree (hist_field->event_name);@@ -3707,6 +3709,40 @@ static int create_val_field(struct hist_trigger_data *hist_data,return __create_val_field (hist_data, val_idx, file,NULL , field_str,0 ); } +static const char *no_comm ="(no comm)" ; + +static u64 hist_field_execname (struct hist_field *hist_field, +struct tracing_map_elt *elt, +struct trace_buffer *buffer, +struct ring_buffer_event *rbe, +void *event) +{ +struct hist_elt_data *elt_data; + +if (WARN_ON_ONCE (!elt)) +return (u64 )(unsigned long )no_comm; + + elt_data = elt->private_data; + +if (WARN_ON_ONCE (!elt_data->comm)) +return (u64 )(unsigned long )no_comm; + +return (u64 )(unsigned long )(elt_data->comm); +} + +/* Convert a var that points to common_pid.execname to a string */ +static void update_var_execname (struct hist_field *hist_field) +{ + hist_field->flags =HIST_FIELD_FL_STRING |HIST_FIELD_FL_VAR | +HIST_FIELD_FL_EXECNAME ; + hist_field->size =MAX_FILTER_STR_VAL ; + hist_field->is_signed =0 ; +kfree (hist_field->type); +/* This uses kfree_const() to free */ + hist_field->type ="char[]" ; + hist_field->fn = hist_field_execname; +} + static int create_var_field(struct hist_trigger_data *hist_data, unsigned int val_idx, struct trace_event_file *file,@@ -3731,6 +3767,9 @@ static int create_var_field(struct hist_trigger_data *hist_data, ret =__create_val_field (hist_data, val_idx, file, var_name, expr_str, flags); +if (!ret && hist_data->fields[val_idx]->flags &HIST_FIELD_FL_EXECNAME ) +update_var_execname (hist_data->fields[val_idx]); +if (!ret && hist_data->fields[val_idx]->flags &HIST_FIELD_FL_STRING ) hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;Signature · 1 line
2.31.1
Signature · 1 line
Masami Hiramatsu <[email protected]>
Steven Rostedt
maintainerQuoted 2 lines · On Thu, 22 Jul 2021 22:29:24 +0900 Masami Hiramatsu <[email protected]> wrote:
This looks an interesting improvement. If we can do this, can we also filter the event by 'comm' (or execname)?
We would be able to filter on the synthetic event, just like any other event with a comm (or other string) But I don't think we can filter before (that is, in the histogram code).
Quoted 9 lines
Quoted 7 lines
Signed-off-by Steven Rostedt (VMware) <[email protected]>---
Changes since v1:
- Hit a bug on freeing the histogram, found that I used "char[]" for the type, and it expects to be freed. Freed the old "type" and still use "char[]" but have the normal freeing usekfree_const()on type.
Quoted 5 lines
BTW, if we can use this technic on the
hist_field->type, we can cleanup other type =strdup("<any const type>", GFP_KERNEL)with type = "<any const type>" ?Anyway, that will be done in another patch (I think that should be done first, then we can avoid an odd
kfree()inupdate_var_execname().)
You mean like this? It was done after the patch, but I can move it first.
-- Steve
kernel/trace/trace_events_hist.c +12 -18
@@ -1590,7 +1590,7 @@ static void __destroy_hist_field(struct hist_field *hist_field)kfree (hist_field->var.name);kfree (hist_field->name); -/* execname vars use a constant type */ +/* Can likely be a const */ kfree_const (hist_field->type);kfree (hist_field->system);
@@ -1648,9 +1648,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,if (flags &HIST_FIELD_FL_HITCOUNT ) { hist_field->fn = hist_field_counter; hist_field->size =sizeof (u64); - hist_field->type =kstrdup ("u64" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="u64" ; goto out; }
@@ -1664,7 +1662,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, hist_field->fn = hist_field_log2; hist_field->operands[0 ] =create_hist_field (hist_data, field, fl,NULL ); hist_field->size = hist_field->operands[0 ]->size; - hist_field->type =kstrdup (hist_field->operands[0 ]->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (hist_field->operands[0 ]->type,GFP_KERNEL );if (!hist_field->type) goto free; goto out;
@@ -1673,18 +1671,14 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,if (flags &HIST_FIELD_FL_TIMESTAMP ) { hist_field->fn = hist_field_timestamp; hist_field->size =sizeof (u64); - hist_field->type =kstrdup ("u64" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="u64" ; goto out; }if (flags &HIST_FIELD_FL_CPU ) { hist_field->fn = hist_field_cpu; hist_field->size =sizeof (int ); - hist_field->type =kstrdup ("unsigned int" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="unsigned int" ; goto out; }
@@ -1697,7 +1691,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, flags |=HIST_FIELD_FL_STRING ; hist_field->size =MAX_FILTER_STR_VAL ; - hist_field->type =kstrdup (field->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (field->type,GFP_KERNEL );if (!hist_field->type) goto free;
@@ -1710,7 +1704,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, }else { hist_field->size = field->size; hist_field->is_signed = field->is_signed; - hist_field->type =kstrdup (field->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (field->type,GFP_KERNEL );if (!hist_field->type) goto free;
@@ -1796,7 +1790,7 @@ static int init_var_ref(struct hist_field *ref_field, } } - ref_field->type =kstrdup (var_field->type,GFP_KERNEL ); + ref_field->type =kstrdup_const (var_field->type,GFP_KERNEL );if (!ref_field->type) { err = -ENOMEM ; goto free;
@@ -2165,7 +2159,7 @@ static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, expr->operands[0 ] = operand1; expr->operator =FIELD_OP_UNARY_MINUS ; expr->name =expr_str (expr,0 ); - expr->type =kstrdup (operand1->type,GFP_KERNEL ); + expr->type =kstrdup_const (operand1->type,GFP_KERNEL );if (!expr->type) { ret = -ENOMEM ; goto free;
@@ -2291,7 +2285,7 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, expr->operands[1 ] = operand2; expr->operator = field_op; expr->name =expr_str (expr,0 ); - expr->type =kstrdup (operand1->type,GFP_KERNEL ); + expr->type =kstrdup_const (operand1->type,GFP_KERNEL );if (!expr->type) { ret = -ENOMEM ; goto free;
@@ -2679,10 +2673,10 @@ static struct hist_field *create_var(struct hist_trigger_data *hist_data, var->var.hist_data = var->hist_data = hist_data; var->size = size; var->var.name =kstrdup (name,GFP_KERNEL ); - var->type =kstrdup (type,GFP_KERNEL ); + var->type =kstrdup_const (type,GFP_KERNEL );if (!var->var.name || !var->type) {kfree (var->var.name); -kfree (var->type); +kfree_const (var->type);kfree (var); var =ERR_PTR (-ENOMEM ); }
Quoted 19 lines · On Thu, 22 Jul 2021 09:46:38 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Thu, 22 Jul 2021 22:29:24 +0900 Masami Hiramatsu <[email protected]> wrote:
This looks an interesting improvement. If we can do this, can we also filter the event by 'comm' (or execname)?
We would be able to filter on the synthetic event, just like any other event with a comm (or other string) But I don't think we can filter before (that is, in the histogram code).
Quoted 9 lines
Quoted 7 lines
Signed-off-by Steven Rostedt (VMware) <[email protected]>---
Changes since v1:
- Hit a bug on freeing the histogram, found that I used "char[]" for the type, and it expects to be freed. Freed the old "type" and still use "char[]" but have the normal freeing usekfree_const()on type.
Quoted 7 lines
Quoted 5 lines
BTW, if we can use this technic on the
hist_field->type, we can cleanup other type =strdup("<any const type>", GFP_KERNEL)with type = "<any const type>" ?Anyway, that will be done in another patch (I think that should be done first, then we can avoid an odd
kfree()inupdate_var_execname().)You mean like this? It was done after the patch, but I can move it first.
Yes, this looks good to me.
Thank you,
Quoted 115 lines
-- Steve
kernel/trace/trace_events_hist.c +12
-18@@ -1590,7 +1590,7 @@ static void __destroy_hist_field(struct hist_field *hist_field)kfree (hist_field->var.name);kfree (hist_field->name); -/* execname vars use a constant type */ +/* Can likely be a const */ kfree_const (hist_field->type);kfree (hist_field->system);@@ -1648,9 +1648,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,if (flags &HIST_FIELD_FL_HITCOUNT ) { hist_field->fn = hist_field_counter; hist_field->size =sizeof (u64); - hist_field->type =kstrdup ("u64" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="u64" ; goto out; }@@ -1664,7 +1662,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, hist_field->fn = hist_field_log2; hist_field->operands[0 ] =create_hist_field (hist_data, field, fl,NULL ); hist_field->size = hist_field->operands[0 ]->size; - hist_field->type =kstrdup (hist_field->operands[0 ]->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (hist_field->operands[0 ]->type,GFP_KERNEL );if (!hist_field->type) goto free; goto out;@@ -1673,18 +1671,14 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,if (flags &HIST_FIELD_FL_TIMESTAMP ) { hist_field->fn = hist_field_timestamp; hist_field->size =sizeof (u64); - hist_field->type =kstrdup ("u64" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="u64" ; goto out; }if (flags &HIST_FIELD_FL_CPU ) { hist_field->fn = hist_field_cpu; hist_field->size =sizeof (int ); - hist_field->type =kstrdup ("unsigned int" ,GFP_KERNEL ); -if (!hist_field->type) - goto free; + hist_field->type ="unsigned int" ; goto out; }@@ -1697,7 +1691,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, flags |=HIST_FIELD_FL_STRING ; hist_field->size =MAX_FILTER_STR_VAL ; - hist_field->type =kstrdup (field->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (field->type,GFP_KERNEL );if (!hist_field->type) goto free;@@ -1710,7 +1704,7 @@ static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, }else { hist_field->size = field->size; hist_field->is_signed = field->is_signed; - hist_field->type =kstrdup (field->type,GFP_KERNEL ); + hist_field->type =kstrdup_const (field->type,GFP_KERNEL );if (!hist_field->type) goto free;@@ -1796,7 +1790,7 @@ static int init_var_ref(struct hist_field *ref_field, } } - ref_field->type =kstrdup (var_field->type,GFP_KERNEL ); + ref_field->type =kstrdup_const (var_field->type,GFP_KERNEL );if (!ref_field->type) { err = -ENOMEM ; goto free;@@ -2165,7 +2159,7 @@ static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, expr->operands[0 ] = operand1; expr->operator =FIELD_OP_UNARY_MINUS ; expr->name =expr_str (expr,0 ); - expr->type =kstrdup (operand1->type,GFP_KERNEL ); + expr->type =kstrdup_const (operand1->type,GFP_KERNEL );if (!expr->type) { ret = -ENOMEM ; goto free;@@ -2291,7 +2285,7 @@ static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, expr->operands[1 ] = operand2; expr->operator = field_op; expr->name =expr_str (expr,0 ); - expr->type =kstrdup (operand1->type,GFP_KERNEL ); + expr->type =kstrdup_const (operand1->type,GFP_KERNEL );if (!expr->type) { ret = -ENOMEM ; goto free;@@ -2679,10 +2673,10 @@ static struct hist_field *create_var(struct hist_trigger_data *hist_data, var->var.hist_data = var->hist_data = hist_data; var->size = size; var->var.name =kstrdup (name,GFP_KERNEL ); - var->type =kstrdup (type,GFP_KERNEL ); + var->type =kstrdup_const (type,GFP_KERNEL );if (!var->var.name || !var->type) {kfree (var->var.name); -kfree (var->type); +kfree_const (var->type);kfree (var); var =ERR_PTR (-ENOMEM ); }
Signature · 1 line
Masami Hiramatsu <[email protected]>