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