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