1b83f4097e9d46aa2ab1a7d009d743193e778bed
[lttng-tools.git] / src / bin / lttng-sessiond / buffer-registry.cpp
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include "buffer-registry.hpp"
10 #include "fd-limit.hpp"
11 #include "lttng-ust-ctl.hpp"
12 #include "lttng-ust-error.hpp"
13 #include "ust-consumer.hpp"
14 #include "utils.hpp"
15
16 #include <common/common.hpp>
17 #include <common/hashtable/utils.hpp>
18 #include <common/urcu.hpp>
19
20 #include <inttypes.h>
21
22 /*
23 * Set in main.c during initialization process of the daemon. This contains
24 * buffer_reg_uid object which are global registry for per UID buffer. Object
25 * are indexed by session id and matched by the triplet
26 * <session_id/bits_per_long/uid>.
27 */
28 static struct lttng_ht *buffer_registry_uid;
29
30 /*
31 * Initialized at the daemon start. This contains buffer_reg_pid object and
32 * indexed by session id.
33 */
34 static struct lttng_ht *buffer_registry_pid;
35
36 /*
37 * Match function for the per UID registry hash table. It matches a registry
38 * uid object with the triplet <session_id/abi/uid>.
39 */
40 static int ht_match_reg_uid(struct cds_lfht_node *node, const void *_key)
41 {
42 LTTNG_ASSERT(node);
43 LTTNG_ASSERT(_key);
44
45 auto *reg = lttng_ht_node_container_of(node, &buffer_reg_uid::node);
46 const auto *key = (buffer_reg_uid *) _key;
47
48 if (key->session_id != reg->session_id || key->bits_per_long != reg->bits_per_long ||
49 key->uid != reg->uid) {
50 goto no_match;
51 }
52
53 /* Match */
54 return 1;
55 no_match:
56 return 0;
57 }
58
59 /*
60 * Hash function for the per UID registry hash table. This XOR the triplet
61 * together.
62 */
63 static unsigned long ht_hash_reg_uid(const void *_key, unsigned long seed)
64 {
65 uint64_t xored_key;
66 const struct buffer_reg_uid *key = (buffer_reg_uid *) _key;
67
68 LTTNG_ASSERT(key);
69
70 xored_key = (uint64_t) (key->session_id ^ key->bits_per_long ^ key->uid);
71 return hash_key_u64(&xored_key, seed);
72 }
73
74 /*
75 * Initialize global buffer per UID registry. Should only be called ONCE!.
76 */
77 void buffer_reg_init_uid_registry()
78 {
79 /* Should be called once. */
80 LTTNG_ASSERT(!buffer_registry_uid);
81 buffer_registry_uid = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
82 LTTNG_ASSERT(buffer_registry_uid);
83 buffer_registry_uid->match_fct = ht_match_reg_uid;
84 buffer_registry_uid->hash_fct = ht_hash_reg_uid;
85
86 DBG3("Global buffer per UID registry initialized");
87 }
88
89 /*
90 * Allocate and initialize object. Set regp with the object pointer.
91 *
92 * Return 0 on success else a negative value and regp is untouched.
93 */
94 int buffer_reg_uid_create(uint64_t session_id,
95 uint32_t bits_per_long,
96 uid_t uid,
97 enum lttng_domain_type domain,
98 struct buffer_reg_uid **regp,
99 const char *root_shm_path,
100 const char *shm_path)
101 {
102 int ret = 0;
103 struct buffer_reg_uid *reg = nullptr;
104
105 LTTNG_ASSERT(regp);
106
107 reg = zmalloc<buffer_reg_uid>();
108 if (!reg) {
109 PERROR("zmalloc buffer registry uid");
110 ret = -ENOMEM;
111 goto error;
112 }
113
114 reg->registry = zmalloc<buffer_reg_session>();
115 if (!reg->registry) {
116 PERROR("zmalloc buffer registry uid session");
117 ret = -ENOMEM;
118 goto error;
119 }
120
121 reg->session_id = session_id;
122 reg->bits_per_long = bits_per_long;
123 reg->uid = uid;
124 reg->domain = domain;
125 if (shm_path[0]) {
126 strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path));
127 reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0';
128 strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
129 reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
130 DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64,
131 reg->shm_path,
132 session_id);
133 }
134 reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
135 if (!reg->registry->channels) {
136 ret = -ENOMEM;
137 goto error_session;
138 }
139
140 cds_lfht_node_init(&reg->node.node);
141 *regp = reg;
142
143 DBG3("Buffer registry per UID created id: %" PRIu64 ", ABI: %u, uid: %d, domain: %d",
144 session_id,
145 bits_per_long,
146 uid,
147 domain);
148
149 return 0;
150
151 error_session:
152 free(reg->registry);
153 error:
154 free(reg);
155 return ret;
156 }
157
158 /*
159 * Add a buffer registry per UID object to the global registry.
160 */
161 void buffer_reg_uid_add(struct buffer_reg_uid *reg)
162 {
163 struct cds_lfht_node *nodep;
164 struct lttng_ht *ht = buffer_registry_uid;
165
166 LTTNG_ASSERT(reg);
167
168 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64,
169 reg->session_id);
170
171 const lttng::urcu::read_lock_guard read_lock;
172 nodep = cds_lfht_add_unique(
173 ht->ht, ht->hash_fct(reg, lttng_ht_seed), ht->match_fct, reg, &reg->node.node);
174 LTTNG_ASSERT(nodep == &reg->node.node);
175 }
176
177 /*
178 * Find a buffer registry per UID object with given params. RCU read side lock
179 * MUST be acquired before calling this and hold on to protect the object.
180 *
181 * Return the object pointer or NULL on error.
182 */
183 struct buffer_reg_uid *buffer_reg_uid_find(uint64_t session_id, uint32_t bits_per_long, uid_t uid)
184 {
185 struct lttng_ht_node_u64 *node;
186 struct lttng_ht_iter iter;
187 struct buffer_reg_uid *reg = nullptr, key;
188 struct lttng_ht *ht = buffer_registry_uid;
189
190 ASSERT_RCU_READ_LOCKED();
191
192 /* Setup key we are looking for. */
193 key.session_id = session_id;
194 key.bits_per_long = bits_per_long;
195 key.uid = uid;
196
197 DBG3("Buffer registry per UID find id: %" PRIu64 ", ABI: %u, uid: %d",
198 session_id,
199 bits_per_long,
200 uid);
201
202 /* Custom lookup function since it's a different key. */
203 cds_lfht_lookup(ht->ht, ht->hash_fct(&key, lttng_ht_seed), ht->match_fct, &key, &iter.iter);
204 node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
205 if (!node) {
206 goto end;
207 }
208 reg = lttng::utils::container_of(node, &buffer_reg_uid::node);
209
210 end:
211 return reg;
212 }
213
214 /*
215 * Initialize global buffer per PID registry. Should only be called ONCE!.
216 */
217 void buffer_reg_init_pid_registry()
218 {
219 /* Should be called once. */
220 LTTNG_ASSERT(!buffer_registry_pid);
221 buffer_registry_pid = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
222 LTTNG_ASSERT(buffer_registry_pid);
223
224 DBG3("Global buffer per PID registry initialized");
225 }
226
227 /*
228 * Allocate and initialize object. Set regp with the object pointer.
229 *
230 * Return 0 on success else a negative value and regp is untouched.
231 */
232 int buffer_reg_pid_create(uint64_t session_id,
233 struct buffer_reg_pid **regp,
234 const char *root_shm_path,
235 const char *shm_path)
236 {
237 int ret = 0;
238 struct buffer_reg_pid *reg = nullptr;
239
240 LTTNG_ASSERT(regp);
241
242 reg = zmalloc<buffer_reg_pid>();
243 if (!reg) {
244 PERROR("zmalloc buffer registry pid");
245 ret = -ENOMEM;
246 goto error;
247 }
248
249 reg->registry = zmalloc<buffer_reg_session>();
250 if (!reg->registry) {
251 PERROR("zmalloc buffer registry pid session");
252 ret = -ENOMEM;
253 goto error;
254 }
255
256 /* A cast is done here so we can use the session ID as a u64 ht node. */
257 reg->session_id = session_id;
258 if (shm_path[0]) {
259 strncpy(reg->root_shm_path, root_shm_path, sizeof(reg->root_shm_path));
260 reg->root_shm_path[sizeof(reg->root_shm_path) - 1] = '\0';
261 strncpy(reg->shm_path, shm_path, sizeof(reg->shm_path));
262 reg->shm_path[sizeof(reg->shm_path) - 1] = '\0';
263 DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64,
264 reg->shm_path,
265 session_id);
266 }
267 reg->registry->channels = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
268 if (!reg->registry->channels) {
269 ret = -ENOMEM;
270 goto error_session;
271 }
272
273 lttng_ht_node_init_u64(&reg->node, reg->session_id);
274 *regp = reg;
275
276 DBG3("Buffer registry per PID created with session id: %" PRIu64, session_id);
277
278 return 0;
279
280 error_session:
281 free(reg->registry);
282 error:
283 free(reg);
284 return ret;
285 }
286
287 /*
288 * Add a buffer registry per PID object to the global registry.
289 */
290 void buffer_reg_pid_add(struct buffer_reg_pid *reg)
291 {
292 LTTNG_ASSERT(reg);
293
294 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64,
295 reg->session_id);
296
297 const lttng::urcu::read_lock_guard read_lock;
298 lttng_ht_add_unique_u64(buffer_registry_pid, &reg->node);
299 }
300
301 /*
302 * Find a buffer registry per PID object with given params. RCU read side lock
303 * MUST be acquired before calling this and hold on to protect the object.
304 *
305 * Return the object pointer or NULL on error.
306 */
307 struct buffer_reg_pid *buffer_reg_pid_find(uint64_t session_id)
308 {
309 struct lttng_ht_node_u64 *node;
310 struct lttng_ht_iter iter;
311 struct buffer_reg_pid *reg = nullptr;
312 struct lttng_ht *ht = buffer_registry_pid;
313
314 DBG3("Buffer registry per PID find id: %" PRIu64, session_id);
315
316 lttng_ht_lookup(ht, &session_id, &iter);
317 node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
318 if (!node) {
319 goto end;
320 }
321 reg = lttng::utils::container_of(node, &buffer_reg_pid::node);
322
323 end:
324 return reg;
325 }
326
327 /*
328 * Find the consumer channel key from a UST session per-uid channel key.
329 *
330 * Return the matching key or -1 if not found.
331 */
332 int buffer_reg_uid_consumer_channel_key(struct cds_list_head *buffer_reg_uid_list,
333 uint64_t chan_key,
334 uint64_t *consumer_chan_key)
335 {
336 int ret = -1;
337
338 /*
339 * For the per-uid registry, we have to iterate since we don't have the
340 * uid and bitness key.
341 */
342 for (auto uid_reg :
343 lttng::urcu::list_iteration_adapter<buffer_reg_uid, &buffer_reg_uid::lnode>(
344 *buffer_reg_uid_list)) {
345 auto *session_reg = uid_reg->registry;
346 for (auto *reg_chan :
347 lttng::urcu::lfht_iteration_adapter<buffer_reg_channel,
348 decltype(buffer_reg_channel::node),
349 &buffer_reg_channel::node>(
350 *session_reg->channels->ht)) {
351 if (reg_chan->key == chan_key) {
352 *consumer_chan_key = reg_chan->consumer_key;
353 ret = 0;
354 goto end;
355 }
356 }
357 }
358 end:
359 return ret;
360 }
361
362 /*
363 * Allocate and initialize a buffer registry channel with the given key. Set
364 * regp with the object pointer.
365 *
366 * Return 0 on success or else a negative value keeping regp untouched.
367 */
368 int buffer_reg_channel_create(uint64_t key, struct buffer_reg_channel **regp)
369 {
370 struct buffer_reg_channel *reg;
371
372 LTTNG_ASSERT(regp);
373
374 DBG3("Buffer registry channel create with key: %" PRIu64, key);
375
376 reg = zmalloc<buffer_reg_channel>();
377 if (!reg) {
378 PERROR("zmalloc buffer registry channel");
379 return -ENOMEM;
380 }
381
382 reg->key = key;
383 CDS_INIT_LIST_HEAD(&reg->streams);
384 pthread_mutex_init(&reg->stream_list_lock, nullptr);
385
386 lttng_ht_node_init_u64(&reg->node, key);
387 *regp = reg;
388
389 return 0;
390 }
391
392 /*
393 * Allocate and initialize a buffer registry stream. Set regp with the object
394 * pointer.
395 *
396 * Return 0 on success or else a negative value keeping regp untouched.
397 */
398 int buffer_reg_stream_create(struct buffer_reg_stream **regp)
399 {
400 struct buffer_reg_stream *reg;
401
402 LTTNG_ASSERT(regp);
403
404 DBG3("Buffer registry creating stream");
405
406 reg = zmalloc<buffer_reg_stream>();
407 if (!reg) {
408 PERROR("zmalloc buffer registry stream");
409 return -ENOMEM;
410 }
411
412 *regp = reg;
413
414 return 0;
415 }
416
417 /*
418 * Add stream to the list in the channel.
419 */
420 void buffer_reg_stream_add(struct buffer_reg_stream *stream, struct buffer_reg_channel *channel)
421 {
422 LTTNG_ASSERT(stream);
423 LTTNG_ASSERT(channel);
424
425 pthread_mutex_lock(&channel->stream_list_lock);
426 cds_list_add_tail(&stream->lnode, &channel->streams);
427 channel->stream_count++;
428 pthread_mutex_unlock(&channel->stream_list_lock);
429 }
430
431 /*
432 * Add a buffer registry channel object to the given session.
433 */
434 void buffer_reg_channel_add(struct buffer_reg_session *session, struct buffer_reg_channel *channel)
435 {
436 LTTNG_ASSERT(session);
437 LTTNG_ASSERT(channel);
438
439 const lttng::urcu::read_lock_guard read_lock;
440 lttng_ht_add_unique_u64(session->channels, &channel->node);
441 }
442
443 /*
444 * Find a buffer registry channel object with the given key. RCU read side lock
445 * MUST be acquired and hold on until the object reference is not needed
446 * anymore.
447 *
448 * Return the object pointer or NULL on error.
449 */
450 struct buffer_reg_channel *buffer_reg_channel_find(uint64_t key, struct buffer_reg_uid *reg)
451 {
452 struct lttng_ht_node_u64 *node;
453 struct lttng_ht_iter iter;
454 struct buffer_reg_channel *chan = nullptr;
455 struct lttng_ht *ht;
456
457 LTTNG_ASSERT(reg);
458
459 switch (reg->domain) {
460 case LTTNG_DOMAIN_UST:
461 ht = reg->registry->channels;
462 break;
463 default:
464 abort();
465 goto end;
466 }
467
468 lttng_ht_lookup(ht, &key, &iter);
469 node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter);
470 if (!node) {
471 goto end;
472 }
473 chan = lttng::utils::container_of(node, &buffer_reg_channel::node);
474
475 end:
476 return chan;
477 }
478
479 /*
480 * Destroy a buffer registry stream with the given domain.
481 */
482 void buffer_reg_stream_destroy(struct buffer_reg_stream *regp, enum lttng_domain_type domain)
483 {
484 if (!regp) {
485 return;
486 }
487
488 DBG3("Buffer registry stream destroy with handle %d", regp->obj.ust->handle);
489
490 switch (domain) {
491 case LTTNG_DOMAIN_UST:
492 {
493 int ret;
494
495 ret = ust_app_release_object(nullptr, regp->obj.ust);
496 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
497 ERR("Buffer reg stream release obj handle %d failed with ret %d",
498 regp->obj.ust->handle,
499 ret);
500 }
501 free(regp->obj.ust);
502 lttng_fd_put(LTTNG_FD_APPS, 2);
503 break;
504 }
505 default:
506 abort();
507 }
508
509 free(regp);
510 return;
511 }
512
513 /*
514 * Remove buffer registry channel object from the session hash table. RCU read
515 * side lock MUST be acquired before calling this.
516 */
517 void buffer_reg_channel_remove(struct buffer_reg_session *session, struct buffer_reg_channel *regp)
518 {
519 int ret;
520 struct lttng_ht_iter iter;
521
522 LTTNG_ASSERT(session);
523 LTTNG_ASSERT(regp);
524
525 iter.iter.node = &regp->node.node;
526 ret = lttng_ht_del(session->channels, &iter);
527 LTTNG_ASSERT(!ret);
528 }
529
530 /*
531 * Destroy a buffer registry channel with the given domain.
532 */
533 void buffer_reg_channel_destroy(struct buffer_reg_channel *regp, enum lttng_domain_type domain)
534 {
535 if (!regp) {
536 return;
537 }
538
539 DBG3("Buffer registry channel destroy with key %" PRIu32, regp->key);
540
541 switch (domain) {
542 case LTTNG_DOMAIN_UST:
543 {
544 int ret;
545
546 /* Wipe stream */
547 for (auto reg_stream :
548 lttng::urcu::list_iteration_adapter<buffer_reg_stream,
549 &buffer_reg_stream::lnode>(
550 regp->streams)) {
551 cds_list_del(&reg_stream->lnode);
552 regp->stream_count--;
553 buffer_reg_stream_destroy(reg_stream, domain);
554 }
555
556 if (regp->obj.ust) {
557 ret = ust_app_release_object(nullptr, regp->obj.ust);
558 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
559 ERR("Buffer reg channel release obj handle %d failed with ret %d",
560 regp->obj.ust->handle,
561 ret);
562 }
563 free(regp->obj.ust);
564 }
565 lttng_fd_put(LTTNG_FD_APPS, 1);
566 break;
567 }
568 default:
569 abort();
570 }
571
572 free(regp);
573 return;
574 }
575
576 /*
577 * Destroy a buffer registry session with the given domain.
578 */
579 static void buffer_reg_session_destroy(struct buffer_reg_session *regp,
580 enum lttng_domain_type domain)
581 {
582 DBG3("Buffer registry session destroy");
583
584 /* Destroy all channels. */
585 for (auto *reg_chan :
586 lttng::urcu::lfht_iteration_adapter<buffer_reg_channel,
587 decltype(buffer_reg_channel::node),
588 &buffer_reg_channel::node>(*regp->channels->ht)) {
589 const auto ret = cds_lfht_del(regp->channels->ht, &reg_chan->node.node);
590 LTTNG_ASSERT(!ret);
591 buffer_reg_channel_destroy(reg_chan, domain);
592 }
593
594 lttng_ht_destroy(regp->channels);
595
596 switch (domain) {
597 case LTTNG_DOMAIN_UST:
598 ust_registry_session_destroy(regp->reg.ust);
599 break;
600 default:
601 abort();
602 }
603
604 free(regp);
605 return;
606 }
607
608 /*
609 * Remove buffer registry UID object from the global hash table.
610 */
611 void buffer_reg_uid_remove(struct buffer_reg_uid *regp)
612 {
613 int ret;
614 struct lttng_ht_iter iter;
615
616 LTTNG_ASSERT(regp);
617
618 const lttng::urcu::read_lock_guard read_lock;
619 iter.iter.node = &regp->node.node;
620 ret = lttng_ht_del(buffer_registry_uid, &iter);
621 LTTNG_ASSERT(!ret);
622 }
623
624 static void rcu_free_buffer_reg_uid(struct rcu_head *head)
625 {
626 struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, &lttng_ht_node_u64::head);
627 struct buffer_reg_uid *reg = lttng::utils::container_of(node, &buffer_reg_uid::node);
628
629 buffer_reg_session_destroy(reg->registry, reg->domain);
630 free(reg);
631 }
632
633 static void rcu_free_buffer_reg_pid(struct rcu_head *head)
634 {
635 struct lttng_ht_node_u64 *node = lttng::utils::container_of(head, &lttng_ht_node_u64::head);
636 struct buffer_reg_pid *reg = lttng::utils::container_of(node, &buffer_reg_pid::node);
637
638 buffer_reg_session_destroy(reg->registry, LTTNG_DOMAIN_UST);
639 free(reg);
640 }
641
642 /*
643 * Destroy buffer registry per UID. The given pointer is NOT removed from any
644 * list or hash table. Use buffer_reg_pid_remove() before calling this function
645 * for the case that the object is in the global hash table.
646 */
647 void buffer_reg_uid_destroy(struct buffer_reg_uid *regp, struct consumer_output *consumer)
648 {
649 struct consumer_socket *socket;
650
651 if (!regp) {
652 return;
653 }
654
655 DBG3("Buffer registry per UID destroy with id: %" PRIu64 ", ABI: %u, uid: %d",
656 regp->session_id,
657 regp->bits_per_long,
658 regp->uid);
659
660 if (!consumer) {
661 goto destroy;
662 }
663
664 {
665 const lttng::urcu::read_lock_guard read_lock;
666 /* Get the right socket from the consumer object. */
667 socket = consumer_find_socket_by_bitness(regp->bits_per_long, consumer);
668 if (!socket) {
669 goto destroy;
670 }
671
672 switch (regp->domain) {
673 case LTTNG_DOMAIN_UST:
674 if (regp->registry->reg.ust->_metadata_key) {
675 /* Return value does not matter. This call will print errors. */
676 (void) consumer_close_metadata(
677 socket, regp->registry->reg.ust->_metadata_key);
678 }
679 break;
680 default:
681 abort();
682 return;
683 }
684 }
685
686 destroy:
687 call_rcu(&regp->node.head, rcu_free_buffer_reg_uid);
688 }
689
690 /*
691 * Remove buffer registry UID object from the global hash table. RCU read side
692 * lock MUST be acquired before calling this.
693 */
694 void buffer_reg_pid_remove(struct buffer_reg_pid *regp)
695 {
696 int ret;
697 struct lttng_ht_iter iter;
698
699 LTTNG_ASSERT(regp);
700
701 iter.iter.node = &regp->node.node;
702 ret = lttng_ht_del(buffer_registry_pid, &iter);
703 LTTNG_ASSERT(!ret);
704 }
705
706 /*
707 * Destroy buffer registry per PID. The pointer is NOT removed from the global
708 * hash table. Call buffer_reg_pid_remove() before that if the object was
709 * previously added to the global hash table.
710 */
711 void buffer_reg_pid_destroy(struct buffer_reg_pid *regp)
712 {
713 if (!regp) {
714 return;
715 }
716
717 DBG3("Buffer registry per PID destroy with id: %" PRIu64, regp->session_id);
718
719 /* This registry is only used by UST. */
720 call_rcu(&regp->node.head, rcu_free_buffer_reg_pid);
721 }
722
723 /*
724 * Destroy per PID and UID registry hash table.
725 */
726 void buffer_reg_destroy_registries()
727 {
728 DBG3("Buffer registry destroy all registry");
729 lttng_ht_destroy(buffer_registry_uid);
730 lttng_ht_destroy(buffer_registry_pid);
731 }
This page took 0.04438 seconds and 5 git commands to generate.