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