Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
b3530820 | 4 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3bd1e081 | 5 | * |
d14d33bf AM |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
3bd1e081 MD |
9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
d14d33bf AM |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
3bd1e081 MD |
18 | */ |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
3bd1e081 | 21 | #include <assert.h> |
3bd1e081 MD |
22 | #include <poll.h> |
23 | #include <pthread.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <sys/mman.h> | |
27 | #include <sys/socket.h> | |
28 | #include <sys/types.h> | |
77c7c900 | 29 | #include <inttypes.h> |
3bd1e081 | 30 | #include <unistd.h> |
dbb5dfe6 | 31 | #include <sys/stat.h> |
3bd1e081 | 32 | |
51a9e1c7 | 33 | #include <bin/lttng-consumerd/health-consumerd.h> |
990570ed | 34 | #include <common/common.h> |
10a8a223 | 35 | #include <common/kernel-ctl/kernel-ctl.h> |
10a8a223 | 36 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 37 | #include <common/sessiond-comm/relayd.h> |
dbb5dfe6 | 38 | #include <common/compat/fcntl.h> |
f263b7fd | 39 | #include <common/compat/endian.h> |
acdb9057 | 40 | #include <common/pipe.h> |
00e2e675 | 41 | #include <common/relayd/relayd.h> |
fe4477ee | 42 | #include <common/utils.h> |
c8fea79c | 43 | #include <common/consumer/consumer-stream.h> |
309167d2 | 44 | #include <common/index/index.h> |
c8fea79c | 45 | #include <common/consumer/consumer-timer.h> |
0857097f | 46 | |
10a8a223 | 47 | #include "kernel-consumer.h" |
3bd1e081 MD |
48 | |
49 | extern struct lttng_consumer_global_data consumer_data; | |
50 | extern int consumer_poll_timeout; | |
51 | extern volatile int consumer_quit; | |
52 | ||
3bd1e081 MD |
53 | /* |
54 | * Take a snapshot for a specific fd | |
55 | * | |
56 | * Returns 0 on success, < 0 on error | |
57 | */ | |
ffe60014 | 58 | int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream) |
3bd1e081 MD |
59 | { |
60 | int ret = 0; | |
61 | int infd = stream->wait_fd; | |
62 | ||
63 | ret = kernctl_snapshot(infd); | |
64 | if (ret != 0) { | |
5a510c9f | 65 | PERROR("Getting sub-buffer snapshot."); |
3bd1e081 MD |
66 | } |
67 | ||
68 | return ret; | |
69 | } | |
70 | ||
e9404c27 JG |
71 | /* |
72 | * Sample consumed and produced positions for a specific fd. | |
73 | * | |
74 | * Returns 0 on success, < 0 on error. | |
75 | */ | |
76 | int lttng_kconsumer_sample_snapshot_positions( | |
77 | struct lttng_consumer_stream *stream) | |
78 | { | |
79 | assert(stream); | |
80 | ||
81 | return kernctl_snapshot_sample_positions(stream->wait_fd); | |
82 | } | |
83 | ||
3bd1e081 MD |
84 | /* |
85 | * Get the produced position | |
86 | * | |
87 | * Returns 0 on success, < 0 on error | |
88 | */ | |
ffe60014 | 89 | int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream, |
3bd1e081 MD |
90 | unsigned long *pos) |
91 | { | |
92 | int ret; | |
93 | int infd = stream->wait_fd; | |
94 | ||
95 | ret = kernctl_snapshot_get_produced(infd, pos); | |
96 | if (ret != 0) { | |
5a510c9f | 97 | PERROR("kernctl_snapshot_get_produced"); |
3bd1e081 MD |
98 | } |
99 | ||
100 | return ret; | |
101 | } | |
102 | ||
07b86b52 JD |
103 | /* |
104 | * Get the consumerd position | |
105 | * | |
106 | * Returns 0 on success, < 0 on error | |
107 | */ | |
108 | int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream, | |
109 | unsigned long *pos) | |
110 | { | |
111 | int ret; | |
112 | int infd = stream->wait_fd; | |
113 | ||
114 | ret = kernctl_snapshot_get_consumed(infd, pos); | |
115 | if (ret != 0) { | |
5a510c9f | 116 | PERROR("kernctl_snapshot_get_consumed"); |
07b86b52 JD |
117 | } |
118 | ||
119 | return ret; | |
120 | } | |
121 | ||
07b86b52 JD |
122 | /* |
123 | * Take a snapshot of all the stream of a channel | |
124 | * | |
125 | * Returns 0 on success, < 0 on error | |
126 | */ | |
127 | int lttng_kconsumer_snapshot_channel(uint64_t key, char *path, | |
d07ceecd | 128 | uint64_t relayd_id, uint64_t nb_packets_per_stream, |
5c786ded | 129 | struct lttng_consumer_local_data *ctx) |
07b86b52 JD |
130 | { |
131 | int ret; | |
07b86b52 JD |
132 | struct lttng_consumer_channel *channel; |
133 | struct lttng_consumer_stream *stream; | |
134 | ||
6a00837f | 135 | DBG("Kernel consumer snapshot channel %" PRIu64, key); |
07b86b52 JD |
136 | |
137 | rcu_read_lock(); | |
138 | ||
139 | channel = consumer_find_channel(key); | |
140 | if (!channel) { | |
6a00837f | 141 | ERR("No channel found for key %" PRIu64, key); |
07b86b52 JD |
142 | ret = -1; |
143 | goto end; | |
144 | } | |
145 | ||
146 | /* Splice is not supported yet for channel snapshot. */ | |
147 | if (channel->output != CONSUMER_CHANNEL_MMAP) { | |
148 | ERR("Unsupported output %d", channel->output); | |
149 | ret = -1; | |
150 | goto end; | |
151 | } | |
152 | ||
10a50311 | 153 | cds_list_for_each_entry(stream, &channel->streams.head, send_node) { |
c13082a9 JD |
154 | /* Are we at a position _before_ the first available packet ? */ |
155 | bool before_first_packet = true; | |
923333cd | 156 | unsigned long consumed_pos, produced_pos; |
9ce5646a MD |
157 | |
158 | health_code_update(); | |
159 | ||
07b86b52 JD |
160 | /* |
161 | * Lock stream because we are about to change its state. | |
162 | */ | |
163 | pthread_mutex_lock(&stream->lock); | |
164 | ||
29decac3 DG |
165 | /* |
166 | * Assign the received relayd ID so we can use it for streaming. The streams | |
167 | * are not visible to anyone so this is OK to change it. | |
168 | */ | |
07b86b52 JD |
169 | stream->net_seq_idx = relayd_id; |
170 | channel->relayd_id = relayd_id; | |
171 | if (relayd_id != (uint64_t) -1ULL) { | |
10a50311 | 172 | ret = consumer_send_relayd_stream(stream, path); |
07b86b52 JD |
173 | if (ret < 0) { |
174 | ERR("sending stream to relayd"); | |
175 | goto end_unlock; | |
176 | } | |
07b86b52 JD |
177 | } else { |
178 | ret = utils_create_stream_file(path, stream->name, | |
10a50311 JD |
179 | stream->chan->tracefile_size, |
180 | stream->tracefile_count_current, | |
309167d2 | 181 | stream->uid, stream->gid, NULL); |
07b86b52 JD |
182 | if (ret < 0) { |
183 | ERR("utils_create_stream_file"); | |
184 | goto end_unlock; | |
185 | } | |
186 | ||
187 | stream->out_fd = ret; | |
188 | stream->tracefile_size_current = 0; | |
189 | ||
81ea21bf MD |
190 | DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")", |
191 | path, stream->name, stream->key); | |
07b86b52 | 192 | } |
601262d6 JD |
193 | if (relayd_id != -1ULL) { |
194 | ret = consumer_send_relayd_streams_sent(relayd_id); | |
195 | if (ret < 0) { | |
196 | ERR("sending streams sent to relayd"); | |
197 | goto end_unlock; | |
198 | } | |
1bf914e0 | 199 | channel->streams_sent_to_relayd = true; |
a4baae1b | 200 | } |
07b86b52 JD |
201 | |
202 | ret = kernctl_buffer_flush(stream->wait_fd); | |
203 | if (ret < 0) { | |
10a50311 | 204 | ERR("Failed to flush kernel stream"); |
07b86b52 JD |
205 | goto end_unlock; |
206 | } | |
207 | ||
208 | ret = lttng_kconsumer_take_snapshot(stream); | |
209 | if (ret < 0) { | |
210 | ERR("Taking kernel snapshot"); | |
211 | goto end_unlock; | |
212 | } | |
213 | ||
214 | ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos); | |
215 | if (ret < 0) { | |
216 | ERR("Produced kernel snapshot position"); | |
217 | goto end_unlock; | |
218 | } | |
219 | ||
220 | ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos); | |
221 | if (ret < 0) { | |
222 | ERR("Consumerd kernel snapshot position"); | |
223 | goto end_unlock; | |
224 | } | |
225 | ||
226 | if (stream->max_sb_size == 0) { | |
227 | ret = kernctl_get_max_subbuf_size(stream->wait_fd, | |
228 | &stream->max_sb_size); | |
229 | if (ret < 0) { | |
230 | ERR("Getting kernel max_sb_size"); | |
231 | goto end_unlock; | |
232 | } | |
233 | } | |
234 | ||
d07ceecd MD |
235 | consumed_pos = consumer_get_consume_start_pos(consumed_pos, |
236 | produced_pos, nb_packets_per_stream, | |
237 | stream->max_sb_size); | |
5c786ded | 238 | |
07b86b52 JD |
239 | while (consumed_pos < produced_pos) { |
240 | ssize_t read_len; | |
241 | unsigned long len, padded_len; | |
c13082a9 | 242 | int lost_packet = 0; |
07b86b52 | 243 | |
9ce5646a MD |
244 | health_code_update(); |
245 | ||
07b86b52 JD |
246 | DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos); |
247 | ||
248 | ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos); | |
249 | if (ret < 0) { | |
32af2c95 | 250 | if (ret != -EAGAIN) { |
07b86b52 JD |
251 | PERROR("kernctl_get_subbuf snapshot"); |
252 | goto end_unlock; | |
253 | } | |
254 | DBG("Kernel consumer get subbuf failed. Skipping it."); | |
255 | consumed_pos += stream->max_sb_size; | |
c13082a9 JD |
256 | |
257 | /* | |
258 | * Start accounting lost packets only when we | |
259 | * already have extracted packets (to match the | |
260 | * content of the final snapshot). | |
261 | */ | |
262 | if (!before_first_packet) { | |
263 | lost_packet = 1; | |
264 | } | |
07b86b52 JD |
265 | continue; |
266 | } | |
267 | ||
268 | ret = kernctl_get_subbuf_size(stream->wait_fd, &len); | |
269 | if (ret < 0) { | |
270 | ERR("Snapshot kernctl_get_subbuf_size"); | |
29decac3 | 271 | goto error_put_subbuf; |
07b86b52 JD |
272 | } |
273 | ||
274 | ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len); | |
275 | if (ret < 0) { | |
276 | ERR("Snapshot kernctl_get_padded_subbuf_size"); | |
29decac3 | 277 | goto error_put_subbuf; |
07b86b52 JD |
278 | } |
279 | ||
280 | read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len, | |
309167d2 | 281 | padded_len - len, NULL); |
07b86b52 | 282 | /* |
29decac3 DG |
283 | * We write the padded len in local tracefiles but the data len |
284 | * when using a relay. Display the error but continue processing | |
285 | * to try to release the subbuffer. | |
07b86b52 JD |
286 | */ |
287 | if (relayd_id != (uint64_t) -1ULL) { | |
288 | if (read_len != len) { | |
289 | ERR("Error sending to the relay (ret: %zd != len: %lu)", | |
290 | read_len, len); | |
291 | } | |
292 | } else { | |
293 | if (read_len != padded_len) { | |
294 | ERR("Error writing to tracefile (ret: %zd != len: %lu)", | |
295 | read_len, padded_len); | |
296 | } | |
297 | } | |
298 | ||
299 | ret = kernctl_put_subbuf(stream->wait_fd); | |
300 | if (ret < 0) { | |
301 | ERR("Snapshot kernctl_put_subbuf"); | |
302 | goto end_unlock; | |
303 | } | |
304 | consumed_pos += stream->max_sb_size; | |
c13082a9 JD |
305 | |
306 | /* | |
307 | * Only account lost packets located between | |
308 | * succesfully extracted packets (do not account before | |
309 | * and after since they are not visible in the | |
310 | * resulting snapshot). | |
311 | */ | |
312 | stream->chan->lost_packets += lost_packet; | |
313 | lost_packet = 0; | |
314 | before_first_packet = false; | |
07b86b52 JD |
315 | } |
316 | ||
317 | if (relayd_id == (uint64_t) -1ULL) { | |
fdf9986c MD |
318 | if (stream->out_fd >= 0) { |
319 | ret = close(stream->out_fd); | |
320 | if (ret < 0) { | |
321 | PERROR("Kernel consumer snapshot close out_fd"); | |
322 | goto end_unlock; | |
323 | } | |
324 | stream->out_fd = -1; | |
07b86b52 | 325 | } |
07b86b52 JD |
326 | } else { |
327 | close_relayd_stream(stream); | |
328 | stream->net_seq_idx = (uint64_t) -1ULL; | |
329 | } | |
330 | pthread_mutex_unlock(&stream->lock); | |
331 | } | |
332 | ||
333 | /* All good! */ | |
334 | ret = 0; | |
335 | goto end; | |
336 | ||
29decac3 DG |
337 | error_put_subbuf: |
338 | ret = kernctl_put_subbuf(stream->wait_fd); | |
339 | if (ret < 0) { | |
340 | ERR("Snapshot kernctl_put_subbuf error path"); | |
341 | } | |
07b86b52 JD |
342 | end_unlock: |
343 | pthread_mutex_unlock(&stream->lock); | |
344 | end: | |
345 | rcu_read_unlock(); | |
346 | return ret; | |
347 | } | |
348 | ||
349 | /* | |
350 | * Read the whole metadata available for a snapshot. | |
351 | * | |
352 | * Returns 0 on success, < 0 on error | |
353 | */ | |
354 | int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path, | |
e2039c7a | 355 | uint64_t relayd_id, struct lttng_consumer_local_data *ctx) |
07b86b52 | 356 | { |
d771f832 DG |
357 | int ret, use_relayd = 0; |
358 | ssize_t ret_read; | |
07b86b52 JD |
359 | struct lttng_consumer_channel *metadata_channel; |
360 | struct lttng_consumer_stream *metadata_stream; | |
d771f832 DG |
361 | |
362 | assert(ctx); | |
07b86b52 JD |
363 | |
364 | DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s", | |
365 | key, path); | |
366 | ||
367 | rcu_read_lock(); | |
368 | ||
369 | metadata_channel = consumer_find_channel(key); | |
370 | if (!metadata_channel) { | |
d771f832 | 371 | ERR("Kernel snapshot metadata not found for key %" PRIu64, key); |
07b86b52 | 372 | ret = -1; |
d771f832 | 373 | goto error; |
07b86b52 JD |
374 | } |
375 | ||
376 | metadata_stream = metadata_channel->metadata_stream; | |
377 | assert(metadata_stream); | |
378 | ||
d771f832 | 379 | /* Flag once that we have a valid relayd for the stream. */ |
e2039c7a | 380 | if (relayd_id != (uint64_t) -1ULL) { |
d771f832 DG |
381 | use_relayd = 1; |
382 | } | |
383 | ||
384 | if (use_relayd) { | |
10a50311 | 385 | ret = consumer_send_relayd_stream(metadata_stream, path); |
e2039c7a | 386 | if (ret < 0) { |
d771f832 | 387 | goto error; |
e2039c7a | 388 | } |
e2039c7a JD |
389 | } else { |
390 | ret = utils_create_stream_file(path, metadata_stream->name, | |
391 | metadata_stream->chan->tracefile_size, | |
392 | metadata_stream->tracefile_count_current, | |
309167d2 | 393 | metadata_stream->uid, metadata_stream->gid, NULL); |
e2039c7a | 394 | if (ret < 0) { |
d771f832 | 395 | goto error; |
e2039c7a JD |
396 | } |
397 | metadata_stream->out_fd = ret; | |
07b86b52 | 398 | } |
07b86b52 | 399 | |
d771f832 | 400 | do { |
9ce5646a MD |
401 | health_code_update(); |
402 | ||
d771f832 DG |
403 | ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx); |
404 | if (ret_read < 0) { | |
56591bac | 405 | if (ret_read != -EAGAIN) { |
6a00837f | 406 | ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)", |
d771f832 DG |
407 | ret_read); |
408 | goto error; | |
07b86b52 | 409 | } |
d771f832 | 410 | /* ret_read is negative at this point so we will exit the loop. */ |
07b86b52 JD |
411 | continue; |
412 | } | |
d771f832 | 413 | } while (ret_read >= 0); |
07b86b52 | 414 | |
d771f832 DG |
415 | if (use_relayd) { |
416 | close_relayd_stream(metadata_stream); | |
417 | metadata_stream->net_seq_idx = (uint64_t) -1ULL; | |
418 | } else { | |
fdf9986c MD |
419 | if (metadata_stream->out_fd >= 0) { |
420 | ret = close(metadata_stream->out_fd); | |
421 | if (ret < 0) { | |
422 | PERROR("Kernel consumer snapshot metadata close out_fd"); | |
423 | /* | |
424 | * Don't go on error here since the snapshot was successful at this | |
425 | * point but somehow the close failed. | |
426 | */ | |
427 | } | |
428 | metadata_stream->out_fd = -1; | |
e2039c7a | 429 | } |
e2039c7a JD |
430 | } |
431 | ||
07b86b52 | 432 | ret = 0; |
d771f832 | 433 | |
cf53a8a6 JD |
434 | cds_list_del(&metadata_stream->send_node); |
435 | consumer_stream_destroy(metadata_stream, NULL); | |
436 | metadata_channel->metadata_stream = NULL; | |
d771f832 | 437 | error: |
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. */ |
7735ef9e DG |
479 | ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index, |
480 | msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll, | |
d3e2ba59 JD |
481 | &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id, |
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; |
3bd1e081 | 489 | |
9ce5646a MD |
490 | health_code_update(); |
491 | ||
f50f23d9 DG |
492 | /* First send a status message before receiving the fds. */ |
493 | ret = consumer_send_status_msg(sock, ret_code); | |
494 | if (ret < 0) { | |
495 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 496 | goto error_fatal; |
f50f23d9 | 497 | } |
9ce5646a MD |
498 | |
499 | health_code_update(); | |
500 | ||
d88aee68 | 501 | DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key); |
3bd1e081 | 502 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, |
ffe60014 DG |
503 | msg.u.channel.session_id, msg.u.channel.pathname, |
504 | msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid, | |
1624d5b7 JD |
505 | msg.u.channel.relayd_id, msg.u.channel.output, |
506 | msg.u.channel.tracefile_size, | |
1950109e | 507 | msg.u.channel.tracefile_count, 0, |
ecc48a90 | 508 | msg.u.channel.monitor, |
d7ba1388 | 509 | msg.u.channel.live_timer_interval, |
3d071855 | 510 | NULL, NULL); |
3bd1e081 | 511 | if (new_channel == NULL) { |
f73fabfd | 512 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); |
3bd1e081 MD |
513 | goto end_nosignal; |
514 | } | |
ffe60014 | 515 | new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams; |
95a1109b JD |
516 | switch (msg.u.channel.output) { |
517 | case LTTNG_EVENT_SPLICE: | |
518 | new_channel->output = CONSUMER_CHANNEL_SPLICE; | |
519 | break; | |
520 | case LTTNG_EVENT_MMAP: | |
521 | new_channel->output = CONSUMER_CHANNEL_MMAP; | |
522 | break; | |
523 | default: | |
524 | ERR("Channel output unknown %d", msg.u.channel.output); | |
525 | goto end_nosignal; | |
526 | } | |
ffe60014 DG |
527 | |
528 | /* Translate and save channel type. */ | |
529 | switch (msg.u.channel.type) { | |
530 | case CONSUMER_CHANNEL_TYPE_DATA: | |
531 | case CONSUMER_CHANNEL_TYPE_METADATA: | |
532 | new_channel->type = msg.u.channel.type; | |
533 | break; | |
534 | default: | |
535 | assert(0); | |
536 | goto end_nosignal; | |
537 | }; | |
538 | ||
9ce5646a MD |
539 | health_code_update(); |
540 | ||
3bd1e081 | 541 | if (ctx->on_recv_channel != NULL) { |
e43c41c5 JD |
542 | ret_recv = ctx->on_recv_channel(new_channel); |
543 | if (ret_recv == 0) { | |
544 | ret = consumer_add_channel(new_channel, ctx); | |
545 | } else if (ret_recv < 0) { | |
3bd1e081 MD |
546 | goto end_nosignal; |
547 | } | |
548 | } else { | |
e43c41c5 | 549 | ret = consumer_add_channel(new_channel, ctx); |
3bd1e081 | 550 | } |
e9404c27 JG |
551 | if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) { |
552 | int monitor_start_ret; | |
553 | ||
554 | DBG("Consumer starting monitor timer"); | |
94d49140 JD |
555 | consumer_timer_live_start(new_channel, |
556 | msg.u.channel.live_timer_interval); | |
e9404c27 JG |
557 | monitor_start_ret = consumer_timer_monitor_start( |
558 | new_channel, | |
559 | msg.u.channel.monitor_timer_interval); | |
560 | if (monitor_start_ret < 0) { | |
561 | ERR("Starting channel monitoring timer failed"); | |
562 | goto end_nosignal; | |
563 | } | |
564 | ||
94d49140 | 565 | } |
e43c41c5 | 566 | |
9ce5646a MD |
567 | health_code_update(); |
568 | ||
e43c41c5 | 569 | /* If we received an error in add_channel, we need to report it. */ |
821fffb2 | 570 | if (ret < 0) { |
1803a064 MD |
571 | ret = consumer_send_status_msg(sock, ret); |
572 | if (ret < 0) { | |
573 | goto error_fatal; | |
574 | } | |
e43c41c5 JD |
575 | goto end_nosignal; |
576 | } | |
577 | ||
3bd1e081 MD |
578 | goto end_nosignal; |
579 | } | |
580 | case LTTNG_CONSUMER_ADD_STREAM: | |
581 | { | |
dae10966 DG |
582 | int fd; |
583 | struct lttng_pipe *stream_pipe; | |
00e2e675 | 584 | struct lttng_consumer_stream *new_stream; |
ffe60014 | 585 | struct lttng_consumer_channel *channel; |
c80048c6 | 586 | int alloc_ret = 0; |
3bd1e081 | 587 | |
ffe60014 DG |
588 | /* |
589 | * Get stream's channel reference. Needed when adding the stream to the | |
590 | * global hash table. | |
591 | */ | |
592 | channel = consumer_find_channel(msg.u.stream.channel_key); | |
593 | if (!channel) { | |
594 | /* | |
595 | * We could not find the channel. Can happen if cpu hotplug | |
596 | * happens while tearing down. | |
597 | */ | |
d88aee68 | 598 | ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key); |
e462382a | 599 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
ffe60014 DG |
600 | } |
601 | ||
9ce5646a MD |
602 | health_code_update(); |
603 | ||
f50f23d9 DG |
604 | /* First send a status message before receiving the fds. */ |
605 | ret = consumer_send_status_msg(sock, ret_code); | |
1803a064 | 606 | if (ret < 0) { |
d771f832 | 607 | /* Somehow, the session daemon is not responding anymore. */ |
1803a064 MD |
608 | goto error_fatal; |
609 | } | |
9ce5646a MD |
610 | |
611 | health_code_update(); | |
612 | ||
0c759fc9 | 613 | if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
d771f832 | 614 | /* Channel was not found. */ |
f50f23d9 DG |
615 | goto end_nosignal; |
616 | } | |
617 | ||
d771f832 | 618 | /* Blocking call */ |
9ce5646a MD |
619 | health_poll_entry(); |
620 | ret = lttng_consumer_poll_socket(consumer_sockpoll); | |
621 | health_poll_exit(); | |
84382d49 MD |
622 | if (ret) { |
623 | goto error_fatal; | |
3bd1e081 | 624 | } |
00e2e675 | 625 | |
9ce5646a MD |
626 | health_code_update(); |
627 | ||
00e2e675 | 628 | /* Get stream file descriptor from socket */ |
f2fc6720 MD |
629 | ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1); |
630 | if (ret != sizeof(fd)) { | |
f73fabfd | 631 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD); |
3f8e211f | 632 | rcu_read_unlock(); |
3bd1e081 MD |
633 | return ret; |
634 | } | |
3bd1e081 | 635 | |
9ce5646a MD |
636 | health_code_update(); |
637 | ||
f50f23d9 DG |
638 | /* |
639 | * Send status code to session daemon only if the recv works. If the | |
640 | * above recv() failed, the session daemon is notified through the | |
641 | * error socket and the teardown is eventually done. | |
642 | */ | |
643 | ret = consumer_send_status_msg(sock, ret_code); | |
644 | if (ret < 0) { | |
645 | /* Somehow, the session daemon is not responding anymore. */ | |
646 | goto end_nosignal; | |
647 | } | |
648 | ||
9ce5646a MD |
649 | health_code_update(); |
650 | ||
ffe60014 DG |
651 | new_stream = consumer_allocate_stream(channel->key, |
652 | fd, | |
653 | LTTNG_CONSUMER_ACTIVE_STREAM, | |
654 | channel->name, | |
655 | channel->uid, | |
656 | channel->gid, | |
657 | channel->relayd_id, | |
658 | channel->session_id, | |
659 | msg.u.stream.cpu, | |
660 | &alloc_ret, | |
4891ece8 DG |
661 | channel->type, |
662 | channel->monitor); | |
3bd1e081 | 663 | if (new_stream == NULL) { |
c80048c6 MD |
664 | switch (alloc_ret) { |
665 | case -ENOMEM: | |
666 | case -EINVAL: | |
667 | default: | |
668 | lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR); | |
669 | break; | |
c80048c6 | 670 | } |
3f8e211f | 671 | goto end_nosignal; |
3bd1e081 | 672 | } |
d771f832 | 673 | |
ffe60014 DG |
674 | new_stream->chan = channel; |
675 | new_stream->wait_fd = fd; | |
07b86b52 JD |
676 | switch (channel->output) { |
677 | case CONSUMER_CHANNEL_SPLICE: | |
678 | new_stream->output = LTTNG_EVENT_SPLICE; | |
a2361a61 JD |
679 | ret = utils_create_pipe(new_stream->splice_pipe); |
680 | if (ret < 0) { | |
681 | goto end_nosignal; | |
682 | } | |
07b86b52 JD |
683 | break; |
684 | case CONSUMER_CHANNEL_MMAP: | |
685 | new_stream->output = LTTNG_EVENT_MMAP; | |
686 | break; | |
687 | default: | |
688 | ERR("Stream output unknown %d", channel->output); | |
689 | goto end_nosignal; | |
690 | } | |
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 | ||
633d0084 DG |
714 | if (ctx->on_recv_stream) { |
715 | ret = ctx->on_recv_stream(new_stream); | |
716 | if (ret < 0) { | |
d771f832 | 717 | consumer_stream_free(new_stream); |
633d0084 | 718 | goto end_nosignal; |
fb3a43a9 | 719 | } |
633d0084 | 720 | } |
fb3a43a9 | 721 | |
9ce5646a MD |
722 | health_code_update(); |
723 | ||
07b86b52 JD |
724 | if (new_stream->metadata_flag) { |
725 | channel->metadata_stream = new_stream; | |
726 | } | |
727 | ||
2bba9e53 DG |
728 | /* Do not monitor this stream. */ |
729 | if (!channel->monitor) { | |
5eecee74 | 730 | DBG("Kernel consumer add stream %s in no monitor mode with " |
6dc3064a | 731 | "relayd id %" PRIu64, new_stream->name, |
5eecee74 | 732 | new_stream->net_seq_idx); |
10a50311 | 733 | cds_list_add(&new_stream->send_node, &channel->streams.head); |
6dc3064a DG |
734 | break; |
735 | } | |
736 | ||
e1b71bdc DG |
737 | /* Send stream to relayd if the stream has an ID. */ |
738 | if (new_stream->net_seq_idx != (uint64_t) -1ULL) { | |
194ee077 DG |
739 | ret = consumer_send_relayd_stream(new_stream, |
740 | new_stream->chan->pathname); | |
e1b71bdc DG |
741 | if (ret < 0) { |
742 | consumer_stream_free(new_stream); | |
743 | goto end_nosignal; | |
744 | } | |
1bf914e0 MD |
745 | |
746 | /* | |
747 | * If adding an extra stream to an already | |
748 | * existing channel (e.g. cpu hotplug), we need | |
749 | * to send the "streams_sent" command to relayd. | |
750 | */ | |
751 | if (channel->streams_sent_to_relayd) { | |
752 | ret = consumer_send_relayd_streams_sent( | |
753 | new_stream->net_seq_idx); | |
754 | if (ret < 0) { | |
755 | goto end_nosignal; | |
756 | } | |
757 | } | |
e2039c7a JD |
758 | } |
759 | ||
50f8ae69 | 760 | /* Get the right pipe where the stream will be sent. */ |
633d0084 | 761 | if (new_stream->metadata_flag) { |
5ab66908 MD |
762 | ret = consumer_add_metadata_stream(new_stream); |
763 | if (ret) { | |
764 | ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing", | |
765 | new_stream->key); | |
766 | consumer_stream_free(new_stream); | |
767 | goto end_nosignal; | |
768 | } | |
dae10966 | 769 | stream_pipe = ctx->consumer_metadata_pipe; |
3bd1e081 | 770 | } else { |
5ab66908 MD |
771 | ret = consumer_add_data_stream(new_stream); |
772 | if (ret) { | |
773 | ERR("Consumer add stream %" PRIu64 " failed. Continuing", | |
774 | new_stream->key); | |
775 | consumer_stream_free(new_stream); | |
776 | goto end_nosignal; | |
777 | } | |
dae10966 | 778 | stream_pipe = ctx->consumer_data_pipe; |
50f8ae69 DG |
779 | } |
780 | ||
5ab66908 MD |
781 | /* Vitible to other threads */ |
782 | new_stream->globally_visible = 1; | |
783 | ||
9ce5646a MD |
784 | health_code_update(); |
785 | ||
dae10966 | 786 | ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream)); |
50f8ae69 | 787 | if (ret < 0) { |
dae10966 | 788 | ERR("Consumer write %s stream to pipe %d", |
50f8ae69 | 789 | new_stream->metadata_flag ? "metadata" : "data", |
dae10966 | 790 | lttng_pipe_get_writefd(stream_pipe)); |
5ab66908 MD |
791 | if (new_stream->metadata_flag) { |
792 | consumer_del_stream_for_metadata(new_stream); | |
793 | } else { | |
794 | consumer_del_stream_for_data(new_stream); | |
795 | } | |
50f8ae69 | 796 | goto end_nosignal; |
3bd1e081 | 797 | } |
00e2e675 | 798 | |
50f8ae69 | 799 | DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64, |
ffe60014 | 800 | new_stream->name, fd, new_stream->relayd_stream_id); |
3bd1e081 MD |
801 | break; |
802 | } | |
a4baae1b JD |
803 | case LTTNG_CONSUMER_STREAMS_SENT: |
804 | { | |
805 | struct lttng_consumer_channel *channel; | |
806 | ||
807 | /* | |
808 | * Get stream's channel reference. Needed when adding the stream to the | |
809 | * global hash table. | |
810 | */ | |
811 | channel = consumer_find_channel(msg.u.sent_streams.channel_key); | |
812 | if (!channel) { | |
813 | /* | |
814 | * We could not find the channel. Can happen if cpu hotplug | |
815 | * happens while tearing down. | |
816 | */ | |
817 | ERR("Unable to find channel key %" PRIu64, | |
818 | msg.u.sent_streams.channel_key); | |
e462382a | 819 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
a4baae1b JD |
820 | } |
821 | ||
822 | health_code_update(); | |
823 | ||
824 | /* | |
825 | * Send status code to session daemon. | |
826 | */ | |
827 | ret = consumer_send_status_msg(sock, ret_code); | |
f261ad0a | 828 | if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) { |
a4baae1b JD |
829 | /* Somehow, the session daemon is not responding anymore. */ |
830 | goto end_nosignal; | |
831 | } | |
832 | ||
833 | health_code_update(); | |
834 | ||
835 | /* | |
836 | * We should not send this message if we don't monitor the | |
837 | * streams in this channel. | |
838 | */ | |
839 | if (!channel->monitor) { | |
840 | break; | |
841 | } | |
842 | ||
843 | health_code_update(); | |
844 | /* Send stream to relayd if the stream has an ID. */ | |
845 | if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) { | |
846 | ret = consumer_send_relayd_streams_sent( | |
847 | msg.u.sent_streams.net_seq_idx); | |
848 | if (ret < 0) { | |
849 | goto end_nosignal; | |
850 | } | |
1bf914e0 | 851 | channel->streams_sent_to_relayd = true; |
a4baae1b JD |
852 | } |
853 | break; | |
854 | } | |
3bd1e081 MD |
855 | case LTTNG_CONSUMER_UPDATE_STREAM: |
856 | { | |
3f8e211f DG |
857 | rcu_read_unlock(); |
858 | return -ENOSYS; | |
859 | } | |
860 | case LTTNG_CONSUMER_DESTROY_RELAYD: | |
861 | { | |
a6ba4fe1 | 862 | uint64_t index = msg.u.destroy_relayd.net_seq_idx; |
3f8e211f DG |
863 | struct consumer_relayd_sock_pair *relayd; |
864 | ||
a6ba4fe1 | 865 | DBG("Kernel consumer destroying relayd %" PRIu64, index); |
3f8e211f DG |
866 | |
867 | /* Get relayd reference if exists. */ | |
a6ba4fe1 | 868 | relayd = consumer_find_relayd(index); |
3f8e211f | 869 | if (relayd == NULL) { |
3448e266 | 870 | DBG("Unable to find relayd %" PRIu64, index); |
e462382a | 871 | ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL; |
3bd1e081 | 872 | } |
3f8e211f | 873 | |
a6ba4fe1 DG |
874 | /* |
875 | * Each relayd socket pair has a refcount of stream attached to it | |
876 | * which tells if the relayd is still active or not depending on the | |
877 | * refcount value. | |
878 | * | |
879 | * This will set the destroy flag of the relayd object and destroy it | |
880 | * if the refcount reaches zero when called. | |
881 | * | |
882 | * The destroy can happen either here or when a stream fd hangs up. | |
883 | */ | |
f50f23d9 DG |
884 | if (relayd) { |
885 | consumer_flag_relayd_for_destroy(relayd); | |
886 | } | |
887 | ||
9ce5646a MD |
888 | health_code_update(); |
889 | ||
f50f23d9 DG |
890 | ret = consumer_send_status_msg(sock, ret_code); |
891 | if (ret < 0) { | |
892 | /* Somehow, the session daemon is not responding anymore. */ | |
1803a064 | 893 | goto error_fatal; |
f50f23d9 | 894 | } |
3f8e211f | 895 | |
3f8e211f | 896 | goto end_nosignal; |
3bd1e081 | 897 | } |
6d805429 | 898 | case LTTNG_CONSUMER_DATA_PENDING: |
53632229 | 899 | { |
c8f59ee5 | 900 | int32_t ret; |
6d805429 | 901 | uint64_t id = msg.u.data_pending.session_id; |
c8f59ee5 | 902 | |
6d805429 | 903 | DBG("Kernel consumer data pending command for id %" PRIu64, id); |
c8f59ee5 | 904 | |
6d805429 | 905 | ret = consumer_data_pending(id); |
c8f59ee5 | 906 | |
9ce5646a MD |
907 | health_code_update(); |
908 | ||
c8f59ee5 DG |
909 | /* Send back returned value to session daemon */ |
910 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
911 | if (ret < 0) { | |
6d805429 | 912 | PERROR("send data pending ret code"); |
1803a064 | 913 | goto error_fatal; |
c8f59ee5 | 914 | } |
f50f23d9 DG |
915 | |
916 | /* | |
917 | * No need to send back a status message since the data pending | |
918 | * returned value is the response. | |
919 | */ | |
c8f59ee5 | 920 | break; |
53632229 | 921 | } |
6dc3064a DG |
922 | case LTTNG_CONSUMER_SNAPSHOT_CHANNEL: |
923 | { | |
07b86b52 JD |
924 | if (msg.u.snapshot_channel.metadata == 1) { |
925 | ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key, | |
e2039c7a JD |
926 | msg.u.snapshot_channel.pathname, |
927 | msg.u.snapshot_channel.relayd_id, ctx); | |
07b86b52 JD |
928 | if (ret < 0) { |
929 | ERR("Snapshot metadata failed"); | |
e462382a | 930 | ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA; |
07b86b52 JD |
931 | } |
932 | } else { | |
933 | ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key, | |
934 | msg.u.snapshot_channel.pathname, | |
5c786ded | 935 | msg.u.snapshot_channel.relayd_id, |
d07ceecd | 936 | msg.u.snapshot_channel.nb_packets_per_stream, |
5c786ded | 937 | ctx); |
07b86b52 JD |
938 | if (ret < 0) { |
939 | ERR("Snapshot channel failed"); | |
e462382a | 940 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
07b86b52 JD |
941 | } |
942 | } | |
943 | ||
9ce5646a MD |
944 | health_code_update(); |
945 | ||
6dc3064a DG |
946 | ret = consumer_send_status_msg(sock, ret_code); |
947 | if (ret < 0) { | |
948 | /* Somehow, the session daemon is not responding anymore. */ | |
949 | goto end_nosignal; | |
950 | } | |
951 | break; | |
952 | } | |
07b86b52 JD |
953 | case LTTNG_CONSUMER_DESTROY_CHANNEL: |
954 | { | |
955 | uint64_t key = msg.u.destroy_channel.key; | |
956 | struct lttng_consumer_channel *channel; | |
957 | ||
958 | channel = consumer_find_channel(key); | |
959 | if (!channel) { | |
960 | ERR("Kernel consumer destroy channel %" PRIu64 " not found", key); | |
e462382a | 961 | ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND; |
07b86b52 JD |
962 | } |
963 | ||
9ce5646a MD |
964 | health_code_update(); |
965 | ||
07b86b52 JD |
966 | ret = consumer_send_status_msg(sock, ret_code); |
967 | if (ret < 0) { | |
968 | /* Somehow, the session daemon is not responding anymore. */ | |
969 | goto end_nosignal; | |
970 | } | |
971 | ||
9ce5646a MD |
972 | health_code_update(); |
973 | ||
15dc512a DG |
974 | /* Stop right now if no channel was found. */ |
975 | if (!channel) { | |
976 | goto end_nosignal; | |
977 | } | |
978 | ||
07b86b52 JD |
979 | /* |
980 | * This command should ONLY be issued for channel with streams set in | |
981 | * no monitor mode. | |
982 | */ | |
983 | assert(!channel->monitor); | |
984 | ||
985 | /* | |
986 | * The refcount should ALWAYS be 0 in the case of a channel in no | |
987 | * monitor mode. | |
988 | */ | |
989 | assert(!uatomic_sub_return(&channel->refcount, 1)); | |
990 | ||
991 | consumer_del_channel(channel); | |
992 | ||
993 | goto end_nosignal; | |
994 | } | |
fb83fe64 JD |
995 | case LTTNG_CONSUMER_DISCARDED_EVENTS: |
996 | { | |
997 | uint64_t ret; | |
998 | struct lttng_consumer_channel *channel; | |
999 | uint64_t id = msg.u.discarded_events.session_id; | |
1000 | uint64_t key = msg.u.discarded_events.channel_key; | |
1001 | ||
e5742757 MD |
1002 | DBG("Kernel consumer discarded events command for session id %" |
1003 | PRIu64 ", channel key %" PRIu64, id, key); | |
1004 | ||
fb83fe64 JD |
1005 | channel = consumer_find_channel(key); |
1006 | if (!channel) { | |
1007 | ERR("Kernel consumer discarded events channel %" | |
1008 | PRIu64 " not found", key); | |
e5742757 MD |
1009 | ret = 0; |
1010 | } else { | |
1011 | ret = channel->discarded_events; | |
fb83fe64 JD |
1012 | } |
1013 | ||
fb83fe64 JD |
1014 | health_code_update(); |
1015 | ||
1016 | /* Send back returned value to session daemon */ | |
1017 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
1018 | if (ret < 0) { | |
1019 | PERROR("send discarded events"); | |
1020 | goto error_fatal; | |
1021 | } | |
1022 | ||
1023 | break; | |
1024 | } | |
1025 | case LTTNG_CONSUMER_LOST_PACKETS: | |
1026 | { | |
1027 | uint64_t ret; | |
1028 | struct lttng_consumer_channel *channel; | |
1029 | uint64_t id = msg.u.lost_packets.session_id; | |
1030 | uint64_t key = msg.u.lost_packets.channel_key; | |
1031 | ||
e5742757 MD |
1032 | DBG("Kernel consumer lost packets command for session id %" |
1033 | PRIu64 ", channel key %" PRIu64, id, key); | |
1034 | ||
fb83fe64 JD |
1035 | channel = consumer_find_channel(key); |
1036 | if (!channel) { | |
1037 | ERR("Kernel consumer lost packets channel %" | |
1038 | PRIu64 " not found", key); | |
e5742757 MD |
1039 | ret = 0; |
1040 | } else { | |
1041 | ret = channel->lost_packets; | |
fb83fe64 JD |
1042 | } |
1043 | ||
fb83fe64 JD |
1044 | health_code_update(); |
1045 | ||
1046 | /* Send back returned value to session daemon */ | |
1047 | ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret)); | |
1048 | if (ret < 0) { | |
1049 | PERROR("send lost packets"); | |
1050 | goto error_fatal; | |
1051 | } | |
1052 | ||
1053 | break; | |
1054 | } | |
b3530820 JG |
1055 | case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE: |
1056 | { | |
1057 | int channel_monitor_pipe; | |
1058 | ||
1059 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1060 | /* Successfully received the command's type. */ | |
1061 | ret = consumer_send_status_msg(sock, ret_code); | |
1062 | if (ret < 0) { | |
1063 | goto error_fatal; | |
1064 | } | |
1065 | ||
1066 | ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe, | |
1067 | 1); | |
1068 | if (ret != sizeof(channel_monitor_pipe)) { | |
1069 | ERR("Failed to receive channel monitor pipe"); | |
1070 | goto error_fatal; | |
1071 | } | |
1072 | ||
1073 | DBG("Received channel monitor pipe (%d)", channel_monitor_pipe); | |
1074 | ret = consumer_timer_thread_set_channel_monitor_pipe( | |
1075 | channel_monitor_pipe); | |
1076 | if (!ret) { | |
1077 | int flags; | |
1078 | ||
1079 | ret_code = LTTCOMM_CONSUMERD_SUCCESS; | |
1080 | /* Set the pipe as non-blocking. */ | |
1081 | ret = fcntl(channel_monitor_pipe, F_GETFL, 0); | |
1082 | if (ret == -1) { | |
1083 | PERROR("fcntl get flags of the channel monitoring pipe"); | |
1084 | goto error_fatal; | |
1085 | } | |
1086 | flags = ret; | |
1087 | ||
1088 | ret = fcntl(channel_monitor_pipe, F_SETFL, | |
1089 | flags | O_NONBLOCK); | |
1090 | if (ret == -1) { | |
1091 | PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe"); | |
1092 | goto error_fatal; | |
1093 | } | |
1094 | DBG("Channel monitor pipe set as non-blocking"); | |
1095 | } else { | |
1096 | ret_code = LTTCOMM_CONSUMERD_ALREADY_SET; | |
1097 | } | |
1098 | ret = consumer_send_status_msg(sock, ret_code); | |
1099 | if (ret < 0) { | |
1100 | goto error_fatal; | |
1101 | } | |
1102 | break; | |
1103 | } | |
3bd1e081 | 1104 | default: |
3f8e211f | 1105 | goto end_nosignal; |
3bd1e081 | 1106 | } |
3f8e211f | 1107 | |
3bd1e081 | 1108 | end_nosignal: |
b0b335c8 | 1109 | rcu_read_unlock(); |
4cbc1a04 DG |
1110 | |
1111 | /* | |
1112 | * Return 1 to indicate success since the 0 value can be a socket | |
1113 | * shutdown during the recv() or send() call. | |
1114 | */ | |
9ce5646a | 1115 | health_code_update(); |
4cbc1a04 | 1116 | return 1; |
1803a064 MD |
1117 | |
1118 | error_fatal: | |
1119 | rcu_read_unlock(); | |
1120 | /* This will issue a consumer stop. */ | |
1121 | return -1; | |
3bd1e081 | 1122 | } |
d41f73b7 | 1123 | |
309167d2 JD |
1124 | /* |
1125 | * Populate index values of a kernel stream. Values are set in big endian order. | |
1126 | * | |
1127 | * Return 0 on success or else a negative value. | |
1128 | */ | |
50adc264 | 1129 | static int get_index_values(struct ctf_packet_index *index, int infd) |
309167d2 JD |
1130 | { |
1131 | int ret; | |
1132 | ||
1133 | ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin); | |
1134 | if (ret < 0) { | |
1135 | PERROR("kernctl_get_timestamp_begin"); | |
1136 | goto error; | |
1137 | } | |
1138 | index->timestamp_begin = htobe64(index->timestamp_begin); | |
1139 | ||
1140 | ret = kernctl_get_timestamp_end(infd, &index->timestamp_end); | |
1141 | if (ret < 0) { | |
1142 | PERROR("kernctl_get_timestamp_end"); | |
1143 | goto error; | |
1144 | } | |
1145 | index->timestamp_end = htobe64(index->timestamp_end); | |
1146 | ||
1147 | ret = kernctl_get_events_discarded(infd, &index->events_discarded); | |
1148 | if (ret < 0) { | |
1149 | PERROR("kernctl_get_events_discarded"); | |
1150 | goto error; | |
1151 | } | |
1152 | index->events_discarded = htobe64(index->events_discarded); | |
1153 | ||
1154 | ret = kernctl_get_content_size(infd, &index->content_size); | |
1155 | if (ret < 0) { | |
1156 | PERROR("kernctl_get_content_size"); | |
1157 | goto error; | |
1158 | } | |
1159 | index->content_size = htobe64(index->content_size); | |
1160 | ||
1161 | ret = kernctl_get_packet_size(infd, &index->packet_size); | |
1162 | if (ret < 0) { | |
1163 | PERROR("kernctl_get_packet_size"); | |
1164 | goto error; | |
1165 | } | |
1166 | index->packet_size = htobe64(index->packet_size); | |
1167 | ||
1168 | ret = kernctl_get_stream_id(infd, &index->stream_id); | |
1169 | if (ret < 0) { | |
1170 | PERROR("kernctl_get_stream_id"); | |
1171 | goto error; | |
1172 | } | |
1173 | index->stream_id = htobe64(index->stream_id); | |
1174 | ||
234cd636 JD |
1175 | ret = kernctl_get_instance_id(infd, &index->stream_instance_id); |
1176 | if (ret < 0) { | |
f0b03c22 MD |
1177 | if (ret == -ENOTTY) { |
1178 | /* Command not implemented by lttng-modules. */ | |
1179 | index->stream_instance_id = -1ULL; | |
1180 | ret = 0; | |
1181 | } else { | |
1182 | PERROR("kernctl_get_instance_id"); | |
1183 | goto error; | |
1184 | } | |
234cd636 JD |
1185 | } |
1186 | index->stream_instance_id = htobe64(index->stream_instance_id); | |
1187 | ||
1188 | ret = kernctl_get_sequence_number(infd, &index->packet_seq_num); | |
1189 | if (ret < 0) { | |
f0b03c22 MD |
1190 | if (ret == -ENOTTY) { |
1191 | /* Command not implemented by lttng-modules. */ | |
1192 | index->packet_seq_num = -1ULL; | |
1193 | ret = 0; | |
1194 | } else { | |
1195 | PERROR("kernctl_get_sequence_number"); | |
1196 | goto error; | |
1197 | } | |
234cd636 JD |
1198 | } |
1199 | index->packet_seq_num = htobe64(index->packet_seq_num); | |
1200 | ||
309167d2 JD |
1201 | error: |
1202 | return ret; | |
1203 | } | |
94d49140 JD |
1204 | /* |
1205 | * Sync metadata meaning request them to the session daemon and snapshot to the | |
1206 | * metadata thread can consumer them. | |
1207 | * | |
1208 | * Metadata stream lock MUST be acquired. | |
1209 | * | |
1210 | * Return 0 if new metadatda is available, EAGAIN if the metadata stream | |
1211 | * is empty or a negative value on error. | |
1212 | */ | |
1213 | int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata) | |
1214 | { | |
1215 | int ret; | |
1216 | ||
1217 | assert(metadata); | |
1218 | ||
1219 | ret = kernctl_buffer_flush(metadata->wait_fd); | |
1220 | if (ret < 0) { | |
1221 | ERR("Failed to flush kernel stream"); | |
1222 | goto end; | |
1223 | } | |
1224 | ||
1225 | ret = kernctl_snapshot(metadata->wait_fd); | |
1226 | if (ret < 0) { | |
32af2c95 | 1227 | if (ret != -EAGAIN) { |
94d49140 JD |
1228 | ERR("Sync metadata, taking kernel snapshot failed."); |
1229 | goto end; | |
1230 | } | |
1231 | DBG("Sync metadata, no new kernel metadata"); | |
1232 | /* No new metadata, exit. */ | |
1233 | ret = ENODATA; | |
1234 | goto end; | |
1235 | } | |
1236 | ||
1237 | end: | |
1238 | return ret; | |
1239 | } | |
309167d2 | 1240 | |
fb83fe64 JD |
1241 | static |
1242 | int update_stream_stats(struct lttng_consumer_stream *stream) | |
1243 | { | |
1244 | int ret; | |
1245 | uint64_t seq, discarded; | |
1246 | ||
1247 | ret = kernctl_get_sequence_number(stream->wait_fd, &seq); | |
1248 | if (ret < 0) { | |
f0b03c22 MD |
1249 | if (ret == -ENOTTY) { |
1250 | /* Command not implemented by lttng-modules. */ | |
1251 | seq = -1ULL; | |
1252 | ret = 0; | |
1253 | } else { | |
1254 | PERROR("kernctl_get_sequence_number"); | |
1255 | goto end; | |
1256 | } | |
fb83fe64 JD |
1257 | } |
1258 | ||
1259 | /* | |
1260 | * Start the sequence when we extract the first packet in case we don't | |
1261 | * start at 0 (for example if a consumer is not connected to the | |
1262 | * session immediately after the beginning). | |
1263 | */ | |
1264 | if (stream->last_sequence_number == -1ULL) { | |
1265 | stream->last_sequence_number = seq; | |
1266 | } else if (seq > stream->last_sequence_number) { | |
1267 | stream->chan->lost_packets += seq - | |
1268 | stream->last_sequence_number - 1; | |
1269 | } else { | |
1270 | /* seq <= last_sequence_number */ | |
1271 | ERR("Sequence number inconsistent : prev = %" PRIu64 | |
1272 | ", current = %" PRIu64, | |
1273 | stream->last_sequence_number, seq); | |
1274 | ret = -1; | |
1275 | goto end; | |
1276 | } | |
1277 | stream->last_sequence_number = seq; | |
1278 | ||
1279 | ret = kernctl_get_events_discarded(stream->wait_fd, &discarded); | |
1280 | if (ret < 0) { | |
1281 | PERROR("kernctl_get_events_discarded"); | |
1282 | goto end; | |
1283 | } | |
1284 | if (discarded < stream->last_discarded_events) { | |
1285 | /* | |
83f4233d MJ |
1286 | * Overflow has occurred. We assume only one wrap-around |
1287 | * has occurred. | |
fb83fe64 JD |
1288 | */ |
1289 | stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) - | |
1290 | stream->last_discarded_events + discarded; | |
1291 | } else { | |
1292 | stream->chan->discarded_events += discarded - | |
1293 | stream->last_discarded_events; | |
1294 | } | |
1295 | stream->last_discarded_events = discarded; | |
1296 | ret = 0; | |
1297 | ||
1298 | end: | |
1299 | return ret; | |
1300 | } | |
1301 | ||
93ec662e JD |
1302 | /* |
1303 | * Check if the local version of the metadata stream matches with the version | |
1304 | * of the metadata stream in the kernel. If it was updated, set the reset flag | |
1305 | * on the stream. | |
1306 | */ | |
1307 | static | |
1308 | int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream) | |
1309 | { | |
1310 | int ret; | |
1311 | uint64_t cur_version; | |
1312 | ||
1313 | ret = kernctl_get_metadata_version(infd, &cur_version); | |
1314 | if (ret < 0) { | |
f0b03c22 MD |
1315 | if (ret == -ENOTTY) { |
1316 | /* | |
1317 | * LTTng-modules does not implement this | |
1318 | * command. | |
1319 | */ | |
1320 | ret = 0; | |
1321 | goto end; | |
1322 | } | |
93ec662e JD |
1323 | ERR("Failed to get the metadata version"); |
1324 | goto end; | |
1325 | } | |
1326 | ||
1327 | if (stream->metadata_version == cur_version) { | |
1328 | ret = 0; | |
1329 | goto end; | |
1330 | } | |
1331 | ||
1332 | DBG("New metadata version detected"); | |
1333 | stream->metadata_version = cur_version; | |
1334 | stream->reset_metadata_flag = 1; | |
1335 | ret = 0; | |
1336 | ||
1337 | end: | |
1338 | return ret; | |
1339 | } | |
1340 | ||
d41f73b7 MD |
1341 | /* |
1342 | * Consume data on a file descriptor and write it on a trace file. | |
1343 | */ | |
4078b776 | 1344 | ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream, |
d41f73b7 MD |
1345 | struct lttng_consumer_local_data *ctx) |
1346 | { | |
1d4dfdef | 1347 | unsigned long len, subbuf_size, padding; |
1c20f0e2 | 1348 | int err, write_index = 1; |
4078b776 | 1349 | ssize_t ret = 0; |
d41f73b7 | 1350 | int infd = stream->wait_fd; |
50adc264 | 1351 | struct ctf_packet_index index; |
d41f73b7 MD |
1352 | |
1353 | DBG("In read_subbuffer (infd : %d)", infd); | |
309167d2 | 1354 | |
d41f73b7 MD |
1355 | /* Get the next subbuffer */ |
1356 | err = kernctl_get_next_subbuf(infd); | |
1357 | if (err != 0) { | |
d41f73b7 MD |
1358 | /* |
1359 | * This is a debug message even for single-threaded consumer, | |
1360 | * because poll() have more relaxed criterions than get subbuf, | |
1361 | * so get_subbuf may fail for short race windows where poll() | |
1362 | * would issue wakeups. | |
1363 | */ | |
1364 | DBG("Reserving sub buffer failed (everything is normal, " | |
1365 | "it is due to concurrency)"); | |
32af2c95 | 1366 | ret = err; |
d41f73b7 MD |
1367 | goto end; |
1368 | } | |
1369 | ||
1d4dfdef DG |
1370 | /* Get the full subbuffer size including padding */ |
1371 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
1372 | if (err != 0) { | |
5a510c9f | 1373 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1374 | err = kernctl_put_subbuf(infd); |
1375 | if (err != 0) { | |
32af2c95 | 1376 | if (err == -EFAULT) { |
5a510c9f | 1377 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1378 | } else if (err == -EIO) { |
8265f19e | 1379 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1380 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1381 | } |
32af2c95 | 1382 | ret = err; |
8265f19e MD |
1383 | goto end; |
1384 | } | |
32af2c95 | 1385 | ret = err; |
1d4dfdef DG |
1386 | goto end; |
1387 | } | |
1388 | ||
1c20f0e2 | 1389 | if (!stream->metadata_flag) { |
309167d2 JD |
1390 | ret = get_index_values(&index, infd); |
1391 | if (ret < 0) { | |
8265f19e MD |
1392 | err = kernctl_put_subbuf(infd); |
1393 | if (err != 0) { | |
32af2c95 | 1394 | if (err == -EFAULT) { |
5a510c9f | 1395 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1396 | } else if (err == -EIO) { |
8265f19e | 1397 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1398 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1399 | } |
32af2c95 | 1400 | ret = err; |
8265f19e MD |
1401 | goto end; |
1402 | } | |
309167d2 JD |
1403 | goto end; |
1404 | } | |
fb83fe64 JD |
1405 | ret = update_stream_stats(stream); |
1406 | if (ret < 0) { | |
7b87473d MD |
1407 | err = kernctl_put_subbuf(infd); |
1408 | if (err != 0) { | |
1409 | if (err == -EFAULT) { | |
1410 | PERROR("Error in unreserving sub buffer\n"); | |
1411 | } else if (err == -EIO) { | |
1412 | /* Should never happen with newer LTTng versions */ | |
1413 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
1414 | } | |
1415 | ret = err; | |
1416 | goto end; | |
1417 | } | |
fb83fe64 JD |
1418 | goto end; |
1419 | } | |
1c20f0e2 JD |
1420 | } else { |
1421 | write_index = 0; | |
93ec662e JD |
1422 | ret = metadata_stream_check_version(infd, stream); |
1423 | if (ret < 0) { | |
7b87473d MD |
1424 | err = kernctl_put_subbuf(infd); |
1425 | if (err != 0) { | |
1426 | if (err == -EFAULT) { | |
1427 | PERROR("Error in unreserving sub buffer\n"); | |
1428 | } else if (err == -EIO) { | |
1429 | /* Should never happen with newer LTTng versions */ | |
1430 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
1431 | } | |
1432 | ret = err; | |
1433 | goto end; | |
1434 | } | |
93ec662e JD |
1435 | goto end; |
1436 | } | |
309167d2 JD |
1437 | } |
1438 | ||
ffe60014 | 1439 | switch (stream->chan->output) { |
07b86b52 | 1440 | case CONSUMER_CHANNEL_SPLICE: |
1d4dfdef DG |
1441 | /* |
1442 | * XXX: The lttng-modules splice "actor" does not handle copying | |
1443 | * partial pages hence only using the subbuffer size without the | |
1444 | * padding makes the splice fail. | |
1445 | */ | |
1446 | subbuf_size = len; | |
1447 | padding = 0; | |
1448 | ||
1449 | /* splice the subbuffer to the tracefile */ | |
91dfef6e | 1450 | ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size, |
309167d2 | 1451 | padding, &index); |
91dfef6e DG |
1452 | /* |
1453 | * XXX: Splice does not support network streaming so the return value | |
1454 | * is simply checked against subbuf_size and not like the mmap() op. | |
1455 | */ | |
1d4dfdef DG |
1456 | if (ret != subbuf_size) { |
1457 | /* | |
1458 | * display the error but continue processing to try | |
1459 | * to release the subbuffer | |
1460 | */ | |
1461 | ERR("Error splicing to tracefile (ret: %zd != len: %lu)", | |
1462 | ret, subbuf_size); | |
309167d2 | 1463 | write_index = 0; |
1d4dfdef DG |
1464 | } |
1465 | break; | |
07b86b52 | 1466 | case CONSUMER_CHANNEL_MMAP: |
1d4dfdef DG |
1467 | /* Get subbuffer size without padding */ |
1468 | err = kernctl_get_subbuf_size(infd, &subbuf_size); | |
1469 | if (err != 0) { | |
5a510c9f | 1470 | PERROR("Getting sub-buffer len failed."); |
8265f19e MD |
1471 | err = kernctl_put_subbuf(infd); |
1472 | if (err != 0) { | |
32af2c95 | 1473 | if (err == -EFAULT) { |
5a510c9f | 1474 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1475 | } else if (err == -EIO) { |
8265f19e | 1476 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1477 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
8265f19e | 1478 | } |
32af2c95 | 1479 | ret = err; |
8265f19e MD |
1480 | goto end; |
1481 | } | |
32af2c95 | 1482 | ret = err; |
1d4dfdef DG |
1483 | goto end; |
1484 | } | |
47e81c02 | 1485 | |
1d4dfdef DG |
1486 | /* Make sure the tracer is not gone mad on us! */ |
1487 | assert(len >= subbuf_size); | |
1488 | ||
1489 | padding = len - subbuf_size; | |
1490 | ||
1491 | /* write the subbuffer to the tracefile */ | |
91dfef6e | 1492 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, |
309167d2 | 1493 | padding, &index); |
91dfef6e DG |
1494 | /* |
1495 | * The mmap operation should write subbuf_size amount of data when | |
1496 | * network streaming or the full padding (len) size when we are _not_ | |
1497 | * streaming. | |
1498 | */ | |
d88aee68 DG |
1499 | if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) || |
1500 | (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) { | |
1d4dfdef | 1501 | /* |
91dfef6e | 1502 | * Display the error but continue processing to try to release the |
2336629e DG |
1503 | * subbuffer. This is a DBG statement since this is possible to |
1504 | * happen without being a critical error. | |
1d4dfdef | 1505 | */ |
2336629e | 1506 | DBG("Error writing to tracefile " |
91dfef6e DG |
1507 | "(ret: %zd != len: %lu != subbuf_size: %lu)", |
1508 | ret, len, subbuf_size); | |
309167d2 | 1509 | write_index = 0; |
1d4dfdef DG |
1510 | } |
1511 | break; | |
1512 | default: | |
1513 | ERR("Unknown output method"); | |
56591bac | 1514 | ret = -EPERM; |
d41f73b7 MD |
1515 | } |
1516 | ||
1517 | err = kernctl_put_next_subbuf(infd); | |
1518 | if (err != 0) { | |
32af2c95 | 1519 | if (err == -EFAULT) { |
5a510c9f | 1520 | PERROR("Error in unreserving sub buffer\n"); |
32af2c95 | 1521 | } else if (err == -EIO) { |
d41f73b7 | 1522 | /* Should never happen with newer LTTng versions */ |
5a510c9f | 1523 | PERROR("Reader has been pushed by the writer, last sub-buffer corrupted."); |
d41f73b7 | 1524 | } |
32af2c95 | 1525 | ret = err; |
d41f73b7 MD |
1526 | goto end; |
1527 | } | |
1528 | ||
309167d2 | 1529 | /* Write index if needed. */ |
1c20f0e2 JD |
1530 | if (!write_index) { |
1531 | goto end; | |
1532 | } | |
1533 | ||
94d49140 JD |
1534 | if (stream->chan->live_timer_interval && !stream->metadata_flag) { |
1535 | /* | |
1536 | * In live, block until all the metadata is sent. | |
1537 | */ | |
c585821b MD |
1538 | pthread_mutex_lock(&stream->metadata_timer_lock); |
1539 | assert(!stream->missed_metadata_flush); | |
1540 | stream->waiting_on_metadata = true; | |
1541 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1542 | ||
94d49140 | 1543 | err = consumer_stream_sync_metadata(ctx, stream->session_id); |
c585821b MD |
1544 | |
1545 | pthread_mutex_lock(&stream->metadata_timer_lock); | |
1546 | stream->waiting_on_metadata = false; | |
1547 | if (stream->missed_metadata_flush) { | |
1548 | stream->missed_metadata_flush = false; | |
1549 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1550 | (void) consumer_flush_kernel_index(stream); | |
1551 | } else { | |
1552 | pthread_mutex_unlock(&stream->metadata_timer_lock); | |
1553 | } | |
94d49140 JD |
1554 | if (err < 0) { |
1555 | goto end; | |
1556 | } | |
1557 | } | |
1558 | ||
1c20f0e2 JD |
1559 | err = consumer_stream_write_index(stream, &index); |
1560 | if (err < 0) { | |
1561 | goto end; | |
309167d2 JD |
1562 | } |
1563 | ||
d41f73b7 MD |
1564 | end: |
1565 | return ret; | |
1566 | } | |
1567 | ||
1568 | int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
1569 | { | |
1570 | int ret; | |
ffe60014 DG |
1571 | |
1572 | assert(stream); | |
1573 | ||
2bba9e53 DG |
1574 | /* |
1575 | * Don't create anything if this is set for streaming or should not be | |
1576 | * monitored. | |
1577 | */ | |
1578 | if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) { | |
fe4477ee JD |
1579 | ret = utils_create_stream_file(stream->chan->pathname, stream->name, |
1580 | stream->chan->tracefile_size, stream->tracefile_count_current, | |
309167d2 | 1581 | stream->uid, stream->gid, NULL); |
fe4477ee JD |
1582 | if (ret < 0) { |
1583 | goto error; | |
1584 | } | |
1585 | stream->out_fd = ret; | |
1586 | stream->tracefile_size_current = 0; | |
309167d2 JD |
1587 | |
1588 | if (!stream->metadata_flag) { | |
f8f3885c MD |
1589 | struct lttng_index_file *index_file; |
1590 | ||
1591 | index_file = lttng_index_file_create(stream->chan->pathname, | |
309167d2 JD |
1592 | stream->name, stream->uid, stream->gid, |
1593 | stream->chan->tracefile_size, | |
f8f3885c MD |
1594 | stream->tracefile_count_current, |
1595 | CTF_INDEX_MAJOR, CTF_INDEX_MINOR); | |
1596 | if (!index_file) { | |
309167d2 JD |
1597 | goto error; |
1598 | } | |
1b47ae58 | 1599 | assert(!stream->index_file); |
f8f3885c | 1600 | stream->index_file = index_file; |
309167d2 | 1601 | } |
ffe60014 | 1602 | } |
d41f73b7 | 1603 | |
d41f73b7 MD |
1604 | if (stream->output == LTTNG_EVENT_MMAP) { |
1605 | /* get the len of the mmap region */ | |
1606 | unsigned long mmap_len; | |
1607 | ||
1608 | ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len); | |
1609 | if (ret != 0) { | |
ffe60014 | 1610 | PERROR("kernctl_get_mmap_len"); |
d41f73b7 MD |
1611 | goto error_close_fd; |
1612 | } | |
1613 | stream->mmap_len = (size_t) mmap_len; | |
1614 | ||
ffe60014 DG |
1615 | stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ, |
1616 | MAP_PRIVATE, stream->wait_fd, 0); | |
d41f73b7 | 1617 | if (stream->mmap_base == MAP_FAILED) { |
ffe60014 | 1618 | PERROR("Error mmaping"); |
d41f73b7 MD |
1619 | ret = -1; |
1620 | goto error_close_fd; | |
1621 | } | |
1622 | } | |
1623 | ||
1624 | /* we return 0 to let the library handle the FD internally */ | |
1625 | return 0; | |
1626 | ||
1627 | error_close_fd: | |
2f225ce2 | 1628 | if (stream->out_fd >= 0) { |
d41f73b7 MD |
1629 | int err; |
1630 | ||
1631 | err = close(stream->out_fd); | |
1632 | assert(!err); | |
2f225ce2 | 1633 | stream->out_fd = -1; |
d41f73b7 MD |
1634 | } |
1635 | error: | |
1636 | return ret; | |
1637 | } | |
1638 | ||
ca22feea DG |
1639 | /* |
1640 | * Check if data is still being extracted from the buffers for a specific | |
4e9a4686 DG |
1641 | * stream. Consumer data lock MUST be acquired before calling this function |
1642 | * and the stream lock. | |
ca22feea | 1643 | * |
6d805429 | 1644 | * Return 1 if the traced data are still getting read else 0 meaning that the |
ca22feea DG |
1645 | * data is available for trace viewer reading. |
1646 | */ | |
6d805429 | 1647 | int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream) |
ca22feea DG |
1648 | { |
1649 | int ret; | |
1650 | ||
1651 | assert(stream); | |
1652 | ||
873b9e9a MD |
1653 | if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { |
1654 | ret = 0; | |
1655 | goto end; | |
1656 | } | |
1657 | ||
ca22feea DG |
1658 | ret = kernctl_get_next_subbuf(stream->wait_fd); |
1659 | if (ret == 0) { | |
1660 | /* There is still data so let's put back this subbuffer. */ | |
1661 | ret = kernctl_put_subbuf(stream->wait_fd); | |
1662 | assert(ret == 0); | |
6d805429 | 1663 | ret = 1; /* Data is pending */ |
4e9a4686 | 1664 | goto end; |
ca22feea DG |
1665 | } |
1666 | ||
6d805429 DG |
1667 | /* Data is NOT pending and ready to be read. */ |
1668 | ret = 0; | |
ca22feea | 1669 | |
6efae65e DG |
1670 | end: |
1671 | return ret; | |
ca22feea | 1672 | } |