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