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