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