4 * (C) Copyright 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
6 * LTTng channel management.
9 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <ust/kernelcompat.h>
28 #include <ust/marker.h>
33 * ltt_channel_mutex may be nested inside the LTT trace mutex.
34 * ltt_channel_mutex mutex may be nested inside markers mutex.
36 static DEFINE_MUTEX(ltt_channel_mutex
);
37 static LIST_HEAD(ltt_channels
);
39 * Index of next channel in array. Makes sure that as long as a trace channel is
40 * allocated, no array index will be re-used when a channel is freed and then
41 * another channel is allocated. This index is cleared and the array indexeds
42 * get reassigned when the index_kref goes back to 0, which indicates that no
43 * more trace channels are allocated.
45 static unsigned int free_index
;
46 static struct kref index_kref
; /* Keeps track of allocated trace channels */
48 int ust_channels_overwrite_by_default
= 0;
49 int ust_channels_request_collection_by_default
= 1;
51 static struct ltt_channel_setting
*lookup_channel(const char *name
)
53 struct ltt_channel_setting
*iter
;
55 list_for_each_entry(iter
, <t_channels
, list
)
56 if (strcmp(name
, iter
->name
) == 0)
62 * Must be called when channel refcount falls to 0 _and_ also when the last
63 * trace is freed. This function is responsible for compacting the channel and
64 * event IDs when no users are active.
66 * Called with lock_markers() and channels mutex held.
68 static void release_channel_setting(struct kref
*kref
)
70 struct ltt_channel_setting
*setting
= container_of(kref
,
71 struct ltt_channel_setting
, kref
);
72 struct ltt_channel_setting
*iter
;
74 if (uatomic_read(&index_kref
.refcount
) == 0
75 && uatomic_read(&setting
->kref
.refcount
) == 0) {
76 list_del(&setting
->list
);
80 list_for_each_entry(iter
, <t_channels
, list
) {
81 iter
->index
= free_index
++;
82 iter
->free_event_id
= 0;
84 /* FIXME: why not run this? */
85 //ust// markers_compact_event_ids();
90 * Perform channel index compaction when the last trace channel is freed.
92 * Called with lock_markers() and channels mutex held.
94 static void release_trace_channel(struct kref
*kref
)
96 struct ltt_channel_setting
*iter
, *n
;
98 list_for_each_entry_safe(iter
, n
, <t_channels
, list
)
99 release_channel_setting(&iter
->kref
);
103 * ltt_channels_register - Register a trace channel.
104 * @name: channel name
108 int ltt_channels_register(const char *name
)
110 struct ltt_channel_setting
*setting
;
113 mutex_lock(<t_channel_mutex
);
114 setting
= lookup_channel(name
);
116 if (uatomic_read(&setting
->kref
.refcount
) == 0)
119 kref_get(&setting
->kref
);
123 setting
= zmalloc(sizeof(*setting
));
128 list_add(&setting
->list
, <t_channels
);
129 strncpy(setting
->name
, name
, PATH_MAX
-1);
130 setting
->index
= free_index
++;
132 kref_init(&setting
->kref
);
134 mutex_unlock(<t_channel_mutex
);
137 //ust// EXPORT_SYMBOL_GPL(ltt_channels_register);
140 * ltt_channels_unregister - Unregister a trace channel.
141 * @name: channel name
143 * Must be called with markers mutex held.
145 int ltt_channels_unregister(const char *name
)
147 struct ltt_channel_setting
*setting
;
150 mutex_lock(<t_channel_mutex
);
151 setting
= lookup_channel(name
);
152 if (!setting
|| uatomic_read(&setting
->kref
.refcount
) == 0) {
156 kref_put(&setting
->kref
, release_channel_setting
);
158 mutex_unlock(<t_channel_mutex
);
161 //ust// EXPORT_SYMBOL_GPL(ltt_channels_unregister);
164 * ltt_channels_set_default - Set channel default behavior.
165 * @name: default channel name
166 * @subbuf_size: size of the subbuffers
167 * @subbuf_cnt: number of subbuffers
169 int ltt_channels_set_default(const char *name
,
170 unsigned int subbuf_size
,
171 unsigned int subbuf_cnt
)
173 struct ltt_channel_setting
*setting
;
176 mutex_lock(<t_channel_mutex
);
177 setting
= lookup_channel(name
);
178 if (!setting
|| uatomic_read(&setting
->kref
.refcount
) == 0) {
182 setting
->subbuf_size
= subbuf_size
;
183 setting
->subbuf_cnt
= subbuf_cnt
;
185 mutex_unlock(<t_channel_mutex
);
188 //ust// EXPORT_SYMBOL_GPL(ltt_channels_set_default);
191 * ltt_channels_get_name_from_index - get channel name from channel index
192 * @index: channel index
194 * Allows to lookup the channel name given its index. Done to keep the name
195 * information outside of each trace channel instance.
197 const char *ltt_channels_get_name_from_index(unsigned int index
)
199 struct ltt_channel_setting
*iter
;
201 list_for_each_entry(iter
, <t_channels
, list
)
202 if (iter
->index
== index
&& uatomic_read(&iter
->kref
.refcount
))
206 //ust// EXPORT_SYMBOL_GPL(ltt_channels_get_name_from_index);
208 static struct ltt_channel_setting
*
209 ltt_channels_get_setting_from_name(const char *name
)
211 struct ltt_channel_setting
*iter
;
213 list_for_each_entry(iter
, <t_channels
, list
)
214 if (!strcmp(iter
->name
, name
)
215 && uatomic_read(&iter
->kref
.refcount
))
221 * ltt_channels_get_index_from_name - get channel index from channel name
222 * @name: channel name
224 * Allows to lookup the channel index given its name. Done to keep the name
225 * information outside of each trace channel instance.
226 * Returns -1 if not found.
228 int ltt_channels_get_index_from_name(const char *name
)
230 struct ltt_channel_setting
*setting
;
232 setting
= ltt_channels_get_setting_from_name(name
);
234 return setting
->index
;
238 //ust// EXPORT_SYMBOL_GPL(ltt_channels_get_index_from_name);
241 * ltt_channels_trace_alloc - Allocate channel structures for a trace
242 * @subbuf_size: subbuffer size. 0 uses default.
243 * @subbuf_cnt: number of subbuffers per per-cpu buffers. 0 uses default.
244 * @flags: Default channel flags
246 * Use the current channel list to allocate the channels for a trace.
247 * Called with trace lock held. Does not perform the trace buffer allocation,
248 * because we must let the user overwrite specific channel sizes.
250 struct ust_channel
*ltt_channels_trace_alloc(unsigned int *nr_channels
,
252 int request_collection
,
255 struct ust_channel
*channel
= NULL
;
256 struct ltt_channel_setting
*iter
;
258 mutex_lock(<t_channel_mutex
);
260 WARN("ltt_channels_trace_alloc: no free_index; are there any probes connected?");
263 if (!uatomic_read(&index_kref
.refcount
))
264 kref_init(&index_kref
);
266 kref_get(&index_kref
);
267 *nr_channels
= free_index
;
268 channel
= zmalloc(sizeof(struct ust_channel
) * free_index
);
270 WARN("ltt_channel_struct: channel null after alloc");
273 list_for_each_entry(iter
, <t_channels
, list
) {
274 if (!uatomic_read(&iter
->kref
.refcount
))
276 channel
[iter
->index
].subbuf_size
= iter
->subbuf_size
;
277 channel
[iter
->index
].subbuf_cnt
= iter
->subbuf_cnt
;
278 channel
[iter
->index
].overwrite
= overwrite
;
279 channel
[iter
->index
].request_collection
= request_collection
;
280 channel
[iter
->index
].active
= active
;
281 channel
[iter
->index
].channel_name
= iter
->name
;
284 mutex_unlock(<t_channel_mutex
);
287 //ust// EXPORT_SYMBOL_GPL(ltt_channels_trace_alloc);
290 * ltt_channels_trace_free - Free one trace's channels
291 * @channels: channels to free
293 * Called with trace lock held. The actual channel buffers must be freed before
294 * this function is called.
296 void ltt_channels_trace_free(struct ust_channel
*channels
)
299 mutex_lock(<t_channel_mutex
);
301 kref_put(&index_kref
, release_trace_channel
);
302 mutex_unlock(<t_channel_mutex
);
305 //ust// EXPORT_SYMBOL_GPL(ltt_channels_trace_free);
308 * _ltt_channels_get_event_id - get next event ID for a marker
309 * @channel: channel name
312 * Returns a unique event ID (for this channel) or < 0 on error.
313 * Must be called with channels mutex held.
315 int _ltt_channels_get_event_id(const char *channel
, const char *name
)
317 struct ltt_channel_setting
*setting
;
320 setting
= ltt_channels_get_setting_from_name(channel
);
325 if (strcmp(channel
, "metadata") == 0) {
326 if (strcmp(name
, "core_marker_id") == 0)
328 else if (strcmp(name
, "core_marker_format") == 0)
330 else if (strcmp(name
, "testev") == 0)
336 if (setting
->free_event_id
== EVENTS_PER_CHANNEL
- 1) {
340 ret
= setting
->free_event_id
++;
346 * ltt_channels_get_event_id - get next event ID for a marker
347 * @channel: channel name
350 * Returns a unique event ID (for this channel) or < 0 on error.
352 int ltt_channels_get_event_id(const char *channel
, const char *name
)
356 mutex_lock(<t_channel_mutex
);
357 ret
= _ltt_channels_get_event_id(channel
, name
);
358 mutex_unlock(<t_channel_mutex
);
362 //ust// MODULE_LICENSE("GPL");
363 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
364 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Channel Management");