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