relayd: live: implement support for clear feature
[lttng-tools.git] / src / bin / lttng-relayd / viewer-stream.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
20 #define _LGPL_SOURCE
21 #include <common/common.h>
22 #include <common/index/index.h>
23 #include <common/compat/string.h>
24 #include <common/utils.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #include "lttng-relayd.h"
30 #include "viewer-stream.h"
31
32 static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
33 {
34 free(vstream->path_name);
35 free(vstream->channel_name);
36 free(vstream);
37 }
38
39 static void viewer_stream_destroy_rcu(struct rcu_head *head)
40 {
41 struct relay_viewer_stream *vstream =
42 caa_container_of(head, struct relay_viewer_stream, rcu_node);
43
44 viewer_stream_destroy(vstream);
45 }
46
47 struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
48 struct lttng_trace_chunk *viewer_trace_chunk,
49 enum lttng_viewer_seek seek_t)
50 {
51 struct relay_viewer_stream *vstream = NULL;
52 const bool acquired_reference = lttng_trace_chunk_get(
53 viewer_trace_chunk);
54
55 if (!acquired_reference) {
56 goto error;
57 }
58
59 vstream = zmalloc(sizeof(*vstream));
60 if (!vstream) {
61 PERROR("relay viewer stream zmalloc");
62 goto error;
63 }
64
65 vstream->stream_file.trace_chunk = viewer_trace_chunk;
66 viewer_trace_chunk = NULL;
67 vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
68 if (vstream->path_name == NULL) {
69 PERROR("relay viewer path_name alloc");
70 goto error;
71 }
72 vstream->channel_name = lttng_strndup(stream->channel_name,
73 LTTNG_VIEWER_NAME_MAX);
74 if (vstream->channel_name == NULL) {
75 PERROR("relay viewer channel_name alloc");
76 goto error;
77 }
78
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
92 switch (seek_t) {
93 case LTTNG_VIEWER_SEEK_BEGINNING:
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;
108 break;
109 }
110 case LTTNG_VIEWER_SEEK_LAST:
111 vstream->current_tracefile_id =
112 tracefile_array_get_read_file_index_head(stream->tfa);
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;
122 break;
123 default:
124 goto error_unlock;
125 }
126
127 /*
128 * If we never received an index for the current stream, delay
129 * the opening of the index, otherwise open it right now.
130 */
131 if (stream->index_file == NULL) {
132 vstream->index_file = NULL;
133 } else {
134 const uint32_t connection_major = stream->trace->session->major;
135 const uint32_t connection_minor = stream->trace->session->minor;
136 enum lttng_trace_chunk_status chunk_status;
137
138 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
139 vstream->stream_file.trace_chunk,
140 stream->path_name,
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,
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 }
154 }
155 }
156
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
190 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) {
191 off_t lseek_ret;
192
193 lseek_ret = lseek(vstream->index_file->fd, 0, SEEK_END);
194 if (lseek_ret < 0) {
195 goto error_unlock;
196 }
197 }
198 if (stream->is_metadata) {
199 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
200 vstream);
201 }
202 pthread_mutex_unlock(&stream->lock);
203
204 /* Globally visible after the add unique. */
205 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
206 urcu_ref_init(&vstream->ref);
207 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
208
209 return vstream;
210
211 error_unlock:
212 pthread_mutex_unlock(&stream->lock);
213 error:
214 if (vstream) {
215 viewer_stream_destroy(vstream);
216 }
217 if (viewer_trace_chunk && acquired_reference) {
218 lttng_trace_chunk_put(viewer_trace_chunk);
219 }
220 return NULL;
221 }
222
223 static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
224 {
225 int ret;
226 struct lttng_ht_iter iter;
227
228 iter.iter.node = &vstream->stream_n.node;
229 ret = lttng_ht_del(viewer_streams_ht, &iter);
230 assert(!ret);
231 }
232
233 static void viewer_stream_release(struct urcu_ref *ref)
234 {
235 struct relay_viewer_stream *vstream = caa_container_of(ref,
236 struct relay_viewer_stream, ref);
237
238 if (vstream->stream->is_metadata) {
239 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
240 }
241
242 viewer_stream_unpublish(vstream);
243
244 if (vstream->stream_file.fd) {
245 stream_fd_put(vstream->stream_file.fd);
246 vstream->stream_file.fd = NULL;
247 }
248 if (vstream->index_file) {
249 lttng_index_file_put(vstream->index_file);
250 vstream->index_file = NULL;
251 }
252 if (vstream->stream) {
253 stream_put(vstream->stream);
254 vstream->stream = NULL;
255 }
256 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
257 vstream->stream_file.trace_chunk = NULL;
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 {
264 return urcu_ref_get_unless_zero(&vstream->ref);
265 }
266
267 /*
268 * Get viewer stream by id.
269 *
270 * Return viewer stream if found else NULL.
271 */
272 struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
273 {
274 struct lttng_ht_node_u64 *node;
275 struct lttng_ht_iter iter;
276 struct relay_viewer_stream *vstream = NULL;
277
278 rcu_read_lock();
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 }
285 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
286 if (!viewer_stream_get(vstream)) {
287 vstream = NULL;
288 }
289 end:
290 rcu_read_unlock();
291 return vstream;
292 }
293
294 void viewer_stream_put(struct relay_viewer_stream *vstream)
295 {
296 rcu_read_lock();
297 urcu_ref_put(&vstream->ref, viewer_stream_release);
298 rcu_read_unlock();
299 }
300
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
326 /*
327 * Rotate a stream to the next tracefile.
328 *
329 * Must be called with the rstream lock held.
330 * Returns 0 on success, 1 on EOF.
331 */
332 int viewer_stream_rotate(struct relay_viewer_stream *vstream)
333 {
334 int ret;
335 uint64_t new_id;
336 const struct relay_stream *stream = vstream->stream;
337
338 /* Detect the last tracefile to open. */
339 if (stream->index_received_seqcount
340 == vstream->index_sent_seqcount
341 && stream->trace->session->connection_closed) {
342 ret = 1;
343 goto end;
344 }
345
346 if (stream->tracefile_count == 0) {
347 /* Ignore rotation, there is none to do. */
348 ret = 0;
349 goto end;
350 }
351
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;
360 } else {
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 */
372 vstream->current_tracefile_id =
373 tracefile_array_get_file_index_tail(stream->tfa);
374 vstream->index_sent_seqcount = seq_tail;
375 }
376 viewer_stream_close_files(vstream);
377 ret = 0;
378 end:
379 return ret;
380 }
381
382 void print_viewer_streams(void)
383 {
384 struct lttng_ht_iter iter;
385 struct relay_viewer_stream *vstream;
386
387 if (!viewer_streams_ht) {
388 return;
389 }
390
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 }
This page took 0.058769 seconds and 5 git commands to generate.