[RFC][PATCH 04/10] tracing: Add trace event accessor functions
From: Tom Zanussi
Date: Fri Feb 12 2016 - 11:12:09 EST
This adds a set of accessor functions for integer and string trace
event fields. When a trace event field is defined, the appropriate
accessor function for that field is chosen and set; clients can access
it via the ftrace_event_field's 'accessor' field.
This code is moved directly from the hist triggers code and the hist
triggers fixed up to make use of it.
Signed-off-by: Tom Zanussi <tom.zanussi@xxxxxxxxxxxxxxx>
---
kernel/trace/trace_events.c | 111 +++++++++++++++++++++++++++++++++++++++
kernel/trace/trace_events_hist.c | 103 ++++--------------------------------
2 files changed, 121 insertions(+), 93 deletions(-)
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 8b54f21..47cb12c 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -70,6 +70,115 @@ static int system_refcount_dec(struct event_subsystem *system)
#define while_for_each_event_file() \
}
+static u64 ftrace_event_field_fn_none(struct ftrace_event_field *field,
+ void *event)
+{
+ return 0;
+}
+
+static u64 ftrace_event_field_fn_string(struct ftrace_event_field *field,
+ void *event)
+{
+ char *addr = (char *)(event + field->offset);
+
+ return (u64)(unsigned long)addr;
+}
+
+static u64 ftrace_event_field_fn_dynstring(struct ftrace_event_field *field,
+ void *event)
+{
+ u32 str_item = *(u32 *)(event + field->offset);
+ int str_loc = str_item & 0xffff;
+ char *addr = (char *)(event + str_loc);
+
+ return (u64)(unsigned long)addr;
+}
+
+static u64 ftrace_event_field_fn_pstring(struct ftrace_event_field *field,
+ void *event)
+{
+ char **addr = (char **)(event + field->offset);
+
+ return (u64)(unsigned long)*addr;
+}
+
+#define DEFINE_FTRACE_EVENT_FIELD_FN(type) \
+static u64 \
+ftrace_event_field_fn_##type(struct ftrace_event_field *field, \
+ void *event) \
+{ \
+ type *addr = (type *)(event + field->offset); \
+ \
+ return (u64)*addr; \
+}
+
+DEFINE_FTRACE_EVENT_FIELD_FN(s64);
+DEFINE_FTRACE_EVENT_FIELD_FN(u64);
+DEFINE_FTRACE_EVENT_FIELD_FN(s32);
+DEFINE_FTRACE_EVENT_FIELD_FN(u32);
+DEFINE_FTRACE_EVENT_FIELD_FN(s16);
+DEFINE_FTRACE_EVENT_FIELD_FN(u16);
+DEFINE_FTRACE_EVENT_FIELD_FN(s8);
+DEFINE_FTRACE_EVENT_FIELD_FN(u8);
+
+static ftrace_event_field_fn_t select_integer_accessor_fn(int field_size,
+ int field_is_signed)
+{
+ ftrace_event_field_fn_t fn = NULL;
+
+ switch (field_size) {
+ case 8:
+ if (field_is_signed)
+ fn = ftrace_event_field_fn_s64;
+ else
+ fn = ftrace_event_field_fn_u64;
+ break;
+ case 4:
+ if (field_is_signed)
+ fn = ftrace_event_field_fn_s32;
+ else
+ fn = ftrace_event_field_fn_u32;
+ break;
+ case 2:
+ if (field_is_signed)
+ fn = ftrace_event_field_fn_s16;
+ else
+ fn = ftrace_event_field_fn_u16;
+ break;
+ case 1:
+ if (field_is_signed)
+ fn = ftrace_event_field_fn_s8;
+ else
+ fn = ftrace_event_field_fn_u8;
+ break;
+ }
+
+ return fn;
+}
+
+static void set_field_accessor(struct ftrace_event_field *field)
+{
+ field->accessor = ftrace_event_field_fn_none;
+
+ if (field && is_function_field(field))
+ return;
+
+ if (is_string_field(field)) {
+ if (field->filter_type == FILTER_STATIC_STRING)
+ field->accessor = ftrace_event_field_fn_string;
+ else if (field->filter_type == FILTER_DYN_STRING)
+ field->accessor = ftrace_event_field_fn_dynstring;
+ else
+ field->accessor = ftrace_event_field_fn_pstring;
+ } else {
+ ftrace_event_field_fn_t fn;
+
+ fn = select_integer_accessor_fn(field->size, field->is_signed);
+ if (fn)
+ field->accessor = fn;
+ }
+}
+
static struct list_head *
trace_get_fields(struct trace_event_call *event_call)
{
@@ -131,6 +240,8 @@ static int __trace_define_field(struct list_head *head, const char *type,
field->size = size;
field->is_signed = is_signed;
+ set_field_accessor(field);
+
list_add(&field->link, head);
return 0;
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index 6e12388..66ca35e 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -30,7 +30,7 @@ typedef u64 (*hist_field_fn_t) (struct hist_field *field, void *event);
struct hist_field {
struct ftrace_event_field *field;
unsigned long flags;
- hist_field_fn_t fn;
+ hist_field_fn_t fn;
unsigned int size;
unsigned int offset;
};
@@ -45,29 +45,6 @@ static u64 hist_field_counter(struct hist_field *field, void *event)
return 1;
}
-static u64 hist_field_string(struct hist_field *hist_field, void *event)
-{
- char *addr = (char *)(event + hist_field->field->offset);
-
- return (u64)(unsigned long)addr;
-}
-
-static u64 hist_field_dynstring(struct hist_field *hist_field, void *event)
-{
- u32 str_item = *(u32 *)(event + hist_field->field->offset);
- int str_loc = str_item & 0xffff;
- char *addr = (char *)(event + str_loc);
-
- return (u64)(unsigned long)addr;
-}
-
-static u64 hist_field_pstring(struct hist_field *hist_field, void *event)
-{
- char **addr = (char **)(event + hist_field->field->offset);
-
- return (u64)(unsigned long)*addr;
-}
-
static u64 hist_field_log2(struct hist_field *hist_field, void *event)
{
u64 val = *(u64 *)(event + hist_field->field->offset);
@@ -75,23 +52,6 @@ static u64 hist_field_log2(struct hist_field *hist_field, void *event)
return (u64) ilog2(roundup_pow_of_two(val));
}
-#define DEFINE_HIST_FIELD_FN(type) \
-static u64 hist_field_##type(struct hist_field *hist_field, void *event)\
-{ \
- type *addr = (type *)(event + hist_field->field->offset); \
- \
- return (u64)*addr; \
-}
-
-DEFINE_HIST_FIELD_FN(s64);
-DEFINE_HIST_FIELD_FN(u64);
-DEFINE_HIST_FIELD_FN(s32);
-DEFINE_HIST_FIELD_FN(u32);
-DEFINE_HIST_FIELD_FN(s16);
-DEFINE_HIST_FIELD_FN(u16);
-DEFINE_HIST_FIELD_FN(s8);
-DEFINE_HIST_FIELD_FN(u8);
-
#define for_each_hist_field(i, hist_data) \
for (i = 0; i < hist_data->n_fields; i++)
@@ -146,40 +106,6 @@ struct hist_trigger_data {
struct tracing_map *map;
};
-static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
-{
- hist_field_fn_t fn = NULL;
-
- switch (field_size) {
- case 8:
- if (field_is_signed)
- fn = hist_field_s64;
- else
- fn = hist_field_u64;
- break;
- case 4:
- if (field_is_signed)
- fn = hist_field_s32;
- else
- fn = hist_field_u32;
- break;
- case 2:
- if (field_is_signed)
- fn = hist_field_s16;
- else
- fn = hist_field_u16;
- break;
- case 1:
- if (field_is_signed)
- fn = hist_field_s8;
- else
- fn = hist_field_u8;
- break;
- }
-
- return fn;
-}
-
static int parse_map_size(char *str)
{
unsigned long size, map_bits;
@@ -372,23 +298,8 @@ static struct hist_field *create_hist_field(struct ftrace_event_field *field,
goto out;
}
- if (is_string_field(field)) {
+ if (is_string_field(field))
flags |= HIST_FIELD_STRING;
-
- if (field->filter_type == FILTER_STATIC_STRING)
- hist_field->fn = hist_field_string;
- else if (field->filter_type == FILTER_DYN_STRING)
- hist_field->fn = hist_field_dynstring;
- else
- hist_field->fn = hist_field_pstring;
- } else {
- hist_field->fn = select_value_fn(field->size,
- field->is_signed);
- if (!hist_field->fn) {
- destroy_hist_field(hist_field);
- return NULL;
- }
- }
out:
hist_field->field = field;
hist_field->flags = flags;
@@ -828,7 +739,10 @@ static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
for_each_hist_val_field(i, hist_data) {
hist_field = hist_data->fields[i];
- hist_val = hist_field->fn(hist_field, rec);
+ if (hist_field->fn)
+ hist_val = hist_field->fn(hist_field, rec);
+ else
+ hist_val = hist_field->field->accessor(hist_field->field, rec);
tracing_map_update_sum(elt, i, hist_val);
}
}
@@ -886,7 +800,10 @@ static void event_hist_trigger(struct event_trigger_data *data, void *rec)
key = entries;
} else {
- field_contents = key_field->fn(key_field, rec);
+ if (key_field->fn)
+ field_contents = key_field->fn(key_field, rec);
+ else
+ field_contents = key_field->field->accessor(key_field->field, rec);
if (key_field->flags & HIST_FIELD_STRING) {
key = (void *)(unsigned long)field_contents;
use_compound_key = true;
--
1.9.3