Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
17 | */ |
18 | ||
19 | #define _GNU_SOURCE | |
6c1c0768 | 20 | #define _LGPL_SOURCE |
3bd1e081 | 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> | |
77c7c900 | 29 | #include <inttypes.h> |
3bd1e081 | 30 | #include <unistd.h> |
dbb5dfe6 | 31 | #include <sys/stat.h> |
3bd1e081 | 32 | |
51a9e1c7 | 33 | #include <bin/lttng-consumerd/health-consumerd.h> |
990570ed | 34 | #include <common/common.h> |
10a8a223 | 35 | #include <common/kernel-ctl/kernel-ctl.h> |
10a8a223 | 36 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 37 | #include <common/sessiond-comm/relayd.h> |
dbb5dfe6 | 38 | #include <common/compat/fcntl.h> |
f263b7fd | 39 | #include <common/compat/endian.h> |
acdb9057 | 40 | #include <common/pipe.h> |
00e2e675 | 41 | #include <common/relayd/relayd.h> |
fe4477ee | 42 | #include <common/utils.h> |
07b86b52 | 43 | #include <common/consumer-stream.h> |
309167d2 | 44 | #include <common/index/index.h> |
d3e2ba59 | 45 | #include <common/consumer-timer.h> |
0857097f | 46 | |
10a8a223 | 47 | #include "kernel-consumer.h" |
3bd1e081 MD |
48 | |
49 | extern struct lttng_consumer_global_data consumer_data; | |
50 | extern int consumer_poll_timeout; | |
51 | extern volatile int consumer_quit; | |
52 | ||
3bd1e081 MD |
53 | /* |
54 | * Take a snapshot for a specific fd | |
55 | * | |
56 | * Returns 0 on success, < 0 on error | |
57 | */ | |
ffe60014 | 58 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
59 | { |
60 | int ret = 0; | |
61 | int infd = stream->wait_fd; | |
62 | ||
63 | ret = kernctl_snapshot(infd); | |
64 | if (ret != 0) { | |
5a510c9f | 65 | PERROR("Getting sub-buffer snapshot."); |
56591bac | 66 | ret = -errno; |
3bd1e081 MD |
67 | } |
68 | ||
69 | return ret; | |
70 | } | |
71 | ||
72 | /* | |
73 | * Get the produced position | |
74 | * | |
75 | * Returns 0 on success, < 0 on error | |
76 | */ | |
ffe60014 | 77 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
78 | unsigned long *pos) |
79 | { | |
80 | int ret; | |
81 | int infd = stream->wait_fd; | |
82 | ||
83 | ret = kernctl_snapshot_get_produced(infd, pos); | |
84 | if (ret != 0) { | |
5a510c9f | 85 | PERROR("kernctl_snapshot_get_produced"); |
56591bac | 86 | ret = -errno; |
3bd1e081 MD |
87 | } |
88 | ||
89 | return ret; | |
90 | } | |
91 | ||
07b86b52 JD |
92 | /* |
93 | * Get the consumerd position | |
94 | * | |
95 | * Returns 0 on success, < 0 on error | |
96 | */ | |
97 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
98 | unsigned long *pos) | |
99 | { | |
100 | int ret; | |
101 | int infd = stream->wait_fd; | |
102 | ||
103 | ret = kernctl_snapshot_get_consumed(infd, pos); | |
104 | if (ret != 0) { | |
5a510c9f | 105 | PERROR("kernctl_snapshot_get_consumed"); |
56591bac | 106 | ret = -errno; |
07b86b52 JD |
107 | } |
108 | ||
109 | return ret; | |
110 | } | |
111 | ||
07b86b52 JD |
112 | /* |
113 | * Take a snapshot of all the stream of a channel | |
114 | * | |
115 | * Returns 0 on success, < 0 on error | |
116 | */ | |
117 | int lttng_kconsumer_snapshot_channel(uint64_t key, char *path, | |
5c786ded JD |
118 | uint64_t relayd_id, uint64_t max_stream_size, |
119 | struct lttng_consumer_local_data *ctx) | |
07b86b52 JD |
120 | { |
121 | int ret; | |
122 | unsigned long consumed_pos, produced_pos; | |
123 | struct lttng_consumer_channel *channel; | |
124 | struct lttng_consumer_stream *stream; | |
125 | ||
6a00837f | 126 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
07b86b52 JD |
127 | |
128 | rcu_read_lock(); | |
129 | ||
130 | channel = consumer_find_channel(key); | |
131 | if (!channel) { | |
6a00837f | 132 | ERR("No channel found for key %" PRIu64, key); |
07b86b52 JD |
133 | ret = -1; |
134 | goto end; | |
135 | } | |
136 | ||
137 | /* Splice is not supported yet for channel snapshot. */ | |
138 | if (channel->output != CONSUMER_CHANNEL_MMAP) { | |
139 | ERR("Unsupported output %d", channel->output); | |
140 | ret = -1; | |
141 | goto end; | |
142 | } | |
143 | ||
10a50311 | 144 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
9ce5646a MD |
145 | |
146 | health_code_update(); | |
147 | ||
07b86b52 JD |
148 | /* |
149 | * Lock stream because we are about to change its state. | |
150 | */ | |
151 | pthread_mutex_lock(&stream->lock); | |
152 | ||
29decac3 DG |
153 | /* |
154 | * Assign the received relayd ID so we can use it for streaming. The streams | |
155 | * are not visible to anyone so this is OK to change it. | |
156 | */ | |
07b86b52 JD |
157 | stream->net_seq_idx = relayd_id; |
158 | channel->relayd_id = relayd_id; | |
159 | if (relayd_id != (uint64_t) -1ULL) { | |
10a50311 | 160 | ret = consumer_send_relayd_stream(stream, path); |
07b86b52 JD |
161 | if (ret < 0) { |
162 | ERR("sending stream to relayd"); | |
163 | goto end_unlock; | |
164 | } | |
07b86b52 JD |
165 | } else { |
166 | ret = utils_create_stream_file(path, stream->name, | |
10a50311 JD |
167 | stream->chan->tracefile_size, |
168 | stream->tracefile_count_current, | |
309167d2 | 169 | stream->uid, stream->gid, NULL); |
07b86b52 JD |
170 | if (ret < 0) { |
171 | ERR("utils_create_stream_file"); | |
172 | goto end_unlock; | |
173 | } | |
174 | ||
175 | stream->out_fd = ret; | |
176 | stream->tracefile_size_current = 0; | |
177 | ||
81ea21bf MD |
178 | DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")", |
179 | path, stream->name, stream->key); | |
07b86b52 | 180 | } |
601262d6 JD |
181 | if (relayd_id != -1ULL) { |
182 | ret = consumer_send_relayd_streams_sent(relayd_id); | |
183 | if (ret < 0) { | |
184 | ERR("sending streams sent to relayd"); | |
185 | goto end_unlock; | |
186 | } | |
a4baae1b | 187 | } |
07b86b52 JD |
188 | |
189 | ret = kernctl_buffer_flush(stream->wait_fd); | |
190 | if (ret < 0) { | |
10a50311 | 191 | ERR("Failed to flush kernel stream"); |
56591bac | 192 | ret = -errno; |
07b86b52 JD |
193 | goto end_unlock; |
194 | } | |
195 | ||
196 | ret = lttng_kconsumer_take_snapshot(stream); | |
197 | if (ret < 0) { | |
198 | ERR("Taking kernel snapshot"); | |
199 | goto end_unlock; | |
200 | } | |
201 | ||
202 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); | |
203 | if (ret < 0) { | |
204 | ERR("Produced kernel snapshot position"); | |
205 | goto end_unlock; | |
206 | } | |
207 | ||
208 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
209 | if (ret < 0) { | |
210 | ERR("Consumerd kernel snapshot position"); | |
211 | goto end_unlock; | |
212 | } | |
213 | ||
214 | if (stream->max_sb_size == 0) { | |
215 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, | |
216 | &stream->max_sb_size); | |
217 | if (ret < 0) { | |
218 | ERR("Getting kernel max_sb_size"); | |
56591bac | 219 | ret = -errno; |
07b86b52 JD |
220 | goto end_unlock; |
221 | } | |
222 | } | |
223 | ||
5c786ded JD |
224 | /* |
225 | * The original value is sent back if max stream size is larger than | |
226 | * the possible size of the snapshot. Also, we asume that the session | |
227 | * daemon should never send a maximum stream size that is lower than | |
228 | * subbuffer size. | |
229 | */ | |
230 | consumed_pos = consumer_get_consumed_maxsize(consumed_pos, | |
231 | produced_pos, max_stream_size); | |
232 | ||
07b86b52 JD |
233 | while (consumed_pos < produced_pos) { |
234 | ssize_t read_len; | |
235 | unsigned long len, padded_len; | |
236 | ||
9ce5646a MD |
237 | health_code_update(); |
238 | ||
07b86b52 JD |
239 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); |
240 | ||
241 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); | |
242 | if (ret < 0) { | |
243 | if (errno != EAGAIN) { | |
244 | PERROR("kernctl_get_subbuf snapshot"); | |
56591bac | 245 | ret = -errno; |
07b86b52 JD |
246 | goto end_unlock; |
247 | } | |
248 | DBG("Kernel consumer get subbuf failed. Skipping it."); | |
249 | consumed_pos += stream->max_sb_size; | |
250 | continue; | |
251 | } | |
252 | ||
253 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); | |
254 | if (ret < 0) { | |
255 | ERR("Snapshot kernctl_get_subbuf_size"); | |
56591bac | 256 | ret = -errno; |
29decac3 | 257 | goto error_put_subbuf; |
07b86b52 JD |
258 | } |
259 | ||
260 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); | |
261 | if (ret < 0) { | |
262 | ERR("Snapshot kernctl_get_padded_subbuf_size"); | |
56591bac | 263 | ret = -errno; |
29decac3 | 264 | goto error_put_subbuf; |
07b86b52 JD |
265 | } |
266 | ||
267 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
309167d2 | 268 | padded_len - len, NULL); |
07b86b52 | 269 | /* |
29decac3 DG |
270 | * We write the padded len in local tracefiles but the data len |
271 | * when using a relay. Display the error but continue processing | |
272 | * to try to release the subbuffer. | |
07b86b52 JD |
273 | */ |
274 | if (relayd_id != (uint64_t) -1ULL) { | |
275 | if (read_len != len) { | |
276 | ERR("Error sending to the relay (ret: %zd != len: %lu)", | |
277 | read_len, len); | |
278 | } | |
279 | } else { | |
280 | if (read_len != padded_len) { | |
281 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", | |
282 | read_len, padded_len); | |
283 | } | |
284 | } | |
285 | ||
286 | ret = kernctl_put_subbuf(stream->wait_fd); | |
287 | if (ret < 0) { | |
288 | ERR("Snapshot kernctl_put_subbuf"); | |
56591bac | 289 | ret = -errno; |
07b86b52 JD |
290 | goto end_unlock; |
291 | } | |
292 | consumed_pos += stream->max_sb_size; | |
293 | } | |
294 | ||
295 | if (relayd_id == (uint64_t) -1ULL) { | |
fdf9986c MD |
296 | if (stream->out_fd >= 0) { |
297 | ret = close(stream->out_fd); | |
298 | if (ret < 0) { | |
299 | PERROR("Kernel consumer snapshot close out_fd"); | |
300 | goto end_unlock; | |
301 | } | |
302 | stream->out_fd = -1; | |
07b86b52 | 303 | } |
07b86b52 JD |
304 | } else { |
305 | close_relayd_stream(stream); | |
306 | stream->net_seq_idx = (uint64_t) -1ULL; | |
307 | } | |
308 | pthread_mutex_unlock(&stream->lock); | |
309 | } | |
310 | ||
311 | /* All good! */ | |
312 | ret = 0; | |
313 | goto end; | |
314 | ||
29decac3 DG |
315 | error_put_subbuf: |
316 | ret = kernctl_put_subbuf(stream->wait_fd); | |
317 | if (ret < 0) { | |
56591bac | 318 | ret = -errno; |
29decac3 DG |
319 | ERR("Snapshot kernctl_put_subbuf error path"); |
320 | } | |
07b86b52 JD |
321 | end_unlock: |
322 | pthread_mutex_unlock(&stream->lock); | |
323 | end: | |
324 | rcu_read_unlock(); | |
325 | return ret; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Read the whole metadata available for a snapshot. | |
330 | * | |
331 | * Returns 0 on success, < 0 on error | |
332 | */ | |
333 | int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path, | |
e2039c7a | 334 | uint64_t relayd_id, struct lttng_consumer_local_data *ctx) |
07b86b52 | 335 | { |
d771f832 DG |
336 | int ret, use_relayd = 0; |
337 | ssize_t ret_read; | |
07b86b52 JD |
338 | struct lttng_consumer_channel *metadata_channel; |
339 | struct lttng_consumer_stream *metadata_stream; | |
d771f832 DG |
340 | |
341 | assert(ctx); | |
07b86b52 JD |
342 | |
343 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", | |
344 | key, path); | |
345 | ||
346 | rcu_read_lock(); | |
347 | ||
348 | metadata_channel = consumer_find_channel(key); | |
349 | if (!metadata_channel) { | |
d771f832 | 350 | ERR("Kernel snapshot metadata not found for key %" PRIu64, key); |
07b86b52 | 351 | ret = -1; |
d771f832 | 352 | goto error; |
07b86b52 JD |
353 | } |
354 | ||
355 | metadata_stream = metadata_channel->metadata_stream; | |
356 | assert(metadata_stream); | |
357 | ||
d771f832 | 358 | /* Flag once that we have a valid relayd for the stream. */ |
e2039c7a | 359 | if (relayd_id != (uint64_t) -1ULL) { |
d771f832 DG |
360 | use_relayd = 1; |
361 | } | |
362 | ||
363 | if (use_relayd) { | |
10a50311 | 364 | ret = consumer_send_relayd_stream(metadata_stream, path); |
e2039c7a | 365 | if (ret < 0) { |
d771f832 | 366 | goto error; |
e2039c7a | 367 | } |
e2039c7a JD |
368 | } else { |
369 | ret = utils_create_stream_file(path, metadata_stream->name, | |
370 | metadata_stream->chan->tracefile_size, | |
371 | metadata_stream->tracefile_count_current, | |
309167d2 | 372 | metadata_stream->uid, metadata_stream->gid, NULL); |
e2039c7a | 373 | if (ret < 0) { |
d771f832 | 374 | goto error; |
e2039c7a JD |
375 | } |
376 | metadata_stream->out_fd = ret; | |
07b86b52 | 377 | } |
07b86b52 | 378 | |
d771f832 | 379 | do { |
9ce5646a MD |
380 | health_code_update(); |
381 | ||
d771f832 DG |
382 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); |
383 | if (ret_read < 0) { | |
56591bac | 384 | if (ret_read != -EAGAIN) { |
6a00837f | 385 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
d771f832 DG |
386 | ret_read); |
387 | goto error; | |
07b86b52 | 388 | } |
d771f832 | 389 | /* ret_read is negative at this point so we will exit the loop. */ |
07b86b52 JD |
390 | continue; |
391 | } | |
d771f832 | 392 | } while (ret_read >= 0); |
07b86b52 | 393 | |
d771f832 DG |
394 | if (use_relayd) { |
395 | close_relayd_stream(metadata_stream); | |
396 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; | |
397 | } else { | |
fdf9986c MD |
398 | if (metadata_stream->out_fd >= 0) { |
399 | ret = close(metadata_stream->out_fd); | |
400 | if (ret < 0) { | |
401 | PERROR("Kernel consumer snapshot metadata close out_fd"); | |
402 | /* | |
403 | * Don't go on error here since the snapshot was successful at this | |
404 | * point but somehow the close failed. | |
405 | */ | |
406 | } | |
407 | metadata_stream->out_fd = -1; | |
e2039c7a | 408 | } |
e2039c7a JD |
409 | } |
410 | ||
07b86b52 | 411 | ret = 0; |
d771f832 | 412 | |
cf53a8a6 JD |
413 | cds_list_del(&metadata_stream->send_node); |
414 | consumer_stream_destroy(metadata_stream, NULL); | |
415 | metadata_channel->metadata_stream = NULL; | |
d771f832 | 416 | error: |
07b86b52 JD |
417 | rcu_read_unlock(); |
418 | return ret; | |
419 | } | |
420 | ||
1803a064 MD |
421 | /* |
422 | * Receive command from session daemon and process it. | |
423 | * | |
424 | * Return 1 on success else a negative value or 0. | |
425 | */ | |
3bd1e081 MD |
426 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
427 | int sock, struct pollfd *consumer_sockpoll) | |
428 | { | |
429 | ssize_t ret; | |
0c759fc9 | 430 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
3bd1e081 MD |
431 | struct lttcomm_consumer_msg msg; |
432 | ||
9ce5646a MD |
433 | health_code_update(); |
434 | ||
3bd1e081 MD |
435 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
436 | if (ret != sizeof(msg)) { | |
1803a064 | 437 | if (ret > 0) { |
c6857fcf | 438 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
1803a064 MD |
439 | ret = -1; |
440 | } | |
3bd1e081 MD |
441 | return ret; |
442 | } | |
9ce5646a MD |
443 | |
444 | health_code_update(); | |
445 | ||
84382d49 MD |
446 | /* Deprecated command */ |
447 | assert(msg.cmd_type != LTTNG_CONSUMER_STOP); | |
3bd1e081 | 448 | |
9ce5646a MD |
449 | health_code_update(); |
450 | ||
b0b335c8 MD |
451 | /* relayd needs RCU read-side protection */ |
452 | rcu_read_lock(); | |
453 | ||
3bd1e081 | 454 | switch (msg.cmd_type) { |
00e2e675 DG |
455 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
456 | { | |
f50f23d9 | 457 | /* Session daemon status message are handled in the following call. */ |
7735ef9e DG |
458 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
459 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
d3e2ba59 JD |
460 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id, |
461 | msg.u.relayd_sock.relayd_session_id); | |
00e2e675 DG |
462 | goto end_nosignal; |
463 | } | |
3bd1e081 MD |
464 | case LTTNG_CONSUMER_ADD_CHANNEL: |
465 | { | |
466 | struct lttng_consumer_channel *new_channel; | |
e43c41c5 | 467 | int ret_recv; |
3bd1e081 | 468 | |
9ce5646a MD |
469 | health_code_update(); |
470 | ||
f50f23d9 DG |
471 | /* First send a status message before receiving the fds. */ |
472 | ret = consumer_send_status_msg(sock, ret_code); | |
473 | if (ret < 0) { | |
474 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 475 | goto error_fatal; |
f50f23d9 | 476 | } |
9ce5646a MD |
477 | |
478 | health_code_update(); | |
479 | ||
d88aee68 | 480 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 481 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
ffe60014 DG |
482 | msg.u.channel.session_id, msg.u.channel.pathname, |
483 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, | |
1624d5b7 JD |
484 | msg.u.channel.relayd_id, msg.u.channel.output, |
485 | msg.u.channel.tracefile_size, | |
1950109e | 486 | msg.u.channel.tracefile_count, 0, |
ecc48a90 JD |
487 | msg.u.channel.monitor, |
488 | msg.u.channel.live_timer_interval); | |
3bd1e081 | 489 | if (new_channel == NULL) { |
f73fabfd | 490 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
491 | goto end_nosignal; |
492 | } | |
ffe60014 | 493 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
95a1109b JD |
494 | switch (msg.u.channel.output) { |
495 | case LTTNG_EVENT_SPLICE: | |
496 | new_channel->output = CONSUMER_CHANNEL_SPLICE; | |
497 | break; | |
498 | case LTTNG_EVENT_MMAP: | |
499 | new_channel->output = CONSUMER_CHANNEL_MMAP; | |
500 | break; | |
501 | default: | |
502 | ERR("Channel output unknown %d", msg.u.channel.output); | |
503 | goto end_nosignal; | |
504 | } | |
ffe60014 DG |
505 | |
506 | /* Translate and save channel type. */ | |
507 | switch (msg.u.channel.type) { | |
508 | case CONSUMER_CHANNEL_TYPE_DATA: | |
509 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
510 | new_channel->type = msg.u.channel.type; | |
511 | break; | |
512 | default: | |
513 | assert(0); | |
514 | goto end_nosignal; | |
515 | }; | |
516 | ||
9ce5646a MD |
517 | health_code_update(); |
518 | ||
3bd1e081 | 519 | if (ctx->on_recv_channel != NULL) { |
e43c41c5 JD |
520 | ret_recv = ctx->on_recv_channel(new_channel); |
521 | if (ret_recv == 0) { | |
522 | ret = consumer_add_channel(new_channel, ctx); | |
523 | } else if (ret_recv < 0) { | |
3bd1e081 MD |
524 | goto end_nosignal; |
525 | } | |
526 | } else { | |
e43c41c5 | 527 | ret = consumer_add_channel(new_channel, ctx); |
3bd1e081 | 528 | } |
94d49140 JD |
529 | if (CONSUMER_CHANNEL_TYPE_DATA) { |
530 | consumer_timer_live_start(new_channel, | |
531 | msg.u.channel.live_timer_interval); | |
532 | } | |
e43c41c5 | 533 | |
9ce5646a MD |
534 | health_code_update(); |
535 | ||
e43c41c5 | 536 | /* If we received an error in add_channel, we need to report it. */ |
821fffb2 | 537 | if (ret < 0) { |
1803a064 MD |
538 | ret = consumer_send_status_msg(sock, ret); |
539 | if (ret < 0) { | |
540 | goto error_fatal; | |
541 | } | |
e43c41c5 JD |
542 | goto end_nosignal; |
543 | } | |
544 | ||
3bd1e081 MD |
545 | goto end_nosignal; |
546 | } | |
547 | case LTTNG_CONSUMER_ADD_STREAM: | |
548 | { | |
dae10966 DG |
549 | int fd; |
550 | struct lttng_pipe *stream_pipe; | |
00e2e675 | 551 | struct lttng_consumer_stream *new_stream; |
ffe60014 | 552 | struct lttng_consumer_channel *channel; |
c80048c6 | 553 | int alloc_ret = 0; |
3bd1e081 | 554 | |
ffe60014 DG |
555 | /* |
556 | * Get stream's channel reference. Needed when adding the stream to the | |
557 | * global hash table. | |
558 | */ | |
559 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
560 | if (!channel) { | |
561 | /* | |
562 | * We could not find the channel. Can happen if cpu hotplug | |
563 | * happens while tearing down. | |
564 | */ | |
d88aee68 | 565 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
e462382a | 566 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
ffe60014 DG |
567 | } |
568 | ||
9ce5646a MD |
569 | health_code_update(); |
570 | ||
f50f23d9 DG |
571 | /* First send a status message before receiving the fds. */ |
572 | ret = consumer_send_status_msg(sock, ret_code); | |
1803a064 | 573 | if (ret < 0) { |
d771f832 | 574 | /* Somehow, the session daemon is not responding anymore. */ |
1803a064 MD |
575 | goto error_fatal; |
576 | } | |
9ce5646a MD |
577 | |
578 | health_code_update(); | |
579 | ||
0c759fc9 | 580 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
d771f832 | 581 | /* Channel was not found. */ |
f50f23d9 DG |
582 | goto end_nosignal; |
583 | } | |
584 | ||
d771f832 | 585 | /* Blocking call */ |
9ce5646a MD |
586 | health_poll_entry(); |
587 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
588 | health_poll_exit(); | |
84382d49 MD |
589 | if (ret) { |
590 | goto error_fatal; | |
3bd1e081 | 591 | } |
00e2e675 | 592 | |
9ce5646a MD |
593 | health_code_update(); |
594 | ||
00e2e675 | 595 | /* Get stream file descriptor from socket */ |
f2fc6720 MD |
596 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
597 | if (ret != sizeof(fd)) { | |
f73fabfd | 598 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 599 | rcu_read_unlock(); |
3bd1e081 MD |
600 | return ret; |
601 | } | |
3bd1e081 | 602 | |
9ce5646a MD |
603 | health_code_update(); |
604 | ||
f50f23d9 DG |
605 | /* |
606 | * Send status code to session daemon only if the recv works. If the | |
607 | * above recv() failed, the session daemon is notified through the | |
608 | * error socket and the teardown is eventually done. | |
609 | */ | |
610 | ret = consumer_send_status_msg(sock, ret_code); | |
611 | if (ret < 0) { | |
612 | /* Somehow, the session daemon is not responding anymore. */ | |
613 | goto end_nosignal; | |
614 | } | |
615 | ||
9ce5646a MD |
616 | health_code_update(); |
617 | ||
ffe60014 DG |
618 | new_stream = consumer_allocate_stream(channel->key, |
619 | fd, | |
620 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
621 | channel->name, | |
622 | channel->uid, | |
623 | channel->gid, | |
624 | channel->relayd_id, | |
625 | channel->session_id, | |
626 | msg.u.stream.cpu, | |
627 | &alloc_ret, | |
4891ece8 DG |
628 | channel->type, |
629 | channel->monitor); | |
3bd1e081 | 630 | if (new_stream == NULL) { |
c80048c6 MD |
631 | switch (alloc_ret) { |
632 | case -ENOMEM: | |
633 | case -EINVAL: | |
634 | default: | |
635 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
636 | break; | |
c80048c6 | 637 | } |
3f8e211f | 638 | goto end_nosignal; |
3bd1e081 | 639 | } |
d771f832 | 640 | |
ffe60014 DG |
641 | new_stream->chan = channel; |
642 | new_stream->wait_fd = fd; | |
07b86b52 JD |
643 | switch (channel->output) { |
644 | case CONSUMER_CHANNEL_SPLICE: | |
645 | new_stream->output = LTTNG_EVENT_SPLICE; | |
a2361a61 JD |
646 | ret = utils_create_pipe(new_stream->splice_pipe); |
647 | if (ret < 0) { | |
648 | goto end_nosignal; | |
649 | } | |
07b86b52 JD |
650 | break; |
651 | case CONSUMER_CHANNEL_MMAP: | |
652 | new_stream->output = LTTNG_EVENT_MMAP; | |
653 | break; | |
654 | default: | |
655 | ERR("Stream output unknown %d", channel->output); | |
656 | goto end_nosignal; | |
657 | } | |
00e2e675 | 658 | |
a0c83db9 DG |
659 | /* |
660 | * We've just assigned the channel to the stream so increment the | |
07b86b52 JD |
661 | * refcount right now. We don't need to increment the refcount for |
662 | * streams in no monitor because we handle manually the cleanup of | |
663 | * those. It is very important to make sure there is NO prior | |
664 | * consumer_del_stream() calls or else the refcount will be unbalanced. | |
a0c83db9 | 665 | */ |
07b86b52 JD |
666 | if (channel->monitor) { |
667 | uatomic_inc(&new_stream->chan->refcount); | |
668 | } | |
9d9353f9 | 669 | |
fb3a43a9 DG |
670 | /* |
671 | * The buffer flush is done on the session daemon side for the kernel | |
672 | * so no need for the stream "hangup_flush_done" variable to be | |
673 | * tracked. This is important for a kernel stream since we don't rely | |
674 | * on the flush state of the stream to read data. It's not the case for | |
675 | * user space tracing. | |
676 | */ | |
677 | new_stream->hangup_flush_done = 0; | |
678 | ||
9ce5646a MD |
679 | health_code_update(); |
680 | ||
633d0084 DG |
681 | if (ctx->on_recv_stream) { |
682 | ret = ctx->on_recv_stream(new_stream); | |
683 | if (ret < 0) { | |
d771f832 | 684 | consumer_stream_free(new_stream); |
633d0084 | 685 | goto end_nosignal; |
fb3a43a9 | 686 | } |
633d0084 | 687 | } |
fb3a43a9 | 688 | |
9ce5646a MD |
689 | health_code_update(); |
690 | ||
07b86b52 JD |
691 | if (new_stream->metadata_flag) { |
692 | channel->metadata_stream = new_stream; | |
693 | } | |
694 | ||
2bba9e53 DG |
695 | /* Do not monitor this stream. */ |
696 | if (!channel->monitor) { | |
5eecee74 | 697 | DBG("Kernel consumer add stream %s in no monitor mode with " |
6dc3064a | 698 | "relayd id %" PRIu64, new_stream->name, |
5eecee74 | 699 | new_stream->net_seq_idx); |
10a50311 | 700 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
6dc3064a DG |
701 | break; |
702 | } | |
703 | ||
e1b71bdc DG |
704 | /* Send stream to relayd if the stream has an ID. */ |
705 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { | |
194ee077 DG |
706 | ret = consumer_send_relayd_stream(new_stream, |
707 | new_stream->chan->pathname); | |
e1b71bdc DG |
708 | if (ret < 0) { |
709 | consumer_stream_free(new_stream); | |
710 | goto end_nosignal; | |
711 | } | |
e2039c7a JD |
712 | } |
713 | ||
50f8ae69 | 714 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 715 | if (new_stream->metadata_flag) { |
5ab66908 MD |
716 | ret = consumer_add_metadata_stream(new_stream); |
717 | if (ret) { | |
718 | ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing", | |
719 | new_stream->key); | |
720 | consumer_stream_free(new_stream); | |
721 | goto end_nosignal; | |
722 | } | |
dae10966 | 723 | stream_pipe = ctx->consumer_metadata_pipe; |
3bd1e081 | 724 | } else { |
5ab66908 MD |
725 | ret = consumer_add_data_stream(new_stream); |
726 | if (ret) { | |
727 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", | |
728 | new_stream->key); | |
729 | consumer_stream_free(new_stream); | |
730 | goto end_nosignal; | |
731 | } | |
dae10966 | 732 | stream_pipe = ctx->consumer_data_pipe; |
50f8ae69 DG |
733 | } |
734 | ||
5ab66908 MD |
735 | /* Vitible to other threads */ |
736 | new_stream->globally_visible = 1; | |
737 | ||
9ce5646a MD |
738 | health_code_update(); |
739 | ||
dae10966 | 740 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
50f8ae69 | 741 | if (ret < 0) { |
dae10966 | 742 | ERR("Consumer write %s stream to pipe %d", |
50f8ae69 | 743 | new_stream->metadata_flag ? "metadata" : "data", |
dae10966 | 744 | lttng_pipe_get_writefd(stream_pipe)); |
5ab66908 MD |
745 | if (new_stream->metadata_flag) { |
746 | consumer_del_stream_for_metadata(new_stream); | |
747 | } else { | |
748 | consumer_del_stream_for_data(new_stream); | |
749 | } | |
50f8ae69 | 750 | goto end_nosignal; |
3bd1e081 | 751 | } |
00e2e675 | 752 | |
50f8ae69 | 753 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
ffe60014 | 754 | new_stream->name, fd, new_stream->relayd_stream_id); |
3bd1e081 MD |
755 | break; |
756 | } | |
a4baae1b JD |
757 | case LTTNG_CONSUMER_STREAMS_SENT: |
758 | { | |
759 | struct lttng_consumer_channel *channel; | |
760 | ||
761 | /* | |
762 | * Get stream's channel reference. Needed when adding the stream to the | |
763 | * global hash table. | |
764 | */ | |
765 | channel = consumer_find_channel(msg.u.sent_streams.channel_key); | |
766 | if (!channel) { | |
767 | /* | |
768 | * We could not find the channel. Can happen if cpu hotplug | |
769 | * happens while tearing down. | |
770 | */ | |
771 | ERR("Unable to find channel key %" PRIu64, | |
772 | msg.u.sent_streams.channel_key); | |
e462382a | 773 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
a4baae1b JD |
774 | } |
775 | ||
776 | health_code_update(); | |
777 | ||
778 | /* | |
779 | * Send status code to session daemon. | |
780 | */ | |
781 | ret = consumer_send_status_msg(sock, ret_code); | |
f261ad0a | 782 | if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
a4baae1b JD |
783 | /* Somehow, the session daemon is not responding anymore. */ |
784 | goto end_nosignal; | |
785 | } | |
786 | ||
787 | health_code_update(); | |
788 | ||
789 | /* | |
790 | * We should not send this message if we don't monitor the | |
791 | * streams in this channel. | |
792 | */ | |
793 | if (!channel->monitor) { | |
794 | break; | |
795 | } | |
796 | ||
797 | health_code_update(); | |
798 | /* Send stream to relayd if the stream has an ID. */ | |
799 | if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) { | |
800 | ret = consumer_send_relayd_streams_sent( | |
801 | msg.u.sent_streams.net_seq_idx); | |
802 | if (ret < 0) { | |
803 | goto end_nosignal; | |
804 | } | |
805 | } | |
806 | break; | |
807 | } | |
3bd1e081 MD |
808 | case LTTNG_CONSUMER_UPDATE_STREAM: |
809 | { | |
3f8e211f DG |
810 | rcu_read_unlock(); |
811 | return -ENOSYS; | |
812 | } | |
813 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
814 | { | |
a6ba4fe1 | 815 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
816 | struct consumer_relayd_sock_pair *relayd; |
817 | ||
a6ba4fe1 | 818 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
819 | |
820 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 821 | relayd = consumer_find_relayd(index); |
3f8e211f | 822 | if (relayd == NULL) { |
3448e266 | 823 | DBG("Unable to find relayd %" PRIu64, index); |
e462382a | 824 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
3bd1e081 | 825 | } |
3f8e211f | 826 | |
a6ba4fe1 DG |
827 | /* |
828 | * Each relayd socket pair has a refcount of stream attached to it | |
829 | * which tells if the relayd is still active or not depending on the | |
830 | * refcount value. | |
831 | * | |
832 | * This will set the destroy flag of the relayd object and destroy it | |
833 | * if the refcount reaches zero when called. | |
834 | * | |
835 | * The destroy can happen either here or when a stream fd hangs up. | |
836 | */ | |
f50f23d9 DG |
837 | if (relayd) { |
838 | consumer_flag_relayd_for_destroy(relayd); | |
839 | } | |
840 | ||
9ce5646a MD |
841 | health_code_update(); |
842 | ||
f50f23d9 DG |
843 | ret = consumer_send_status_msg(sock, ret_code); |
844 | if (ret < 0) { | |
845 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 846 | goto error_fatal; |
f50f23d9 | 847 | } |
3f8e211f | 848 | |
3f8e211f | 849 | goto end_nosignal; |
3bd1e081 | 850 | } |
6d805429 | 851 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 852 | { |
c8f59ee5 | 853 | int32_t ret; |
6d805429 | 854 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 855 | |
6d805429 | 856 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 857 | |
6d805429 | 858 | ret = consumer_data_pending(id); |
c8f59ee5 | 859 | |
9ce5646a MD |
860 | health_code_update(); |
861 | ||
c8f59ee5 DG |
862 | /* Send back returned value to session daemon */ |
863 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
864 | if (ret < 0) { | |
6d805429 | 865 | PERROR("send data pending ret code"); |
1803a064 | 866 | goto error_fatal; |
c8f59ee5 | 867 | } |
f50f23d9 DG |
868 | |
869 | /* | |
870 | * No need to send back a status message since the data pending | |
871 | * returned value is the response. | |
872 | */ | |
c8f59ee5 | 873 | break; |
53632229 | 874 | } |
6dc3064a DG |
875 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
876 | { | |
07b86b52 JD |
877 | if (msg.u.snapshot_channel.metadata == 1) { |
878 | ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key, | |
e2039c7a JD |
879 | msg.u.snapshot_channel.pathname, |
880 | msg.u.snapshot_channel.relayd_id, ctx); | |
07b86b52 JD |
881 | if (ret < 0) { |
882 | ERR("Snapshot metadata failed"); | |
e462382a | 883 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; |
07b86b52 JD |
884 | } |
885 | } else { | |
886 | ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key, | |
887 | msg.u.snapshot_channel.pathname, | |
5c786ded JD |
888 | msg.u.snapshot_channel.relayd_id, |
889 | msg.u.snapshot_channel.max_stream_size, | |
890 | ctx); | |
07b86b52 JD |
891 | if (ret < 0) { |
892 | ERR("Snapshot channel failed"); | |
e462382a | 893 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
07b86b52 JD |
894 | } |
895 | } | |
896 | ||
9ce5646a MD |
897 | health_code_update(); |
898 | ||
6dc3064a DG |
899 | ret = consumer_send_status_msg(sock, ret_code); |
900 | if (ret < 0) { | |
901 | /* Somehow, the session daemon is not responding anymore. */ | |
902 | goto end_nosignal; | |
903 | } | |
904 | break; | |
905 | } | |
07b86b52 JD |
906 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
907 | { | |
908 | uint64_t key = msg.u.destroy_channel.key; | |
909 | struct lttng_consumer_channel *channel; | |
910 | ||
911 | channel = consumer_find_channel(key); | |
912 | if (!channel) { | |
913 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); | |
e462382a | 914 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
07b86b52 JD |
915 | } |
916 | ||
9ce5646a MD |
917 | health_code_update(); |
918 | ||
07b86b52 JD |
919 | ret = consumer_send_status_msg(sock, ret_code); |
920 | if (ret < 0) { | |
921 | /* Somehow, the session daemon is not responding anymore. */ | |
922 | goto end_nosignal; | |
923 | } | |
924 | ||
9ce5646a MD |
925 | health_code_update(); |
926 | ||
15dc512a DG |
927 | /* Stop right now if no channel was found. */ |
928 | if (!channel) { | |
929 | goto end_nosignal; | |
930 | } | |
931 | ||
07b86b52 JD |
932 | /* |
933 | * This command should ONLY be issued for channel with streams set in | |
934 | * no monitor mode. | |
935 | */ | |
936 | assert(!channel->monitor); | |
937 | ||
938 | /* | |
939 | * The refcount should ALWAYS be 0 in the case of a channel in no | |
940 | * monitor mode. | |
941 | */ | |
942 | assert(!uatomic_sub_return(&channel->refcount, 1)); | |
943 | ||
944 | consumer_del_channel(channel); | |
945 | ||
946 | goto end_nosignal; | |
947 | } | |
3bd1e081 | 948 | default: |
3f8e211f | 949 | goto end_nosignal; |
3bd1e081 | 950 | } |
3f8e211f | 951 | |
3bd1e081 | 952 | end_nosignal: |
b0b335c8 | 953 | rcu_read_unlock(); |
4cbc1a04 DG |
954 | |
955 | /* | |
956 | * Return 1 to indicate success since the 0 value can be a socket | |
957 | * shutdown during the recv() or send() call. | |
958 | */ | |
9ce5646a | 959 | health_code_update(); |
4cbc1a04 | 960 | return 1; |
1803a064 MD |
961 | |
962 | error_fatal: | |
963 | rcu_read_unlock(); | |
964 | /* This will issue a consumer stop. */ | |
965 | return -1; | |
3bd1e081 | 966 | } |
d41f73b7 | 967 | |
309167d2 JD |
968 | /* |
969 | * Populate index values of a kernel stream. Values are set in big endian order. | |
970 | * | |
971 | * Return 0 on success or else a negative value. | |
972 | */ | |
50adc264 | 973 | static int get_index_values(struct ctf_packet_index *index, int infd) |
309167d2 JD |
974 | { |
975 | int ret; | |
976 | ||
977 | ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin); | |
978 | if (ret < 0) { | |
979 | PERROR("kernctl_get_timestamp_begin"); | |
980 | goto error; | |
981 | } | |
982 | index->timestamp_begin = htobe64(index->timestamp_begin); | |
983 | ||
984 | ret = kernctl_get_timestamp_end(infd, &index->timestamp_end); | |
985 | if (ret < 0) { | |
986 | PERROR("kernctl_get_timestamp_end"); | |
987 | goto error; | |
988 | } | |
989 | index->timestamp_end = htobe64(index->timestamp_end); | |
990 | ||
991 | ret = kernctl_get_events_discarded(infd, &index->events_discarded); | |
992 | if (ret < 0) { | |
993 | PERROR("kernctl_get_events_discarded"); | |
994 | goto error; | |
995 | } | |
996 | index->events_discarded = htobe64(index->events_discarded); | |
997 | ||
998 | ret = kernctl_get_content_size(infd, &index->content_size); | |
999 | if (ret < 0) { | |
1000 | PERROR("kernctl_get_content_size"); | |
1001 | goto error; | |
1002 | } | |
1003 | index->content_size = htobe64(index->content_size); | |
1004 | ||
1005 | ret = kernctl_get_packet_size(infd, &index->packet_size); | |
1006 | if (ret < 0) { | |
1007 | PERROR("kernctl_get_packet_size"); | |
1008 | goto error; | |
1009 | } | |
1010 | index->packet_size = htobe64(index->packet_size); | |
1011 | ||
1012 | ret = kernctl_get_stream_id(infd, &index->stream_id); | |
1013 | if (ret < 0) { | |
1014 | PERROR("kernctl_get_stream_id"); | |
1015 | goto error; | |
1016 | } | |
1017 | index->stream_id = htobe64(index->stream_id); | |
1018 | ||
1019 | error: | |
1020 | return ret; | |
1021 | } | |
94d49140 JD |
1022 | /* |
1023 | * Sync metadata meaning request them to the session daemon and snapshot to the | |
1024 | * metadata thread can consumer them. | |
1025 | * | |
1026 | * Metadata stream lock MUST be acquired. | |
1027 | * | |
1028 | * Return 0 if new metadatda is available, EAGAIN if the metadata stream | |
1029 | * is empty or a negative value on error. | |
1030 | */ | |
1031 | int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata) | |
1032 | { | |
1033 | int ret; | |
1034 | ||
1035 | assert(metadata); | |
1036 | ||
1037 | ret = kernctl_buffer_flush(metadata->wait_fd); | |
1038 | if (ret < 0) { | |
1039 | ERR("Failed to flush kernel stream"); | |
1040 | goto end; | |
1041 | } | |
1042 | ||
1043 | ret = kernctl_snapshot(metadata->wait_fd); | |
1044 | if (ret < 0) { | |
1045 | if (errno != EAGAIN) { | |
1046 | ERR("Sync metadata, taking kernel snapshot failed."); | |
1047 | goto end; | |
1048 | } | |
1049 | DBG("Sync metadata, no new kernel metadata"); | |
1050 | /* No new metadata, exit. */ | |
1051 | ret = ENODATA; | |
1052 | goto end; | |
1053 | } | |
1054 | ||
1055 | end: | |
1056 | return ret; | |
1057 | } | |
309167d2 | 1058 | |
d41f73b7 MD |
1059 | /* |
1060 | * Consume data on a file descriptor and write it on a trace file. | |
1061 | */ | |
4078b776 | 1062 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
1063 | struct lttng_consumer_local_data *ctx) |
1064 | { | |
1d4dfdef | 1065 | unsigned long len, subbuf_size, padding; |
1c20f0e2 | 1066 | int err, write_index = 1; |
4078b776 | 1067 | ssize_t ret = 0; |
d41f73b7 | 1068 | int infd = stream->wait_fd; |
50adc264 | 1069 | struct ctf_packet_index index; |
d41f73b7 MD |
1070 | |
1071 | DBG("In read_subbuffer (infd : %d)", infd); | |
309167d2 | 1072 | |
d41f73b7 MD |
1073 | /* Get the next subbuffer */ |
1074 | err = kernctl_get_next_subbuf(infd); | |
1075 | if (err != 0) { | |
d41f73b7 MD |
1076 | /* |
1077 | * This is a debug message even for single-threaded consumer, | |
1078 | * because poll() have more relaxed criterions than get subbuf, | |
1079 | * so get_subbuf may fail for short race windows where poll() | |
1080 | * would issue wakeups. | |
1081 | */ | |
1082 | DBG("Reserving sub buffer failed (everything is normal, " | |
1083 | "it is due to concurrency)"); | |
56591bac | 1084 | ret = -errno; |
d41f73b7 MD |
1085 | goto end; |
1086 | } | |
1087 | ||
1d4dfdef DG |
1088 | /* Get the full subbuffer size including padding */ |
1089 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
1090 | if (err != 0) { | |
5a510c9f | 1091 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1092 | err = kernctl_put_subbuf(infd); |
1093 | if (err != 0) { | |
1094 | if (errno == EFAULT) { | |
5a510c9f | 1095 | PERROR("Error in unreserving sub buffer\n"); |
8265f19e MD |
1096 | } else if (errno == EIO) { |
1097 | /* Should never happen with newer LTTng versions */ | |
5a510c9f | 1098 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e MD |
1099 | } |
1100 | ret = -errno; | |
1101 | goto end; | |
1102 | } | |
56591bac | 1103 | ret = -errno; |
1d4dfdef DG |
1104 | goto end; |
1105 | } | |
1106 | ||
1c20f0e2 | 1107 | if (!stream->metadata_flag) { |
309167d2 JD |
1108 | ret = get_index_values(&index, infd); |
1109 | if (ret < 0) { | |
8265f19e MD |
1110 | err = kernctl_put_subbuf(infd); |
1111 | if (err != 0) { | |
1112 | if (errno == EFAULT) { | |
5a510c9f | 1113 | PERROR("Error in unreserving sub buffer\n"); |
8265f19e MD |
1114 | } else if (errno == EIO) { |
1115 | /* Should never happen with newer LTTng versions */ | |
5a510c9f | 1116 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e MD |
1117 | } |
1118 | ret = -errno; | |
1119 | goto end; | |
1120 | } | |
309167d2 JD |
1121 | goto end; |
1122 | } | |
1c20f0e2 JD |
1123 | } else { |
1124 | write_index = 0; | |
309167d2 JD |
1125 | } |
1126 | ||
ffe60014 | 1127 | switch (stream->chan->output) { |
07b86b52 | 1128 | case CONSUMER_CHANNEL_SPLICE: |
1d4dfdef DG |
1129 | /* |
1130 | * XXX: The lttng-modules splice "actor" does not handle copying | |
1131 | * partial pages hence only using the subbuffer size without the | |
1132 | * padding makes the splice fail. | |
1133 | */ | |
1134 | subbuf_size = len; | |
1135 | padding = 0; | |
1136 | ||
1137 | /* splice the subbuffer to the tracefile */ | |
91dfef6e | 1138 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
309167d2 | 1139 | padding, &index); |
91dfef6e DG |
1140 | /* |
1141 | * XXX: Splice does not support network streaming so the return value | |
1142 | * is simply checked against subbuf_size and not like the mmap() op. | |
1143 | */ | |
1d4dfdef DG |
1144 | if (ret != subbuf_size) { |
1145 | /* | |
1146 | * display the error but continue processing to try | |
1147 | * to release the subbuffer | |
1148 | */ | |
1149 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
1150 | ret, subbuf_size); | |
309167d2 | 1151 | write_index = 0; |
1d4dfdef DG |
1152 | } |
1153 | break; | |
07b86b52 | 1154 | case CONSUMER_CHANNEL_MMAP: |
1d4dfdef DG |
1155 | /* Get subbuffer size without padding */ |
1156 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
1157 | if (err != 0) { | |
5a510c9f | 1158 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1159 | err = kernctl_put_subbuf(infd); |
1160 | if (err != 0) { | |
1161 | if (errno == EFAULT) { | |
5a510c9f | 1162 | PERROR("Error in unreserving sub buffer\n"); |
8265f19e MD |
1163 | } else if (errno == EIO) { |
1164 | /* Should never happen with newer LTTng versions */ | |
5a510c9f | 1165 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e MD |
1166 | } |
1167 | ret = -errno; | |
1168 | goto end; | |
1169 | } | |
56591bac | 1170 | ret = -errno; |
1d4dfdef DG |
1171 | goto end; |
1172 | } | |
47e81c02 | 1173 | |
1d4dfdef DG |
1174 | /* Make sure the tracer is not gone mad on us! */ |
1175 | assert(len >= subbuf_size); | |
1176 | ||
1177 | padding = len - subbuf_size; | |
1178 | ||
1179 | /* write the subbuffer to the tracefile */ | |
91dfef6e | 1180 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
309167d2 | 1181 | padding, &index); |
91dfef6e DG |
1182 | /* |
1183 | * The mmap operation should write subbuf_size amount of data when | |
1184 | * network streaming or the full padding (len) size when we are _not_ | |
1185 | * streaming. | |
1186 | */ | |
d88aee68 DG |
1187 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1188 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 1189 | /* |
91dfef6e | 1190 | * Display the error but continue processing to try to release the |
2336629e DG |
1191 | * subbuffer. This is a DBG statement since this is possible to |
1192 | * happen without being a critical error. | |
1d4dfdef | 1193 | */ |
2336629e | 1194 | DBG("Error writing to tracefile " |
91dfef6e DG |
1195 | "(ret: %zd != len: %lu != subbuf_size: %lu)", |
1196 | ret, len, subbuf_size); | |
309167d2 | 1197 | write_index = 0; |
1d4dfdef DG |
1198 | } |
1199 | break; | |
1200 | default: | |
1201 | ERR("Unknown output method"); | |
56591bac | 1202 | ret = -EPERM; |
d41f73b7 MD |
1203 | } |
1204 | ||
1205 | err = kernctl_put_next_subbuf(infd); | |
1206 | if (err != 0) { | |
d41f73b7 | 1207 | if (errno == EFAULT) { |
5a510c9f | 1208 | PERROR("Error in unreserving sub buffer\n"); |
d41f73b7 MD |
1209 | } else if (errno == EIO) { |
1210 | /* Should never happen with newer LTTng versions */ | |
5a510c9f | 1211 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
d41f73b7 | 1212 | } |
56591bac | 1213 | ret = -errno; |
d41f73b7 MD |
1214 | goto end; |
1215 | } | |
1216 | ||
309167d2 | 1217 | /* Write index if needed. */ |
1c20f0e2 JD |
1218 | if (!write_index) { |
1219 | goto end; | |
1220 | } | |
1221 | ||
94d49140 JD |
1222 | if (stream->chan->live_timer_interval && !stream->metadata_flag) { |
1223 | /* | |
1224 | * In live, block until all the metadata is sent. | |
1225 | */ | |
1226 | err = consumer_stream_sync_metadata(ctx, stream->session_id); | |
1227 | if (err < 0) { | |
1228 | goto end; | |
1229 | } | |
1230 | } | |
1231 | ||
1c20f0e2 JD |
1232 | err = consumer_stream_write_index(stream, &index); |
1233 | if (err < 0) { | |
1234 | goto end; | |
309167d2 JD |
1235 | } |
1236 | ||
d41f73b7 MD |
1237 | end: |
1238 | return ret; | |
1239 | } | |
1240 | ||
1241 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
1242 | { | |
1243 | int ret; | |
ffe60014 DG |
1244 | |
1245 | assert(stream); | |
1246 | ||
2bba9e53 DG |
1247 | /* |
1248 | * Don't create anything if this is set for streaming or should not be | |
1249 | * monitored. | |
1250 | */ | |
1251 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { | |
fe4477ee JD |
1252 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
1253 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
309167d2 | 1254 | stream->uid, stream->gid, NULL); |
fe4477ee JD |
1255 | if (ret < 0) { |
1256 | goto error; | |
1257 | } | |
1258 | stream->out_fd = ret; | |
1259 | stream->tracefile_size_current = 0; | |
309167d2 JD |
1260 | |
1261 | if (!stream->metadata_flag) { | |
1262 | ret = index_create_file(stream->chan->pathname, | |
1263 | stream->name, stream->uid, stream->gid, | |
1264 | stream->chan->tracefile_size, | |
1265 | stream->tracefile_count_current); | |
1266 | if (ret < 0) { | |
1267 | goto error; | |
1268 | } | |
1269 | stream->index_fd = ret; | |
1270 | } | |
ffe60014 | 1271 | } |
d41f73b7 | 1272 | |
d41f73b7 MD |
1273 | if (stream->output == LTTNG_EVENT_MMAP) { |
1274 | /* get the len of the mmap region */ | |
1275 | unsigned long mmap_len; | |
1276 | ||
1277 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
1278 | if (ret != 0) { | |
ffe60014 | 1279 | PERROR("kernctl_get_mmap_len"); |
56591bac | 1280 | ret = -errno; |
d41f73b7 MD |
1281 | goto error_close_fd; |
1282 | } | |
1283 | stream->mmap_len = (size_t) mmap_len; | |
1284 | ||
ffe60014 DG |
1285 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
1286 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 1287 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 1288 | PERROR("Error mmaping"); |
d41f73b7 MD |
1289 | ret = -1; |
1290 | goto error_close_fd; | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | /* we return 0 to let the library handle the FD internally */ | |
1295 | return 0; | |
1296 | ||
1297 | error_close_fd: | |
2f225ce2 | 1298 | if (stream->out_fd >= 0) { |
d41f73b7 MD |
1299 | int err; |
1300 | ||
1301 | err = close(stream->out_fd); | |
1302 | assert(!err); | |
2f225ce2 | 1303 | stream->out_fd = -1; |
d41f73b7 MD |
1304 | } |
1305 | error: | |
1306 | return ret; | |
1307 | } | |
1308 | ||
ca22feea DG |
1309 | /* |
1310 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1311 | * stream. Consumer data lock MUST be acquired before calling this function |
1312 | * and the stream lock. | |
ca22feea | 1313 | * |
6d805429 | 1314 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1315 | * data is available for trace viewer reading. |
1316 | */ | |
6d805429 | 1317 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1318 | { |
1319 | int ret; | |
1320 | ||
1321 | assert(stream); | |
1322 | ||
873b9e9a MD |
1323 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1324 | ret = 0; | |
1325 | goto end; | |
1326 | } | |
1327 | ||
ca22feea DG |
1328 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
1329 | if (ret == 0) { | |
1330 | /* There is still data so let's put back this subbuffer. */ | |
1331 | ret = kernctl_put_subbuf(stream->wait_fd); | |
1332 | assert(ret == 0); | |
6d805429 | 1333 | ret = 1; /* Data is pending */ |
4e9a4686 | 1334 | goto end; |
ca22feea DG |
1335 | } |
1336 | ||
6d805429 DG |
1337 | /* Data is NOT pending and ready to be read. */ |
1338 | ret = 0; | |
ca22feea | 1339 | |
6efae65e DG |
1340 | end: |
1341 | return ret; | |
ca22feea | 1342 | } |