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