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