Bump minimum kernel version to 2.6.30 to use EFD_SEMAPHORE
[lttng-tools.git] / src / bin / lttng-relayd / viewer-stream.cpp
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.hpp>
12 #include <common/index/index.hpp>
13 #include <common/compat/string.hpp>
14 #include <common/utils.hpp>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <algorithm>
19
20 #include "lttng-relayd.hpp"
21 #include "viewer-stream.hpp"
22
23 static void viewer_stream_release_composite_objects(struct relay_viewer_stream *vstream)
24 {
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 }
37 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
38 vstream->stream_file.trace_chunk = NULL;
39 }
40
41 static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
42 {
43 free(vstream->path_name);
44 free(vstream->channel_name);
45 free(vstream);
46 }
47
48 static void viewer_stream_destroy_rcu(struct rcu_head *head)
49 {
50 struct relay_viewer_stream *vstream =
51 caa_container_of(head, struct relay_viewer_stream, rcu_node);
52
53 viewer_stream_destroy(vstream);
54 }
55
56 /* Relay stream's lock must be held by the caller. */
57 struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
58 struct lttng_trace_chunk *trace_chunk,
59 enum lttng_viewer_seek seek_t)
60 {
61 struct relay_viewer_stream *vstream = NULL;
62
63 ASSERT_LOCKED(stream->lock);
64
65 vstream = zmalloc<relay_viewer_stream>();
66 if (!vstream) {
67 PERROR("relay viewer stream zmalloc");
68 goto error;
69 }
70
71 if (trace_chunk) {
72 const bool acquired_reference = lttng_trace_chunk_get(
73 trace_chunk);
74
75 LTTNG_ASSERT(acquired_reference);
76 }
77
78 vstream->stream_file.trace_chunk = trace_chunk;
79 vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
80 if (vstream->path_name == NULL) {
81 PERROR("relay viewer path_name alloc");
82 goto error;
83 }
84 vstream->channel_name = lttng_strndup(stream->channel_name,
85 LTTNG_VIEWER_NAME_MAX);
86 if (vstream->channel_name == NULL) {
87 PERROR("relay viewer channel_name alloc");
88 goto error;
89 }
90
91 if (!stream_get(stream)) {
92 ERR("Cannot get stream");
93 goto error;
94 }
95 vstream->stream = stream;
96
97 if (stream->is_metadata && stream->trace->viewer_metadata_stream) {
98 ERR("Cannot attach viewer metadata stream to trace (busy).");
99 goto error;
100 }
101
102 switch (seek_t) {
103 case LTTNG_VIEWER_SEEK_BEGINNING:
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;
118 break;
119 }
120 case LTTNG_VIEWER_SEEK_LAST:
121 vstream->current_tracefile_id =
122 tracefile_array_get_read_file_index_head(stream->tfa);
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;
132 break;
133 default:
134 goto error;
135 }
136
137 /*
138 * If we never received an index for the current stream, delay
139 * the opening of the index, otherwise open it right now.
140 */
141 if (stream->index_file == NULL) {
142 vstream->index_file = NULL;
143 } else if (vstream->stream_file.trace_chunk) {
144 const uint32_t connection_major = stream->trace->session->major;
145 const uint32_t connection_minor = stream->trace->session->minor;
146 enum lttng_trace_chunk_status chunk_status;
147
148 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
149 vstream->stream_file.trace_chunk,
150 stream->path_name,
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,
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 {
162 goto error;
163 }
164 }
165 }
166
167 /*
168 * If we never received a data file for the current stream, delay the
169 * opening, otherwise open it right now.
170 */
171 if (stream->file && vstream->stream_file.trace_chunk) {
172 int ret;
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) {
181 goto error;
182 }
183
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);
188 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
189 goto error;
190 }
191 }
192
193 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) {
194 off_t lseek_ret;
195
196 lseek_ret = fs_handle_seek(
197 vstream->index_file->file, 0, SEEK_END);
198 if (lseek_ret < 0) {
199 goto error;
200 }
201 }
202 if (stream->is_metadata) {
203 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
204 vstream);
205 }
206
207 vstream->last_seen_rotation_count = stream->completed_rotation_count;
208
209 /* Globally visible after the add unique. */
210 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
211 urcu_ref_init(&vstream->ref);
212 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
213
214 return vstream;
215
216 error:
217 if (vstream) {
218 /* Not using `put` since vstream is assumed to be published. */
219 viewer_stream_release_composite_objects(vstream);
220 viewer_stream_destroy(vstream);
221 }
222 return NULL;
223 }
224
225 static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
226 {
227 int ret;
228 struct lttng_ht_iter iter;
229
230 iter.iter.node = &vstream->stream_n.node;
231 ret = lttng_ht_del(viewer_streams_ht, &iter);
232 LTTNG_ASSERT(!ret);
233 }
234
235 static void viewer_stream_release(struct urcu_ref *ref)
236 {
237 struct relay_viewer_stream *vstream = caa_container_of(ref,
238 struct relay_viewer_stream, ref);
239
240 if (vstream->stream->is_metadata) {
241 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
242 }
243 viewer_stream_unpublish(vstream);
244 viewer_stream_release_composite_objects(vstream);
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 {
251 return urcu_ref_get_unless_zero(&vstream->ref);
252 }
253
254 /*
255 * Get viewer stream by id.
256 *
257 * Return viewer stream if found else NULL.
258 */
259 struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
260 {
261 struct lttng_ht_node_u64 *node;
262 struct lttng_ht_iter iter;
263 struct relay_viewer_stream *vstream = NULL;
264
265 rcu_read_lock();
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 }
272 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
273 if (!viewer_stream_get(vstream)) {
274 vstream = NULL;
275 }
276 end:
277 rcu_read_unlock();
278 return vstream;
279 }
280
281 void viewer_stream_put(struct relay_viewer_stream *vstream)
282 {
283 rcu_read_lock();
284 urcu_ref_put(&vstream->ref, viewer_stream_release);
285 rcu_read_unlock();
286 }
287
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 }
294 if (vstream->stream_file.handle) {
295 fs_handle_close(vstream->stream_file.handle);
296 vstream->stream_file.handle = NULL;
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 }
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);
319 }
320
321 /*
322 * Rotate a stream to the next tracefile.
323 *
324 * Must be called with the rstream lock held.
325 * Returns 0 on success, 1 on EOF.
326 */
327 int viewer_stream_rotate(struct relay_viewer_stream *vstream)
328 {
329 int ret;
330 uint64_t new_id;
331 const struct relay_stream *stream = vstream->stream;
332
333 /* Detect the last tracefile to open. */
334 if (stream->index_received_seqcount
335 == vstream->index_sent_seqcount
336 && stream->trace->session->connection_closed) {
337 ret = 1;
338 goto end;
339 }
340
341 if (stream->tracefile_count == 0) {
342 /* Ignore rotation, there is none to do. */
343 ret = 0;
344 goto end;
345 }
346
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;
355 } else {
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 */
363 LTTNG_ASSERT(seq_tail != -1ULL);
364 /*
365 * We need to resync because we lag behind tail.
366 */
367 vstream->current_tracefile_id =
368 tracefile_array_get_file_index_tail(stream->tfa);
369 vstream->index_sent_seqcount = seq_tail;
370 }
371 viewer_stream_close_files(vstream);
372 ret = 0;
373 end:
374 return ret;
375 }
376
377 void print_viewer_streams(void)
378 {
379 struct lttng_ht_iter iter;
380 struct relay_viewer_stream *vstream;
381
382 if (!viewer_streams_ht) {
383 return;
384 }
385
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 }
This page took 0.03869 seconds and 4 git commands to generate.