Tracepoints and markers: cleanup init, add missing mutex lock/unlock
[lttng-ust.git] / libust / channels.c
CommitLineData
99054cee
PMF
1/*
2 * ltt/ltt-channels.c
3 *
4 * (C) Copyright 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
5 *
6 * LTTng channel management.
7 *
8 * Author:
9 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
8fc2d8db
PMF
10 *
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.
15 *
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.
20 *
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
99054cee
PMF
24 */
25
909bc43f 26#include <stdlib.h>
909bc43f 27#include <ust/marker.h>
535b0d0a 28#include <ust/marker-internal.h>
b6bf28ec 29#include "channels.h"
30ffe279 30#include "usterr_signal_safe.h"
99054cee
PMF
31
32/*
33 * ltt_channel_mutex may be nested inside the LTT trace mutex.
34 * ltt_channel_mutex mutex may be nested inside markers mutex.
35 */
36static DEFINE_MUTEX(ltt_channel_mutex);
0222e121 37static CDS_LIST_HEAD(ltt_channels);
99054cee
PMF
38/*
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
205f7ca7 42 * get reassigned when the index_urcu_ref goes back to 0, which indicates that no
99054cee
PMF
43 * more trace channels are allocated.
44 */
45static unsigned int free_index;
205f7ca7 46static struct urcu_ref index_urcu_ref; /* Keeps track of allocated trace channels */
99054cee 47
8649cd59
PMF
48int ust_channels_overwrite_by_default = 0;
49int ust_channels_request_collection_by_default = 1;
50
99054cee
PMF
51static struct ltt_channel_setting *lookup_channel(const char *name)
52{
53 struct ltt_channel_setting *iter;
54
0222e121 55 cds_list_for_each_entry(iter, &ltt_channels, list)
99054cee
PMF
56 if (strcmp(name, iter->name) == 0)
57 return iter;
58 return NULL;
59}
60
61/*
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.
65 *
66 * Called with lock_markers() and channels mutex held.
67 */
205f7ca7 68static void release_channel_setting(struct urcu_ref *urcu_ref)
99054cee 69{
205f7ca7
DG
70 struct ltt_channel_setting *setting = _ust_container_of(urcu_ref,
71 struct ltt_channel_setting, urcu_ref);
99054cee
PMF
72 struct ltt_channel_setting *iter;
73
205f7ca7
DG
74 if (uatomic_read(&index_urcu_ref.refcount) == 0
75 && uatomic_read(&setting->urcu_ref.refcount) == 0) {
0222e121 76 cds_list_del(&setting->list);
909bc43f 77 free(setting);
99054cee
PMF
78
79 free_index = 0;
0222e121 80 cds_list_for_each_entry(iter, &ltt_channels, list) {
99054cee
PMF
81 iter->index = free_index++;
82 iter->free_event_id = 0;
83 }
909bc43f 84 /* FIXME: why not run this? */
b6bf28ec 85//ust// markers_compact_event_ids();
99054cee
PMF
86 }
87}
88
89/*
90 * Perform channel index compaction when the last trace channel is freed.
91 *
92 * Called with lock_markers() and channels mutex held.
93 */
205f7ca7 94static void release_trace_channel(struct urcu_ref *urcu_ref)
99054cee
PMF
95{
96 struct ltt_channel_setting *iter, *n;
97
0222e121 98 cds_list_for_each_entry_safe(iter, n, &ltt_channels, list)
205f7ca7 99 release_channel_setting(&iter->urcu_ref);
99054cee
PMF
100}
101
102/**
103 * ltt_channels_register - Register a trace channel.
104 * @name: channel name
105 *
106 * Uses refcounting.
107 */
108int ltt_channels_register(const char *name)
109{
110 struct ltt_channel_setting *setting;
111 int ret = 0;
112
f7b16408 113 pthread_mutex_lock(&ltt_channel_mutex);
99054cee
PMF
114 setting = lookup_channel(name);
115 if (setting) {
205f7ca7
DG
116 if (uatomic_read(&setting->urcu_ref.refcount) == 0)
117 goto init_urcu_ref;
99054cee 118 else {
205f7ca7 119 urcu_ref_get(&setting->urcu_ref);
99054cee
PMF
120 goto end;
121 }
122 }
909bc43f 123 setting = zmalloc(sizeof(*setting));
99054cee
PMF
124 if (!setting) {
125 ret = -ENOMEM;
126 goto end;
127 }
0222e121 128 cds_list_add(&setting->list, &ltt_channels);
99054cee
PMF
129 strncpy(setting->name, name, PATH_MAX-1);
130 setting->index = free_index++;
205f7ca7
DG
131init_urcu_ref:
132 urcu_ref_init(&setting->urcu_ref);
99054cee 133end:
f7b16408 134 pthread_mutex_unlock(&ltt_channel_mutex);
99054cee
PMF
135 return ret;
136}
b6bf28ec 137//ust// EXPORT_SYMBOL_GPL(ltt_channels_register);
99054cee
PMF
138
139/**
140 * ltt_channels_unregister - Unregister a trace channel.
141 * @name: channel name
142 *
143 * Must be called with markers mutex held.
144 */
145int ltt_channels_unregister(const char *name)
146{
147 struct ltt_channel_setting *setting;
148 int ret = 0;
149
f7b16408 150 pthread_mutex_lock(&ltt_channel_mutex);
99054cee 151 setting = lookup_channel(name);
205f7ca7 152 if (!setting || uatomic_read(&setting->urcu_ref.refcount) == 0) {
99054cee
PMF
153 ret = -ENOENT;
154 goto end;
155 }
205f7ca7 156 urcu_ref_put(&setting->urcu_ref, release_channel_setting);
99054cee 157end:
f7b16408 158 pthread_mutex_unlock(&ltt_channel_mutex);
99054cee
PMF
159 return ret;
160}
b6bf28ec 161//ust// EXPORT_SYMBOL_GPL(ltt_channels_unregister);
99054cee
PMF
162
163/**
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
168 */
169int ltt_channels_set_default(const char *name,
170 unsigned int subbuf_size,
171 unsigned int subbuf_cnt)
172{
173 struct ltt_channel_setting *setting;
174 int ret = 0;
175
f7b16408 176 pthread_mutex_lock(&ltt_channel_mutex);
99054cee 177 setting = lookup_channel(name);
205f7ca7 178 if (!setting || uatomic_read(&setting->urcu_ref.refcount) == 0) {
99054cee
PMF
179 ret = -ENOENT;
180 goto end;
181 }
182 setting->subbuf_size = subbuf_size;
183 setting->subbuf_cnt = subbuf_cnt;
184end:
f7b16408 185 pthread_mutex_unlock(&ltt_channel_mutex);
99054cee
PMF
186 return ret;
187}
b6bf28ec 188//ust// EXPORT_SYMBOL_GPL(ltt_channels_set_default);
99054cee
PMF
189
190/**
191 * ltt_channels_get_name_from_index - get channel name from channel index
192 * @index: channel index
193 *
194 * Allows to lookup the channel name given its index. Done to keep the name
195 * information outside of each trace channel instance.
196 */
197const char *ltt_channels_get_name_from_index(unsigned int index)
198{
199 struct ltt_channel_setting *iter;
200
0222e121 201 cds_list_for_each_entry(iter, &ltt_channels, list)
205f7ca7 202 if (iter->index == index && uatomic_read(&iter->urcu_ref.refcount))
99054cee
PMF
203 return iter->name;
204 return NULL;
205}
b6bf28ec 206//ust// EXPORT_SYMBOL_GPL(ltt_channels_get_name_from_index);
99054cee
PMF
207
208static struct ltt_channel_setting *
209ltt_channels_get_setting_from_name(const char *name)
210{
211 struct ltt_channel_setting *iter;
212
0222e121 213 cds_list_for_each_entry(iter, &ltt_channels, list)
99054cee 214 if (!strcmp(iter->name, name)
205f7ca7 215 && uatomic_read(&iter->urcu_ref.refcount))
99054cee
PMF
216 return iter;
217 return NULL;
218}
219
220/**
221 * ltt_channels_get_index_from_name - get channel index from channel name
222 * @name: channel name
223 *
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.
227 */
228int ltt_channels_get_index_from_name(const char *name)
229{
230 struct ltt_channel_setting *setting;
231
232 setting = ltt_channels_get_setting_from_name(name);
233 if (setting)
234 return setting->index;
235 else
236 return -1;
237}
b6bf28ec 238//ust// EXPORT_SYMBOL_GPL(ltt_channels_get_index_from_name);
99054cee
PMF
239
240/**
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
245 *
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.
249 */
b5b073e2 250struct ust_channel *ltt_channels_trace_alloc(unsigned int *nr_channels,
99054cee 251 int overwrite,
8649cd59 252 int request_collection,
99054cee
PMF
253 int active)
254{
b5b073e2 255 struct ust_channel *channel = NULL;
99054cee
PMF
256 struct ltt_channel_setting *iter;
257
f7b16408 258 pthread_mutex_lock(&ltt_channel_mutex);
5f54827b
PMF
259 if (!free_index) {
260 WARN("ltt_channels_trace_alloc: no free_index; are there any probes connected?");
99054cee 261 goto end;
5f54827b 262 }
205f7ca7
DG
263 if (!uatomic_read(&index_urcu_ref.refcount))
264 urcu_ref_init(&index_urcu_ref);
99054cee 265 else
205f7ca7 266 urcu_ref_get(&index_urcu_ref);
99054cee 267 *nr_channels = free_index;
909bc43f 268 channel = zmalloc(sizeof(struct ust_channel) * free_index);
5f54827b
PMF
269 if (!channel) {
270 WARN("ltt_channel_struct: channel null after alloc");
99054cee 271 goto end;
5f54827b 272 }
0222e121 273 cds_list_for_each_entry(iter, &ltt_channels, list) {
205f7ca7 274 if (!uatomic_read(&iter->urcu_ref.refcount))
99054cee
PMF
275 continue;
276 channel[iter->index].subbuf_size = iter->subbuf_size;
277 channel[iter->index].subbuf_cnt = iter->subbuf_cnt;
278 channel[iter->index].overwrite = overwrite;
8649cd59 279 channel[iter->index].request_collection = request_collection;
99054cee
PMF
280 channel[iter->index].active = active;
281 channel[iter->index].channel_name = iter->name;
282 }
283end:
f7b16408 284 pthread_mutex_unlock(&ltt_channel_mutex);
99054cee
PMF
285 return channel;
286}
b6bf28ec 287//ust// EXPORT_SYMBOL_GPL(ltt_channels_trace_alloc);
99054cee
PMF
288
289/**
290 * ltt_channels_trace_free - Free one trace's channels
291 * @channels: channels to free
292 *
293 * Called with trace lock held. The actual channel buffers must be freed before
294 * this function is called.
295 */
b5b073e2 296void ltt_channels_trace_free(struct ust_channel *channels)
99054cee 297{
b521931e 298 lock_ust_marker();
f7b16408 299 pthread_mutex_lock(&ltt_channel_mutex);
909bc43f 300 free(channels);
205f7ca7 301 urcu_ref_put(&index_urcu_ref, release_trace_channel);
f7b16408 302 pthread_mutex_unlock(&ltt_channel_mutex);
b521931e 303 unlock_ust_marker();
99054cee 304}
b6bf28ec 305//ust// EXPORT_SYMBOL_GPL(ltt_channels_trace_free);
99054cee
PMF
306
307/**
308 * _ltt_channels_get_event_id - get next event ID for a marker
309 * @channel: channel name
310 * @name: event name
311 *
312 * Returns a unique event ID (for this channel) or < 0 on error.
313 * Must be called with channels mutex held.
314 */
315int _ltt_channels_get_event_id(const char *channel, const char *name)
316{
317 struct ltt_channel_setting *setting;
318 int ret;
319
320 setting = ltt_channels_get_setting_from_name(channel);
321 if (!setting) {
322 ret = -ENOENT;
323 goto end;
324 }
325 if (strcmp(channel, "metadata") == 0) {
326 if (strcmp(name, "core_marker_id") == 0)
327 ret = 0;
328 else if (strcmp(name, "core_marker_format") == 0)
329 ret = 1;
9c67dc50
PMF
330 else if (strcmp(name, "testev") == 0)
331 ret = 2;
99054cee
PMF
332 else
333 ret = -ENOENT;
334 goto end;
335 }
336 if (setting->free_event_id == EVENTS_PER_CHANNEL - 1) {
337 ret = -ENOSPC;
338 goto end;
339 }
340 ret = setting->free_event_id++;
341end:
342 return ret;
343}
344
345/**
346 * ltt_channels_get_event_id - get next event ID for a marker
347 * @channel: channel name
348 * @name: event name
349 *
350 * Returns a unique event ID (for this channel) or < 0 on error.
351 */
352int ltt_channels_get_event_id(const char *channel, const char *name)
353{
354 int ret;
355
f7b16408 356 pthread_mutex_lock(&ltt_channel_mutex);
99054cee 357 ret = _ltt_channels_get_event_id(channel, name);
f7b16408 358 pthread_mutex_unlock(&ltt_channel_mutex);
99054cee
PMF
359 return ret;
360}
361
b6bf28ec
PMF
362//ust// MODULE_LICENSE("GPL");
363//ust// MODULE_AUTHOR("Mathieu Desnoyers");
364//ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Channel Management");
This page took 0.051382 seconds and 4 git commands to generate.