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