Commit | Line | Data |
---|---|---|
3bd1e081 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU General Public License | |
7 | * as published by the Free Software Foundation; only version 2 | |
8 | * of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #define _GNU_SOURCE | |
21 | #include <assert.h> | |
22 | #include <fcntl.h> | |
23 | #include <poll.h> | |
24 | #include <pthread.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <sys/mman.h> | |
28 | #include <sys/socket.h> | |
29 | #include <sys/types.h> | |
30 | #include <unistd.h> | |
31 | ||
3bd1e081 MD |
32 | #include <lttng-sessiond-comm.h> |
33 | #include <lttng/lttng-ustconsumer.h> | |
9df8df5e | 34 | #include <lttng/ust-ctl.h> |
3bd1e081 MD |
35 | #include <lttngerr.h> |
36 | ||
37 | extern struct lttng_consumer_global_data consumer_data; | |
38 | extern int consumer_poll_timeout; | |
39 | extern volatile int consumer_quit; | |
40 | ||
41 | /* | |
42 | * Mmap the ring buffer, read it and write the data to the tracefile. | |
43 | * | |
44 | * Returns the number of bytes written | |
45 | */ | |
46 | int lttng_ustconsumer_on_read_subbuffer_mmap( | |
47 | struct lttng_consumer_local_data *ctx, | |
48 | struct lttng_consumer_stream *stream, unsigned long len) | |
49 | { | |
50 | unsigned long mmap_offset; | |
51 | long ret = 0; | |
52 | off_t orig_offset = stream->out_fd_offset; | |
53 | int outfd = stream->out_fd; | |
54 | ||
55 | /* get the offset inside the fd to mmap */ | |
56 | ret = ustctl_get_mmap_read_offset(stream->chan->handle, | |
57 | stream->buf, &mmap_offset); | |
58 | if (ret != 0) { | |
59 | ret = -errno; | |
60 | perror("ustctl_get_mmap_read_offset"); | |
61 | goto end; | |
62 | } | |
63 | while (len > 0) { | |
64 | ret = write(outfd, stream->mmap_base + mmap_offset, len); | |
65 | if (ret >= len) { | |
66 | len = 0; | |
67 | } else if (ret < 0) { | |
68 | ret = -errno; | |
69 | perror("Error in file write"); | |
70 | goto end; | |
71 | } | |
72 | /* This won't block, but will start writeout asynchronously */ | |
73 | sync_file_range(outfd, stream->out_fd_offset, ret, | |
74 | SYNC_FILE_RANGE_WRITE); | |
75 | stream->out_fd_offset += ret; | |
76 | } | |
77 | ||
78 | lttng_consumer_sync_trace_file(stream, orig_offset); | |
79 | ||
80 | goto end; | |
81 | ||
82 | end: | |
83 | return ret; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Splice the data from the ring buffer to the tracefile. | |
88 | * | |
89 | * Returns the number of bytes spliced. | |
90 | */ | |
91 | int lttng_ustconsumer_on_read_subbuffer_splice( | |
92 | struct lttng_consumer_local_data *ctx, | |
93 | struct lttng_consumer_stream *stream, unsigned long len) | |
94 | { | |
95 | return -ENOSYS; | |
96 | } | |
97 | ||
98 | /* | |
99 | * Take a snapshot for a specific fd | |
100 | * | |
101 | * Returns 0 on success, < 0 on error | |
102 | */ | |
103 | int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx, | |
104 | struct lttng_consumer_stream *stream) | |
105 | { | |
106 | int ret = 0; | |
107 | ||
108 | ret = ustctl_snapshot(stream->chan->handle, stream->buf); | |
109 | if (ret != 0) { | |
110 | ret = errno; | |
111 | perror("Getting sub-buffer snapshot."); | |
112 | } | |
113 | ||
114 | return ret; | |
115 | } | |
116 | ||
117 | /* | |
118 | * Get the produced position | |
119 | * | |
120 | * Returns 0 on success, < 0 on error | |
121 | */ | |
122 | int lttng_ustconsumer_get_produced_snapshot( | |
123 | struct lttng_consumer_local_data *ctx, | |
124 | struct lttng_consumer_stream *stream, | |
125 | unsigned long *pos) | |
126 | { | |
127 | int ret; | |
128 | ||
129 | ret = ustctl_snapshot_get_produced(stream->chan->handle, | |
130 | stream->buf, pos); | |
131 | if (ret != 0) { | |
132 | ret = errno; | |
133 | perror("kernctl_snapshot_get_produced"); | |
134 | } | |
135 | ||
136 | return ret; | |
137 | } | |
138 | ||
139 | int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx, | |
140 | int sock, struct pollfd *consumer_sockpoll) | |
141 | { | |
142 | ssize_t ret; | |
143 | struct lttcomm_consumer_msg msg; | |
144 | ||
145 | ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg)); | |
146 | if (ret != sizeof(msg)) { | |
147 | lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD); | |
148 | return ret; | |
149 | } | |
150 | if (msg.cmd_type == LTTNG_CONSUMER_STOP) { | |
151 | return -ENOENT; | |
152 | } | |
153 | ||
154 | switch (msg.cmd_type) { | |
155 | case LTTNG_CONSUMER_ADD_CHANNEL: | |
156 | { | |
157 | struct lttng_consumer_channel *new_channel; | |
158 | int fds[1]; | |
159 | size_t nb_fd = 1; | |
160 | ||
161 | /* block */ | |
162 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
163 | return -EINTR; | |
164 | } | |
165 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
166 | if (ret != sizeof(fds)) { | |
167 | lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD); | |
168 | return ret; | |
169 | } | |
170 | ||
171 | DBG("consumer_add_channel %d", msg.u.channel.channel_key); | |
172 | ||
173 | new_channel = consumer_allocate_channel(msg.u.channel.channel_key, | |
174 | fds[0], -1, | |
175 | msg.u.channel.mmap_len, | |
176 | msg.u.channel.max_sb_size); | |
177 | if (new_channel == NULL) { | |
178 | lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR); | |
179 | goto end_nosignal; | |
180 | } | |
181 | if (ctx->on_recv_channel != NULL) { | |
182 | ret = ctx->on_recv_channel(new_channel); | |
183 | if (ret == 0) { | |
184 | consumer_add_channel(new_channel); | |
185 | } else if (ret < 0) { | |
186 | goto end_nosignal; | |
187 | } | |
188 | } else { | |
189 | consumer_add_channel(new_channel); | |
190 | } | |
191 | goto end_nosignal; | |
192 | } | |
193 | case LTTNG_CONSUMER_ADD_STREAM: | |
194 | { | |
195 | struct lttng_consumer_stream *new_stream; | |
196 | int fds[2]; | |
197 | size_t nb_fd = 2; | |
198 | ||
199 | /* block */ | |
200 | if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) { | |
201 | return -EINTR; | |
202 | } | |
203 | ret = lttcomm_recv_fds_unix_sock(sock, fds, nb_fd); | |
204 | if (ret != sizeof(fds)) { | |
205 | lttng_consumer_send_error(ctx, CONSUMERD_ERROR_RECV_FD); | |
206 | return ret; | |
207 | } | |
208 | ||
209 | DBG("consumer_add_stream %s (%d,%d)", msg.u.stream.path_name, | |
210 | fds[0], fds[1]); | |
d41f73b7 | 211 | assert(msg.u.stream.output == LTTNG_EVENT_MMAP); |
7ad0a0cb | 212 | new_stream = consumer_allocate_stream(msg.u.channel.channel_key, |
3bd1e081 MD |
213 | msg.u.stream.stream_key, |
214 | fds[0], fds[1], | |
215 | msg.u.stream.state, | |
216 | msg.u.stream.mmap_len, | |
217 | msg.u.stream.output, | |
6df2e2c9 MD |
218 | msg.u.stream.path_name, |
219 | msg.u.stream.uid, | |
220 | msg.u.stream.gid); | |
3bd1e081 MD |
221 | if (new_stream == NULL) { |
222 | lttng_consumer_send_error(ctx, CONSUMERD_OUTFD_ERROR); | |
223 | goto end; | |
224 | } | |
225 | if (ctx->on_recv_stream != NULL) { | |
226 | ret = ctx->on_recv_stream(new_stream); | |
227 | if (ret == 0) { | |
228 | consumer_add_stream(new_stream); | |
229 | } else if (ret < 0) { | |
230 | goto end; | |
231 | } | |
232 | } else { | |
233 | consumer_add_stream(new_stream); | |
234 | } | |
235 | break; | |
236 | } | |
237 | case LTTNG_CONSUMER_UPDATE_STREAM: | |
238 | { | |
7ad0a0cb MD |
239 | return -ENOSYS; |
240 | #if 0 | |
3bd1e081 MD |
241 | if (ctx->on_update_stream != NULL) { |
242 | ret = ctx->on_update_stream(msg.u.stream.stream_key, msg.u.stream.state); | |
243 | if (ret == 0) { | |
244 | consumer_change_stream_state(msg.u.stream.stream_key, msg.u.stream.state); | |
245 | } else if (ret < 0) { | |
246 | goto end; | |
247 | } | |
248 | } else { | |
249 | consumer_change_stream_state(msg.u.stream.stream_key, | |
250 | msg.u.stream.state); | |
251 | } | |
7ad0a0cb | 252 | #endif |
3bd1e081 MD |
253 | break; |
254 | } | |
255 | default: | |
256 | break; | |
257 | } | |
258 | end: | |
259 | /* signal the poll thread */ | |
260 | ret = write(ctx->consumer_poll_pipe[1], "4", 1); | |
261 | if (ret < 0) { | |
262 | perror("write consumer poll"); | |
263 | } | |
264 | end_nosignal: | |
265 | return 0; | |
266 | } | |
267 | ||
268 | int lttng_ustconsumer_allocate_channel(struct lttng_consumer_channel *chan) | |
269 | { | |
13161846 | 270 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
271 | |
272 | obj.handle = -1; | |
273 | obj.shm_fd = chan->shm_fd; | |
274 | obj.wait_fd = chan->wait_fd; | |
275 | obj.memory_map_size = chan->mmap_len; | |
276 | chan->handle = ustctl_map_channel(&obj); | |
277 | if (!chan->handle) { | |
278 | return -ENOMEM; | |
279 | } | |
ee77a7b0 | 280 | /* |
b5c5fc29 | 281 | * The channel fds are passed to ustctl, we only keep a copy. |
ee77a7b0 | 282 | */ |
b5c5fc29 MD |
283 | chan->shm_fd_is_copy = 1; |
284 | chan->wait_fd_is_copy = 1; | |
285 | ||
3bd1e081 MD |
286 | return 0; |
287 | } | |
288 | ||
d056b477 MD |
289 | void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream) |
290 | { | |
291 | ustctl_flush_buffer(stream->chan->handle, stream->buf, 0); | |
effcf122 | 292 | stream->hangup_flush_done = 1; |
d056b477 MD |
293 | } |
294 | ||
3bd1e081 MD |
295 | void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) |
296 | { | |
297 | ustctl_unmap_channel(chan->handle); | |
298 | } | |
299 | ||
300 | int lttng_ustconsumer_allocate_stream(struct lttng_consumer_stream *stream) | |
301 | { | |
13161846 | 302 | struct lttng_ust_object_data obj; |
3bd1e081 MD |
303 | int ret; |
304 | ||
305 | obj.handle = -1; | |
306 | obj.shm_fd = stream->shm_fd; | |
307 | obj.wait_fd = stream->wait_fd; | |
308 | obj.memory_map_size = stream->mmap_len; | |
309 | ret = ustctl_add_stream(stream->chan->handle, &obj); | |
310 | if (ret) | |
311 | return ret; | |
312 | stream->buf = ustctl_open_stream_read(stream->chan->handle, stream->cpu); | |
313 | if (!stream->buf) | |
314 | return -EBUSY; | |
315 | stream->mmap_base = ustctl_get_mmap_base(stream->chan->handle, stream->buf); | |
316 | if (!stream->mmap_base) { | |
317 | return -EINVAL; | |
318 | } | |
ee77a7b0 | 319 | /* |
b5c5fc29 | 320 | * The stream fds are passed to ustctl, we only keep a copy. |
ee77a7b0 | 321 | */ |
b5c5fc29 MD |
322 | stream->shm_fd_is_copy = 1; |
323 | stream->wait_fd_is_copy = 1; | |
ee77a7b0 | 324 | |
3bd1e081 MD |
325 | return 0; |
326 | } | |
327 | ||
328 | void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream) | |
329 | { | |
330 | ustctl_close_stream_read(stream->chan->handle, stream->buf); | |
331 | } | |
d41f73b7 MD |
332 | |
333 | ||
334 | int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream, | |
335 | struct lttng_consumer_local_data *ctx) | |
336 | { | |
337 | unsigned long len; | |
338 | int err; | |
339 | long ret = 0; | |
340 | struct lttng_ust_shm_handle *handle; | |
341 | struct lttng_ust_lib_ring_buffer *buf; | |
342 | char dummy; | |
343 | ssize_t readlen; | |
344 | ||
345 | DBG("In read_subbuffer (wait_fd: %d, stream key: %d)", | |
346 | stream->wait_fd, stream->key); | |
347 | ||
348 | /* We can consume the 1 byte written into the wait_fd by UST */ | |
effcf122 MD |
349 | if (!stream->hangup_flush_done) { |
350 | do { | |
351 | readlen = read(stream->wait_fd, &dummy, 1); | |
352 | } while (readlen == -1 && errno == -EINTR); | |
353 | if (readlen == -1) { | |
354 | ret = readlen; | |
355 | goto end; | |
356 | } | |
d41f73b7 MD |
357 | } |
358 | ||
359 | buf = stream->buf; | |
360 | handle = stream->chan->handle; | |
361 | /* Get the next subbuffer */ | |
362 | err = ustctl_get_next_subbuf(handle, buf); | |
363 | if (err != 0) { | |
effcf122 | 364 | ret = -ret; /* ustctl_get_next_subbuf returns negative, caller expect positive. */ |
d41f73b7 MD |
365 | /* |
366 | * This is a debug message even for single-threaded consumer, | |
367 | * because poll() have more relaxed criterions than get subbuf, | |
368 | * so get_subbuf may fail for short race windows where poll() | |
369 | * would issue wakeups. | |
370 | */ | |
371 | DBG("Reserving sub buffer failed (everything is normal, " | |
372 | "it is due to concurrency)"); | |
373 | goto end; | |
374 | } | |
375 | assert(stream->output == LTTNG_EVENT_MMAP); | |
376 | /* read the used subbuffer size */ | |
377 | err = ustctl_get_padded_subbuf_size(handle, buf, &len); | |
effcf122 | 378 | assert(err == 0); |
d41f73b7 MD |
379 | /* write the subbuffer to the tracefile */ |
380 | ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len); | |
381 | if (ret < 0) { | |
382 | /* | |
383 | * display the error but continue processing to try | |
384 | * to release the subbuffer | |
385 | */ | |
386 | ERR("Error writing to tracefile"); | |
387 | } | |
388 | err = ustctl_put_next_subbuf(handle, buf); | |
effcf122 | 389 | assert(err == 0); |
d41f73b7 MD |
390 | end: |
391 | return ret; | |
392 | } | |
393 | ||
394 | int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream) | |
395 | { | |
396 | int ret; | |
397 | ||
398 | /* Opening the tracefile in write mode */ | |
399 | if (stream->path_name != NULL) { | |
400 | ret = open(stream->path_name, | |
401 | O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); | |
402 | if (ret < 0) { | |
403 | ERR("Opening %s", stream->path_name); | |
404 | perror("open"); | |
405 | goto error; | |
406 | } | |
407 | stream->out_fd = ret; | |
6df2e2c9 MD |
408 | ret = chown(stream->path_name, stream->uid, stream->gid); |
409 | if (ret < 0) { | |
410 | ERR("Changing ownership of %s", stream->path_name); | |
411 | perror("chown"); | |
412 | } | |
d41f73b7 MD |
413 | } |
414 | ||
415 | /* we return 0 to let the library handle the FD internally */ | |
416 | return 0; | |
417 | ||
418 | error: | |
419 | return ret; | |
420 | } |