tracing: Add a way to have custom events in the tracefs directory
Steven Rostedt
maintainerv2 cover tracing: Add a way to have custom events in the tracefs directory
We would like to have in production a way to record sched wakeups and sched switch, and be able to save the information in a small file with as much available as possible. Currently the wake up and sched switch events are 36 and 64 bytes each (plus a 4 byte ring buffer event header).
By having a custom module tap into the sched switch and waking trace points we can bring those events down to 16 and 14 bytes respectively.
This version adds the new TRACE_CUSTOM_EVENT() which makes creating a custom event as easy as creating a TRACE_EVENT()!
The TRACE_CUSTOM_EVENT() macro does all the work to create the event, and has the same format as the TRACE_EVENT() does.
Note, currently perf and bpf do not hook to this, but we can add that later.
I kept patch 2 that has the complex way of hand coding the custom event just to keep the histor of it.
But now, to add a custom event for sched_switch, all that needs to be done is:
trace_custom_sched.c:
-------------------------%<------------------------- #include <linux/trace_events.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <trace/events/sched.h>
#define CREATE_CUSTOM_TRACE_EVENTS
#include "trace_custom_sched.h"
static void fct(struct tracepoint *tp, void *priv)
{
trace_custom_event_sched_switch_update(tp);
}static int __init trace_sched_init(void)
{
for_each_kernel_tracepoint(fct, NULL);
return 0;
}static void __exit trace_sched_exit(void)
{
}
module_init(trace_sched_init); module_exit(trace_sched_exit); ------------------------->%-------------------------
-------------------------%<-------------------------
#if !defined(_TRACE_CUSTOM_SCHED_H) || defined(TRACE_CUSTOM_MULTI_READ) #define _TRACE_CUSTOM_SCHED_H
#include <linux/trace_events.h>
TRACE_CUSTOM_EVENT(sched_switch,
/* The below must be the same as the original sched_switch */
TP_PROTO(bool preempt,
struct task_struct *prev,
struct task_struct *next),
TP_ARGS(preempt, prev, next),
/* The below is the customization */
TP_STRUCT__entry(
__field( unsigned short, prev_prio )
__field( unsigned short, next_prio )
__field( pid_t, next_pid )
),
TP_fast_assign(
__entry->prev_prio = prev->prio;
__entry->next_pid = next->pid;
__entry->next_prio = next->prio;
),
TP_printk("prev_prio=%d next_pid=%d next_prio=%d",
__entry->prev_prio, __entry->next_pid, __entry->next_prio))
#endif
#undef TRACE_INCLUDE_PATH
#undef TRACE_INCLUDE_FILE
#define TRACE_INCLUDE_PATH .
#define TRACE_INCLUDE_FILE trace_custom_sched #include <trace/define_custom_trace.h> ------------------------->%-------------------------
And update the Makefile to have:
CFLAGS_trace_custom_sched.o := -I$(src)
Steven Rostedt (Google) (4):
tracing: Allow custom events to be added to the tracefs directory
tracing: Add sample code for custom trace events
tracing: Move the defines to create TRACE_EVENTS into their own files
tracing: Add TRACE_CUSTOM_EVENT() macro
----
arch/x86/kernel/kprobes/core.c | 4 +- include/linux/trace_events.h | 24 +- include/trace/stages/init.h | 37 +++ include/trace/stages/stage1_defines.h | 46 +++ include/trace/stages/stage2_defines.h | 48 +++ include/trace/stages/stage3_defines.h | 129 ++++++++ include/trace/stages/stage4_defines.h | 57 ++++ include/trace/stages/stage5_defines.h | 83 +++++ include/trace/stages/stage6_defines.h | 86 +++++ include/trace/stages/stage7_defines.h | 34 ++ include/trace/trace_events.h | 499 +----------------------------- kernel/trace/ftrace.c | 33 +- kernel/trace/trace_events.c | 2 + samples/Kconfig | 8 +- samples/Makefile | 1 + samples/trace_events/Makefile | 2 + samples/trace_events/trace_custom_sched.c | 60 ++++ samples/trace_events/trace_custom_sched.h | 95 ++++++ 18 files changed, 751 insertions(+), 497 deletions(-) create mode 100644 include/trace/stages/init.h create mode 100644 include/trace/stages/stage1_defines.h create mode 100644 include/trace/stages/stage2_defines.h create mode 100644 include/trace/stages/stage3_defines.h create mode 100644 include/trace/stages/stage4_defines.h create mode 100644 include/trace/stages/stage5_defines.h create mode 100644 include/trace/stages/stage6_defines.h create mode 100644 include/trace/stages/stage7_defines.h create mode 100644 samples/trace_events/trace_custom_sched.c create mode 100644 samples/trace_events/trace_custom_sched.h
Steven Rostedt
maintainerPATCH v2 1/4 tracing: Allow custom events to be added to the tracefs directory
From: "Steven Rostedt (Google)" <[email protected]>
Allow custom events to be added to the events directory in the tracefs file system. For example, a module could be installed that attaches to an event and wants to be enabled and disabled via the tracefs file system. It would use trace_add_event_call() to add the event to the tracefs directory, and trace_remove_event_call() to remove it.
Make both those functions EXPORT_SYMBOL_GPL().
--- kernel/trace/trace_events.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/trace/trace_events.c +2 -0
@@ -2758,6 +2758,7 @@ int trace_add_event_call(struct trace_event_call *call)mutex_unlock (&trace_types_lock);return ret; } +EXPORT_SYMBOL_GPL (trace_add_event_call);/* * Must be called under locking of trace_types_lock, event_mutex and
@@ -2819,6 +2820,7 @@ int trace_remove_event_call(struct trace_event_call *call)return ret; } +EXPORT_SYMBOL_GPL (trace_remove_event_call);#define for_each_event (event, start, end) \ for (event = start; \
Signature · 1 line
2.34.1
Steven Rostedt
maintainerPATCH v2 2/4 tracing: Add sample code for custom trace events
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add sample code to show how to create custom trace events in the tracefs directory that can be enabled and modified like any event in tracefs (including triggers, histograms, synthetic events and event probes).
The example is creating a custom sched_switch and a sched_waking to limit what is recorded:
If the custom sched switch only records the prev_prio, next_prio and next_pid, it can bring the size from 64 bytes per event, down to just 16 bytes!
If sched_waking only records the prio and pid of the woken event, it will bring the size down from 36 bytes to 12 bytes per event.
This will allow for a much smaller footprint into the ring buffer and keep more events from dropping.
--- arch/x86/kernel/kprobes/core.c | 4 +- kernel/trace/ftrace.c | 33 ++- samples/Kconfig | 8 +- samples/Makefile | 1 + samples/trace_events/Makefile | 2 + samples/trace_events/trace_custom_sched.c | 271 ++++++++++++++++++++++ 6 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 samples/trace_events/trace_custom_sched.c
arch/x86/kernel/kprobes/core.c +3 -1
@@ -202,8 +202,10 @@ __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)* arch_check_ftrace_location(). Something went terribly wrong * if such an address is checked here. */ -if (WARN_ON (faddr && faddr != addr)) +if (WARN_ON (faddr && faddr != addr)) { +printk ("faddr=%pS (%lx) addr=%pS (%lx)\n" , (void *)faddr, faddr, (void *)addr, addr);return 0UL ; + }/* * Use the current code if it is not modified by Kprobe * and it cannot be modified by ftrace.
kernel/trace/ftrace.c +30 -3
@@ -1578,7 +1578,24 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end)*/ unsigned long ftrace_location (unsigned long ip) { -return ftrace_location_range (ip, ip); +struct dyn_ftrace *rec; +unsigned long offset; +unsigned long size; + + rec =lookup_rec (ip, ip); +if (!rec) { +if (!kallsyms_lookup_size_offset (ip, &size, &offset)) + goto out; + +if (!offset) + rec =lookup_rec (ip - offset, (ip - offset) + size -1 ); + } + +if (rec) +return rec->ip; + +out: +return 0 ; }/**
@@ -4962,7 +4979,8 @@ ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) {struct ftrace_func_entry *entry; -if (!ftrace_location (ip)) + ip =ftrace_location (ip); +if (!ip)return -EINVAL ;if (remove) {
@@ -5110,11 +5128,16 @@ int register_ftrace_direct(unsigned long ip, unsigned long addr)struct ftrace_func_entry *entry;struct ftrace_hash *free_hash =NULL ;struct dyn_ftrace *rec; -int ret = -EBUSY ; +int ret = -ENODEV ;mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; +/* See if there's a direct function at @ip already */ + ret = -EBUSY ;if (ftrace_find_rec_direct (ip)) goto out_unlock;
@@ -5222,6 +5245,10 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr)mutex_lock (&direct_mutex); + ip =ftrace_location (ip); +if (!ip) + goto out_unlock; + entry =find_direct_entry (&ip,NULL );if (!entry) goto out_unlock;
samples/Kconfig +7 -1
@@ -14,7 +14,13 @@ config SAMPLE_TRACE_EVENTS tristate "Build trace_events examples -- loadable modules only" depends on EVENT_TRACING && m help - This build trace event example modules. + This builds the trace event example module. + +config SAMPLE_TRACE_CUSTOM_EVENTS + tristate "Build custom trace event example -- loadable modules only" + depends on EVENT_TRACING && m + help + This builds the custom trace event example module. config SAMPLE_TRACE_PRINTK tristate "Build trace_printk module - tests various trace_printk formats"
samples/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT) += rpmsg/ subdir-$(CONFIG_SAMPLE_SECCOMP) += seccomp subdir-$(CONFIG_SAMPLE_TIMER) += timers obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace_events/ +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_events/ obj-$(CONFIG_SAMPLE_TRACE_PRINTK) += trace_printk/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace/ obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace/
samples/trace_events/Makefile +2 -0
@@ -13,3 +13,5 @@ CFLAGS_trace-events-sample.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o + +obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_custom_sched.o
samples/trace_events/trace_custom_sched.c new file +271 -0
271 lines not on this page · raw message
Signature · 1 line
2.34.1
Steven Rostedt
maintainerPATCH v2 3/4 tracing: Move the defines to create TRACE_EVENTS into their own files
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
In an effort to add custom event macros that can be used to create your own custom events based on existing tracepoints, move the defines of the special macros used in TRACE_EVENT() into their own files such that they can be reused for TRACE_CUSTOM_EVENT().
--- include/trace/stages/init.h | 37 ++ include/trace/stages/stage1_defines.h | 46 +++ include/trace/stages/stage2_defines.h | 48 +++ include/trace/stages/stage3_defines.h | 129 +++++++ include/trace/stages/stage4_defines.h | 57 +++ include/trace/stages/stage5_defines.h | 83 +++++ include/trace/stages/stage6_defines.h | 86 +++++ include/trace/stages/stage7_defines.h | 34 ++ include/trace/trace_events.h | 499 +------------------------- 9 files changed, 528 insertions(+), 491 deletions(-) create mode 100644 include/trace/stages/init.h create mode 100644 include/trace/stages/stage1_defines.h create mode 100644 include/trace/stages/stage2_defines.h create mode 100644 include/trace/stages/stage3_defines.h create mode 100644 include/trace/stages/stage4_defines.h create mode 100644 include/trace/stages/stage5_defines.h create mode 100644 include/trace/stages/stage6_defines.h create mode 100644 include/trace/stages/stage7_defines.h
include/trace/stages/init.h new file +37 -0
@@ -0,0 +1,37 @@ + +#define __app__ (x, y) str__##x##y +#define __app (x, y) __app__(x, y) + +#define TRACE_SYSTEM_STRING __app(TRACE_SYSTEM_VAR,__trace_system_name) + +#define TRACE_MAKE_SYSTEM_STR () \ + static const char TRACE_SYSTEM_STRING[] = \ + __stringify(TRACE_SYSTEM) + +TRACE_MAKE_SYSTEM_STR (); + +#undef TRACE_DEFINE_ENUM +#define TRACE_DEFINE_ENUM (a) \ + static struct trace_eval_map __used __initdata \ + __##TRACE_SYSTEM##_##a = \ + { \ + .system = TRACE_SYSTEM_STRING, \ + .eval_string = #a, \ + .eval_value = a \ + }; \ + static struct trace_eval_map __used \ + __section("_ftrace_eval_map") \ + *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a + +#undef TRACE_DEFINE_SIZEOF +#define TRACE_DEFINE_SIZEOF (a) \ + static struct trace_eval_map __used __initdata \ + __##TRACE_SYSTEM##_##a = \ + { \ + .system = TRACE_SYSTEM_STRING, \ + .eval_string = "sizeof(" #a ")", \ + .eval_value = sizeof(a) \ + }; \ + static struct trace_eval_map __used \ + __section("_ftrace_eval_map") \ + *TRACE_SYSTEM##_##a = &__##TRACE_SYSTEM##_##a
include/trace/stages/stage1_defines.h new file +46 -0
46 lines not on this page · raw message
include/trace/stages/stage2_defines.h new file +48 -0
48 lines not on this page · raw message
include/trace/stages/stage3_defines.h new file +129 -0
129 lines not on this page · raw message
include/trace/stages/stage4_defines.h new file +57 -0
57 lines not on this page · raw message
include/trace/stages/stage5_defines.h new file +83 -0
83 lines not on this page · raw message
include/trace/stages/stage6_defines.h new file +86 -0
86 lines not on this page · raw message
include/trace/stages/stage7_defines.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* Stage 7 definitions for creating trace events */ + +#undef __entry +#define __entry REC + +#undef __print_flags +#undef __print_symbolic +#undef __print_hex +#undef __print_hex_str +#undef __get_dynamic_array +#undef __get_dynamic_array_len +#undef __get_str +#undef __get_bitmask +#undef __get_rel_dynamic_array +#undef __get_rel_dynamic_array_len +#undef __get_rel_str +#undef __get_rel_bitmask +#undef __print_array +#undef __print_hex_dump + +/* +* The below is not executed in the kernel. It is only what is +* displayed in the print format for userspace to parse. +*/ +#undef __print_ns_to_secs +#define __print_ns_to_secs (val) (val) / 1000000000UL + +#undef __print_ns_without_secs +#define __print_ns_without_secs (val) (val) % 1000000000UL + +#undef TP_printk +#define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args)
include/trace/trace_events.h +8 -491
547 lines not on this page · raw message
Signature · 1 line
2.34.1
Steven Rostedt
maintainerPATCH v2 4/4 tracing: Add TRACE_CUSTOM_EVENT() macro
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
To make it really easy to add custom events from modules, add a TRACE_CUSTOM_EVENT() macro that acts just like the TRACE_EVENT() macro, but creates a custom event to an already existing tracepoint.
The trace_custom_sched.[ch] has been updated to use this new macro to show how simple it is.
--- include/linux/trace_events.h | 24 +- samples/trace_events/Makefile | 2 +- samples/trace_events/trace_custom_sched.c | 259 ++-------------------- samples/trace_events/trace_custom_sched.h | 95 ++++++++ 4 files changed, 143 insertions(+), 237 deletions(-) create mode 100644 samples/trace_events/trace_custom_sched.h
include/linux/trace_events.h +23 -1
@@ -315,6 +315,7 @@ enum {TRACE_EVENT_FL_KPROBE_BIT ,TRACE_EVENT_FL_UPROBE_BIT ,TRACE_EVENT_FL_EPROBE_BIT , + TRACE_EVENT_FL_CUSTOM_BIT, };/*
@@ -328,6 +329,9 @@ enum {* KPROBE - Event is a kprobe * UPROBE - Event is a uprobe * EPROBE - Event is an event probe +* CUSTOM - Event is a custom event (to be attached to an exsiting tracepoint) +* This is set when the custom event has not been attached +* to a tracepoint yet, then it is cleared when it is. */ enum {TRACE_EVENT_FL_FILTERED = (1 <<TRACE_EVENT_FL_FILTERED_BIT ),
@@ -339,6 +343,7 @@ enum {TRACE_EVENT_FL_KPROBE = (1 <<TRACE_EVENT_FL_KPROBE_BIT ),TRACE_EVENT_FL_UPROBE = (1 <<TRACE_EVENT_FL_UPROBE_BIT ),TRACE_EVENT_FL_EPROBE = (1 <<TRACE_EVENT_FL_EPROBE_BIT ), + TRACE_EVENT_FL_CUSTOM = (1 << TRACE_EVENT_FL_CUSTOM_BIT), };#define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
@@ -440,7 +445,9 @@ static inline bool bpf_prog_array_valid(struct trace_event_call *call)static inline const char *trace_event_name (struct trace_event_call *call) { -if (call->flags &TRACE_EVENT_FL_TRACEPOINT ) +if (call->flags &TRACE_EVENT_FL_CUSTOM ) +return call->name; +else if (call->flags &TRACE_EVENT_FL_TRACEPOINT )return call->tp ? call->tp->name :NULL ;else return call->name;
@@ -903,3 +910,18 @@ perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,#endif #endif /* _LINUX_TRACE_EVENT_H */ + +/* +* Note: we keep the TRACE_CUSTOM_EVENT outside the include file ifdef protection. +* This is due to the way trace custom events work. If a file includes two +* trace event headers under one "CREATE_CUSTOM_TRACE_EVENTS" the first include +* will override the TRACE_CUSTOM_EVENT and break the second include. +*/ + +#ifndef TRACE_CUSTOM_EVENT + +#define DECLARE_CUSTOM_EVENT_CLASS (name, proto, args, tstruct, assign, print) +#define DEFINE_CUSTOM_EVENT (template, name, proto, args) +#define TRACE_CUSTOM_EVENT (name, proto, args, struct, assign, print) + +#endif /* ifdef TRACE_CUSTOM_EVENT (see note above) */
samples/trace_events/Makefile +1 -1
@@ -11,7 +11,7 @@ # Here trace-events-sample.c does the CREATE_TRACE_POINTS. # CFLAGS_trace-events-sample.o := -I$(src) +CFLAGS_trace_custom_sched.o := -I$(src) obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o - obj-$(CONFIG_SAMPLE_TRACE_CUSTOM_EVENTS) += trace_custom_sched.o
samples/trace_events/trace_custom_sched.c +24 -235
280 lines not on this page · raw message
samples/trace_events/trace_custom_sched.h new file +95 -0
95 lines not on this page · raw message
Signature · 1 line
2.34.1
Steven Rostedt
maintainerQuoted 3 lines · On Thu, 03 Mar 2022 16:48:32 -0500 Steven Rostedt <[email protected]> wrote:
#define
TRACE_INCLUDE_FILEtrace_custom_sched#include <trace/define_custom_trace.h> ------------------------->%-------------------------
It appears that I forgot to do a:
git add include/trace/trace_custom_events.h include/trace/define_custom_trace.h
and those two very important files are missing from the last patch.
Will send a v3.
-- Steve