[PATCH 0/4 v2] tracing: Add a way to have custom events in the tracefs directory
From: Steven Rostedt
Date: Thu Mar 03 2022 - 16:49:30 EST
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