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