2 * trace event based perf counter profiling
4 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
8 #include <linux/module.h>
12 * We can't use a size but a type in alloc_percpu()
13 * So let's create a dummy type that matches the desired size
15 typedef struct {char buf[FTRACE_MAX_PROFILE_SIZE];} profile_buf_t;
17 char *trace_profile_buf;
18 char *trace_profile_buf_nmi;
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(profile_buf_t);
36 rcu_assign_pointer(trace_profile_buf, buf);
38 buf = (char *)alloc_percpu(profile_buf_t);
42 rcu_assign_pointer(trace_profile_buf_nmi, buf);
45 ret = event->profile_enable();
49 kfree(trace_profile_buf_nmi);
51 kfree(trace_profile_buf);
53 total_profile_count--;
54 atomic_dec(&event->profile_count);
59 int ftrace_profile_enable(int event_id)
61 struct ftrace_event_call *event;
64 mutex_lock(&event_mutex);
65 list_for_each_entry(event, &ftrace_events, list) {
66 if (event->id == event_id && event->profile_enable &&
67 try_module_get(event->mod)) {
68 ret = ftrace_profile_enable_event(event);
72 mutex_unlock(&event_mutex);
77 static void ftrace_profile_disable_event(struct ftrace_event_call *event)
81 if (!atomic_add_negative(-1, &event->profile_count))
84 event->profile_disable();
86 if (!--total_profile_count) {
87 buf = trace_profile_buf;
88 rcu_assign_pointer(trace_profile_buf, NULL);
90 nmi_buf = trace_profile_buf_nmi;
91 rcu_assign_pointer(trace_profile_buf_nmi, NULL);
94 * Ensure every events in profiling have finished before
95 * releasing the buffers
100 free_percpu(nmi_buf);
104 void ftrace_profile_disable(int event_id)
106 struct ftrace_event_call *event;
108 mutex_lock(&event_mutex);
109 list_for_each_entry(event, &ftrace_events, list) {
110 if (event->id == event_id) {
111 ftrace_profile_disable_event(event);
112 module_put(event->mod);
116 mutex_unlock(&event_mutex);