tracing: Factorize the events profile accounting
[linux-2.6.git] / kernel / trace / trace_event_profile.c
1 /*
2  * trace event based perf counter profiling
3  *
4  * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
5  *
6  */
7
8 #include <linux/module.h>
9 #include "trace.h"
10
11 static int ftrace_profile_enable_event(struct ftrace_event_call *event)
12 {
13         if (atomic_inc_return(&event->profile_count))
14                 return 0;
15
16         return event->profile_enable();
17 }
18
19 int ftrace_profile_enable(int event_id)
20 {
21         struct ftrace_event_call *event;
22         int ret = -EINVAL;
23
24         mutex_lock(&event_mutex);
25         list_for_each_entry(event, &ftrace_events, list) {
26                 if (event->id == event_id && event->profile_enable &&
27                     try_module_get(event->mod)) {
28                         ret = ftrace_profile_enable_event(event);
29                         break;
30                 }
31         }
32         mutex_unlock(&event_mutex);
33
34         return ret;
35 }
36
37 static void ftrace_profile_disable_event(struct ftrace_event_call *event)
38 {
39         if (!atomic_add_negative(-1, &event->profile_count))
40                 return;
41
42         event->profile_disable();
43 }
44
45 void ftrace_profile_disable(int event_id)
46 {
47         struct ftrace_event_call *event;
48
49         mutex_lock(&event_mutex);
50         list_for_each_entry(event, &ftrace_events, list) {
51                 if (event->id == event_id) {
52                         ftrace_profile_disable_event(event);
53                         module_put(event->mod);
54                         break;
55                 }
56         }
57         mutex_unlock(&event_mutex);
58 }