| 1 | /* |
| 2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * David Goulet <dgoulet@efficios.com> |
| 4 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License, version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #define _LGPL_SOURCE |
| 21 | #include <common/common.h> |
| 22 | #include <common/utils.h> |
| 23 | #include <common/defaults.h> |
| 24 | #include <urcu/rculist.h> |
| 25 | #include <sys/stat.h> |
| 26 | |
| 27 | #include "lttng-relayd.h" |
| 28 | #include "index.h" |
| 29 | #include "stream.h" |
| 30 | #include "viewer-stream.h" |
| 31 | |
| 32 | /* Should be called with RCU read-side lock held. */ |
| 33 | bool stream_get(struct relay_stream *stream) |
| 34 | { |
| 35 | bool has_ref = false; |
| 36 | |
| 37 | pthread_mutex_lock(&stream->reflock); |
| 38 | if (stream->ref.refcount != 0) { |
| 39 | has_ref = true; |
| 40 | urcu_ref_get(&stream->ref); |
| 41 | } |
| 42 | pthread_mutex_unlock(&stream->reflock); |
| 43 | |
| 44 | return has_ref; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Get stream from stream id from the streams hash table. Return stream |
| 49 | * if found else NULL. A stream reference is taken when a stream is |
| 50 | * returned. stream_put() must be called on that stream. |
| 51 | */ |
| 52 | struct relay_stream *stream_get_by_id(uint64_t stream_id) |
| 53 | { |
| 54 | struct lttng_ht_node_u64 *node; |
| 55 | struct lttng_ht_iter iter; |
| 56 | struct relay_stream *stream = NULL; |
| 57 | |
| 58 | rcu_read_lock(); |
| 59 | lttng_ht_lookup(relay_streams_ht, &stream_id, &iter); |
| 60 | node = lttng_ht_iter_get_node_u64(&iter); |
| 61 | if (!node) { |
| 62 | DBG("Relay stream %" PRIu64 " not found", stream_id); |
| 63 | goto end; |
| 64 | } |
| 65 | stream = caa_container_of(node, struct relay_stream, node); |
| 66 | if (!stream_get(stream)) { |
| 67 | stream = NULL; |
| 68 | } |
| 69 | end: |
| 70 | rcu_read_unlock(); |
| 71 | return stream; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * We keep ownership of path_name and channel_name. |
| 76 | */ |
| 77 | struct relay_stream *stream_create(struct ctf_trace *trace, |
| 78 | uint64_t stream_handle, char *path_name, |
| 79 | char *channel_name, uint64_t tracefile_size, |
| 80 | uint64_t tracefile_count) |
| 81 | { |
| 82 | int ret; |
| 83 | struct relay_stream *stream = NULL; |
| 84 | struct relay_session *session = trace->session; |
| 85 | |
| 86 | stream = zmalloc(sizeof(struct relay_stream)); |
| 87 | if (stream == NULL) { |
| 88 | PERROR("relay stream zmalloc"); |
| 89 | goto error_no_alloc; |
| 90 | } |
| 91 | |
| 92 | stream->stream_handle = stream_handle; |
| 93 | stream->prev_seq = -1ULL; |
| 94 | stream->last_net_seq_num = -1ULL; |
| 95 | stream->ctf_stream_id = -1ULL; |
| 96 | stream->tracefile_size = tracefile_size; |
| 97 | stream->tracefile_count = tracefile_count; |
| 98 | stream->path_name = path_name; |
| 99 | stream->channel_name = channel_name; |
| 100 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
| 101 | pthread_mutex_init(&stream->lock, NULL); |
| 102 | pthread_mutex_init(&stream->reflock, NULL); |
| 103 | urcu_ref_init(&stream->ref); |
| 104 | ctf_trace_get(trace); |
| 105 | stream->trace = trace; |
| 106 | |
| 107 | stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
| 108 | if (!stream->indexes_ht) { |
| 109 | ERR("Cannot created indexes_ht"); |
| 110 | ret = -1; |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG, |
| 115 | -1, -1); |
| 116 | if (ret < 0) { |
| 117 | ERR("relay creating output directory"); |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * No need to use run_as API here because whatever we receive, |
| 123 | * the relayd uses its own credentials for the stream files. |
| 124 | */ |
| 125 | ret = utils_create_stream_file(stream->path_name, stream->channel_name, |
| 126 | stream->tracefile_size, 0, -1, -1, NULL); |
| 127 | if (ret < 0) { |
| 128 | ERR("Create output file"); |
| 129 | goto end; |
| 130 | } |
| 131 | stream->stream_fd = stream_fd_create(ret); |
| 132 | if (!stream->stream_fd) { |
| 133 | if (close(ret)) { |
| 134 | PERROR("Error closing file %d", ret); |
| 135 | } |
| 136 | ret = -1; |
| 137 | goto end; |
| 138 | } |
| 139 | stream->tfa = tracefile_array_create(stream->tracefile_count); |
| 140 | if (!stream->tfa) { |
| 141 | ret = -1; |
| 142 | goto end; |
| 143 | } |
| 144 | if (stream->tracefile_size) { |
| 145 | DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name); |
| 146 | } else { |
| 147 | DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name); |
| 148 | } |
| 149 | |
| 150 | if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) { |
| 151 | stream->is_metadata = 1; |
| 152 | } |
| 153 | |
| 154 | stream->in_recv_list = true; |
| 155 | |
| 156 | /* |
| 157 | * Add the stream in the recv list of the session. Once the end stream |
| 158 | * message is received, all session streams are published. |
| 159 | */ |
| 160 | pthread_mutex_lock(&session->recv_list_lock); |
| 161 | cds_list_add_rcu(&stream->recv_node, &session->recv_list); |
| 162 | session->stream_count++; |
| 163 | pthread_mutex_unlock(&session->recv_list_lock); |
| 164 | |
| 165 | /* |
| 166 | * Both in the ctf_trace object and the global stream ht since the data |
| 167 | * side of the relayd does not have the concept of session. |
| 168 | */ |
| 169 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); |
| 170 | stream->in_stream_ht = true; |
| 171 | |
| 172 | DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name, |
| 173 | stream->stream_handle); |
| 174 | ret = 0; |
| 175 | |
| 176 | end: |
| 177 | if (ret) { |
| 178 | if (stream->stream_fd) { |
| 179 | stream_fd_put(stream->stream_fd); |
| 180 | stream->stream_fd = NULL; |
| 181 | } |
| 182 | stream_put(stream); |
| 183 | stream = NULL; |
| 184 | } |
| 185 | return stream; |
| 186 | |
| 187 | error_no_alloc: |
| 188 | /* |
| 189 | * path_name and channel_name need to be freed explicitly here |
| 190 | * because we cannot rely on stream_put(). |
| 191 | */ |
| 192 | free(path_name); |
| 193 | free(channel_name); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Called with the session lock held. |
| 199 | */ |
| 200 | void stream_publish(struct relay_stream *stream) |
| 201 | { |
| 202 | struct relay_session *session; |
| 203 | |
| 204 | pthread_mutex_lock(&stream->lock); |
| 205 | if (stream->published) { |
| 206 | goto unlock; |
| 207 | } |
| 208 | |
| 209 | session = stream->trace->session; |
| 210 | |
| 211 | pthread_mutex_lock(&session->recv_list_lock); |
| 212 | if (stream->in_recv_list) { |
| 213 | cds_list_del_rcu(&stream->recv_node); |
| 214 | stream->in_recv_list = false; |
| 215 | } |
| 216 | pthread_mutex_unlock(&session->recv_list_lock); |
| 217 | |
| 218 | pthread_mutex_lock(&stream->trace->stream_list_lock); |
| 219 | cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list); |
| 220 | pthread_mutex_unlock(&stream->trace->stream_list_lock); |
| 221 | |
| 222 | stream->published = true; |
| 223 | unlock: |
| 224 | pthread_mutex_unlock(&stream->lock); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Stream must be protected by holding the stream lock or by virtue of being |
| 229 | * called from stream_destroy, in which case it is guaranteed to be accessed |
| 230 | * from a single thread by the reflock. |
| 231 | */ |
| 232 | static void stream_unpublish(struct relay_stream *stream) |
| 233 | { |
| 234 | if (stream->in_stream_ht) { |
| 235 | struct lttng_ht_iter iter; |
| 236 | int ret; |
| 237 | |
| 238 | iter.iter.node = &stream->node.node; |
| 239 | ret = lttng_ht_del(relay_streams_ht, &iter); |
| 240 | assert(!ret); |
| 241 | stream->in_stream_ht = false; |
| 242 | } |
| 243 | if (stream->published) { |
| 244 | pthread_mutex_lock(&stream->trace->stream_list_lock); |
| 245 | cds_list_del_rcu(&stream->stream_node); |
| 246 | pthread_mutex_unlock(&stream->trace->stream_list_lock); |
| 247 | stream->published = false; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static void stream_destroy(struct relay_stream *stream) |
| 252 | { |
| 253 | if (stream->indexes_ht) { |
| 254 | /* |
| 255 | * Calling lttng_ht_destroy in call_rcu worker thread so |
| 256 | * we don't hold the RCU read-side lock while calling |
| 257 | * it. |
| 258 | */ |
| 259 | lttng_ht_destroy(stream->indexes_ht); |
| 260 | } |
| 261 | if (stream->tfa) { |
| 262 | tracefile_array_destroy(stream->tfa); |
| 263 | } |
| 264 | free(stream->path_name); |
| 265 | free(stream->channel_name); |
| 266 | free(stream); |
| 267 | } |
| 268 | |
| 269 | static void stream_destroy_rcu(struct rcu_head *rcu_head) |
| 270 | { |
| 271 | struct relay_stream *stream = |
| 272 | caa_container_of(rcu_head, struct relay_stream, rcu_node); |
| 273 | |
| 274 | stream_destroy(stream); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * No need to take stream->lock since this is only called on the final |
| 279 | * stream_put which ensures that a single thread may act on the stream. |
| 280 | * |
| 281 | * At that point, the object is also protected by the reflock which |
| 282 | * guarantees that no other thread may share ownership of this stream. |
| 283 | */ |
| 284 | static void stream_release(struct urcu_ref *ref) |
| 285 | { |
| 286 | struct relay_stream *stream = |
| 287 | caa_container_of(ref, struct relay_stream, ref); |
| 288 | struct relay_session *session; |
| 289 | |
| 290 | session = stream->trace->session; |
| 291 | |
| 292 | DBG("Releasing stream id %" PRIu64, stream->stream_handle); |
| 293 | |
| 294 | pthread_mutex_lock(&session->recv_list_lock); |
| 295 | session->stream_count--; |
| 296 | if (stream->in_recv_list) { |
| 297 | cds_list_del_rcu(&stream->recv_node); |
| 298 | stream->in_recv_list = false; |
| 299 | } |
| 300 | pthread_mutex_unlock(&session->recv_list_lock); |
| 301 | |
| 302 | stream_unpublish(stream); |
| 303 | |
| 304 | if (stream->stream_fd) { |
| 305 | stream_fd_put(stream->stream_fd); |
| 306 | stream->stream_fd = NULL; |
| 307 | } |
| 308 | if (stream->index_file) { |
| 309 | lttng_index_file_put(stream->index_file); |
| 310 | stream->index_file = NULL; |
| 311 | } |
| 312 | if (stream->trace) { |
| 313 | ctf_trace_put(stream->trace); |
| 314 | stream->trace = NULL; |
| 315 | } |
| 316 | |
| 317 | call_rcu(&stream->rcu_node, stream_destroy_rcu); |
| 318 | } |
| 319 | |
| 320 | void stream_put(struct relay_stream *stream) |
| 321 | { |
| 322 | DBG("stream put for stream id %" PRIu64, stream->stream_handle); |
| 323 | /* |
| 324 | * Ensure existence of stream->reflock for stream unlock. |
| 325 | */ |
| 326 | rcu_read_lock(); |
| 327 | /* |
| 328 | * Stream reflock ensures that concurrent test and update of |
| 329 | * stream ref is atomic. |
| 330 | */ |
| 331 | pthread_mutex_lock(&stream->reflock); |
| 332 | assert(stream->ref.refcount != 0); |
| 333 | /* |
| 334 | * Wait until we have processed all the stream packets before |
| 335 | * actually putting our last stream reference. |
| 336 | */ |
| 337 | DBG("stream put stream id %" PRIu64 " refcount %d", |
| 338 | stream->stream_handle, |
| 339 | (int) stream->ref.refcount); |
| 340 | urcu_ref_put(&stream->ref, stream_release); |
| 341 | pthread_mutex_unlock(&stream->reflock); |
| 342 | rcu_read_unlock(); |
| 343 | } |
| 344 | |
| 345 | void try_stream_close(struct relay_stream *stream) |
| 346 | { |
| 347 | bool session_aborted; |
| 348 | struct relay_session *session = stream->trace->session; |
| 349 | |
| 350 | DBG("Trying to close stream %" PRIu64, stream->stream_handle); |
| 351 | |
| 352 | pthread_mutex_lock(&session->lock); |
| 353 | session_aborted = session->aborted; |
| 354 | pthread_mutex_unlock(&session->lock); |
| 355 | |
| 356 | pthread_mutex_lock(&stream->lock); |
| 357 | /* |
| 358 | * Can be called concurently by connection close and reception of last |
| 359 | * pending data. |
| 360 | */ |
| 361 | if (stream->closed) { |
| 362 | pthread_mutex_unlock(&stream->lock); |
| 363 | DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | stream->close_requested = true; |
| 368 | |
| 369 | if (stream->last_net_seq_num == -1ULL) { |
| 370 | /* |
| 371 | * Handle connection close without explicit stream close |
| 372 | * command. |
| 373 | * |
| 374 | * We can be clever about indexes partially received in |
| 375 | * cases where we received the data socket part, but not |
| 376 | * the control socket part: since we're currently closing |
| 377 | * the stream on behalf of the control socket, we *know* |
| 378 | * there won't be any more control information for this |
| 379 | * socket. Therefore, we can destroy all indexes for |
| 380 | * which we have received only the file descriptor (from |
| 381 | * data socket). This takes care of consumerd crashes |
| 382 | * between sending the data and control information for |
| 383 | * a packet. Since those are sent in that order, we take |
| 384 | * care of consumerd crashes. |
| 385 | */ |
| 386 | relay_index_close_partial_fd(stream); |
| 387 | /* |
| 388 | * Use the highest net_seq_num we currently have pending |
| 389 | * As end of stream indicator. Leave last_net_seq_num |
| 390 | * at -1ULL if we cannot find any index. |
| 391 | */ |
| 392 | stream->last_net_seq_num = relay_index_find_last(stream); |
| 393 | /* Fall-through into the next check. */ |
| 394 | } |
| 395 | |
| 396 | if (stream->last_net_seq_num != -1ULL && |
| 397 | ((int64_t) (stream->prev_seq - stream->last_net_seq_num)) < 0 |
| 398 | && !session_aborted) { |
| 399 | /* |
| 400 | * Don't close since we still have data pending. This |
| 401 | * handles cases where an explicit close command has |
| 402 | * been received for this stream, and cases where the |
| 403 | * connection has been closed, and we are awaiting for |
| 404 | * index information from the data socket. It is |
| 405 | * therefore expected that all the index fd information |
| 406 | * we need has already been received on the control |
| 407 | * socket. Matching index information from data socket |
| 408 | * should be Expected Soon(TM). |
| 409 | * |
| 410 | * TODO: We should implement a timer to garbage collect |
| 411 | * streams after a timeout to be resilient against a |
| 412 | * consumerd implementation that would not match this |
| 413 | * expected behavior. |
| 414 | */ |
| 415 | pthread_mutex_unlock(&stream->lock); |
| 416 | DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle); |
| 417 | return; |
| 418 | } |
| 419 | /* |
| 420 | * We received all the indexes we can expect. |
| 421 | */ |
| 422 | stream_unpublish(stream); |
| 423 | stream->closed = true; |
| 424 | /* Relay indexes are only used by the "consumer/sessiond" end. */ |
| 425 | relay_index_close_all(stream); |
| 426 | pthread_mutex_unlock(&stream->lock); |
| 427 | DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle); |
| 428 | stream_put(stream); |
| 429 | } |
| 430 | |
| 431 | static void print_stream_indexes(struct relay_stream *stream) |
| 432 | { |
| 433 | struct lttng_ht_iter iter; |
| 434 | struct relay_index *index; |
| 435 | |
| 436 | rcu_read_lock(); |
| 437 | cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index, |
| 438 | index_n.node) { |
| 439 | DBG("index %p net_seq_num %" PRIu64 " refcount %ld" |
| 440 | " stream %" PRIu64 " trace %" PRIu64 |
| 441 | " session %" PRIu64, |
| 442 | index, |
| 443 | index->index_n.key, |
| 444 | stream->ref.refcount, |
| 445 | index->stream->stream_handle, |
| 446 | index->stream->trace->id, |
| 447 | index->stream->trace->session->id); |
| 448 | } |
| 449 | rcu_read_unlock(); |
| 450 | } |
| 451 | |
| 452 | void print_relay_streams(void) |
| 453 | { |
| 454 | struct lttng_ht_iter iter; |
| 455 | struct relay_stream *stream; |
| 456 | |
| 457 | if (!relay_streams_ht) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | rcu_read_lock(); |
| 462 | cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream, |
| 463 | node.node) { |
| 464 | if (!stream_get(stream)) { |
| 465 | continue; |
| 466 | } |
| 467 | DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 |
| 468 | " session %" PRIu64, |
| 469 | stream, |
| 470 | stream->ref.refcount, |
| 471 | stream->stream_handle, |
| 472 | stream->trace->id, |
| 473 | stream->trace->session->id); |
| 474 | print_stream_indexes(stream); |
| 475 | stream_put(stream); |
| 476 | } |
| 477 | rcu_read_unlock(); |
| 478 | } |