Send indexes in streaming mode
[lttng-tools.git] / src / common / consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as 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
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <assert.h>
22 #include <poll.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <inttypes.h>
31 #include <signal.h>
32
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/index/index.h>
37 #include <common/kernel-ctl/kernel-ctl.h>
38 #include <common/sessiond-comm/relayd.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/kernel-consumer/kernel-consumer.h>
41 #include <common/relayd/relayd.h>
42 #include <common/ust-consumer/ust-consumer.h>
43
44 #include "consumer.h"
45 #include "consumer-stream.h"
46
47 struct lttng_consumer_global_data consumer_data = {
48 .stream_count = 0,
49 .need_update = 1,
50 .type = LTTNG_CONSUMER_UNKNOWN,
51 };
52
53 enum consumer_channel_action {
54 CONSUMER_CHANNEL_ADD,
55 CONSUMER_CHANNEL_DEL,
56 CONSUMER_CHANNEL_QUIT,
57 };
58
59 struct consumer_channel_msg {
60 enum consumer_channel_action action;
61 struct lttng_consumer_channel *chan; /* add */
62 uint64_t key; /* del */
63 };
64
65 /*
66 * Flag to inform the polling thread to quit when all fd hung up. Updated by
67 * the consumer_thread_receive_fds when it notices that all fds has hung up.
68 * Also updated by the signal handler (consumer_should_exit()). Read by the
69 * polling threads.
70 */
71 volatile int consumer_quit;
72
73 /*
74 * Global hash table containing respectively metadata and data streams. The
75 * stream element in this ht should only be updated by the metadata poll thread
76 * for the metadata and the data poll thread for the data.
77 */
78 static struct lttng_ht *metadata_ht;
79 static struct lttng_ht *data_ht;
80
81 /*
82 * Notify a thread lttng pipe to poll back again. This usually means that some
83 * global state has changed so we just send back the thread in a poll wait
84 * call.
85 */
86 static void notify_thread_lttng_pipe(struct lttng_pipe *pipe)
87 {
88 struct lttng_consumer_stream *null_stream = NULL;
89
90 assert(pipe);
91
92 (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream));
93 }
94
95 static void notify_channel_pipe(struct lttng_consumer_local_data *ctx,
96 struct lttng_consumer_channel *chan,
97 uint64_t key,
98 enum consumer_channel_action action)
99 {
100 struct consumer_channel_msg msg;
101 int ret;
102
103 memset(&msg, 0, sizeof(msg));
104
105 msg.action = action;
106 msg.chan = chan;
107 msg.key = key;
108 do {
109 ret = write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
110 } while (ret < 0 && errno == EINTR);
111 }
112
113 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
114 uint64_t key)
115 {
116 notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL);
117 }
118
119 static int read_channel_pipe(struct lttng_consumer_local_data *ctx,
120 struct lttng_consumer_channel **chan,
121 uint64_t *key,
122 enum consumer_channel_action *action)
123 {
124 struct consumer_channel_msg msg;
125 int ret;
126
127 do {
128 ret = read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
129 } while (ret < 0 && errno == EINTR);
130 if (ret > 0) {
131 *action = msg.action;
132 *chan = msg.chan;
133 *key = msg.key;
134 }
135 return ret;
136 }
137
138 /*
139 * Find a stream. The consumer_data.lock must be locked during this
140 * call.
141 */
142 static struct lttng_consumer_stream *find_stream(uint64_t key,
143 struct lttng_ht *ht)
144 {
145 struct lttng_ht_iter iter;
146 struct lttng_ht_node_u64 *node;
147 struct lttng_consumer_stream *stream = NULL;
148
149 assert(ht);
150
151 /* -1ULL keys are lookup failures */
152 if (key == (uint64_t) -1ULL) {
153 return NULL;
154 }
155
156 rcu_read_lock();
157
158 lttng_ht_lookup(ht, &key, &iter);
159 node = lttng_ht_iter_get_node_u64(&iter);
160 if (node != NULL) {
161 stream = caa_container_of(node, struct lttng_consumer_stream, node);
162 }
163
164 rcu_read_unlock();
165
166 return stream;
167 }
168
169 static void steal_stream_key(uint64_t key, struct lttng_ht *ht)
170 {
171 struct lttng_consumer_stream *stream;
172
173 rcu_read_lock();
174 stream = find_stream(key, ht);
175 if (stream) {
176 stream->key = (uint64_t) -1ULL;
177 /*
178 * We don't want the lookup to match, but we still need
179 * to iterate on this stream when iterating over the hash table. Just
180 * change the node key.
181 */
182 stream->node.key = (uint64_t) -1ULL;
183 }
184 rcu_read_unlock();
185 }
186
187 /*
188 * Return a channel object for the given key.
189 *
190 * RCU read side lock MUST be acquired before calling this function and
191 * protects the channel ptr.
192 */
193 struct lttng_consumer_channel *consumer_find_channel(uint64_t key)
194 {
195 struct lttng_ht_iter iter;
196 struct lttng_ht_node_u64 *node;
197 struct lttng_consumer_channel *channel = NULL;
198
199 /* -1ULL keys are lookup failures */
200 if (key == (uint64_t) -1ULL) {
201 return NULL;
202 }
203
204 lttng_ht_lookup(consumer_data.channel_ht, &key, &iter);
205 node = lttng_ht_iter_get_node_u64(&iter);
206 if (node != NULL) {
207 channel = caa_container_of(node, struct lttng_consumer_channel, node);
208 }
209
210 return channel;
211 }
212
213 static void free_stream_rcu(struct rcu_head *head)
214 {
215 struct lttng_ht_node_u64 *node =
216 caa_container_of(head, struct lttng_ht_node_u64, head);
217 struct lttng_consumer_stream *stream =
218 caa_container_of(node, struct lttng_consumer_stream, node);
219
220 free(stream);
221 }
222
223 static void free_channel_rcu(struct rcu_head *head)
224 {
225 struct lttng_ht_node_u64 *node =
226 caa_container_of(head, struct lttng_ht_node_u64, head);
227 struct lttng_consumer_channel *channel =
228 caa_container_of(node, struct lttng_consumer_channel, node);
229
230 free(channel);
231 }
232
233 /*
234 * RCU protected relayd socket pair free.
235 */
236 static void free_relayd_rcu(struct rcu_head *head)
237 {
238 struct lttng_ht_node_u64 *node =
239 caa_container_of(head, struct lttng_ht_node_u64, head);
240 struct consumer_relayd_sock_pair *relayd =
241 caa_container_of(node, struct consumer_relayd_sock_pair, node);
242
243 /*
244 * Close all sockets. This is done in the call RCU since we don't want the
245 * socket fds to be reassigned thus potentially creating bad state of the
246 * relayd object.
247 *
248 * We do not have to lock the control socket mutex here since at this stage
249 * there is no one referencing to this relayd object.
250 */
251 (void) relayd_close(&relayd->control_sock);
252 (void) relayd_close(&relayd->data_sock);
253
254 free(relayd);
255 }
256
257 /*
258 * Destroy and free relayd socket pair object.
259 */
260 void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
261 {
262 int ret;
263 struct lttng_ht_iter iter;
264
265 if (relayd == NULL) {
266 return;
267 }
268
269 DBG("Consumer destroy and close relayd socket pair");
270
271 iter.iter.node = &relayd->node.node;
272 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
273 if (ret != 0) {
274 /* We assume the relayd is being or is destroyed */
275 return;
276 }
277
278 /* RCU free() call */
279 call_rcu(&relayd->node.head, free_relayd_rcu);
280 }
281
282 /*
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
285 */
286 void consumer_del_channel(struct lttng_consumer_channel *channel)
287 {
288 int ret;
289 struct lttng_ht_iter iter;
290 struct lttng_consumer_stream *stream, *stmp;
291
292 DBG("Consumer delete channel key %" PRIu64, channel->key);
293
294 pthread_mutex_lock(&consumer_data.lock);
295 pthread_mutex_lock(&channel->lock);
296
297 /* Delete streams that might have been left in the stream list. */
298 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
299 send_node) {
300 cds_list_del(&stream->send_node);
301 /*
302 * Once a stream is added to this list, the buffers were created so
303 * we have a guarantee that this call will succeed.
304 */
305 consumer_stream_destroy(stream, NULL);
306 }
307
308 switch (consumer_data.type) {
309 case LTTNG_CONSUMER_KERNEL:
310 break;
311 case LTTNG_CONSUMER32_UST:
312 case LTTNG_CONSUMER64_UST:
313 lttng_ustconsumer_del_channel(channel);
314 break;
315 default:
316 ERR("Unknown consumer_data type");
317 assert(0);
318 goto end;
319 }
320
321 rcu_read_lock();
322 iter.iter.node = &channel->node.node;
323 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
324 assert(!ret);
325 rcu_read_unlock();
326
327 call_rcu(&channel->node.head, free_channel_rcu);
328 end:
329 pthread_mutex_unlock(&channel->lock);
330 pthread_mutex_unlock(&consumer_data.lock);
331 }
332
333 /*
334 * Iterate over the relayd hash table and destroy each element. Finally,
335 * destroy the whole hash table.
336 */
337 static void cleanup_relayd_ht(void)
338 {
339 struct lttng_ht_iter iter;
340 struct consumer_relayd_sock_pair *relayd;
341
342 rcu_read_lock();
343
344 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
345 node.node) {
346 consumer_destroy_relayd(relayd);
347 }
348
349 rcu_read_unlock();
350
351 lttng_ht_destroy(consumer_data.relayd_ht);
352 }
353
354 /*
355 * Update the end point status of all streams having the given network sequence
356 * index (relayd index).
357 *
358 * It's atomically set without having the stream mutex locked which is fine
359 * because we handle the write/read race with a pipe wakeup for each thread.
360 */
361 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx,
362 enum consumer_endpoint_status status)
363 {
364 struct lttng_ht_iter iter;
365 struct lttng_consumer_stream *stream;
366
367 DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx);
368
369 rcu_read_lock();
370
371 /* Let's begin with metadata */
372 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
373 if (stream->net_seq_idx == net_seq_idx) {
374 uatomic_set(&stream->endpoint_status, status);
375 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
376 }
377 }
378
379 /* Follow up by the data streams */
380 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
381 if (stream->net_seq_idx == net_seq_idx) {
382 uatomic_set(&stream->endpoint_status, status);
383 DBG("Delete flag set to data stream %d", stream->wait_fd);
384 }
385 }
386 rcu_read_unlock();
387 }
388
389 /*
390 * Cleanup a relayd object by flagging every associated streams for deletion,
391 * destroying the object meaning removing it from the relayd hash table,
392 * closing the sockets and freeing the memory in a RCU call.
393 *
394 * If a local data context is available, notify the threads that the streams'
395 * state have changed.
396 */
397 static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd,
398 struct lttng_consumer_local_data *ctx)
399 {
400 uint64_t netidx;
401
402 assert(relayd);
403
404 DBG("Cleaning up relayd sockets");
405
406 /* Save the net sequence index before destroying the object */
407 netidx = relayd->net_seq_idx;
408
409 /*
410 * Delete the relayd from the relayd hash table, close the sockets and free
411 * the object in a RCU call.
412 */
413 consumer_destroy_relayd(relayd);
414
415 /* Set inactive endpoint to all streams */
416 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
417
418 /*
419 * With a local data context, notify the threads that the streams' state
420 * have changed. The write() action on the pipe acts as an "implicit"
421 * memory barrier ordering the updates of the end point status from the
422 * read of this status which happens AFTER receiving this notify.
423 */
424 if (ctx) {
425 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
426 notify_thread_lttng_pipe(ctx->consumer_metadata_pipe);
427 }
428 }
429
430 /*
431 * Flag a relayd socket pair for destruction. Destroy it if the refcount
432 * reaches zero.
433 *
434 * RCU read side lock MUST be aquired before calling this function.
435 */
436 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
437 {
438 assert(relayd);
439
440 /* Set destroy flag for this object */
441 uatomic_set(&relayd->destroy_flag, 1);
442
443 /* Destroy the relayd if refcount is 0 */
444 if (uatomic_read(&relayd->refcount) == 0) {
445 consumer_destroy_relayd(relayd);
446 }
447 }
448
449 /*
450 * Completly destroy stream from every visiable data structure and the given
451 * hash table if one.
452 *
453 * One this call returns, the stream object is not longer usable nor visible.
454 */
455 void consumer_del_stream(struct lttng_consumer_stream *stream,
456 struct lttng_ht *ht)
457 {
458 consumer_stream_destroy(stream, ht);
459 }
460
461 /*
462 * XXX naming of del vs destroy is all mixed up.
463 */
464 void consumer_del_stream_for_data(struct lttng_consumer_stream *stream)
465 {
466 consumer_stream_destroy(stream, data_ht);
467 }
468
469 void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream)
470 {
471 consumer_stream_destroy(stream, metadata_ht);
472 }
473
474 struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key,
475 uint64_t stream_key,
476 enum lttng_consumer_stream_state state,
477 const char *channel_name,
478 uid_t uid,
479 gid_t gid,
480 uint64_t relayd_id,
481 uint64_t session_id,
482 int cpu,
483 int *alloc_ret,
484 enum consumer_channel_type type,
485 unsigned int monitor)
486 {
487 int ret;
488 struct lttng_consumer_stream *stream;
489
490 stream = zmalloc(sizeof(*stream));
491 if (stream == NULL) {
492 PERROR("malloc struct lttng_consumer_stream");
493 ret = -ENOMEM;
494 goto end;
495 }
496
497 rcu_read_lock();
498
499 stream->key = stream_key;
500 stream->out_fd = -1;
501 stream->out_fd_offset = 0;
502 stream->output_written = 0;
503 stream->state = state;
504 stream->uid = uid;
505 stream->gid = gid;
506 stream->net_seq_idx = relayd_id;
507 stream->session_id = session_id;
508 stream->monitor = monitor;
509 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
510 stream->index_fd = -1;
511 pthread_mutex_init(&stream->lock, NULL);
512
513 /* If channel is the metadata, flag this stream as metadata. */
514 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
515 stream->metadata_flag = 1;
516 /* Metadata is flat out. */
517 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
518 } else {
519 /* Format stream name to <channel_name>_<cpu_number> */
520 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
521 channel_name, cpu);
522 if (ret < 0) {
523 PERROR("snprintf stream name");
524 goto error;
525 }
526 }
527
528 /* Key is always the wait_fd for streams. */
529 lttng_ht_node_init_u64(&stream->node, stream->key);
530
531 /* Init node per channel id key */
532 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
533
534 /* Init session id node with the stream session id */
535 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
536
537 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64
538 " relayd_id %" PRIu64 ", session_id %" PRIu64,
539 stream->name, stream->key, channel_key,
540 stream->net_seq_idx, stream->session_id);
541
542 rcu_read_unlock();
543 return stream;
544
545 error:
546 rcu_read_unlock();
547 free(stream);
548 end:
549 if (alloc_ret) {
550 *alloc_ret = ret;
551 }
552 return NULL;
553 }
554
555 /*
556 * Add a stream to the global list protected by a mutex.
557 */
558 int consumer_add_data_stream(struct lttng_consumer_stream *stream)
559 {
560 struct lttng_ht *ht = data_ht;
561 int ret = 0;
562
563 assert(stream);
564 assert(ht);
565
566 DBG3("Adding consumer stream %" PRIu64, stream->key);
567
568 pthread_mutex_lock(&consumer_data.lock);
569 pthread_mutex_lock(&stream->chan->lock);
570 pthread_mutex_lock(&stream->chan->timer_lock);
571 pthread_mutex_lock(&stream->lock);
572 rcu_read_lock();
573
574 /* Steal stream identifier to avoid having streams with the same key */
575 steal_stream_key(stream->key, ht);
576
577 lttng_ht_add_unique_u64(ht, &stream->node);
578
579 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
580 &stream->node_channel_id);
581
582 /*
583 * Add stream to the stream_list_ht of the consumer data. No need to steal
584 * the key since the HT does not use it and we allow to add redundant keys
585 * into this table.
586 */
587 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
588
589 /*
590 * When nb_init_stream_left reaches 0, we don't need to trigger any action
591 * in terms of destroying the associated channel, because the action that
592 * causes the count to become 0 also causes a stream to be added. The
593 * channel deletion will thus be triggered by the following removal of this
594 * stream.
595 */
596 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
597 /* Increment refcount before decrementing nb_init_stream_left */
598 cmm_smp_wmb();
599 uatomic_dec(&stream->chan->nb_init_stream_left);
600 }
601
602 /* Update consumer data once the node is inserted. */
603 consumer_data.stream_count++;
604 consumer_data.need_update = 1;
605
606 rcu_read_unlock();
607 pthread_mutex_unlock(&stream->lock);
608 pthread_mutex_unlock(&stream->chan->timer_lock);
609 pthread_mutex_unlock(&stream->chan->lock);
610 pthread_mutex_unlock(&consumer_data.lock);
611
612 return ret;
613 }
614
615 void consumer_del_data_stream(struct lttng_consumer_stream *stream)
616 {
617 consumer_del_stream(stream, data_ht);
618 }
619
620 /*
621 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
622 * be acquired before calling this.
623 */
624 static int add_relayd(struct consumer_relayd_sock_pair *relayd)
625 {
626 int ret = 0;
627 struct lttng_ht_node_u64 *node;
628 struct lttng_ht_iter iter;
629
630 assert(relayd);
631
632 lttng_ht_lookup(consumer_data.relayd_ht,
633 &relayd->net_seq_idx, &iter);
634 node = lttng_ht_iter_get_node_u64(&iter);
635 if (node != NULL) {
636 goto end;
637 }
638 lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node);
639
640 end:
641 return ret;
642 }
643
644 /*
645 * Allocate and return a consumer relayd socket.
646 */
647 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
648 uint64_t net_seq_idx)
649 {
650 struct consumer_relayd_sock_pair *obj = NULL;
651
652 /* net sequence index of -1 is a failure */
653 if (net_seq_idx == (uint64_t) -1ULL) {
654 goto error;
655 }
656
657 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
658 if (obj == NULL) {
659 PERROR("zmalloc relayd sock");
660 goto error;
661 }
662
663 obj->net_seq_idx = net_seq_idx;
664 obj->refcount = 0;
665 obj->destroy_flag = 0;
666 obj->control_sock.sock.fd = -1;
667 obj->data_sock.sock.fd = -1;
668 lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx);
669 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
670
671 error:
672 return obj;
673 }
674
675 /*
676 * Find a relayd socket pair in the global consumer data.
677 *
678 * Return the object if found else NULL.
679 * RCU read-side lock must be held across this call and while using the
680 * returned object.
681 */
682 struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key)
683 {
684 struct lttng_ht_iter iter;
685 struct lttng_ht_node_u64 *node;
686 struct consumer_relayd_sock_pair *relayd = NULL;
687
688 /* Negative keys are lookup failures */
689 if (key == (uint64_t) -1ULL) {
690 goto error;
691 }
692
693 lttng_ht_lookup(consumer_data.relayd_ht, &key,
694 &iter);
695 node = lttng_ht_iter_get_node_u64(&iter);
696 if (node != NULL) {
697 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
698 }
699
700 error:
701 return relayd;
702 }
703
704 /*
705 * Find a relayd and send the stream
706 *
707 * Returns 0 on success, < 0 on error
708 */
709 int consumer_send_relayd_stream(struct lttng_consumer_stream *stream,
710 char *path)
711 {
712 int ret = 0;
713 struct consumer_relayd_sock_pair *relayd;
714
715 assert(stream);
716 assert(stream->net_seq_idx != -1ULL);
717 assert(path);
718
719 /* The stream is not metadata. Get relayd reference if exists. */
720 rcu_read_lock();
721 relayd = consumer_find_relayd(stream->net_seq_idx);
722 if (relayd != NULL) {
723 /* Add stream on the relayd */
724 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
725 ret = relayd_add_stream(&relayd->control_sock, stream->name,
726 path, &stream->relayd_stream_id,
727 stream->chan->tracefile_size, stream->chan->tracefile_count);
728 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
729 if (ret < 0) {
730 goto end;
731 }
732
733 uatomic_inc(&relayd->refcount);
734 stream->sent_to_relayd = 1;
735 } else {
736 ERR("Stream %" PRIu64 " relayd ID %" PRIu64 " unknown. Can't send it.",
737 stream->key, stream->net_seq_idx);
738 ret = -1;
739 goto end;
740 }
741
742 DBG("Stream %s with key %" PRIu64 " sent to relayd id %" PRIu64,
743 stream->name, stream->key, stream->net_seq_idx);
744
745 end:
746 rcu_read_unlock();
747 return ret;
748 }
749
750 /*
751 * Find a relayd and close the stream
752 */
753 void close_relayd_stream(struct lttng_consumer_stream *stream)
754 {
755 struct consumer_relayd_sock_pair *relayd;
756
757 /* The stream is not metadata. Get relayd reference if exists. */
758 rcu_read_lock();
759 relayd = consumer_find_relayd(stream->net_seq_idx);
760 if (relayd) {
761 consumer_stream_relayd_close(stream, relayd);
762 }
763 rcu_read_unlock();
764 }
765
766 /*
767 * Handle stream for relayd transmission if the stream applies for network
768 * streaming where the net sequence index is set.
769 *
770 * Return destination file descriptor or negative value on error.
771 */
772 static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
773 size_t data_size, unsigned long padding,
774 struct consumer_relayd_sock_pair *relayd)
775 {
776 int outfd = -1, ret;
777 struct lttcomm_relayd_data_hdr data_hdr;
778
779 /* Safety net */
780 assert(stream);
781 assert(relayd);
782
783 /* Reset data header */
784 memset(&data_hdr, 0, sizeof(data_hdr));
785
786 if (stream->metadata_flag) {
787 /* Caller MUST acquire the relayd control socket lock */
788 ret = relayd_send_metadata(&relayd->control_sock, data_size);
789 if (ret < 0) {
790 goto error;
791 }
792
793 /* Metadata are always sent on the control socket. */
794 outfd = relayd->control_sock.sock.fd;
795 } else {
796 /* Set header with stream information */
797 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
798 data_hdr.data_size = htobe32(data_size);
799 data_hdr.padding_size = htobe32(padding);
800 /*
801 * Note that net_seq_num below is assigned with the *current* value of
802 * next_net_seq_num and only after that the next_net_seq_num will be
803 * increment. This is why when issuing a command on the relayd using
804 * this next value, 1 should always be substracted in order to compare
805 * the last seen sequence number on the relayd side to the last sent.
806 */
807 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num);
808 /* Other fields are zeroed previously */
809
810 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
811 sizeof(data_hdr));
812 if (ret < 0) {
813 goto error;
814 }
815
816 ++stream->next_net_seq_num;
817
818 /* Set to go on data socket */
819 outfd = relayd->data_sock.sock.fd;
820 }
821
822 error:
823 return outfd;
824 }
825
826 /*
827 * Allocate and return a new lttng_consumer_channel object using the given key
828 * to initialize the hash table node.
829 *
830 * On error, return NULL.
831 */
832 struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
833 uint64_t session_id,
834 const char *pathname,
835 const char *name,
836 uid_t uid,
837 gid_t gid,
838 uint64_t relayd_id,
839 enum lttng_event_output output,
840 uint64_t tracefile_size,
841 uint64_t tracefile_count,
842 uint64_t session_id_per_pid,
843 unsigned int monitor,
844 unsigned int live_timer_interval)
845 {
846 struct lttng_consumer_channel *channel;
847
848 channel = zmalloc(sizeof(*channel));
849 if (channel == NULL) {
850 PERROR("malloc struct lttng_consumer_channel");
851 goto end;
852 }
853
854 channel->key = key;
855 channel->refcount = 0;
856 channel->session_id = session_id;
857 channel->session_id_per_pid = session_id_per_pid;
858 channel->uid = uid;
859 channel->gid = gid;
860 channel->relayd_id = relayd_id;
861 channel->output = output;
862 channel->tracefile_size = tracefile_size;
863 channel->tracefile_count = tracefile_count;
864 channel->monitor = monitor;
865 channel->live_timer_interval = live_timer_interval;
866 pthread_mutex_init(&channel->lock, NULL);
867 pthread_mutex_init(&channel->timer_lock, NULL);
868
869 /*
870 * In monitor mode, the streams associated with the channel will be put in
871 * a special list ONLY owned by this channel. So, the refcount is set to 1
872 * here meaning that the channel itself has streams that are referenced.
873 *
874 * On a channel deletion, once the channel is no longer visible, the
875 * refcount is decremented and checked for a zero value to delete it. With
876 * streams in no monitor mode, it will now be safe to destroy the channel.
877 */
878 if (!channel->monitor) {
879 channel->refcount = 1;
880 }
881
882 strncpy(channel->pathname, pathname, sizeof(channel->pathname));
883 channel->pathname[sizeof(channel->pathname) - 1] = '\0';
884
885 strncpy(channel->name, name, sizeof(channel->name));
886 channel->name[sizeof(channel->name) - 1] = '\0';
887
888 lttng_ht_node_init_u64(&channel->node, channel->key);
889
890 channel->wait_fd = -1;
891
892 CDS_INIT_LIST_HEAD(&channel->streams.head);
893
894 DBG("Allocated channel (key %" PRIu64 ")", channel->key)
895
896 end:
897 return channel;
898 }
899
900 /*
901 * Add a channel to the global list protected by a mutex.
902 *
903 * On success 0 is returned else a negative value.
904 */
905 int consumer_add_channel(struct lttng_consumer_channel *channel,
906 struct lttng_consumer_local_data *ctx)
907 {
908 int ret = 0;
909 struct lttng_ht_node_u64 *node;
910 struct lttng_ht_iter iter;
911
912 pthread_mutex_lock(&consumer_data.lock);
913 pthread_mutex_lock(&channel->lock);
914 pthread_mutex_lock(&channel->timer_lock);
915 rcu_read_lock();
916
917 lttng_ht_lookup(consumer_data.channel_ht, &channel->key, &iter);
918 node = lttng_ht_iter_get_node_u64(&iter);
919 if (node != NULL) {
920 /* Channel already exist. Ignore the insertion */
921 ERR("Consumer add channel key %" PRIu64 " already exists!",
922 channel->key);
923 ret = -EEXIST;
924 goto end;
925 }
926
927 lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node);
928
929 end:
930 rcu_read_unlock();
931 pthread_mutex_unlock(&channel->timer_lock);
932 pthread_mutex_unlock(&channel->lock);
933 pthread_mutex_unlock(&consumer_data.lock);
934
935 if (!ret && channel->wait_fd != -1 &&
936 channel->type == CONSUMER_CHANNEL_TYPE_DATA) {
937 notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD);
938 }
939 return ret;
940 }
941
942 /*
943 * Allocate the pollfd structure and the local view of the out fds to avoid
944 * doing a lookup in the linked list and concurrency issues when writing is
945 * needed. Called with consumer_data.lock held.
946 *
947 * Returns the number of fds in the structures.
948 */
949 static int update_poll_array(struct lttng_consumer_local_data *ctx,
950 struct pollfd **pollfd, struct lttng_consumer_stream **local_stream,
951 struct lttng_ht *ht)
952 {
953 int i = 0;
954 struct lttng_ht_iter iter;
955 struct lttng_consumer_stream *stream;
956
957 assert(ctx);
958 assert(ht);
959 assert(pollfd);
960 assert(local_stream);
961
962 DBG("Updating poll fd array");
963 rcu_read_lock();
964 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
965 /*
966 * Only active streams with an active end point can be added to the
967 * poll set and local stream storage of the thread.
968 *
969 * There is a potential race here for endpoint_status to be updated
970 * just after the check. However, this is OK since the stream(s) will
971 * be deleted once the thread is notified that the end point state has
972 * changed where this function will be called back again.
973 */
974 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
975 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
976 continue;
977 }
978 /*
979 * This clobbers way too much the debug output. Uncomment that if you
980 * need it for debugging purposes.
981 *
982 * DBG("Active FD %d", stream->wait_fd);
983 */
984 (*pollfd)[i].fd = stream->wait_fd;
985 (*pollfd)[i].events = POLLIN | POLLPRI;
986 local_stream[i] = stream;
987 i++;
988 }
989 rcu_read_unlock();
990
991 /*
992 * Insert the consumer_data_pipe at the end of the array and don't
993 * increment i so nb_fd is the number of real FD.
994 */
995 (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe);
996 (*pollfd)[i].events = POLLIN | POLLPRI;
997 return i;
998 }
999
1000 /*
1001 * Poll on the should_quit pipe and the command socket return -1 on error and
1002 * should exit, 0 if data is available on the command socket
1003 */
1004 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1005 {
1006 int num_rdy;
1007
1008 restart:
1009 num_rdy = poll(consumer_sockpoll, 2, -1);
1010 if (num_rdy == -1) {
1011 /*
1012 * Restart interrupted system call.
1013 */
1014 if (errno == EINTR) {
1015 goto restart;
1016 }
1017 PERROR("Poll error");
1018 goto exit;
1019 }
1020 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
1021 DBG("consumer_should_quit wake up");
1022 goto exit;
1023 }
1024 return 0;
1025
1026 exit:
1027 return -1;
1028 }
1029
1030 /*
1031 * Set the error socket.
1032 */
1033 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
1034 int sock)
1035 {
1036 ctx->consumer_error_socket = sock;
1037 }
1038
1039 /*
1040 * Set the command socket path.
1041 */
1042 void lttng_consumer_set_command_sock_path(
1043 struct lttng_consumer_local_data *ctx, char *sock)
1044 {
1045 ctx->consumer_command_sock_path = sock;
1046 }
1047
1048 /*
1049 * Send return code to the session daemon.
1050 * If the socket is not defined, we return 0, it is not a fatal error
1051 */
1052 int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd)
1053 {
1054 if (ctx->consumer_error_socket > 0) {
1055 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1056 sizeof(enum lttcomm_sessiond_command));
1057 }
1058
1059 return 0;
1060 }
1061
1062 /*
1063 * Close all the tracefiles and stream fds and MUST be called when all
1064 * instances are destroyed i.e. when all threads were joined and are ended.
1065 */
1066 void lttng_consumer_cleanup(void)
1067 {
1068 struct lttng_ht_iter iter;
1069 struct lttng_consumer_channel *channel;
1070
1071 rcu_read_lock();
1072
1073 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel,
1074 node.node) {
1075 consumer_del_channel(channel);
1076 }
1077
1078 rcu_read_unlock();
1079
1080 lttng_ht_destroy(consumer_data.channel_ht);
1081
1082 cleanup_relayd_ht();
1083
1084 lttng_ht_destroy(consumer_data.stream_per_chan_id_ht);
1085
1086 /*
1087 * This HT contains streams that are freed by either the metadata thread or
1088 * the data thread so we do *nothing* on the hash table and simply destroy
1089 * it.
1090 */
1091 lttng_ht_destroy(consumer_data.stream_list_ht);
1092 }
1093
1094 /*
1095 * Called from signal handler.
1096 */
1097 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1098 {
1099 int ret;
1100 consumer_quit = 1;
1101 do {
1102 ret = write(ctx->consumer_should_quit[1], "4", 1);
1103 } while (ret < 0 && errno == EINTR);
1104 if (ret < 0 || ret != 1) {
1105 PERROR("write consumer quit");
1106 }
1107
1108 DBG("Consumer flag that it should quit");
1109 }
1110
1111 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1112 off_t orig_offset)
1113 {
1114 int outfd = stream->out_fd;
1115
1116 /*
1117 * This does a blocking write-and-wait on any page that belongs to the
1118 * subbuffer prior to the one we just wrote.
1119 * Don't care about error values, as these are just hints and ways to
1120 * limit the amount of page cache used.
1121 */
1122 if (orig_offset < stream->max_sb_size) {
1123 return;
1124 }
1125 lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size,
1126 stream->max_sb_size,
1127 SYNC_FILE_RANGE_WAIT_BEFORE
1128 | SYNC_FILE_RANGE_WRITE
1129 | SYNC_FILE_RANGE_WAIT_AFTER);
1130 /*
1131 * Give hints to the kernel about how we access the file:
1132 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1133 * we write it.
1134 *
1135 * We need to call fadvise again after the file grows because the
1136 * kernel does not seem to apply fadvise to non-existing parts of the
1137 * file.
1138 *
1139 * Call fadvise _after_ having waited for the page writeback to
1140 * complete because the dirty page writeback semantic is not well
1141 * defined. So it can be expected to lead to lower throughput in
1142 * streaming.
1143 */
1144 posix_fadvise(outfd, orig_offset - stream->max_sb_size,
1145 stream->max_sb_size, POSIX_FADV_DONTNEED);
1146 }
1147
1148 /*
1149 * Initialise the necessary environnement :
1150 * - create a new context
1151 * - create the poll_pipe
1152 * - create the should_quit pipe (for signal handler)
1153 * - create the thread pipe (for splice)
1154 *
1155 * Takes a function pointer as argument, this function is called when data is
1156 * available on a buffer. This function is responsible to do the
1157 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1158 * buffer configuration and then kernctl_put_next_subbuf at the end.
1159 *
1160 * Returns a pointer to the new context or NULL on error.
1161 */
1162 struct lttng_consumer_local_data *lttng_consumer_create(
1163 enum lttng_consumer_type type,
1164 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
1165 struct lttng_consumer_local_data *ctx),
1166 int (*recv_channel)(struct lttng_consumer_channel *channel),
1167 int (*recv_stream)(struct lttng_consumer_stream *stream),
1168 int (*update_stream)(uint64_t stream_key, uint32_t state))
1169 {
1170 int ret;
1171 struct lttng_consumer_local_data *ctx;
1172
1173 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1174 consumer_data.type == type);
1175 consumer_data.type = type;
1176
1177 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
1178 if (ctx == NULL) {
1179 PERROR("allocating context");
1180 goto error;
1181 }
1182
1183 ctx->consumer_error_socket = -1;
1184 ctx->consumer_metadata_socket = -1;
1185 pthread_mutex_init(&ctx->metadata_socket_lock, NULL);
1186 /* assign the callbacks */
1187 ctx->on_buffer_ready = buffer_ready;
1188 ctx->on_recv_channel = recv_channel;
1189 ctx->on_recv_stream = recv_stream;
1190 ctx->on_update_stream = update_stream;
1191
1192 ctx->consumer_data_pipe = lttng_pipe_open(0);
1193 if (!ctx->consumer_data_pipe) {
1194 goto error_poll_pipe;
1195 }
1196
1197 ret = pipe(ctx->consumer_should_quit);
1198 if (ret < 0) {
1199 PERROR("Error creating recv pipe");
1200 goto error_quit_pipe;
1201 }
1202
1203 ret = pipe(ctx->consumer_thread_pipe);
1204 if (ret < 0) {
1205 PERROR("Error creating thread pipe");
1206 goto error_thread_pipe;
1207 }
1208
1209 ret = pipe(ctx->consumer_channel_pipe);
1210 if (ret < 0) {
1211 PERROR("Error creating channel pipe");
1212 goto error_channel_pipe;
1213 }
1214
1215 ctx->consumer_metadata_pipe = lttng_pipe_open(0);
1216 if (!ctx->consumer_metadata_pipe) {
1217 goto error_metadata_pipe;
1218 }
1219
1220 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1221 if (ret < 0) {
1222 goto error_splice_pipe;
1223 }
1224
1225 return ctx;
1226
1227 error_splice_pipe:
1228 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1229 error_metadata_pipe:
1230 utils_close_pipe(ctx->consumer_channel_pipe);
1231 error_channel_pipe:
1232 utils_close_pipe(ctx->consumer_thread_pipe);
1233 error_thread_pipe:
1234 utils_close_pipe(ctx->consumer_should_quit);
1235 error_quit_pipe:
1236 lttng_pipe_destroy(ctx->consumer_data_pipe);
1237 error_poll_pipe:
1238 free(ctx);
1239 error:
1240 return NULL;
1241 }
1242
1243 /*
1244 * Close all fds associated with the instance and free the context.
1245 */
1246 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1247 {
1248 int ret;
1249
1250 DBG("Consumer destroying it. Closing everything.");
1251
1252 ret = close(ctx->consumer_error_socket);
1253 if (ret) {
1254 PERROR("close");
1255 }
1256 ret = close(ctx->consumer_metadata_socket);
1257 if (ret) {
1258 PERROR("close");
1259 }
1260 utils_close_pipe(ctx->consumer_thread_pipe);
1261 utils_close_pipe(ctx->consumer_channel_pipe);
1262 lttng_pipe_destroy(ctx->consumer_data_pipe);
1263 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1264 utils_close_pipe(ctx->consumer_should_quit);
1265 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1266
1267 unlink(ctx->consumer_command_sock_path);
1268 free(ctx);
1269 }
1270
1271 /*
1272 * Write the metadata stream id on the specified file descriptor.
1273 */
1274 static int write_relayd_metadata_id(int fd,
1275 struct lttng_consumer_stream *stream,
1276 struct consumer_relayd_sock_pair *relayd, unsigned long padding)
1277 {
1278 int ret;
1279 struct lttcomm_relayd_metadata_payload hdr;
1280
1281 hdr.stream_id = htobe64(stream->relayd_stream_id);
1282 hdr.padding_size = htobe32(padding);
1283 do {
1284 ret = write(fd, (void *) &hdr, sizeof(hdr));
1285 } while (ret < 0 && errno == EINTR);
1286 if (ret < 0 || ret != sizeof(hdr)) {
1287 /*
1288 * This error means that the fd's end is closed so ignore the perror
1289 * not to clubber the error output since this can happen in a normal
1290 * code path.
1291 */
1292 if (errno != EPIPE) {
1293 PERROR("write metadata stream id");
1294 }
1295 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
1296 /*
1297 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1298 * handle writting the missing part so report that as an error and
1299 * don't lie to the caller.
1300 */
1301 ret = -1;
1302 goto end;
1303 }
1304 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1305 stream->relayd_stream_id, padding);
1306
1307 end:
1308 return ret;
1309 }
1310
1311 /*
1312 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1313 * core function for writing trace buffers to either the local filesystem or
1314 * the network.
1315 *
1316 * It must be called with the stream lock held.
1317 *
1318 * Careful review MUST be put if any changes occur!
1319 *
1320 * Returns the number of bytes written
1321 */
1322 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1323 struct lttng_consumer_local_data *ctx,
1324 struct lttng_consumer_stream *stream, unsigned long len,
1325 unsigned long padding,
1326 struct lttng_packet_index *index)
1327 {
1328 unsigned long mmap_offset;
1329 void *mmap_base;
1330 ssize_t ret = 0, written = 0;
1331 off_t orig_offset = stream->out_fd_offset;
1332 /* Default is on the disk */
1333 int outfd = stream->out_fd;
1334 struct consumer_relayd_sock_pair *relayd = NULL;
1335 unsigned int relayd_hang_up = 0;
1336
1337 /* RCU lock for the relayd pointer */
1338 rcu_read_lock();
1339
1340 /* Flag that the current stream if set for network streaming. */
1341 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1342 relayd = consumer_find_relayd(stream->net_seq_idx);
1343 if (relayd == NULL) {
1344 ret = -EPIPE;
1345 goto end;
1346 }
1347 }
1348
1349 /* get the offset inside the fd to mmap */
1350 switch (consumer_data.type) {
1351 case LTTNG_CONSUMER_KERNEL:
1352 mmap_base = stream->mmap_base;
1353 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1354 if (ret != 0) {
1355 PERROR("tracer ctl get_mmap_read_offset");
1356 written = -errno;
1357 goto end;
1358 }
1359 break;
1360 case LTTNG_CONSUMER32_UST:
1361 case LTTNG_CONSUMER64_UST:
1362 mmap_base = lttng_ustctl_get_mmap_base(stream);
1363 if (!mmap_base) {
1364 ERR("read mmap get mmap base for stream %s", stream->name);
1365 written = -EPERM;
1366 goto end;
1367 }
1368 ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset);
1369 if (ret != 0) {
1370 PERROR("tracer ctl get_mmap_read_offset");
1371 written = ret;
1372 goto end;
1373 }
1374 break;
1375 default:
1376 ERR("Unknown consumer_data type");
1377 assert(0);
1378 }
1379
1380 /* Handle stream on the relayd if the output is on the network */
1381 if (relayd) {
1382 unsigned long netlen = len;
1383
1384 /*
1385 * Lock the control socket for the complete duration of the function
1386 * since from this point on we will use the socket.
1387 */
1388 if (stream->metadata_flag) {
1389 /* Metadata requires the control socket. */
1390 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1391 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
1392 }
1393
1394 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
1395 if (ret >= 0) {
1396 /* Use the returned socket. */
1397 outfd = ret;
1398
1399 /* Write metadata stream id before payload */
1400 if (stream->metadata_flag) {
1401 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
1402 if (ret < 0) {
1403 written = ret;
1404 /* Socket operation failed. We consider the relayd dead */
1405 if (ret == -EPIPE || ret == -EINVAL) {
1406 relayd_hang_up = 1;
1407 goto write_error;
1408 }
1409 goto end;
1410 }
1411 }
1412 } else {
1413 /* Socket operation failed. We consider the relayd dead */
1414 if (ret == -EPIPE || ret == -EINVAL) {
1415 relayd_hang_up = 1;
1416 goto write_error;
1417 }
1418 /* Else, use the default set before which is the filesystem. */
1419 }
1420 } else {
1421 /* No streaming, we have to set the len with the full padding */
1422 len += padding;
1423
1424 /*
1425 * Check if we need to change the tracefile before writing the packet.
1426 */
1427 if (stream->chan->tracefile_size > 0 &&
1428 (stream->tracefile_size_current + len) >
1429 stream->chan->tracefile_size) {
1430 ret = utils_rotate_stream_file(stream->chan->pathname,
1431 stream->name, stream->chan->tracefile_size,
1432 stream->chan->tracefile_count, stream->uid, stream->gid,
1433 stream->out_fd, &(stream->tracefile_count_current),
1434 &stream->out_fd);
1435 if (ret < 0) {
1436 ERR("Rotating output file");
1437 goto end;
1438 }
1439 outfd = stream->out_fd;
1440
1441 if (stream->index_fd >= 0) {
1442 ret = index_create_file(stream->chan->pathname,
1443 stream->name, stream->uid, stream->gid,
1444 stream->chan->tracefile_size,
1445 stream->tracefile_count_current);
1446 if (ret < 0) {
1447 goto end;
1448 }
1449 stream->index_fd = ret;
1450 }
1451
1452 /* Reset current size because we just perform a rotation. */
1453 stream->tracefile_size_current = 0;
1454 stream->out_fd_offset = 0;
1455 orig_offset = 0;
1456 }
1457 stream->tracefile_size_current += len;
1458 if (index) {
1459 index->offset = htobe64(stream->out_fd_offset);
1460 }
1461 }
1462
1463 while (len > 0) {
1464 do {
1465 ret = write(outfd, mmap_base + mmap_offset, len);
1466 } while (ret < 0 && errno == EINTR);
1467 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
1468 if (ret < 0) {
1469 /*
1470 * This is possible if the fd is closed on the other side (outfd)
1471 * or any write problem. It can be verbose a bit for a normal
1472 * execution if for instance the relayd is stopped abruptly. This
1473 * can happen so set this to a DBG statement.
1474 */
1475 DBG("Error in file write mmap");
1476 if (written == 0) {
1477 written = -errno;
1478 }
1479 /* Socket operation failed. We consider the relayd dead */
1480 if (errno == EPIPE || errno == EINVAL) {
1481 relayd_hang_up = 1;
1482 goto write_error;
1483 }
1484 goto end;
1485 } else if (ret > len) {
1486 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
1487 written += ret;
1488 goto end;
1489 } else {
1490 len -= ret;
1491 mmap_offset += ret;
1492 }
1493
1494 /* This call is useless on a socket so better save a syscall. */
1495 if (!relayd) {
1496 /* This won't block, but will start writeout asynchronously */
1497 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1498 SYNC_FILE_RANGE_WRITE);
1499 stream->out_fd_offset += ret;
1500 }
1501 stream->output_written += ret;
1502 written += ret;
1503 }
1504 lttng_consumer_sync_trace_file(stream, orig_offset);
1505
1506 write_error:
1507 /*
1508 * This is a special case that the relayd has closed its socket. Let's
1509 * cleanup the relayd object and all associated streams.
1510 */
1511 if (relayd && relayd_hang_up) {
1512 cleanup_relayd(relayd, ctx);
1513 }
1514
1515 end:
1516 /* Unlock only if ctrl socket used */
1517 if (relayd && stream->metadata_flag) {
1518 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1519 }
1520
1521 rcu_read_unlock();
1522 return written;
1523 }
1524
1525 /*
1526 * Splice the data from the ring buffer to the tracefile.
1527 *
1528 * It must be called with the stream lock held.
1529 *
1530 * Returns the number of bytes spliced.
1531 */
1532 ssize_t lttng_consumer_on_read_subbuffer_splice(
1533 struct lttng_consumer_local_data *ctx,
1534 struct lttng_consumer_stream *stream, unsigned long len,
1535 unsigned long padding,
1536 struct lttng_packet_index *index)
1537 {
1538 ssize_t ret = 0, written = 0, ret_splice = 0;
1539 loff_t offset = 0;
1540 off_t orig_offset = stream->out_fd_offset;
1541 int fd = stream->wait_fd;
1542 /* Default is on the disk */
1543 int outfd = stream->out_fd;
1544 struct consumer_relayd_sock_pair *relayd = NULL;
1545 int *splice_pipe;
1546 unsigned int relayd_hang_up = 0;
1547
1548 switch (consumer_data.type) {
1549 case LTTNG_CONSUMER_KERNEL:
1550 break;
1551 case LTTNG_CONSUMER32_UST:
1552 case LTTNG_CONSUMER64_UST:
1553 /* Not supported for user space tracing */
1554 return -ENOSYS;
1555 default:
1556 ERR("Unknown consumer_data type");
1557 assert(0);
1558 }
1559
1560 /* RCU lock for the relayd pointer */
1561 rcu_read_lock();
1562
1563 /* Flag that the current stream if set for network streaming. */
1564 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1565 relayd = consumer_find_relayd(stream->net_seq_idx);
1566 if (relayd == NULL) {
1567 ret = -EPIPE;
1568 goto end;
1569 }
1570 }
1571
1572 /*
1573 * Choose right pipe for splice. Metadata and trace data are handled by
1574 * different threads hence the use of two pipes in order not to race or
1575 * corrupt the written data.
1576 */
1577 if (stream->metadata_flag) {
1578 splice_pipe = ctx->consumer_splice_metadata_pipe;
1579 } else {
1580 splice_pipe = ctx->consumer_thread_pipe;
1581 }
1582
1583 /* Write metadata stream id before payload */
1584 if (relayd) {
1585 int total_len = len;
1586
1587 if (stream->metadata_flag) {
1588 /*
1589 * Lock the control socket for the complete duration of the function
1590 * since from this point on we will use the socket.
1591 */
1592 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1593
1594 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1595 padding);
1596 if (ret < 0) {
1597 written = ret;
1598 /* Socket operation failed. We consider the relayd dead */
1599 if (ret == -EBADF) {
1600 WARN("Remote relayd disconnected. Stopping");
1601 relayd_hang_up = 1;
1602 goto write_error;
1603 }
1604 goto end;
1605 }
1606
1607 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1608 }
1609
1610 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1611 if (ret >= 0) {
1612 /* Use the returned socket. */
1613 outfd = ret;
1614 } else {
1615 /* Socket operation failed. We consider the relayd dead */
1616 if (ret == -EBADF) {
1617 WARN("Remote relayd disconnected. Stopping");
1618 relayd_hang_up = 1;
1619 goto write_error;
1620 }
1621 goto end;
1622 }
1623 } else {
1624 /* No streaming, we have to set the len with the full padding */
1625 len += padding;
1626
1627 /*
1628 * Check if we need to change the tracefile before writing the packet.
1629 */
1630 if (stream->chan->tracefile_size > 0 &&
1631 (stream->tracefile_size_current + len) >
1632 stream->chan->tracefile_size) {
1633 ret = utils_rotate_stream_file(stream->chan->pathname,
1634 stream->name, stream->chan->tracefile_size,
1635 stream->chan->tracefile_count, stream->uid, stream->gid,
1636 stream->out_fd, &(stream->tracefile_count_current),
1637 &stream->out_fd);
1638 if (ret < 0) {
1639 ERR("Rotating output file");
1640 goto end;
1641 }
1642 outfd = stream->out_fd;
1643
1644 if (stream->index_fd >= 0) {
1645 ret = index_create_file(stream->chan->pathname,
1646 stream->name, stream->uid, stream->gid,
1647 stream->chan->tracefile_size,
1648 stream->tracefile_count_current);
1649 if (ret < 0) {
1650 goto end;
1651 }
1652 stream->index_fd = ret;
1653 }
1654
1655 /* Reset current size because we just perform a rotation. */
1656 stream->tracefile_size_current = 0;
1657 stream->out_fd_offset = 0;
1658 orig_offset = 0;
1659 }
1660 stream->tracefile_size_current += len;
1661 index->offset = htobe64(stream->out_fd_offset);
1662 }
1663
1664 while (len > 0) {
1665 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1666 (unsigned long)offset, len, fd, splice_pipe[1]);
1667 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
1668 SPLICE_F_MOVE | SPLICE_F_MORE);
1669 DBG("splice chan to pipe, ret %zd", ret_splice);
1670 if (ret_splice < 0) {
1671 PERROR("Error in relay splice");
1672 if (written == 0) {
1673 written = ret_splice;
1674 }
1675 ret = errno;
1676 goto splice_error;
1677 }
1678
1679 /* Handle stream on the relayd if the output is on the network */
1680 if (relayd) {
1681 if (stream->metadata_flag) {
1682 size_t metadata_payload_size =
1683 sizeof(struct lttcomm_relayd_metadata_payload);
1684
1685 /* Update counter to fit the spliced data */
1686 ret_splice += metadata_payload_size;
1687 len += metadata_payload_size;
1688 /*
1689 * We do this so the return value can match the len passed as
1690 * argument to this function.
1691 */
1692 written -= metadata_payload_size;
1693 }
1694 }
1695
1696 /* Splice data out */
1697 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
1698 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1699 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
1700 if (ret_splice < 0) {
1701 PERROR("Error in file splice");
1702 if (written == 0) {
1703 written = ret_splice;
1704 }
1705 /* Socket operation failed. We consider the relayd dead */
1706 if (errno == EBADF || errno == EPIPE) {
1707 WARN("Remote relayd disconnected. Stopping");
1708 relayd_hang_up = 1;
1709 goto write_error;
1710 }
1711 ret = errno;
1712 goto splice_error;
1713 } else if (ret_splice > len) {
1714 errno = EINVAL;
1715 PERROR("Wrote more data than requested %zd (len: %lu)",
1716 ret_splice, len);
1717 written += ret_splice;
1718 ret = errno;
1719 goto splice_error;
1720 }
1721 len -= ret_splice;
1722
1723 /* This call is useless on a socket so better save a syscall. */
1724 if (!relayd) {
1725 /* This won't block, but will start writeout asynchronously */
1726 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1727 SYNC_FILE_RANGE_WRITE);
1728 stream->out_fd_offset += ret_splice;
1729 }
1730 stream->output_written += ret_splice;
1731 written += ret_splice;
1732 }
1733 lttng_consumer_sync_trace_file(stream, orig_offset);
1734
1735 ret = ret_splice;
1736
1737 goto end;
1738
1739 write_error:
1740 /*
1741 * This is a special case that the relayd has closed its socket. Let's
1742 * cleanup the relayd object and all associated streams.
1743 */
1744 if (relayd && relayd_hang_up) {
1745 cleanup_relayd(relayd, ctx);
1746 /* Skip splice error so the consumer does not fail */
1747 goto end;
1748 }
1749
1750 splice_error:
1751 /* send the appropriate error description to sessiond */
1752 switch (ret) {
1753 case EINVAL:
1754 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
1755 break;
1756 case ENOMEM:
1757 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
1758 break;
1759 case ESPIPE:
1760 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
1761 break;
1762 }
1763
1764 end:
1765 if (relayd && stream->metadata_flag) {
1766 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1767 }
1768
1769 rcu_read_unlock();
1770 return written;
1771 }
1772
1773 /*
1774 * Take a snapshot for a specific fd
1775 *
1776 * Returns 0 on success, < 0 on error
1777 */
1778 int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
1779 {
1780 switch (consumer_data.type) {
1781 case LTTNG_CONSUMER_KERNEL:
1782 return lttng_kconsumer_take_snapshot(stream);
1783 case LTTNG_CONSUMER32_UST:
1784 case LTTNG_CONSUMER64_UST:
1785 return lttng_ustconsumer_take_snapshot(stream);
1786 default:
1787 ERR("Unknown consumer_data type");
1788 assert(0);
1789 return -ENOSYS;
1790 }
1791 }
1792
1793 /*
1794 * Get the produced position
1795 *
1796 * Returns 0 on success, < 0 on error
1797 */
1798 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
1799 unsigned long *pos)
1800 {
1801 switch (consumer_data.type) {
1802 case LTTNG_CONSUMER_KERNEL:
1803 return lttng_kconsumer_get_produced_snapshot(stream, pos);
1804 case LTTNG_CONSUMER32_UST:
1805 case LTTNG_CONSUMER64_UST:
1806 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
1807 default:
1808 ERR("Unknown consumer_data type");
1809 assert(0);
1810 return -ENOSYS;
1811 }
1812 }
1813
1814 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1815 int sock, struct pollfd *consumer_sockpoll)
1816 {
1817 switch (consumer_data.type) {
1818 case LTTNG_CONSUMER_KERNEL:
1819 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1820 case LTTNG_CONSUMER32_UST:
1821 case LTTNG_CONSUMER64_UST:
1822 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1823 default:
1824 ERR("Unknown consumer_data type");
1825 assert(0);
1826 return -ENOSYS;
1827 }
1828 }
1829
1830 /*
1831 * Iterate over all streams of the hashtable and free them properly.
1832 *
1833 * WARNING: *MUST* be used with data stream only.
1834 */
1835 static void destroy_data_stream_ht(struct lttng_ht *ht)
1836 {
1837 struct lttng_ht_iter iter;
1838 struct lttng_consumer_stream *stream;
1839
1840 if (ht == NULL) {
1841 return;
1842 }
1843
1844 rcu_read_lock();
1845 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1846 /*
1847 * Ignore return value since we are currently cleaning up so any error
1848 * can't be handled.
1849 */
1850 (void) consumer_del_stream(stream, ht);
1851 }
1852 rcu_read_unlock();
1853
1854 lttng_ht_destroy(ht);
1855 }
1856
1857 /*
1858 * Iterate over all streams of the hashtable and free them properly.
1859 *
1860 * XXX: Should not be only for metadata stream or else use an other name.
1861 */
1862 static void destroy_stream_ht(struct lttng_ht *ht)
1863 {
1864 struct lttng_ht_iter iter;
1865 struct lttng_consumer_stream *stream;
1866
1867 if (ht == NULL) {
1868 return;
1869 }
1870
1871 rcu_read_lock();
1872 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1873 /*
1874 * Ignore return value since we are currently cleaning up so any error
1875 * can't be handled.
1876 */
1877 (void) consumer_del_metadata_stream(stream, ht);
1878 }
1879 rcu_read_unlock();
1880
1881 lttng_ht_destroy(ht);
1882 }
1883
1884 void lttng_consumer_close_metadata(void)
1885 {
1886 switch (consumer_data.type) {
1887 case LTTNG_CONSUMER_KERNEL:
1888 /*
1889 * The Kernel consumer has a different metadata scheme so we don't
1890 * close anything because the stream will be closed by the session
1891 * daemon.
1892 */
1893 break;
1894 case LTTNG_CONSUMER32_UST:
1895 case LTTNG_CONSUMER64_UST:
1896 /*
1897 * Close all metadata streams. The metadata hash table is passed and
1898 * this call iterates over it by closing all wakeup fd. This is safe
1899 * because at this point we are sure that the metadata producer is
1900 * either dead or blocked.
1901 */
1902 lttng_ustconsumer_close_metadata(metadata_ht);
1903 break;
1904 default:
1905 ERR("Unknown consumer_data type");
1906 assert(0);
1907 }
1908 }
1909
1910 /*
1911 * Clean up a metadata stream and free its memory.
1912 */
1913 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1914 struct lttng_ht *ht)
1915 {
1916 int ret;
1917 struct lttng_ht_iter iter;
1918 struct lttng_consumer_channel *free_chan = NULL;
1919 struct consumer_relayd_sock_pair *relayd;
1920
1921 assert(stream);
1922 /*
1923 * This call should NEVER receive regular stream. It must always be
1924 * metadata stream and this is crucial for data structure synchronization.
1925 */
1926 assert(stream->metadata_flag);
1927
1928 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1929
1930 if (ht == NULL) {
1931 /* Means the stream was allocated but not successfully added */
1932 goto free_stream_rcu;
1933 }
1934
1935 pthread_mutex_lock(&consumer_data.lock);
1936 pthread_mutex_lock(&stream->chan->lock);
1937 pthread_mutex_lock(&stream->lock);
1938
1939 switch (consumer_data.type) {
1940 case LTTNG_CONSUMER_KERNEL:
1941 if (stream->mmap_base != NULL) {
1942 ret = munmap(stream->mmap_base, stream->mmap_len);
1943 if (ret != 0) {
1944 PERROR("munmap metadata stream");
1945 }
1946 }
1947 if (stream->wait_fd >= 0) {
1948 ret = close(stream->wait_fd);
1949 if (ret < 0) {
1950 PERROR("close kernel metadata wait_fd");
1951 }
1952 }
1953 break;
1954 case LTTNG_CONSUMER32_UST:
1955 case LTTNG_CONSUMER64_UST:
1956 if (stream->monitor) {
1957 /* close the write-side in close_metadata */
1958 ret = close(stream->ust_metadata_poll_pipe[0]);
1959 if (ret < 0) {
1960 PERROR("Close UST metadata read-side poll pipe");
1961 }
1962 }
1963 lttng_ustconsumer_del_stream(stream);
1964 break;
1965 default:
1966 ERR("Unknown consumer_data type");
1967 assert(0);
1968 goto end;
1969 }
1970
1971 rcu_read_lock();
1972 iter.iter.node = &stream->node.node;
1973 ret = lttng_ht_del(ht, &iter);
1974 assert(!ret);
1975
1976 iter.iter.node = &stream->node_channel_id.node;
1977 ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
1978 assert(!ret);
1979
1980 iter.iter.node = &stream->node_session_id.node;
1981 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1982 assert(!ret);
1983 rcu_read_unlock();
1984
1985 if (stream->out_fd >= 0) {
1986 ret = close(stream->out_fd);
1987 if (ret) {
1988 PERROR("close");
1989 }
1990 }
1991
1992 /* Check and cleanup relayd */
1993 rcu_read_lock();
1994 relayd = consumer_find_relayd(stream->net_seq_idx);
1995 if (relayd != NULL) {
1996 uatomic_dec(&relayd->refcount);
1997 assert(uatomic_read(&relayd->refcount) >= 0);
1998
1999 /* Closing streams requires to lock the control socket. */
2000 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2001 ret = relayd_send_close_stream(&relayd->control_sock,
2002 stream->relayd_stream_id, stream->next_net_seq_num - 1);
2003 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2004 if (ret < 0) {
2005 DBG("Unable to close stream on the relayd. Continuing");
2006 /*
2007 * Continue here. There is nothing we can do for the relayd.
2008 * Chances are that the relayd has closed the socket so we just
2009 * continue cleaning up.
2010 */
2011 }
2012
2013 /* Both conditions are met, we destroy the relayd. */
2014 if (uatomic_read(&relayd->refcount) == 0 &&
2015 uatomic_read(&relayd->destroy_flag)) {
2016 consumer_destroy_relayd(relayd);
2017 }
2018 }
2019 rcu_read_unlock();
2020
2021 /* Atomically decrement channel refcount since other threads can use it. */
2022 if (!uatomic_sub_return(&stream->chan->refcount, 1)
2023 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
2024 /* Go for channel deletion! */
2025 free_chan = stream->chan;
2026 }
2027
2028 end:
2029 /*
2030 * Nullify the stream reference so it is not used after deletion. The
2031 * channel lock MUST be acquired before being able to check for
2032 * a NULL pointer value.
2033 */
2034 stream->chan->metadata_stream = NULL;
2035
2036 pthread_mutex_unlock(&stream->lock);
2037 pthread_mutex_unlock(&stream->chan->lock);
2038 pthread_mutex_unlock(&consumer_data.lock);
2039
2040 if (free_chan) {
2041 consumer_del_channel(free_chan);
2042 }
2043
2044 free_stream_rcu:
2045 call_rcu(&stream->node.head, free_stream_rcu);
2046 }
2047
2048 /*
2049 * Action done with the metadata stream when adding it to the consumer internal
2050 * data structures to handle it.
2051 */
2052 int consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
2053 {
2054 struct lttng_ht *ht = metadata_ht;
2055 int ret = 0;
2056 struct lttng_ht_iter iter;
2057 struct lttng_ht_node_u64 *node;
2058
2059 assert(stream);
2060 assert(ht);
2061
2062 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
2063
2064 pthread_mutex_lock(&consumer_data.lock);
2065 pthread_mutex_lock(&stream->chan->lock);
2066 pthread_mutex_lock(&stream->chan->timer_lock);
2067 pthread_mutex_lock(&stream->lock);
2068
2069 /*
2070 * From here, refcounts are updated so be _careful_ when returning an error
2071 * after this point.
2072 */
2073
2074 rcu_read_lock();
2075
2076 /*
2077 * Lookup the stream just to make sure it does not exist in our internal
2078 * state. This should NEVER happen.
2079 */
2080 lttng_ht_lookup(ht, &stream->key, &iter);
2081 node = lttng_ht_iter_get_node_u64(&iter);
2082 assert(!node);
2083
2084 /*
2085 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2086 * in terms of destroying the associated channel, because the action that
2087 * causes the count to become 0 also causes a stream to be added. The
2088 * channel deletion will thus be triggered by the following removal of this
2089 * stream.
2090 */
2091 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
2092 /* Increment refcount before decrementing nb_init_stream_left */
2093 cmm_smp_wmb();
2094 uatomic_dec(&stream->chan->nb_init_stream_left);
2095 }
2096
2097 lttng_ht_add_unique_u64(ht, &stream->node);
2098
2099 lttng_ht_add_unique_u64(consumer_data.stream_per_chan_id_ht,
2100 &stream->node_channel_id);
2101
2102 /*
2103 * Add stream to the stream_list_ht of the consumer data. No need to steal
2104 * the key since the HT does not use it and we allow to add redundant keys
2105 * into this table.
2106 */
2107 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
2108
2109 rcu_read_unlock();
2110
2111 pthread_mutex_unlock(&stream->lock);
2112 pthread_mutex_unlock(&stream->chan->lock);
2113 pthread_mutex_unlock(&stream->chan->timer_lock);
2114 pthread_mutex_unlock(&consumer_data.lock);
2115 return ret;
2116 }
2117
2118 /*
2119 * Delete data stream that are flagged for deletion (endpoint_status).
2120 */
2121 static void validate_endpoint_status_data_stream(void)
2122 {
2123 struct lttng_ht_iter iter;
2124 struct lttng_consumer_stream *stream;
2125
2126 DBG("Consumer delete flagged data stream");
2127
2128 rcu_read_lock();
2129 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2130 /* Validate delete flag of the stream */
2131 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2132 continue;
2133 }
2134 /* Delete it right now */
2135 consumer_del_stream(stream, data_ht);
2136 }
2137 rcu_read_unlock();
2138 }
2139
2140 /*
2141 * Delete metadata stream that are flagged for deletion (endpoint_status).
2142 */
2143 static void validate_endpoint_status_metadata_stream(
2144 struct lttng_poll_event *pollset)
2145 {
2146 struct lttng_ht_iter iter;
2147 struct lttng_consumer_stream *stream;
2148
2149 DBG("Consumer delete flagged metadata stream");
2150
2151 assert(pollset);
2152
2153 rcu_read_lock();
2154 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2155 /* Validate delete flag of the stream */
2156 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2157 continue;
2158 }
2159 /*
2160 * Remove from pollset so the metadata thread can continue without
2161 * blocking on a deleted stream.
2162 */
2163 lttng_poll_del(pollset, stream->wait_fd);
2164
2165 /* Delete it right now */
2166 consumer_del_metadata_stream(stream, metadata_ht);
2167 }
2168 rcu_read_unlock();
2169 }
2170
2171 /*
2172 * Thread polls on metadata file descriptor and write them on disk or on the
2173 * network.
2174 */
2175 void *consumer_thread_metadata_poll(void *data)
2176 {
2177 int ret, i, pollfd;
2178 uint32_t revents, nb_fd;
2179 struct lttng_consumer_stream *stream = NULL;
2180 struct lttng_ht_iter iter;
2181 struct lttng_ht_node_u64 *node;
2182 struct lttng_poll_event events;
2183 struct lttng_consumer_local_data *ctx = data;
2184 ssize_t len;
2185
2186 rcu_register_thread();
2187
2188 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2189 if (!metadata_ht) {
2190 /* ENOMEM at this point. Better to bail out. */
2191 goto end_ht;
2192 }
2193
2194 DBG("Thread metadata poll started");
2195
2196 /* Size is set to 1 for the consumer_metadata pipe */
2197 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2198 if (ret < 0) {
2199 ERR("Poll set creation failed");
2200 goto end_poll;
2201 }
2202
2203 ret = lttng_poll_add(&events,
2204 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN);
2205 if (ret < 0) {
2206 goto end;
2207 }
2208
2209 /* Main loop */
2210 DBG("Metadata main loop started");
2211
2212 while (1) {
2213 /* Only the metadata pipe is set */
2214 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2215 goto end;
2216 }
2217
2218 restart:
2219 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2220 ret = lttng_poll_wait(&events, -1);
2221 DBG("Metadata event catched in thread");
2222 if (ret < 0) {
2223 if (errno == EINTR) {
2224 ERR("Poll EINTR catched");
2225 goto restart;
2226 }
2227 goto error;
2228 }
2229
2230 nb_fd = ret;
2231
2232 /* From here, the event is a metadata wait fd */
2233 for (i = 0; i < nb_fd; i++) {
2234 revents = LTTNG_POLL_GETEV(&events, i);
2235 pollfd = LTTNG_POLL_GETFD(&events, i);
2236
2237 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
2238 if (revents & (LPOLLERR | LPOLLHUP )) {
2239 DBG("Metadata thread pipe hung up");
2240 /*
2241 * Remove the pipe from the poll set and continue the loop
2242 * since their might be data to consume.
2243 */
2244 lttng_poll_del(&events,
2245 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2246 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2247 continue;
2248 } else if (revents & LPOLLIN) {
2249 ssize_t pipe_len;
2250
2251 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2252 &stream, sizeof(stream));
2253 if (pipe_len < 0) {
2254 ERR("read metadata stream, ret: %zd", pipe_len);
2255 /*
2256 * Continue here to handle the rest of the streams.
2257 */
2258 continue;
2259 }
2260
2261 /* A NULL stream means that the state has changed. */
2262 if (stream == NULL) {
2263 /* Check for deleted streams. */
2264 validate_endpoint_status_metadata_stream(&events);
2265 goto restart;
2266 }
2267
2268 DBG("Adding metadata stream %d to poll set",
2269 stream->wait_fd);
2270
2271 /* Add metadata stream to the global poll events list */
2272 lttng_poll_add(&events, stream->wait_fd,
2273 LPOLLIN | LPOLLPRI);
2274 }
2275
2276 /* Handle other stream */
2277 continue;
2278 }
2279
2280 rcu_read_lock();
2281 {
2282 uint64_t tmp_id = (uint64_t) pollfd;
2283
2284 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2285 }
2286 node = lttng_ht_iter_get_node_u64(&iter);
2287 assert(node);
2288
2289 stream = caa_container_of(node, struct lttng_consumer_stream,
2290 node);
2291
2292 /* Check for error event */
2293 if (revents & (LPOLLERR | LPOLLHUP)) {
2294 DBG("Metadata fd %d is hup|err.", pollfd);
2295 if (!stream->hangup_flush_done
2296 && (consumer_data.type == LTTNG_CONSUMER32_UST
2297 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2298 DBG("Attempting to flush and consume the UST buffers");
2299 lttng_ustconsumer_on_stream_hangup(stream);
2300
2301 /* We just flushed the stream now read it. */
2302 do {
2303 len = ctx->on_buffer_ready(stream, ctx);
2304 /*
2305 * We don't check the return value here since if we get
2306 * a negative len, it means an error occured thus we
2307 * simply remove it from the poll set and free the
2308 * stream.
2309 */
2310 } while (len > 0);
2311 }
2312
2313 lttng_poll_del(&events, stream->wait_fd);
2314 /*
2315 * This call update the channel states, closes file descriptors
2316 * and securely free the stream.
2317 */
2318 consumer_del_metadata_stream(stream, metadata_ht);
2319 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2320 /* Get the data out of the metadata file descriptor */
2321 DBG("Metadata available on fd %d", pollfd);
2322 assert(stream->wait_fd == pollfd);
2323
2324 do {
2325 len = ctx->on_buffer_ready(stream, ctx);
2326 /*
2327 * We don't check the return value here since if we get
2328 * a negative len, it means an error occured thus we
2329 * simply remove it from the poll set and free the
2330 * stream.
2331 */
2332 } while (len > 0);
2333
2334 /* It's ok to have an unavailable sub-buffer */
2335 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2336 /* Clean up stream from consumer and free it. */
2337 lttng_poll_del(&events, stream->wait_fd);
2338 consumer_del_metadata_stream(stream, metadata_ht);
2339 }
2340 }
2341
2342 /* Release RCU lock for the stream looked up */
2343 rcu_read_unlock();
2344 }
2345 }
2346
2347 error:
2348 end:
2349 DBG("Metadata poll thread exiting");
2350
2351 lttng_poll_clean(&events);
2352 end_poll:
2353 destroy_stream_ht(metadata_ht);
2354 end_ht:
2355 rcu_unregister_thread();
2356 return NULL;
2357 }
2358
2359 /*
2360 * This thread polls the fds in the set to consume the data and write
2361 * it to tracefile if necessary.
2362 */
2363 void *consumer_thread_data_poll(void *data)
2364 {
2365 int num_rdy, num_hup, high_prio, ret, i;
2366 struct pollfd *pollfd = NULL;
2367 /* local view of the streams */
2368 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2369 /* local view of consumer_data.fds_count */
2370 int nb_fd = 0;
2371 struct lttng_consumer_local_data *ctx = data;
2372 ssize_t len;
2373
2374 rcu_register_thread();
2375
2376 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2377 if (data_ht == NULL) {
2378 /* ENOMEM at this point. Better to bail out. */
2379 goto end;
2380 }
2381
2382 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2383 if (local_stream == NULL) {
2384 PERROR("local_stream malloc");
2385 goto end;
2386 }
2387
2388 while (1) {
2389 high_prio = 0;
2390 num_hup = 0;
2391
2392 /*
2393 * the fds set has been updated, we need to update our
2394 * local array as well
2395 */
2396 pthread_mutex_lock(&consumer_data.lock);
2397 if (consumer_data.need_update) {
2398 free(pollfd);
2399 pollfd = NULL;
2400
2401 free(local_stream);
2402 local_stream = NULL;
2403
2404 /* allocate for all fds + 1 for the consumer_data_pipe */
2405 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
2406 if (pollfd == NULL) {
2407 PERROR("pollfd malloc");
2408 pthread_mutex_unlock(&consumer_data.lock);
2409 goto end;
2410 }
2411
2412 /* allocate for all fds + 1 for the consumer_data_pipe */
2413 local_stream = zmalloc((consumer_data.stream_count + 1) *
2414 sizeof(struct lttng_consumer_stream *));
2415 if (local_stream == NULL) {
2416 PERROR("local_stream malloc");
2417 pthread_mutex_unlock(&consumer_data.lock);
2418 goto end;
2419 }
2420 ret = update_poll_array(ctx, &pollfd, local_stream,
2421 data_ht);
2422 if (ret < 0) {
2423 ERR("Error in allocating pollfd or local_outfds");
2424 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2425 pthread_mutex_unlock(&consumer_data.lock);
2426 goto end;
2427 }
2428 nb_fd = ret;
2429 consumer_data.need_update = 0;
2430 }
2431 pthread_mutex_unlock(&consumer_data.lock);
2432
2433 /* No FDs and consumer_quit, consumer_cleanup the thread */
2434 if (nb_fd == 0 && consumer_quit == 1) {
2435 goto end;
2436 }
2437 /* poll on the array of fds */
2438 restart:
2439 DBG("polling on %d fd", nb_fd + 1);
2440 num_rdy = poll(pollfd, nb_fd + 1, -1);
2441 DBG("poll num_rdy : %d", num_rdy);
2442 if (num_rdy == -1) {
2443 /*
2444 * Restart interrupted system call.
2445 */
2446 if (errno == EINTR) {
2447 goto restart;
2448 }
2449 PERROR("Poll error");
2450 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2451 goto end;
2452 } else if (num_rdy == 0) {
2453 DBG("Polling thread timed out");
2454 goto end;
2455 }
2456
2457 /*
2458 * If the consumer_data_pipe triggered poll go directly to the
2459 * beginning of the loop to update the array. We want to prioritize
2460 * array update over low-priority reads.
2461 */
2462 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2463 ssize_t pipe_readlen;
2464
2465 DBG("consumer_data_pipe wake up");
2466 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2467 &new_stream, sizeof(new_stream));
2468 if (pipe_readlen < 0) {
2469 ERR("Consumer data pipe ret %zd", pipe_readlen);
2470 /* Continue so we can at least handle the current stream(s). */
2471 continue;
2472 }
2473
2474 /*
2475 * If the stream is NULL, just ignore it. It's also possible that
2476 * the sessiond poll thread changed the consumer_quit state and is
2477 * waking us up to test it.
2478 */
2479 if (new_stream == NULL) {
2480 validate_endpoint_status_data_stream();
2481 continue;
2482 }
2483
2484 /* Continue to update the local streams and handle prio ones */
2485 continue;
2486 }
2487
2488 /* Take care of high priority channels first. */
2489 for (i = 0; i < nb_fd; i++) {
2490 if (local_stream[i] == NULL) {
2491 continue;
2492 }
2493 if (pollfd[i].revents & POLLPRI) {
2494 DBG("Urgent read on fd %d", pollfd[i].fd);
2495 high_prio = 1;
2496 len = ctx->on_buffer_ready(local_stream[i], ctx);
2497 /* it's ok to have an unavailable sub-buffer */
2498 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2499 /* Clean the stream and free it. */
2500 consumer_del_stream(local_stream[i], data_ht);
2501 local_stream[i] = NULL;
2502 } else if (len > 0) {
2503 local_stream[i]->data_read = 1;
2504 }
2505 }
2506 }
2507
2508 /*
2509 * If we read high prio channel in this loop, try again
2510 * for more high prio data.
2511 */
2512 if (high_prio) {
2513 continue;
2514 }
2515
2516 /* Take care of low priority channels. */
2517 for (i = 0; i < nb_fd; i++) {
2518 if (local_stream[i] == NULL) {
2519 continue;
2520 }
2521 if ((pollfd[i].revents & POLLIN) ||
2522 local_stream[i]->hangup_flush_done) {
2523 DBG("Normal read on fd %d", pollfd[i].fd);
2524 len = ctx->on_buffer_ready(local_stream[i], ctx);
2525 /* it's ok to have an unavailable sub-buffer */
2526 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2527 /* Clean the stream and free it. */
2528 consumer_del_stream(local_stream[i], data_ht);
2529 local_stream[i] = NULL;
2530 } else if (len > 0) {
2531 local_stream[i]->data_read = 1;
2532 }
2533 }
2534 }
2535
2536 /* Handle hangup and errors */
2537 for (i = 0; i < nb_fd; i++) {
2538 if (local_stream[i] == NULL) {
2539 continue;
2540 }
2541 if (!local_stream[i]->hangup_flush_done
2542 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2543 && (consumer_data.type == LTTNG_CONSUMER32_UST
2544 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2545 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2546 pollfd[i].fd);
2547 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2548 /* Attempt read again, for the data we just flushed. */
2549 local_stream[i]->data_read = 1;
2550 }
2551 /*
2552 * If the poll flag is HUP/ERR/NVAL and we have
2553 * read no data in this pass, we can remove the
2554 * stream from its hash table.
2555 */
2556 if ((pollfd[i].revents & POLLHUP)) {
2557 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2558 if (!local_stream[i]->data_read) {
2559 consumer_del_stream(local_stream[i], data_ht);
2560 local_stream[i] = NULL;
2561 num_hup++;
2562 }
2563 } else if (pollfd[i].revents & POLLERR) {
2564 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2565 if (!local_stream[i]->data_read) {
2566 consumer_del_stream(local_stream[i], data_ht);
2567 local_stream[i] = NULL;
2568 num_hup++;
2569 }
2570 } else if (pollfd[i].revents & POLLNVAL) {
2571 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2572 if (!local_stream[i]->data_read) {
2573 consumer_del_stream(local_stream[i], data_ht);
2574 local_stream[i] = NULL;
2575 num_hup++;
2576 }
2577 }
2578 if (local_stream[i] != NULL) {
2579 local_stream[i]->data_read = 0;
2580 }
2581 }
2582 }
2583 end:
2584 DBG("polling thread exiting");
2585 free(pollfd);
2586 free(local_stream);
2587
2588 /*
2589 * Close the write side of the pipe so epoll_wait() in
2590 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2591 * read side of the pipe. If we close them both, epoll_wait strangely does
2592 * not return and could create a endless wait period if the pipe is the
2593 * only tracked fd in the poll set. The thread will take care of closing
2594 * the read side.
2595 */
2596 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
2597
2598 destroy_data_stream_ht(data_ht);
2599
2600 rcu_unregister_thread();
2601 return NULL;
2602 }
2603
2604 /*
2605 * Close wake-up end of each stream belonging to the channel. This will
2606 * allow the poll() on the stream read-side to detect when the
2607 * write-side (application) finally closes them.
2608 */
2609 static
2610 void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2611 {
2612 struct lttng_ht *ht;
2613 struct lttng_consumer_stream *stream;
2614 struct lttng_ht_iter iter;
2615
2616 ht = consumer_data.stream_per_chan_id_ht;
2617
2618 rcu_read_lock();
2619 cds_lfht_for_each_entry_duplicate(ht->ht,
2620 ht->hash_fct(&channel->key, lttng_ht_seed),
2621 ht->match_fct, &channel->key,
2622 &iter.iter, stream, node_channel_id.node) {
2623 /*
2624 * Protect against teardown with mutex.
2625 */
2626 pthread_mutex_lock(&stream->lock);
2627 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2628 goto next;
2629 }
2630 switch (consumer_data.type) {
2631 case LTTNG_CONSUMER_KERNEL:
2632 break;
2633 case LTTNG_CONSUMER32_UST:
2634 case LTTNG_CONSUMER64_UST:
2635 /*
2636 * Note: a mutex is taken internally within
2637 * liblttng-ust-ctl to protect timer wakeup_fd
2638 * use from concurrent close.
2639 */
2640 lttng_ustconsumer_close_stream_wakeup(stream);
2641 break;
2642 default:
2643 ERR("Unknown consumer_data type");
2644 assert(0);
2645 }
2646 next:
2647 pthread_mutex_unlock(&stream->lock);
2648 }
2649 rcu_read_unlock();
2650 }
2651
2652 static void destroy_channel_ht(struct lttng_ht *ht)
2653 {
2654 struct lttng_ht_iter iter;
2655 struct lttng_consumer_channel *channel;
2656 int ret;
2657
2658 if (ht == NULL) {
2659 return;
2660 }
2661
2662 rcu_read_lock();
2663 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2664 ret = lttng_ht_del(ht, &iter);
2665 assert(ret != 0);
2666 }
2667 rcu_read_unlock();
2668
2669 lttng_ht_destroy(ht);
2670 }
2671
2672 /*
2673 * This thread polls the channel fds to detect when they are being
2674 * closed. It closes all related streams if the channel is detected as
2675 * closed. It is currently only used as a shim layer for UST because the
2676 * consumerd needs to keep the per-stream wakeup end of pipes open for
2677 * periodical flush.
2678 */
2679 void *consumer_thread_channel_poll(void *data)
2680 {
2681 int ret, i, pollfd;
2682 uint32_t revents, nb_fd;
2683 struct lttng_consumer_channel *chan = NULL;
2684 struct lttng_ht_iter iter;
2685 struct lttng_ht_node_u64 *node;
2686 struct lttng_poll_event events;
2687 struct lttng_consumer_local_data *ctx = data;
2688 struct lttng_ht *channel_ht;
2689
2690 rcu_register_thread();
2691
2692 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2693 if (!channel_ht) {
2694 /* ENOMEM at this point. Better to bail out. */
2695 goto end_ht;
2696 }
2697
2698 DBG("Thread channel poll started");
2699
2700 /* Size is set to 1 for the consumer_channel pipe */
2701 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2702 if (ret < 0) {
2703 ERR("Poll set creation failed");
2704 goto end_poll;
2705 }
2706
2707 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2708 if (ret < 0) {
2709 goto end;
2710 }
2711
2712 /* Main loop */
2713 DBG("Channel main loop started");
2714
2715 while (1) {
2716 /* Only the channel pipe is set */
2717 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2718 goto end;
2719 }
2720
2721 restart:
2722 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2723 ret = lttng_poll_wait(&events, -1);
2724 DBG("Channel event catched in thread");
2725 if (ret < 0) {
2726 if (errno == EINTR) {
2727 ERR("Poll EINTR catched");
2728 goto restart;
2729 }
2730 goto end;
2731 }
2732
2733 nb_fd = ret;
2734
2735 /* From here, the event is a channel wait fd */
2736 for (i = 0; i < nb_fd; i++) {
2737 revents = LTTNG_POLL_GETEV(&events, i);
2738 pollfd = LTTNG_POLL_GETFD(&events, i);
2739
2740 /* Just don't waste time if no returned events for the fd */
2741 if (!revents) {
2742 continue;
2743 }
2744 if (pollfd == ctx->consumer_channel_pipe[0]) {
2745 if (revents & (LPOLLERR | LPOLLHUP)) {
2746 DBG("Channel thread pipe hung up");
2747 /*
2748 * Remove the pipe from the poll set and continue the loop
2749 * since their might be data to consume.
2750 */
2751 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2752 continue;
2753 } else if (revents & LPOLLIN) {
2754 enum consumer_channel_action action;
2755 uint64_t key;
2756
2757 ret = read_channel_pipe(ctx, &chan, &key, &action);
2758 if (ret <= 0) {
2759 ERR("Error reading channel pipe");
2760 continue;
2761 }
2762
2763 switch (action) {
2764 case CONSUMER_CHANNEL_ADD:
2765 DBG("Adding channel %d to poll set",
2766 chan->wait_fd);
2767
2768 lttng_ht_node_init_u64(&chan->wait_fd_node,
2769 chan->wait_fd);
2770 rcu_read_lock();
2771 lttng_ht_add_unique_u64(channel_ht,
2772 &chan->wait_fd_node);
2773 rcu_read_unlock();
2774 /* Add channel to the global poll events list */
2775 lttng_poll_add(&events, chan->wait_fd,
2776 LPOLLIN | LPOLLPRI);
2777 break;
2778 case CONSUMER_CHANNEL_DEL:
2779 {
2780 struct lttng_consumer_stream *stream, *stmp;
2781
2782 rcu_read_lock();
2783 chan = consumer_find_channel(key);
2784 if (!chan) {
2785 rcu_read_unlock();
2786 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
2787 break;
2788 }
2789 lttng_poll_del(&events, chan->wait_fd);
2790 iter.iter.node = &chan->wait_fd_node.node;
2791 ret = lttng_ht_del(channel_ht, &iter);
2792 assert(ret == 0);
2793 consumer_close_channel_streams(chan);
2794
2795 switch (consumer_data.type) {
2796 case LTTNG_CONSUMER_KERNEL:
2797 break;
2798 case LTTNG_CONSUMER32_UST:
2799 case LTTNG_CONSUMER64_UST:
2800 /* Delete streams that might have been left in the stream list. */
2801 cds_list_for_each_entry_safe(stream, stmp, &chan->streams.head,
2802 send_node) {
2803 cds_list_del(&stream->send_node);
2804 lttng_ustconsumer_del_stream(stream);
2805 uatomic_sub(&stream->chan->refcount, 1);
2806 assert(&chan->refcount);
2807 free(stream);
2808 }
2809 break;
2810 default:
2811 ERR("Unknown consumer_data type");
2812 assert(0);
2813 }
2814
2815 /*
2816 * Release our own refcount. Force channel deletion even if
2817 * streams were not initialized.
2818 */
2819 if (!uatomic_sub_return(&chan->refcount, 1)) {
2820 consumer_del_channel(chan);
2821 }
2822 rcu_read_unlock();
2823 goto restart;
2824 }
2825 case CONSUMER_CHANNEL_QUIT:
2826 /*
2827 * Remove the pipe from the poll set and continue the loop
2828 * since their might be data to consume.
2829 */
2830 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2831 continue;
2832 default:
2833 ERR("Unknown action");
2834 break;
2835 }
2836 }
2837
2838 /* Handle other stream */
2839 continue;
2840 }
2841
2842 rcu_read_lock();
2843 {
2844 uint64_t tmp_id = (uint64_t) pollfd;
2845
2846 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
2847 }
2848 node = lttng_ht_iter_get_node_u64(&iter);
2849 assert(node);
2850
2851 chan = caa_container_of(node, struct lttng_consumer_channel,
2852 wait_fd_node);
2853
2854 /* Check for error event */
2855 if (revents & (LPOLLERR | LPOLLHUP)) {
2856 DBG("Channel fd %d is hup|err.", pollfd);
2857
2858 lttng_poll_del(&events, chan->wait_fd);
2859 ret = lttng_ht_del(channel_ht, &iter);
2860 assert(ret == 0);
2861 consumer_close_channel_streams(chan);
2862
2863 /* Release our own refcount */
2864 if (!uatomic_sub_return(&chan->refcount, 1)
2865 && !uatomic_read(&chan->nb_init_stream_left)) {
2866 consumer_del_channel(chan);
2867 }
2868 }
2869
2870 /* Release RCU lock for the channel looked up */
2871 rcu_read_unlock();
2872 }
2873 }
2874
2875 end:
2876 lttng_poll_clean(&events);
2877 end_poll:
2878 destroy_channel_ht(channel_ht);
2879 end_ht:
2880 DBG("Channel poll thread exiting");
2881 rcu_unregister_thread();
2882 return NULL;
2883 }
2884
2885 static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
2886 struct pollfd *sockpoll, int client_socket)
2887 {
2888 int ret;
2889
2890 assert(ctx);
2891 assert(sockpoll);
2892
2893 if (lttng_consumer_poll_socket(sockpoll) < 0) {
2894 ret = -1;
2895 goto error;
2896 }
2897 DBG("Metadata connection on client_socket");
2898
2899 /* Blocking call, waiting for transmission */
2900 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
2901 if (ctx->consumer_metadata_socket < 0) {
2902 WARN("On accept metadata");
2903 ret = -1;
2904 goto error;
2905 }
2906 ret = 0;
2907
2908 error:
2909 return ret;
2910 }
2911
2912 /*
2913 * This thread listens on the consumerd socket and receives the file
2914 * descriptors from the session daemon.
2915 */
2916 void *consumer_thread_sessiond_poll(void *data)
2917 {
2918 int sock = -1, client_socket, ret;
2919 /*
2920 * structure to poll for incoming data on communication socket avoids
2921 * making blocking sockets.
2922 */
2923 struct pollfd consumer_sockpoll[2];
2924 struct lttng_consumer_local_data *ctx = data;
2925
2926 rcu_register_thread();
2927
2928 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2929 unlink(ctx->consumer_command_sock_path);
2930 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2931 if (client_socket < 0) {
2932 ERR("Cannot create command socket");
2933 goto end;
2934 }
2935
2936 ret = lttcomm_listen_unix_sock(client_socket);
2937 if (ret < 0) {
2938 goto end;
2939 }
2940
2941 DBG("Sending ready command to lttng-sessiond");
2942 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
2943 /* return < 0 on error, but == 0 is not fatal */
2944 if (ret < 0) {
2945 ERR("Error sending ready command to lttng-sessiond");
2946 goto end;
2947 }
2948
2949 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2950 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2951 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2952 consumer_sockpoll[1].fd = client_socket;
2953 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2954
2955 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2956 goto end;
2957 }
2958 DBG("Connection on client_socket");
2959
2960 /* Blocking call, waiting for transmission */
2961 sock = lttcomm_accept_unix_sock(client_socket);
2962 if (sock < 0) {
2963 WARN("On accept");
2964 goto end;
2965 }
2966
2967 /*
2968 * Setup metadata socket which is the second socket connection on the
2969 * command unix socket.
2970 */
2971 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
2972 if (ret < 0) {
2973 goto end;
2974 }
2975
2976 /* This socket is not useful anymore. */
2977 ret = close(client_socket);
2978 if (ret < 0) {
2979 PERROR("close client_socket");
2980 }
2981 client_socket = -1;
2982
2983 /* update the polling structure to poll on the established socket */
2984 consumer_sockpoll[1].fd = sock;
2985 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2986
2987 while (1) {
2988 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2989 goto end;
2990 }
2991 DBG("Incoming command on sock");
2992 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2993 if (ret == -ENOENT) {
2994 DBG("Received STOP command");
2995 goto end;
2996 }
2997 if (ret <= 0) {
2998 /*
2999 * This could simply be a session daemon quitting. Don't output
3000 * ERR() here.
3001 */
3002 DBG("Communication interrupted on command socket");
3003 goto end;
3004 }
3005 if (consumer_quit) {
3006 DBG("consumer_thread_receive_fds received quit from signal");
3007 goto end;
3008 }
3009 DBG("received command on sock");
3010 }
3011 end:
3012 DBG("Consumer thread sessiond poll exiting");
3013
3014 /*
3015 * Close metadata streams since the producer is the session daemon which
3016 * just died.
3017 *
3018 * NOTE: for now, this only applies to the UST tracer.
3019 */
3020 lttng_consumer_close_metadata();
3021
3022 /*
3023 * when all fds have hung up, the polling thread
3024 * can exit cleanly
3025 */
3026 consumer_quit = 1;
3027
3028 /*
3029 * Notify the data poll thread to poll back again and test the
3030 * consumer_quit state that we just set so to quit gracefully.
3031 */
3032 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
3033
3034 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
3035
3036 /* Cleaning up possibly open sockets. */
3037 if (sock >= 0) {
3038 ret = close(sock);
3039 if (ret < 0) {
3040 PERROR("close sock sessiond poll");
3041 }
3042 }
3043 if (client_socket >= 0) {
3044 ret = close(client_socket);
3045 if (ret < 0) {
3046 PERROR("close client_socket sessiond poll");
3047 }
3048 }
3049
3050 rcu_unregister_thread();
3051 return NULL;
3052 }
3053
3054 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
3055 struct lttng_consumer_local_data *ctx)
3056 {
3057 ssize_t ret;
3058
3059 pthread_mutex_lock(&stream->lock);
3060
3061 switch (consumer_data.type) {
3062 case LTTNG_CONSUMER_KERNEL:
3063 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3064 break;
3065 case LTTNG_CONSUMER32_UST:
3066 case LTTNG_CONSUMER64_UST:
3067 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3068 break;
3069 default:
3070 ERR("Unknown consumer_data type");
3071 assert(0);
3072 ret = -ENOSYS;
3073 break;
3074 }
3075
3076 pthread_mutex_unlock(&stream->lock);
3077 return ret;
3078 }
3079
3080 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3081 {
3082 switch (consumer_data.type) {
3083 case LTTNG_CONSUMER_KERNEL:
3084 return lttng_kconsumer_on_recv_stream(stream);
3085 case LTTNG_CONSUMER32_UST:
3086 case LTTNG_CONSUMER64_UST:
3087 return lttng_ustconsumer_on_recv_stream(stream);
3088 default:
3089 ERR("Unknown consumer_data type");
3090 assert(0);
3091 return -ENOSYS;
3092 }
3093 }
3094
3095 /*
3096 * Allocate and set consumer data hash tables.
3097 */
3098 void lttng_consumer_init(void)
3099 {
3100 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3101 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3102 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3103 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3104 }
3105
3106 /*
3107 * Process the ADD_RELAYD command receive by a consumer.
3108 *
3109 * This will create a relayd socket pair and add it to the relayd hash table.
3110 * The caller MUST acquire a RCU read side lock before calling it.
3111 */
3112 int consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type,
3113 struct lttng_consumer_local_data *ctx, int sock,
3114 struct pollfd *consumer_sockpoll,
3115 struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id)
3116 {
3117 int fd = -1, ret = -1, relayd_created = 0;
3118 enum lttng_error_code ret_code = LTTNG_OK;
3119 struct consumer_relayd_sock_pair *relayd = NULL;
3120
3121 assert(ctx);
3122 assert(relayd_sock);
3123
3124 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx);
3125
3126 /* Get relayd reference if exists. */
3127 relayd = consumer_find_relayd(net_seq_idx);
3128 if (relayd == NULL) {
3129 assert(sock_type == LTTNG_STREAM_CONTROL);
3130 /* Not found. Allocate one. */
3131 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3132 if (relayd == NULL) {
3133 ret = -ENOMEM;
3134 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3135 goto error;
3136 } else {
3137 relayd->sessiond_session_id = sessiond_id;
3138 relayd_created = 1;
3139 }
3140
3141 /*
3142 * This code path MUST continue to the consumer send status message to
3143 * we can notify the session daemon and continue our work without
3144 * killing everything.
3145 */
3146 } else {
3147 /*
3148 * relayd key should never be found for control socket.
3149 */
3150 assert(sock_type != LTTNG_STREAM_CONTROL);
3151 }
3152
3153 /* First send a status message before receiving the fds. */
3154 ret = consumer_send_status_msg(sock, LTTNG_OK);
3155 if (ret < 0) {
3156 /* Somehow, the session daemon is not responding anymore. */
3157 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3158 goto error_nosignal;
3159 }
3160
3161 /* Poll on consumer socket. */
3162 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3163 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3164 ret = -EINTR;
3165 goto error_nosignal;
3166 }
3167
3168 /* Get relayd socket from session daemon */
3169 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3170 if (ret != sizeof(fd)) {
3171 ret = -1;
3172 fd = -1; /* Just in case it gets set with an invalid value. */
3173
3174 /*
3175 * Failing to receive FDs might indicate a major problem such as
3176 * reaching a fd limit during the receive where the kernel returns a
3177 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3178 * don't take any chances and stop everything.
3179 *
3180 * XXX: Feature request #558 will fix that and avoid this possible
3181 * issue when reaching the fd limit.
3182 */
3183 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3184 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
3185 goto error;
3186 }
3187
3188 /* Copy socket information and received FD */
3189 switch (sock_type) {
3190 case LTTNG_STREAM_CONTROL:
3191 /* Copy received lttcomm socket */
3192 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3193 ret = lttcomm_create_sock(&relayd->control_sock.sock);
3194 /* Handle create_sock error. */
3195 if (ret < 0) {
3196 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3197 goto error;
3198 }
3199 /*
3200 * Close the socket created internally by
3201 * lttcomm_create_sock, so we can replace it by the one
3202 * received from sessiond.
3203 */
3204 if (close(relayd->control_sock.sock.fd)) {
3205 PERROR("close");
3206 }
3207
3208 /* Assign new file descriptor */
3209 relayd->control_sock.sock.fd = fd;
3210 fd = -1; /* For error path */
3211 /* Assign version values. */
3212 relayd->control_sock.major = relayd_sock->major;
3213 relayd->control_sock.minor = relayd_sock->minor;
3214
3215 /*
3216 * Create a session on the relayd and store the returned id. Lock the
3217 * control socket mutex if the relayd was NOT created before.
3218 */
3219 if (!relayd_created) {
3220 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3221 }
3222 ret = relayd_create_session(&relayd->control_sock,
3223 &relayd->relayd_session_id);
3224 if (!relayd_created) {
3225 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3226 }
3227 if (ret < 0) {
3228 /*
3229 * Close all sockets of a relayd object. It will be freed if it was
3230 * created at the error code path or else it will be garbage
3231 * collect.
3232 */
3233 (void) relayd_close(&relayd->control_sock);
3234 (void) relayd_close(&relayd->data_sock);
3235 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3236 goto error;
3237 }
3238
3239 break;
3240 case LTTNG_STREAM_DATA:
3241 /* Copy received lttcomm socket */
3242 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3243 ret = lttcomm_create_sock(&relayd->data_sock.sock);
3244 /* Handle create_sock error. */
3245 if (ret < 0) {
3246 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3247 goto error;
3248 }
3249 /*
3250 * Close the socket created internally by
3251 * lttcomm_create_sock, so we can replace it by the one
3252 * received from sessiond.
3253 */
3254 if (close(relayd->data_sock.sock.fd)) {
3255 PERROR("close");
3256 }
3257
3258 /* Assign new file descriptor */
3259 relayd->data_sock.sock.fd = fd;
3260 fd = -1; /* for eventual error paths */
3261 /* Assign version values. */
3262 relayd->data_sock.major = relayd_sock->major;
3263 relayd->data_sock.minor = relayd_sock->minor;
3264 break;
3265 default:
3266 ERR("Unknown relayd socket type (%d)", sock_type);
3267 ret = -1;
3268 ret_code = LTTCOMM_CONSUMERD_FATAL;
3269 goto error;
3270 }
3271
3272 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
3273 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3274 relayd->net_seq_idx, fd);
3275
3276 /* We successfully added the socket. Send status back. */
3277 ret = consumer_send_status_msg(sock, ret_code);
3278 if (ret < 0) {
3279 /* Somehow, the session daemon is not responding anymore. */
3280 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3281 goto error_nosignal;
3282 }
3283
3284 /*
3285 * Add relayd socket pair to consumer data hashtable. If object already
3286 * exists or on error, the function gracefully returns.
3287 */
3288 add_relayd(relayd);
3289
3290 /* All good! */
3291 return 0;
3292
3293 error:
3294 if (consumer_send_status_msg(sock, ret_code) < 0) {
3295 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3296 }
3297
3298 error_nosignal:
3299 /* Close received socket if valid. */
3300 if (fd >= 0) {
3301 if (close(fd)) {
3302 PERROR("close received socket");
3303 }
3304 }
3305
3306 if (relayd_created) {
3307 free(relayd);
3308 }
3309
3310 return ret;
3311 }
3312
3313 /*
3314 * Try to lock the stream mutex.
3315 *
3316 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3317 */
3318 static int stream_try_lock(struct lttng_consumer_stream *stream)
3319 {
3320 int ret;
3321
3322 assert(stream);
3323
3324 /*
3325 * Try to lock the stream mutex. On failure, we know that the stream is
3326 * being used else where hence there is data still being extracted.
3327 */
3328 ret = pthread_mutex_trylock(&stream->lock);
3329 if (ret) {
3330 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3331 ret = 0;
3332 goto end;
3333 }
3334
3335 ret = 1;
3336
3337 end:
3338 return ret;
3339 }
3340
3341 /*
3342 * Search for a relayd associated to the session id and return the reference.
3343 *
3344 * A rcu read side lock MUST be acquire before calling this function and locked
3345 * until the relayd object is no longer necessary.
3346 */
3347 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3348 {
3349 struct lttng_ht_iter iter;
3350 struct consumer_relayd_sock_pair *relayd = NULL;
3351
3352 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3353 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3354 node.node) {
3355 /*
3356 * Check by sessiond id which is unique here where the relayd session
3357 * id might not be when having multiple relayd.
3358 */
3359 if (relayd->sessiond_session_id == id) {
3360 /* Found the relayd. There can be only one per id. */
3361 goto found;
3362 }
3363 }
3364
3365 return NULL;
3366
3367 found:
3368 return relayd;
3369 }
3370
3371 /*
3372 * Check if for a given session id there is still data needed to be extract
3373 * from the buffers.
3374 *
3375 * Return 1 if data is pending or else 0 meaning ready to be read.
3376 */
3377 int consumer_data_pending(uint64_t id)
3378 {
3379 int ret;
3380 struct lttng_ht_iter iter;
3381 struct lttng_ht *ht;
3382 struct lttng_consumer_stream *stream;
3383 struct consumer_relayd_sock_pair *relayd = NULL;
3384 int (*data_pending)(struct lttng_consumer_stream *);
3385
3386 DBG("Consumer data pending command on session id %" PRIu64, id);
3387
3388 rcu_read_lock();
3389 pthread_mutex_lock(&consumer_data.lock);
3390
3391 switch (consumer_data.type) {
3392 case LTTNG_CONSUMER_KERNEL:
3393 data_pending = lttng_kconsumer_data_pending;
3394 break;
3395 case LTTNG_CONSUMER32_UST:
3396 case LTTNG_CONSUMER64_UST:
3397 data_pending = lttng_ustconsumer_data_pending;
3398 break;
3399 default:
3400 ERR("Unknown consumer data type");
3401 assert(0);
3402 }
3403
3404 /* Ease our life a bit */
3405 ht = consumer_data.stream_list_ht;
3406
3407 relayd = find_relayd_by_session_id(id);
3408 if (relayd) {
3409 /* Send init command for data pending. */
3410 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3411 ret = relayd_begin_data_pending(&relayd->control_sock,
3412 relayd->relayd_session_id);
3413 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3414 if (ret < 0) {
3415 /* Communication error thus the relayd so no data pending. */
3416 goto data_not_pending;
3417 }
3418 }
3419
3420 cds_lfht_for_each_entry_duplicate(ht->ht,
3421 ht->hash_fct(&id, lttng_ht_seed),
3422 ht->match_fct, &id,
3423 &iter.iter, stream, node_session_id.node) {
3424 /* If this call fails, the stream is being used hence data pending. */
3425 ret = stream_try_lock(stream);
3426 if (!ret) {
3427 goto data_pending;
3428 }
3429
3430 /*
3431 * A removed node from the hash table indicates that the stream has
3432 * been deleted thus having a guarantee that the buffers are closed
3433 * on the consumer side. However, data can still be transmitted
3434 * over the network so don't skip the relayd check.
3435 */
3436 ret = cds_lfht_is_node_deleted(&stream->node.node);
3437 if (!ret) {
3438 /*
3439 * An empty output file is not valid. We need at least one packet
3440 * generated per stream, even if it contains no event, so it
3441 * contains at least one packet header.
3442 */
3443 if (stream->output_written == 0) {
3444 pthread_mutex_unlock(&stream->lock);
3445 goto data_pending;
3446 }
3447 /* Check the stream if there is data in the buffers. */
3448 ret = data_pending(stream);
3449 if (ret == 1) {
3450 pthread_mutex_unlock(&stream->lock);
3451 goto data_pending;
3452 }
3453 }
3454
3455 /* Relayd check */
3456 if (relayd) {
3457 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3458 if (stream->metadata_flag) {
3459 ret = relayd_quiescent_control(&relayd->control_sock,
3460 stream->relayd_stream_id);
3461 } else {
3462 ret = relayd_data_pending(&relayd->control_sock,
3463 stream->relayd_stream_id,
3464 stream->next_net_seq_num - 1);
3465 }
3466 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3467 if (ret == 1) {
3468 pthread_mutex_unlock(&stream->lock);
3469 goto data_pending;
3470 }
3471 }
3472 pthread_mutex_unlock(&stream->lock);
3473 }
3474
3475 if (relayd) {
3476 unsigned int is_data_inflight = 0;
3477
3478 /* Send init command for data pending. */
3479 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3480 ret = relayd_end_data_pending(&relayd->control_sock,
3481 relayd->relayd_session_id, &is_data_inflight);
3482 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3483 if (ret < 0) {
3484 goto data_not_pending;
3485 }
3486 if (is_data_inflight) {
3487 goto data_pending;
3488 }
3489 }
3490
3491 /*
3492 * Finding _no_ node in the hash table and no inflight data means that the
3493 * stream(s) have been removed thus data is guaranteed to be available for
3494 * analysis from the trace files.
3495 */
3496
3497 data_not_pending:
3498 /* Data is available to be read by a viewer. */
3499 pthread_mutex_unlock(&consumer_data.lock);
3500 rcu_read_unlock();
3501 return 0;
3502
3503 data_pending:
3504 /* Data is still being extracted from buffers. */
3505 pthread_mutex_unlock(&consumer_data.lock);
3506 rcu_read_unlock();
3507 return 1;
3508 }
3509
3510 /*
3511 * Send a ret code status message to the sessiond daemon.
3512 *
3513 * Return the sendmsg() return value.
3514 */
3515 int consumer_send_status_msg(int sock, int ret_code)
3516 {
3517 struct lttcomm_consumer_status_msg msg;
3518
3519 msg.ret_code = ret_code;
3520
3521 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3522 }
3523
3524 /*
3525 * Send a channel status message to the sessiond daemon.
3526 *
3527 * Return the sendmsg() return value.
3528 */
3529 int consumer_send_status_channel(int sock,
3530 struct lttng_consumer_channel *channel)
3531 {
3532 struct lttcomm_consumer_status_channel msg;
3533
3534 assert(sock >= 0);
3535
3536 if (!channel) {
3537 msg.ret_code = -LTTNG_ERR_UST_CHAN_FAIL;
3538 } else {
3539 msg.ret_code = LTTNG_OK;
3540 msg.key = channel->key;
3541 msg.stream_count = channel->streams.count;
3542 }
3543
3544 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3545 }
3546
3547 /*
3548 * Using a maximum stream size with the produced and consumed position of a
3549 * stream, computes the new consumed position to be as close as possible to the
3550 * maximum possible stream size.
3551 *
3552 * If maximum stream size is lower than the possible buffer size (produced -
3553 * consumed), the consumed_pos given is returned untouched else the new value
3554 * is returned.
3555 */
3556 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos,
3557 unsigned long produced_pos, uint64_t max_stream_size)
3558 {
3559 if (max_stream_size && max_stream_size < (produced_pos - consumed_pos)) {
3560 /* Offset from the produced position to get the latest buffers. */
3561 return produced_pos - max_stream_size;
3562 }
3563
3564 return consumed_pos;
3565 }
This page took 0.149278 seconds and 5 git commands to generate.