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