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