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