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