Fix: deadlock between UST registry lock and consumer lock
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.h
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #ifndef LTTNG_UST_REGISTRY_H
19 #define LTTNG_UST_REGISTRY_H
20
21 #include <pthread.h>
22 #include <stdint.h>
23
24 #include <common/hashtable/hashtable.h>
25 #include <common/compat/uuid.h>
26
27 #include "ust-ctl.h"
28
29 #define CTF_SPEC_MAJOR 1
30 #define CTF_SPEC_MINOR 8
31
32 struct ust_app;
33
34 struct ust_registry_session {
35 /*
36 * With multiple writers and readers, use this lock to access
37 * the registry. Can nest within the ust app session lock.
38 * Also acts as a registry serialization lock. Used by registry
39 * readers to serialize the registry information sent from the
40 * sessiond to the consumerd.
41 * The consumer socket lock nests within this lock.
42 */
43 pthread_mutex_t lock;
44 /* Next channel ID available for a newly registered channel. */
45 uint32_t next_channel_id;
46 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
47 uint32_t used_channel_id;
48 /* Universal unique identifier used by the tracer. */
49 unsigned char uuid[UUID_LEN];
50
51 /* session ABI description */
52
53 /* Size of long, in bits */
54 unsigned int bits_per_long;
55 /* Alignment, in bits */
56 unsigned int uint8_t_alignment,
57 uint16_t_alignment,
58 uint32_t_alignment,
59 uint64_t_alignment,
60 long_alignment;
61 /* endianness */
62 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
63
64 /* Generated metadata. */
65 char *metadata; /* NOT null-terminated ! Use memcpy. */
66 size_t metadata_len, metadata_alloc_len;
67 /* Length of bytes sent to the consumer. */
68 size_t metadata_len_sent;
69 /*
70 * Hash table containing channels sent by the UST tracer. MUST
71 * be accessed with a RCU read side lock acquired.
72 */
73 struct lttng_ht *channels;
74 /*
75 * Unique key to identify the metadata on the consumer side.
76 */
77 uint64_t metadata_key;
78 /*
79 * Indicates if the metadata is closed on the consumer side. This is to
80 * avoid double close of metadata when an application unregisters AND
81 * deletes its sessions.
82 */
83 unsigned int metadata_closed;
84 };
85
86 struct ust_registry_channel {
87 uint64_t key;
88 /* Id set when replying to a register channel. */
89 uint32_t chan_id;
90 enum ustctl_channel_header header_type;
91
92 /*
93 * Flag for this channel if the metadata was dumped once during
94 * registration. 0 means no, 1 yes.
95 */
96 unsigned int metadata_dumped;
97 /* Indicates if this channel registry has already been registered. */
98 unsigned int register_done;
99
100 /*
101 * Hash table containing events sent by the UST tracer. MUST be accessed
102 * with a RCU read side lock acquired.
103 */
104 struct lttng_ht *ht;
105 /* Next event ID available for a newly registered event. */
106 uint32_t next_event_id;
107 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
108 uint32_t used_event_id;
109 /*
110 * Context fields of the registry. Context are per channel. Allocated by a
111 * register channel notification from the UST tracer.
112 */
113 size_t nr_ctx_fields;
114 struct ustctl_field *ctx_fields;
115 struct lttng_ht_node_u64 node;
116 /* For delayed reclaim */
117 struct rcu_head rcu_head;
118 };
119
120 /*
121 * Event registered from a UST tracer sent to the session daemon. This is
122 * indexed and matched by <event_name/signature>.
123 */
124 struct ust_registry_event {
125 int id;
126 /* Both objd are set by the tracer. */
127 int session_objd;
128 int channel_objd;
129 /* Name of the event returned by the tracer. */
130 char name[LTTNG_UST_SYM_NAME_LEN];
131 char *signature;
132 int loglevel;
133 size_t nr_fields;
134 struct ustctl_field *fields;
135 char *model_emf_uri;
136 /*
137 * Flag for this channel if the metadata was dumped once during
138 * registration. 0 means no, 1 yes.
139 */
140 unsigned int metadata_dumped;
141 /*
142 * Node in the ust-registry hash table. The event name is used to
143 * initialize the node and the event_name/signature for the match function.
144 */
145 struct lttng_ht_node_u64 node;
146 };
147
148 /*
149 * Validate that the id has reached the maximum allowed or not.
150 *
151 * Return 0 if NOT else 1.
152 */
153 static inline int ust_registry_is_max_id(uint32_t id)
154 {
155 return (id == UINT32_MAX) ? 1 : 0;
156 }
157
158 /*
159 * Return next available event id and increment the used counter. The
160 * ust_registry_is_max_id function MUST be called before in order to validate
161 * if the maximum number of IDs have been reached. If not, it is safe to call
162 * this function.
163 *
164 * Return a unique channel ID. If max is reached, the used_event_id counter is
165 * returned.
166 */
167 static inline uint32_t ust_registry_get_next_event_id(
168 struct ust_registry_channel *r)
169 {
170 if (ust_registry_is_max_id(r->used_event_id)) {
171 return r->used_event_id;
172 }
173
174 r->used_event_id++;
175 return r->next_event_id++;
176 }
177
178 /*
179 * Return next available channel id and increment the used counter. The
180 * ust_registry_is_max_id function MUST be called before in order to validate
181 * if the maximum number of IDs have been reached. If not, it is safe to call
182 * this function.
183 *
184 * Return a unique channel ID. If max is reached, the used_channel_id counter
185 * is returned.
186 */
187 static inline uint32_t ust_registry_get_next_chan_id(
188 struct ust_registry_session *r)
189 {
190 if (ust_registry_is_max_id(r->used_channel_id)) {
191 return r->used_channel_id;
192 }
193
194 r->used_channel_id++;
195 return r->next_channel_id++;
196 }
197
198 /*
199 * Return registry event count. This is read atomically.
200 */
201 static inline uint32_t ust_registry_get_event_count(
202 struct ust_registry_channel *r)
203 {
204 return (uint32_t) uatomic_read(&r->used_event_id);
205 }
206
207 #ifdef HAVE_LIBLTTNG_UST_CTL
208
209 void ust_registry_channel_destroy(struct ust_registry_session *session,
210 struct ust_registry_channel *chan);
211 struct ust_registry_channel *ust_registry_channel_find(
212 struct ust_registry_session *session, uint64_t key);
213 int ust_registry_channel_add(struct ust_registry_session *session,
214 uint64_t key);
215 void ust_registry_channel_del_free(struct ust_registry_session *session,
216 uint64_t key);
217
218 int ust_registry_session_init(struct ust_registry_session **sessionp,
219 struct ust_app *app,
220 uint32_t bits_per_long,
221 uint32_t uint8_t_alignment,
222 uint32_t uint16_t_alignment,
223 uint32_t uint32_t_alignment,
224 uint32_t uint64_t_alignment,
225 uint32_t long_alignment,
226 int byte_order,
227 uint32_t major,
228 uint32_t minor);
229 void ust_registry_session_destroy(struct ust_registry_session *session);
230
231 int ust_registry_create_event(struct ust_registry_session *session,
232 uint64_t chan_key, int session_objd, int channel_objd, char *name,
233 char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel,
234 char *model_emf_uri, int buffer_type, uint32_t *event_id_p,
235 struct ust_app *app);
236 struct ust_registry_event *ust_registry_find_event(
237 struct ust_registry_channel *chan, char *name, char *sig);
238 void ust_registry_destroy_event(struct ust_registry_channel *chan,
239 struct ust_registry_event *event);
240
241 /* app can be NULL for registry shared across applications. */
242 int ust_metadata_session_statedump(struct ust_registry_session *session,
243 struct ust_app *app, uint32_t major, uint32_t minor);
244 int ust_metadata_channel_statedump(struct ust_registry_session *session,
245 struct ust_registry_channel *chan);
246 int ust_metadata_event_statedump(struct ust_registry_session *session,
247 struct ust_registry_channel *chan,
248 struct ust_registry_event *event);
249
250 #else /* HAVE_LIBLTTNG_UST_CTL */
251
252 static inline
253 void ust_registry_channel_destroy(struct ust_registry_session *session,
254 struct ust_registry_channel *chan)
255 {}
256 static inline
257 struct ust_registry_channel *ust_registry_channel_find(
258 struct ust_registry_session *session, uint64_t key)
259 {
260 return NULL;
261 }
262 static inline
263 int ust_registry_channel_add(struct ust_registry_session *session,
264 uint64_t key)
265 {
266 return 0;
267 }
268 static inline
269 void ust_registry_channel_del_free(struct ust_registry_session *session,
270 uint64_t key)
271 {}
272 static inline
273 int ust_registry_session_init(struct ust_registry_session **sessionp,
274 struct ust_app *app,
275 uint32_t bits_per_long,
276 uint32_t uint8_t_alignment,
277 uint32_t uint16_t_alignment,
278 uint32_t uint32_t_alignment,
279 uint32_t uint64_t_alignment,
280 uint32_t long_alignment,
281 int byte_order)
282 {
283 return 0;
284 }
285 static inline
286 void ust_registry_session_destroy(struct ust_registry_session *session)
287 {}
288 static inline
289 int ust_registry_create_event(struct ust_registry_session *session,
290 uint64_t chan_key, int session_objd, int channel_objd, char *name,
291 char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel,
292 char *model_emf_uri, int buffer_type, uint32_t *event_id_p)
293 {
294 return 0;
295 }
296 static inline
297 struct ust_registry_event *ust_registry_find_event(
298 struct ust_registry_channel *chan, char *name, char *sig)
299 {
300 return NULL;
301 }
302 static inline
303 void ust_registry_destroy_event(struct ust_registry_channel *chan,
304 struct ust_registry_event *event)
305 {}
306
307 /* The app object can be NULL for registry shared across applications. */
308 static inline
309 int ust_metadata_session_statedump(struct ust_registry_session *session,
310 struct ust_app *app)
311 {
312 return 0;
313 }
314 static inline
315 int ust_metadata_channel_statedump(struct ust_registry_session *session,
316 struct ust_registry_channel *chan)
317 {
318 return 0;
319 }
320 static inline
321 int ust_metadata_event_statedump(struct ust_registry_session *session,
322 struct ust_registry_channel *chan,
323 struct ust_registry_event *event)
324 {
325 return 0;
326 }
327
328 #endif /* HAVE_LIBLTTNG_UST_CTL */
329
330 #endif /* LTTNG_UST_REGISTRY_H */
This page took 0.036228 seconds and 5 git commands to generate.