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