Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
b3530820 | 4 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3bd1e081 | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
18 | */ |
19 | ||
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> |
c8fea79c | 43 | #include <common/consumer/consumer-stream.h> |
309167d2 | 44 | #include <common/index/index.h> |
c8fea79c | 45 | #include <common/consumer/consumer-timer.h> |
d2956687 | 46 | #include <common/optional.h> |
0857097f | 47 | |
10a8a223 | 48 | #include "kernel-consumer.h" |
3bd1e081 MD |
49 | |
50 | extern struct lttng_consumer_global_data consumer_data; | |
51 | extern int consumer_poll_timeout; | |
3bd1e081 | 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); | |
d2d2f190 JD |
64 | /* |
65 | * -EAGAIN is not an error, it just means that there is no data to | |
66 | * be read. | |
67 | */ | |
68 | if (ret != 0 && ret != -EAGAIN) { | |
5a510c9f | 69 | PERROR("Getting sub-buffer snapshot."); |
3bd1e081 MD |
70 | } |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
e9404c27 JG |
75 | /* |
76 | * Sample consumed and produced positions for a specific fd. | |
77 | * | |
78 | * Returns 0 on success, < 0 on error. | |
79 | */ | |
80 | int lttng_kconsumer_sample_snapshot_positions( | |
81 | struct lttng_consumer_stream *stream) | |
82 | { | |
83 | assert(stream); | |
84 | ||
85 | return kernctl_snapshot_sample_positions(stream->wait_fd); | |
86 | } | |
87 | ||
3bd1e081 MD |
88 | /* |
89 | * Get the produced position | |
90 | * | |
91 | * Returns 0 on success, < 0 on error | |
92 | */ | |
ffe60014 | 93 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
94 | unsigned long *pos) |
95 | { | |
96 | int ret; | |
97 | int infd = stream->wait_fd; | |
98 | ||
99 | ret = kernctl_snapshot_get_produced(infd, pos); | |
100 | if (ret != 0) { | |
5a510c9f | 101 | PERROR("kernctl_snapshot_get_produced"); |
3bd1e081 MD |
102 | } |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
07b86b52 JD |
107 | /* |
108 | * Get the consumerd position | |
109 | * | |
110 | * Returns 0 on success, < 0 on error | |
111 | */ | |
112 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
113 | unsigned long *pos) | |
114 | { | |
115 | int ret; | |
116 | int infd = stream->wait_fd; | |
117 | ||
118 | ret = kernctl_snapshot_get_consumed(infd, pos); | |
119 | if (ret != 0) { | |
5a510c9f | 120 | PERROR("kernctl_snapshot_get_consumed"); |
07b86b52 JD |
121 | } |
122 | ||
123 | return ret; | |
124 | } | |
125 | ||
07b86b52 JD |
126 | /* |
127 | * Take a snapshot of all the stream of a channel | |
3eb928aa | 128 | * RCU read-side lock must be held across this function to ensure existence of |
d2956687 | 129 | * channel. The channel lock must be held by the caller. |
07b86b52 JD |
130 | * |
131 | * Returns 0 on success, < 0 on error | |
132 | */ | |
f72bb42f JG |
133 | static int lttng_kconsumer_snapshot_channel( |
134 | struct lttng_consumer_channel *channel, | |
135 | uint64_t key, char *path, uint64_t relayd_id, | |
136 | uint64_t nb_packets_per_stream, | |
5c786ded | 137 | struct lttng_consumer_local_data *ctx) |
07b86b52 JD |
138 | { |
139 | int ret; | |
07b86b52 JD |
140 | struct lttng_consumer_stream *stream; |
141 | ||
6a00837f | 142 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
07b86b52 JD |
143 | |
144 | rcu_read_lock(); | |
145 | ||
07b86b52 JD |
146 | /* Splice is not supported yet for channel snapshot. */ |
147 | if (channel->output != CONSUMER_CHANNEL_MMAP) { | |
9381314c JG |
148 | ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot", |
149 | channel->name); | |
07b86b52 JD |
150 | ret = -1; |
151 | goto end; | |
152 | } | |
153 | ||
10a50311 | 154 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
923333cd | 155 | unsigned long consumed_pos, produced_pos; |
9ce5646a MD |
156 | |
157 | health_code_update(); | |
158 | ||
07b86b52 JD |
159 | /* |
160 | * Lock stream because we are about to change its state. | |
161 | */ | |
162 | pthread_mutex_lock(&stream->lock); | |
163 | ||
d2956687 JG |
164 | assert(channel->trace_chunk); |
165 | if (!lttng_trace_chunk_get(channel->trace_chunk)) { | |
166 | /* | |
167 | * Can't happen barring an internal error as the channel | |
168 | * holds a reference to the trace chunk. | |
169 | */ | |
170 | ERR("Failed to acquire reference to channel's trace chunk"); | |
171 | ret = -1; | |
172 | goto end_unlock; | |
173 | } | |
174 | assert(!stream->trace_chunk); | |
175 | stream->trace_chunk = channel->trace_chunk; | |
176 | ||
29decac3 DG |
177 | /* |
178 | * Assign the received relayd ID so we can use it for streaming. The streams | |
179 | * are not visible to anyone so this is OK to change it. | |
180 | */ | |
07b86b52 JD |
181 | stream->net_seq_idx = relayd_id; |
182 | channel->relayd_id = relayd_id; | |
183 | if (relayd_id != (uint64_t) -1ULL) { | |
10a50311 | 184 | ret = consumer_send_relayd_stream(stream, path); |
07b86b52 JD |
185 | if (ret < 0) { |
186 | ERR("sending stream to relayd"); | |
187 | goto end_unlock; | |
188 | } | |
07b86b52 | 189 | } else { |
d2956687 JG |
190 | ret = consumer_stream_create_output_files(stream, |
191 | false); | |
07b86b52 | 192 | if (ret < 0) { |
07b86b52 JD |
193 | goto end_unlock; |
194 | } | |
d2956687 JG |
195 | DBG("Kernel consumer snapshot stream (%" PRIu64 ")", |
196 | stream->key); | |
07b86b52 JD |
197 | } |
198 | ||
f22dd891 | 199 | ret = kernctl_buffer_flush_empty(stream->wait_fd); |
07b86b52 | 200 | if (ret < 0) { |
f22dd891 MD |
201 | /* |
202 | * Doing a buffer flush which does not take into | |
203 | * account empty packets. This is not perfect | |
204 | * for stream intersection, but required as a | |
205 | * fall-back when "flush_empty" is not | |
206 | * implemented by lttng-modules. | |
207 | */ | |
208 | ret = kernctl_buffer_flush(stream->wait_fd); | |
209 | if (ret < 0) { | |
210 | ERR("Failed to flush kernel stream"); | |
211 | goto end_unlock; | |
212 | } | |
07b86b52 JD |
213 | goto end_unlock; |
214 | } | |
215 | ||
216 | ret = lttng_kconsumer_take_snapshot(stream); | |
217 | if (ret < 0) { | |
218 | ERR("Taking kernel snapshot"); | |
219 | goto end_unlock; | |
220 | } | |
221 | ||
222 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); | |
223 | if (ret < 0) { | |
224 | ERR("Produced kernel snapshot position"); | |
225 | goto end_unlock; | |
226 | } | |
227 | ||
228 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
229 | if (ret < 0) { | |
230 | ERR("Consumerd kernel snapshot position"); | |
231 | goto end_unlock; | |
232 | } | |
233 | ||
234 | if (stream->max_sb_size == 0) { | |
235 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, | |
236 | &stream->max_sb_size); | |
237 | if (ret < 0) { | |
238 | ERR("Getting kernel max_sb_size"); | |
239 | goto end_unlock; | |
240 | } | |
241 | } | |
242 | ||
d07ceecd MD |
243 | consumed_pos = consumer_get_consume_start_pos(consumed_pos, |
244 | produced_pos, nb_packets_per_stream, | |
245 | stream->max_sb_size); | |
5c786ded | 246 | |
9377d830 | 247 | while ((long) (consumed_pos - produced_pos) < 0) { |
07b86b52 JD |
248 | ssize_t read_len; |
249 | unsigned long len, padded_len; | |
250 | ||
9ce5646a MD |
251 | health_code_update(); |
252 | ||
07b86b52 JD |
253 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); |
254 | ||
255 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); | |
256 | if (ret < 0) { | |
32af2c95 | 257 | if (ret != -EAGAIN) { |
07b86b52 JD |
258 | PERROR("kernctl_get_subbuf snapshot"); |
259 | goto end_unlock; | |
260 | } | |
261 | DBG("Kernel consumer get subbuf failed. Skipping it."); | |
262 | consumed_pos += stream->max_sb_size; | |
ddc93ee4 | 263 | stream->chan->lost_packets++; |
07b86b52 JD |
264 | continue; |
265 | } | |
266 | ||
267 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); | |
268 | if (ret < 0) { | |
269 | ERR("Snapshot kernctl_get_subbuf_size"); | |
29decac3 | 270 | goto error_put_subbuf; |
07b86b52 JD |
271 | } |
272 | ||
273 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); | |
274 | if (ret < 0) { | |
275 | ERR("Snapshot kernctl_get_padded_subbuf_size"); | |
29decac3 | 276 | goto error_put_subbuf; |
07b86b52 JD |
277 | } |
278 | ||
279 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
309167d2 | 280 | padded_len - len, NULL); |
07b86b52 | 281 | /* |
29decac3 DG |
282 | * We write the padded len in local tracefiles but the data len |
283 | * when using a relay. Display the error but continue processing | |
284 | * to try to release the subbuffer. | |
07b86b52 JD |
285 | */ |
286 | if (relayd_id != (uint64_t) -1ULL) { | |
287 | if (read_len != len) { | |
288 | ERR("Error sending to the relay (ret: %zd != len: %lu)", | |
289 | read_len, len); | |
290 | } | |
291 | } else { | |
292 | if (read_len != padded_len) { | |
293 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", | |
294 | read_len, padded_len); | |
295 | } | |
296 | } | |
297 | ||
298 | ret = kernctl_put_subbuf(stream->wait_fd); | |
299 | if (ret < 0) { | |
300 | ERR("Snapshot kernctl_put_subbuf"); | |
301 | goto end_unlock; | |
302 | } | |
303 | consumed_pos += stream->max_sb_size; | |
304 | } | |
305 | ||
306 | if (relayd_id == (uint64_t) -1ULL) { | |
fdf9986c MD |
307 | if (stream->out_fd >= 0) { |
308 | ret = close(stream->out_fd); | |
309 | if (ret < 0) { | |
310 | PERROR("Kernel consumer snapshot close out_fd"); | |
311 | goto end_unlock; | |
312 | } | |
313 | stream->out_fd = -1; | |
07b86b52 | 314 | } |
07b86b52 JD |
315 | } else { |
316 | close_relayd_stream(stream); | |
317 | stream->net_seq_idx = (uint64_t) -1ULL; | |
318 | } | |
d2956687 JG |
319 | lttng_trace_chunk_put(stream->trace_chunk); |
320 | stream->trace_chunk = NULL; | |
07b86b52 JD |
321 | pthread_mutex_unlock(&stream->lock); |
322 | } | |
323 | ||
324 | /* All good! */ | |
325 | ret = 0; | |
326 | goto end; | |
327 | ||
29decac3 DG |
328 | error_put_subbuf: |
329 | ret = kernctl_put_subbuf(stream->wait_fd); | |
330 | if (ret < 0) { | |
331 | ERR("Snapshot kernctl_put_subbuf error path"); | |
332 | } | |
07b86b52 JD |
333 | end_unlock: |
334 | pthread_mutex_unlock(&stream->lock); | |
335 | end: | |
336 | rcu_read_unlock(); | |
337 | return ret; | |
338 | } | |
339 | ||
340 | /* | |
341 | * Read the whole metadata available for a snapshot. | |
3eb928aa | 342 | * RCU read-side lock must be held across this function to ensure existence of |
d2956687 | 343 | * metadata_channel. The channel lock must be held by the caller. |
07b86b52 JD |
344 | * |
345 | * Returns 0 on success, < 0 on error | |
346 | */ | |
d2956687 JG |
347 | static int lttng_kconsumer_snapshot_metadata( |
348 | struct lttng_consumer_channel *metadata_channel, | |
3eb928aa MD |
349 | uint64_t key, char *path, uint64_t relayd_id, |
350 | struct lttng_consumer_local_data *ctx) | |
07b86b52 | 351 | { |
d771f832 DG |
352 | int ret, use_relayd = 0; |
353 | ssize_t ret_read; | |
07b86b52 | 354 | struct lttng_consumer_stream *metadata_stream; |
d771f832 DG |
355 | |
356 | assert(ctx); | |
07b86b52 JD |
357 | |
358 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", | |
359 | key, path); | |
360 | ||
361 | rcu_read_lock(); | |
362 | ||
07b86b52 JD |
363 | metadata_stream = metadata_channel->metadata_stream; |
364 | assert(metadata_stream); | |
d2956687 | 365 | |
fa27abe8 | 366 | pthread_mutex_lock(&metadata_stream->lock); |
d2956687 JG |
367 | assert(metadata_channel->trace_chunk); |
368 | assert(metadata_stream->trace_chunk); | |
07b86b52 | 369 | |
d771f832 | 370 | /* Flag once that we have a valid relayd for the stream. */ |
e2039c7a | 371 | if (relayd_id != (uint64_t) -1ULL) { |
d771f832 DG |
372 | use_relayd = 1; |
373 | } | |
374 | ||
375 | if (use_relayd) { | |
10a50311 | 376 | ret = consumer_send_relayd_stream(metadata_stream, path); |
e2039c7a | 377 | if (ret < 0) { |
fa27abe8 | 378 | goto error_snapshot; |
e2039c7a | 379 | } |
e2039c7a | 380 | } else { |
d2956687 JG |
381 | ret = consumer_stream_create_output_files(metadata_stream, |
382 | false); | |
e2039c7a | 383 | if (ret < 0) { |
fa27abe8 | 384 | goto error_snapshot; |
e2039c7a | 385 | } |
07b86b52 | 386 | } |
07b86b52 | 387 | |
d771f832 | 388 | do { |
9ce5646a MD |
389 | health_code_update(); |
390 | ||
d2956687 | 391 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); |
d771f832 | 392 | if (ret_read < 0) { |
56591bac | 393 | if (ret_read != -EAGAIN) { |
6a00837f | 394 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
d771f832 | 395 | ret_read); |
fa27abe8 JG |
396 | ret = ret_read; |
397 | goto error_snapshot; | |
07b86b52 | 398 | } |
d771f832 | 399 | /* ret_read is negative at this point so we will exit the loop. */ |
07b86b52 JD |
400 | continue; |
401 | } | |
d771f832 | 402 | } while (ret_read >= 0); |
07b86b52 | 403 | |
d771f832 DG |
404 | if (use_relayd) { |
405 | close_relayd_stream(metadata_stream); | |
406 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; | |
407 | } else { | |
fdf9986c MD |
408 | if (metadata_stream->out_fd >= 0) { |
409 | ret = close(metadata_stream->out_fd); | |
410 | if (ret < 0) { | |
411 | PERROR("Kernel consumer snapshot metadata close out_fd"); | |
412 | /* | |
413 | * Don't go on error here since the snapshot was successful at this | |
414 | * point but somehow the close failed. | |
415 | */ | |
416 | } | |
417 | metadata_stream->out_fd = -1; | |
d2956687 JG |
418 | lttng_trace_chunk_put(metadata_stream->trace_chunk); |
419 | metadata_stream->trace_chunk = NULL; | |
e2039c7a | 420 | } |
e2039c7a JD |
421 | } |
422 | ||
07b86b52 | 423 | ret = 0; |
fa27abe8 JG |
424 | error_snapshot: |
425 | pthread_mutex_unlock(&metadata_stream->lock); | |
cf53a8a6 JD |
426 | cds_list_del(&metadata_stream->send_node); |
427 | consumer_stream_destroy(metadata_stream, NULL); | |
428 | metadata_channel->metadata_stream = NULL; | |
07b86b52 JD |
429 | rcu_read_unlock(); |
430 | return ret; | |
431 | } | |
432 | ||
1803a064 MD |
433 | /* |
434 | * Receive command from session daemon and process it. | |
435 | * | |
436 | * Return 1 on success else a negative value or 0. | |
437 | */ | |
3bd1e081 MD |
438 | int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, |
439 | int sock, struct pollfd *consumer_sockpoll) | |
440 | { | |
441 | ssize_t ret; | |
0c759fc9 | 442 | enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS; |
3bd1e081 MD |
443 | struct lttcomm_consumer_msg msg; |
444 | ||
9ce5646a MD |
445 | health_code_update(); |
446 | ||
3bd1e081 MD |
447 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); |
448 | if (ret != sizeof(msg)) { | |
1803a064 | 449 | if (ret > 0) { |
c6857fcf | 450 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD); |
1803a064 MD |
451 | ret = -1; |
452 | } | |
3bd1e081 MD |
453 | return ret; |
454 | } | |
9ce5646a MD |
455 | |
456 | health_code_update(); | |
457 | ||
84382d49 MD |
458 | /* Deprecated command */ |
459 | assert(msg.cmd_type != LTTNG_CONSUMER_STOP); | |
3bd1e081 | 460 | |
9ce5646a MD |
461 | health_code_update(); |
462 | ||
b0b335c8 MD |
463 | /* relayd needs RCU read-side protection */ |
464 | rcu_read_lock(); | |
465 | ||
3bd1e081 | 466 | switch (msg.cmd_type) { |
00e2e675 DG |
467 | case LTTNG_CONSUMER_ADD_RELAYD_SOCKET: |
468 | { | |
f50f23d9 | 469 | /* Session daemon status message are handled in the following call. */ |
2527bf85 | 470 | consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
7735ef9e | 471 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, |
d3e2ba59 | 472 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id, |
2527bf85 | 473 | msg.u.relayd_sock.relayd_session_id); |
00e2e675 DG |
474 | goto end_nosignal; |
475 | } | |
3bd1e081 MD |
476 | case LTTNG_CONSUMER_ADD_CHANNEL: |
477 | { | |
478 | struct lttng_consumer_channel *new_channel; | |
e43c41c5 | 479 | int ret_recv; |
d2956687 | 480 | const uint64_t chunk_id = msg.u.channel.chunk_id.value; |
3bd1e081 | 481 | |
9ce5646a MD |
482 | health_code_update(); |
483 | ||
f50f23d9 DG |
484 | /* First send a status message before receiving the fds. */ |
485 | ret = consumer_send_status_msg(sock, ret_code); | |
486 | if (ret < 0) { | |
487 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 488 | goto error_fatal; |
f50f23d9 | 489 | } |
9ce5646a MD |
490 | |
491 | health_code_update(); | |
492 | ||
d88aee68 | 493 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 494 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
d2956687 JG |
495 | msg.u.channel.session_id, |
496 | msg.u.channel.chunk_id.is_set ? | |
497 | &chunk_id : NULL, | |
498 | msg.u.channel.pathname, | |
499 | msg.u.channel.name, | |
1624d5b7 JD |
500 | msg.u.channel.relayd_id, msg.u.channel.output, |
501 | msg.u.channel.tracefile_size, | |
1950109e | 502 | msg.u.channel.tracefile_count, 0, |
ecc48a90 | 503 | msg.u.channel.monitor, |
d7ba1388 | 504 | msg.u.channel.live_timer_interval, |
3d071855 | 505 | NULL, NULL); |
3bd1e081 | 506 | if (new_channel == NULL) { |
f73fabfd | 507 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
508 | goto end_nosignal; |
509 | } | |
ffe60014 | 510 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
95a1109b JD |
511 | switch (msg.u.channel.output) { |
512 | case LTTNG_EVENT_SPLICE: | |
513 | new_channel->output = CONSUMER_CHANNEL_SPLICE; | |
514 | break; | |
515 | case LTTNG_EVENT_MMAP: | |
516 | new_channel->output = CONSUMER_CHANNEL_MMAP; | |
517 | break; | |
518 | default: | |
519 | ERR("Channel output unknown %d", msg.u.channel.output); | |
520 | goto end_nosignal; | |
521 | } | |
ffe60014 DG |
522 | |
523 | /* Translate and save channel type. */ | |
524 | switch (msg.u.channel.type) { | |
525 | case CONSUMER_CHANNEL_TYPE_DATA: | |
526 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
527 | new_channel->type = msg.u.channel.type; | |
528 | break; | |
529 | default: | |
530 | assert(0); | |
531 | goto end_nosignal; | |
532 | }; | |
533 | ||
9ce5646a MD |
534 | health_code_update(); |
535 | ||
3bd1e081 | 536 | if (ctx->on_recv_channel != NULL) { |
e43c41c5 JD |
537 | ret_recv = ctx->on_recv_channel(new_channel); |
538 | if (ret_recv == 0) { | |
539 | ret = consumer_add_channel(new_channel, ctx); | |
540 | } else if (ret_recv < 0) { | |
3bd1e081 MD |
541 | goto end_nosignal; |
542 | } | |
543 | } else { | |
e43c41c5 | 544 | ret = consumer_add_channel(new_channel, ctx); |
3bd1e081 | 545 | } |
e9404c27 JG |
546 | if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) { |
547 | int monitor_start_ret; | |
548 | ||
549 | DBG("Consumer starting monitor timer"); | |
94d49140 JD |
550 | consumer_timer_live_start(new_channel, |
551 | msg.u.channel.live_timer_interval); | |
e9404c27 JG |
552 | monitor_start_ret = consumer_timer_monitor_start( |
553 | new_channel, | |
554 | msg.u.channel.monitor_timer_interval); | |
555 | if (monitor_start_ret < 0) { | |
556 | ERR("Starting channel monitoring timer failed"); | |
557 | goto end_nosignal; | |
558 | } | |
559 | ||
94d49140 | 560 | } |
e43c41c5 | 561 | |
9ce5646a MD |
562 | health_code_update(); |
563 | ||
e43c41c5 | 564 | /* If we received an error in add_channel, we need to report it. */ |
821fffb2 | 565 | if (ret < 0) { |
1803a064 MD |
566 | ret = consumer_send_status_msg(sock, ret); |
567 | if (ret < 0) { | |
568 | goto error_fatal; | |
569 | } | |
e43c41c5 JD |
570 | goto end_nosignal; |
571 | } | |
572 | ||
3bd1e081 MD |
573 | goto end_nosignal; |
574 | } | |
575 | case LTTNG_CONSUMER_ADD_STREAM: | |
576 | { | |
dae10966 DG |
577 | int fd; |
578 | struct lttng_pipe *stream_pipe; | |
00e2e675 | 579 | struct lttng_consumer_stream *new_stream; |
ffe60014 | 580 | struct lttng_consumer_channel *channel; |
c80048c6 | 581 | int alloc_ret = 0; |
3bd1e081 | 582 | |
ffe60014 DG |
583 | /* |
584 | * Get stream's channel reference. Needed when adding the stream to the | |
585 | * global hash table. | |
586 | */ | |
587 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
588 | if (!channel) { | |
589 | /* | |
590 | * We could not find the channel. Can happen if cpu hotplug | |
591 | * happens while tearing down. | |
592 | */ | |
d88aee68 | 593 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
e462382a | 594 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
ffe60014 DG |
595 | } |
596 | ||
9ce5646a MD |
597 | health_code_update(); |
598 | ||
f50f23d9 DG |
599 | /* First send a status message before receiving the fds. */ |
600 | ret = consumer_send_status_msg(sock, ret_code); | |
1803a064 | 601 | if (ret < 0) { |
d771f832 | 602 | /* Somehow, the session daemon is not responding anymore. */ |
1803a064 MD |
603 | goto error_fatal; |
604 | } | |
9ce5646a MD |
605 | |
606 | health_code_update(); | |
607 | ||
0c759fc9 | 608 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
d771f832 | 609 | /* Channel was not found. */ |
f50f23d9 DG |
610 | goto end_nosignal; |
611 | } | |
612 | ||
d771f832 | 613 | /* Blocking call */ |
9ce5646a MD |
614 | health_poll_entry(); |
615 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
616 | health_poll_exit(); | |
84382d49 MD |
617 | if (ret) { |
618 | goto error_fatal; | |
3bd1e081 | 619 | } |
00e2e675 | 620 | |
9ce5646a MD |
621 | health_code_update(); |
622 | ||
00e2e675 | 623 | /* Get stream file descriptor from socket */ |
f2fc6720 MD |
624 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
625 | if (ret != sizeof(fd)) { | |
f73fabfd | 626 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 627 | rcu_read_unlock(); |
3bd1e081 MD |
628 | return ret; |
629 | } | |
3bd1e081 | 630 | |
9ce5646a MD |
631 | health_code_update(); |
632 | ||
f50f23d9 DG |
633 | /* |
634 | * Send status code to session daemon only if the recv works. If the | |
635 | * above recv() failed, the session daemon is notified through the | |
636 | * error socket and the teardown is eventually done. | |
637 | */ | |
638 | ret = consumer_send_status_msg(sock, ret_code); | |
639 | if (ret < 0) { | |
640 | /* Somehow, the session daemon is not responding anymore. */ | |
641 | goto end_nosignal; | |
642 | } | |
643 | ||
9ce5646a MD |
644 | health_code_update(); |
645 | ||
d2956687 | 646 | pthread_mutex_lock(&channel->lock); |
ffe60014 DG |
647 | new_stream = consumer_allocate_stream(channel->key, |
648 | fd, | |
ffe60014 | 649 | channel->name, |
ffe60014 DG |
650 | channel->relayd_id, |
651 | channel->session_id, | |
d2956687 | 652 | channel->trace_chunk, |
ffe60014 DG |
653 | msg.u.stream.cpu, |
654 | &alloc_ret, | |
4891ece8 | 655 | channel->type, |
d2956687 | 656 | channel->monitor); |
3bd1e081 | 657 | if (new_stream == NULL) { |
c80048c6 MD |
658 | switch (alloc_ret) { |
659 | case -ENOMEM: | |
660 | case -EINVAL: | |
661 | default: | |
662 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
663 | break; | |
c80048c6 | 664 | } |
d2956687 | 665 | pthread_mutex_unlock(&channel->lock); |
3f8e211f | 666 | goto end_nosignal; |
3bd1e081 | 667 | } |
d771f832 | 668 | |
ffe60014 DG |
669 | new_stream->chan = channel; |
670 | new_stream->wait_fd = fd; | |
d9a2e16e JD |
671 | consumer_stream_update_channel_attributes(new_stream, |
672 | channel); | |
07b86b52 JD |
673 | switch (channel->output) { |
674 | case CONSUMER_CHANNEL_SPLICE: | |
675 | new_stream->output = LTTNG_EVENT_SPLICE; | |
a2361a61 JD |
676 | ret = utils_create_pipe(new_stream->splice_pipe); |
677 | if (ret < 0) { | |
d2956687 | 678 | pthread_mutex_unlock(&channel->lock); |
a2361a61 JD |
679 | goto end_nosignal; |
680 | } | |
07b86b52 JD |
681 | break; |
682 | case CONSUMER_CHANNEL_MMAP: | |
683 | new_stream->output = LTTNG_EVENT_MMAP; | |
684 | break; | |
685 | default: | |
686 | ERR("Stream output unknown %d", channel->output); | |
d2956687 | 687 | pthread_mutex_unlock(&channel->lock); |
07b86b52 JD |
688 | goto end_nosignal; |
689 | } | |
00e2e675 | 690 | |
a0c83db9 DG |
691 | /* |
692 | * We've just assigned the channel to the stream so increment the | |
07b86b52 JD |
693 | * refcount right now. We don't need to increment the refcount for |
694 | * streams in no monitor because we handle manually the cleanup of | |
695 | * those. It is very important to make sure there is NO prior | |
696 | * consumer_del_stream() calls or else the refcount will be unbalanced. | |
a0c83db9 | 697 | */ |
07b86b52 JD |
698 | if (channel->monitor) { |
699 | uatomic_inc(&new_stream->chan->refcount); | |
700 | } | |
9d9353f9 | 701 | |
fb3a43a9 DG |
702 | /* |
703 | * The buffer flush is done on the session daemon side for the kernel | |
704 | * so no need for the stream "hangup_flush_done" variable to be | |
705 | * tracked. This is important for a kernel stream since we don't rely | |
706 | * on the flush state of the stream to read data. It's not the case for | |
707 | * user space tracing. | |
708 | */ | |
709 | new_stream->hangup_flush_done = 0; | |
710 | ||
9ce5646a MD |
711 | health_code_update(); |
712 | ||
d2956687 | 713 | pthread_mutex_lock(&new_stream->lock); |
633d0084 DG |
714 | if (ctx->on_recv_stream) { |
715 | ret = ctx->on_recv_stream(new_stream); | |
716 | if (ret < 0) { | |
d2956687 JG |
717 | pthread_mutex_unlock(&new_stream->lock); |
718 | pthread_mutex_unlock(&channel->lock); | |
d771f832 | 719 | consumer_stream_free(new_stream); |
633d0084 | 720 | goto end_nosignal; |
fb3a43a9 | 721 | } |
633d0084 | 722 | } |
9ce5646a MD |
723 | health_code_update(); |
724 | ||
07b86b52 JD |
725 | if (new_stream->metadata_flag) { |
726 | channel->metadata_stream = new_stream; | |
727 | } | |
728 | ||
2bba9e53 DG |
729 | /* Do not monitor this stream. */ |
730 | if (!channel->monitor) { | |
5eecee74 | 731 | DBG("Kernel consumer add stream %s in no monitor mode with " |
6dc3064a | 732 | "relayd id %" PRIu64, new_stream->name, |
5eecee74 | 733 | new_stream->net_seq_idx); |
10a50311 | 734 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
d2956687 JG |
735 | pthread_mutex_unlock(&new_stream->lock); |
736 | pthread_mutex_unlock(&channel->lock); | |
6dc3064a DG |
737 | break; |
738 | } | |
739 | ||
e1b71bdc DG |
740 | /* Send stream to relayd if the stream has an ID. */ |
741 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { | |
194ee077 DG |
742 | ret = consumer_send_relayd_stream(new_stream, |
743 | new_stream->chan->pathname); | |
e1b71bdc | 744 | if (ret < 0) { |
d2956687 JG |
745 | pthread_mutex_unlock(&new_stream->lock); |
746 | pthread_mutex_unlock(&channel->lock); | |
e1b71bdc DG |
747 | consumer_stream_free(new_stream); |
748 | goto end_nosignal; | |
749 | } | |
001b7e62 MD |
750 | |
751 | /* | |
752 | * If adding an extra stream to an already | |
753 | * existing channel (e.g. cpu hotplug), we need | |
754 | * to send the "streams_sent" command to relayd. | |
755 | */ | |
756 | if (channel->streams_sent_to_relayd) { | |
757 | ret = consumer_send_relayd_streams_sent( | |
758 | new_stream->net_seq_idx); | |
759 | if (ret < 0) { | |
d2956687 JG |
760 | pthread_mutex_unlock(&new_stream->lock); |
761 | pthread_mutex_unlock(&channel->lock); | |
001b7e62 MD |
762 | goto end_nosignal; |
763 | } | |
764 | } | |
e2039c7a | 765 | } |
d2956687 JG |
766 | pthread_mutex_unlock(&new_stream->lock); |
767 | pthread_mutex_unlock(&channel->lock); | |
e2039c7a | 768 | |
50f8ae69 | 769 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 770 | if (new_stream->metadata_flag) { |
66d583dc | 771 | consumer_add_metadata_stream(new_stream); |
dae10966 | 772 | stream_pipe = ctx->consumer_metadata_pipe; |
3bd1e081 | 773 | } else { |
66d583dc | 774 | consumer_add_data_stream(new_stream); |
dae10966 | 775 | stream_pipe = ctx->consumer_data_pipe; |
50f8ae69 DG |
776 | } |
777 | ||
66d583dc | 778 | /* Visible to other threads */ |
5ab66908 MD |
779 | new_stream->globally_visible = 1; |
780 | ||
9ce5646a MD |
781 | health_code_update(); |
782 | ||
dae10966 | 783 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
50f8ae69 | 784 | if (ret < 0) { |
dae10966 | 785 | ERR("Consumer write %s stream to pipe %d", |
50f8ae69 | 786 | new_stream->metadata_flag ? "metadata" : "data", |
dae10966 | 787 | lttng_pipe_get_writefd(stream_pipe)); |
5ab66908 MD |
788 | if (new_stream->metadata_flag) { |
789 | consumer_del_stream_for_metadata(new_stream); | |
790 | } else { | |
791 | consumer_del_stream_for_data(new_stream); | |
792 | } | |
50f8ae69 | 793 | goto end_nosignal; |
3bd1e081 | 794 | } |
00e2e675 | 795 | |
02d02e31 JD |
796 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64, |
797 | new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id); | |
3bd1e081 MD |
798 | break; |
799 | } | |
a4baae1b JD |
800 | case LTTNG_CONSUMER_STREAMS_SENT: |
801 | { | |
802 | struct lttng_consumer_channel *channel; | |
803 | ||
804 | /* | |
805 | * Get stream's channel reference. Needed when adding the stream to the | |
806 | * global hash table. | |
807 | */ | |
808 | channel = consumer_find_channel(msg.u.sent_streams.channel_key); | |
809 | if (!channel) { | |
810 | /* | |
811 | * We could not find the channel. Can happen if cpu hotplug | |
812 | * happens while tearing down. | |
813 | */ | |
814 | ERR("Unable to find channel key %" PRIu64, | |
815 | msg.u.sent_streams.channel_key); | |
e462382a | 816 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
a4baae1b JD |
817 | } |
818 | ||
819 | health_code_update(); | |
820 | ||
821 | /* | |
822 | * Send status code to session daemon. | |
823 | */ | |
824 | ret = consumer_send_status_msg(sock, ret_code); | |
f261ad0a | 825 | if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
a4baae1b JD |
826 | /* Somehow, the session daemon is not responding anymore. */ |
827 | goto end_nosignal; | |
828 | } | |
829 | ||
830 | health_code_update(); | |
831 | ||
832 | /* | |
833 | * We should not send this message if we don't monitor the | |
834 | * streams in this channel. | |
835 | */ | |
836 | if (!channel->monitor) { | |
837 | break; | |
838 | } | |
839 | ||
840 | health_code_update(); | |
841 | /* Send stream to relayd if the stream has an ID. */ | |
842 | if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) { | |
843 | ret = consumer_send_relayd_streams_sent( | |
844 | msg.u.sent_streams.net_seq_idx); | |
845 | if (ret < 0) { | |
846 | goto end_nosignal; | |
847 | } | |
001b7e62 | 848 | channel->streams_sent_to_relayd = true; |
a4baae1b JD |
849 | } |
850 | break; | |
851 | } | |
3bd1e081 MD |
852 | case LTTNG_CONSUMER_UPDATE_STREAM: |
853 | { | |
3f8e211f DG |
854 | rcu_read_unlock(); |
855 | return -ENOSYS; | |
856 | } | |
857 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
858 | { | |
a6ba4fe1 | 859 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
860 | struct consumer_relayd_sock_pair *relayd; |
861 | ||
a6ba4fe1 | 862 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
863 | |
864 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 865 | relayd = consumer_find_relayd(index); |
3f8e211f | 866 | if (relayd == NULL) { |
3448e266 | 867 | DBG("Unable to find relayd %" PRIu64, index); |
e462382a | 868 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
3bd1e081 | 869 | } |
3f8e211f | 870 | |
a6ba4fe1 DG |
871 | /* |
872 | * Each relayd socket pair has a refcount of stream attached to it | |
873 | * which tells if the relayd is still active or not depending on the | |
874 | * refcount value. | |
875 | * | |
876 | * This will set the destroy flag of the relayd object and destroy it | |
877 | * if the refcount reaches zero when called. | |
878 | * | |
879 | * The destroy can happen either here or when a stream fd hangs up. | |
880 | */ | |
f50f23d9 DG |
881 | if (relayd) { |
882 | consumer_flag_relayd_for_destroy(relayd); | |
883 | } | |
884 | ||
9ce5646a MD |
885 | health_code_update(); |
886 | ||
f50f23d9 DG |
887 | ret = consumer_send_status_msg(sock, ret_code); |
888 | if (ret < 0) { | |
889 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 890 | goto error_fatal; |
f50f23d9 | 891 | } |
3f8e211f | 892 | |
3f8e211f | 893 | goto end_nosignal; |
3bd1e081 | 894 | } |
6d805429 | 895 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 896 | { |
c8f59ee5 | 897 | int32_t ret; |
6d805429 | 898 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 899 | |
6d805429 | 900 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 901 | |
6d805429 | 902 | ret = consumer_data_pending(id); |
c8f59ee5 | 903 | |
9ce5646a MD |
904 | health_code_update(); |
905 | ||
c8f59ee5 DG |
906 | /* Send back returned value to session daemon */ |
907 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
908 | if (ret < 0) { | |
6d805429 | 909 | PERROR("send data pending ret code"); |
1803a064 | 910 | goto error_fatal; |
c8f59ee5 | 911 | } |
f50f23d9 DG |
912 | |
913 | /* | |
914 | * No need to send back a status message since the data pending | |
915 | * returned value is the response. | |
916 | */ | |
c8f59ee5 | 917 | break; |
53632229 | 918 | } |
6dc3064a DG |
919 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
920 | { | |
3eb928aa MD |
921 | struct lttng_consumer_channel *channel; |
922 | uint64_t key = msg.u.snapshot_channel.key; | |
923 | ||
924 | channel = consumer_find_channel(key); | |
925 | if (!channel) { | |
926 | ERR("Channel %" PRIu64 " not found", key); | |
927 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
07b86b52 | 928 | } else { |
d2956687 | 929 | pthread_mutex_lock(&channel->lock); |
3eb928aa MD |
930 | if (msg.u.snapshot_channel.metadata == 1) { |
931 | ret = lttng_kconsumer_snapshot_metadata(channel, key, | |
932 | msg.u.snapshot_channel.pathname, | |
933 | msg.u.snapshot_channel.relayd_id, ctx); | |
934 | if (ret < 0) { | |
935 | ERR("Snapshot metadata failed"); | |
936 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; | |
937 | } | |
938 | } else { | |
939 | ret = lttng_kconsumer_snapshot_channel(channel, key, | |
940 | msg.u.snapshot_channel.pathname, | |
941 | msg.u.snapshot_channel.relayd_id, | |
942 | msg.u.snapshot_channel.nb_packets_per_stream, | |
943 | ctx); | |
944 | if (ret < 0) { | |
945 | ERR("Snapshot channel failed"); | |
946 | ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED; | |
947 | } | |
07b86b52 | 948 | } |
d2956687 | 949 | pthread_mutex_unlock(&channel->lock); |
07b86b52 | 950 | } |
9ce5646a MD |
951 | health_code_update(); |
952 | ||
6dc3064a DG |
953 | ret = consumer_send_status_msg(sock, ret_code); |
954 | if (ret < 0) { | |
955 | /* Somehow, the session daemon is not responding anymore. */ | |
956 | goto end_nosignal; | |
957 | } | |
958 | break; | |
959 | } | |
07b86b52 JD |
960 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
961 | { | |
962 | uint64_t key = msg.u.destroy_channel.key; | |
963 | struct lttng_consumer_channel *channel; | |
964 | ||
965 | channel = consumer_find_channel(key); | |
966 | if (!channel) { | |
967 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); | |
e462382a | 968 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
07b86b52 JD |
969 | } |
970 | ||
9ce5646a MD |
971 | health_code_update(); |
972 | ||
07b86b52 JD |
973 | ret = consumer_send_status_msg(sock, ret_code); |
974 | if (ret < 0) { | |
975 | /* Somehow, the session daemon is not responding anymore. */ | |
976 | goto end_nosignal; | |
977 | } | |
978 | ||
9ce5646a MD |
979 | health_code_update(); |
980 | ||
15dc512a DG |
981 | /* Stop right now if no channel was found. */ |
982 | if (!channel) { | |
983 | goto end_nosignal; | |
984 | } | |
985 | ||
07b86b52 JD |
986 | /* |
987 | * This command should ONLY be issued for channel with streams set in | |
988 | * no monitor mode. | |
989 | */ | |
990 | assert(!channel->monitor); | |
991 | ||
992 | /* | |
993 | * The refcount should ALWAYS be 0 in the case of a channel in no | |
994 | * monitor mode. | |
995 | */ | |
996 | assert(!uatomic_sub_return(&channel->refcount, 1)); | |
997 | ||
998 | consumer_del_channel(channel); | |
999 | ||
1000 | goto end_nosignal; | |
1001 | } | |
fb83fe64 JD |
1002 | case LTTNG_CONSUMER_DISCARDED_EVENTS: |
1003 | { | |
66ab32be JD |
1004 | ssize_t ret; |
1005 | uint64_t count; | |
fb83fe64 JD |
1006 | struct lttng_consumer_channel *channel; |
1007 | uint64_t id = msg.u.discarded_events.session_id; | |
1008 | uint64_t key = msg.u.discarded_events.channel_key; | |
1009 | ||
e5742757 MD |
1010 | DBG("Kernel consumer discarded events command for session id %" |
1011 | PRIu64 ", channel key %" PRIu64, id, key); | |
1012 | ||
fb83fe64 JD |
1013 | channel = consumer_find_channel(key); |
1014 | if (!channel) { | |
1015 | ERR("Kernel consumer discarded events channel %" | |
1016 | PRIu64 " not found", key); | |
66ab32be | 1017 | count = 0; |
e5742757 | 1018 | } else { |
66ab32be | 1019 | count = channel->discarded_events; |
fb83fe64 JD |
1020 | } |
1021 | ||
fb83fe64 JD |
1022 | health_code_update(); |
1023 | ||
1024 | /* Send back returned value to session daemon */ | |
66ab32be | 1025 | ret = lttcomm_send_unix_sock(sock, &count, sizeof(count)); |
fb83fe64 JD |
1026 | if (ret < 0) { |
1027 | PERROR("send discarded events"); | |
1028 | goto error_fatal; | |
1029 | } | |
1030 | ||
1031 | break; | |
1032 | } | |
1033 | case LTTNG_CONSUMER_LOST_PACKETS: | |
1034 | { | |
66ab32be JD |
1035 | ssize_t ret; |
1036 | uint64_t count; | |
fb83fe64 JD |
1037 | struct lttng_consumer_channel *channel; |
1038 | uint64_t id = msg.u.lost_packets.session_id; | |
1039 | uint64_t key = msg.u.lost_packets.channel_key; | |
1040 | ||
e5742757 MD |
1041 | DBG("Kernel consumer lost packets command for session id %" |
1042 | PRIu64 ", channel key %" PRIu64, id, key); | |
1043 | ||
fb83fe64 JD |
1044 | channel = consumer_find_channel(key); |
1045 | if (!channel) { | |
1046 | ERR("Kernel consumer lost packets channel %" | |
1047 | PRIu64 " not found", key); | |
66ab32be | 1048 | count = 0; |
e5742757 | 1049 | } else { |
66ab32be | 1050 | count = channel->lost_packets; |
fb83fe64 JD |
1051 | } |
1052 | ||
fb83fe64 JD |
1053 | health_code_update(); |
1054 | ||
1055 | /* Send back returned value to session daemon */ | |
66ab32be | 1056 | ret = lttcomm_send_unix_sock(sock, &count, sizeof(count)); |
fb83fe64 JD |
1057 | if (ret < 0) { |
1058 | PERROR("send lost packets"); | |
1059 | goto error_fatal; | |
1060 | } | |
1061 | ||
1062 | break; | |
1063 | } | |
b3530820 JG |
1064 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
1065 | { | |
1066 | int channel_monitor_pipe; | |
1067 | ||
1068 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1069 | /* Successfully received the command's type. */ | |
1070 | ret = consumer_send_status_msg(sock, ret_code); | |
1071 | if (ret < 0) { | |
1072 | goto error_fatal; | |
1073 | } | |
1074 | ||
1075 | ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe, | |
1076 | 1); | |
1077 | if (ret != sizeof(channel_monitor_pipe)) { | |
1078 | ERR("Failed to receive channel monitor pipe"); | |
1079 | goto error_fatal; | |
1080 | } | |
1081 | ||
1082 | DBG("Received channel monitor pipe (%d)", channel_monitor_pipe); | |
1083 | ret = consumer_timer_thread_set_channel_monitor_pipe( | |
1084 | channel_monitor_pipe); | |
1085 | if (!ret) { | |
1086 | int flags; | |
1087 | ||
1088 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1089 | /* Set the pipe as non-blocking. */ | |
1090 | ret = fcntl(channel_monitor_pipe, F_GETFL, 0); | |
1091 | if (ret == -1) { | |
1092 | PERROR("fcntl get flags of the channel monitoring pipe"); | |
1093 | goto error_fatal; | |
1094 | } | |
1095 | flags = ret; | |
1096 | ||
1097 | ret = fcntl(channel_monitor_pipe, F_SETFL, | |
1098 | flags | O_NONBLOCK); | |
1099 | if (ret == -1) { | |
1100 | PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe"); | |
1101 | goto error_fatal; | |
1102 | } | |
1103 | DBG("Channel monitor pipe set as non-blocking"); | |
1104 | } else { | |
1105 | ret_code = LTTCOMM_CONSUMERD_ALREADY_SET; | |
1106 | } | |
1107 | ret = consumer_send_status_msg(sock, ret_code); | |
1108 | if (ret < 0) { | |
1109 | goto error_fatal; | |
1110 | } | |
1111 | break; | |
1112 | } | |
b99a8d42 JD |
1113 | case LTTNG_CONSUMER_ROTATE_CHANNEL: |
1114 | { | |
92b7a7f8 MD |
1115 | struct lttng_consumer_channel *channel; |
1116 | uint64_t key = msg.u.rotate_channel.key; | |
b99a8d42 | 1117 | |
92b7a7f8 | 1118 | DBG("Consumer rotate channel %" PRIu64, key); |
b99a8d42 | 1119 | |
92b7a7f8 MD |
1120 | channel = consumer_find_channel(key); |
1121 | if (!channel) { | |
1122 | ERR("Channel %" PRIu64 " not found", key); | |
1123 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; | |
1124 | } else { | |
1125 | /* | |
1126 | * Sample the rotate position of all the streams in this channel. | |
1127 | */ | |
1128 | ret = lttng_consumer_rotate_channel(channel, key, | |
92b7a7f8 MD |
1129 | msg.u.rotate_channel.relayd_id, |
1130 | msg.u.rotate_channel.metadata, | |
92b7a7f8 MD |
1131 | ctx); |
1132 | if (ret < 0) { | |
1133 | ERR("Rotate channel failed"); | |
1134 | ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL; | |
1135 | } | |
b99a8d42 | 1136 | |
92b7a7f8 MD |
1137 | health_code_update(); |
1138 | } | |
b99a8d42 JD |
1139 | ret = consumer_send_status_msg(sock, ret_code); |
1140 | if (ret < 0) { | |
1141 | /* Somehow, the session daemon is not responding anymore. */ | |
1142 | goto end_nosignal; | |
1143 | } | |
92b7a7f8 MD |
1144 | if (channel) { |
1145 | /* Rotate the streams that are ready right now. */ | |
1146 | ret = lttng_consumer_rotate_ready_streams( | |
1147 | channel, key, ctx); | |
1148 | if (ret < 0) { | |
1149 | ERR("Rotate ready streams failed"); | |
1150 | } | |
b99a8d42 JD |
1151 | } |
1152 | ||
1153 | break; | |
1154 | } | |
d2956687 | 1155 | case LTTNG_CONSUMER_INIT: |
00fb02ac | 1156 | { |
d2956687 JG |
1157 | ret_code = lttng_consumer_init_command(ctx, |
1158 | msg.u.init.sessiond_uuid); | |
00fb02ac | 1159 | health_code_update(); |
00fb02ac JD |
1160 | ret = consumer_send_status_msg(sock, ret_code); |
1161 | if (ret < 0) { | |
1162 | /* Somehow, the session daemon is not responding anymore. */ | |
1163 | goto end_nosignal; | |
1164 | } | |
1165 | break; | |
1166 | } | |
d2956687 | 1167 | case LTTNG_CONSUMER_CREATE_TRACE_CHUNK: |
d88744a4 | 1168 | { |
d2956687 | 1169 | const struct lttng_credentials credentials = { |
e5add6d0 JG |
1170 | .uid = msg.u.create_trace_chunk.credentials.value.uid, |
1171 | .gid = msg.u.create_trace_chunk.credentials.value.gid, | |
d2956687 JG |
1172 | }; |
1173 | const bool is_local_trace = | |
1174 | !msg.u.create_trace_chunk.relayd_id.is_set; | |
1175 | const uint64_t relayd_id = | |
1176 | msg.u.create_trace_chunk.relayd_id.value; | |
1177 | const char *chunk_override_name = | |
1178 | *msg.u.create_trace_chunk.override_name ? | |
1179 | msg.u.create_trace_chunk.override_name : | |
1180 | NULL; | |
1181 | LTTNG_OPTIONAL(struct lttng_directory_handle) chunk_directory_handle = | |
1182 | LTTNG_OPTIONAL_INIT; | |
d88744a4 | 1183 | |
d2956687 JG |
1184 | /* |
1185 | * The session daemon will only provide a chunk directory file | |
1186 | * descriptor for local traces. | |
1187 | */ | |
1188 | if (is_local_trace) { | |
1189 | int chunk_dirfd; | |
19990ed5 | 1190 | |
d2956687 JG |
1191 | /* Acnowledge the reception of the command. */ |
1192 | ret = consumer_send_status_msg(sock, | |
1193 | LTTCOMM_CONSUMERD_SUCCESS); | |
1194 | if (ret < 0) { | |
1195 | /* Somehow, the session daemon is not responding anymore. */ | |
1196 | goto end_nosignal; | |
1197 | } | |
92816cc3 | 1198 | |
d2956687 JG |
1199 | ret = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1); |
1200 | if (ret != sizeof(chunk_dirfd)) { | |
1201 | ERR("Failed to receive trace chunk directory file descriptor"); | |
1202 | goto error_fatal; | |
1203 | } | |
92816cc3 | 1204 | |
d2956687 JG |
1205 | DBG("Received trace chunk directory fd (%d)", |
1206 | chunk_dirfd); | |
1207 | ret = lttng_directory_handle_init_from_dirfd( | |
1208 | &chunk_directory_handle.value, | |
1209 | chunk_dirfd); | |
1210 | if (ret) { | |
1211 | ERR("Failed to initialize chunk directory handle from directory file descriptor"); | |
1212 | if (close(chunk_dirfd)) { | |
1213 | PERROR("Failed to close chunk directory file descriptor"); | |
1214 | } | |
1215 | goto error_fatal; | |
1216 | } | |
1217 | chunk_directory_handle.is_set = true; | |
92816cc3 JG |
1218 | } |
1219 | ||
d2956687 JG |
1220 | ret_code = lttng_consumer_create_trace_chunk( |
1221 | !is_local_trace ? &relayd_id : NULL, | |
1222 | msg.u.create_trace_chunk.session_id, | |
1223 | msg.u.create_trace_chunk.chunk_id, | |
e5add6d0 JG |
1224 | (time_t) msg.u.create_trace_chunk |
1225 | .creation_timestamp, | |
d2956687 | 1226 | chunk_override_name, |
e5add6d0 JG |
1227 | msg.u.create_trace_chunk.credentials.is_set ? |
1228 | &credentials : | |
1229 | NULL, | |
d2956687 JG |
1230 | chunk_directory_handle.is_set ? |
1231 | &chunk_directory_handle.value : | |
1232 | NULL); | |
92816cc3 | 1233 | |
d2956687 JG |
1234 | if (chunk_directory_handle.is_set) { |
1235 | lttng_directory_handle_fini( | |
1236 | &chunk_directory_handle.value); | |
d88744a4 | 1237 | } |
d2956687 | 1238 | goto end_msg_sessiond; |
d88744a4 | 1239 | } |
d2956687 | 1240 | case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK: |
a1ae2ea5 | 1241 | { |
d2956687 JG |
1242 | const uint64_t relayd_id = |
1243 | msg.u.close_trace_chunk.relayd_id.value; | |
1244 | ||
1245 | ret_code = lttng_consumer_close_trace_chunk( | |
1246 | msg.u.close_trace_chunk.relayd_id.is_set ? | |
1247 | &relayd_id : NULL, | |
1248 | msg.u.close_trace_chunk.session_id, | |
1249 | msg.u.close_trace_chunk.chunk_id, | |
1250 | (time_t) msg.u.close_trace_chunk.close_timestamp); | |
1251 | goto end_msg_sessiond; | |
3654ed19 | 1252 | } |
d2956687 | 1253 | case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS: |
3654ed19 | 1254 | { |
d2956687 JG |
1255 | const uint64_t relayd_id = |
1256 | msg.u.trace_chunk_exists.relayd_id.value; | |
1257 | ||
1258 | ret_code = lttng_consumer_trace_chunk_exists( | |
1259 | msg.u.trace_chunk_exists.relayd_id.is_set ? | |
1260 | &relayd_id : NULL, | |
1261 | msg.u.trace_chunk_exists.session_id, | |
1262 | msg.u.trace_chunk_exists.chunk_id); | |
1263 | goto end_msg_sessiond; | |
a1ae2ea5 | 1264 | } |
3bd1e081 | 1265 | default: |
3f8e211f | 1266 | goto end_nosignal; |
3bd1e081 | 1267 | } |
3f8e211f | 1268 | |
3bd1e081 | 1269 | end_nosignal: |
b0b335c8 | 1270 | rcu_read_unlock(); |
4cbc1a04 DG |
1271 | |
1272 | /* | |
1273 | * Return 1 to indicate success since the 0 value can be a socket | |
1274 | * shutdown during the recv() or send() call. | |
1275 | */ | |
9ce5646a | 1276 | health_code_update(); |
4cbc1a04 | 1277 | return 1; |
1803a064 | 1278 | |
d2956687 JG |
1279 | end_msg_sessiond: |
1280 | /* | |
1281 | * The returned value here is not useful since either way we'll return 1 to | |
1282 | * the caller because the session daemon socket management is done | |
1283 | * elsewhere. Returning a negative code or 0 will shutdown the consumer. | |
1284 | */ | |
1285 | ret = consumer_send_status_msg(sock, ret_code); | |
1286 | if (ret < 0) { | |
1287 | goto error_fatal; | |
1288 | } | |
1289 | rcu_read_unlock(); | |
1290 | ||
1291 | health_code_update(); | |
1292 | ||
1293 | return 1; | |
1294 | ||
1803a064 MD |
1295 | error_fatal: |
1296 | rcu_read_unlock(); | |
1297 | /* This will issue a consumer stop. */ | |
1298 | return -1; | |
3bd1e081 | 1299 | } |
d41f73b7 | 1300 | |
309167d2 JD |
1301 | /* |
1302 | * Populate index values of a kernel stream. Values are set in big endian order. | |
1303 | * | |
1304 | * Return 0 on success or else a negative value. | |
1305 | */ | |
50adc264 | 1306 | static int get_index_values(struct ctf_packet_index *index, int infd) |
309167d2 JD |
1307 | { |
1308 | int ret; | |
1309 | ||
1310 | ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin); | |
1311 | if (ret < 0) { | |
1312 | PERROR("kernctl_get_timestamp_begin"); | |
1313 | goto error; | |
1314 | } | |
1315 | index->timestamp_begin = htobe64(index->timestamp_begin); | |
1316 | ||
1317 | ret = kernctl_get_timestamp_end(infd, &index->timestamp_end); | |
1318 | if (ret < 0) { | |
1319 | PERROR("kernctl_get_timestamp_end"); | |
1320 | goto error; | |
1321 | } | |
1322 | index->timestamp_end = htobe64(index->timestamp_end); | |
1323 | ||
1324 | ret = kernctl_get_events_discarded(infd, &index->events_discarded); | |
1325 | if (ret < 0) { | |
1326 | PERROR("kernctl_get_events_discarded"); | |
1327 | goto error; | |
1328 | } | |
1329 | index->events_discarded = htobe64(index->events_discarded); | |
1330 | ||
1331 | ret = kernctl_get_content_size(infd, &index->content_size); | |
1332 | if (ret < 0) { | |
1333 | PERROR("kernctl_get_content_size"); | |
1334 | goto error; | |
1335 | } | |
1336 | index->content_size = htobe64(index->content_size); | |
1337 | ||
1338 | ret = kernctl_get_packet_size(infd, &index->packet_size); | |
1339 | if (ret < 0) { | |
1340 | PERROR("kernctl_get_packet_size"); | |
1341 | goto error; | |
1342 | } | |
1343 | index->packet_size = htobe64(index->packet_size); | |
1344 | ||
1345 | ret = kernctl_get_stream_id(infd, &index->stream_id); | |
1346 | if (ret < 0) { | |
1347 | PERROR("kernctl_get_stream_id"); | |
1348 | goto error; | |
1349 | } | |
1350 | index->stream_id = htobe64(index->stream_id); | |
1351 | ||
234cd636 JD |
1352 | ret = kernctl_get_instance_id(infd, &index->stream_instance_id); |
1353 | if (ret < 0) { | |
f0b03c22 MD |
1354 | if (ret == -ENOTTY) { |
1355 | /* Command not implemented by lttng-modules. */ | |
1356 | index->stream_instance_id = -1ULL; | |
f0b03c22 MD |
1357 | } else { |
1358 | PERROR("kernctl_get_instance_id"); | |
1359 | goto error; | |
1360 | } | |
234cd636 JD |
1361 | } |
1362 | index->stream_instance_id = htobe64(index->stream_instance_id); | |
1363 | ||
1364 | ret = kernctl_get_sequence_number(infd, &index->packet_seq_num); | |
1365 | if (ret < 0) { | |
f0b03c22 MD |
1366 | if (ret == -ENOTTY) { |
1367 | /* Command not implemented by lttng-modules. */ | |
1368 | index->packet_seq_num = -1ULL; | |
1369 | ret = 0; | |
1370 | } else { | |
1371 | PERROR("kernctl_get_sequence_number"); | |
1372 | goto error; | |
1373 | } | |
234cd636 JD |
1374 | } |
1375 | index->packet_seq_num = htobe64(index->packet_seq_num); | |
1376 | ||
309167d2 JD |
1377 | error: |
1378 | return ret; | |
1379 | } | |
94d49140 JD |
1380 | /* |
1381 | * Sync metadata meaning request them to the session daemon and snapshot to the | |
1382 | * metadata thread can consumer them. | |
1383 | * | |
1384 | * Metadata stream lock MUST be acquired. | |
1385 | * | |
1386 | * Return 0 if new metadatda is available, EAGAIN if the metadata stream | |
1387 | * is empty or a negative value on error. | |
1388 | */ | |
1389 | int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata) | |
1390 | { | |
1391 | int ret; | |
1392 | ||
1393 | assert(metadata); | |
1394 | ||
1395 | ret = kernctl_buffer_flush(metadata->wait_fd); | |
1396 | if (ret < 0) { | |
1397 | ERR("Failed to flush kernel stream"); | |
1398 | goto end; | |
1399 | } | |
1400 | ||
1401 | ret = kernctl_snapshot(metadata->wait_fd); | |
1402 | if (ret < 0) { | |
32af2c95 | 1403 | if (ret != -EAGAIN) { |
94d49140 JD |
1404 | ERR("Sync metadata, taking kernel snapshot failed."); |
1405 | goto end; | |
1406 | } | |
1407 | DBG("Sync metadata, no new kernel metadata"); | |
1408 | /* No new metadata, exit. */ | |
1409 | ret = ENODATA; | |
1410 | goto end; | |
1411 | } | |
1412 | ||
1413 | end: | |
1414 | return ret; | |
1415 | } | |
309167d2 | 1416 | |
fb83fe64 JD |
1417 | static |
1418 | int update_stream_stats(struct lttng_consumer_stream *stream) | |
1419 | { | |
1420 | int ret; | |
1421 | uint64_t seq, discarded; | |
1422 | ||
1423 | ret = kernctl_get_sequence_number(stream->wait_fd, &seq); | |
1424 | if (ret < 0) { | |
f0b03c22 MD |
1425 | if (ret == -ENOTTY) { |
1426 | /* Command not implemented by lttng-modules. */ | |
1427 | seq = -1ULL; | |
f0b03c22 MD |
1428 | } else { |
1429 | PERROR("kernctl_get_sequence_number"); | |
1430 | goto end; | |
1431 | } | |
fb83fe64 JD |
1432 | } |
1433 | ||
1434 | /* | |
1435 | * Start the sequence when we extract the first packet in case we don't | |
1436 | * start at 0 (for example if a consumer is not connected to the | |
1437 | * session immediately after the beginning). | |
1438 | */ | |
1439 | if (stream->last_sequence_number == -1ULL) { | |
1440 | stream->last_sequence_number = seq; | |
1441 | } else if (seq > stream->last_sequence_number) { | |
1442 | stream->chan->lost_packets += seq - | |
1443 | stream->last_sequence_number - 1; | |
1444 | } else { | |
1445 | /* seq <= last_sequence_number */ | |
1446 | ERR("Sequence number inconsistent : prev = %" PRIu64 | |
1447 | ", current = %" PRIu64, | |
1448 | stream->last_sequence_number, seq); | |
1449 | ret = -1; | |
1450 | goto end; | |
1451 | } | |
1452 | stream->last_sequence_number = seq; | |
1453 | ||
1454 | ret = kernctl_get_events_discarded(stream->wait_fd, &discarded); | |
1455 | if (ret < 0) { | |
1456 | PERROR("kernctl_get_events_discarded"); | |
1457 | goto end; | |
1458 | } | |
1459 | if (discarded < stream->last_discarded_events) { | |
1460 | /* | |
83f4233d MJ |
1461 | * Overflow has occurred. We assume only one wrap-around |
1462 | * has occurred. | |
fb83fe64 JD |
1463 | */ |
1464 | stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) - | |
1465 | stream->last_discarded_events + discarded; | |
1466 | } else { | |
1467 | stream->chan->discarded_events += discarded - | |
1468 | stream->last_discarded_events; | |
1469 | } | |
1470 | stream->last_discarded_events = discarded; | |
1471 | ret = 0; | |
1472 | ||
1473 | end: | |
1474 | return ret; | |
1475 | } | |
1476 | ||
93ec662e JD |
1477 | /* |
1478 | * Check if the local version of the metadata stream matches with the version | |
1479 | * of the metadata stream in the kernel. If it was updated, set the reset flag | |
1480 | * on the stream. | |
1481 | */ | |
1482 | static | |
1483 | int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream) | |
1484 | { | |
1485 | int ret; | |
1486 | uint64_t cur_version; | |
1487 | ||
1488 | ret = kernctl_get_metadata_version(infd, &cur_version); | |
1489 | if (ret < 0) { | |
f0b03c22 MD |
1490 | if (ret == -ENOTTY) { |
1491 | /* | |
1492 | * LTTng-modules does not implement this | |
1493 | * command. | |
1494 | */ | |
1495 | ret = 0; | |
1496 | goto end; | |
1497 | } | |
93ec662e JD |
1498 | ERR("Failed to get the metadata version"); |
1499 | goto end; | |
1500 | } | |
1501 | ||
1502 | if (stream->metadata_version == cur_version) { | |
1503 | ret = 0; | |
1504 | goto end; | |
1505 | } | |
1506 | ||
1507 | DBG("New metadata version detected"); | |
1508 | stream->metadata_version = cur_version; | |
1509 | stream->reset_metadata_flag = 1; | |
1510 | ret = 0; | |
1511 | ||
1512 | end: | |
1513 | return ret; | |
1514 | } | |
1515 | ||
d41f73b7 MD |
1516 | /* |
1517 | * Consume data on a file descriptor and write it on a trace file. | |
d2956687 | 1518 | * The stream and channel locks must be held by the caller. |
d41f73b7 | 1519 | */ |
4078b776 | 1520 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d2956687 | 1521 | struct lttng_consumer_local_data *ctx) |
d41f73b7 | 1522 | { |
1d4dfdef | 1523 | unsigned long len, subbuf_size, padding; |
02d02e31 | 1524 | int err, write_index = 1, rotation_ret; |
4078b776 | 1525 | ssize_t ret = 0; |
d41f73b7 | 1526 | int infd = stream->wait_fd; |
50adc264 | 1527 | struct ctf_packet_index index; |
d41f73b7 MD |
1528 | |
1529 | DBG("In read_subbuffer (infd : %d)", infd); | |
309167d2 | 1530 | |
02d02e31 JD |
1531 | /* |
1532 | * If the stream was flagged to be ready for rotation before we extract the | |
1533 | * next packet, rotate it now. | |
1534 | */ | |
1535 | if (stream->rotate_ready) { | |
1536 | DBG("Rotate stream before extracting data"); | |
d2956687 | 1537 | rotation_ret = lttng_consumer_rotate_stream(ctx, stream); |
02d02e31 JD |
1538 | if (rotation_ret < 0) { |
1539 | ERR("Stream rotation error"); | |
1540 | ret = -1; | |
1541 | goto error; | |
1542 | } | |
1543 | } | |
1544 | ||
d41f73b7 MD |
1545 | /* Get the next subbuffer */ |
1546 | err = kernctl_get_next_subbuf(infd); | |
1547 | if (err != 0) { | |
d41f73b7 MD |
1548 | /* |
1549 | * This is a debug message even for single-threaded consumer, | |
1550 | * because poll() have more relaxed criterions than get subbuf, | |
1551 | * so get_subbuf may fail for short race windows where poll() | |
1552 | * would issue wakeups. | |
1553 | */ | |
1554 | DBG("Reserving sub buffer failed (everything is normal, " | |
1555 | "it is due to concurrency)"); | |
32af2c95 | 1556 | ret = err; |
02d02e31 | 1557 | goto error; |
d41f73b7 MD |
1558 | } |
1559 | ||
1d4dfdef DG |
1560 | /* Get the full subbuffer size including padding */ |
1561 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
1562 | if (err != 0) { | |
5a510c9f | 1563 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1564 | err = kernctl_put_subbuf(infd); |
1565 | if (err != 0) { | |
32af2c95 | 1566 | if (err == -EFAULT) { |
5a510c9f | 1567 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1568 | } else if (err == -EIO) { |
8265f19e | 1569 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1570 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1571 | } |
32af2c95 | 1572 | ret = err; |
02d02e31 | 1573 | goto error; |
8265f19e | 1574 | } |
32af2c95 | 1575 | ret = err; |
02d02e31 | 1576 | goto error; |
1d4dfdef DG |
1577 | } |
1578 | ||
1c20f0e2 | 1579 | if (!stream->metadata_flag) { |
309167d2 JD |
1580 | ret = get_index_values(&index, infd); |
1581 | if (ret < 0) { | |
8265f19e MD |
1582 | err = kernctl_put_subbuf(infd); |
1583 | if (err != 0) { | |
32af2c95 | 1584 | if (err == -EFAULT) { |
5a510c9f | 1585 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1586 | } else if (err == -EIO) { |
8265f19e | 1587 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1588 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1589 | } |
32af2c95 | 1590 | ret = err; |
02d02e31 | 1591 | goto error; |
8265f19e | 1592 | } |
02d02e31 | 1593 | goto error; |
309167d2 | 1594 | } |
fb83fe64 JD |
1595 | ret = update_stream_stats(stream); |
1596 | if (ret < 0) { | |
7b87473d MD |
1597 | err = kernctl_put_subbuf(infd); |
1598 | if (err != 0) { | |
1599 | if (err == -EFAULT) { | |
1600 | PERROR("Error in unreserving sub buffer\n"); | |
1601 | } else if (err == -EIO) { | |
1602 | /* Should never happen with newer LTTng versions */ | |
1603 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
1604 | } | |
1605 | ret = err; | |
02d02e31 | 1606 | goto error; |
7b87473d | 1607 | } |
02d02e31 | 1608 | goto error; |
fb83fe64 | 1609 | } |
1c20f0e2 JD |
1610 | } else { |
1611 | write_index = 0; | |
93ec662e JD |
1612 | ret = metadata_stream_check_version(infd, stream); |
1613 | if (ret < 0) { | |
7b87473d MD |
1614 | err = kernctl_put_subbuf(infd); |
1615 | if (err != 0) { | |
1616 | if (err == -EFAULT) { | |
1617 | PERROR("Error in unreserving sub buffer\n"); | |
1618 | } else if (err == -EIO) { | |
1619 | /* Should never happen with newer LTTng versions */ | |
1620 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
1621 | } | |
1622 | ret = err; | |
02d02e31 | 1623 | goto error; |
7b87473d | 1624 | } |
02d02e31 | 1625 | goto error; |
93ec662e | 1626 | } |
309167d2 JD |
1627 | } |
1628 | ||
ffe60014 | 1629 | switch (stream->chan->output) { |
07b86b52 | 1630 | case CONSUMER_CHANNEL_SPLICE: |
1d4dfdef DG |
1631 | /* |
1632 | * XXX: The lttng-modules splice "actor" does not handle copying | |
1633 | * partial pages hence only using the subbuffer size without the | |
1634 | * padding makes the splice fail. | |
1635 | */ | |
1636 | subbuf_size = len; | |
1637 | padding = 0; | |
1638 | ||
1639 | /* splice the subbuffer to the tracefile */ | |
91dfef6e | 1640 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
309167d2 | 1641 | padding, &index); |
91dfef6e DG |
1642 | /* |
1643 | * XXX: Splice does not support network streaming so the return value | |
1644 | * is simply checked against subbuf_size and not like the mmap() op. | |
1645 | */ | |
1d4dfdef DG |
1646 | if (ret != subbuf_size) { |
1647 | /* | |
1648 | * display the error but continue processing to try | |
1649 | * to release the subbuffer | |
1650 | */ | |
1651 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
1652 | ret, subbuf_size); | |
309167d2 | 1653 | write_index = 0; |
1d4dfdef DG |
1654 | } |
1655 | break; | |
07b86b52 | 1656 | case CONSUMER_CHANNEL_MMAP: |
1d4dfdef DG |
1657 | /* Get subbuffer size without padding */ |
1658 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
1659 | if (err != 0) { | |
5a510c9f | 1660 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1661 | err = kernctl_put_subbuf(infd); |
1662 | if (err != 0) { | |
32af2c95 | 1663 | if (err == -EFAULT) { |
5a510c9f | 1664 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1665 | } else if (err == -EIO) { |
8265f19e | 1666 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1667 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1668 | } |
32af2c95 | 1669 | ret = err; |
02d02e31 | 1670 | goto error; |
8265f19e | 1671 | } |
32af2c95 | 1672 | ret = err; |
02d02e31 | 1673 | goto error; |
1d4dfdef | 1674 | } |
47e81c02 | 1675 | |
1d4dfdef DG |
1676 | /* Make sure the tracer is not gone mad on us! */ |
1677 | assert(len >= subbuf_size); | |
1678 | ||
1679 | padding = len - subbuf_size; | |
1680 | ||
1681 | /* write the subbuffer to the tracefile */ | |
91dfef6e | 1682 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
309167d2 | 1683 | padding, &index); |
91dfef6e DG |
1684 | /* |
1685 | * The mmap operation should write subbuf_size amount of data when | |
1686 | * network streaming or the full padding (len) size when we are _not_ | |
1687 | * streaming. | |
1688 | */ | |
d88aee68 DG |
1689 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1690 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 1691 | /* |
91dfef6e | 1692 | * Display the error but continue processing to try to release the |
2336629e DG |
1693 | * subbuffer. This is a DBG statement since this is possible to |
1694 | * happen without being a critical error. | |
1d4dfdef | 1695 | */ |
2336629e | 1696 | DBG("Error writing to tracefile " |
91dfef6e DG |
1697 | "(ret: %zd != len: %lu != subbuf_size: %lu)", |
1698 | ret, len, subbuf_size); | |
309167d2 | 1699 | write_index = 0; |
1d4dfdef DG |
1700 | } |
1701 | break; | |
1702 | default: | |
1703 | ERR("Unknown output method"); | |
56591bac | 1704 | ret = -EPERM; |
d41f73b7 MD |
1705 | } |
1706 | ||
1707 | err = kernctl_put_next_subbuf(infd); | |
1708 | if (err != 0) { | |
32af2c95 | 1709 | if (err == -EFAULT) { |
5a510c9f | 1710 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1711 | } else if (err == -EIO) { |
d41f73b7 | 1712 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1713 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
d41f73b7 | 1714 | } |
32af2c95 | 1715 | ret = err; |
02d02e31 | 1716 | goto error; |
d41f73b7 MD |
1717 | } |
1718 | ||
309167d2 | 1719 | /* Write index if needed. */ |
1c20f0e2 | 1720 | if (!write_index) { |
02d02e31 | 1721 | goto rotate; |
1c20f0e2 JD |
1722 | } |
1723 | ||
94d49140 JD |
1724 | if (stream->chan->live_timer_interval && !stream->metadata_flag) { |
1725 | /* | |
1726 | * In live, block until all the metadata is sent. | |
1727 | */ | |
c585821b MD |
1728 | pthread_mutex_lock(&stream->metadata_timer_lock); |
1729 | assert(!stream->missed_metadata_flush); | |
1730 | stream->waiting_on_metadata = true; | |
1731 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1732 | ||
94d49140 | 1733 | err = consumer_stream_sync_metadata(ctx, stream->session_id); |
c585821b MD |
1734 | |
1735 | pthread_mutex_lock(&stream->metadata_timer_lock); | |
1736 | stream->waiting_on_metadata = false; | |
1737 | if (stream->missed_metadata_flush) { | |
1738 | stream->missed_metadata_flush = false; | |
1739 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1740 | (void) consumer_flush_kernel_index(stream); | |
1741 | } else { | |
1742 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1743 | } | |
94d49140 | 1744 | if (err < 0) { |
02d02e31 | 1745 | goto error; |
94d49140 JD |
1746 | } |
1747 | } | |
1748 | ||
1c20f0e2 JD |
1749 | err = consumer_stream_write_index(stream, &index); |
1750 | if (err < 0) { | |
02d02e31 | 1751 | goto error; |
309167d2 JD |
1752 | } |
1753 | ||
02d02e31 JD |
1754 | rotate: |
1755 | /* | |
1756 | * After extracting the packet, we check if the stream is now ready to be | |
1757 | * rotated and perform the action immediately. | |
1758 | */ | |
1759 | rotation_ret = lttng_consumer_stream_is_rotate_ready(stream); | |
1760 | if (rotation_ret == 1) { | |
d2956687 | 1761 | rotation_ret = lttng_consumer_rotate_stream(ctx, stream); |
02d02e31 JD |
1762 | if (rotation_ret < 0) { |
1763 | ERR("Stream rotation error"); | |
1764 | ret = -1; | |
1765 | goto error; | |
1766 | } | |
1767 | } else if (rotation_ret < 0) { | |
1768 | ERR("Checking if stream is ready to rotate"); | |
1769 | ret = -1; | |
1770 | goto error; | |
1771 | } | |
1772 | ||
1773 | error: | |
d41f73b7 MD |
1774 | return ret; |
1775 | } | |
1776 | ||
1777 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
1778 | { | |
1779 | int ret; | |
ffe60014 DG |
1780 | |
1781 | assert(stream); | |
1782 | ||
2bba9e53 | 1783 | /* |
d2956687 JG |
1784 | * Don't create anything if this is set for streaming or if there is |
1785 | * no current trace chunk on the parent channel. | |
2bba9e53 | 1786 | */ |
d2956687 JG |
1787 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor && |
1788 | stream->chan->trace_chunk) { | |
1789 | ret = consumer_stream_create_output_files(stream, true); | |
1790 | if (ret) { | |
fe4477ee JD |
1791 | goto error; |
1792 | } | |
ffe60014 | 1793 | } |
d41f73b7 | 1794 | |
d41f73b7 MD |
1795 | if (stream->output == LTTNG_EVENT_MMAP) { |
1796 | /* get the len of the mmap region */ | |
1797 | unsigned long mmap_len; | |
1798 | ||
1799 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
1800 | if (ret != 0) { | |
ffe60014 | 1801 | PERROR("kernctl_get_mmap_len"); |
d41f73b7 MD |
1802 | goto error_close_fd; |
1803 | } | |
1804 | stream->mmap_len = (size_t) mmap_len; | |
1805 | ||
ffe60014 DG |
1806 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
1807 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 1808 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 1809 | PERROR("Error mmaping"); |
d41f73b7 MD |
1810 | ret = -1; |
1811 | goto error_close_fd; | |
1812 | } | |
1813 | } | |
1814 | ||
1815 | /* we return 0 to let the library handle the FD internally */ | |
1816 | return 0; | |
1817 | ||
1818 | error_close_fd: | |
2f225ce2 | 1819 | if (stream->out_fd >= 0) { |
d41f73b7 MD |
1820 | int err; |
1821 | ||
1822 | err = close(stream->out_fd); | |
1823 | assert(!err); | |
2f225ce2 | 1824 | stream->out_fd = -1; |
d41f73b7 MD |
1825 | } |
1826 | error: | |
1827 | return ret; | |
1828 | } | |
1829 | ||
ca22feea DG |
1830 | /* |
1831 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1832 | * stream. Consumer data lock MUST be acquired before calling this function |
1833 | * and the stream lock. | |
ca22feea | 1834 | * |
6d805429 | 1835 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1836 | * data is available for trace viewer reading. |
1837 | */ | |
6d805429 | 1838 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1839 | { |
1840 | int ret; | |
1841 | ||
1842 | assert(stream); | |
1843 | ||
873b9e9a MD |
1844 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1845 | ret = 0; | |
1846 | goto end; | |
1847 | } | |
1848 | ||
ca22feea DG |
1849 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
1850 | if (ret == 0) { | |
1851 | /* There is still data so let's put back this subbuffer. */ | |
1852 | ret = kernctl_put_subbuf(stream->wait_fd); | |
1853 | assert(ret == 0); | |
6d805429 | 1854 | ret = 1; /* Data is pending */ |
4e9a4686 | 1855 | goto end; |
ca22feea DG |
1856 | } |
1857 | ||
6d805429 DG |
1858 | /* Data is NOT pending and ready to be read. */ |
1859 | ret = 0; | |
ca22feea | 1860 | |
6efae65e DG |
1861 | end: |
1862 | return ret; | |
ca22feea | 1863 | } |