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