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