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