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