Skip to contentSkip to conversation

tracing: Introduce TRACE_MARKER() no argument trace event

RFC PATCH In review Steven Rostedt 3 participants 6 messages Feb 7, 2014 – Feb 13, 2014

Subsystems (3)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

RFC PATCH 1/1 tracing: Introduce TRACE_MARKER() no argument trace event

A while back ago, Wolfgang (and others) have asked about the ability to add a trace event that recorder no data other than the fact that the trace event was hit.

I've been reluctant to do so, but I noticed that one already exists in the iwlwifi tracepoints (iwlwifi_dev_irq) where it does a wasteful:

        /* TP_printk("") doesn't compile */
        TP_printk("%d", 0)

The reason this is wasteful, is that there's a lot of code generated by the TRACE_EVENT() macros that end up basically being nops.

I figured I would instead create a TRACE_MARKER(name, print) that would be something like:

Added to tracepoint header:

TRACE_MARKER(tpname, "Show this message");

Then you have:

	trace_tpname();

in the code.

Notice that the tracepoint function (trace_<name>()) has no arguments. That's because the message is stored in the tracepoint (in one place) and is printed when the tracepoint is read. That is, the message isn't even recorded in the ring buffer.

It still shows up in the tracepoint format file:

name: tpname
ID: 281
format:

	field:unsigned short common_type;	offset:0;	size:2;	signed:0;
	field:unsigned char common_flags;	offset:2;	size:1;	signed:0;
	field:unsigned char common_preempt_count;	offset:3;	size:1;	signed:0;
	field:int common_pid;	offset:4;	size:4;	signed:1;

print fmt: "Show this message"

The TRACE_MARKER() is basically an optimized version of TRACE_EVENT() with no arguments. It can be enabled and disabled the same way as any other event, and stays within the system it was created in.

Is this worth doing?

Attached is a patch that implements this idea, but it is very much in a beta form. It requires comments, documentation, updates to perf, etc.

-- Steve

include/linux/ftrace_event.h +6 -0
index 884d8ad..e464511 100644
@@ -526,6 +526,12 @@ extern int trace_define_field(struct ftrace_event_call *call, const char *type,
 			      int is_signed, int filter_type);
 extern int trace_add_event_call(struct ftrace_event_call *call);
 extern int trace_remove_event_call(struct ftrace_event_call *call);
+extern void ftrace_raw_event_trace_marker(void *data);
+
+struct ftrace_raw_trace_marker {
+	struct trace_entry	ent;
+};
+extern struct trace_event_functions ftrace_event_type_funcs_trace_marker;
 
 #define is_signed_type(type)	(((type)(-1)) < (type)1)
 
include/linux/tracepoint.h +3 -0
index accc497..171e3aa 100644
@@ -399,6 +399,9 @@ static inline void tracepoint_synchronize_unregister(void)
 	DECLARE_TRACE_CONDITION(name, PARAMS(proto),		\
 				PARAMS(args), PARAMS(cond))
 
+#define TRACE_MARKER(name, print)				\
+	DECLARE_TRACE_NOARGS(name)
+
 #define TRACE_EVENT_FLAGS(event, flag)
 
 #define TRACE_EVENT_PERF_PERM(event, expr...)
include/trace/define_trace.h +5 -0
index 02e1003..773d05a 100644
@@ -60,6 +60,10 @@
 #define DECLARE_TRACE(name, proto, args)	\
 	DEFINE_TRACE(name)
 
+#undef TRACE_MARKER
+#define TRACE_MARKER(name, print)		\
+	DEFINE_TRACE(name)
+
 #undef TRACE_INCLUDE
 #undef __TRACE_INCLUDE
 
@@ -93,6 +97,7 @@
 #undef TRACE_EVENT
 #undef TRACE_EVENT_FN
 #undef TRACE_EVENT_CONDITION
+#undef TRACE_MARKER
 #undef DECLARE_EVENT_CLASS
 #undef DEFINE_EVENT
 #undef DEFINE_EVENT_FN
include/trace/events/sched.h +4 -0
index 67e1bbf..fab482f 100644
@@ -8,6 +8,10 @@
 #include <linux/tracepoint.h>
 #include <linux/binfmts.h>
 
+TRACE_MARKER(sched_mark1, "This is a test");
+
+TRACE_MARKER(sched_mark2, "This is another test");
+
 /*
  * Tracepoint for calling kthread_stop, performed to end a kthread:
  */
include/trace/ftrace.h +43 -0

73 lines not on this page · raw message

kernel/sched/core.c +2 -0
index b46131e..00bcf93 100644
@@ -2656,6 +2656,7 @@ static void __sched __schedule(void)
 	struct rq *rq;
 	int cpu;
 
+	trace_sched_mark1();
 need_resched:
 	preempt_disable();
 	cpu = smp_processor_id();
@@ -2848,6 +2849,7 @@ asmlinkage void __sched preempt_schedule_irq(void)
 int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
 			  void *key)
 {
+	trace_sched_mark2();
 	return try_to_wake_up(curr->private, mode, wake_flags);
 }
 EXPORT_SYMBOL(default_wake_function);
kernel/trace/trace_events.c +46 -1

59 lines not on this page · raw message

author maintainer
#1 lore raw
Quoted 7 lines · On Fri, 7 Feb 2014 11:23:57 -0500 Steven Rostedt <[email protected]> wrote:

The TRACE_MARKER() is basically an optimized version of TRACE_EVENT() with no arguments. It can be enabled and disabled the same way as any other event, and stays within the system it was created in.

Is this worth doing?

I'm thinking no. And unless someone can give me a good reason that we should have such a thing, I will Nack my own patch!

Johannes,

What's up with trace_iwlwifi_dev_irq()? You are tracing it but not saving any information. There's obviously a reason for the iwl_pcie_isr() to be called (it's handling something). Does it do the exact same thing every time? No variables are needed then?

What I'm trying to say is, if you go through the trouble of inserting a tracepoint into some location, might as well extract data from it. Otherwise you are wasting space in memory. If all you want to know is if the function was called or not, then simply use a kprobe or function trace that function (noinline to allow ftrace to trace it).

Unless I get some feedback to why this is a good idea...

-- Steve

#2 lore raw
Quoted 2 lines · Steven Rostedt

I'm thinking no. And unless someone can give me a good reason that we should have such a thing, I will Nack my own patch!

:)

Quoted 10 lines

What's up with trace_iwlwifi_dev_irq()? You are tracing it but not saving any information. There's obviously a reason for the iwl_pcie_isr() to be called (it's handling something). Does it do the exact same thing every time? No variables are needed then?

What I'm trying to say is, if you go through the trouble of inserting a tracepoint into some location, might as well extract data from it. Otherwise you are wasting space in memory. If all you want to know is if the function was called or not, then simply use a kprobe or function trace that function (noinline to allow ftrace to trace it).

I think right now the tracepoint is misplaced, I meant it to be in the function that's now iwl_pcie_isr() but I guess I haven't used it much.

In any case, the reasoning was that we don't yet know any useful data, we really only know that an interrupt happened. We later have this ICT (interrupt cause table or something) that we read the information from, but for tracing I want to know each field in the table even though only the OR of all of them is later relevant.

So originally we had "interrupt" and then "interrupt reason" tracepoints. Looking at the code now, that seems to have been lost, so I guess I should instead add the reason to the tracepoint and only have the ICT tracing separate when ICT is enabled (in fact, if ICT isn't enabled, I don't have the reason status at all...)

Looking at this I'd agree that having a special 'hit only' trace event isn't really needed (any more), at least not for this particular case.

johannes

#3 lore raw
In reply to Steven Rostedt
Quoted 42 lines · Steven Rostedt

A while back ago, Wolfgang (and others) have asked about the ability to add a trace event that recorder no data other than the fact that the trace event was hit.

I've been reluctant to do so, but I noticed that one already exists in the iwlwifi tracepoints (iwlwifi_dev_irq) where it does a wasteful:

        /* TP_printk("") doesn't compile */
        TP_printk("%d", 0)

The reason this is wasteful, is that there's a lot of code generated by the TRACE_EVENT() macros that end up basically being nops.

I figured I would instead create a TRACE_MARKER(name, print) that would be something like:

Added to tracepoint header:

TRACE_MARKER(tpname, "Show this message");

Then you have:

	trace_tpname();

in the code.

Notice that the tracepoint function (trace_<name>()) has no arguments. That's because the message is stored in the tracepoint (in one place) and is printed when the tracepoint is read. That is, the message isn't even recorded in the ring buffer.

It still shows up in the tracepoint format file:

name: tpname
ID: 281
format:

	field:unsigned short common_type;	offset:0;	size:2;	signed:0;
	field:unsigned char common_flags;	offset:2;	size:1;	signed:0;
	field:unsigned char common_preempt_count;	offset:3;	size:1;	signed:0;
	field:int common_pid;	offset:4;	size:4;	signed:1;
Quoted 7 lines

print fmt: "Show this message"

The TRACE_MARKER() is basically an optimized version of TRACE_EVENT() with no arguments. It can be enabled and disabled the same way as any other event, and stays within the system it was created in.

Is this worth doing?

It sounds worth yeah. I've run into this situation multiple times where I had to pass 0 instead of nothing on a tracepoint.

Now about the name, why not TRACE_EVENT_EMPTY?

Thanks.

author maintainer
#4 lore raw
Quoted 3 lines · On Thu, 13 Feb 2014 15:26:42 +0100 Frederic Weisbecker <[email protected]>…
Quoted 1 line

Is this worth doing?

It sounds worth yeah. I've run into this situation multiple times where I had

Is it?

Quoted 1 line

to pass 0 instead of nothing on a tracepoint.

What tracepoints?

The problem I have with a tracepoint that does not pass any information what's so ever, is that it can be too tempting to use. Tracepoints do not have zero overhead when not used, but a negligible one. Too many tracepoints add up, and their foot print can start to be noticed.

Point being, why have a tracepoint if you are not recording any data? Just do a function trace, or add a kprobe. That's easy enough.

But that said, see below.

Quoted 2 lines

Now about the name, why not TRACE_EVENT_EMPTY?

Because that's an ugly name ;-)

Also a bit misleading, because it sounds like the there's no items attached or something. It is too close to "list_empty()". My original name was TRACE_EVENT_NOARGS(), which could work too.

Now another possible solution is to just introduce a trace_marker() function that you could place anywhere. And then they could show up in trace/events/markers/file-func-line/

We could do something like:

struct trace_marker {
	char *file;
	char *func;
	int line;
	struct static_key key;
};

#define trace_marker() __trace_marker(__FILE__,__func__, __LINE__) static inline void __trace_marker(const char *file,
const char *func, int line)
{
static struct trace_marker marker
__attribute__((section("__trace_marker"))) = { .file = file,
.func = func,
.line = line
};

if (static_key_false(&marker.key))
trace_marker_call(&marker);
}

As marker would be a static value, gcc should hard code the first parameter to it and make the call. Basically something like:

	mov $0x<marker-address>, %rdi
	call trace_marker_call

If we really want to be efficient, we could extend jump labels for each arch, and remove the call completely.

	asm goto ("1:"
		  ".byte NOP\n"
		  ".pushsection __trace_marker_struct\n"
		  "2:.quad 1b\n"
		  ".quad %file\n"
		  ".quad %func\n'
		  ".word %line\n"
		  ".popsection\n"
		  ".pushsection __trace_marker_ptrs\n"
		  ".quad 2b\n"
		  ".popsection\n"
		 : : file, func, line);

[ OK, I didn't follow the true format for asm syntax, but that's because I'm just trying to relay an idea, not actually make working code ]

Then the only thing that would be inserted in the code is a nop. We could then replace that nop with a call to a trampoline (similar to mcount) that can call a C function with the address that it came from so that the function could do a look up to find the matching marker to trace.

OK, this is probably a bit too much, but it is feasible. Most likely not worth the pain.

-- Steve

#5 lore raw
Quoted 5 lines · Steven Rostedt
Quoted 2 lines · On Thu, 13 Feb 2014 15:26:42 +0100 Frederic Weisbecker <[email protected]>…
Quoted 1 line

Is this worth doing?

Quoted 7 lines
Quoted 1 line

It sounds worth yeah. I've run into this situation multiple times where I had

Is it?

Quoted 1 line

to pass 0 instead of nothing on a tracepoint.

What tracepoints?

The context tracking ones. Yeah TBH that sole user doesn't justify the feature :)

Quoted 5 lines

The problem I have with a tracepoint that does not pass any information what's so ever, is that it can be too tempting to use. Tracepoints do not have zero overhead when not used, but a negligible one. Too many tracepoints add up, and their foot print can start to be noticed.

Yeah, that maybe overused. I don't know, actually the problem hasn't arised much.

Quoted 2 lines

Point being, why have a tracepoint if you are not recording any data? Just do a function trace, or add a kprobe. That's easy enough.

Yeah but it's still easier to use static tracepoints. Especially when you need to ask somebody to report some traces.

One of the point of static tracepoints is also to propose some key events to the user. It matters when we do all-events wide tracing for example.

Quoted 66 lines

But that said, see below.

Quoted 2 lines

Now about the name, why not TRACE_EVENT_EMPTY?

Because that's an ugly name ;-)

Also a bit misleading, because it sounds like the there's no items attached or something. It is too close to "list_empty()". My original name was TRACE_EVENT_NOARGS(), which could work too.

Now another possible solution is to just introduce a trace_marker() function that you could place anywhere. And then they could show up in trace/events/markers/file-func-line/

We could do something like:

struct trace_marker {
	char *file;
	char *func;
	int line;
	struct static_key key;
};

#define trace_marker() __trace_marker(__FILE__,__func__, __LINE__) static inline void __trace_marker(const char *file,
const char *func, int line)
{
static struct trace_marker marker
__attribute__((section("__trace_marker"))) = { .file = file,
.func = func,
.line = line
};

if (static_key_false(&marker.key))
trace_marker_call(&marker);
}

As marker would be a static value, gcc should hard code the first parameter to it and make the call. Basically something like:

	mov $0x<marker-address>, %rdi
	call trace_marker_call

If we really want to be efficient, we could extend jump labels for each arch, and remove the call completely.

	asm goto ("1:"
		  ".byte NOP\n"
		  ".pushsection __trace_marker_struct\n"
		  "2:.quad 1b\n"
		  ".quad %file\n"
		  ".quad %func\n'
		  ".word %line\n"
		  ".popsection\n"
		  ".pushsection __trace_marker_ptrs\n"
		  ".quad 2b\n"
		  ".popsection\n"
		 : : file, func, line);

[ OK, I didn't follow the true format for asm syntax, but that's because I'm just trying to relay an idea, not actually make working code ]

Quoted 8 lines

Then the only thing that would be inserted in the code is a nop. We could then replace that nop with a call to a trampoline (similar to mcount) that can call a C function with the address that it came from so that the function could do a look up to find the matching marker to trace.

OK, this is probably a bit too much, but it is feasible. Most likely not worth the pain.

Anyway yeah, given the complication and the fact there are very few actual users, lets forget that feature.

Thanks.