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