Make the consumer sends a ACK after each command
[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);
173af62f 751 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
752 /* Other fields are zeroed previously */
753
754 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
755 sizeof(data_hdr));
756 if (ret < 0) {
757 goto error;
758 }
759
760 /* Set to go on data socket */
761 outfd = relayd->data_sock.fd;
762 }
763
764error:
765 return outfd;
766}
767
702b1ea4
MD
768static
769void consumer_free_channel(struct rcu_head *head)
770{
771 struct lttng_ht_node_ulong *node =
772 caa_container_of(head, struct lttng_ht_node_ulong, head);
773 struct lttng_consumer_channel *channel =
774 caa_container_of(node, struct lttng_consumer_channel, node);
775
776 free(channel);
777}
778
3bd1e081
MD
779/*
780 * Remove a channel from the global list protected by a mutex. This
781 * function is also responsible for freeing its data structures.
782 */
783void consumer_del_channel(struct lttng_consumer_channel *channel)
784{
785 int ret;
e4421fec 786 struct lttng_ht_iter iter;
3bd1e081 787
5c540210
DG
788 DBG("Consumer delete channel key %d", channel->key);
789
3bd1e081
MD
790 pthread_mutex_lock(&consumer_data.lock);
791
792 switch (consumer_data.type) {
793 case LTTNG_CONSUMER_KERNEL:
794 break;
7753dea8
MD
795 case LTTNG_CONSUMER32_UST:
796 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
797 lttng_ustconsumer_del_channel(channel);
798 break;
799 default:
800 ERR("Unknown consumer_data type");
801 assert(0);
802 goto end;
803 }
804
6065ceec 805 rcu_read_lock();
04253271
MD
806 iter.iter.node = &channel->node.node;
807 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
808 assert(!ret);
6065ceec
DG
809 rcu_read_unlock();
810
3bd1e081
MD
811 if (channel->mmap_base != NULL) {
812 ret = munmap(channel->mmap_base, channel->mmap_len);
813 if (ret != 0) {
7a57cf92 814 PERROR("munmap");
3bd1e081
MD
815 }
816 }
b5c5fc29 817 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
818 ret = close(channel->wait_fd);
819 if (ret) {
820 PERROR("close");
821 }
3bd1e081 822 }
2c1dd183 823 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
824 ret = close(channel->shm_fd);
825 if (ret) {
826 PERROR("close");
827 }
3bd1e081 828 }
702b1ea4
MD
829
830 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
831end:
832 pthread_mutex_unlock(&consumer_data.lock);
833}
834
835struct lttng_consumer_channel *consumer_allocate_channel(
836 int channel_key,
837 int shm_fd, int wait_fd,
838 uint64_t mmap_len,
c30aaa51
MD
839 uint64_t max_sb_size,
840 unsigned int nb_init_streams)
3bd1e081
MD
841{
842 struct lttng_consumer_channel *channel;
843 int ret;
844
276b26d1 845 channel = zmalloc(sizeof(*channel));
3bd1e081 846 if (channel == NULL) {
7a57cf92 847 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
848 goto end;
849 }
850 channel->key = channel_key;
851 channel->shm_fd = shm_fd;
852 channel->wait_fd = wait_fd;
853 channel->mmap_len = mmap_len;
854 channel->max_sb_size = max_sb_size;
855 channel->refcount = 0;
c30aaa51 856 channel->nb_init_streams = nb_init_streams;
e4421fec 857 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
858
859 switch (consumer_data.type) {
860 case LTTNG_CONSUMER_KERNEL:
861 channel->mmap_base = NULL;
862 channel->mmap_len = 0;
863 break;
7753dea8
MD
864 case LTTNG_CONSUMER32_UST:
865 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
866 ret = lttng_ustconsumer_allocate_channel(channel);
867 if (ret) {
868 free(channel);
869 return NULL;
870 }
871 break;
872 default:
873 ERR("Unknown consumer_data type");
874 assert(0);
875 goto end;
876 }
877 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 878 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
879 (unsigned long long) channel->mmap_len,
880 (unsigned long long) channel->max_sb_size);
881end:
882 return channel;
883}
884
885/*
886 * Add a channel to the global list protected by a mutex.
887 */
888int consumer_add_channel(struct lttng_consumer_channel *channel)
889{
c77fc10a
DG
890 struct lttng_ht_node_ulong *node;
891 struct lttng_ht_iter iter;
892
3bd1e081 893 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
894 /* Steal channel identifier, for UST */
895 consumer_steal_channel_key(channel->key);
6065ceec 896 rcu_read_lock();
c77fc10a
DG
897
898 lttng_ht_lookup(consumer_data.channel_ht,
899 (void *)((unsigned long) channel->key), &iter);
900 node = lttng_ht_iter_get_node_ulong(&iter);
901 if (node != NULL) {
902 /* Channel already exist. Ignore the insertion */
903 goto end;
904 }
905
04253271 906 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
907
908end:
6065ceec 909 rcu_read_unlock();
3bd1e081 910 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 911
7ad0a0cb 912 return 0;
3bd1e081
MD
913}
914
915/*
916 * Allocate the pollfd structure and the local view of the out fds to avoid
917 * doing a lookup in the linked list and concurrency issues when writing is
918 * needed. Called with consumer_data.lock held.
919 *
920 * Returns the number of fds in the structures.
921 */
43c34bc3 922static int consumer_update_poll_array(
3bd1e081 923 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
43c34bc3 924 struct lttng_consumer_stream **local_stream, struct lttng_ht *ht)
3bd1e081 925{
3bd1e081 926 int i = 0;
e4421fec
DG
927 struct lttng_ht_iter iter;
928 struct lttng_consumer_stream *stream;
3bd1e081
MD
929
930 DBG("Updating poll fd array");
481d6c57 931 rcu_read_lock();
43c34bc3 932 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
8994307f
DG
933 /*
934 * Only active streams with an active end point can be added to the
935 * poll set and local stream storage of the thread.
936 *
937 * There is a potential race here for endpoint_status to be updated
938 * just after the check. However, this is OK since the stream(s) will
939 * be deleted once the thread is notified that the end point state has
940 * changed where this function will be called back again.
941 */
942 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
79d4ffb7 943 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
3bd1e081
MD
944 continue;
945 }
e4421fec
DG
946 DBG("Active FD %d", stream->wait_fd);
947 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 948 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 949 local_stream[i] = stream;
3bd1e081
MD
950 i++;
951 }
481d6c57 952 rcu_read_unlock();
3bd1e081
MD
953
954 /*
50f8ae69 955 * Insert the consumer_data_pipe at the end of the array and don't
3bd1e081
MD
956 * increment i so nb_fd is the number of real FD.
957 */
50f8ae69 958 (*pollfd)[i].fd = ctx->consumer_data_pipe[0];
509bb1cf 959 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
960 return i;
961}
962
963/*
964 * Poll on the should_quit pipe and the command socket return -1 on error and
965 * should exit, 0 if data is available on the command socket
966 */
967int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
968{
969 int num_rdy;
970
88f2b785 971restart:
3bd1e081
MD
972 num_rdy = poll(consumer_sockpoll, 2, -1);
973 if (num_rdy == -1) {
88f2b785
MD
974 /*
975 * Restart interrupted system call.
976 */
977 if (errno == EINTR) {
978 goto restart;
979 }
7a57cf92 980 PERROR("Poll error");
3bd1e081
MD
981 goto exit;
982 }
509bb1cf 983 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
984 DBG("consumer_should_quit wake up");
985 goto exit;
986 }
987 return 0;
988
989exit:
990 return -1;
991}
992
993/*
994 * Set the error socket.
995 */
996void lttng_consumer_set_error_sock(
997 struct lttng_consumer_local_data *ctx, int sock)
998{
999 ctx->consumer_error_socket = sock;
1000}
1001
1002/*
1003 * Set the command socket path.
1004 */
3bd1e081
MD
1005void lttng_consumer_set_command_sock_path(
1006 struct lttng_consumer_local_data *ctx, char *sock)
1007{
1008 ctx->consumer_command_sock_path = sock;
1009}
1010
1011/*
1012 * Send return code to the session daemon.
1013 * If the socket is not defined, we return 0, it is not a fatal error
1014 */
1015int lttng_consumer_send_error(
1016 struct lttng_consumer_local_data *ctx, int cmd)
1017{
1018 if (ctx->consumer_error_socket > 0) {
1019 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1020 sizeof(enum lttcomm_sessiond_command));
1021 }
1022
1023 return 0;
1024}
1025
1026/*
228b5bf7
DG
1027 * Close all the tracefiles and stream fds and MUST be called when all
1028 * instances are destroyed i.e. when all threads were joined and are ended.
3bd1e081
MD
1029 */
1030void lttng_consumer_cleanup(void)
1031{
e4421fec 1032 struct lttng_ht_iter iter;
6065ceec
DG
1033 struct lttng_ht_node_ulong *node;
1034
1035 rcu_read_lock();
3bd1e081 1036
6065ceec
DG
1037 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
1038 node) {
702b1ea4
MD
1039 struct lttng_consumer_channel *channel =
1040 caa_container_of(node, struct lttng_consumer_channel, node);
1041 consumer_del_channel(channel);
3bd1e081 1042 }
6065ceec
DG
1043
1044 rcu_read_unlock();
d6ce1df2 1045
d6ce1df2 1046 lttng_ht_destroy(consumer_data.channel_ht);
228b5bf7
DG
1047
1048 cleanup_relayd_ht();
1049
1050 /*
1051 * This HT contains streams that are freed by either the metadata thread or
1052 * the data thread so we do *nothing* on the hash table and simply destroy
1053 * it.
1054 */
1055 lttng_ht_destroy(consumer_data.stream_list_ht);
3bd1e081
MD
1056}
1057
1058/*
1059 * Called from signal handler.
1060 */
1061void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1062{
1063 int ret;
1064 consumer_quit = 1;
6f94560a
MD
1065 do {
1066 ret = write(ctx->consumer_should_quit[1], "4", 1);
1067 } while (ret < 0 && errno == EINTR);
3bd1e081 1068 if (ret < 0) {
7a57cf92 1069 PERROR("write consumer quit");
3bd1e081 1070 }
ab1027f4
DG
1071
1072 DBG("Consumer flag that it should quit");
3bd1e081
MD
1073}
1074
00e2e675
DG
1075void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1076 off_t orig_offset)
3bd1e081
MD
1077{
1078 int outfd = stream->out_fd;
1079
1080 /*
1081 * This does a blocking write-and-wait on any page that belongs to the
1082 * subbuffer prior to the one we just wrote.
1083 * Don't care about error values, as these are just hints and ways to
1084 * limit the amount of page cache used.
1085 */
1086 if (orig_offset < stream->chan->max_sb_size) {
1087 return;
1088 }
b9182dd9 1089 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
1090 stream->chan->max_sb_size,
1091 SYNC_FILE_RANGE_WAIT_BEFORE
1092 | SYNC_FILE_RANGE_WRITE
1093 | SYNC_FILE_RANGE_WAIT_AFTER);
1094 /*
1095 * Give hints to the kernel about how we access the file:
1096 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1097 * we write it.
1098 *
1099 * We need to call fadvise again after the file grows because the
1100 * kernel does not seem to apply fadvise to non-existing parts of the
1101 * file.
1102 *
1103 * Call fadvise _after_ having waited for the page writeback to
1104 * complete because the dirty page writeback semantic is not well
1105 * defined. So it can be expected to lead to lower throughput in
1106 * streaming.
1107 */
1108 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
1109 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
1110}
1111
1112/*
1113 * Initialise the necessary environnement :
1114 * - create a new context
1115 * - create the poll_pipe
1116 * - create the should_quit pipe (for signal handler)
1117 * - create the thread pipe (for splice)
1118 *
1119 * Takes a function pointer as argument, this function is called when data is
1120 * available on a buffer. This function is responsible to do the
1121 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1122 * buffer configuration and then kernctl_put_next_subbuf at the end.
1123 *
1124 * Returns a pointer to the new context or NULL on error.
1125 */
1126struct lttng_consumer_local_data *lttng_consumer_create(
1127 enum lttng_consumer_type type,
4078b776 1128 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 1129 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
1130 int (*recv_channel)(struct lttng_consumer_channel *channel),
1131 int (*recv_stream)(struct lttng_consumer_stream *stream),
1132 int (*update_stream)(int stream_key, uint32_t state))
1133{
1134 int ret, i;
1135 struct lttng_consumer_local_data *ctx;
1136
1137 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1138 consumer_data.type == type);
1139 consumer_data.type = type;
1140
effcf122 1141 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 1142 if (ctx == NULL) {
7a57cf92 1143 PERROR("allocating context");
3bd1e081
MD
1144 goto error;
1145 }
1146
1147 ctx->consumer_error_socket = -1;
1148 /* assign the callbacks */
1149 ctx->on_buffer_ready = buffer_ready;
1150 ctx->on_recv_channel = recv_channel;
1151 ctx->on_recv_stream = recv_stream;
1152 ctx->on_update_stream = update_stream;
1153
50f8ae69 1154 ret = pipe(ctx->consumer_data_pipe);
3bd1e081 1155 if (ret < 0) {
7a57cf92 1156 PERROR("Error creating poll pipe");
3bd1e081
MD
1157 goto error_poll_pipe;
1158 }
1159
04fdd819 1160 /* set read end of the pipe to non-blocking */
50f8ae69 1161 ret = fcntl(ctx->consumer_data_pipe[0], F_SETFL, O_NONBLOCK);
04fdd819 1162 if (ret < 0) {
7a57cf92 1163 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1164 goto error_poll_fcntl;
1165 }
1166
1167 /* set write end of the pipe to non-blocking */
50f8ae69 1168 ret = fcntl(ctx->consumer_data_pipe[1], F_SETFL, O_NONBLOCK);
04fdd819 1169 if (ret < 0) {
7a57cf92 1170 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1171 goto error_poll_fcntl;
1172 }
1173
3bd1e081
MD
1174 ret = pipe(ctx->consumer_should_quit);
1175 if (ret < 0) {
7a57cf92 1176 PERROR("Error creating recv pipe");
3bd1e081
MD
1177 goto error_quit_pipe;
1178 }
1179
1180 ret = pipe(ctx->consumer_thread_pipe);
1181 if (ret < 0) {
7a57cf92 1182 PERROR("Error creating thread pipe");
3bd1e081
MD
1183 goto error_thread_pipe;
1184 }
1185
fb3a43a9
DG
1186 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1187 if (ret < 0) {
1188 goto error_metadata_pipe;
1189 }
3bd1e081 1190
fb3a43a9
DG
1191 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1192 if (ret < 0) {
1193 goto error_splice_pipe;
1194 }
1195
1196 return ctx;
3bd1e081 1197
fb3a43a9
DG
1198error_splice_pipe:
1199 utils_close_pipe(ctx->consumer_metadata_pipe);
1200error_metadata_pipe:
1201 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1202error_thread_pipe:
1203 for (i = 0; i < 2; i++) {
1204 int err;
1205
1206 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1207 if (err) {
1208 PERROR("close");
1209 }
3bd1e081 1210 }
04fdd819 1211error_poll_fcntl:
3bd1e081
MD
1212error_quit_pipe:
1213 for (i = 0; i < 2; i++) {
1214 int err;
1215
50f8ae69 1216 err = close(ctx->consumer_data_pipe[i]);
4c462e79
MD
1217 if (err) {
1218 PERROR("close");
1219 }
3bd1e081
MD
1220 }
1221error_poll_pipe:
1222 free(ctx);
1223error:
1224 return NULL;
1225}
1226
1227/*
1228 * Close all fds associated with the instance and free the context.
1229 */
1230void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1231{
4c462e79
MD
1232 int ret;
1233
ab1027f4
DG
1234 DBG("Consumer destroying it. Closing everything.");
1235
4c462e79
MD
1236 ret = close(ctx->consumer_error_socket);
1237 if (ret) {
1238 PERROR("close");
1239 }
1240 ret = close(ctx->consumer_thread_pipe[0]);
1241 if (ret) {
1242 PERROR("close");
1243 }
1244 ret = close(ctx->consumer_thread_pipe[1]);
1245 if (ret) {
1246 PERROR("close");
1247 }
50f8ae69 1248 ret = close(ctx->consumer_data_pipe[0]);
4c462e79
MD
1249 if (ret) {
1250 PERROR("close");
1251 }
50f8ae69 1252 ret = close(ctx->consumer_data_pipe[1]);
4c462e79
MD
1253 if (ret) {
1254 PERROR("close");
1255 }
1256 ret = close(ctx->consumer_should_quit[0]);
1257 if (ret) {
1258 PERROR("close");
1259 }
1260 ret = close(ctx->consumer_should_quit[1]);
1261 if (ret) {
1262 PERROR("close");
1263 }
fb3a43a9
DG
1264 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1265
3bd1e081
MD
1266 unlink(ctx->consumer_command_sock_path);
1267 free(ctx);
1268}
1269
6197aea7
DG
1270/*
1271 * Write the metadata stream id on the specified file descriptor.
1272 */
1273static int write_relayd_metadata_id(int fd,
1274 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1275 struct consumer_relayd_sock_pair *relayd,
1276 unsigned long padding)
6197aea7
DG
1277{
1278 int ret;
1d4dfdef 1279 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1280
1d4dfdef
DG
1281 hdr.stream_id = htobe64(stream->relayd_stream_id);
1282 hdr.padding_size = htobe32(padding);
6197aea7 1283 do {
1d4dfdef 1284 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1285 } while (ret < 0 && errno == EINTR);
1286 if (ret < 0) {
1287 PERROR("write metadata stream id");
1288 goto end;
1289 }
1d4dfdef
DG
1290 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1291 stream->relayd_stream_id, padding);
6197aea7
DG
1292
1293end:
1294 return ret;
1295}
1296
3bd1e081 1297/*
09e26845
DG
1298 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1299 * core function for writing trace buffers to either the local filesystem or
1300 * the network.
1301 *
79d4ffb7
DG
1302 * It must be called with the stream lock held.
1303 *
09e26845 1304 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1305 *
1306 * Returns the number of bytes written
1307 */
4078b776 1308ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1309 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1310 struct lttng_consumer_stream *stream, unsigned long len,
1311 unsigned long padding)
3bd1e081 1312{
f02e1e8a
DG
1313 unsigned long mmap_offset;
1314 ssize_t ret = 0, written = 0;
1315 off_t orig_offset = stream->out_fd_offset;
1316 /* Default is on the disk */
1317 int outfd = stream->out_fd;
f02e1e8a 1318 struct consumer_relayd_sock_pair *relayd = NULL;
8994307f 1319 unsigned int relayd_hang_up = 0;
f02e1e8a
DG
1320
1321 /* RCU lock for the relayd pointer */
1322 rcu_read_lock();
1323
1324 /* Flag that the current stream if set for network streaming. */
1325 if (stream->net_seq_idx != -1) {
1326 relayd = consumer_find_relayd(stream->net_seq_idx);
1327 if (relayd == NULL) {
1328 goto end;
1329 }
1330 }
1331
1332 /* get the offset inside the fd to mmap */
3bd1e081
MD
1333 switch (consumer_data.type) {
1334 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1335 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1336 break;
7753dea8
MD
1337 case LTTNG_CONSUMER32_UST:
1338 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1339 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1340 stream->buf, &mmap_offset);
1341 break;
3bd1e081
MD
1342 default:
1343 ERR("Unknown consumer_data type");
1344 assert(0);
1345 }
f02e1e8a
DG
1346 if (ret != 0) {
1347 errno = -ret;
1348 PERROR("tracer ctl get_mmap_read_offset");
1349 written = ret;
1350 goto end;
1351 }
b9182dd9 1352
f02e1e8a
DG
1353 /* Handle stream on the relayd if the output is on the network */
1354 if (relayd) {
1355 unsigned long netlen = len;
1356
1357 /*
1358 * Lock the control socket for the complete duration of the function
1359 * since from this point on we will use the socket.
1360 */
1361 if (stream->metadata_flag) {
1362 /* Metadata requires the control socket. */
1363 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1364 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1365 }
1366
1d4dfdef 1367 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1368 if (ret >= 0) {
1369 /* Use the returned socket. */
1370 outfd = ret;
1371
1372 /* Write metadata stream id before payload */
1373 if (stream->metadata_flag) {
1d4dfdef 1374 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1375 if (ret < 0) {
f02e1e8a 1376 written = ret;
8994307f
DG
1377 /* Socket operation failed. We consider the relayd dead */
1378 if (ret == -EPIPE || ret == -EINVAL) {
1379 relayd_hang_up = 1;
1380 goto write_error;
1381 }
f02e1e8a
DG
1382 goto end;
1383 }
f02e1e8a 1384 }
8994307f
DG
1385 } else {
1386 /* Socket operation failed. We consider the relayd dead */
1387 if (ret == -EPIPE || ret == -EINVAL) {
1388 relayd_hang_up = 1;
1389 goto write_error;
1390 }
1391 /* Else, use the default set before which is the filesystem. */
f02e1e8a 1392 }
1d4dfdef
DG
1393 } else {
1394 /* No streaming, we have to set the len with the full padding */
1395 len += padding;
f02e1e8a
DG
1396 }
1397
1398 while (len > 0) {
1399 do {
1400 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1401 } while (ret < 0 && errno == EINTR);
1d4dfdef 1402 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a
DG
1403 if (ret < 0) {
1404 PERROR("Error in file write");
1405 if (written == 0) {
1406 written = ret;
1407 }
8994307f
DG
1408 /* Socket operation failed. We consider the relayd dead */
1409 if (errno == EPIPE || errno == EINVAL) {
1410 relayd_hang_up = 1;
1411 goto write_error;
1412 }
f02e1e8a
DG
1413 goto end;
1414 } else if (ret > len) {
77c7c900 1415 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1416 written += ret;
1417 goto end;
1418 } else {
1419 len -= ret;
1420 mmap_offset += ret;
1421 }
f02e1e8a
DG
1422
1423 /* This call is useless on a socket so better save a syscall. */
1424 if (!relayd) {
1425 /* This won't block, but will start writeout asynchronously */
1426 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1427 SYNC_FILE_RANGE_WRITE);
1428 stream->out_fd_offset += ret;
1429 }
1430 written += ret;
1431 }
1432 lttng_consumer_sync_trace_file(stream, orig_offset);
1433
8994307f
DG
1434write_error:
1435 /*
1436 * This is a special case that the relayd has closed its socket. Let's
1437 * cleanup the relayd object and all associated streams.
1438 */
1439 if (relayd && relayd_hang_up) {
1440 cleanup_relayd(relayd, ctx);
1441 }
1442
f02e1e8a
DG
1443end:
1444 /* Unlock only if ctrl socket used */
1445 if (relayd && stream->metadata_flag) {
1446 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1447 }
1448
1449 rcu_read_unlock();
1450 return written;
3bd1e081
MD
1451}
1452
1453/*
1454 * Splice the data from the ring buffer to the tracefile.
1455 *
79d4ffb7
DG
1456 * It must be called with the stream lock held.
1457 *
3bd1e081
MD
1458 * Returns the number of bytes spliced.
1459 */
4078b776 1460ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1461 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1462 struct lttng_consumer_stream *stream, unsigned long len,
1463 unsigned long padding)
3bd1e081 1464{
f02e1e8a
DG
1465 ssize_t ret = 0, written = 0, ret_splice = 0;
1466 loff_t offset = 0;
1467 off_t orig_offset = stream->out_fd_offset;
1468 int fd = stream->wait_fd;
1469 /* Default is on the disk */
1470 int outfd = stream->out_fd;
f02e1e8a 1471 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1472 int *splice_pipe;
8994307f 1473 unsigned int relayd_hang_up = 0;
f02e1e8a 1474
3bd1e081
MD
1475 switch (consumer_data.type) {
1476 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1477 break;
7753dea8
MD
1478 case LTTNG_CONSUMER32_UST:
1479 case LTTNG_CONSUMER64_UST:
f02e1e8a 1480 /* Not supported for user space tracing */
3bd1e081
MD
1481 return -ENOSYS;
1482 default:
1483 ERR("Unknown consumer_data type");
1484 assert(0);
3bd1e081
MD
1485 }
1486
f02e1e8a
DG
1487 /* RCU lock for the relayd pointer */
1488 rcu_read_lock();
1489
1490 /* Flag that the current stream if set for network streaming. */
1491 if (stream->net_seq_idx != -1) {
1492 relayd = consumer_find_relayd(stream->net_seq_idx);
1493 if (relayd == NULL) {
1494 goto end;
1495 }
1496 }
1497
fb3a43a9
DG
1498 /*
1499 * Choose right pipe for splice. Metadata and trace data are handled by
1500 * different threads hence the use of two pipes in order not to race or
1501 * corrupt the written data.
1502 */
1503 if (stream->metadata_flag) {
1504 splice_pipe = ctx->consumer_splice_metadata_pipe;
1505 } else {
1506 splice_pipe = ctx->consumer_thread_pipe;
1507 }
1508
f02e1e8a 1509 /* Write metadata stream id before payload */
1d4dfdef
DG
1510 if (relayd) {
1511 int total_len = len;
f02e1e8a 1512
1d4dfdef
DG
1513 if (stream->metadata_flag) {
1514 /*
1515 * Lock the control socket for the complete duration of the function
1516 * since from this point on we will use the socket.
1517 */
1518 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1519
1520 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1521 padding);
1522 if (ret < 0) {
1523 written = ret;
8994307f
DG
1524 /* Socket operation failed. We consider the relayd dead */
1525 if (ret == -EBADF) {
1526 WARN("Remote relayd disconnected. Stopping");
1527 relayd_hang_up = 1;
1528 goto write_error;
1529 }
1d4dfdef
DG
1530 goto end;
1531 }
1532
1533 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1534 }
1535
1536 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1537 if (ret >= 0) {
1538 /* Use the returned socket. */
1539 outfd = ret;
1540 } else {
8994307f
DG
1541 /* Socket operation failed. We consider the relayd dead */
1542 if (ret == -EBADF) {
1543 WARN("Remote relayd disconnected. Stopping");
1544 relayd_hang_up = 1;
1545 goto write_error;
1546 }
f02e1e8a
DG
1547 goto end;
1548 }
1d4dfdef
DG
1549 } else {
1550 /* No streaming, we have to set the len with the full padding */
1551 len += padding;
f02e1e8a
DG
1552 }
1553
1554 while (len > 0) {
1d4dfdef
DG
1555 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1556 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1557 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1558 SPLICE_F_MOVE | SPLICE_F_MORE);
1559 DBG("splice chan to pipe, ret %zd", ret_splice);
1560 if (ret_splice < 0) {
1561 PERROR("Error in relay splice");
1562 if (written == 0) {
1563 written = ret_splice;
1564 }
1565 ret = errno;
1566 goto splice_error;
1567 }
1568
1569 /* Handle stream on the relayd if the output is on the network */
1570 if (relayd) {
1571 if (stream->metadata_flag) {
1d4dfdef
DG
1572 size_t metadata_payload_size =
1573 sizeof(struct lttcomm_relayd_metadata_payload);
1574
f02e1e8a 1575 /* Update counter to fit the spliced data */
1d4dfdef
DG
1576 ret_splice += metadata_payload_size;
1577 len += metadata_payload_size;
f02e1e8a
DG
1578 /*
1579 * We do this so the return value can match the len passed as
1580 * argument to this function.
1581 */
1d4dfdef 1582 written -= metadata_payload_size;
f02e1e8a
DG
1583 }
1584 }
1585
1586 /* Splice data out */
fb3a43a9 1587 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1588 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1589 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1590 if (ret_splice < 0) {
1591 PERROR("Error in file splice");
1592 if (written == 0) {
1593 written = ret_splice;
1594 }
8994307f 1595 /* Socket operation failed. We consider the relayd dead */
00c8752b 1596 if (errno == EBADF || errno == EPIPE) {
8994307f
DG
1597 WARN("Remote relayd disconnected. Stopping");
1598 relayd_hang_up = 1;
1599 goto write_error;
1600 }
f02e1e8a
DG
1601 ret = errno;
1602 goto splice_error;
1603 } else if (ret_splice > len) {
1604 errno = EINVAL;
1605 PERROR("Wrote more data than requested %zd (len: %lu)",
1606 ret_splice, len);
1607 written += ret_splice;
1608 ret = errno;
1609 goto splice_error;
1610 }
1611 len -= ret_splice;
1612
1613 /* This call is useless on a socket so better save a syscall. */
1614 if (!relayd) {
1615 /* This won't block, but will start writeout asynchronously */
1616 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1617 SYNC_FILE_RANGE_WRITE);
1618 stream->out_fd_offset += ret_splice;
1619 }
1620 written += ret_splice;
1621 }
1622 lttng_consumer_sync_trace_file(stream, orig_offset);
1623
1624 ret = ret_splice;
1625
1626 goto end;
1627
8994307f
DG
1628write_error:
1629 /*
1630 * This is a special case that the relayd has closed its socket. Let's
1631 * cleanup the relayd object and all associated streams.
1632 */
1633 if (relayd && relayd_hang_up) {
1634 cleanup_relayd(relayd, ctx);
1635 /* Skip splice error so the consumer does not fail */
1636 goto end;
1637 }
1638
f02e1e8a
DG
1639splice_error:
1640 /* send the appropriate error description to sessiond */
1641 switch (ret) {
f02e1e8a 1642 case EINVAL:
f73fabfd 1643 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1644 break;
1645 case ENOMEM:
f73fabfd 1646 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1647 break;
1648 case ESPIPE:
f73fabfd 1649 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1650 break;
1651 }
1652
1653end:
1654 if (relayd && stream->metadata_flag) {
1655 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1656 }
1657
1658 rcu_read_unlock();
1659 return written;
3bd1e081
MD
1660}
1661
1662/*
1663 * Take a snapshot for a specific fd
1664 *
1665 * Returns 0 on success, < 0 on error
1666 */
1667int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1668 struct lttng_consumer_stream *stream)
1669{
1670 switch (consumer_data.type) {
1671 case LTTNG_CONSUMER_KERNEL:
1672 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1673 case LTTNG_CONSUMER32_UST:
1674 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1675 return lttng_ustconsumer_take_snapshot(ctx, stream);
1676 default:
1677 ERR("Unknown consumer_data type");
1678 assert(0);
1679 return -ENOSYS;
1680 }
1681
1682}
1683
1684/*
1685 * Get the produced position
1686 *
1687 * Returns 0 on success, < 0 on error
1688 */
1689int lttng_consumer_get_produced_snapshot(
1690 struct lttng_consumer_local_data *ctx,
1691 struct lttng_consumer_stream *stream,
1692 unsigned long *pos)
1693{
1694 switch (consumer_data.type) {
1695 case LTTNG_CONSUMER_KERNEL:
1696 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1697 case LTTNG_CONSUMER32_UST:
1698 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1699 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1700 default:
1701 ERR("Unknown consumer_data type");
1702 assert(0);
1703 return -ENOSYS;
1704 }
1705}
1706
1707int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1708 int sock, struct pollfd *consumer_sockpoll)
1709{
1710 switch (consumer_data.type) {
1711 case LTTNG_CONSUMER_KERNEL:
1712 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1713 case LTTNG_CONSUMER32_UST:
1714 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1715 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1716 default:
1717 ERR("Unknown consumer_data type");
1718 assert(0);
1719 return -ENOSYS;
1720 }
1721}
1722
43c34bc3
DG
1723/*
1724 * Iterate over all streams of the hashtable and free them properly.
1725 *
1726 * WARNING: *MUST* be used with data stream only.
1727 */
1728static void destroy_data_stream_ht(struct lttng_ht *ht)
1729{
43c34bc3
DG
1730 struct lttng_ht_iter iter;
1731 struct lttng_consumer_stream *stream;
1732
1733 if (ht == NULL) {
1734 return;
1735 }
1736
1737 rcu_read_lock();
1738 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
5c540210
DG
1739 /*
1740 * Ignore return value since we are currently cleaning up so any error
1741 * can't be handled.
1742 */
1743 (void) consumer_del_stream(stream, ht);
43c34bc3
DG
1744 }
1745 rcu_read_unlock();
1746
1747 lttng_ht_destroy(ht);
1748}
1749
fb3a43a9 1750/*
f724d81e 1751 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1752 *
1753 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1754 */
1755static void destroy_stream_ht(struct lttng_ht *ht)
1756{
fb3a43a9
DG
1757 struct lttng_ht_iter iter;
1758 struct lttng_consumer_stream *stream;
1759
1760 if (ht == NULL) {
1761 return;
1762 }
1763
d09e1200 1764 rcu_read_lock();
58b1f425 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_metadata_stream(stream, ht);
fb3a43a9 1771 }
d09e1200 1772 rcu_read_unlock();
fb3a43a9
DG
1773
1774 lttng_ht_destroy(ht);
1775}
1776
1777/*
1778 * Clean up a metadata stream and free its memory.
1779 */
e316aad5
DG
1780void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1781 struct lttng_ht *ht)
fb3a43a9
DG
1782{
1783 int ret;
e316aad5
DG
1784 struct lttng_ht_iter iter;
1785 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1786 struct consumer_relayd_sock_pair *relayd;
1787
1788 assert(stream);
1789 /*
1790 * This call should NEVER receive regular stream. It must always be
1791 * metadata stream and this is crucial for data structure synchronization.
1792 */
1793 assert(stream->metadata_flag);
1794
e316aad5
DG
1795 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1796
1797 if (ht == NULL) {
1798 /* Means the stream was allocated but not successfully added */
1799 goto free_stream;
1800 }
1801
74251bb8 1802 pthread_mutex_lock(&consumer_data.lock);
8994307f
DG
1803 pthread_mutex_lock(&stream->lock);
1804
fb3a43a9
DG
1805 switch (consumer_data.type) {
1806 case LTTNG_CONSUMER_KERNEL:
1807 if (stream->mmap_base != NULL) {
1808 ret = munmap(stream->mmap_base, stream->mmap_len);
1809 if (ret != 0) {
1810 PERROR("munmap metadata stream");
1811 }
1812 }
1813 break;
1814 case LTTNG_CONSUMER32_UST:
1815 case LTTNG_CONSUMER64_UST:
1816 lttng_ustconsumer_del_stream(stream);
1817 break;
1818 default:
1819 ERR("Unknown consumer_data type");
1820 assert(0);
e316aad5 1821 goto end;
fb3a43a9 1822 }
fb3a43a9 1823
c869f647 1824 rcu_read_lock();
58b1f425 1825 iter.iter.node = &stream->node.node;
c869f647
DG
1826 ret = lttng_ht_del(ht, &iter);
1827 assert(!ret);
ca22feea
DG
1828
1829 /* Remove node session id from the consumer_data stream ht */
1830 iter.iter.node = &stream->node_session_id.node;
1831 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1832 assert(!ret);
c869f647
DG
1833 rcu_read_unlock();
1834
fb3a43a9
DG
1835 if (stream->out_fd >= 0) {
1836 ret = close(stream->out_fd);
1837 if (ret) {
1838 PERROR("close");
1839 }
1840 }
1841
1842 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1843 ret = close(stream->wait_fd);
1844 if (ret) {
1845 PERROR("close");
1846 }
1847 }
1848
1849 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1850 ret = close(stream->shm_fd);
1851 if (ret) {
1852 PERROR("close");
1853 }
1854 }
1855
1856 /* Check and cleanup relayd */
1857 rcu_read_lock();
1858 relayd = consumer_find_relayd(stream->net_seq_idx);
1859 if (relayd != NULL) {
1860 uatomic_dec(&relayd->refcount);
1861 assert(uatomic_read(&relayd->refcount) >= 0);
1862
1863 /* Closing streams requires to lock the control socket. */
1864 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1865 ret = relayd_send_close_stream(&relayd->control_sock,
1866 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1867 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1868 if (ret < 0) {
1869 DBG("Unable to close stream on the relayd. Continuing");
1870 /*
1871 * Continue here. There is nothing we can do for the relayd.
1872 * Chances are that the relayd has closed the socket so we just
1873 * continue cleaning up.
1874 */
1875 }
1876
1877 /* Both conditions are met, we destroy the relayd. */
1878 if (uatomic_read(&relayd->refcount) == 0 &&
1879 uatomic_read(&relayd->destroy_flag)) {
d09e1200 1880 destroy_relayd(relayd);
fb3a43a9
DG
1881 }
1882 }
1883 rcu_read_unlock();
1884
1885 /* Atomically decrement channel refcount since other threads can use it. */
1886 uatomic_dec(&stream->chan->refcount);
c30aaa51
MD
1887 if (!uatomic_read(&stream->chan->refcount)
1888 && !uatomic_read(&stream->chan->nb_init_streams)) {
1889 /* Go for channel deletion! */
e316aad5 1890 free_chan = stream->chan;
fb3a43a9
DG
1891 }
1892
e316aad5 1893end:
8994307f 1894 pthread_mutex_unlock(&stream->lock);
74251bb8 1895 pthread_mutex_unlock(&consumer_data.lock);
e316aad5
DG
1896
1897 if (free_chan) {
1898 consumer_del_channel(free_chan);
1899 }
1900
1901free_stream:
58b1f425 1902 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9
DG
1903}
1904
1905/*
1906 * Action done with the metadata stream when adding it to the consumer internal
1907 * data structures to handle it.
1908 */
e316aad5
DG
1909static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1910 struct lttng_ht *ht)
fb3a43a9 1911{
e316aad5 1912 int ret = 0;
fb3a43a9 1913 struct consumer_relayd_sock_pair *relayd;
76082088
DG
1914 struct lttng_ht_iter iter;
1915 struct lttng_ht_node_ulong *node;
fb3a43a9 1916
e316aad5
DG
1917 assert(stream);
1918 assert(ht);
1919
1920 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1921
1922 pthread_mutex_lock(&consumer_data.lock);
2e818a6a 1923 pthread_mutex_lock(&stream->lock);
e316aad5 1924
e316aad5
DG
1925 /*
1926 * From here, refcounts are updated so be _careful_ when returning an error
1927 * after this point.
1928 */
1929
fb3a43a9 1930 rcu_read_lock();
76082088
DG
1931
1932 /*
1933 * Lookup the stream just to make sure it does not exist in our internal
1934 * state. This should NEVER happen.
1935 */
1936 lttng_ht_lookup(ht, (void *)((unsigned long) stream->wait_fd), &iter);
1937 node = lttng_ht_iter_get_node_ulong(&iter);
1938 assert(!node);
1939
e316aad5 1940 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
1941 relayd = consumer_find_relayd(stream->net_seq_idx);
1942 if (relayd != NULL) {
1943 uatomic_inc(&relayd->refcount);
1944 }
e316aad5
DG
1945
1946 /* Update channel refcount once added without error(s). */
1947 uatomic_inc(&stream->chan->refcount);
1948
1949 /*
1950 * When nb_init_streams reaches 0, we don't need to trigger any action in
1951 * terms of destroying the associated channel, because the action that
1952 * causes the count to become 0 also causes a stream to be added. The
1953 * channel deletion will thus be triggered by the following removal of this
1954 * stream.
1955 */
1956 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
1957 uatomic_dec(&stream->chan->nb_init_streams);
1958 }
1959
58b1f425 1960 lttng_ht_add_unique_ulong(ht, &stream->node);
ca22feea
DG
1961
1962 /*
1963 * Add stream to the stream_list_ht of the consumer data. No need to steal
1964 * the key since the HT does not use it and we allow to add redundant keys
1965 * into this table.
1966 */
1967 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
1968
fb3a43a9 1969 rcu_read_unlock();
e316aad5 1970
2e818a6a 1971 pthread_mutex_unlock(&stream->lock);
e316aad5
DG
1972 pthread_mutex_unlock(&consumer_data.lock);
1973 return ret;
fb3a43a9
DG
1974}
1975
8994307f
DG
1976/*
1977 * Delete data stream that are flagged for deletion (endpoint_status).
1978 */
1979static void validate_endpoint_status_data_stream(void)
1980{
1981 struct lttng_ht_iter iter;
1982 struct lttng_consumer_stream *stream;
1983
1984 DBG("Consumer delete flagged data stream");
1985
1986 rcu_read_lock();
1987 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
1988 /* Validate delete flag of the stream */
79d4ffb7 1989 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
1990 continue;
1991 }
1992 /* Delete it right now */
1993 consumer_del_stream(stream, data_ht);
1994 }
1995 rcu_read_unlock();
1996}
1997
1998/*
1999 * Delete metadata stream that are flagged for deletion (endpoint_status).
2000 */
2001static void validate_endpoint_status_metadata_stream(
2002 struct lttng_poll_event *pollset)
2003{
2004 struct lttng_ht_iter iter;
2005 struct lttng_consumer_stream *stream;
2006
2007 DBG("Consumer delete flagged metadata stream");
2008
2009 assert(pollset);
2010
2011 rcu_read_lock();
2012 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2013 /* Validate delete flag of the stream */
79d4ffb7 2014 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2015 continue;
2016 }
2017 /*
2018 * Remove from pollset so the metadata thread can continue without
2019 * blocking on a deleted stream.
2020 */
2021 lttng_poll_del(pollset, stream->wait_fd);
2022
2023 /* Delete it right now */
2024 consumer_del_metadata_stream(stream, metadata_ht);
2025 }
2026 rcu_read_unlock();
2027}
2028
fb3a43a9
DG
2029/*
2030 * Thread polls on metadata file descriptor and write them on disk or on the
2031 * network.
2032 */
7d980def 2033void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
2034{
2035 int ret, i, pollfd;
2036 uint32_t revents, nb_fd;
e316aad5 2037 struct lttng_consumer_stream *stream = NULL;
fb3a43a9
DG
2038 struct lttng_ht_iter iter;
2039 struct lttng_ht_node_ulong *node;
fb3a43a9
DG
2040 struct lttng_poll_event events;
2041 struct lttng_consumer_local_data *ctx = data;
2042 ssize_t len;
2043
2044 rcu_register_thread();
2045
04bb2b64
DG
2046 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2047 if (!metadata_ht) {
2048 /* ENOMEM at this point. Better to bail out. */
2049 goto error;
2050 }
2051
fb3a43a9
DG
2052 DBG("Thread metadata poll started");
2053
fb3a43a9
DG
2054 /* Size is set to 1 for the consumer_metadata pipe */
2055 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2056 if (ret < 0) {
2057 ERR("Poll set creation failed");
2058 goto end;
2059 }
2060
2061 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
2062 if (ret < 0) {
2063 goto end;
2064 }
2065
2066 /* Main loop */
2067 DBG("Metadata main loop started");
2068
2069 while (1) {
2070 lttng_poll_reset(&events);
2071
2072 nb_fd = LTTNG_POLL_GETNB(&events);
2073
2074 /* Only the metadata pipe is set */
2075 if (nb_fd == 0 && consumer_quit == 1) {
2076 goto end;
2077 }
2078
2079restart:
2080 DBG("Metadata poll wait with %d fd(s)", nb_fd);
2081 ret = lttng_poll_wait(&events, -1);
2082 DBG("Metadata event catched in thread");
2083 if (ret < 0) {
2084 if (errno == EINTR) {
e316aad5 2085 ERR("Poll EINTR catched");
fb3a43a9
DG
2086 goto restart;
2087 }
2088 goto error;
2089 }
2090
e316aad5 2091 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
2092 for (i = 0; i < nb_fd; i++) {
2093 revents = LTTNG_POLL_GETEV(&events, i);
2094 pollfd = LTTNG_POLL_GETFD(&events, i);
2095
e316aad5
DG
2096 /* Just don't waste time if no returned events for the fd */
2097 if (!revents) {
2098 continue;
2099 }
2100
fb3a43a9 2101 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 2102 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
2103 DBG("Metadata thread pipe hung up");
2104 /*
2105 * Remove the pipe from the poll set and continue the loop
2106 * since their might be data to consume.
2107 */
2108 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
f66c074c
DG
2109 ret = close(ctx->consumer_metadata_pipe[0]);
2110 if (ret < 0) {
2111 PERROR("close metadata pipe");
2112 }
fb3a43a9
DG
2113 continue;
2114 } else if (revents & LPOLLIN) {
fb3a43a9 2115 do {
633d0084
DG
2116 /* Get the stream pointer received */
2117 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 2118 } while (ret < 0 && errno == EINTR);
633d0084
DG
2119 if (ret < 0 ||
2120 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 2121 PERROR("read metadata stream");
fb3a43a9
DG
2122 /*
2123 * Let's continue here and hope we can still work
2124 * without stopping the consumer. XXX: Should we?
2125 */
2126 continue;
2127 }
2128
8994307f
DG
2129 /* A NULL stream means that the state has changed. */
2130 if (stream == NULL) {
2131 /* Check for deleted streams. */
2132 validate_endpoint_status_metadata_stream(&events);
2133 continue;
2134 }
2135
fb3a43a9
DG
2136 DBG("Adding metadata stream %d to poll set",
2137 stream->wait_fd);
2138
e316aad5
DG
2139 ret = consumer_add_metadata_stream(stream, metadata_ht);
2140 if (ret) {
2141 ERR("Unable to add metadata stream");
2142 /* Stream was not setup properly. Continuing. */
2143 consumer_del_metadata_stream(stream, NULL);
2144 continue;
2145 }
fb3a43a9
DG
2146
2147 /* Add metadata stream to the global poll events list */
2148 lttng_poll_add(&events, stream->wait_fd,
2149 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
2150 }
2151
e316aad5 2152 /* Handle other stream */
fb3a43a9
DG
2153 continue;
2154 }
2155
d09e1200 2156 rcu_read_lock();
fb3a43a9
DG
2157 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
2158 &iter);
2159 node = lttng_ht_iter_get_node_ulong(&iter);
e316aad5 2160 assert(node);
fb3a43a9
DG
2161
2162 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 2163 node);
fb3a43a9 2164
e316aad5 2165 /* Check for error event */
4adabd61 2166 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 2167 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
2168 if (!stream->hangup_flush_done
2169 && (consumer_data.type == LTTNG_CONSUMER32_UST
2170 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2171 DBG("Attempting to flush and consume the UST buffers");
2172 lttng_ustconsumer_on_stream_hangup(stream);
2173
2174 /* We just flushed the stream now read it. */
4bb94b75
DG
2175 do {
2176 len = ctx->on_buffer_ready(stream, ctx);
2177 /*
2178 * We don't check the return value here since if we get
2179 * a negative len, it means an error occured thus we
2180 * simply remove it from the poll set and free the
2181 * stream.
2182 */
2183 } while (len > 0);
fb3a43a9
DG
2184 }
2185
fb3a43a9 2186 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
2187 /*
2188 * This call update the channel states, closes file descriptors
2189 * and securely free the stream.
2190 */
2191 consumer_del_metadata_stream(stream, metadata_ht);
2192 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2193 /* Get the data out of the metadata file descriptor */
2194 DBG("Metadata available on fd %d", pollfd);
2195 assert(stream->wait_fd == pollfd);
2196
2197 len = ctx->on_buffer_ready(stream, ctx);
2198 /* It's ok to have an unavailable sub-buffer */
b64403e3 2199 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2200 /* Clean up stream from consumer and free it. */
2201 lttng_poll_del(&events, stream->wait_fd);
2202 consumer_del_metadata_stream(stream, metadata_ht);
e316aad5
DG
2203 } else if (len > 0) {
2204 stream->data_read = 1;
2205 }
fb3a43a9 2206 }
e316aad5
DG
2207
2208 /* Release RCU lock for the stream looked up */
d09e1200 2209 rcu_read_unlock();
fb3a43a9
DG
2210 }
2211 }
2212
2213error:
2214end:
2215 DBG("Metadata poll thread exiting");
2216 lttng_poll_clean(&events);
2217
04bb2b64 2218 destroy_stream_ht(metadata_ht);
fb3a43a9
DG
2219
2220 rcu_unregister_thread();
2221 return NULL;
2222}
2223
3bd1e081 2224/*
e4421fec 2225 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
2226 * it to tracefile if necessary.
2227 */
7d980def 2228void *consumer_thread_data_poll(void *data)
3bd1e081
MD
2229{
2230 int num_rdy, num_hup, high_prio, ret, i;
2231 struct pollfd *pollfd = NULL;
2232 /* local view of the streams */
c869f647 2233 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
2234 /* local view of consumer_data.fds_count */
2235 int nb_fd = 0;
3bd1e081 2236 struct lttng_consumer_local_data *ctx = data;
00e2e675 2237 ssize_t len;
3bd1e081 2238
e7b994a3
DG
2239 rcu_register_thread();
2240
43c34bc3
DG
2241 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2242 if (data_ht == NULL) {
04bb2b64 2243 /* ENOMEM at this point. Better to bail out. */
43c34bc3
DG
2244 goto end;
2245 }
2246
effcf122 2247 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
2248
2249 while (1) {
2250 high_prio = 0;
2251 num_hup = 0;
2252
2253 /*
e4421fec 2254 * the fds set has been updated, we need to update our
3bd1e081
MD
2255 * local array as well
2256 */
2257 pthread_mutex_lock(&consumer_data.lock);
2258 if (consumer_data.need_update) {
2259 if (pollfd != NULL) {
2260 free(pollfd);
2261 pollfd = NULL;
2262 }
2263 if (local_stream != NULL) {
2264 free(local_stream);
2265 local_stream = NULL;
2266 }
2267
50f8ae69 2268 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2269 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 2270 if (pollfd == NULL) {
7a57cf92 2271 PERROR("pollfd malloc");
3bd1e081
MD
2272 pthread_mutex_unlock(&consumer_data.lock);
2273 goto end;
2274 }
2275
50f8ae69 2276 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2277 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
2278 sizeof(struct lttng_consumer_stream));
2279 if (local_stream == NULL) {
7a57cf92 2280 PERROR("local_stream malloc");
3bd1e081
MD
2281 pthread_mutex_unlock(&consumer_data.lock);
2282 goto end;
2283 }
43c34bc3
DG
2284 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
2285 data_ht);
3bd1e081
MD
2286 if (ret < 0) {
2287 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 2288 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2289 pthread_mutex_unlock(&consumer_data.lock);
2290 goto end;
2291 }
2292 nb_fd = ret;
2293 consumer_data.need_update = 0;
2294 }
2295 pthread_mutex_unlock(&consumer_data.lock);
2296
4078b776
MD
2297 /* No FDs and consumer_quit, consumer_cleanup the thread */
2298 if (nb_fd == 0 && consumer_quit == 1) {
2299 goto end;
2300 }
3bd1e081 2301 /* poll on the array of fds */
88f2b785 2302 restart:
3bd1e081 2303 DBG("polling on %d fd", nb_fd + 1);
cb365c03 2304 num_rdy = poll(pollfd, nb_fd + 1, -1);
3bd1e081
MD
2305 DBG("poll num_rdy : %d", num_rdy);
2306 if (num_rdy == -1) {
88f2b785
MD
2307 /*
2308 * Restart interrupted system call.
2309 */
2310 if (errno == EINTR) {
2311 goto restart;
2312 }
7a57cf92 2313 PERROR("Poll error");
f73fabfd 2314 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2315 goto end;
2316 } else if (num_rdy == 0) {
2317 DBG("Polling thread timed out");
2318 goto end;
2319 }
2320
3bd1e081 2321 /*
50f8ae69 2322 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2323 * beginning of the loop to update the array. We want to prioritize
2324 * array update over low-priority reads.
3bd1e081 2325 */
509bb1cf 2326 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819 2327 size_t pipe_readlen;
04fdd819 2328
50f8ae69 2329 DBG("consumer_data_pipe wake up");
04fdd819
MD
2330 /* Consume 1 byte of pipe data */
2331 do {
50f8ae69 2332 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
c869f647 2333 sizeof(new_stream));
04fdd819 2334 } while (pipe_readlen == -1 && errno == EINTR);
c869f647
DG
2335
2336 /*
2337 * If the stream is NULL, just ignore it. It's also possible that
2338 * the sessiond poll thread changed the consumer_quit state and is
2339 * waking us up to test it.
2340 */
2341 if (new_stream == NULL) {
8994307f 2342 validate_endpoint_status_data_stream();
c869f647
DG
2343 continue;
2344 }
2345
43c34bc3 2346 ret = consumer_add_stream(new_stream, data_ht);
c869f647
DG
2347 if (ret) {
2348 ERR("Consumer add stream %d failed. Continuing",
2349 new_stream->key);
2350 /*
2351 * At this point, if the add_stream fails, it is not in the
2352 * hash table thus passing the NULL value here.
2353 */
2354 consumer_del_stream(new_stream, NULL);
2355 }
2356
2357 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2358 continue;
2359 }
2360
2361 /* Take care of high priority channels first. */
2362 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2363 if (local_stream[i] == NULL) {
2364 continue;
2365 }
fb3a43a9 2366 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2367 DBG("Urgent read on fd %d", pollfd[i].fd);
2368 high_prio = 1;
4078b776 2369 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2370 /* it's ok to have an unavailable sub-buffer */
b64403e3 2371 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2372 /* Clean the stream and free it. */
2373 consumer_del_stream(local_stream[i], data_ht);
9617607b 2374 local_stream[i] = NULL;
4078b776
MD
2375 } else if (len > 0) {
2376 local_stream[i]->data_read = 1;
d41f73b7 2377 }
3bd1e081
MD
2378 }
2379 }
2380
4078b776
MD
2381 /*
2382 * If we read high prio channel in this loop, try again
2383 * for more high prio data.
2384 */
2385 if (high_prio) {
3bd1e081
MD
2386 continue;
2387 }
2388
2389 /* Take care of low priority channels. */
4078b776 2390 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2391 if (local_stream[i] == NULL) {
2392 continue;
2393 }
4078b776
MD
2394 if ((pollfd[i].revents & POLLIN) ||
2395 local_stream[i]->hangup_flush_done) {
4078b776
MD
2396 DBG("Normal read on fd %d", pollfd[i].fd);
2397 len = ctx->on_buffer_ready(local_stream[i], ctx);
2398 /* it's ok to have an unavailable sub-buffer */
b64403e3 2399 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2400 /* Clean the stream and free it. */
2401 consumer_del_stream(local_stream[i], data_ht);
9617607b 2402 local_stream[i] = NULL;
4078b776
MD
2403 } else if (len > 0) {
2404 local_stream[i]->data_read = 1;
2405 }
2406 }
2407 }
2408
2409 /* Handle hangup and errors */
2410 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2411 if (local_stream[i] == NULL) {
2412 continue;
2413 }
4078b776
MD
2414 if (!local_stream[i]->hangup_flush_done
2415 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2416 && (consumer_data.type == LTTNG_CONSUMER32_UST
2417 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2418 DBG("fd %d is hup|err|nval. Attempting flush and read.",
9617607b 2419 pollfd[i].fd);
4078b776
MD
2420 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2421 /* Attempt read again, for the data we just flushed. */
2422 local_stream[i]->data_read = 1;
2423 }
2424 /*
2425 * If the poll flag is HUP/ERR/NVAL and we have
2426 * read no data in this pass, we can remove the
2427 * stream from its hash table.
2428 */
2429 if ((pollfd[i].revents & POLLHUP)) {
2430 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2431 if (!local_stream[i]->data_read) {
43c34bc3 2432 consumer_del_stream(local_stream[i], data_ht);
9617607b 2433 local_stream[i] = NULL;
4078b776
MD
2434 num_hup++;
2435 }
2436 } else if (pollfd[i].revents & POLLERR) {
2437 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2438 if (!local_stream[i]->data_read) {
43c34bc3 2439 consumer_del_stream(local_stream[i], data_ht);
9617607b 2440 local_stream[i] = NULL;
4078b776
MD
2441 num_hup++;
2442 }
2443 } else if (pollfd[i].revents & POLLNVAL) {
2444 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2445 if (!local_stream[i]->data_read) {
43c34bc3 2446 consumer_del_stream(local_stream[i], data_ht);
9617607b 2447 local_stream[i] = NULL;
4078b776 2448 num_hup++;
3bd1e081
MD
2449 }
2450 }
9617607b
DG
2451 if (local_stream[i] != NULL) {
2452 local_stream[i]->data_read = 0;
2453 }
3bd1e081
MD
2454 }
2455 }
2456end:
2457 DBG("polling thread exiting");
2458 if (pollfd != NULL) {
2459 free(pollfd);
2460 pollfd = NULL;
2461 }
2462 if (local_stream != NULL) {
2463 free(local_stream);
2464 local_stream = NULL;
2465 }
fb3a43a9
DG
2466
2467 /*
2468 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2469 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2470 * read side of the pipe. If we close them both, epoll_wait strangely does
2471 * not return and could create a endless wait period if the pipe is the
2472 * only tracked fd in the poll set. The thread will take care of closing
2473 * the read side.
fb3a43a9 2474 */
f66c074c
DG
2475 ret = close(ctx->consumer_metadata_pipe[1]);
2476 if (ret < 0) {
2477 PERROR("close data pipe");
2478 }
fb3a43a9 2479
04bb2b64 2480 destroy_data_stream_ht(data_ht);
43c34bc3 2481
e7b994a3 2482 rcu_unregister_thread();
3bd1e081
MD
2483 return NULL;
2484}
2485
2486/*
2487 * This thread listens on the consumerd socket and receives the file
2488 * descriptors from the session daemon.
2489 */
7d980def 2490void *consumer_thread_sessiond_poll(void *data)
3bd1e081 2491{
d96f09c6 2492 int sock = -1, client_socket, ret;
3bd1e081
MD
2493 /*
2494 * structure to poll for incoming data on communication socket avoids
2495 * making blocking sockets.
2496 */
2497 struct pollfd consumer_sockpoll[2];
2498 struct lttng_consumer_local_data *ctx = data;
2499
e7b994a3
DG
2500 rcu_register_thread();
2501
3bd1e081
MD
2502 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2503 unlink(ctx->consumer_command_sock_path);
2504 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2505 if (client_socket < 0) {
2506 ERR("Cannot create command socket");
2507 goto end;
2508 }
2509
2510 ret = lttcomm_listen_unix_sock(client_socket);
2511 if (ret < 0) {
2512 goto end;
2513 }
2514
32258573 2515 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2516 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2517 /* return < 0 on error, but == 0 is not fatal */
2518 if (ret < 0) {
32258573 2519 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2520 goto end;
2521 }
2522
2523 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2524 if (ret < 0) {
7a57cf92 2525 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2526 goto end;
2527 }
2528
2529 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2530 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2531 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2532 consumer_sockpoll[1].fd = client_socket;
2533 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2534
2535 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2536 goto end;
2537 }
2538 DBG("Connection on client_socket");
2539
2540 /* Blocking call, waiting for transmission */
2541 sock = lttcomm_accept_unix_sock(client_socket);
2542 if (sock <= 0) {
2543 WARN("On accept");
2544 goto end;
2545 }
2546 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2547 if (ret < 0) {
7a57cf92 2548 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2549 goto end;
2550 }
2551
d96f09c6
DG
2552 /* This socket is not useful anymore. */
2553 ret = close(client_socket);
2554 if (ret < 0) {
2555 PERROR("close client_socket");
2556 }
2557 client_socket = -1;
2558
3bd1e081
MD
2559 /* update the polling structure to poll on the established socket */
2560 consumer_sockpoll[1].fd = sock;
2561 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2562
2563 while (1) {
2564 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2565 goto end;
2566 }
2567 DBG("Incoming command on sock");
2568 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2569 if (ret == -ENOENT) {
2570 DBG("Received STOP command");
2571 goto end;
2572 }
4cbc1a04
DG
2573 if (ret <= 0) {
2574 /*
2575 * This could simply be a session daemon quitting. Don't output
2576 * ERR() here.
2577 */
2578 DBG("Communication interrupted on command socket");
3bd1e081
MD
2579 goto end;
2580 }
2581 if (consumer_quit) {
2582 DBG("consumer_thread_receive_fds received quit from signal");
2583 goto end;
2584 }
2585 DBG("received fds on sock");
2586 }
2587end:
2588 DBG("consumer_thread_receive_fds exiting");
2589
2590 /*
2591 * when all fds have hung up, the polling thread
2592 * can exit cleanly
2593 */
2594 consumer_quit = 1;
2595
04fdd819 2596 /*
c869f647 2597 * Notify the data poll thread to poll back again and test the
8994307f 2598 * consumer_quit state that we just set so to quit gracefully.
04fdd819 2599 */
8994307f 2600 notify_thread_pipe(ctx->consumer_data_pipe[1]);
c869f647 2601
d96f09c6
DG
2602 /* Cleaning up possibly open sockets. */
2603 if (sock >= 0) {
2604 ret = close(sock);
2605 if (ret < 0) {
2606 PERROR("close sock sessiond poll");
2607 }
2608 }
2609 if (client_socket >= 0) {
2610 ret = close(sock);
2611 if (ret < 0) {
2612 PERROR("close client_socket sessiond poll");
2613 }
2614 }
2615
e7b994a3 2616 rcu_unregister_thread();
3bd1e081
MD
2617 return NULL;
2618}
d41f73b7 2619
4078b776 2620ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2621 struct lttng_consumer_local_data *ctx)
2622{
74251bb8
DG
2623 ssize_t ret;
2624
2625 pthread_mutex_lock(&stream->lock);
2626
d41f73b7
MD
2627 switch (consumer_data.type) {
2628 case LTTNG_CONSUMER_KERNEL:
74251bb8
DG
2629 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
2630 break;
7753dea8
MD
2631 case LTTNG_CONSUMER32_UST:
2632 case LTTNG_CONSUMER64_UST:
74251bb8
DG
2633 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
2634 break;
d41f73b7
MD
2635 default:
2636 ERR("Unknown consumer_data type");
2637 assert(0);
74251bb8
DG
2638 ret = -ENOSYS;
2639 break;
d41f73b7 2640 }
74251bb8
DG
2641
2642 pthread_mutex_unlock(&stream->lock);
2643 return ret;
d41f73b7
MD
2644}
2645
2646int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2647{
2648 switch (consumer_data.type) {
2649 case LTTNG_CONSUMER_KERNEL:
2650 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2651 case LTTNG_CONSUMER32_UST:
2652 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2653 return lttng_ustconsumer_on_recv_stream(stream);
2654 default:
2655 ERR("Unknown consumer_data type");
2656 assert(0);
2657 return -ENOSYS;
2658 }
2659}
e4421fec
DG
2660
2661/*
2662 * Allocate and set consumer data hash tables.
2663 */
2664void lttng_consumer_init(void)
2665{
e4421fec 2666 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2667 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
53632229 2668 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
e4421fec 2669}
7735ef9e
DG
2670
2671/*
2672 * Process the ADD_RELAYD command receive by a consumer.
2673 *
2674 * This will create a relayd socket pair and add it to the relayd hash table.
2675 * The caller MUST acquire a RCU read side lock before calling it.
2676 */
2677int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2678 struct lttng_consumer_local_data *ctx, int sock,
2679 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
2680{
4028eeb9 2681 int fd = -1, ret = -1;
f50f23d9 2682 enum lttng_error_code ret_code = LTTNG_OK;
7735ef9e
DG
2683 struct consumer_relayd_sock_pair *relayd;
2684
2685 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2686
f50f23d9
DG
2687 /* First send a status message before receiving the fds. */
2688 ret = consumer_send_status_msg(sock, ret_code);
2689 if (ret < 0) {
2690 /* Somehow, the session daemon is not responding anymore. */
2691 goto error;
2692 }
2693
7735ef9e
DG
2694 /* Get relayd reference if exists. */
2695 relayd = consumer_find_relayd(net_seq_idx);
2696 if (relayd == NULL) {
2697 /* Not found. Allocate one. */
2698 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2699 if (relayd == NULL) {
f73fabfd 2700 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2701 goto error;
2702 }
2703 }
2704
2705 /* Poll on consumer socket. */
2706 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2707 ret = -EINTR;
2708 goto error;
2709 }
2710
2711 /* Get relayd socket from session daemon */
2712 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2713 if (ret != sizeof(fd)) {
f73fabfd 2714 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e 2715 ret = -1;
4028eeb9 2716 fd = -1; /* Just in case it gets set with an invalid value. */
7735ef9e
DG
2717 goto error;
2718 }
2719
f50f23d9
DG
2720 /* We have the fds without error. Send status back. */
2721 ret = consumer_send_status_msg(sock, ret_code);
2722 if (ret < 0) {
2723 /* Somehow, the session daemon is not responding anymore. */
2724 goto error;
2725 }
2726
7735ef9e
DG
2727 /* Copy socket information and received FD */
2728 switch (sock_type) {
2729 case LTTNG_STREAM_CONTROL:
2730 /* Copy received lttcomm socket */
2731 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2732 ret = lttcomm_create_sock(&relayd->control_sock);
4028eeb9
DG
2733 /* Immediately try to close the created socket if valid. */
2734 if (relayd->control_sock.fd >= 0) {
2735 if (close(relayd->control_sock.fd)) {
2736 PERROR("close relayd control socket");
2737 }
7735ef9e 2738 }
4028eeb9 2739 /* Handle create_sock error. */
f66c074c 2740 if (ret < 0) {
4028eeb9 2741 goto error;
f66c074c 2742 }
7735ef9e
DG
2743
2744 /* Assign new file descriptor */
2745 relayd->control_sock.fd = fd;
2746 break;
2747 case LTTNG_STREAM_DATA:
2748 /* Copy received lttcomm socket */
2749 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2750 ret = lttcomm_create_sock(&relayd->data_sock);
4028eeb9
DG
2751 /* Immediately try to close the created socket if valid. */
2752 if (relayd->data_sock.fd >= 0) {
2753 if (close(relayd->data_sock.fd)) {
2754 PERROR("close relayd data socket");
2755 }
7735ef9e 2756 }
4028eeb9 2757 /* Handle create_sock error. */
f66c074c 2758 if (ret < 0) {
4028eeb9 2759 goto error;
f66c074c 2760 }
7735ef9e
DG
2761
2762 /* Assign new file descriptor */
2763 relayd->data_sock.fd = fd;
2764 break;
2765 default:
2766 ERR("Unknown relayd socket type (%d)", sock_type);
2767 goto error;
2768 }
2769
2770 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2771 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2772 relayd->net_seq_idx, fd);
2773
2774 /*
2775 * Add relayd socket pair to consumer data hashtable. If object already
2776 * exists or on error, the function gracefully returns.
2777 */
d09e1200 2778 add_relayd(relayd);
7735ef9e
DG
2779
2780 /* All good! */
4028eeb9 2781 return 0;
7735ef9e
DG
2782
2783error:
4028eeb9
DG
2784 /* Close received socket if valid. */
2785 if (fd >= 0) {
2786 if (close(fd)) {
2787 PERROR("close received socket");
2788 }
2789 }
7735ef9e
DG
2790 return ret;
2791}
ca22feea 2792
4e9a4686
DG
2793/*
2794 * Try to lock the stream mutex.
2795 *
2796 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2797 */
2798static int stream_try_lock(struct lttng_consumer_stream *stream)
2799{
2800 int ret;
2801
2802 assert(stream);
2803
2804 /*
2805 * Try to lock the stream mutex. On failure, we know that the stream is
2806 * being used else where hence there is data still being extracted.
2807 */
2808 ret = pthread_mutex_trylock(&stream->lock);
2809 if (ret) {
2810 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2811 ret = 0;
2812 goto end;
2813 }
2814
2815 ret = 1;
2816
2817end:
2818 return ret;
2819}
2820
ca22feea
DG
2821/*
2822 * Check if for a given session id there is still data needed to be extract
2823 * from the buffers.
2824 *
6d805429 2825 * Return 1 if data is pending or else 0 meaning ready to be read.
ca22feea 2826 */
6d805429 2827int consumer_data_pending(uint64_t id)
ca22feea
DG
2828{
2829 int ret;
2830 struct lttng_ht_iter iter;
2831 struct lttng_ht *ht;
2832 struct lttng_consumer_stream *stream;
c8f59ee5 2833 struct consumer_relayd_sock_pair *relayd;
6d805429 2834 int (*data_pending)(struct lttng_consumer_stream *);
ca22feea 2835
6d805429 2836 DBG("Consumer data pending command on session id %" PRIu64, id);
ca22feea 2837
6f6eda74 2838 rcu_read_lock();
ca22feea
DG
2839 pthread_mutex_lock(&consumer_data.lock);
2840
2841 switch (consumer_data.type) {
2842 case LTTNG_CONSUMER_KERNEL:
6d805429 2843 data_pending = lttng_kconsumer_data_pending;
ca22feea
DG
2844 break;
2845 case LTTNG_CONSUMER32_UST:
2846 case LTTNG_CONSUMER64_UST:
6d805429 2847 data_pending = lttng_ustconsumer_data_pending;
ca22feea
DG
2848 break;
2849 default:
2850 ERR("Unknown consumer data type");
2851 assert(0);
2852 }
2853
2854 /* Ease our life a bit */
2855 ht = consumer_data.stream_list_ht;
2856
c8f59ee5 2857 cds_lfht_for_each_entry_duplicate(ht->ht,
b6314938 2858 ht->hash_fct((void *)((unsigned long) id), lttng_ht_seed),
ca22feea
DG
2859 ht->match_fct, (void *)((unsigned long) id),
2860 &iter.iter, stream, node_session_id.node) {
4e9a4686
DG
2861 /* If this call fails, the stream is being used hence data pending. */
2862 ret = stream_try_lock(stream);
2863 if (!ret) {
6d805429 2864 goto data_not_pending;
ca22feea 2865 }
ca22feea 2866
4e9a4686
DG
2867 /*
2868 * A removed node from the hash table indicates that the stream has
2869 * been deleted thus having a guarantee that the buffers are closed
2870 * on the consumer side. However, data can still be transmitted
2871 * over the network so don't skip the relayd check.
2872 */
2873 ret = cds_lfht_is_node_deleted(&stream->node.node);
2874 if (!ret) {
2875 /* Check the stream if there is data in the buffers. */
6d805429
DG
2876 ret = data_pending(stream);
2877 if (ret == 1) {
4e9a4686 2878 pthread_mutex_unlock(&stream->lock);
6d805429 2879 goto data_not_pending;
4e9a4686
DG
2880 }
2881 }
2882
2883 /* Relayd check */
c8f59ee5
DG
2884 if (stream->net_seq_idx != -1) {
2885 relayd = consumer_find_relayd(stream->net_seq_idx);
4e9a4686
DG
2886 if (!relayd) {
2887 /*
2888 * At this point, if the relayd object is not available for the
2889 * given stream, it is because the relayd is being cleaned up
2890 * so every stream associated with it (for a session id value)
2891 * are or will be marked for deletion hence no data pending.
2892 */
2893 pthread_mutex_unlock(&stream->lock);
6d805429 2894 goto data_not_pending;
4e9a4686 2895 }
c8f59ee5 2896
c8f59ee5
DG
2897 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2898 if (stream->metadata_flag) {
2899 ret = relayd_quiescent_control(&relayd->control_sock);
2900 } else {
6d805429 2901 ret = relayd_data_pending(&relayd->control_sock,
c8f59ee5
DG
2902 stream->relayd_stream_id, stream->next_net_seq_num);
2903 }
2904 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
6d805429 2905 if (ret == 1) {
4e9a4686 2906 pthread_mutex_unlock(&stream->lock);
6d805429 2907 goto data_not_pending;
c8f59ee5
DG
2908 }
2909 }
4e9a4686 2910 pthread_mutex_unlock(&stream->lock);
c8f59ee5 2911 }
ca22feea
DG
2912
2913 /*
2914 * Finding _no_ node in the hash table means that the stream(s) have been
2915 * removed thus data is guaranteed to be available for analysis from the
2916 * trace files. This is *only* true for local consumer and not network
2917 * streaming.
2918 */
2919
2920 /* Data is available to be read by a viewer. */
2921 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 2922 rcu_read_unlock();
6d805429 2923 return 0;
ca22feea 2924
6d805429 2925data_not_pending:
ca22feea
DG
2926 /* Data is still being extracted from buffers. */
2927 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 2928 rcu_read_unlock();
6d805429 2929 return 1;
ca22feea 2930}
f50f23d9
DG
2931
2932/*
2933 * Send a ret code status message to the sessiond daemon.
2934 *
2935 * Return the sendmsg() return value.
2936 */
2937int consumer_send_status_msg(int sock, int ret_code)
2938{
2939 struct lttcomm_consumer_status_msg msg;
2940
2941 msg.ret_code = ret_code;
2942
2943 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
2944}
This page took 0.180554 seconds and 4 git commands to generate.