Skip to content

Commit 287050d

Browse files
Steven Rostedtrostedt
authored andcommitted
tracing: Add TRACE_EVENT_CONDITIONAL()
There are instances in the kernel that we only want to trace a tracepoint when a certain condition is set. But we do not want to test for that condition in the core kernel. If we test for that condition before calling the tracepoin, then we will be performing that test even when tracing is not enabled. This is 99.99% of the time. We currently can just filter out on that condition, but that happens after we write to the trace buffer. We just wasted time writing to the ring buffer for an event we never cared about. This patch adds: TRACE_EVENT_CONDITION() and DEFINE_EVENT_CONDITION() These have a new TP_CONDITION() argument that comes right after the TP_ARGS(). This condition can use the parameters of TP_ARGS() in the TRACE_EVENT() to determine if the tracepoint should be traced or not. The TP_CONDITION() will be placed in a if (cond) trace; For example, for the tracepoint sched_wakeup, it is useless to trace a wakeup event where the caller never actually wakes anything up (where success == 0). So adding: TP_CONDITION(success), which uses the "success" parameter of the wakeup tracepoint will have it only trace when we have successfully woken up a task. Acked-by: Mathieu Desnoyers <[email protected]> Acked-by: Frederic Weisbecker <[email protected]> Cc: Arjan van de Ven <[email protected]> Cc: Thomas Gleixner <[email protected]> Signed-off-by: Steven Rostedt <[email protected]>
1 parent 0429578 commit 287050d

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

include/linux/tracepoint.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ static inline void tracepoint_update_probe_range(struct tracepoint *begin,
106106

107107
#define TP_PROTO(args...) args
108108
#define TP_ARGS(args...) args
109+
#define TP_CONDITION(args...) args
109110

110111
#ifdef CONFIG_TRACEPOINTS
111112

@@ -119,12 +120,14 @@ static inline void tracepoint_update_probe_range(struct tracepoint *begin,
119120
* as "(void *, void)". The DECLARE_TRACE_NOARGS() will pass in just
120121
* "void *data", where as the DECLARE_TRACE() will pass in "void *data, proto".
121122
*/
122-
#define __DO_TRACE(tp, proto, args) \
123+
#define __DO_TRACE(tp, proto, args, cond) \
123124
do { \
124125
struct tracepoint_func *it_func_ptr; \
125126
void *it_func; \
126127
void *__data; \
127128
\
129+
if (!(cond)) \
130+
return; \
128131
rcu_read_lock_sched_notrace(); \
129132
it_func_ptr = rcu_dereference_sched((tp)->funcs); \
130133
if (it_func_ptr) { \
@@ -142,7 +145,7 @@ static inline void tracepoint_update_probe_range(struct tracepoint *begin,
142145
* not add unwanted padding between the beginning of the section and the
143146
* structure. Force alignment to the same alignment as the section start.
144147
*/
145-
#define __DECLARE_TRACE(name, proto, args, data_proto, data_args) \
148+
#define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
146149
extern struct tracepoint __tracepoint_##name; \
147150
static inline void trace_##name(proto) \
148151
{ \
@@ -151,7 +154,8 @@ static inline void tracepoint_update_probe_range(struct tracepoint *begin,
151154
do_trace: \
152155
__DO_TRACE(&__tracepoint_##name, \
153156
TP_PROTO(data_proto), \
154-
TP_ARGS(data_args)); \
157+
TP_ARGS(data_args), \
158+
TP_CONDITION(cond)); \
155159
} \
156160
static inline int \
157161
register_trace_##name(void (*probe)(data_proto), void *data) \
@@ -186,7 +190,7 @@ do_trace: \
186190
EXPORT_SYMBOL(__tracepoint_##name)
187191

188192
#else /* !CONFIG_TRACEPOINTS */
189-
#define __DECLARE_TRACE(name, proto, args, data_proto, data_args) \
193+
#define __DECLARE_TRACE(name, proto, args, cond, data_proto, data_args) \
190194
static inline void trace_##name(proto) \
191195
{ } \
192196
static inline int \
@@ -227,13 +231,18 @@ do_trace: \
227231
* "void *__data, proto" as the callback prototype.
228232
*/
229233
#define DECLARE_TRACE_NOARGS(name) \
230-
__DECLARE_TRACE(name, void, , void *__data, __data)
234+
__DECLARE_TRACE(name, void, , 1, void *__data, __data)
231235

232236
#define DECLARE_TRACE(name, proto, args) \
233-
__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), \
237+
__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), 1, \
234238
PARAMS(void *__data, proto), \
235239
PARAMS(__data, args))
236240

241+
#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \
242+
__DECLARE_TRACE(name, PARAMS(proto), PARAMS(args), PARAMS(cond), \
243+
PARAMS(void *__data, proto), \
244+
PARAMS(__data, args))
245+
237246
#define TRACE_EVENT_FLAGS(event, flag)
238247

239248
#endif /* DECLARE_TRACE */
@@ -349,12 +358,20 @@ do_trace: \
349358
DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
350359
#define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
351360
DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
361+
#define DEFINE_EVENT_CONDITION(template, name, proto, \
362+
args, cond) \
363+
DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
364+
PARAMS(args), PARAMS(cond))
352365

353366
#define TRACE_EVENT(name, proto, args, struct, assign, print) \
354367
DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
355368
#define TRACE_EVENT_FN(name, proto, args, struct, \
356369
assign, print, reg, unreg) \
357370
DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))
371+
#define TRACE_EVENT_CONDITION(name, proto, args, cond, \
372+
struct, assign, print) \
373+
DECLARE_TRACE_CONDITION(name, PARAMS(proto), \
374+
PARAMS(args), PARAMS(cond))
358375

359376
#define TRACE_EVENT_FLAGS(event, flag)
360377

include/trace/define_trace.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
#define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
2727
DEFINE_TRACE(name)
2828

29+
#undef TRACE_EVENT_CONDITION
30+
#define TRACE_EVENT_CONDITION(name, proto, args, cond, tstruct, assign, print) \
31+
TRACE_EVENT(name, \
32+
PARAMS(proto), \
33+
PARAMS(args), \
34+
PARAMS(tstruct), \
35+
PARAMS(assign), \
36+
PARAMS(print))
37+
2938
#undef TRACE_EVENT_FN
3039
#define TRACE_EVENT_FN(name, proto, args, tstruct, \
3140
assign, print, reg, unreg) \
@@ -39,6 +48,10 @@
3948
#define DEFINE_EVENT_PRINT(template, name, proto, args, print) \
4049
DEFINE_TRACE(name)
4150

51+
#undef DEFINE_EVENT_CONDITION
52+
#define DEFINE_EVENT_CONDITION(template, name, proto, args, cond) \
53+
DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))
54+
4255
#undef DECLARE_TRACE
4356
#define DECLARE_TRACE(name, proto, args) \
4457
DEFINE_TRACE(name)
@@ -75,9 +88,11 @@
7588

7689
#undef TRACE_EVENT
7790
#undef TRACE_EVENT_FN
91+
#undef TRACE_EVENT_CONDITION
7892
#undef DECLARE_EVENT_CLASS
7993
#undef DEFINE_EVENT
8094
#undef DEFINE_EVENT_PRINT
95+
#undef DEFINE_EVENT_CONDITION
8196
#undef TRACE_HEADER_MULTI_READ
8297
#undef DECLARE_TRACE
8398

0 commit comments

Comments
 (0)