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