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