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