All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Joel Fernandes <joel@joelfernandes.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Tom Zanussi <zanussi@kernel.org>
Subject: [PATCH 0/4 v2] tracing: Add a way to have custom events in the tracefs directory
Date: Thu, 03 Mar 2022 16:48:32 -0500	[thread overview]
Message-ID: <20220303214832.031378059@goodmis.org> (raw)

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

             reply	other threads:[~2022-03-03 21:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03 21:48 Steven Rostedt [this message]
2022-03-03 21:48 ` [PATCH 1/4 v2] tracing: Allow custom events to be added to the tracefs directory Steven Rostedt
2022-03-03 21:48 ` [PATCH 2/4 v2] tracing: Add sample code for custom trace events Steven Rostedt
2022-03-03 21:48 ` [PATCH 3/4 v2] tracing: Move the defines to create TRACE_EVENTS into their own files Steven Rostedt
2022-03-03 21:48 ` [PATCH 4/4 v2] tracing: Add TRACE_CUSTOM_EVENT() macro Steven Rostedt
2022-03-03 22:00 ` [PATCH 0/4 v2] tracing: Add a way to have custom events in the tracefs directory Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220303214832.031378059@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=joel@joelfernandes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=zanussi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.