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