Commit | Line | Data |
---|---|---|
51230d70 DG |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License, version 2 only, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
51230d70 | 21 | #include <assert.h> |
10a50311 | 22 | #include <inttypes.h> |
51230d70 DG |
23 | #include <sys/mman.h> |
24 | #include <unistd.h> | |
25 | ||
26 | #include <common/common.h> | |
1c20f0e2 | 27 | #include <common/index/index.h> |
94d49140 | 28 | #include <common/kernel-consumer/kernel-consumer.h> |
51230d70 DG |
29 | #include <common/relayd/relayd.h> |
30 | #include <common/ust-consumer/ust-consumer.h> | |
a2361a61 | 31 | #include <common/utils.h> |
51230d70 DG |
32 | |
33 | #include "consumer-stream.h" | |
34 | ||
35 | /* | |
36 | * RCU call to free stream. MUST only be used with call_rcu(). | |
37 | */ | |
38 | static void free_stream_rcu(struct rcu_head *head) | |
39 | { | |
40 | struct lttng_ht_node_u64 *node = | |
41 | caa_container_of(head, struct lttng_ht_node_u64, head); | |
42 | struct lttng_consumer_stream *stream = | |
43 | caa_container_of(node, struct lttng_consumer_stream, node); | |
44 | ||
45 | pthread_mutex_destroy(&stream->lock); | |
46 | free(stream); | |
47 | } | |
48 | ||
49 | /* | |
50 | * Close stream on the relayd side. This call can destroy a relayd if the | |
51 | * conditions are met. | |
52 | * | |
53 | * A RCU read side lock MUST be acquired if the relayd object was looked up in | |
54 | * a hash table before calling this. | |
55 | */ | |
56 | void consumer_stream_relayd_close(struct lttng_consumer_stream *stream, | |
57 | struct consumer_relayd_sock_pair *relayd) | |
58 | { | |
59 | int ret; | |
60 | ||
61 | assert(stream); | |
62 | assert(relayd); | |
63 | ||
d01178b6 DG |
64 | if (stream->sent_to_relayd) { |
65 | uatomic_dec(&relayd->refcount); | |
66 | assert(uatomic_read(&relayd->refcount) >= 0); | |
67 | } | |
51230d70 DG |
68 | |
69 | /* Closing streams requires to lock the control socket. */ | |
70 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
71 | ret = relayd_send_close_stream(&relayd->control_sock, | |
72 | stream->relayd_stream_id, | |
73 | stream->next_net_seq_num - 1); | |
74 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); | |
75 | if (ret < 0) { | |
76 | DBG("Unable to close stream on the relayd. Continuing"); | |
77 | /* | |
78 | * Continue here. There is nothing we can do for the relayd. | |
79 | * Chances are that the relayd has closed the socket so we just | |
80 | * continue cleaning up. | |
81 | */ | |
82 | } | |
83 | ||
84 | /* Both conditions are met, we destroy the relayd. */ | |
85 | if (uatomic_read(&relayd->refcount) == 0 && | |
86 | uatomic_read(&relayd->destroy_flag)) { | |
87 | consumer_destroy_relayd(relayd); | |
88 | } | |
10a50311 | 89 | stream->net_seq_idx = (uint64_t) -1ULL; |
d01178b6 | 90 | stream->sent_to_relayd = 0; |
51230d70 DG |
91 | } |
92 | ||
93 | /* | |
94 | * Close stream's file descriptors and, if needed, close stream also on the | |
95 | * relayd side. | |
96 | * | |
97 | * The consumer data lock MUST be acquired. | |
98 | * The stream lock MUST be acquired. | |
99 | */ | |
100 | void consumer_stream_close(struct lttng_consumer_stream *stream) | |
101 | { | |
102 | int ret; | |
103 | struct consumer_relayd_sock_pair *relayd; | |
104 | ||
105 | assert(stream); | |
106 | ||
107 | switch (consumer_data.type) { | |
108 | case LTTNG_CONSUMER_KERNEL: | |
109 | if (stream->mmap_base != NULL) { | |
110 | ret = munmap(stream->mmap_base, stream->mmap_len); | |
111 | if (ret != 0) { | |
112 | PERROR("munmap"); | |
113 | } | |
114 | } | |
115 | ||
116 | if (stream->wait_fd >= 0) { | |
117 | ret = close(stream->wait_fd); | |
118 | if (ret) { | |
119 | PERROR("close"); | |
120 | } | |
10a50311 | 121 | stream->wait_fd = -1; |
51230d70 | 122 | } |
a2361a61 JD |
123 | if (stream->chan->output == CONSUMER_CHANNEL_SPLICE) { |
124 | utils_close_pipe(stream->splice_pipe); | |
125 | } | |
51230d70 DG |
126 | break; |
127 | case LTTNG_CONSUMER32_UST: | |
128 | case LTTNG_CONSUMER64_UST: | |
6d574024 DG |
129 | { |
130 | /* | |
131 | * Special case for the metadata since the wait fd is an internal pipe | |
132 | * polled in the metadata thread. | |
133 | */ | |
134 | if (stream->metadata_flag && stream->chan->monitor) { | |
135 | int rpipe = stream->ust_metadata_poll_pipe[0]; | |
136 | ||
137 | /* | |
138 | * This will stop the channel timer if one and close the write side | |
139 | * of the metadata poll pipe. | |
140 | */ | |
141 | lttng_ustconsumer_close_metadata(stream->chan); | |
142 | if (rpipe >= 0) { | |
143 | ret = close(rpipe); | |
144 | if (ret < 0) { | |
b4a650f3 | 145 | PERROR("closing metadata pipe read side"); |
6d574024 DG |
146 | } |
147 | stream->ust_metadata_poll_pipe[0] = -1; | |
148 | } | |
149 | } | |
51230d70 | 150 | break; |
6d574024 | 151 | } |
51230d70 DG |
152 | default: |
153 | ERR("Unknown consumer_data type"); | |
154 | assert(0); | |
155 | } | |
156 | ||
157 | /* Close output fd. Could be a socket or local file at this point. */ | |
158 | if (stream->out_fd >= 0) { | |
159 | ret = close(stream->out_fd); | |
160 | if (ret) { | |
161 | PERROR("close"); | |
162 | } | |
10a50311 | 163 | stream->out_fd = -1; |
51230d70 DG |
164 | } |
165 | ||
f8f3885c MD |
166 | if (stream->index_file) { |
167 | lttng_index_file_put(stream->index_file); | |
168 | stream->index_file = NULL; | |
309167d2 JD |
169 | } |
170 | ||
51230d70 DG |
171 | /* Check and cleanup relayd if needed. */ |
172 | rcu_read_lock(); | |
173 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
174 | if (relayd != NULL) { | |
175 | consumer_stream_relayd_close(stream, relayd); | |
176 | } | |
177 | rcu_read_unlock(); | |
178 | } | |
179 | ||
180 | /* | |
181 | * Delete the stream from all possible hash tables. | |
182 | * | |
183 | * The consumer data lock MUST be acquired. | |
184 | * The stream lock MUST be acquired. | |
185 | */ | |
186 | void consumer_stream_delete(struct lttng_consumer_stream *stream, | |
187 | struct lttng_ht *ht) | |
188 | { | |
189 | int ret; | |
190 | struct lttng_ht_iter iter; | |
191 | ||
192 | assert(stream); | |
10a50311 JD |
193 | /* Should NEVER be called not in monitor mode. */ |
194 | assert(stream->chan->monitor); | |
51230d70 DG |
195 | |
196 | rcu_read_lock(); | |
197 | ||
198 | if (ht) { | |
199 | iter.iter.node = &stream->node.node; | |
200 | ret = lttng_ht_del(ht, &iter); | |
201 | assert(!ret); | |
202 | } | |
203 | ||
204 | /* Delete from stream per channel ID hash table. */ | |
205 | iter.iter.node = &stream->node_channel_id.node; | |
206 | /* | |
207 | * The returned value is of no importance. Even if the node is NOT in the | |
208 | * hash table, we continue since we may have been called by a code path | |
209 | * that did not add the stream to a (all) hash table. Same goes for the | |
210 | * next call ht del call. | |
211 | */ | |
212 | (void) lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter); | |
213 | ||
214 | /* Delete from the global stream list. */ | |
215 | iter.iter.node = &stream->node_session_id.node; | |
216 | /* See the previous ht del on why we ignore the returned value. */ | |
217 | (void) lttng_ht_del(consumer_data.stream_list_ht, &iter); | |
218 | ||
219 | rcu_read_unlock(); | |
220 | ||
6d574024 DG |
221 | if (!stream->metadata_flag) { |
222 | /* Decrement the stream count of the global consumer data. */ | |
223 | assert(consumer_data.stream_count > 0); | |
224 | consumer_data.stream_count--; | |
225 | } | |
51230d70 DG |
226 | } |
227 | ||
228 | /* | |
229 | * Free the given stream within a RCU call. | |
230 | */ | |
231 | void consumer_stream_free(struct lttng_consumer_stream *stream) | |
232 | { | |
233 | assert(stream); | |
234 | ||
235 | call_rcu(&stream->node.head, free_stream_rcu); | |
236 | } | |
237 | ||
238 | /* | |
10a50311 | 239 | * Destroy the stream's buffers of the tracer. |
51230d70 | 240 | */ |
10a50311 | 241 | void consumer_stream_destroy_buffers(struct lttng_consumer_stream *stream) |
51230d70 | 242 | { |
10a50311 JD |
243 | assert(stream); |
244 | ||
245 | switch (consumer_data.type) { | |
246 | case LTTNG_CONSUMER_KERNEL: | |
247 | break; | |
248 | case LTTNG_CONSUMER32_UST: | |
249 | case LTTNG_CONSUMER64_UST: | |
250 | lttng_ustconsumer_del_stream(stream); | |
251 | break; | |
252 | default: | |
253 | ERR("Unknown consumer_data type"); | |
254 | assert(0); | |
255 | } | |
256 | } | |
51230d70 | 257 | |
10a50311 | 258 | /* |
4891ece8 | 259 | * Destroy and close a already created stream. |
10a50311 | 260 | */ |
4891ece8 | 261 | static void destroy_close_stream(struct lttng_consumer_stream *stream) |
10a50311 | 262 | { |
51230d70 DG |
263 | assert(stream); |
264 | ||
4891ece8 | 265 | DBG("Consumer stream destroy monitored key: %" PRIu64, stream->key); |
10a50311 JD |
266 | |
267 | /* Destroy tracer buffers of the stream. */ | |
268 | consumer_stream_destroy_buffers(stream); | |
269 | /* Close down everything including the relayd if one. */ | |
270 | consumer_stream_close(stream); | |
271 | } | |
51230d70 | 272 | |
10a50311 | 273 | /* |
4891ece8 DG |
274 | * Decrement the stream's channel refcount and if down to 0, return the channel |
275 | * pointer so it can be destroyed by the caller or NULL if not. | |
10a50311 | 276 | */ |
4891ece8 DG |
277 | static struct lttng_consumer_channel *unref_channel( |
278 | struct lttng_consumer_stream *stream) | |
10a50311 | 279 | { |
4891ece8 DG |
280 | struct lttng_consumer_channel *free_chan = NULL; |
281 | ||
10a50311 | 282 | assert(stream); |
4891ece8 | 283 | assert(stream->chan); |
10a50311 | 284 | |
4891ece8 DG |
285 | /* Update refcount of channel and see if we need to destroy it. */ |
286 | if (!uatomic_sub_return(&stream->chan->refcount, 1) | |
287 | && !uatomic_read(&stream->chan->nb_init_stream_left)) { | |
288 | free_chan = stream->chan; | |
289 | } | |
51230d70 | 290 | |
4891ece8 | 291 | return free_chan; |
10a50311 | 292 | } |
51230d70 | 293 | |
10a50311 JD |
294 | /* |
295 | * Destroy a stream completely. This will delete, close and free the stream. | |
296 | * Once return, the stream is NO longer usable. Its channel may get destroyed | |
297 | * if conditions are met for a monitored stream. | |
298 | * | |
299 | * This MUST be called WITHOUT the consumer data and stream lock acquired if | |
300 | * the stream is in _monitor_ mode else it does not matter. | |
301 | */ | |
302 | void consumer_stream_destroy(struct lttng_consumer_stream *stream, | |
303 | struct lttng_ht *ht) | |
304 | { | |
305 | assert(stream); | |
306 | ||
307 | /* Stream is in monitor mode. */ | |
4891ece8 | 308 | if (stream->monitor) { |
10a50311 | 309 | struct lttng_consumer_channel *free_chan = NULL; |
51230d70 | 310 | |
4891ece8 DG |
311 | /* |
312 | * This means that the stream was successfully removed from the streams | |
313 | * list of the channel and sent to the right thread managing this | |
314 | * stream thus being globally visible. | |
315 | */ | |
316 | if (stream->globally_visible) { | |
317 | pthread_mutex_lock(&consumer_data.lock); | |
a9838785 | 318 | pthread_mutex_lock(&stream->chan->lock); |
4891ece8 DG |
319 | pthread_mutex_lock(&stream->lock); |
320 | /* Remove every reference of the stream in the consumer. */ | |
321 | consumer_stream_delete(stream, ht); | |
322 | ||
323 | destroy_close_stream(stream); | |
324 | ||
325 | /* Update channel's refcount of the stream. */ | |
326 | free_chan = unref_channel(stream); | |
327 | ||
328 | /* Indicates that the consumer data state MUST be updated after this. */ | |
329 | consumer_data.need_update = 1; | |
330 | ||
331 | pthread_mutex_unlock(&stream->lock); | |
a9838785 | 332 | pthread_mutex_unlock(&stream->chan->lock); |
4891ece8 DG |
333 | pthread_mutex_unlock(&consumer_data.lock); |
334 | } else { | |
335 | /* | |
336 | * If the stream is not visible globally, this needs to be done | |
337 | * outside of the consumer data lock section. | |
338 | */ | |
339 | free_chan = unref_channel(stream); | |
10a50311 JD |
340 | } |
341 | ||
10a50311 JD |
342 | if (free_chan) { |
343 | consumer_del_channel(free_chan); | |
344 | } | |
345 | } else { | |
4891ece8 | 346 | destroy_close_stream(stream); |
51230d70 DG |
347 | } |
348 | ||
349 | /* Free stream within a RCU call. */ | |
350 | consumer_stream_free(stream); | |
351 | } | |
1c20f0e2 JD |
352 | |
353 | /* | |
354 | * Write index of a specific stream either on the relayd or local disk. | |
355 | * | |
356 | * Return 0 on success or else a negative value. | |
357 | */ | |
358 | int consumer_stream_write_index(struct lttng_consumer_stream *stream, | |
f8f3885c | 359 | struct ctf_packet_index *element) |
1c20f0e2 JD |
360 | { |
361 | int ret; | |
1c20f0e2 JD |
362 | |
363 | assert(stream); | |
f8f3885c | 364 | assert(element); |
1c20f0e2 JD |
365 | |
366 | rcu_read_lock(); | |
23c910e5 JR |
367 | if (stream->net_seq_idx != (uint64_t) -1ULL) { |
368 | struct consumer_relayd_sock_pair *relayd; | |
369 | relayd = consumer_find_relayd(stream->net_seq_idx); | |
370 | if (relayd) { | |
371 | pthread_mutex_lock(&relayd->ctrl_sock_mutex); | |
372 | ret = relayd_send_index(&relayd->control_sock, element, | |
1c20f0e2 | 373 | stream->relayd_stream_id, stream->next_net_seq_num - 1); |
23c910e5 JR |
374 | pthread_mutex_unlock(&relayd->ctrl_sock_mutex); |
375 | } else { | |
376 | ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't write index.", | |
377 | stream->key, stream->net_seq_idx); | |
378 | ret = -1; | |
379 | } | |
1c20f0e2 | 380 | } else { |
f8f3885c | 381 | if (lttng_index_file_write(stream->index_file, element)) { |
6cd525e8 MD |
382 | ret = -1; |
383 | } else { | |
384 | ret = 0; | |
385 | } | |
1c20f0e2 JD |
386 | } |
387 | if (ret < 0) { | |
388 | goto error; | |
389 | } | |
390 | ||
391 | error: | |
392 | rcu_read_unlock(); | |
393 | return ret; | |
394 | } | |
94d49140 JD |
395 | |
396 | /* | |
e5ca40ee | 397 | * Actually do the metadata sync using the given metadata stream. |
94d49140 | 398 | * |
e5ca40ee DG |
399 | * Return 0 on success else a negative value. ENODATA can be returned also |
400 | * indicating that there is no metadata available for that stream. | |
94d49140 | 401 | */ |
e5ca40ee DG |
402 | static int do_sync_metadata(struct lttng_consumer_stream *metadata, |
403 | struct lttng_consumer_local_data *ctx) | |
94d49140 JD |
404 | { |
405 | int ret; | |
94d49140 | 406 | |
e5ca40ee DG |
407 | assert(metadata); |
408 | assert(metadata->metadata_flag); | |
94d49140 JD |
409 | assert(ctx); |
410 | ||
94d49140 JD |
411 | /* |
412 | * In UST, since we have to write the metadata from the cache packet | |
413 | * by packet, we might need to start this procedure multiple times | |
414 | * until all the metadata from the cache has been extracted. | |
415 | */ | |
416 | do { | |
417 | /* | |
418 | * Steps : | |
419 | * - Lock the metadata stream | |
420 | * - Check if metadata stream node was deleted before locking. | |
421 | * - if yes, release and return success | |
422 | * - Check if new metadata is ready (flush + snapshot pos) | |
423 | * - If nothing : release and return. | |
424 | * - Lock the metadata_rdv_lock | |
425 | * - Unlock the metadata stream | |
426 | * - cond_wait on metadata_rdv to wait the wakeup from the | |
427 | * metadata thread | |
428 | * - Unlock the metadata_rdv_lock | |
429 | */ | |
430 | pthread_mutex_lock(&metadata->lock); | |
431 | ||
432 | /* | |
433 | * There is a possibility that we were able to acquire a reference on the | |
434 | * stream from the RCU hash table but between then and now, the node might | |
435 | * have been deleted just before the lock is acquired. Thus, after locking, | |
436 | * we make sure the metadata node has not been deleted which means that the | |
437 | * buffers are closed. | |
438 | * | |
439 | * In that case, there is no need to sync the metadata hence returning a | |
440 | * success return code. | |
441 | */ | |
442 | ret = cds_lfht_is_node_deleted(&metadata->node.node); | |
443 | if (ret) { | |
444 | ret = 0; | |
445 | goto end_unlock_mutex; | |
446 | } | |
447 | ||
448 | switch (ctx->type) { | |
449 | case LTTNG_CONSUMER_KERNEL: | |
450 | /* | |
451 | * Empty the metadata cache and flush the current stream. | |
452 | */ | |
453 | ret = lttng_kconsumer_sync_metadata(metadata); | |
454 | break; | |
455 | case LTTNG_CONSUMER32_UST: | |
456 | case LTTNG_CONSUMER64_UST: | |
457 | /* | |
458 | * Ask the sessiond if we have new metadata waiting and update the | |
459 | * consumer metadata cache. | |
460 | */ | |
461 | ret = lttng_ustconsumer_sync_metadata(ctx, metadata); | |
462 | break; | |
463 | default: | |
464 | assert(0); | |
465 | ret = -1; | |
466 | break; | |
467 | } | |
468 | /* | |
469 | * Error or no new metadata, we exit here. | |
470 | */ | |
471 | if (ret <= 0 || ret == ENODATA) { | |
472 | goto end_unlock_mutex; | |
473 | } | |
474 | ||
475 | /* | |
476 | * At this point, new metadata have been flushed, so we wait on the | |
477 | * rendez-vous point for the metadata thread to wake us up when it | |
478 | * finishes consuming the metadata and continue execution. | |
479 | */ | |
480 | ||
481 | pthread_mutex_lock(&metadata->metadata_rdv_lock); | |
482 | ||
483 | /* | |
484 | * Release metadata stream lock so the metadata thread can process it. | |
485 | */ | |
486 | pthread_mutex_unlock(&metadata->lock); | |
487 | ||
488 | /* | |
489 | * Wait on the rendez-vous point. Once woken up, it means the metadata was | |
490 | * consumed and thus synchronization is achieved. | |
491 | */ | |
492 | pthread_cond_wait(&metadata->metadata_rdv, &metadata->metadata_rdv_lock); | |
493 | pthread_mutex_unlock(&metadata->metadata_rdv_lock); | |
494 | } while (ret == EAGAIN); | |
495 | ||
e5ca40ee DG |
496 | /* Success */ |
497 | return 0; | |
94d49140 JD |
498 | |
499 | end_unlock_mutex: | |
500 | pthread_mutex_unlock(&metadata->lock); | |
e5ca40ee DG |
501 | return ret; |
502 | } | |
503 | ||
504 | /* | |
505 | * Synchronize the metadata using a given session ID. A successful acquisition | |
506 | * of a metadata stream will trigger a request to the session daemon and a | |
507 | * snapshot so the metadata thread can consume it. | |
508 | * | |
509 | * This function call is a rendez-vous point between the metadata thread and | |
510 | * the data thread. | |
511 | * | |
512 | * Return 0 on success or else a negative value. | |
513 | */ | |
514 | int consumer_stream_sync_metadata(struct lttng_consumer_local_data *ctx, | |
515 | uint64_t session_id) | |
516 | { | |
517 | int ret; | |
518 | struct lttng_consumer_stream *stream = NULL; | |
519 | struct lttng_ht_iter iter; | |
520 | struct lttng_ht *ht; | |
521 | ||
522 | assert(ctx); | |
523 | ||
524 | /* Ease our life a bit. */ | |
525 | ht = consumer_data.stream_list_ht; | |
526 | ||
527 | rcu_read_lock(); | |
528 | ||
529 | /* Search the metadata associated with the session id of the given stream. */ | |
530 | ||
531 | cds_lfht_for_each_entry_duplicate(ht->ht, | |
532 | ht->hash_fct(&session_id, lttng_ht_seed), ht->match_fct, | |
533 | &session_id, &iter.iter, stream, node_session_id.node) { | |
534 | if (!stream->metadata_flag) { | |
535 | continue; | |
536 | } | |
537 | ||
538 | ret = do_sync_metadata(stream, ctx); | |
539 | if (ret < 0) { | |
540 | goto end; | |
541 | } | |
542 | } | |
543 | ||
544 | /* | |
545 | * Force return code to 0 (success) since ret might be ENODATA for instance | |
546 | * which is not an error but rather that we should come back. | |
547 | */ | |
548 | ret = 0; | |
549 | ||
550 | end: | |
94d49140 JD |
551 | rcu_read_unlock(); |
552 | return ret; | |
553 | } |