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