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