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