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