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