05499215a8049e5e98e239b7e8f50d030230328e
[lttng-tools.git] / src / bin / lttng-relayd / viewer-stream.c
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <common/common.h>
12 #include <common/index/index.h>
13 #include <common/compat/string.h>
14 #include <common/utils.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18
19 #include "lttng-relayd.h"
20 #include "viewer-stream.h"
21
22 static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
23 {
24 free(vstream->path_name);
25 free(vstream->channel_name);
26 free(vstream);
27 }
28
29 static void viewer_stream_destroy_rcu(struct rcu_head *head)
30 {
31 struct relay_viewer_stream *vstream =
32 caa_container_of(head, struct relay_viewer_stream, rcu_node);
33
34 viewer_stream_destroy(vstream);
35 }
36
37 struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
38 struct lttng_trace_chunk *viewer_trace_chunk,
39 enum lttng_viewer_seek seek_t)
40 {
41 struct relay_viewer_stream *vstream = NULL;
42 const bool acquired_reference = lttng_trace_chunk_get(
43 viewer_trace_chunk);
44
45 if (!acquired_reference) {
46 goto error;
47 }
48
49 vstream = zmalloc(sizeof(*vstream));
50 if (!vstream) {
51 PERROR("relay viewer stream zmalloc");
52 goto error;
53 }
54
55 vstream->stream_file.trace_chunk = viewer_trace_chunk;
56 viewer_trace_chunk = NULL;
57 vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
58 if (vstream->path_name == NULL) {
59 PERROR("relay viewer path_name alloc");
60 goto error;
61 }
62 vstream->channel_name = lttng_strndup(stream->channel_name,
63 LTTNG_VIEWER_NAME_MAX);
64 if (vstream->channel_name == NULL) {
65 PERROR("relay viewer channel_name alloc");
66 goto error;
67 }
68
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
82 switch (seek_t) {
83 case LTTNG_VIEWER_SEEK_BEGINNING:
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;
98 break;
99 }
100 case LTTNG_VIEWER_SEEK_LAST:
101 vstream->current_tracefile_id =
102 tracefile_array_get_read_file_index_head(stream->tfa);
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;
112 break;
113 default:
114 goto error_unlock;
115 }
116
117 /*
118 * If we never received an index for the current stream, delay
119 * the opening of the index, otherwise open it right now.
120 */
121 if (stream->index_file == NULL) {
122 vstream->index_file = NULL;
123 } else {
124 const uint32_t connection_major = stream->trace->session->major;
125 const uint32_t connection_minor = stream->trace->session->minor;
126 enum lttng_trace_chunk_status chunk_status;
127
128 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
129 vstream->stream_file.trace_chunk,
130 stream->path_name,
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,
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 }
144 }
145 }
146
147 /*
148 * If we never received a data file for the current stream, delay the
149 * opening, otherwise open it right now.
150 */
151 if (stream->file) {
152 int ret;
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
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);
168 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
169 goto error_unlock;
170 }
171 }
172
173 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) {
174 off_t lseek_ret;
175
176 lseek_ret = fs_handle_seek(
177 vstream->index_file->file, 0, SEEK_END);
178 if (lseek_ret < 0) {
179 goto error_unlock;
180 }
181 }
182 if (stream->is_metadata) {
183 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
184 vstream);
185 }
186 pthread_mutex_unlock(&stream->lock);
187
188 /* Globally visible after the add unique. */
189 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
190 urcu_ref_init(&vstream->ref);
191 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
192
193 return vstream;
194
195 error_unlock:
196 pthread_mutex_unlock(&stream->lock);
197 error:
198 if (vstream) {
199 viewer_stream_destroy(vstream);
200 }
201 if (viewer_trace_chunk && acquired_reference) {
202 lttng_trace_chunk_put(viewer_trace_chunk);
203 }
204 return NULL;
205 }
206
207 static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
208 {
209 int ret;
210 struct lttng_ht_iter iter;
211
212 iter.iter.node = &vstream->stream_n.node;
213 ret = lttng_ht_del(viewer_streams_ht, &iter);
214 assert(!ret);
215 }
216
217 static void viewer_stream_release(struct urcu_ref *ref)
218 {
219 struct relay_viewer_stream *vstream = caa_container_of(ref,
220 struct relay_viewer_stream, ref);
221
222 if (vstream->stream->is_metadata) {
223 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
224 }
225
226 viewer_stream_unpublish(vstream);
227
228 if (vstream->stream_file.handle) {
229 fs_handle_close(vstream->stream_file.handle);
230 vstream->stream_file.handle = NULL;
231 }
232 if (vstream->index_file) {
233 lttng_index_file_put(vstream->index_file);
234 vstream->index_file = NULL;
235 }
236 if (vstream->stream) {
237 stream_put(vstream->stream);
238 vstream->stream = NULL;
239 }
240 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
241 vstream->stream_file.trace_chunk = NULL;
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 {
248 return urcu_ref_get_unless_zero(&vstream->ref);
249 }
250
251 /*
252 * Get viewer stream by id.
253 *
254 * Return viewer stream if found else NULL.
255 */
256 struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
257 {
258 struct lttng_ht_node_u64 *node;
259 struct lttng_ht_iter iter;
260 struct relay_viewer_stream *vstream = NULL;
261
262 rcu_read_lock();
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 }
269 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
270 if (!viewer_stream_get(vstream)) {
271 vstream = NULL;
272 }
273 end:
274 rcu_read_unlock();
275 return vstream;
276 }
277
278 void viewer_stream_put(struct relay_viewer_stream *vstream)
279 {
280 rcu_read_lock();
281 urcu_ref_put(&vstream->ref, viewer_stream_release);
282 rcu_read_unlock();
283 }
284
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 }
291 if (vstream->stream_file.handle) {
292 fs_handle_close(vstream->stream_file.handle);
293 vstream->stream_file.handle = NULL;
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
310 /*
311 * Rotate a stream to the next tracefile.
312 *
313 * Must be called with the rstream lock held.
314 * Returns 0 on success, 1 on EOF.
315 */
316 int viewer_stream_rotate(struct relay_viewer_stream *vstream)
317 {
318 int ret;
319 uint64_t new_id;
320 const struct relay_stream *stream = vstream->stream;
321
322 /* Detect the last tracefile to open. */
323 if (stream->index_received_seqcount
324 == vstream->index_sent_seqcount
325 && stream->trace->session->connection_closed) {
326 ret = 1;
327 goto end;
328 }
329
330 if (stream->tracefile_count == 0) {
331 /* Ignore rotation, there is none to do. */
332 ret = 0;
333 goto end;
334 }
335
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;
344 } else {
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 */
356 vstream->current_tracefile_id =
357 tracefile_array_get_file_index_tail(stream->tfa);
358 vstream->index_sent_seqcount = seq_tail;
359 }
360 viewer_stream_close_files(vstream);
361 ret = 0;
362 end:
363 return ret;
364 }
365
366 void print_viewer_streams(void)
367 {
368 struct lttng_ht_iter iter;
369 struct relay_viewer_stream *vstream;
370
371 if (!viewer_streams_ht) {
372 return;
373 }
374
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 }
This page took 0.037142 seconds and 4 git commands to generate.