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