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