2 * trace event based perf counter profiling
4 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
8 #include <linux/module.h>
13 EXPORT_SYMBOL_GPL(perf_trace_buf
);
15 char *perf_trace_buf_nmi
;
16 EXPORT_SYMBOL_GPL(perf_trace_buf_nmi
);
18 typedef typeof(char [FTRACE_MAX_PROFILE_SIZE
]) perf_trace_t
;
20 /* Count the events in use (per event id, not per instance) */
21 static int total_profile_count
;
23 static int ftrace_profile_enable_event(struct ftrace_event_call
*event
)
28 if (atomic_inc_return(&event
->profile_count
))
31 if (!total_profile_count
) {
32 buf
= (char *)alloc_percpu(perf_trace_t
);
36 rcu_assign_pointer(perf_trace_buf
, buf
);
38 buf
= (char *)alloc_percpu(perf_trace_t
);
42 rcu_assign_pointer(perf_trace_buf_nmi
, buf
);
45 ret
= event
->profile_enable(event
);
47 total_profile_count
++;
52 if (!total_profile_count
) {
53 free_percpu(perf_trace_buf_nmi
);
54 free_percpu(perf_trace_buf
);
55 perf_trace_buf_nmi
= NULL
;
56 perf_trace_buf
= NULL
;
59 atomic_dec(&event
->profile_count
);
64 int ftrace_profile_enable(int event_id
)
66 struct ftrace_event_call
*event
;
69 mutex_lock(&event_mutex
);
70 list_for_each_entry(event
, &ftrace_events
, list
) {
71 if (event
->id
== event_id
&& event
->profile_enable
&&
72 try_module_get(event
->mod
)) {
73 ret
= ftrace_profile_enable_event(event
);
77 mutex_unlock(&event_mutex
);
82 static void ftrace_profile_disable_event(struct ftrace_event_call
*event
)
86 if (!atomic_add_negative(-1, &event
->profile_count
))
89 event
->profile_disable(event
);
91 if (!--total_profile_count
) {
93 rcu_assign_pointer(perf_trace_buf
, NULL
);
95 nmi_buf
= perf_trace_buf_nmi
;
96 rcu_assign_pointer(perf_trace_buf_nmi
, NULL
);
99 * Ensure every events in profiling have finished before
100 * releasing the buffers
105 free_percpu(nmi_buf
);
109 void ftrace_profile_disable(int event_id
)
111 struct ftrace_event_call
*event
;
113 mutex_lock(&event_mutex
);
114 list_for_each_entry(event
, &ftrace_events
, list
) {
115 if (event
->id
== event_id
) {
116 ftrace_profile_disable_event(event
);
117 module_put(event
->mod
);
121 mutex_unlock(&event_mutex
);