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