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