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