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