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