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