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