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