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