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