b43: N-PHY: handle allocation fail in samples generation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_event_profile.c
blobd9c60f80aa0d20958c647a86310b29701b4bcdcc
1 /*
2 * trace event based perf counter profiling
4 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
6 */
8 #include <linux/module.h>
9 #include "trace.h"
12 char *perf_trace_buf;
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)
25 char *buf;
26 int ret = -ENOMEM;
28 if (atomic_inc_return(&event->profile_count))
29 return 0;
31 if (!total_profile_count) {
32 buf = (char *)alloc_percpu(perf_trace_t);
33 if (!buf)
34 goto fail_buf;
36 rcu_assign_pointer(perf_trace_buf, buf);
38 buf = (char *)alloc_percpu(perf_trace_t);
39 if (!buf)
40 goto fail_buf_nmi;
42 rcu_assign_pointer(perf_trace_buf_nmi, buf);
45 ret = event->profile_enable(event);
46 if (!ret) {
47 total_profile_count++;
48 return 0;
51 fail_buf_nmi:
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;
58 fail_buf:
59 atomic_dec(&event->profile_count);
61 return ret;
64 int ftrace_profile_enable(int event_id)
66 struct ftrace_event_call *event;
67 int ret = -EINVAL;
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);
74 break;
77 mutex_unlock(&event_mutex);
79 return ret;
82 static void ftrace_profile_disable_event(struct ftrace_event_call *event)
84 char *buf, *nmi_buf;
86 if (!atomic_add_negative(-1, &event->profile_count))
87 return;
89 event->profile_disable(event);
91 if (!--total_profile_count) {
92 buf = perf_trace_buf;
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
102 synchronize_sched();
104 free_percpu(buf);
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);
118 break;
121 mutex_unlock(&event_mutex);