Fix: relayd viewer stream: unhandled out of memory error
[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 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <common/common.h>
21 #include <common/index/index.h>
22
23 #include "lttng-relayd.h"
24 #include "viewer-stream.h"
25
26 static void free_stream(struct relay_viewer_stream *stream)
27 {
28 assert(stream);
29
30 free(stream->path_name);
31 free(stream->channel_name);
32 free(stream);
33 }
34
35 static void deferred_free_viewer_stream(struct rcu_head *head)
36 {
37 struct relay_viewer_stream *stream =
38 caa_container_of(head, struct relay_viewer_stream, rcu_node);
39
40 free_stream(stream);
41 }
42
43 struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
44 enum lttng_viewer_seek seek_t, struct ctf_trace *ctf_trace)
45 {
46 struct relay_viewer_stream *vstream;
47
48 assert(stream);
49 assert(ctf_trace);
50
51 vstream = zmalloc(sizeof(*vstream));
52 if (!vstream) {
53 PERROR("relay viewer stream zmalloc");
54 goto error;
55 }
56
57 vstream->session_id = stream->session_id;
58 vstream->stream_handle = stream->stream_handle;
59 vstream->path_name = strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
60 if (vstream->path_name == NULL) {
61 PERROR("relay viewer path_name alloc");
62 goto error;
63 }
64 vstream->channel_name = strndup(stream->channel_name,
65 LTTNG_VIEWER_NAME_MAX);
66 if (vstream->channel_name == NULL) {
67 PERROR("relay viewer channel_name alloc");
68 goto error;
69 }
70 vstream->tracefile_count = stream->tracefile_count;
71 vstream->metadata_flag = stream->metadata_flag;
72 vstream->tracefile_count_last = -1ULL;
73
74 switch (seek_t) {
75 case LTTNG_VIEWER_SEEK_BEGINNING:
76 vstream->tracefile_count_current = stream->oldest_tracefile_id;
77 break;
78 case LTTNG_VIEWER_SEEK_LAST:
79 vstream->tracefile_count_current = stream->tracefile_count_current;
80 break;
81 default:
82 assert(0);
83 goto error;
84 }
85
86 if (vstream->metadata_flag) {
87 ctf_trace->viewer_metadata_stream = vstream;
88 }
89
90 /* Globally visible after the add unique. */
91 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
92 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
93
94 vstream->index_read_fd = -1;
95 vstream->read_fd = -1;
96
97 /*
98 * This is to avoid a race between the initialization of this object and
99 * the close of the given stream. If the stream is unable to find this
100 * viewer stream when closing, this copy will at least take the latest
101 * value. We also need that for the seek_last.
102 */
103 vstream->total_index_received = stream->total_index_received;
104
105 /*
106 * If we never received an index for the current stream, delay the opening
107 * of the index, otherwise open it right now.
108 */
109 if (vstream->tracefile_count_current == stream->tracefile_count_current
110 && vstream->total_index_received == 0) {
111 vstream->index_read_fd = -1;
112 } else {
113 int read_fd;
114
115 read_fd = index_open(vstream->path_name, vstream->channel_name,
116 vstream->tracefile_count, vstream->tracefile_count_current);
117 if (read_fd < 0) {
118 goto error;
119 }
120 vstream->index_read_fd = read_fd;
121 }
122
123 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_read_fd >= 0) {
124 off_t lseek_ret;
125
126 lseek_ret = lseek(vstream->index_read_fd,
127 vstream->total_index_received * sizeof(struct ctf_packet_index),
128 SEEK_CUR);
129 if (lseek_ret < 0) {
130 goto error;
131 }
132 vstream->last_sent_index = vstream->total_index_received;
133 }
134
135 return vstream;
136
137 error:
138 if (vstream) {
139 free_stream(vstream);
140 }
141 return NULL;
142 }
143
144 void viewer_stream_delete(struct relay_viewer_stream *stream)
145 {
146 int ret;
147 struct lttng_ht_iter iter;
148
149 iter.iter.node = &stream->stream_n.node;
150 ret = lttng_ht_del(viewer_streams_ht, &iter);
151 assert(!ret);
152 }
153
154 void viewer_stream_destroy(struct ctf_trace *ctf_trace,
155 struct relay_viewer_stream *stream)
156 {
157 int ret;
158
159 assert(stream);
160
161 if (ctf_trace) {
162 ctf_trace_put_ref(ctf_trace);
163 }
164
165 if (stream->read_fd >= 0) {
166 ret = close(stream->read_fd);
167 if (ret < 0) {
168 PERROR("close read_fd");
169 }
170 }
171 if (stream->index_read_fd >= 0) {
172 ret = close(stream->index_read_fd);
173 if (ret < 0) {
174 PERROR("close index_read_fd");
175 }
176 }
177
178 call_rcu(&stream->rcu_node, deferred_free_viewer_stream);
179 }
180
181 /*
182 * Find viewer stream by id. RCU read side lock MUST be acquired.
183 *
184 * Return stream if found else NULL.
185 */
186 struct relay_viewer_stream *viewer_stream_find_by_id(uint64_t id)
187 {
188 struct lttng_ht_node_u64 *node;
189 struct lttng_ht_iter iter;
190 struct relay_viewer_stream *stream = NULL;
191
192 lttng_ht_lookup(viewer_streams_ht, &id, &iter);
193 node = lttng_ht_iter_get_node_u64(&iter);
194 if (!node) {
195 DBG("Relay viewer stream %" PRIu64 " not found", id);
196 goto end;
197 }
198 stream = caa_container_of(node, struct relay_viewer_stream, stream_n);
199
200 end:
201 return stream;
202 }
203
204 /*
205 * Rotate a stream to the next tracefile.
206 *
207 * Must be called with viewer_stream_rotation_lock held.
208 * Returns 0 on success, 1 on EOF, a negative value on error.
209 */
210 int viewer_stream_rotate(struct relay_viewer_stream *vstream,
211 struct relay_stream *stream)
212 {
213 int ret;
214 uint64_t tracefile_id;
215
216 assert(vstream);
217 assert(stream);
218
219 if (vstream->tracefile_count == 0) {
220 /* Ignore rotation, there is none to do. */
221 ret = 0;
222 goto end;
223 }
224
225 tracefile_id = (vstream->tracefile_count_current + 1) %
226 vstream->tracefile_count;
227
228 /* Detect the last tracefile to open. */
229 if (vstream->tracefile_count_last != -1ULL &&
230 vstream->tracefile_count_last ==
231 vstream->tracefile_count_current) {
232 ret = 1;
233 goto end;
234 }
235
236 /*
237 * The writer and the reader are not working in the same tracefile, we can
238 * read up to EOF, we don't care about the total_index_received.
239 */
240 if (stream->close_flag || (stream->tracefile_count_current != tracefile_id)) {
241 vstream->close_write_flag = 1;
242 } else {
243 /*
244 * We are opening a file that is still open in write, make sure we
245 * limit our reading to the number of indexes received.
246 */
247 vstream->close_write_flag = 0;
248 if (stream->close_flag) {
249 vstream->total_index_received = stream->total_index_received;
250 }
251 }
252 vstream->tracefile_count_current = tracefile_id;
253
254 ret = close(vstream->index_read_fd);
255 if (ret < 0) {
256 PERROR("close index file %d", vstream->index_read_fd);
257 }
258 vstream->index_read_fd = -1;
259
260 ret = close(vstream->read_fd);
261 if (ret < 0) {
262 PERROR("close tracefile %d", vstream->read_fd);
263 }
264 vstream->read_fd = -1;
265
266 pthread_mutex_lock(&vstream->overwrite_lock);
267 vstream->abort_flag = 0;
268 pthread_mutex_unlock(&vstream->overwrite_lock);
269
270 ret = index_open(vstream->path_name, vstream->channel_name,
271 vstream->tracefile_count, vstream->tracefile_count_current);
272 if (ret < 0) {
273 goto error;
274 }
275 vstream->index_read_fd = ret;
276
277 ret = 0;
278
279 end:
280 error:
281 return ret;
282 }
This page took 0.034892 seconds and 4 git commands to generate.