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