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