Remove unused code
[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
702b1ea4
MD
626static
627void consumer_free_channel(struct rcu_head *head)
628{
629 struct lttng_ht_node_ulong *node =
630 caa_container_of(head, struct lttng_ht_node_ulong, head);
631 struct lttng_consumer_channel *channel =
632 caa_container_of(node, struct lttng_consumer_channel, node);
633
634 free(channel);
635}
636
3bd1e081
MD
637/*
638 * Remove a channel from the global list protected by a mutex. This
639 * function is also responsible for freeing its data structures.
640 */
641void consumer_del_channel(struct lttng_consumer_channel *channel)
642{
643 int ret;
e4421fec 644 struct lttng_ht_iter iter;
3bd1e081
MD
645
646 pthread_mutex_lock(&consumer_data.lock);
647
648 switch (consumer_data.type) {
649 case LTTNG_CONSUMER_KERNEL:
650 break;
7753dea8
MD
651 case LTTNG_CONSUMER32_UST:
652 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
653 lttng_ustconsumer_del_channel(channel);
654 break;
655 default:
656 ERR("Unknown consumer_data type");
657 assert(0);
658 goto end;
659 }
660
6065ceec 661 rcu_read_lock();
04253271
MD
662 iter.iter.node = &channel->node.node;
663 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
664 assert(!ret);
6065ceec
DG
665 rcu_read_unlock();
666
3bd1e081
MD
667 if (channel->mmap_base != NULL) {
668 ret = munmap(channel->mmap_base, channel->mmap_len);
669 if (ret != 0) {
7a57cf92 670 PERROR("munmap");
3bd1e081
MD
671 }
672 }
b5c5fc29 673 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
674 ret = close(channel->wait_fd);
675 if (ret) {
676 PERROR("close");
677 }
3bd1e081 678 }
2c1dd183 679 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
680 ret = close(channel->shm_fd);
681 if (ret) {
682 PERROR("close");
683 }
3bd1e081 684 }
702b1ea4
MD
685
686 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
687end:
688 pthread_mutex_unlock(&consumer_data.lock);
689}
690
691struct lttng_consumer_channel *consumer_allocate_channel(
692 int channel_key,
693 int shm_fd, int wait_fd,
694 uint64_t mmap_len,
c30aaa51
MD
695 uint64_t max_sb_size,
696 unsigned int nb_init_streams)
3bd1e081
MD
697{
698 struct lttng_consumer_channel *channel;
699 int ret;
700
276b26d1 701 channel = zmalloc(sizeof(*channel));
3bd1e081 702 if (channel == NULL) {
7a57cf92 703 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
704 goto end;
705 }
706 channel->key = channel_key;
707 channel->shm_fd = shm_fd;
708 channel->wait_fd = wait_fd;
709 channel->mmap_len = mmap_len;
710 channel->max_sb_size = max_sb_size;
711 channel->refcount = 0;
c30aaa51 712 channel->nb_init_streams = nb_init_streams;
e4421fec 713 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
714
715 switch (consumer_data.type) {
716 case LTTNG_CONSUMER_KERNEL:
717 channel->mmap_base = NULL;
718 channel->mmap_len = 0;
719 break;
7753dea8
MD
720 case LTTNG_CONSUMER32_UST:
721 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
722 ret = lttng_ustconsumer_allocate_channel(channel);
723 if (ret) {
724 free(channel);
725 return NULL;
726 }
727 break;
728 default:
729 ERR("Unknown consumer_data type");
730 assert(0);
731 goto end;
732 }
733 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 734 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
735 (unsigned long long) channel->mmap_len,
736 (unsigned long long) channel->max_sb_size);
737end:
738 return channel;
739}
740
741/*
742 * Add a channel to the global list protected by a mutex.
743 */
744int consumer_add_channel(struct lttng_consumer_channel *channel)
745{
c77fc10a
DG
746 struct lttng_ht_node_ulong *node;
747 struct lttng_ht_iter iter;
748
3bd1e081 749 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
750 /* Steal channel identifier, for UST */
751 consumer_steal_channel_key(channel->key);
6065ceec 752 rcu_read_lock();
c77fc10a
DG
753
754 lttng_ht_lookup(consumer_data.channel_ht,
755 (void *)((unsigned long) channel->key), &iter);
756 node = lttng_ht_iter_get_node_ulong(&iter);
757 if (node != NULL) {
758 /* Channel already exist. Ignore the insertion */
759 goto end;
760 }
761
04253271 762 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
763
764end:
6065ceec 765 rcu_read_unlock();
3bd1e081 766 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 767
7ad0a0cb 768 return 0;
3bd1e081
MD
769}
770
771/*
772 * Allocate the pollfd structure and the local view of the out fds to avoid
773 * doing a lookup in the linked list and concurrency issues when writing is
774 * needed. Called with consumer_data.lock held.
775 *
776 * Returns the number of fds in the structures.
777 */
43c34bc3 778static int consumer_update_poll_array(
3bd1e081 779 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
43c34bc3 780 struct lttng_consumer_stream **local_stream, struct lttng_ht *ht)
3bd1e081 781{
3bd1e081 782 int i = 0;
e4421fec
DG
783 struct lttng_ht_iter iter;
784 struct lttng_consumer_stream *stream;
3bd1e081
MD
785
786 DBG("Updating poll fd array");
481d6c57 787 rcu_read_lock();
43c34bc3 788 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
e4421fec 789 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
3bd1e081
MD
790 continue;
791 }
e4421fec
DG
792 DBG("Active FD %d", stream->wait_fd);
793 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 794 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 795 local_stream[i] = stream;
3bd1e081
MD
796 i++;
797 }
481d6c57 798 rcu_read_unlock();
3bd1e081
MD
799
800 /*
50f8ae69 801 * Insert the consumer_data_pipe at the end of the array and don't
3bd1e081
MD
802 * increment i so nb_fd is the number of real FD.
803 */
50f8ae69 804 (*pollfd)[i].fd = ctx->consumer_data_pipe[0];
509bb1cf 805 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
806 return i;
807}
808
809/*
810 * Poll on the should_quit pipe and the command socket return -1 on error and
811 * should exit, 0 if data is available on the command socket
812 */
813int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
814{
815 int num_rdy;
816
88f2b785 817restart:
3bd1e081
MD
818 num_rdy = poll(consumer_sockpoll, 2, -1);
819 if (num_rdy == -1) {
88f2b785
MD
820 /*
821 * Restart interrupted system call.
822 */
823 if (errno == EINTR) {
824 goto restart;
825 }
7a57cf92 826 PERROR("Poll error");
3bd1e081
MD
827 goto exit;
828 }
509bb1cf 829 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
830 DBG("consumer_should_quit wake up");
831 goto exit;
832 }
833 return 0;
834
835exit:
836 return -1;
837}
838
839/*
840 * Set the error socket.
841 */
842void lttng_consumer_set_error_sock(
843 struct lttng_consumer_local_data *ctx, int sock)
844{
845 ctx->consumer_error_socket = sock;
846}
847
848/*
849 * Set the command socket path.
850 */
3bd1e081
MD
851void lttng_consumer_set_command_sock_path(
852 struct lttng_consumer_local_data *ctx, char *sock)
853{
854 ctx->consumer_command_sock_path = sock;
855}
856
857/*
858 * Send return code to the session daemon.
859 * If the socket is not defined, we return 0, it is not a fatal error
860 */
861int lttng_consumer_send_error(
862 struct lttng_consumer_local_data *ctx, int cmd)
863{
864 if (ctx->consumer_error_socket > 0) {
865 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
866 sizeof(enum lttcomm_sessiond_command));
867 }
868
869 return 0;
870}
871
872/*
873 * Close all the tracefiles and stream fds, should be called when all instances
874 * are destroyed.
875 */
876void lttng_consumer_cleanup(void)
877{
e4421fec 878 struct lttng_ht_iter iter;
6065ceec
DG
879 struct lttng_ht_node_ulong *node;
880
881 rcu_read_lock();
3bd1e081 882
6065ceec
DG
883 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
884 node) {
702b1ea4
MD
885 struct lttng_consumer_channel *channel =
886 caa_container_of(node, struct lttng_consumer_channel, node);
887 consumer_del_channel(channel);
3bd1e081 888 }
6065ceec
DG
889
890 rcu_read_unlock();
d6ce1df2 891
d6ce1df2 892 lttng_ht_destroy(consumer_data.channel_ht);
3bd1e081
MD
893}
894
895/*
896 * Called from signal handler.
897 */
898void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
899{
900 int ret;
901 consumer_quit = 1;
6f94560a
MD
902 do {
903 ret = write(ctx->consumer_should_quit[1], "4", 1);
904 } while (ret < 0 && errno == EINTR);
3bd1e081 905 if (ret < 0) {
7a57cf92 906 PERROR("write consumer quit");
3bd1e081
MD
907 }
908}
909
00e2e675
DG
910void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
911 off_t orig_offset)
3bd1e081
MD
912{
913 int outfd = stream->out_fd;
914
915 /*
916 * This does a blocking write-and-wait on any page that belongs to the
917 * subbuffer prior to the one we just wrote.
918 * Don't care about error values, as these are just hints and ways to
919 * limit the amount of page cache used.
920 */
921 if (orig_offset < stream->chan->max_sb_size) {
922 return;
923 }
b9182dd9 924 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
925 stream->chan->max_sb_size,
926 SYNC_FILE_RANGE_WAIT_BEFORE
927 | SYNC_FILE_RANGE_WRITE
928 | SYNC_FILE_RANGE_WAIT_AFTER);
929 /*
930 * Give hints to the kernel about how we access the file:
931 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
932 * we write it.
933 *
934 * We need to call fadvise again after the file grows because the
935 * kernel does not seem to apply fadvise to non-existing parts of the
936 * file.
937 *
938 * Call fadvise _after_ having waited for the page writeback to
939 * complete because the dirty page writeback semantic is not well
940 * defined. So it can be expected to lead to lower throughput in
941 * streaming.
942 */
943 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
944 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
945}
946
947/*
948 * Initialise the necessary environnement :
949 * - create a new context
950 * - create the poll_pipe
951 * - create the should_quit pipe (for signal handler)
952 * - create the thread pipe (for splice)
953 *
954 * Takes a function pointer as argument, this function is called when data is
955 * available on a buffer. This function is responsible to do the
956 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
957 * buffer configuration and then kernctl_put_next_subbuf at the end.
958 *
959 * Returns a pointer to the new context or NULL on error.
960 */
961struct lttng_consumer_local_data *lttng_consumer_create(
962 enum lttng_consumer_type type,
4078b776 963 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 964 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
965 int (*recv_channel)(struct lttng_consumer_channel *channel),
966 int (*recv_stream)(struct lttng_consumer_stream *stream),
967 int (*update_stream)(int stream_key, uint32_t state))
968{
969 int ret, i;
970 struct lttng_consumer_local_data *ctx;
971
972 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
973 consumer_data.type == type);
974 consumer_data.type = type;
975
effcf122 976 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 977 if (ctx == NULL) {
7a57cf92 978 PERROR("allocating context");
3bd1e081
MD
979 goto error;
980 }
981
982 ctx->consumer_error_socket = -1;
983 /* assign the callbacks */
984 ctx->on_buffer_ready = buffer_ready;
985 ctx->on_recv_channel = recv_channel;
986 ctx->on_recv_stream = recv_stream;
987 ctx->on_update_stream = update_stream;
988
50f8ae69 989 ret = pipe(ctx->consumer_data_pipe);
3bd1e081 990 if (ret < 0) {
7a57cf92 991 PERROR("Error creating poll pipe");
3bd1e081
MD
992 goto error_poll_pipe;
993 }
994
04fdd819 995 /* set read end of the pipe to non-blocking */
50f8ae69 996 ret = fcntl(ctx->consumer_data_pipe[0], F_SETFL, O_NONBLOCK);
04fdd819 997 if (ret < 0) {
7a57cf92 998 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
999 goto error_poll_fcntl;
1000 }
1001
1002 /* set write end of the pipe to non-blocking */
50f8ae69 1003 ret = fcntl(ctx->consumer_data_pipe[1], F_SETFL, O_NONBLOCK);
04fdd819 1004 if (ret < 0) {
7a57cf92 1005 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1006 goto error_poll_fcntl;
1007 }
1008
3bd1e081
MD
1009 ret = pipe(ctx->consumer_should_quit);
1010 if (ret < 0) {
7a57cf92 1011 PERROR("Error creating recv pipe");
3bd1e081
MD
1012 goto error_quit_pipe;
1013 }
1014
1015 ret = pipe(ctx->consumer_thread_pipe);
1016 if (ret < 0) {
7a57cf92 1017 PERROR("Error creating thread pipe");
3bd1e081
MD
1018 goto error_thread_pipe;
1019 }
1020
fb3a43a9
DG
1021 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1022 if (ret < 0) {
1023 goto error_metadata_pipe;
1024 }
3bd1e081 1025
fb3a43a9
DG
1026 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1027 if (ret < 0) {
1028 goto error_splice_pipe;
1029 }
1030
1031 return ctx;
3bd1e081 1032
fb3a43a9
DG
1033error_splice_pipe:
1034 utils_close_pipe(ctx->consumer_metadata_pipe);
1035error_metadata_pipe:
1036 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1037error_thread_pipe:
1038 for (i = 0; i < 2; i++) {
1039 int err;
1040
1041 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1042 if (err) {
1043 PERROR("close");
1044 }
3bd1e081 1045 }
04fdd819 1046error_poll_fcntl:
3bd1e081
MD
1047error_quit_pipe:
1048 for (i = 0; i < 2; i++) {
1049 int err;
1050
50f8ae69 1051 err = close(ctx->consumer_data_pipe[i]);
4c462e79
MD
1052 if (err) {
1053 PERROR("close");
1054 }
3bd1e081
MD
1055 }
1056error_poll_pipe:
1057 free(ctx);
1058error:
1059 return NULL;
1060}
1061
1062/*
1063 * Close all fds associated with the instance and free the context.
1064 */
1065void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1066{
4c462e79
MD
1067 int ret;
1068
1069 ret = close(ctx->consumer_error_socket);
1070 if (ret) {
1071 PERROR("close");
1072 }
1073 ret = close(ctx->consumer_thread_pipe[0]);
1074 if (ret) {
1075 PERROR("close");
1076 }
1077 ret = close(ctx->consumer_thread_pipe[1]);
1078 if (ret) {
1079 PERROR("close");
1080 }
50f8ae69 1081 ret = close(ctx->consumer_data_pipe[0]);
4c462e79
MD
1082 if (ret) {
1083 PERROR("close");
1084 }
50f8ae69 1085 ret = close(ctx->consumer_data_pipe[1]);
4c462e79
MD
1086 if (ret) {
1087 PERROR("close");
1088 }
1089 ret = close(ctx->consumer_should_quit[0]);
1090 if (ret) {
1091 PERROR("close");
1092 }
1093 ret = close(ctx->consumer_should_quit[1]);
1094 if (ret) {
1095 PERROR("close");
1096 }
fb3a43a9
DG
1097 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1098
3bd1e081
MD
1099 unlink(ctx->consumer_command_sock_path);
1100 free(ctx);
1101}
1102
6197aea7
DG
1103/*
1104 * Write the metadata stream id on the specified file descriptor.
1105 */
1106static int write_relayd_metadata_id(int fd,
1107 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1108 struct consumer_relayd_sock_pair *relayd,
1109 unsigned long padding)
6197aea7
DG
1110{
1111 int ret;
1d4dfdef 1112 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1113
1d4dfdef
DG
1114 hdr.stream_id = htobe64(stream->relayd_stream_id);
1115 hdr.padding_size = htobe32(padding);
6197aea7 1116 do {
1d4dfdef 1117 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1118 } while (ret < 0 && errno == EINTR);
1119 if (ret < 0) {
1120 PERROR("write metadata stream id");
1121 goto end;
1122 }
1d4dfdef
DG
1123 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1124 stream->relayd_stream_id, padding);
6197aea7
DG
1125
1126end:
1127 return ret;
1128}
1129
3bd1e081 1130/*
09e26845
DG
1131 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1132 * core function for writing trace buffers to either the local filesystem or
1133 * the network.
1134 *
1135 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1136 *
1137 * Returns the number of bytes written
1138 */
4078b776 1139ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1140 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1141 struct lttng_consumer_stream *stream, unsigned long len,
1142 unsigned long padding)
3bd1e081 1143{
f02e1e8a
DG
1144 unsigned long mmap_offset;
1145 ssize_t ret = 0, written = 0;
1146 off_t orig_offset = stream->out_fd_offset;
1147 /* Default is on the disk */
1148 int outfd = stream->out_fd;
f02e1e8a
DG
1149 struct consumer_relayd_sock_pair *relayd = NULL;
1150
1151 /* RCU lock for the relayd pointer */
1152 rcu_read_lock();
1153
1154 /* Flag that the current stream if set for network streaming. */
1155 if (stream->net_seq_idx != -1) {
1156 relayd = consumer_find_relayd(stream->net_seq_idx);
1157 if (relayd == NULL) {
1158 goto end;
1159 }
1160 }
1161
1162 /* get the offset inside the fd to mmap */
3bd1e081
MD
1163 switch (consumer_data.type) {
1164 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1165 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1166 break;
7753dea8
MD
1167 case LTTNG_CONSUMER32_UST:
1168 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1169 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1170 stream->buf, &mmap_offset);
1171 break;
3bd1e081
MD
1172 default:
1173 ERR("Unknown consumer_data type");
1174 assert(0);
1175 }
f02e1e8a
DG
1176 if (ret != 0) {
1177 errno = -ret;
1178 PERROR("tracer ctl get_mmap_read_offset");
1179 written = ret;
1180 goto end;
1181 }
b9182dd9 1182
f02e1e8a
DG
1183 /* Handle stream on the relayd if the output is on the network */
1184 if (relayd) {
1185 unsigned long netlen = len;
1186
1187 /*
1188 * Lock the control socket for the complete duration of the function
1189 * since from this point on we will use the socket.
1190 */
1191 if (stream->metadata_flag) {
1192 /* Metadata requires the control socket. */
1193 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1194 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1195 }
1196
1d4dfdef 1197 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1198 if (ret >= 0) {
1199 /* Use the returned socket. */
1200 outfd = ret;
1201
1202 /* Write metadata stream id before payload */
1203 if (stream->metadata_flag) {
1d4dfdef 1204 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1205 if (ret < 0) {
f02e1e8a
DG
1206 written = ret;
1207 goto end;
1208 }
f02e1e8a
DG
1209 }
1210 }
1211 /* Else, use the default set before which is the filesystem. */
1d4dfdef
DG
1212 } else {
1213 /* No streaming, we have to set the len with the full padding */
1214 len += padding;
f02e1e8a
DG
1215 }
1216
1217 while (len > 0) {
1218 do {
1219 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1220 } while (ret < 0 && errno == EINTR);
1d4dfdef 1221 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a
DG
1222 if (ret < 0) {
1223 PERROR("Error in file write");
1224 if (written == 0) {
1225 written = ret;
1226 }
1227 goto end;
1228 } else if (ret > len) {
77c7c900 1229 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1230 written += ret;
1231 goto end;
1232 } else {
1233 len -= ret;
1234 mmap_offset += ret;
1235 }
f02e1e8a
DG
1236
1237 /* This call is useless on a socket so better save a syscall. */
1238 if (!relayd) {
1239 /* This won't block, but will start writeout asynchronously */
1240 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1241 SYNC_FILE_RANGE_WRITE);
1242 stream->out_fd_offset += ret;
1243 }
1244 written += ret;
1245 }
1246 lttng_consumer_sync_trace_file(stream, orig_offset);
1247
1248end:
1249 /* Unlock only if ctrl socket used */
1250 if (relayd && stream->metadata_flag) {
1251 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1252 }
1253
1254 rcu_read_unlock();
1255 return written;
3bd1e081
MD
1256}
1257
1258/*
1259 * Splice the data from the ring buffer to the tracefile.
1260 *
1261 * Returns the number of bytes spliced.
1262 */
4078b776 1263ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1264 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1265 struct lttng_consumer_stream *stream, unsigned long len,
1266 unsigned long padding)
3bd1e081 1267{
f02e1e8a
DG
1268 ssize_t ret = 0, written = 0, ret_splice = 0;
1269 loff_t offset = 0;
1270 off_t orig_offset = stream->out_fd_offset;
1271 int fd = stream->wait_fd;
1272 /* Default is on the disk */
1273 int outfd = stream->out_fd;
f02e1e8a 1274 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1275 int *splice_pipe;
f02e1e8a 1276
3bd1e081
MD
1277 switch (consumer_data.type) {
1278 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1279 break;
7753dea8
MD
1280 case LTTNG_CONSUMER32_UST:
1281 case LTTNG_CONSUMER64_UST:
f02e1e8a 1282 /* Not supported for user space tracing */
3bd1e081
MD
1283 return -ENOSYS;
1284 default:
1285 ERR("Unknown consumer_data type");
1286 assert(0);
3bd1e081
MD
1287 }
1288
f02e1e8a
DG
1289 /* RCU lock for the relayd pointer */
1290 rcu_read_lock();
1291
1292 /* Flag that the current stream if set for network streaming. */
1293 if (stream->net_seq_idx != -1) {
1294 relayd = consumer_find_relayd(stream->net_seq_idx);
1295 if (relayd == NULL) {
1296 goto end;
1297 }
1298 }
1299
fb3a43a9
DG
1300 /*
1301 * Choose right pipe for splice. Metadata and trace data are handled by
1302 * different threads hence the use of two pipes in order not to race or
1303 * corrupt the written data.
1304 */
1305 if (stream->metadata_flag) {
1306 splice_pipe = ctx->consumer_splice_metadata_pipe;
1307 } else {
1308 splice_pipe = ctx->consumer_thread_pipe;
1309 }
1310
f02e1e8a 1311 /* Write metadata stream id before payload */
1d4dfdef
DG
1312 if (relayd) {
1313 int total_len = len;
f02e1e8a 1314
1d4dfdef
DG
1315 if (stream->metadata_flag) {
1316 /*
1317 * Lock the control socket for the complete duration of the function
1318 * since from this point on we will use the socket.
1319 */
1320 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1321
1322 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1323 padding);
1324 if (ret < 0) {
1325 written = ret;
1326 goto end;
1327 }
1328
1329 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1330 }
1331
1332 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1333 if (ret >= 0) {
1334 /* Use the returned socket. */
1335 outfd = ret;
1336 } else {
1337 ERR("Remote relayd disconnected. Stopping");
f02e1e8a
DG
1338 goto end;
1339 }
1d4dfdef
DG
1340 } else {
1341 /* No streaming, we have to set the len with the full padding */
1342 len += padding;
f02e1e8a
DG
1343 }
1344
1345 while (len > 0) {
1d4dfdef
DG
1346 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1347 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1348 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1349 SPLICE_F_MOVE | SPLICE_F_MORE);
1350 DBG("splice chan to pipe, ret %zd", ret_splice);
1351 if (ret_splice < 0) {
1352 PERROR("Error in relay splice");
1353 if (written == 0) {
1354 written = ret_splice;
1355 }
1356 ret = errno;
1357 goto splice_error;
1358 }
1359
1360 /* Handle stream on the relayd if the output is on the network */
1361 if (relayd) {
1362 if (stream->metadata_flag) {
1d4dfdef
DG
1363 size_t metadata_payload_size =
1364 sizeof(struct lttcomm_relayd_metadata_payload);
1365
f02e1e8a 1366 /* Update counter to fit the spliced data */
1d4dfdef
DG
1367 ret_splice += metadata_payload_size;
1368 len += metadata_payload_size;
f02e1e8a
DG
1369 /*
1370 * We do this so the return value can match the len passed as
1371 * argument to this function.
1372 */
1d4dfdef 1373 written -= metadata_payload_size;
f02e1e8a
DG
1374 }
1375 }
1376
1377 /* Splice data out */
fb3a43a9 1378 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1379 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1380 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1381 if (ret_splice < 0) {
1382 PERROR("Error in file splice");
1383 if (written == 0) {
1384 written = ret_splice;
1385 }
1386 ret = errno;
1387 goto splice_error;
1388 } else if (ret_splice > len) {
1389 errno = EINVAL;
1390 PERROR("Wrote more data than requested %zd (len: %lu)",
1391 ret_splice, len);
1392 written += ret_splice;
1393 ret = errno;
1394 goto splice_error;
1395 }
1396 len -= ret_splice;
1397
1398 /* This call is useless on a socket so better save a syscall. */
1399 if (!relayd) {
1400 /* This won't block, but will start writeout asynchronously */
1401 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1402 SYNC_FILE_RANGE_WRITE);
1403 stream->out_fd_offset += ret_splice;
1404 }
1405 written += ret_splice;
1406 }
1407 lttng_consumer_sync_trace_file(stream, orig_offset);
1408
1409 ret = ret_splice;
1410
1411 goto end;
1412
1413splice_error:
1414 /* send the appropriate error description to sessiond */
1415 switch (ret) {
1416 case EBADF:
f73fabfd 1417 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EBADF);
f02e1e8a
DG
1418 break;
1419 case EINVAL:
f73fabfd 1420 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1421 break;
1422 case ENOMEM:
f73fabfd 1423 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1424 break;
1425 case ESPIPE:
f73fabfd 1426 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1427 break;
1428 }
1429
1430end:
1431 if (relayd && stream->metadata_flag) {
1432 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1433 }
1434
1435 rcu_read_unlock();
1436 return written;
3bd1e081
MD
1437}
1438
1439/*
1440 * Take a snapshot for a specific fd
1441 *
1442 * Returns 0 on success, < 0 on error
1443 */
1444int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1445 struct lttng_consumer_stream *stream)
1446{
1447 switch (consumer_data.type) {
1448 case LTTNG_CONSUMER_KERNEL:
1449 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1450 case LTTNG_CONSUMER32_UST:
1451 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1452 return lttng_ustconsumer_take_snapshot(ctx, stream);
1453 default:
1454 ERR("Unknown consumer_data type");
1455 assert(0);
1456 return -ENOSYS;
1457 }
1458
1459}
1460
1461/*
1462 * Get the produced position
1463 *
1464 * Returns 0 on success, < 0 on error
1465 */
1466int lttng_consumer_get_produced_snapshot(
1467 struct lttng_consumer_local_data *ctx,
1468 struct lttng_consumer_stream *stream,
1469 unsigned long *pos)
1470{
1471 switch (consumer_data.type) {
1472 case LTTNG_CONSUMER_KERNEL:
1473 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1474 case LTTNG_CONSUMER32_UST:
1475 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1476 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1477 default:
1478 ERR("Unknown consumer_data type");
1479 assert(0);
1480 return -ENOSYS;
1481 }
1482}
1483
1484int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1485 int sock, struct pollfd *consumer_sockpoll)
1486{
1487 switch (consumer_data.type) {
1488 case LTTNG_CONSUMER_KERNEL:
1489 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1490 case LTTNG_CONSUMER32_UST:
1491 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1492 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1493 default:
1494 ERR("Unknown consumer_data type");
1495 assert(0);
1496 return -ENOSYS;
1497 }
1498}
1499
43c34bc3
DG
1500/*
1501 * Iterate over all streams of the hashtable and free them properly.
1502 *
1503 * WARNING: *MUST* be used with data stream only.
1504 */
1505static void destroy_data_stream_ht(struct lttng_ht *ht)
1506{
1507 int ret;
1508 struct lttng_ht_iter iter;
1509 struct lttng_consumer_stream *stream;
1510
1511 if (ht == NULL) {
1512 return;
1513 }
1514
1515 rcu_read_lock();
1516 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1517 ret = lttng_ht_del(ht, &iter);
1518 assert(!ret);
1519
1520 call_rcu(&stream->node.head, consumer_free_stream);
1521 }
1522 rcu_read_unlock();
1523
1524 lttng_ht_destroy(ht);
1525}
1526
fb3a43a9 1527/*
f724d81e 1528 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1529 *
1530 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1531 */
1532static void destroy_stream_ht(struct lttng_ht *ht)
1533{
1534 int ret;
1535 struct lttng_ht_iter iter;
1536 struct lttng_consumer_stream *stream;
1537
1538 if (ht == NULL) {
1539 return;
1540 }
1541
d09e1200 1542 rcu_read_lock();
58b1f425 1543 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
fb3a43a9
DG
1544 ret = lttng_ht_del(ht, &iter);
1545 assert(!ret);
1546
58b1f425 1547 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9 1548 }
d09e1200 1549 rcu_read_unlock();
fb3a43a9
DG
1550
1551 lttng_ht_destroy(ht);
1552}
1553
1554/*
1555 * Clean up a metadata stream and free its memory.
1556 */
e316aad5
DG
1557void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1558 struct lttng_ht *ht)
fb3a43a9
DG
1559{
1560 int ret;
e316aad5
DG
1561 struct lttng_ht_iter iter;
1562 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1563 struct consumer_relayd_sock_pair *relayd;
1564
1565 assert(stream);
1566 /*
1567 * This call should NEVER receive regular stream. It must always be
1568 * metadata stream and this is crucial for data structure synchronization.
1569 */
1570 assert(stream->metadata_flag);
1571
e316aad5
DG
1572 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1573
1574 if (ht == NULL) {
1575 /* Means the stream was allocated but not successfully added */
1576 goto free_stream;
1577 }
1578
fb3a43a9
DG
1579 pthread_mutex_lock(&consumer_data.lock);
1580 switch (consumer_data.type) {
1581 case LTTNG_CONSUMER_KERNEL:
1582 if (stream->mmap_base != NULL) {
1583 ret = munmap(stream->mmap_base, stream->mmap_len);
1584 if (ret != 0) {
1585 PERROR("munmap metadata stream");
1586 }
1587 }
1588 break;
1589 case LTTNG_CONSUMER32_UST:
1590 case LTTNG_CONSUMER64_UST:
1591 lttng_ustconsumer_del_stream(stream);
1592 break;
1593 default:
1594 ERR("Unknown consumer_data type");
1595 assert(0);
e316aad5 1596 goto end;
fb3a43a9 1597 }
fb3a43a9 1598
c869f647 1599 rcu_read_lock();
58b1f425 1600 iter.iter.node = &stream->node.node;
c869f647
DG
1601 ret = lttng_ht_del(ht, &iter);
1602 assert(!ret);
1603 rcu_read_unlock();
1604
fb3a43a9
DG
1605 if (stream->out_fd >= 0) {
1606 ret = close(stream->out_fd);
1607 if (ret) {
1608 PERROR("close");
1609 }
1610 }
1611
1612 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1613 ret = close(stream->wait_fd);
1614 if (ret) {
1615 PERROR("close");
1616 }
1617 }
1618
1619 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1620 ret = close(stream->shm_fd);
1621 if (ret) {
1622 PERROR("close");
1623 }
1624 }
1625
1626 /* Check and cleanup relayd */
1627 rcu_read_lock();
1628 relayd = consumer_find_relayd(stream->net_seq_idx);
1629 if (relayd != NULL) {
1630 uatomic_dec(&relayd->refcount);
1631 assert(uatomic_read(&relayd->refcount) >= 0);
1632
1633 /* Closing streams requires to lock the control socket. */
1634 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1635 ret = relayd_send_close_stream(&relayd->control_sock,
1636 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1637 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1638 if (ret < 0) {
1639 DBG("Unable to close stream on the relayd. Continuing");
1640 /*
1641 * Continue here. There is nothing we can do for the relayd.
1642 * Chances are that the relayd has closed the socket so we just
1643 * continue cleaning up.
1644 */
1645 }
1646
1647 /* Both conditions are met, we destroy the relayd. */
1648 if (uatomic_read(&relayd->refcount) == 0 &&
1649 uatomic_read(&relayd->destroy_flag)) {
d09e1200 1650 destroy_relayd(relayd);
fb3a43a9
DG
1651 }
1652 }
1653 rcu_read_unlock();
1654
1655 /* Atomically decrement channel refcount since other threads can use it. */
1656 uatomic_dec(&stream->chan->refcount);
c30aaa51
MD
1657 if (!uatomic_read(&stream->chan->refcount)
1658 && !uatomic_read(&stream->chan->nb_init_streams)) {
1659 /* Go for channel deletion! */
e316aad5 1660 free_chan = stream->chan;
fb3a43a9
DG
1661 }
1662
e316aad5
DG
1663end:
1664 pthread_mutex_unlock(&consumer_data.lock);
1665
1666 if (free_chan) {
1667 consumer_del_channel(free_chan);
1668 }
1669
1670free_stream:
58b1f425 1671 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9
DG
1672}
1673
1674/*
1675 * Action done with the metadata stream when adding it to the consumer internal
1676 * data structures to handle it.
1677 */
e316aad5
DG
1678static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1679 struct lttng_ht *ht)
fb3a43a9 1680{
e316aad5 1681 int ret = 0;
fb3a43a9
DG
1682 struct consumer_relayd_sock_pair *relayd;
1683
e316aad5
DG
1684 assert(stream);
1685 assert(ht);
1686
1687 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1688
1689 pthread_mutex_lock(&consumer_data.lock);
1690
e316aad5
DG
1691 /*
1692 * From here, refcounts are updated so be _careful_ when returning an error
1693 * after this point.
1694 */
1695
fb3a43a9 1696 rcu_read_lock();
e316aad5 1697 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
1698 relayd = consumer_find_relayd(stream->net_seq_idx);
1699 if (relayd != NULL) {
1700 uatomic_inc(&relayd->refcount);
1701 }
e316aad5
DG
1702
1703 /* Update channel refcount once added without error(s). */
1704 uatomic_inc(&stream->chan->refcount);
1705
1706 /*
1707 * When nb_init_streams reaches 0, we don't need to trigger any action in
1708 * terms of destroying the associated channel, because the action that
1709 * causes the count to become 0 also causes a stream to be added. The
1710 * channel deletion will thus be triggered by the following removal of this
1711 * stream.
1712 */
1713 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
1714 uatomic_dec(&stream->chan->nb_init_streams);
1715 }
1716
43c34bc3
DG
1717 /* Steal stream identifier to avoid having streams with the same key */
1718 consumer_steal_stream_key(stream->key, ht);
1719
58b1f425 1720 lttng_ht_add_unique_ulong(ht, &stream->node);
fb3a43a9 1721 rcu_read_unlock();
e316aad5 1722
e316aad5
DG
1723 pthread_mutex_unlock(&consumer_data.lock);
1724 return ret;
fb3a43a9
DG
1725}
1726
1727/*
1728 * Thread polls on metadata file descriptor and write them on disk or on the
1729 * network.
1730 */
7d980def 1731void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
1732{
1733 int ret, i, pollfd;
1734 uint32_t revents, nb_fd;
e316aad5 1735 struct lttng_consumer_stream *stream = NULL;
fb3a43a9
DG
1736 struct lttng_ht_iter iter;
1737 struct lttng_ht_node_ulong *node;
fb3a43a9
DG
1738 struct lttng_poll_event events;
1739 struct lttng_consumer_local_data *ctx = data;
1740 ssize_t len;
1741
1742 rcu_register_thread();
1743
1744 DBG("Thread metadata poll started");
1745
fb3a43a9
DG
1746 /* Size is set to 1 for the consumer_metadata pipe */
1747 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
1748 if (ret < 0) {
1749 ERR("Poll set creation failed");
1750 goto end;
1751 }
1752
1753 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
1754 if (ret < 0) {
1755 goto end;
1756 }
1757
1758 /* Main loop */
1759 DBG("Metadata main loop started");
1760
1761 while (1) {
1762 lttng_poll_reset(&events);
1763
1764 nb_fd = LTTNG_POLL_GETNB(&events);
1765
1766 /* Only the metadata pipe is set */
1767 if (nb_fd == 0 && consumer_quit == 1) {
1768 goto end;
1769 }
1770
1771restart:
1772 DBG("Metadata poll wait with %d fd(s)", nb_fd);
1773 ret = lttng_poll_wait(&events, -1);
1774 DBG("Metadata event catched in thread");
1775 if (ret < 0) {
1776 if (errno == EINTR) {
e316aad5 1777 ERR("Poll EINTR catched");
fb3a43a9
DG
1778 goto restart;
1779 }
1780 goto error;
1781 }
1782
e316aad5 1783 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
1784 for (i = 0; i < nb_fd; i++) {
1785 revents = LTTNG_POLL_GETEV(&events, i);
1786 pollfd = LTTNG_POLL_GETFD(&events, i);
1787
e316aad5
DG
1788 /* Just don't waste time if no returned events for the fd */
1789 if (!revents) {
1790 continue;
1791 }
1792
fb3a43a9 1793 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 1794 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
1795 DBG("Metadata thread pipe hung up");
1796 /*
1797 * Remove the pipe from the poll set and continue the loop
1798 * since their might be data to consume.
1799 */
1800 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
1801 close(ctx->consumer_metadata_pipe[0]);
1802 continue;
1803 } else if (revents & LPOLLIN) {
fb3a43a9 1804 do {
633d0084
DG
1805 /* Get the stream pointer received */
1806 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 1807 } while (ret < 0 && errno == EINTR);
633d0084
DG
1808 if (ret < 0 ||
1809 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 1810 PERROR("read metadata stream");
fb3a43a9
DG
1811 /*
1812 * Let's continue here and hope we can still work
1813 * without stopping the consumer. XXX: Should we?
1814 */
1815 continue;
1816 }
1817
1818 DBG("Adding metadata stream %d to poll set",
1819 stream->wait_fd);
1820
e316aad5
DG
1821 ret = consumer_add_metadata_stream(stream, metadata_ht);
1822 if (ret) {
1823 ERR("Unable to add metadata stream");
1824 /* Stream was not setup properly. Continuing. */
1825 consumer_del_metadata_stream(stream, NULL);
1826 continue;
1827 }
fb3a43a9
DG
1828
1829 /* Add metadata stream to the global poll events list */
1830 lttng_poll_add(&events, stream->wait_fd,
1831 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
1832 }
1833
e316aad5 1834 /* Handle other stream */
fb3a43a9
DG
1835 continue;
1836 }
1837
d09e1200 1838 rcu_read_lock();
fb3a43a9
DG
1839 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
1840 &iter);
1841 node = lttng_ht_iter_get_node_ulong(&iter);
e316aad5 1842 assert(node);
fb3a43a9
DG
1843
1844 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 1845 node);
fb3a43a9 1846
e316aad5 1847 /* Check for error event */
4adabd61 1848 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 1849 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
1850 if (!stream->hangup_flush_done
1851 && (consumer_data.type == LTTNG_CONSUMER32_UST
1852 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1853 DBG("Attempting to flush and consume the UST buffers");
1854 lttng_ustconsumer_on_stream_hangup(stream);
1855
1856 /* We just flushed the stream now read it. */
4bb94b75
DG
1857 do {
1858 len = ctx->on_buffer_ready(stream, ctx);
1859 /*
1860 * We don't check the return value here since if we get
1861 * a negative len, it means an error occured thus we
1862 * simply remove it from the poll set and free the
1863 * stream.
1864 */
1865 } while (len > 0);
fb3a43a9
DG
1866 }
1867
fb3a43a9 1868 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
1869 /*
1870 * This call update the channel states, closes file descriptors
1871 * and securely free the stream.
1872 */
1873 consumer_del_metadata_stream(stream, metadata_ht);
1874 } else if (revents & (LPOLLIN | LPOLLPRI)) {
1875 /* Get the data out of the metadata file descriptor */
1876 DBG("Metadata available on fd %d", pollfd);
1877 assert(stream->wait_fd == pollfd);
1878
1879 len = ctx->on_buffer_ready(stream, ctx);
1880 /* It's ok to have an unavailable sub-buffer */
b64403e3 1881 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
e316aad5
DG
1882 rcu_read_unlock();
1883 goto end;
1884 } else if (len > 0) {
1885 stream->data_read = 1;
1886 }
fb3a43a9 1887 }
e316aad5
DG
1888
1889 /* Release RCU lock for the stream looked up */
d09e1200 1890 rcu_read_unlock();
fb3a43a9
DG
1891 }
1892 }
1893
1894error:
1895end:
1896 DBG("Metadata poll thread exiting");
1897 lttng_poll_clean(&events);
1898
1899 if (metadata_ht) {
1900 destroy_stream_ht(metadata_ht);
1901 }
1902
1903 rcu_unregister_thread();
1904 return NULL;
1905}
1906
3bd1e081 1907/*
e4421fec 1908 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
1909 * it to tracefile if necessary.
1910 */
7d980def 1911void *consumer_thread_data_poll(void *data)
3bd1e081
MD
1912{
1913 int num_rdy, num_hup, high_prio, ret, i;
1914 struct pollfd *pollfd = NULL;
1915 /* local view of the streams */
c869f647 1916 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
1917 /* local view of consumer_data.fds_count */
1918 int nb_fd = 0;
3bd1e081 1919 struct lttng_consumer_local_data *ctx = data;
00e2e675 1920 ssize_t len;
3bd1e081 1921
e7b994a3
DG
1922 rcu_register_thread();
1923
43c34bc3
DG
1924 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1925 if (data_ht == NULL) {
1926 goto end;
1927 }
1928
effcf122 1929 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
1930
1931 while (1) {
1932 high_prio = 0;
1933 num_hup = 0;
1934
1935 /*
e4421fec 1936 * the fds set has been updated, we need to update our
3bd1e081
MD
1937 * local array as well
1938 */
1939 pthread_mutex_lock(&consumer_data.lock);
1940 if (consumer_data.need_update) {
1941 if (pollfd != NULL) {
1942 free(pollfd);
1943 pollfd = NULL;
1944 }
1945 if (local_stream != NULL) {
1946 free(local_stream);
1947 local_stream = NULL;
1948 }
1949
50f8ae69 1950 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 1951 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 1952 if (pollfd == NULL) {
7a57cf92 1953 PERROR("pollfd malloc");
3bd1e081
MD
1954 pthread_mutex_unlock(&consumer_data.lock);
1955 goto end;
1956 }
1957
50f8ae69 1958 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 1959 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
1960 sizeof(struct lttng_consumer_stream));
1961 if (local_stream == NULL) {
7a57cf92 1962 PERROR("local_stream malloc");
3bd1e081
MD
1963 pthread_mutex_unlock(&consumer_data.lock);
1964 goto end;
1965 }
43c34bc3
DG
1966 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
1967 data_ht);
3bd1e081
MD
1968 if (ret < 0) {
1969 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 1970 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1971 pthread_mutex_unlock(&consumer_data.lock);
1972 goto end;
1973 }
1974 nb_fd = ret;
1975 consumer_data.need_update = 0;
1976 }
1977 pthread_mutex_unlock(&consumer_data.lock);
1978
4078b776
MD
1979 /* No FDs and consumer_quit, consumer_cleanup the thread */
1980 if (nb_fd == 0 && consumer_quit == 1) {
1981 goto end;
1982 }
3bd1e081 1983 /* poll on the array of fds */
88f2b785 1984 restart:
3bd1e081
MD
1985 DBG("polling on %d fd", nb_fd + 1);
1986 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
1987 DBG("poll num_rdy : %d", num_rdy);
1988 if (num_rdy == -1) {
88f2b785
MD
1989 /*
1990 * Restart interrupted system call.
1991 */
1992 if (errno == EINTR) {
1993 goto restart;
1994 }
7a57cf92 1995 PERROR("Poll error");
f73fabfd 1996 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1997 goto end;
1998 } else if (num_rdy == 0) {
1999 DBG("Polling thread timed out");
2000 goto end;
2001 }
2002
3bd1e081 2003 /*
50f8ae69 2004 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2005 * beginning of the loop to update the array. We want to prioritize
2006 * array update over low-priority reads.
3bd1e081 2007 */
509bb1cf 2008 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819 2009 size_t pipe_readlen;
04fdd819 2010
50f8ae69 2011 DBG("consumer_data_pipe wake up");
04fdd819
MD
2012 /* Consume 1 byte of pipe data */
2013 do {
50f8ae69 2014 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
c869f647 2015 sizeof(new_stream));
04fdd819 2016 } while (pipe_readlen == -1 && errno == EINTR);
c869f647
DG
2017
2018 /*
2019 * If the stream is NULL, just ignore it. It's also possible that
2020 * the sessiond poll thread changed the consumer_quit state and is
2021 * waking us up to test it.
2022 */
2023 if (new_stream == NULL) {
2024 continue;
2025 }
2026
43c34bc3 2027 ret = consumer_add_stream(new_stream, data_ht);
c869f647
DG
2028 if (ret) {
2029 ERR("Consumer add stream %d failed. Continuing",
2030 new_stream->key);
2031 /*
2032 * At this point, if the add_stream fails, it is not in the
2033 * hash table thus passing the NULL value here.
2034 */
2035 consumer_del_stream(new_stream, NULL);
2036 }
2037
2038 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2039 continue;
2040 }
2041
2042 /* Take care of high priority channels first. */
2043 for (i = 0; i < nb_fd; i++) {
fb3a43a9 2044 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2045 DBG("Urgent read on fd %d", pollfd[i].fd);
2046 high_prio = 1;
4078b776 2047 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2048 /* it's ok to have an unavailable sub-buffer */
b64403e3 2049 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
4078b776
MD
2050 goto end;
2051 } else if (len > 0) {
2052 local_stream[i]->data_read = 1;
d41f73b7 2053 }
3bd1e081
MD
2054 }
2055 }
2056
4078b776
MD
2057 /*
2058 * If we read high prio channel in this loop, try again
2059 * for more high prio data.
2060 */
2061 if (high_prio) {
3bd1e081
MD
2062 continue;
2063 }
2064
2065 /* Take care of low priority channels. */
4078b776
MD
2066 for (i = 0; i < nb_fd; i++) {
2067 if ((pollfd[i].revents & POLLIN) ||
2068 local_stream[i]->hangup_flush_done) {
4078b776
MD
2069 DBG("Normal read on fd %d", pollfd[i].fd);
2070 len = ctx->on_buffer_ready(local_stream[i], ctx);
2071 /* it's ok to have an unavailable sub-buffer */
b64403e3 2072 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
4078b776
MD
2073 goto end;
2074 } else if (len > 0) {
2075 local_stream[i]->data_read = 1;
2076 }
2077 }
2078 }
2079
2080 /* Handle hangup and errors */
2081 for (i = 0; i < nb_fd; i++) {
2082 if (!local_stream[i]->hangup_flush_done
2083 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2084 && (consumer_data.type == LTTNG_CONSUMER32_UST
2085 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2086 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2087 pollfd[i].fd);
2088 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2089 /* Attempt read again, for the data we just flushed. */
2090 local_stream[i]->data_read = 1;
2091 }
2092 /*
2093 * If the poll flag is HUP/ERR/NVAL and we have
2094 * read no data in this pass, we can remove the
2095 * stream from its hash table.
2096 */
2097 if ((pollfd[i].revents & POLLHUP)) {
2098 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2099 if (!local_stream[i]->data_read) {
43c34bc3 2100 consumer_del_stream(local_stream[i], data_ht);
4078b776
MD
2101 num_hup++;
2102 }
2103 } else if (pollfd[i].revents & POLLERR) {
2104 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2105 if (!local_stream[i]->data_read) {
43c34bc3 2106 consumer_del_stream(local_stream[i], data_ht);
4078b776
MD
2107 num_hup++;
2108 }
2109 } else if (pollfd[i].revents & POLLNVAL) {
2110 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2111 if (!local_stream[i]->data_read) {
43c34bc3 2112 consumer_del_stream(local_stream[i], data_ht);
4078b776 2113 num_hup++;
3bd1e081
MD
2114 }
2115 }
4078b776 2116 local_stream[i]->data_read = 0;
3bd1e081
MD
2117 }
2118 }
2119end:
2120 DBG("polling thread exiting");
2121 if (pollfd != NULL) {
2122 free(pollfd);
2123 pollfd = NULL;
2124 }
2125 if (local_stream != NULL) {
2126 free(local_stream);
2127 local_stream = NULL;
2128 }
fb3a43a9
DG
2129
2130 /*
2131 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2132 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2133 * read side of the pipe. If we close them both, epoll_wait strangely does
2134 * not return and could create a endless wait period if the pipe is the
2135 * only tracked fd in the poll set. The thread will take care of closing
2136 * the read side.
fb3a43a9
DG
2137 */
2138 close(ctx->consumer_metadata_pipe[1]);
fb3a43a9 2139
43c34bc3
DG
2140 if (data_ht) {
2141 destroy_data_stream_ht(data_ht);
2142 }
2143
e7b994a3 2144 rcu_unregister_thread();
3bd1e081
MD
2145 return NULL;
2146}
2147
2148/*
2149 * This thread listens on the consumerd socket and receives the file
2150 * descriptors from the session daemon.
2151 */
7d980def 2152void *consumer_thread_sessiond_poll(void *data)
3bd1e081
MD
2153{
2154 int sock, client_socket, ret;
2155 /*
2156 * structure to poll for incoming data on communication socket avoids
2157 * making blocking sockets.
2158 */
2159 struct pollfd consumer_sockpoll[2];
2160 struct lttng_consumer_local_data *ctx = data;
2161
e7b994a3
DG
2162 rcu_register_thread();
2163
3bd1e081
MD
2164 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2165 unlink(ctx->consumer_command_sock_path);
2166 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2167 if (client_socket < 0) {
2168 ERR("Cannot create command socket");
2169 goto end;
2170 }
2171
2172 ret = lttcomm_listen_unix_sock(client_socket);
2173 if (ret < 0) {
2174 goto end;
2175 }
2176
32258573 2177 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2178 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2179 /* return < 0 on error, but == 0 is not fatal */
2180 if (ret < 0) {
32258573 2181 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2182 goto end;
2183 }
2184
2185 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2186 if (ret < 0) {
7a57cf92 2187 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2188 goto end;
2189 }
2190
2191 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2192 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2193 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2194 consumer_sockpoll[1].fd = client_socket;
2195 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2196
2197 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2198 goto end;
2199 }
2200 DBG("Connection on client_socket");
2201
2202 /* Blocking call, waiting for transmission */
2203 sock = lttcomm_accept_unix_sock(client_socket);
2204 if (sock <= 0) {
2205 WARN("On accept");
2206 goto end;
2207 }
2208 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2209 if (ret < 0) {
7a57cf92 2210 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2211 goto end;
2212 }
2213
2214 /* update the polling structure to poll on the established socket */
2215 consumer_sockpoll[1].fd = sock;
2216 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2217
2218 while (1) {
2219 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2220 goto end;
2221 }
2222 DBG("Incoming command on sock");
2223 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2224 if (ret == -ENOENT) {
2225 DBG("Received STOP command");
2226 goto end;
2227 }
4cbc1a04
DG
2228 if (ret <= 0) {
2229 /*
2230 * This could simply be a session daemon quitting. Don't output
2231 * ERR() here.
2232 */
2233 DBG("Communication interrupted on command socket");
3bd1e081
MD
2234 goto end;
2235 }
2236 if (consumer_quit) {
2237 DBG("consumer_thread_receive_fds received quit from signal");
2238 goto end;
2239 }
2240 DBG("received fds on sock");
2241 }
2242end:
2243 DBG("consumer_thread_receive_fds exiting");
2244
2245 /*
2246 * when all fds have hung up, the polling thread
2247 * can exit cleanly
2248 */
2249 consumer_quit = 1;
2250
2251 /*
2252 * 2s of grace period, if no polling events occur during
2253 * this period, the polling thread will exit even if there
2254 * are still open FDs (should not happen, but safety mechanism).
2255 */
2256 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
2257
04fdd819 2258 /*
c869f647
DG
2259 * Notify the data poll thread to poll back again and test the
2260 * consumer_quit state to quit gracefully.
04fdd819
MD
2261 */
2262 do {
c869f647
DG
2263 struct lttng_consumer_stream *null_stream = NULL;
2264
50f8ae69 2265 ret = write(ctx->consumer_data_pipe[1], &null_stream,
c869f647 2266 sizeof(null_stream));
6f94560a 2267 } while (ret < 0 && errno == EINTR);
c869f647 2268
e7b994a3 2269 rcu_unregister_thread();
3bd1e081
MD
2270 return NULL;
2271}
d41f73b7 2272
4078b776 2273ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2274 struct lttng_consumer_local_data *ctx)
2275{
2276 switch (consumer_data.type) {
2277 case LTTNG_CONSUMER_KERNEL:
2278 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
2279 case LTTNG_CONSUMER32_UST:
2280 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2281 return lttng_ustconsumer_read_subbuffer(stream, ctx);
2282 default:
2283 ERR("Unknown consumer_data type");
2284 assert(0);
2285 return -ENOSYS;
2286 }
2287}
2288
2289int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2290{
2291 switch (consumer_data.type) {
2292 case LTTNG_CONSUMER_KERNEL:
2293 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2294 case LTTNG_CONSUMER32_UST:
2295 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2296 return lttng_ustconsumer_on_recv_stream(stream);
2297 default:
2298 ERR("Unknown consumer_data type");
2299 assert(0);
2300 return -ENOSYS;
2301 }
2302}
e4421fec
DG
2303
2304/*
2305 * Allocate and set consumer data hash tables.
2306 */
2307void lttng_consumer_init(void)
2308{
e4421fec 2309 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2310 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
43c34bc3
DG
2311
2312 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2313 assert(metadata_ht);
2314 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2315 assert(data_ht);
e4421fec 2316}
7735ef9e
DG
2317
2318/*
2319 * Process the ADD_RELAYD command receive by a consumer.
2320 *
2321 * This will create a relayd socket pair and add it to the relayd hash table.
2322 * The caller MUST acquire a RCU read side lock before calling it.
2323 */
2324int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2325 struct lttng_consumer_local_data *ctx, int sock,
2326 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
2327{
2328 int fd, ret = -1;
2329 struct consumer_relayd_sock_pair *relayd;
2330
2331 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2332
2333 /* Get relayd reference if exists. */
2334 relayd = consumer_find_relayd(net_seq_idx);
2335 if (relayd == NULL) {
2336 /* Not found. Allocate one. */
2337 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2338 if (relayd == NULL) {
f73fabfd 2339 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2340 goto error;
2341 }
2342 }
2343
2344 /* Poll on consumer socket. */
2345 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2346 ret = -EINTR;
2347 goto error;
2348 }
2349
2350 /* Get relayd socket from session daemon */
2351 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2352 if (ret != sizeof(fd)) {
f73fabfd 2353 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e
DG
2354 ret = -1;
2355 goto error;
2356 }
2357
2358 /* Copy socket information and received FD */
2359 switch (sock_type) {
2360 case LTTNG_STREAM_CONTROL:
2361 /* Copy received lttcomm socket */
2362 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2363 ret = lttcomm_create_sock(&relayd->control_sock);
2364 if (ret < 0) {
2365 goto error;
2366 }
2367
2368 /* Close the created socket fd which is useless */
2369 close(relayd->control_sock.fd);
2370
2371 /* Assign new file descriptor */
2372 relayd->control_sock.fd = fd;
2373 break;
2374 case LTTNG_STREAM_DATA:
2375 /* Copy received lttcomm socket */
2376 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2377 ret = lttcomm_create_sock(&relayd->data_sock);
2378 if (ret < 0) {
2379 goto error;
2380 }
2381
2382 /* Close the created socket fd which is useless */
2383 close(relayd->data_sock.fd);
2384
2385 /* Assign new file descriptor */
2386 relayd->data_sock.fd = fd;
2387 break;
2388 default:
2389 ERR("Unknown relayd socket type (%d)", sock_type);
2390 goto error;
2391 }
2392
2393 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2394 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2395 relayd->net_seq_idx, fd);
2396
2397 /*
2398 * Add relayd socket pair to consumer data hashtable. If object already
2399 * exists or on error, the function gracefully returns.
2400 */
d09e1200 2401 add_relayd(relayd);
7735ef9e
DG
2402
2403 /* All good! */
2404 ret = 0;
2405
2406error:
2407 return ret;
2408}
This page took 0.144275 seconds and 4 git commands to generate.