Skip to content

tracing: Add a way to have custom events in the tracefs directory

Steven Rostedt lkml 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest4 patches 2 repliesIn review
  2. v24 patches 1 replySuperseded
  3. v12 patches 7 repliesSuperseded

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4tracing: Allow custom events to be added to the tracefs directorykernel/trace/trace_events.c+2 -00 replies
2/4tracing: Add sample code for custom trace eventsarch/x86/kernel/kprobes/core.c kernel/trace/ftrace.c samples/Kconfig +3 more+314 -52 replies
3/4tracing: Move the defines to create TRACE_EVENTS into their own filesinclude/trace/stages/init.h include/trace/stages/stage1_defines.h include/trace/stages/stage2_defines.h +6 more+528 -4910 replies
4/4tracing: Add TRACE_CUSTOM_EVENT() macroinclude/linux/trace_events.h include/trace/define_custom_trace.h include/trace/trace_custom_events.h +3 more+441 -2370 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v3 yet.

Read the v3 thread

Cover letter [PATCH 0/4 v3] 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)

Changes since v2: https://lore.kernel.org/all/[email protected]/
Forgot to do a:
git add include/trace/trace_custom_events.h include/trace/define_custom_trace.h
in v2.

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/define_custom_trace.h       |  77 +++++
 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_custom_events.h       | 221 +++++++++++++
 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 ++++++
 20 files changed, 1049 insertions(+), 497 deletions(-)
 create mode 100644 include/trace/define_custom_trace.h
 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 include/trace/trace_custom_events.h
 create mode 100644 samples/trace_events/trace_custom_sched.c
 create mode 100644 samples/trace_events/trace_custom_sched.h