Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tracing: Allow execnames to be passed as args for synthetic events

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.

># echo 'hist:keys=pid:__arg__1=common_timestamp.usecs:arg2=common_pid.execname' \
> events/sched/sched_waking/trigger
># 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)' \
> 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.

># 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>

Link: https://lkml.kernel.org/r/20210722142837.458596338@goodmis.org

Reviewed-by: Tom Zanussi <zanussi@kernel.org>
Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>

+42 -4
+42 -4
kernel/trace/trace_events_hist.c
··· 1420 1420 struct hist_trigger_data *hist_data = elt->map->private_data; 1421 1421 unsigned int size = TASK_COMM_LEN; 1422 1422 struct hist_elt_data *elt_data; 1423 - struct hist_field *key_field; 1423 + struct hist_field *hist_field; 1424 1424 unsigned int i, n_str; 1425 1425 1426 1426 elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); 1427 1427 if (!elt_data) 1428 1428 return -ENOMEM; 1429 1429 1430 - for_each_hist_key_field(i, hist_data) { 1431 - key_field = hist_data->fields[i]; 1430 + for_each_hist_field(i, hist_data) { 1431 + hist_field = hist_data->fields[i]; 1432 1432 1433 - if (key_field->flags & HIST_FIELD_FL_EXECNAME) { 1433 + if (hist_field->flags & HIST_FIELD_FL_EXECNAME) { 1434 1434 elt_data->comm = kzalloc(size, GFP_KERNEL); 1435 1435 if (!elt_data->comm) { 1436 1436 kfree(elt_data); ··· 3771 3771 return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0); 3772 3772 } 3773 3773 3774 + static const char *no_comm = "(no comm)"; 3775 + 3776 + static u64 hist_field_execname(struct hist_field *hist_field, 3777 + struct tracing_map_elt *elt, 3778 + struct trace_buffer *buffer, 3779 + struct ring_buffer_event *rbe, 3780 + void *event) 3781 + { 3782 + struct hist_elt_data *elt_data; 3783 + 3784 + if (WARN_ON_ONCE(!elt)) 3785 + return (u64)(unsigned long)no_comm; 3786 + 3787 + elt_data = elt->private_data; 3788 + 3789 + if (WARN_ON_ONCE(!elt_data->comm)) 3790 + return (u64)(unsigned long)no_comm; 3791 + 3792 + return (u64)(unsigned long)(elt_data->comm); 3793 + } 3794 + 3795 + /* Convert a var that points to common_pid.execname to a string */ 3796 + static void update_var_execname(struct hist_field *hist_field) 3797 + { 3798 + hist_field->flags = HIST_FIELD_FL_STRING | HIST_FIELD_FL_VAR | 3799 + HIST_FIELD_FL_EXECNAME; 3800 + hist_field->size = MAX_FILTER_STR_VAL; 3801 + hist_field->is_signed = 0; 3802 + 3803 + kfree_const(hist_field->type); 3804 + hist_field->type = "char[]"; 3805 + 3806 + hist_field->fn = hist_field_execname; 3807 + } 3808 + 3774 3809 static int create_var_field(struct hist_trigger_data *hist_data, 3775 3810 unsigned int val_idx, 3776 3811 struct trace_event_file *file, ··· 3829 3794 return -EINVAL; 3830 3795 3831 3796 ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags); 3797 + 3798 + if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_EXECNAME) 3799 + update_var_execname(hist_data->fields[val_idx]); 3832 3800 3833 3801 if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING) 3834 3802 hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++;