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