Fix: add missing rcu read lock across RCU HT iteration
[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>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
ca4537d3 22#include <fcntl.h>
3bd1e081
MD
23#include <poll.h>
24#include <pthread.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/mman.h>
28#include <sys/socket.h>
29#include <sys/types.h>
30#include <unistd.h>
31
990570ed 32#include <common/common.h>
10a8a223
DG
33#include <common/kernel-ctl/kernel-ctl.h>
34#include <common/sessiond-comm/sessiond-comm.h>
35#include <common/kernel-consumer/kernel-consumer.h>
36#include <common/ust-consumer/ust-consumer.h>
37
38#include "consumer.h"
3bd1e081
MD
39
40struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
41 .stream_count = 0,
42 .need_update = 1,
43 .type = LTTNG_CONSUMER_UNKNOWN,
44};
45
46/* timeout parameter, to control the polling thread grace period. */
47int consumer_poll_timeout = -1;
48
49/*
50 * Flag to inform the polling thread to quit when all fd hung up. Updated by
51 * the consumer_thread_receive_fds when it notices that all fds has hung up.
52 * Also updated by the signal handler (consumer_should_exit()). Read by the
53 * polling threads.
54 */
55volatile int consumer_quit = 0;
56
57/*
58 * Find a stream. The consumer_data.lock must be locked during this
59 * call.
60 */
61static struct lttng_consumer_stream *consumer_find_stream(int key)
62{
e4421fec
DG
63 struct lttng_ht_iter iter;
64 struct lttng_ht_node_ulong *node;
65 struct lttng_consumer_stream *stream = NULL;
3bd1e081 66
7ad0a0cb
MD
67 /* Negative keys are lookup failures */
68 if (key < 0)
69 return NULL;
e4421fec 70
6065ceec
DG
71 rcu_read_lock();
72
e4421fec
DG
73 lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
74 &iter);
75 node = lttng_ht_iter_get_node_ulong(&iter);
76 if (node != NULL) {
77 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 78 }
e4421fec 79
6065ceec
DG
80 rcu_read_unlock();
81
e4421fec 82 return stream;
3bd1e081
MD
83}
84
7ad0a0cb
MD
85static void consumer_steal_stream_key(int key)
86{
87 struct lttng_consumer_stream *stream;
88
89 stream = consumer_find_stream(key);
90 if (stream)
91 stream->key = -1;
92}
93
3bd1e081
MD
94static struct lttng_consumer_channel *consumer_find_channel(int key)
95{
e4421fec
DG
96 struct lttng_ht_iter iter;
97 struct lttng_ht_node_ulong *node;
98 struct lttng_consumer_channel *channel = NULL;
3bd1e081 99
7ad0a0cb
MD
100 /* Negative keys are lookup failures */
101 if (key < 0)
102 return NULL;
e4421fec 103
6065ceec
DG
104 rcu_read_lock();
105
e4421fec
DG
106 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
107 &iter);
108 node = lttng_ht_iter_get_node_ulong(&iter);
109 if (node != NULL) {
110 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 111 }
e4421fec 112
6065ceec
DG
113 rcu_read_unlock();
114
e4421fec 115 return channel;
3bd1e081
MD
116}
117
7ad0a0cb
MD
118static void consumer_steal_channel_key(int key)
119{
120 struct lttng_consumer_channel *channel;
121
122 channel = consumer_find_channel(key);
123 if (channel)
124 channel->key = -1;
125}
126
702b1ea4
MD
127static
128void consumer_free_stream(struct rcu_head *head)
129{
130 struct lttng_ht_node_ulong *node =
131 caa_container_of(head, struct lttng_ht_node_ulong, head);
132 struct lttng_consumer_stream *stream =
133 caa_container_of(node, struct lttng_consumer_stream, node);
134
135 free(stream);
136}
137
3bd1e081
MD
138/*
139 * Remove a stream from the global list protected by a mutex. This
140 * function is also responsible for freeing its data structures.
141 */
142void consumer_del_stream(struct lttng_consumer_stream *stream)
143{
144 int ret;
e4421fec 145 struct lttng_ht_iter iter;
3bd1e081
MD
146 struct lttng_consumer_channel *free_chan = NULL;
147
148 pthread_mutex_lock(&consumer_data.lock);
149
150 switch (consumer_data.type) {
151 case LTTNG_CONSUMER_KERNEL:
152 if (stream->mmap_base != NULL) {
153 ret = munmap(stream->mmap_base, stream->mmap_len);
154 if (ret != 0) {
155 perror("munmap");
156 }
157 }
158 break;
7753dea8
MD
159 case LTTNG_CONSUMER32_UST:
160 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
161 lttng_ustconsumer_del_stream(stream);
162 break;
163 default:
164 ERR("Unknown consumer_data type");
165 assert(0);
166 goto end;
167 }
168
6065ceec
DG
169 rcu_read_lock();
170
e4421fec
DG
171 /* Get stream node from hash table */
172 lttng_ht_lookup(consumer_data.stream_ht,
173 (void *)((unsigned long) stream->key), &iter);
702b1ea4
MD
174 /*
175 * Remove stream node from hash table. It can fail if it's been
176 * replaced due to key reuse.
177 */
178 (void) lttng_ht_del(consumer_data.stream_ht, &iter);
e4421fec 179
6065ceec
DG
180 rcu_read_unlock();
181
3bd1e081
MD
182 if (consumer_data.stream_count <= 0) {
183 goto end;
184 }
185 consumer_data.stream_count--;
186 if (!stream) {
187 goto end;
188 }
189 if (stream->out_fd >= 0) {
4c462e79
MD
190 ret = close(stream->out_fd);
191 if (ret) {
192 PERROR("close");
193 }
3bd1e081 194 }
b5c5fc29 195 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
196 ret = close(stream->wait_fd);
197 if (ret) {
198 PERROR("close");
199 }
3bd1e081 200 }
2c1dd183 201 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
202 ret = close(stream->shm_fd);
203 if (ret) {
204 PERROR("close");
205 }
3bd1e081
MD
206 }
207 if (!--stream->chan->refcount)
208 free_chan = stream->chan;
702b1ea4
MD
209
210 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
211end:
212 consumer_data.need_update = 1;
213 pthread_mutex_unlock(&consumer_data.lock);
214
215 if (free_chan)
216 consumer_del_channel(free_chan);
217}
218
219struct lttng_consumer_stream *consumer_allocate_stream(
220 int channel_key, int stream_key,
221 int shm_fd, int wait_fd,
222 enum lttng_consumer_stream_state state,
223 uint64_t mmap_len,
224 enum lttng_event_output output,
6df2e2c9
MD
225 const char *path_name,
226 uid_t uid,
227 gid_t gid)
3bd1e081
MD
228{
229 struct lttng_consumer_stream *stream;
230 int ret;
231
effcf122 232 stream = zmalloc(sizeof(*stream));
3bd1e081
MD
233 if (stream == NULL) {
234 perror("malloc struct lttng_consumer_stream");
235 goto end;
236 }
237 stream->chan = consumer_find_channel(channel_key);
238 if (!stream->chan) {
239 perror("Unable to find channel key");
240 goto end;
241 }
242 stream->chan->refcount++;
243 stream->key = stream_key;
244 stream->shm_fd = shm_fd;
245 stream->wait_fd = wait_fd;
246 stream->out_fd = -1;
247 stream->out_fd_offset = 0;
248 stream->state = state;
249 stream->mmap_len = mmap_len;
250 stream->mmap_base = NULL;
251 stream->output = output;
6df2e2c9
MD
252 stream->uid = uid;
253 stream->gid = gid;
3bd1e081
MD
254 strncpy(stream->path_name, path_name, PATH_MAX - 1);
255 stream->path_name[PATH_MAX - 1] = '\0';
e4421fec 256 lttng_ht_node_init_ulong(&stream->node, stream->key);
3bd1e081
MD
257
258 switch (consumer_data.type) {
259 case LTTNG_CONSUMER_KERNEL:
260 break;
7753dea8
MD
261 case LTTNG_CONSUMER32_UST:
262 case LTTNG_CONSUMER64_UST:
5af2f756 263 stream->cpu = stream->chan->cpucount++;
3bd1e081
MD
264 ret = lttng_ustconsumer_allocate_stream(stream);
265 if (ret) {
266 free(stream);
267 return NULL;
268 }
269 break;
270 default:
271 ERR("Unknown consumer_data type");
272 assert(0);
273 goto end;
274 }
275 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d)",
276 stream->path_name, stream->key,
277 stream->shm_fd,
278 stream->wait_fd,
279 (unsigned long long) stream->mmap_len,
280 stream->out_fd);
281end:
282 return stream;
283}
284
285/*
286 * Add a stream to the global list protected by a mutex.
287 */
288int consumer_add_stream(struct lttng_consumer_stream *stream)
289{
290 int ret = 0;
291
292 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
293 /* Steal stream identifier, for UST */
294 consumer_steal_stream_key(stream->key);
6065ceec 295 rcu_read_lock();
702b1ea4
MD
296 /*
297 * We simply remove the old channel from the hash table. It's
298 * ok, since we know for sure the sessiond wants to replace it
299 * with the new version, because the key has been reused.
300 */
301 (void) lttng_ht_add_replace_ulong(consumer_data.stream_ht, &stream->node);
6065ceec 302 rcu_read_unlock();
3bd1e081
MD
303 consumer_data.stream_count++;
304 consumer_data.need_update = 1;
305
306 switch (consumer_data.type) {
307 case LTTNG_CONSUMER_KERNEL:
308 break;
7753dea8
MD
309 case LTTNG_CONSUMER32_UST:
310 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
311 /* Streams are in CPU number order (we rely on this) */
312 stream->cpu = stream->chan->nr_streams++;
313 break;
314 default:
315 ERR("Unknown consumer_data type");
316 assert(0);
317 goto end;
318 }
319
320end:
321 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 322
3bd1e081
MD
323 return ret;
324}
325
326/*
327 * Update a stream according to what we just received.
328 */
329void consumer_change_stream_state(int stream_key,
330 enum lttng_consumer_stream_state state)
331{
332 struct lttng_consumer_stream *stream;
333
334 pthread_mutex_lock(&consumer_data.lock);
335 stream = consumer_find_stream(stream_key);
336 if (stream) {
337 stream->state = state;
338 }
339 consumer_data.need_update = 1;
340 pthread_mutex_unlock(&consumer_data.lock);
341}
342
702b1ea4
MD
343static
344void consumer_free_channel(struct rcu_head *head)
345{
346 struct lttng_ht_node_ulong *node =
347 caa_container_of(head, struct lttng_ht_node_ulong, head);
348 struct lttng_consumer_channel *channel =
349 caa_container_of(node, struct lttng_consumer_channel, node);
350
351 free(channel);
352}
353
3bd1e081
MD
354/*
355 * Remove a channel from the global list protected by a mutex. This
356 * function is also responsible for freeing its data structures.
357 */
358void consumer_del_channel(struct lttng_consumer_channel *channel)
359{
360 int ret;
e4421fec 361 struct lttng_ht_iter iter;
3bd1e081
MD
362
363 pthread_mutex_lock(&consumer_data.lock);
364
365 switch (consumer_data.type) {
366 case LTTNG_CONSUMER_KERNEL:
367 break;
7753dea8
MD
368 case LTTNG_CONSUMER32_UST:
369 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
370 lttng_ustconsumer_del_channel(channel);
371 break;
372 default:
373 ERR("Unknown consumer_data type");
374 assert(0);
375 goto end;
376 }
377
6065ceec
DG
378 rcu_read_lock();
379
e4421fec
DG
380 lttng_ht_lookup(consumer_data.channel_ht,
381 (void *)((unsigned long) channel->key), &iter);
702b1ea4
MD
382
383 /*
384 * Remove channel node from hash table. It can fail if it's been
385 * replaced due to key reuse.
386 */
387 (void) lttng_ht_del(consumer_data.channel_ht, &iter);
e4421fec 388
6065ceec
DG
389 rcu_read_unlock();
390
3bd1e081
MD
391 if (channel->mmap_base != NULL) {
392 ret = munmap(channel->mmap_base, channel->mmap_len);
393 if (ret != 0) {
394 perror("munmap");
395 }
396 }
b5c5fc29 397 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
398 ret = close(channel->wait_fd);
399 if (ret) {
400 PERROR("close");
401 }
3bd1e081 402 }
2c1dd183 403 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
404 ret = close(channel->shm_fd);
405 if (ret) {
406 PERROR("close");
407 }
3bd1e081 408 }
702b1ea4
MD
409
410 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
411end:
412 pthread_mutex_unlock(&consumer_data.lock);
413}
414
415struct lttng_consumer_channel *consumer_allocate_channel(
416 int channel_key,
417 int shm_fd, int wait_fd,
418 uint64_t mmap_len,
419 uint64_t max_sb_size)
420{
421 struct lttng_consumer_channel *channel;
422 int ret;
423
276b26d1 424 channel = zmalloc(sizeof(*channel));
3bd1e081
MD
425 if (channel == NULL) {
426 perror("malloc struct lttng_consumer_channel");
427 goto end;
428 }
429 channel->key = channel_key;
430 channel->shm_fd = shm_fd;
431 channel->wait_fd = wait_fd;
432 channel->mmap_len = mmap_len;
433 channel->max_sb_size = max_sb_size;
434 channel->refcount = 0;
435 channel->nr_streams = 0;
e4421fec 436 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
437
438 switch (consumer_data.type) {
439 case LTTNG_CONSUMER_KERNEL:
440 channel->mmap_base = NULL;
441 channel->mmap_len = 0;
442 break;
7753dea8
MD
443 case LTTNG_CONSUMER32_UST:
444 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
445 ret = lttng_ustconsumer_allocate_channel(channel);
446 if (ret) {
447 free(channel);
448 return NULL;
449 }
450 break;
451 default:
452 ERR("Unknown consumer_data type");
453 assert(0);
454 goto end;
455 }
456 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
457 channel->key,
458 channel->shm_fd,
459 channel->wait_fd,
460 (unsigned long long) channel->mmap_len,
461 (unsigned long long) channel->max_sb_size);
462end:
463 return channel;
464}
465
466/*
467 * Add a channel to the global list protected by a mutex.
468 */
469int consumer_add_channel(struct lttng_consumer_channel *channel)
470{
3bd1e081 471 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
472 /* Steal channel identifier, for UST */
473 consumer_steal_channel_key(channel->key);
6065ceec 474 rcu_read_lock();
702b1ea4
MD
475 /*
476 * We simply remove the old channel from the hash table. It's
477 * ok, since we know for sure the sessiond wants to replace it
478 * with the new version, because the key has been reused.
479 */
480 (void) lttng_ht_add_replace_ulong(consumer_data.channel_ht, &channel->node);
6065ceec 481 rcu_read_unlock();
3bd1e081 482 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 483
7ad0a0cb 484 return 0;
3bd1e081
MD
485}
486
487/*
488 * Allocate the pollfd structure and the local view of the out fds to avoid
489 * doing a lookup in the linked list and concurrency issues when writing is
490 * needed. Called with consumer_data.lock held.
491 *
492 * Returns the number of fds in the structures.
493 */
494int consumer_update_poll_array(
495 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
496 struct lttng_consumer_stream **local_stream)
497{
3bd1e081 498 int i = 0;
e4421fec
DG
499 struct lttng_ht_iter iter;
500 struct lttng_consumer_stream *stream;
3bd1e081
MD
501
502 DBG("Updating poll fd array");
481d6c57 503 rcu_read_lock();
e4421fec
DG
504 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
505 node.node) {
506 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
3bd1e081
MD
507 continue;
508 }
e4421fec
DG
509 DBG("Active FD %d", stream->wait_fd);
510 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 511 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 512 local_stream[i] = stream;
3bd1e081
MD
513 i++;
514 }
481d6c57 515 rcu_read_unlock();
3bd1e081
MD
516
517 /*
518 * Insert the consumer_poll_pipe at the end of the array and don't
519 * increment i so nb_fd is the number of real FD.
520 */
521 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
522 (*pollfd)[i].events = POLLIN;
523 return i;
524}
525
526/*
527 * Poll on the should_quit pipe and the command socket return -1 on error and
528 * should exit, 0 if data is available on the command socket
529 */
530int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
531{
532 int num_rdy;
533
a8b0b341 534restart:
3bd1e081
MD
535 num_rdy = poll(consumer_sockpoll, 2, -1);
536 if (num_rdy == -1) {
a8b0b341
MD
537 /*
538 * Restart interrupted system call.
539 */
540 if (errno == EINTR) {
541 goto restart;
542 }
3bd1e081
MD
543 perror("Poll error");
544 goto exit;
545 }
546 if (consumer_sockpoll[0].revents == POLLIN) {
547 DBG("consumer_should_quit wake up");
548 goto exit;
549 }
550 return 0;
551
552exit:
553 return -1;
554}
555
556/*
557 * Set the error socket.
558 */
559void lttng_consumer_set_error_sock(
560 struct lttng_consumer_local_data *ctx, int sock)
561{
562 ctx->consumer_error_socket = sock;
563}
564
565/*
566 * Set the command socket path.
567 */
568
569void lttng_consumer_set_command_sock_path(
570 struct lttng_consumer_local_data *ctx, char *sock)
571{
572 ctx->consumer_command_sock_path = sock;
573}
574
575/*
576 * Send return code to the session daemon.
577 * If the socket is not defined, we return 0, it is not a fatal error
578 */
579int lttng_consumer_send_error(
580 struct lttng_consumer_local_data *ctx, int cmd)
581{
582 if (ctx->consumer_error_socket > 0) {
583 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
584 sizeof(enum lttcomm_sessiond_command));
585 }
586
587 return 0;
588}
589
590/*
591 * Close all the tracefiles and stream fds, should be called when all instances
592 * are destroyed.
593 */
594void lttng_consumer_cleanup(void)
595{
e4421fec 596 struct lttng_ht_iter iter;
6065ceec
DG
597 struct lttng_ht_node_ulong *node;
598
599 rcu_read_lock();
3bd1e081
MD
600
601 /*
6065ceec
DG
602 * close all outfd. Called when there are no more threads running (after
603 * joining on the threads), no need to protect list iteration with mutex.
3bd1e081 604 */
6065ceec
DG
605 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
606 node) {
702b1ea4
MD
607 struct lttng_consumer_stream *stream =
608 caa_container_of(node, struct lttng_consumer_stream, node);
609 consumer_del_stream(stream);
3bd1e081 610 }
e4421fec 611
6065ceec
DG
612 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
613 node) {
702b1ea4
MD
614 struct lttng_consumer_channel *channel =
615 caa_container_of(node, struct lttng_consumer_channel, node);
616 consumer_del_channel(channel);
3bd1e081 617 }
6065ceec
DG
618
619 rcu_read_unlock();
3bd1e081
MD
620}
621
622/*
623 * Called from signal handler.
624 */
625void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
626{
627 int ret;
628 consumer_quit = 1;
629 ret = write(ctx->consumer_should_quit[1], "4", 1);
630 if (ret < 0) {
631 perror("write consumer quit");
632 }
633}
634
635void lttng_consumer_sync_trace_file(
636 struct lttng_consumer_stream *stream, off_t orig_offset)
637{
638 int outfd = stream->out_fd;
639
640 /*
641 * This does a blocking write-and-wait on any page that belongs to the
642 * subbuffer prior to the one we just wrote.
643 * Don't care about error values, as these are just hints and ways to
644 * limit the amount of page cache used.
645 */
646 if (orig_offset < stream->chan->max_sb_size) {
647 return;
648 }
ca4537d3 649 sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
650 stream->chan->max_sb_size,
651 SYNC_FILE_RANGE_WAIT_BEFORE
652 | SYNC_FILE_RANGE_WRITE
653 | SYNC_FILE_RANGE_WAIT_AFTER);
654 /*
655 * Give hints to the kernel about how we access the file:
656 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
657 * we write it.
658 *
659 * We need to call fadvise again after the file grows because the
660 * kernel does not seem to apply fadvise to non-existing parts of the
661 * file.
662 *
663 * Call fadvise _after_ having waited for the page writeback to
664 * complete because the dirty page writeback semantic is not well
665 * defined. So it can be expected to lead to lower throughput in
666 * streaming.
667 */
668 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
669 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
670}
671
672/*
673 * Initialise the necessary environnement :
674 * - create a new context
675 * - create the poll_pipe
676 * - create the should_quit pipe (for signal handler)
677 * - create the thread pipe (for splice)
678 *
679 * Takes a function pointer as argument, this function is called when data is
680 * available on a buffer. This function is responsible to do the
681 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
682 * buffer configuration and then kernctl_put_next_subbuf at the end.
683 *
684 * Returns a pointer to the new context or NULL on error.
685 */
686struct lttng_consumer_local_data *lttng_consumer_create(
687 enum lttng_consumer_type type,
f5204c94 688 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 689 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
690 int (*recv_channel)(struct lttng_consumer_channel *channel),
691 int (*recv_stream)(struct lttng_consumer_stream *stream),
692 int (*update_stream)(int stream_key, uint32_t state))
693{
694 int ret, i;
695 struct lttng_consumer_local_data *ctx;
696
697 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
698 consumer_data.type == type);
699 consumer_data.type = type;
700
effcf122 701 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081
MD
702 if (ctx == NULL) {
703 perror("allocating context");
704 goto error;
705 }
706
707 ctx->consumer_error_socket = -1;
708 /* assign the callbacks */
709 ctx->on_buffer_ready = buffer_ready;
710 ctx->on_recv_channel = recv_channel;
711 ctx->on_recv_stream = recv_stream;
712 ctx->on_update_stream = update_stream;
713
714 ret = pipe(ctx->consumer_poll_pipe);
715 if (ret < 0) {
716 perror("Error creating poll pipe");
717 goto error_poll_pipe;
718 }
719
720 ret = pipe(ctx->consumer_should_quit);
721 if (ret < 0) {
722 perror("Error creating recv pipe");
723 goto error_quit_pipe;
724 }
725
726 ret = pipe(ctx->consumer_thread_pipe);
727 if (ret < 0) {
728 perror("Error creating thread pipe");
729 goto error_thread_pipe;
730 }
731
732 return ctx;
733
734
735error_thread_pipe:
736 for (i = 0; i < 2; i++) {
737 int err;
738
739 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
740 if (err) {
741 PERROR("close");
742 }
3bd1e081
MD
743 }
744error_quit_pipe:
745 for (i = 0; i < 2; i++) {
746 int err;
747
748 err = close(ctx->consumer_poll_pipe[i]);
4c462e79
MD
749 if (err) {
750 PERROR("close");
751 }
3bd1e081
MD
752 }
753error_poll_pipe:
754 free(ctx);
755error:
756 return NULL;
757}
758
759/*
760 * Close all fds associated with the instance and free the context.
761 */
762void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
763{
4c462e79
MD
764 int ret;
765
766 ret = close(ctx->consumer_error_socket);
767 if (ret) {
768 PERROR("close");
769 }
770 ret = close(ctx->consumer_thread_pipe[0]);
771 if (ret) {
772 PERROR("close");
773 }
774 ret = close(ctx->consumer_thread_pipe[1]);
775 if (ret) {
776 PERROR("close");
777 }
778 ret = close(ctx->consumer_poll_pipe[0]);
779 if (ret) {
780 PERROR("close");
781 }
782 ret = close(ctx->consumer_poll_pipe[1]);
783 if (ret) {
784 PERROR("close");
785 }
786 ret = close(ctx->consumer_should_quit[0]);
787 if (ret) {
788 PERROR("close");
789 }
790 ret = close(ctx->consumer_should_quit[1]);
791 if (ret) {
792 PERROR("close");
793 }
3bd1e081
MD
794 unlink(ctx->consumer_command_sock_path);
795 free(ctx);
796}
797
798/*
799 * Mmap the ring buffer, read it and write the data to the tracefile.
800 *
801 * Returns the number of bytes written
802 */
f5204c94 803ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081
MD
804 struct lttng_consumer_local_data *ctx,
805 struct lttng_consumer_stream *stream, unsigned long len)
806{
807 switch (consumer_data.type) {
808 case LTTNG_CONSUMER_KERNEL:
809 return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len);
7753dea8
MD
810 case LTTNG_CONSUMER32_UST:
811 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
812 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len);
813 default:
814 ERR("Unknown consumer_data type");
815 assert(0);
816 }
817}
818
819/*
820 * Splice the data from the ring buffer to the tracefile.
821 *
822 * Returns the number of bytes spliced.
823 */
f5204c94 824ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081
MD
825 struct lttng_consumer_local_data *ctx,
826 struct lttng_consumer_stream *stream, unsigned long len)
827{
828 switch (consumer_data.type) {
829 case LTTNG_CONSUMER_KERNEL:
830 return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len);
7753dea8
MD
831 case LTTNG_CONSUMER32_UST:
832 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
833 return -ENOSYS;
834 default:
835 ERR("Unknown consumer_data type");
836 assert(0);
837 return -ENOSYS;
838 }
839
840}
841
842/*
843 * Take a snapshot for a specific fd
844 *
845 * Returns 0 on success, < 0 on error
846 */
847int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
848 struct lttng_consumer_stream *stream)
849{
850 switch (consumer_data.type) {
851 case LTTNG_CONSUMER_KERNEL:
852 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
853 case LTTNG_CONSUMER32_UST:
854 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
855 return lttng_ustconsumer_take_snapshot(ctx, stream);
856 default:
857 ERR("Unknown consumer_data type");
858 assert(0);
859 return -ENOSYS;
860 }
861
862}
863
864/*
865 * Get the produced position
866 *
867 * Returns 0 on success, < 0 on error
868 */
869int lttng_consumer_get_produced_snapshot(
870 struct lttng_consumer_local_data *ctx,
871 struct lttng_consumer_stream *stream,
872 unsigned long *pos)
873{
874 switch (consumer_data.type) {
875 case LTTNG_CONSUMER_KERNEL:
876 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
877 case LTTNG_CONSUMER32_UST:
878 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
879 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
880 default:
881 ERR("Unknown consumer_data type");
882 assert(0);
883 return -ENOSYS;
884 }
885}
886
887int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
888 int sock, struct pollfd *consumer_sockpoll)
889{
890 switch (consumer_data.type) {
891 case LTTNG_CONSUMER_KERNEL:
892 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
893 case LTTNG_CONSUMER32_UST:
894 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
895 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
896 default:
897 ERR("Unknown consumer_data type");
898 assert(0);
899 return -ENOSYS;
900 }
901}
902
903/*
e4421fec 904 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
905 * it to tracefile if necessary.
906 */
907void *lttng_consumer_thread_poll_fds(void *data)
908{
909 int num_rdy, num_hup, high_prio, ret, i;
910 struct pollfd *pollfd = NULL;
911 /* local view of the streams */
912 struct lttng_consumer_stream **local_stream = NULL;
913 /* local view of consumer_data.fds_count */
914 int nb_fd = 0;
915 char tmp;
916 int tmp2;
917 struct lttng_consumer_local_data *ctx = data;
918
e7b994a3
DG
919 rcu_register_thread();
920
effcf122 921 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
922
923 while (1) {
924 high_prio = 0;
925 num_hup = 0;
926
927 /*
e4421fec 928 * the fds set has been updated, we need to update our
3bd1e081
MD
929 * local array as well
930 */
931 pthread_mutex_lock(&consumer_data.lock);
932 if (consumer_data.need_update) {
933 if (pollfd != NULL) {
934 free(pollfd);
935 pollfd = NULL;
936 }
937 if (local_stream != NULL) {
938 free(local_stream);
939 local_stream = NULL;
940 }
941
942 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 943 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081
MD
944 if (pollfd == NULL) {
945 perror("pollfd malloc");
946 pthread_mutex_unlock(&consumer_data.lock);
947 goto end;
948 }
949
950 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 951 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
952 sizeof(struct lttng_consumer_stream));
953 if (local_stream == NULL) {
954 perror("local_stream malloc");
955 pthread_mutex_unlock(&consumer_data.lock);
956 goto end;
957 }
958 ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
959 if (ret < 0) {
960 ERR("Error in allocating pollfd or local_outfds");
961 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
962 pthread_mutex_unlock(&consumer_data.lock);
963 goto end;
964 }
965 nb_fd = ret;
966 consumer_data.need_update = 0;
967 }
968 pthread_mutex_unlock(&consumer_data.lock);
969
f5204c94
MD
970 /* No FDs and consumer_quit, consumer_cleanup the thread */
971 if (nb_fd == 0 && consumer_quit == 1) {
972 goto end;
973 }
3bd1e081 974 /* poll on the array of fds */
a8b0b341 975 restart:
3bd1e081
MD
976 DBG("polling on %d fd", nb_fd + 1);
977 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
978 DBG("poll num_rdy : %d", num_rdy);
979 if (num_rdy == -1) {
a8b0b341
MD
980 /*
981 * Restart interrupted system call.
982 */
983 if (errno == EINTR) {
984 goto restart;
985 }
3bd1e081
MD
986 perror("Poll error");
987 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
988 goto end;
989 } else if (num_rdy == 0) {
990 DBG("Polling thread timed out");
991 goto end;
992 }
993
3bd1e081
MD
994 /*
995 * If the consumer_poll_pipe triggered poll go
996 * directly to the beginning of the loop to update the
997 * array. We want to prioritize array update over
998 * low-priority reads.
999 */
d41f73b7 1000 if (pollfd[nb_fd].revents & POLLIN) {
3bd1e081
MD
1001 DBG("consumer_poll_pipe wake up");
1002 tmp2 = read(ctx->consumer_poll_pipe[0], &tmp, 1);
1003 if (tmp2 < 0) {
d41f73b7 1004 perror("read consumer poll");
3bd1e081
MD
1005 }
1006 continue;
1007 }
1008
1009 /* Take care of high priority channels first. */
1010 for (i = 0; i < nb_fd; i++) {
d41f73b7 1011 if (pollfd[i].revents & POLLPRI) {
f5204c94
MD
1012 ssize_t len;
1013
d41f73b7
MD
1014 DBG("Urgent read on fd %d", pollfd[i].fd);
1015 high_prio = 1;
f5204c94 1016 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 1017 /* it's ok to have an unavailable sub-buffer */
f5204c94
MD
1018 if (len < 0 && len != -EAGAIN) {
1019 goto end;
1020 } else if (len > 0) {
1021 local_stream[i]->data_read = 1;
d41f73b7 1022 }
3bd1e081
MD
1023 }
1024 }
1025
f5204c94
MD
1026 /*
1027 * If we read high prio channel in this loop, try again
1028 * for more high prio data.
1029 */
1030 if (high_prio) {
3bd1e081
MD
1031 continue;
1032 }
1033
1034 /* Take care of low priority channels. */
f5204c94
MD
1035 for (i = 0; i < nb_fd; i++) {
1036 if ((pollfd[i].revents & POLLIN) ||
1037 local_stream[i]->hangup_flush_done) {
1038 ssize_t len;
1039
1040 assert(!(pollfd[i].revents & POLLERR));
1041 assert(!(pollfd[i].revents & POLLNVAL));
1042 DBG("Normal read on fd %d", pollfd[i].fd);
1043 len = ctx->on_buffer_ready(local_stream[i], ctx);
1044 /* it's ok to have an unavailable sub-buffer */
1045 if (len < 0 && len != -EAGAIN) {
1046 goto end;
1047 } else if (len > 0) {
1048 local_stream[i]->data_read = 1;
1049 }
1050 }
1051 }
1052
1053 /* Handle hangup and errors */
1054 for (i = 0; i < nb_fd; i++) {
1055 if (!local_stream[i]->hangup_flush_done
1056 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
1057 && (consumer_data.type == LTTNG_CONSUMER32_UST
1058 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1059 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1060 pollfd[i].fd);
1061 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
1062 /* Attempt read again, for the data we just flushed. */
1063 local_stream[i]->data_read = 1;
1064 }
1065 /*
1066 * If the poll flag is HUP/ERR/NVAL and we have
1067 * read no data in this pass, we can remove the
1068 * stream from its hash table.
1069 */
1070 if ((pollfd[i].revents & POLLHUP)) {
1071 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
1072 if (!local_stream[i]->data_read) {
702b1ea4 1073 consumer_del_stream(local_stream[i]);
f5204c94
MD
1074 num_hup++;
1075 }
1076 } else if (pollfd[i].revents & POLLERR) {
1077 ERR("Error returned in polling fd %d.", pollfd[i].fd);
1078 if (!local_stream[i]->data_read) {
702b1ea4 1079 consumer_del_stream(local_stream[i]);
f5204c94
MD
1080 num_hup++;
1081 }
1082 } else if (pollfd[i].revents & POLLNVAL) {
1083 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
1084 if (!local_stream[i]->data_read) {
702b1ea4 1085 consumer_del_stream(local_stream[i]);
f5204c94 1086 num_hup++;
3bd1e081
MD
1087 }
1088 }
f5204c94 1089 local_stream[i]->data_read = 0;
3bd1e081
MD
1090 }
1091 }
1092end:
1093 DBG("polling thread exiting");
1094 if (pollfd != NULL) {
1095 free(pollfd);
1096 pollfd = NULL;
1097 }
1098 if (local_stream != NULL) {
1099 free(local_stream);
1100 local_stream = NULL;
1101 }
e7b994a3 1102 rcu_unregister_thread();
3bd1e081
MD
1103 return NULL;
1104}
1105
1106/*
1107 * This thread listens on the consumerd socket and receives the file
1108 * descriptors from the session daemon.
1109 */
1110void *lttng_consumer_thread_receive_fds(void *data)
1111{
1112 int sock, client_socket, ret;
1113 /*
1114 * structure to poll for incoming data on communication socket avoids
1115 * making blocking sockets.
1116 */
1117 struct pollfd consumer_sockpoll[2];
1118 struct lttng_consumer_local_data *ctx = data;
1119
e7b994a3
DG
1120 rcu_register_thread();
1121
3bd1e081
MD
1122 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
1123 unlink(ctx->consumer_command_sock_path);
1124 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
1125 if (client_socket < 0) {
1126 ERR("Cannot create command socket");
1127 goto end;
1128 }
1129
1130 ret = lttcomm_listen_unix_sock(client_socket);
1131 if (ret < 0) {
1132 goto end;
1133 }
1134
32258573 1135 DBG("Sending ready command to lttng-sessiond");
3bd1e081
MD
1136 ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
1137 /* return < 0 on error, but == 0 is not fatal */
1138 if (ret < 0) {
32258573 1139 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
1140 goto end;
1141 }
1142
1143 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
1144 if (ret < 0) {
1145 perror("fcntl O_NONBLOCK");
1146 goto end;
1147 }
1148
1149 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1150 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
1151 consumer_sockpoll[0].events = POLLIN | POLLPRI;
1152 consumer_sockpoll[1].fd = client_socket;
1153 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1154
1155 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1156 goto end;
1157 }
1158 DBG("Connection on client_socket");
1159
1160 /* Blocking call, waiting for transmission */
1161 sock = lttcomm_accept_unix_sock(client_socket);
1162 if (sock <= 0) {
1163 WARN("On accept");
1164 goto end;
1165 }
1166 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1167 if (ret < 0) {
1168 perror("fcntl O_NONBLOCK");
1169 goto end;
1170 }
1171
1172 /* update the polling structure to poll on the established socket */
1173 consumer_sockpoll[1].fd = sock;
1174 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1175
1176 while (1) {
1177 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1178 goto end;
1179 }
1180 DBG("Incoming command on sock");
1181 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
1182 if (ret == -ENOENT) {
1183 DBG("Received STOP command");
1184 goto end;
1185 }
1186 if (ret < 0) {
1187 ERR("Communication interrupted on command socket");
1188 goto end;
1189 }
1190 if (consumer_quit) {
1191 DBG("consumer_thread_receive_fds received quit from signal");
1192 goto end;
1193 }
1194 DBG("received fds on sock");
1195 }
1196end:
1197 DBG("consumer_thread_receive_fds exiting");
1198
1199 /*
1200 * when all fds have hung up, the polling thread
1201 * can exit cleanly
1202 */
1203 consumer_quit = 1;
1204
1205 /*
1206 * 2s of grace period, if no polling events occur during
1207 * this period, the polling thread will exit even if there
1208 * are still open FDs (should not happen, but safety mechanism).
1209 */
1210 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
1211
1212 /* wake up the polling thread */
1213 ret = write(ctx->consumer_poll_pipe[1], "4", 1);
1214 if (ret < 0) {
1215 perror("poll pipe write");
1216 }
e7b994a3 1217 rcu_unregister_thread();
3bd1e081
MD
1218 return NULL;
1219}
d41f73b7 1220
f5204c94 1221ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
1222 struct lttng_consumer_local_data *ctx)
1223{
1224 switch (consumer_data.type) {
1225 case LTTNG_CONSUMER_KERNEL:
1226 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
1227 case LTTNG_CONSUMER32_UST:
1228 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
1229 return lttng_ustconsumer_read_subbuffer(stream, ctx);
1230 default:
1231 ERR("Unknown consumer_data type");
1232 assert(0);
1233 return -ENOSYS;
1234 }
1235}
1236
1237int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
1238{
1239 switch (consumer_data.type) {
1240 case LTTNG_CONSUMER_KERNEL:
1241 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
1242 case LTTNG_CONSUMER32_UST:
1243 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
1244 return lttng_ustconsumer_on_recv_stream(stream);
1245 default:
1246 ERR("Unknown consumer_data type");
1247 assert(0);
1248 return -ENOSYS;
1249 }
1250}
e4421fec
DG
1251
1252/*
1253 * Allocate and set consumer data hash tables.
1254 */
1255void lttng_consumer_init(void)
1256{
1257 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1258 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1259}
1260
This page took 0.080125 seconds and 4 git commands to generate.