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