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