Commit | Line | Data |
---|---|---|
2f8f53af | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
2f8f53af | 5 | * |
ab5be9fa | 6 | * SPDX-License-Identifier: GPL-2.0-only |
2f8f53af | 7 | * |
2f8f53af DG |
8 | */ |
9 | ||
6c1c0768 | 10 | #define _LGPL_SOURCE |
28ab034a JG |
11 | #include "lttng-relayd.hpp" |
12 | #include "viewer-stream.hpp" | |
13 | ||
c9e313bc | 14 | #include <common/common.hpp> |
c9e313bc | 15 | #include <common/compat/string.hpp> |
28ab034a | 16 | #include <common/index/index.hpp> |
56047f5a | 17 | #include <common/urcu.hpp> |
c9e313bc | 18 | #include <common/utils.hpp> |
2f8f53af | 19 | |
28ab034a JG |
20 | #include <algorithm> |
21 | #include <fcntl.h> | |
22 | #include <sys/stat.h> | |
23 | #include <sys/types.h> | |
2f8f53af | 24 | |
b9973dea | 25 | static void viewer_stream_release_composite_objects(struct relay_viewer_stream *vstream) |
2f8f53af | 26 | { |
b9973dea MD |
27 | if (vstream->stream_file.handle) { |
28 | fs_handle_close(vstream->stream_file.handle); | |
cd9adb8b | 29 | vstream->stream_file.handle = nullptr; |
b9973dea MD |
30 | } |
31 | if (vstream->index_file) { | |
32 | lttng_index_file_put(vstream->index_file); | |
cd9adb8b | 33 | vstream->index_file = nullptr; |
b9973dea MD |
34 | } |
35 | if (vstream->stream) { | |
36 | stream_put(vstream->stream); | |
cd9adb8b | 37 | vstream->stream = nullptr; |
b9973dea | 38 | } |
80516611 | 39 | lttng_trace_chunk_put(vstream->stream_file.trace_chunk); |
cd9adb8b | 40 | vstream->stream_file.trace_chunk = nullptr; |
b9973dea MD |
41 | } |
42 | ||
43 | static void viewer_stream_destroy(struct relay_viewer_stream *vstream) | |
44 | { | |
7591bab1 MD |
45 | free(vstream->path_name); |
46 | free(vstream->channel_name); | |
47 | free(vstream); | |
2f8f53af DG |
48 | } |
49 | ||
7591bab1 | 50 | static void viewer_stream_destroy_rcu(struct rcu_head *head) |
2f8f53af | 51 | { |
7591bab1 | 52 | struct relay_viewer_stream *vstream = |
0114db0e | 53 | lttng::utils::container_of(head, &relay_viewer_stream::rcu_node); |
2f8f53af | 54 | |
7591bab1 | 55 | viewer_stream_destroy(vstream); |
2f8f53af DG |
56 | } |
57 | ||
9edaf114 | 58 | /* Relay stream's lock must be held by the caller. */ |
2f8f53af | 59 | struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream, |
28ab034a JG |
60 | struct lttng_trace_chunk *trace_chunk, |
61 | enum lttng_viewer_seek seek_t) | |
2f8f53af | 62 | { |
cd9adb8b | 63 | struct relay_viewer_stream *vstream = nullptr; |
ebb29c10 | 64 | |
9edaf114 | 65 | ASSERT_LOCKED(stream->lock); |
2f8f53af | 66 | |
64803277 | 67 | vstream = zmalloc<relay_viewer_stream>(); |
2f8f53af DG |
68 | if (!vstream) { |
69 | PERROR("relay viewer stream zmalloc"); | |
70 | goto error; | |
71 | } | |
80516611 | 72 | if (trace_chunk) { |
28ab034a | 73 | const bool acquired_reference = lttng_trace_chunk_get(trace_chunk); |
80516611 | 74 | |
a0377dfe | 75 | LTTNG_ASSERT(acquired_reference); |
80516611 JG |
76 | } |
77 | ||
9edaf114 | 78 | vstream->stream_file.trace_chunk = trace_chunk; |
f5436bfc | 79 | vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX); |
cd9adb8b | 80 | if (vstream->path_name == nullptr) { |
b272577e MD |
81 | PERROR("relay viewer path_name alloc"); |
82 | goto error; | |
83 | } | |
28ab034a | 84 | vstream->channel_name = lttng_strndup(stream->channel_name, LTTNG_VIEWER_NAME_MAX); |
cd9adb8b | 85 | if (vstream->channel_name == nullptr) { |
b272577e MD |
86 | PERROR("relay viewer channel_name alloc"); |
87 | goto error; | |
88 | } | |
2f8f53af | 89 | |
a44ca2ca MD |
90 | if (!stream_get(stream)) { |
91 | ERR("Cannot get stream"); | |
92 | goto error; | |
93 | } | |
94 | vstream->stream = stream; | |
95 | ||
a44ca2ca MD |
96 | if (stream->is_metadata && stream->trace->viewer_metadata_stream) { |
97 | ERR("Cannot attach viewer metadata stream to trace (busy)."); | |
71381736 | 98 | goto error; |
a44ca2ca MD |
99 | } |
100 | ||
2f8f53af | 101 | switch (seek_t) { |
c4e361a4 | 102 | case LTTNG_VIEWER_SEEK_BEGINNING: |
a44ca2ca MD |
103 | { |
104 | uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); | |
105 | ||
106 | if (seq_tail == -1ULL) { | |
107 | /* | |
108 | * Tail may not be initialized yet. Nonetheless, we know | |
109 | * we want to send the first index once it becomes | |
110 | * available. | |
111 | */ | |
112 | seq_tail = 0; | |
113 | } | |
28ab034a | 114 | vstream->current_tracefile_id = tracefile_array_get_file_index_tail(stream->tfa); |
a44ca2ca | 115 | vstream->index_sent_seqcount = seq_tail; |
2f8f53af | 116 | break; |
a44ca2ca | 117 | } |
c4e361a4 | 118 | case LTTNG_VIEWER_SEEK_LAST: |
a44ca2ca | 119 | vstream->current_tracefile_id = |
78118e3b | 120 | tracefile_array_get_read_file_index_head(stream->tfa); |
a44ca2ca MD |
121 | /* |
122 | * We seek at the very end of each stream, awaiting for | |
123 | * a future packet to eventually come in. | |
124 | * | |
125 | * We don't need to check the head position for -1ULL since the | |
126 | * increment will set it to 0. | |
127 | */ | |
28ab034a | 128 | vstream->index_sent_seqcount = tracefile_array_get_seq_head(stream->tfa) + 1; |
2f8f53af DG |
129 | break; |
130 | default: | |
71381736 | 131 | goto error; |
2f8f53af | 132 | } |
2f8f53af | 133 | |
2f8f53af | 134 | /* |
a44ca2ca MD |
135 | * If we never received an index for the current stream, delay |
136 | * the opening of the index, otherwise open it right now. | |
2f8f53af | 137 | */ |
cd9adb8b JG |
138 | if (stream->index_file == nullptr) { |
139 | vstream->index_file = nullptr; | |
0eafad74 | 140 | } else if (vstream->stream_file.trace_chunk) { |
ebb29c10 JG |
141 | const uint32_t connection_major = stream->trace->session->major; |
142 | const uint32_t connection_minor = stream->trace->session->minor; | |
3ff5c5db | 143 | enum lttng_trace_chunk_status chunk_status; |
ebb29c10 | 144 | |
3ff5c5db | 145 | chunk_status = lttng_index_file_create_from_trace_chunk_read_only( |
28ab034a JG |
146 | vstream->stream_file.trace_chunk, |
147 | stream->path_name, | |
148 | stream->channel_name, | |
149 | stream->tracefile_size, | |
150 | vstream->current_tracefile_id, | |
151 | lttng_to_index_major(connection_major, connection_minor), | |
152 | lttng_to_index_minor(connection_major, connection_minor), | |
153 | true, | |
154 | &vstream->index_file); | |
3ff5c5db MD |
155 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
156 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) { | |
cd9adb8b | 157 | vstream->index_file = nullptr; |
3ff5c5db | 158 | } else { |
71381736 | 159 | goto error; |
3ff5c5db | 160 | } |
2f8f53af | 161 | } |
2f8f53af DG |
162 | } |
163 | ||
b0d240a2 MD |
164 | /* |
165 | * If we never received a data file for the current stream, delay the | |
166 | * opening, otherwise open it right now. | |
167 | */ | |
0eafad74 | 168 | if (stream->file && vstream->stream_file.trace_chunk) { |
8bb66c3c | 169 | int ret; |
b0d240a2 MD |
170 | char file_path[LTTNG_PATH_MAX]; |
171 | enum lttng_trace_chunk_status status; | |
172 | ||
173 | ret = utils_stream_file_path(stream->path_name, | |
28ab034a JG |
174 | stream->channel_name, |
175 | stream->tracefile_size, | |
176 | vstream->current_tracefile_id, | |
cd9adb8b | 177 | nullptr, |
28ab034a JG |
178 | file_path, |
179 | sizeof(file_path)); | |
b0d240a2 | 180 | if (ret < 0) { |
71381736 | 181 | goto error; |
b0d240a2 MD |
182 | } |
183 | ||
28ab034a JG |
184 | status = lttng_trace_chunk_open_fs_handle(vstream->stream_file.trace_chunk, |
185 | file_path, | |
186 | O_RDONLY, | |
187 | 0, | |
188 | &vstream->stream_file.handle, | |
189 | true); | |
b0d240a2 | 190 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
71381736 | 191 | goto error; |
b0d240a2 | 192 | } |
b0d240a2 MD |
193 | } |
194 | ||
f8f3885c | 195 | if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) { |
2f8f53af DG |
196 | off_t lseek_ret; |
197 | ||
28ab034a | 198 | lseek_ret = fs_handle_seek(vstream->index_file->file, 0, SEEK_END); |
2f8f53af | 199 | if (lseek_ret < 0) { |
71381736 | 200 | goto error; |
2f8f53af | 201 | } |
7591bab1 | 202 | } |
7591bab1 | 203 | if (stream->is_metadata) { |
28ab034a | 204 | rcu_assign_pointer(stream->trace->viewer_metadata_stream, vstream); |
2f8f53af DG |
205 | } |
206 | ||
80516611 JG |
207 | vstream->last_seen_rotation_count = stream->completed_rotation_count; |
208 | ||
7591bab1 MD |
209 | /* Globally visible after the add unique. */ |
210 | lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle); | |
7591bab1 | 211 | urcu_ref_init(&vstream->ref); |
c51b2177 | 212 | lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n); |
2f8f53af DG |
213 | return vstream; |
214 | ||
215 | error: | |
216 | if (vstream) { | |
b9973dea MD |
217 | /* Not using `put` since vstream is assumed to be published. */ |
218 | viewer_stream_release_composite_objects(vstream); | |
7591bab1 | 219 | viewer_stream_destroy(vstream); |
2f8f53af | 220 | } |
cd9adb8b | 221 | return nullptr; |
2f8f53af DG |
222 | } |
223 | ||
7591bab1 | 224 | static void viewer_stream_unpublish(struct relay_viewer_stream *vstream) |
2f8f53af DG |
225 | { |
226 | int ret; | |
227 | struct lttng_ht_iter iter; | |
228 | ||
7591bab1 | 229 | iter.iter.node = &vstream->stream_n.node; |
2f8f53af | 230 | ret = lttng_ht_del(viewer_streams_ht, &iter); |
a0377dfe | 231 | LTTNG_ASSERT(!ret); |
2f8f53af DG |
232 | } |
233 | ||
7591bab1 | 234 | static void viewer_stream_release(struct urcu_ref *ref) |
2f8f53af | 235 | { |
1b2a5aac | 236 | auto *vstream = lttng::utils::container_of(ref, &relay_viewer_stream::ref); |
2a174661 | 237 | |
7591bab1 MD |
238 | if (vstream->stream->is_metadata) { |
239 | rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL); | |
2a174661 | 240 | } |
7591bab1 | 241 | viewer_stream_unpublish(vstream); |
b9973dea | 242 | viewer_stream_release_composite_objects(vstream); |
7591bab1 MD |
243 | call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu); |
244 | } | |
245 | ||
246 | /* Must be called with RCU read-side lock held. */ | |
247 | bool viewer_stream_get(struct relay_viewer_stream *vstream) | |
248 | { | |
ce4d4083 | 249 | return urcu_ref_get_unless_zero(&vstream->ref); |
2f8f53af DG |
250 | } |
251 | ||
252 | /* | |
7591bab1 | 253 | * Get viewer stream by id. |
2f8f53af | 254 | * |
7591bab1 | 255 | * Return viewer stream if found else NULL. |
2f8f53af | 256 | */ |
7591bab1 | 257 | struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id) |
2f8f53af DG |
258 | { |
259 | struct lttng_ht_node_u64 *node; | |
260 | struct lttng_ht_iter iter; | |
cd9adb8b | 261 | struct relay_viewer_stream *vstream = nullptr; |
2f8f53af | 262 | |
07c4863f | 263 | const lttng::urcu::read_lock_guard read_lock; |
2f8f53af | 264 | lttng_ht_lookup(viewer_streams_ht, &id, &iter); |
00d7d903 | 265 | node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter); |
2f8f53af DG |
266 | if (!node) { |
267 | DBG("Relay viewer stream %" PRIu64 " not found", id); | |
268 | goto end; | |
269 | } | |
0114db0e | 270 | vstream = lttng::utils::container_of(node, &relay_viewer_stream::stream_n); |
7591bab1 | 271 | if (!viewer_stream_get(vstream)) { |
cd9adb8b | 272 | vstream = nullptr; |
7591bab1 | 273 | } |
2f8f53af | 274 | end: |
7591bab1 MD |
275 | return vstream; |
276 | } | |
277 | ||
278 | void viewer_stream_put(struct relay_viewer_stream *vstream) | |
279 | { | |
07c4863f | 280 | const lttng::urcu::read_lock_guard read_lock; |
7591bab1 | 281 | urcu_ref_put(&vstream->ref, viewer_stream_release); |
7591bab1 MD |
282 | } |
283 | ||
3087b021 MD |
284 | void viewer_stream_close_files(struct relay_viewer_stream *vstream) |
285 | { | |
286 | if (vstream->index_file) { | |
287 | lttng_index_file_put(vstream->index_file); | |
cd9adb8b | 288 | vstream->index_file = nullptr; |
3087b021 | 289 | } |
8bb66c3c | 290 | if (vstream->stream_file.handle) { |
28ab034a | 291 | fs_handle_close(vstream->stream_file.handle); |
cd9adb8b | 292 | vstream->stream_file.handle = nullptr; |
3087b021 MD |
293 | } |
294 | } | |
295 | ||
296 | void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream *vstream) | |
297 | { | |
298 | const struct relay_stream *stream = vstream->stream; | |
299 | uint64_t seq_tail; | |
300 | ||
301 | vstream->current_tracefile_id = tracefile_array_get_file_index_tail(stream->tfa); | |
302 | seq_tail = tracefile_array_get_seq_tail(stream->tfa); | |
303 | if (seq_tail == -1ULL) { | |
304 | seq_tail = 0; | |
305 | } | |
7fb6d478 MD |
306 | |
307 | /* | |
308 | * Move the index sent seqcount forward if it was lagging behind | |
309 | * the new tail of the tracefile array. If the current | |
310 | * index_sent_seqcount is already further than the tracefile | |
311 | * array tail position, keep its current position. | |
312 | */ | |
28ab034a | 313 | vstream->index_sent_seqcount = std::max(seq_tail, vstream->index_sent_seqcount); |
3087b021 MD |
314 | } |
315 | ||
2f8f53af DG |
316 | /* |
317 | * Rotate a stream to the next tracefile. | |
318 | * | |
7591bab1 | 319 | * Must be called with the rstream lock held. |
b0d240a2 | 320 | * Returns 0 on success, 1 on EOF. |
2f8f53af | 321 | */ |
7591bab1 | 322 | int viewer_stream_rotate(struct relay_viewer_stream *vstream) |
2f8f53af DG |
323 | { |
324 | int ret; | |
a44ca2ca | 325 | uint64_t new_id; |
ebb29c10 | 326 | const struct relay_stream *stream = vstream->stream; |
2f8f53af | 327 | |
7591bab1 | 328 | /* Detect the last tracefile to open. */ |
28ab034a JG |
329 | if (stream->index_received_seqcount == vstream->index_sent_seqcount && |
330 | stream->trace->session->connection_closed) { | |
7591bab1 | 331 | ret = 1; |
2a174661 DG |
332 | goto end; |
333 | } | |
2f8f53af | 334 | |
7591bab1 MD |
335 | if (stream->tracefile_count == 0) { |
336 | /* Ignore rotation, there is none to do. */ | |
337 | ret = 0; | |
2f8f53af DG |
338 | goto end; |
339 | } | |
340 | ||
a44ca2ca MD |
341 | /* |
342 | * Try to move to the next file. | |
343 | */ | |
28ab034a JG |
344 | new_id = (vstream->current_tracefile_id + 1) % stream->tracefile_count; |
345 | if (tracefile_array_seq_in_file(stream->tfa, new_id, vstream->index_sent_seqcount)) { | |
a44ca2ca | 346 | vstream->current_tracefile_id = new_id; |
2f8f53af | 347 | } else { |
07c4863f | 348 | const uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa); |
a44ca2ca MD |
349 | |
350 | /* | |
351 | * This can only be reached on overwrite, which implies there | |
352 | * has been data written at some point, which will have set the | |
353 | * tail. | |
354 | */ | |
a0377dfe | 355 | LTTNG_ASSERT(seq_tail != -1ULL); |
a44ca2ca MD |
356 | /* |
357 | * We need to resync because we lag behind tail. | |
358 | */ | |
28ab034a | 359 | vstream->current_tracefile_id = tracefile_array_get_file_index_tail(stream->tfa); |
a44ca2ca | 360 | vstream->index_sent_seqcount = seq_tail; |
2f8f53af | 361 | } |
b0d240a2 MD |
362 | viewer_stream_close_files(vstream); |
363 | ret = 0; | |
2f8f53af | 364 | end: |
2f8f53af DG |
365 | return ret; |
366 | } | |
7591bab1 | 367 | |
cd9adb8b | 368 | void print_viewer_streams() |
7591bab1 | 369 | { |
ce3f3ba3 JG |
370 | if (!viewer_streams_ht) { |
371 | return; | |
372 | } | |
373 | ||
54986183 JG |
374 | for (auto *vstream : |
375 | lttng::urcu::lfht_iteration_adapter<relay_viewer_stream, | |
376 | decltype(relay_viewer_stream::stream_n), | |
377 | &relay_viewer_stream::stream_n>( | |
378 | *viewer_streams_ht->ht)) { | |
379 | if (!viewer_stream_get(vstream)) { | |
380 | continue; | |
7591bab1 | 381 | } |
54986183 JG |
382 | |
383 | DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 " session %" PRIu64, | |
384 | vstream, | |
385 | vstream->ref.refcount, | |
386 | vstream->stream->stream_handle, | |
387 | vstream->stream->trace->id, | |
388 | vstream->stream->trace->session->id); | |
389 | viewer_stream_put(vstream); | |
7591bab1 | 390 | } |
7591bab1 | 391 | } |