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