Commit | Line | Data |
---|---|---|
d0b96690 DG |
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 | #define _GNU_SOURCE | |
18 | #include <assert.h> | |
7972aab2 | 19 | #include <inttypes.h> |
d0b96690 DG |
20 | |
21 | #include <common/common.h> | |
7972aab2 DG |
22 | #include <common/hashtable/utils.h> |
23 | #include <lttng/lttng.h> | |
24 | ||
d0b96690 | 25 | #include "ust-registry.h" |
0b2dc8df | 26 | #include "utils.h" |
d0b96690 DG |
27 | |
28 | /* | |
29 | * Hash table match function for event in the registry. | |
30 | */ | |
31 | static int ht_match_event(struct cds_lfht_node *node, const void *_key) | |
32 | { | |
33 | struct ust_registry_event *event; | |
34 | const struct ust_registry_event *key; | |
35 | ||
36 | assert(node); | |
37 | assert(_key); | |
38 | ||
39 | event = caa_container_of(node, struct ust_registry_event, node.node); | |
40 | assert(event); | |
41 | key = _key; | |
42 | ||
43 | /* It has to be a perfect match. */ | |
44 | if (strncmp(event->name, key->name, sizeof(event->name)) != 0) { | |
45 | goto no_match; | |
46 | } | |
47 | ||
48 | /* It has to be a perfect match. */ | |
49 | if (strncmp(event->signature, key->signature, | |
50 | strlen(event->signature) != 0)) { | |
51 | goto no_match; | |
52 | } | |
53 | ||
54 | /* Match */ | |
55 | return 1; | |
56 | ||
57 | no_match: | |
58 | return 0; | |
59 | } | |
60 | ||
7972aab2 DG |
61 | static unsigned long ht_hash_event(void *_key, unsigned long seed) |
62 | { | |
63 | uint64_t xored_key; | |
64 | struct ust_registry_event *key = _key; | |
65 | ||
66 | assert(key); | |
67 | ||
68 | xored_key = (uint64_t) (hash_key_str(key->name, seed) ^ | |
69 | hash_key_str(key->signature, seed)); | |
70 | ||
71 | return hash_key_u64(&xored_key, seed); | |
72 | } | |
73 | ||
d0b96690 DG |
74 | /* |
75 | * Allocate event and initialize it. This does NOT set a valid event id from a | |
76 | * registry. | |
77 | */ | |
78 | static struct ust_registry_event *alloc_event(int session_objd, | |
79 | int channel_objd, char *name, char *sig, size_t nr_fields, | |
80 | struct ustctl_field *fields, int loglevel, char *model_emf_uri) | |
81 | { | |
82 | struct ust_registry_event *event = NULL; | |
83 | ||
84 | event = zmalloc(sizeof(*event)); | |
85 | if (!event) { | |
86 | PERROR("zmalloc ust registry event"); | |
87 | goto error; | |
88 | } | |
89 | ||
90 | event->session_objd = session_objd; | |
91 | event->channel_objd = channel_objd; | |
92 | /* Allocated by ustctl. */ | |
93 | event->signature = sig; | |
94 | event->nr_fields = nr_fields; | |
95 | event->fields = fields; | |
96 | event->loglevel = loglevel; | |
97 | event->model_emf_uri = model_emf_uri; | |
98 | if (name) { | |
99 | /* Copy event name and force NULL byte. */ | |
100 | strncpy(event->name, name, sizeof(event->name)); | |
101 | event->name[sizeof(event->name) - 1] = '\0'; | |
102 | } | |
7972aab2 | 103 | cds_lfht_node_init(&event->node.node); |
d0b96690 DG |
104 | |
105 | error: | |
106 | return event; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Free event data structure. This does NOT delete it from any hash table. It's | |
111 | * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the | |
112 | * event is previously deleted from a rcu hash table. | |
113 | */ | |
114 | static void destroy_event(struct ust_registry_event *event) | |
115 | { | |
116 | if (!event) { | |
117 | return; | |
118 | } | |
119 | ||
120 | free(event->fields); | |
121 | free(event->model_emf_uri); | |
122 | free(event->signature); | |
123 | free(event); | |
124 | } | |
125 | ||
126 | /* | |
127 | * Destroy event function call of the call RCU. | |
128 | */ | |
129 | static void destroy_event_rcu(struct rcu_head *head) | |
130 | { | |
7972aab2 DG |
131 | struct lttng_ht_node_u64 *node = |
132 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
d0b96690 DG |
133 | struct ust_registry_event *event = |
134 | caa_container_of(node, struct ust_registry_event, node); | |
135 | ||
136 | destroy_event(event); | |
137 | } | |
138 | ||
139 | /* | |
140 | * Find an event using the name and signature in the given registry. RCU read | |
141 | * side lock MUST be acquired before calling this function and as long as the | |
142 | * event reference is kept by the caller. | |
143 | * | |
144 | * On success, the event pointer is returned else NULL. | |
145 | */ | |
146 | struct ust_registry_event *ust_registry_find_event( | |
147 | struct ust_registry_channel *chan, char *name, char *sig) | |
148 | { | |
7972aab2 | 149 | struct lttng_ht_node_u64 *node; |
d0b96690 DG |
150 | struct lttng_ht_iter iter; |
151 | struct ust_registry_event *event = NULL; | |
152 | struct ust_registry_event key; | |
153 | ||
154 | assert(chan); | |
155 | assert(name); | |
156 | assert(sig); | |
157 | ||
158 | /* Setup key for the match function. */ | |
159 | strncpy(key.name, name, sizeof(key.name)); | |
160 | key.name[sizeof(key.name) - 1] = '\0'; | |
161 | key.signature = sig; | |
162 | ||
7972aab2 | 163 | cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(&key, lttng_ht_seed), |
d0b96690 | 164 | chan->ht->match_fct, &key, &iter.iter); |
7972aab2 | 165 | node = lttng_ht_iter_get_node_u64(&iter); |
d0b96690 DG |
166 | if (!node) { |
167 | goto end; | |
168 | } | |
169 | event = caa_container_of(node, struct ust_registry_event, node); | |
170 | ||
171 | end: | |
172 | return event; | |
173 | } | |
174 | ||
175 | /* | |
176 | * Create a ust_registry_event from the given parameters and add it to the | |
177 | * registry hash table. If event_id is valid, it is set with the newly created | |
178 | * event id. | |
179 | * | |
180 | * On success, return 0 else a negative value. The created event MUST be unique | |
181 | * so on duplicate entry -EINVAL is returned. On error, event_id is untouched. | |
182 | * | |
183 | * Should be called with session registry mutex held. | |
184 | */ | |
185 | int ust_registry_create_event(struct ust_registry_session *session, | |
45893984 DG |
186 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
187 | char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel, | |
7972aab2 | 188 | char *model_emf_uri, int buffer_type, uint32_t *event_id_p) |
d0b96690 DG |
189 | { |
190 | int ret; | |
7972aab2 | 191 | uint32_t event_id; |
d0b96690 DG |
192 | struct cds_lfht_node *nptr; |
193 | struct ust_registry_event *event = NULL; | |
45893984 | 194 | struct ust_registry_channel *chan; |
d0b96690 DG |
195 | |
196 | assert(session); | |
d0b96690 DG |
197 | assert(name); |
198 | assert(sig); | |
7972aab2 | 199 | assert(event_id_p); |
d0b96690 DG |
200 | |
201 | /* | |
202 | * This should not happen but since it comes from the UST tracer, an | |
203 | * external party, don't assert and simply validate values. | |
204 | */ | |
205 | if (session_objd < 0 || channel_objd < 0) { | |
206 | ret = -EINVAL; | |
207 | goto error; | |
208 | } | |
209 | ||
45893984 DG |
210 | rcu_read_lock(); |
211 | ||
212 | chan = ust_registry_channel_find(session, chan_key); | |
213 | if (!chan) { | |
214 | ret = -EINVAL; | |
215 | goto error_unlock; | |
216 | } | |
217 | ||
d0b96690 DG |
218 | /* Check if we've reached the maximum possible id. */ |
219 | if (ust_registry_is_max_id(chan->used_event_id)) { | |
220 | ret = -ENOENT; | |
45893984 | 221 | goto error_unlock; |
d0b96690 DG |
222 | } |
223 | ||
224 | event = alloc_event(session_objd, channel_objd, name, sig, nr_fields, | |
225 | fields, loglevel, model_emf_uri); | |
226 | if (!event) { | |
227 | ret = -ENOMEM; | |
45893984 | 228 | goto error_unlock; |
d0b96690 DG |
229 | } |
230 | ||
d0b96690 | 231 | DBG3("UST registry creating event with event: %s, sig: %s, id: %u, " |
7972aab2 DG |
232 | "chan_objd: %u, sess_objd: %u, chan_id: %u", event->name, |
233 | event->signature, event->id, event->channel_objd, | |
234 | event->session_objd, chan->chan_id); | |
d0b96690 | 235 | |
d0b96690 DG |
236 | /* |
237 | * This is an add unique with a custom match function for event. The node | |
238 | * are matched using the event name and signature. | |
239 | */ | |
7972aab2 | 240 | nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event, |
d0b96690 DG |
241 | lttng_ht_seed), chan->ht->match_fct, event, &event->node.node); |
242 | if (nptr != &event->node.node) { | |
7972aab2 DG |
243 | if (buffer_type == LTTNG_BUFFER_PER_UID) { |
244 | /* | |
245 | * This is normal, we just have to send the event id of the | |
246 | * returned node and make sure we destroy the previously allocated | |
247 | * event object. | |
248 | */ | |
249 | destroy_event(event); | |
250 | event = caa_container_of(nptr, struct ust_registry_event, | |
251 | node.node); | |
252 | assert(event); | |
253 | event_id = event->id; | |
254 | } else { | |
255 | ERR("UST registry create event add unique failed for event: %s, " | |
256 | "sig: %s, id: %u, chan_objd: %u, sess_objd: %u", | |
257 | event->name, event->signature, event->id, | |
258 | event->channel_objd, event->session_objd); | |
259 | ret = -EINVAL; | |
260 | goto error_unlock; | |
261 | } | |
262 | } else { | |
263 | /* Request next event id if the node was successfully added. */ | |
264 | event_id = event->id = ust_registry_get_next_event_id(chan); | |
d0b96690 DG |
265 | } |
266 | ||
7972aab2 | 267 | *event_id_p = event_id; |
d0b96690 | 268 | |
7972aab2 DG |
269 | if (!event->metadata_dumped) { |
270 | /* Append to metadata */ | |
271 | ret = ust_metadata_event_statedump(session, chan, event); | |
272 | if (ret) { | |
273 | ERR("Error appending event metadata (errno = %d)", ret); | |
274 | rcu_read_unlock(); | |
275 | return ret; | |
276 | } | |
d0b96690 DG |
277 | } |
278 | ||
45893984 | 279 | rcu_read_unlock(); |
d0b96690 DG |
280 | return 0; |
281 | ||
282 | error_unlock: | |
283 | rcu_read_unlock(); | |
284 | error: | |
285 | destroy_event(event); | |
286 | return ret; | |
287 | } | |
288 | ||
289 | /* | |
290 | * For a given event in a registry, delete the entry and destroy the event. | |
291 | * This MUST be called within a RCU read side lock section. | |
292 | */ | |
293 | void ust_registry_destroy_event(struct ust_registry_channel *chan, | |
294 | struct ust_registry_event *event) | |
295 | { | |
296 | int ret; | |
297 | struct lttng_ht_iter iter; | |
298 | ||
299 | assert(chan); | |
300 | assert(event); | |
301 | ||
302 | /* Delete the node first. */ | |
303 | iter.iter.node = &event->node.node; | |
304 | ret = lttng_ht_del(chan->ht, &iter); | |
305 | assert(!ret); | |
306 | ||
307 | call_rcu(&event->node.head, destroy_event_rcu); | |
308 | ||
309 | return; | |
310 | } | |
311 | ||
36b588ed MD |
312 | /* |
313 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
314 | * section and outside of call_rcu thread, so we postpone its execution |
315 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
316 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
317 | */ |
318 | static | |
319 | void destroy_channel_rcu(struct rcu_head *head) | |
320 | { | |
321 | struct ust_registry_channel *chan = | |
322 | caa_container_of(head, struct ust_registry_channel, rcu_head); | |
323 | ||
9dbcf332 | 324 | if (chan->ht) { |
0b2dc8df | 325 | ht_cleanup_push(chan->ht); |
9dbcf332 | 326 | } |
3295105b | 327 | free(chan->ctx_fields); |
36b588ed MD |
328 | free(chan); |
329 | } | |
330 | ||
d0b96690 DG |
331 | /* |
332 | * Destroy every element of the registry and free the memory. This does NOT | |
333 | * free the registry pointer since it might not have been allocated before so | |
334 | * it's the caller responsability. | |
d0b96690 | 335 | */ |
45893984 | 336 | static void destroy_channel(struct ust_registry_channel *chan) |
d0b96690 DG |
337 | { |
338 | struct lttng_ht_iter iter; | |
339 | struct ust_registry_event *event; | |
340 | ||
341 | assert(chan); | |
342 | ||
9209cee7 | 343 | rcu_read_lock(); |
d0b96690 DG |
344 | /* Destroy all event associated with this registry. */ |
345 | cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) { | |
346 | /* Delete the node from the ht and free it. */ | |
347 | ust_registry_destroy_event(chan, event); | |
348 | } | |
9209cee7 | 349 | rcu_read_unlock(); |
36b588ed | 350 | call_rcu(&chan->rcu_head, destroy_channel_rcu); |
d0b96690 DG |
351 | } |
352 | ||
353 | /* | |
354 | * Initialize registry with default values. | |
355 | */ | |
45893984 DG |
356 | int ust_registry_channel_add(struct ust_registry_session *session, |
357 | uint64_t key) | |
358 | { | |
359 | int ret = 0; | |
360 | struct ust_registry_channel *chan; | |
361 | ||
362 | assert(session); | |
363 | ||
364 | chan = zmalloc(sizeof(*chan)); | |
365 | if (!chan) { | |
366 | PERROR("zmalloc ust registry channel"); | |
367 | ret = -ENOMEM; | |
9dbcf332 | 368 | goto error_alloc; |
45893984 DG |
369 | } |
370 | ||
371 | chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
372 | if (!chan->ht) { | |
373 | ret = -ENOMEM; | |
374 | goto error; | |
375 | } | |
376 | ||
377 | /* Set custom match function. */ | |
378 | chan->ht->match_fct = ht_match_event; | |
7972aab2 DG |
379 | chan->ht->hash_fct = ht_hash_event; |
380 | ||
381 | /* | |
382 | * Assign a channel ID right now since the event notification comes | |
383 | * *before* the channel notify so the ID needs to be set at this point so | |
384 | * the metadata can be dumped for that event. | |
385 | */ | |
386 | if (ust_registry_is_max_id(session->used_channel_id)) { | |
387 | ret = -1; | |
388 | goto error; | |
389 | } | |
390 | chan->chan_id = ust_registry_get_next_chan_id(session); | |
45893984 DG |
391 | |
392 | rcu_read_lock(); | |
393 | lttng_ht_node_init_u64(&chan->node, key); | |
394 | lttng_ht_add_unique_u64(session->channels, &chan->node); | |
395 | rcu_read_unlock(); | |
396 | ||
9dbcf332 DG |
397 | return 0; |
398 | ||
45893984 | 399 | error: |
9dbcf332 DG |
400 | destroy_channel(chan); |
401 | error_alloc: | |
45893984 DG |
402 | return ret; |
403 | } | |
404 | ||
405 | /* | |
406 | * Find a channel in the given registry. RCU read side lock MUST be acquired | |
407 | * before calling this function and as long as the event reference is kept by | |
408 | * the caller. | |
409 | * | |
410 | * On success, the pointer is returned else NULL. | |
411 | */ | |
412 | struct ust_registry_channel *ust_registry_channel_find( | |
413 | struct ust_registry_session *session, uint64_t key) | |
414 | { | |
415 | struct lttng_ht_node_u64 *node; | |
416 | struct lttng_ht_iter iter; | |
417 | struct ust_registry_channel *chan = NULL; | |
418 | ||
419 | assert(session); | |
420 | assert(session->channels); | |
421 | ||
7972aab2 DG |
422 | DBG3("UST registry channel finding key %" PRIu64, key); |
423 | ||
45893984 DG |
424 | lttng_ht_lookup(session->channels, &key, &iter); |
425 | node = lttng_ht_iter_get_node_u64(&iter); | |
426 | if (!node) { | |
427 | goto end; | |
428 | } | |
429 | chan = caa_container_of(node, struct ust_registry_channel, node); | |
430 | ||
431 | end: | |
432 | return chan; | |
433 | } | |
434 | ||
435 | /* | |
436 | * Remove channel using key from registry and free memory. | |
437 | */ | |
438 | void ust_registry_channel_del_free(struct ust_registry_session *session, | |
439 | uint64_t key) | |
440 | { | |
441 | struct lttng_ht_iter iter; | |
442 | struct ust_registry_channel *chan; | |
9209cee7 | 443 | int ret; |
45893984 DG |
444 | |
445 | assert(session); | |
446 | ||
447 | rcu_read_lock(); | |
448 | chan = ust_registry_channel_find(session, key); | |
449 | if (!chan) { | |
9209cee7 | 450 | rcu_read_unlock(); |
45893984 DG |
451 | goto end; |
452 | } | |
453 | ||
454 | iter.iter.node = &chan->node.node; | |
9209cee7 MD |
455 | ret = lttng_ht_del(session->channels, &iter); |
456 | assert(!ret); | |
457 | rcu_read_unlock(); | |
45893984 DG |
458 | destroy_channel(chan); |
459 | ||
460 | end: | |
45893984 DG |
461 | return; |
462 | } | |
463 | ||
464 | /* | |
465 | * Initialize registry with default values and set the newly allocated session | |
466 | * pointer to sessionp. | |
467 | * | |
468 | * Return 0 on success and sessionp is set or else return -1 and sessionp is | |
469 | * kept untouched. | |
470 | */ | |
471 | int ust_registry_session_init(struct ust_registry_session **sessionp, | |
d0b96690 DG |
472 | struct ust_app *app, |
473 | uint32_t bits_per_long, | |
474 | uint32_t uint8_t_alignment, | |
475 | uint32_t uint16_t_alignment, | |
476 | uint32_t uint32_t_alignment, | |
477 | uint32_t uint64_t_alignment, | |
478 | uint32_t long_alignment, | |
af6142cf MD |
479 | int byte_order, |
480 | uint32_t major, | |
481 | uint32_t minor) | |
d0b96690 DG |
482 | { |
483 | int ret; | |
45893984 | 484 | struct ust_registry_session *session; |
d0b96690 | 485 | |
45893984 | 486 | assert(sessionp); |
d0b96690 | 487 | |
45893984 DG |
488 | session = zmalloc(sizeof(*session)); |
489 | if (!session) { | |
490 | PERROR("zmalloc ust registry session"); | |
9dbcf332 | 491 | goto error_alloc; |
45893984 | 492 | } |
d0b96690 DG |
493 | |
494 | pthread_mutex_init(&session->lock, NULL); | |
495 | session->bits_per_long = bits_per_long; | |
496 | session->uint8_t_alignment = uint8_t_alignment; | |
497 | session->uint16_t_alignment = uint16_t_alignment; | |
498 | session->uint32_t_alignment = uint32_t_alignment; | |
499 | session->uint64_t_alignment = uint64_t_alignment; | |
500 | session->long_alignment = long_alignment; | |
501 | session->byte_order = byte_order; | |
502 | ||
45893984 DG |
503 | session->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
504 | if (!session->channels) { | |
505 | goto error; | |
506 | } | |
507 | ||
d0b96690 DG |
508 | ret = lttng_uuid_generate(session->uuid); |
509 | if (ret) { | |
510 | ERR("Failed to generate UST uuid (errno = %d)", ret); | |
511 | goto error; | |
512 | } | |
513 | ||
514 | pthread_mutex_lock(&session->lock); | |
af6142cf | 515 | ret = ust_metadata_session_statedump(session, app, major, minor); |
d0b96690 DG |
516 | pthread_mutex_unlock(&session->lock); |
517 | if (ret) { | |
518 | ERR("Failed to generate session metadata (errno = %d)", ret); | |
519 | goto error; | |
520 | } | |
521 | ||
45893984 DG |
522 | *sessionp = session; |
523 | ||
d0b96690 DG |
524 | return 0; |
525 | ||
526 | error: | |
9dbcf332 DG |
527 | ust_registry_session_destroy(session); |
528 | error_alloc: | |
d0b96690 DG |
529 | return -1; |
530 | } | |
531 | ||
532 | /* | |
533 | * Destroy session registry. This does NOT free the given pointer since it | |
534 | * might get passed as a reference. The registry lock should NOT be acquired. | |
535 | */ | |
536 | void ust_registry_session_destroy(struct ust_registry_session *reg) | |
537 | { | |
538 | int ret; | |
45893984 DG |
539 | struct lttng_ht_iter iter; |
540 | struct ust_registry_channel *chan; | |
d0b96690 DG |
541 | |
542 | /* On error, EBUSY can be returned if lock. Code flow error. */ | |
543 | ret = pthread_mutex_destroy(®->lock); | |
544 | assert(!ret); | |
545 | ||
45893984 DG |
546 | rcu_read_lock(); |
547 | /* Destroy all event associated with this registry. */ | |
548 | cds_lfht_for_each_entry(reg->channels->ht, &iter.iter, chan, node.node) { | |
549 | /* Delete the node from the ht and free it. */ | |
550 | ret = lttng_ht_del(reg->channels, &iter); | |
551 | assert(!ret); | |
552 | destroy_channel(chan); | |
553 | } | |
45893984 DG |
554 | rcu_read_unlock(); |
555 | ||
9dbcf332 | 556 | if (reg->channels) { |
0b2dc8df | 557 | ht_cleanup_push(reg->channels); |
9dbcf332 | 558 | } |
d0b96690 DG |
559 | free(reg->metadata); |
560 | } |