Commit | Line | Data |
---|---|---|
4e3c1b9b MD |
1 | /* |
2 | * ltt-events.c | |
3 | * | |
4 | * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Holds LTTng per-session event registry. | |
7 | */ | |
8 | ||
9 | #include <linux/module.h> | |
10 | #include "ltt-events.h" | |
11 | ||
12 | static LIST_HEAD(sessions); | |
13 | static DEFINE_MUTEX(sessions_mutex); | |
14 | static struct kmem_cache *event_cache; | |
15 | ||
abcca994 MD |
16 | static void synchronize_trace(void) |
17 | { | |
18 | synchronize_sched(); | |
19 | #ifdef CONFIG_PREEMPT_RT | |
20 | synchronize_rcu(); | |
21 | #endif | |
22 | } | |
23 | ||
baf20995 | 24 | struct ltt_session *ltt_session_create(void) |
4e3c1b9b MD |
25 | { |
26 | struct ltt_session *session; | |
27 | ||
28 | mutex_lock(&sessions_mutex); | |
baf20995 | 29 | session = kmalloc(sizeof(struct ltt_session)); |
4e3c1b9b MD |
30 | if (!session) |
31 | return NULL; | |
4e3c1b9b MD |
32 | INIT_LIST_HEAD(&session->chan); |
33 | list_add(&session->list, &sessions); | |
34 | mutex_unlock(&sessions_mutex); | |
35 | return session; | |
36 | ||
37 | exist: | |
38 | mutex_unlock(&sessions_mutex); | |
39 | return NULL; | |
40 | } | |
41 | ||
42 | int ltt_session_destroy(struct ltt_session *session) | |
43 | { | |
44 | struct ltt_channel *chan, *tmpchan; | |
45 | struct ltt_event *event, *tmpevent; | |
46 | ||
47 | mutex_lock(&sessions_mutex); | |
abcca994 MD |
48 | session->active = 0; |
49 | synchronize_trace(); /* Wait for in-flight events to complete */ | |
4e3c1b9b MD |
50 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
51 | _ltt_event_destroy(event); | |
52 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) | |
53 | _ltt_channel_destroy(chan); | |
54 | list_del(&session->list); | |
55 | mutex_unlock(&sessions_mutex); | |
56 | kfree(session); | |
57 | } | |
58 | ||
baf20995 | 59 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
4e3c1b9b MD |
60 | int overwrite, void *buf_addr, |
61 | size_t subbuf_size, size_t num_subbuf, | |
62 | unsigned int switch_timer_interval, | |
63 | unsigned int read_timer_interval) | |
64 | { | |
65 | struct ltt_channel *chan; | |
66 | ||
67 | mutex_lock(&sessions_mutex); | |
e5382b6d MD |
68 | if (session->active) |
69 | goto active; /* Refuse to add channel to active session */ | |
baf20995 | 70 | chan = kmalloc(sizeof(struct ltt_channel), GFP_KERNEL); |
4e3c1b9b MD |
71 | if (!chan) |
72 | return NULL; | |
4e3c1b9b MD |
73 | chan->session = session; |
74 | ||
75 | /* TODO: create rb channel */ | |
76 | list_add(&chan->list, &session->chan); | |
77 | mutex_unlock(&sessions_mutex); | |
78 | return chan; | |
79 | ||
80 | exist: | |
e5382b6d | 81 | active: |
4e3c1b9b MD |
82 | mutex_unlock(&sessions_mutex); |
83 | return NULL; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Only used internally at session destruction. | |
88 | */ | |
89 | int _ltt_channel_destroy(struct ltt_channel *chan) | |
90 | { | |
abcca994 | 91 | /* TODO: destroy rb channel */ |
4e3c1b9b MD |
92 | list_del(&chan->list); |
93 | kfree(chan); | |
94 | } | |
95 | ||
e5382b6d MD |
96 | /* |
97 | * Supports event creation while tracing session is active. | |
98 | */ | |
4e3c1b9b | 99 | struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name, |
baf20995 MD |
100 | enum instrum_type itype, |
101 | void *probe, void *filter) | |
4e3c1b9b MD |
102 | { |
103 | struct ltt_event *event; | |
3d084699 | 104 | int ret; |
4e3c1b9b MD |
105 | |
106 | mutex_lock(&sessions_mutex); | |
e5382b6d MD |
107 | if (chan->free_event_id == -1UL) |
108 | goto full; | |
4e3c1b9b MD |
109 | /* |
110 | * This is O(n^2) (for each event loop called at event creation). | |
111 | * Might require a hash if we have lots of events. | |
112 | */ | |
113 | list_for_each_entry(event, &chan->session->events, list) | |
114 | if (!strcmp(event->name, name)) | |
115 | goto exist; | |
116 | event = kmem_cache_zalloc(events_cache, GFP_KERNEL); | |
117 | if (!event) | |
118 | goto cache_error; | |
119 | event->name = kmalloc(strlen(name) + 1, GFP_KERNEL); | |
120 | if (!event->name) | |
121 | goto error; | |
122 | strcpy(event->name, name); | |
123 | event->chan = chan; | |
baf20995 | 124 | event->probe = probe; |
4e3c1b9b | 125 | event->filter = filter; |
e5382b6d | 126 | event->id = chan->free_event_id++; |
baf20995 | 127 | event->itype = itype; |
4e3c1b9b | 128 | mutex_unlock(&sessions_mutex); |
e5382b6d MD |
129 | /* Populate ltt_event structure before tracepoint registration. */ |
130 | smp_wmb(); | |
baf20995 MD |
131 | switch (itype) { |
132 | case INSTRUM_TRACEPOINTS: | |
133 | ret = tracepoint_probe_register(name, probe, event); | |
134 | break; | |
135 | default: | |
136 | WARN_ON_ONCE(1); | |
137 | } | |
4e3c1b9b MD |
138 | return event; |
139 | ||
140 | error: | |
141 | kmem_cache_free(event); | |
142 | cache_error: | |
143 | exist: | |
e5382b6d | 144 | full: |
4e3c1b9b MD |
145 | mutex_unlock(&sessions_mutex); |
146 | return NULL; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Only used internally at session destruction. | |
151 | */ | |
152 | int _ltt_event_destroy(struct ltt_event *event) | |
153 | { | |
baf20995 MD |
154 | switch (event->itype) { |
155 | case INSTRUM_TRACEPOINTS: | |
156 | ret = tracepoint_probe_unregister(name, event->probe, event); | |
157 | break; | |
158 | default: | |
159 | WARN_ON_ONCE(1); | |
160 | } | |
4e3c1b9b MD |
161 | kfree(event->name); |
162 | kmem_cache_free(event); | |
163 | } | |
164 | ||
165 | static int __init ltt_events_init(void) | |
166 | { | |
167 | int ret; | |
168 | ||
169 | events_cache = KMEM_CACHE(ltt_event, 0); | |
170 | if (!events_cache) | |
171 | return -ENOMEM; | |
92e94819 MD |
172 | |
173 | /* TODO: show ABI to userspace */ | |
174 | ||
4e3c1b9b MD |
175 | return 0; |
176 | } | |
177 | ||
178 | static void __exit ltt_events_exit(void) | |
179 | { | |
92e94819 MD |
180 | struct ltt_session *session, *tmpsession; |
181 | ||
182 | /* TODO: hide ABI from userspace, wait for callers to release refs. */ | |
183 | ||
184 | list_for_each_entry_safe(session, tmpsession, &sessions, list) | |
185 | ltt_session_destroy(session); | |
4e3c1b9b MD |
186 | kmem_cache_destroy(events_cache); |
187 | } | |
92e94819 MD |
188 | |
189 | MODULE_LICENSE("GPL and additional rights"); | |
190 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); | |
191 | MODULE_DESCRIPTION("LTTng Events"); |