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