Commit | Line | Data |
---|---|---|
2a174661 | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
2a174661 | 6 | * |
ab5be9fa | 7 | * SPDX-License-Identifier: GPL-2.0-only |
2a174661 | 8 | * |
2a174661 DG |
9 | */ |
10 | ||
6c1c0768 | 11 | #define _LGPL_SOURCE |
28ab034a JG |
12 | #include "index.hpp" |
13 | #include "lttng-relayd.hpp" | |
14 | #include "stream.hpp" | |
15 | #include "viewer-stream.hpp" | |
16 | ||
c9e313bc SM |
17 | #include <common/common.hpp> |
18 | #include <common/defaults.hpp> | |
19 | #include <common/fs-handle.hpp> | |
20 | #include <common/sessiond-comm/relayd.hpp> | |
56047f5a | 21 | #include <common/urcu.hpp> |
c9e313bc | 22 | #include <common/utils.hpp> |
2a174661 | 23 | |
28ab034a | 24 | #include <algorithm> |
348a81dc | 25 | #include <fcntl.h> |
28ab034a JG |
26 | #include <sys/stat.h> |
27 | #include <sys/types.h> | |
28 | #include <urcu/rculist.h> | |
348a81dc | 29 | |
28ab034a | 30 | #define FILE_IO_STACK_BUFFER_SIZE 65536 |
c35f9726 | 31 | |
7591bab1 MD |
32 | /* Should be called with RCU read-side lock held. */ |
33 | bool stream_get(struct relay_stream *stream) | |
34 | { | |
48b7cdc2 FD |
35 | ASSERT_RCU_READ_LOCKED(); |
36 | ||
ce4d4083 | 37 | return urcu_ref_get_unless_zero(&stream->ref); |
7591bab1 MD |
38 | } |
39 | ||
2a174661 | 40 | /* |
7591bab1 MD |
41 | * Get stream from stream id from the streams hash table. Return stream |
42 | * if found else NULL. A stream reference is taken when a stream is | |
43 | * returned. stream_put() must be called on that stream. | |
2a174661 | 44 | */ |
7591bab1 | 45 | struct relay_stream *stream_get_by_id(uint64_t stream_id) |
2a174661 DG |
46 | { |
47 | struct lttng_ht_node_u64 *node; | |
48 | struct lttng_ht_iter iter; | |
cd9adb8b | 49 | struct relay_stream *stream = nullptr; |
2a174661 | 50 | |
56047f5a | 51 | lttng::urcu::read_lock_guard read_lock; |
7591bab1 | 52 | lttng_ht_lookup(relay_streams_ht, &stream_id, &iter); |
00d7d903 | 53 | node = lttng_ht_iter_get_node<lttng_ht_node_u64>(&iter); |
7591bab1 | 54 | if (!node) { |
2a174661 DG |
55 | DBG("Relay stream %" PRIu64 " not found", stream_id); |
56 | goto end; | |
57 | } | |
0114db0e | 58 | stream = lttng::utils::container_of(node, &relay_stream::node); |
7591bab1 | 59 | if (!stream_get(stream)) { |
cd9adb8b | 60 | stream = nullptr; |
7591bab1 | 61 | } |
2a174661 DG |
62 | end: |
63 | return stream; | |
64 | } | |
65 | ||
c35f9726 JG |
66 | static void stream_complete_rotation(struct relay_stream *stream) |
67 | { | |
68 | DBG("Rotation completed for stream %" PRIu64, stream->stream_handle); | |
f607fc46 MD |
69 | if (stream->ongoing_rotation.value.next_trace_chunk) { |
70 | tracefile_array_reset(stream->tfa); | |
28ab034a | 71 | tracefile_array_commit_seq(stream->tfa, stream->index_received_seqcount); |
f607fc46 | 72 | } |
c35f9726 JG |
73 | lttng_trace_chunk_put(stream->trace_chunk); |
74 | stream->trace_chunk = stream->ongoing_rotation.value.next_trace_chunk; | |
75864046 | 75 | stream->ongoing_rotation = LTTNG_OPTIONAL_INIT_UNSET; |
80516611 | 76 | stream->completed_rotation_count++; |
c35f9726 JG |
77 | } |
78 | ||
28ab034a JG |
79 | static int stream_create_data_output_file_from_trace_chunk(struct relay_stream *stream, |
80 | struct lttng_trace_chunk *trace_chunk, | |
81 | bool force_unlink, | |
82 | struct fs_handle **out_file) | |
348a81dc | 83 | { |
8bb66c3c | 84 | int ret; |
c35f9726 | 85 | char stream_path[LTTNG_PATH_MAX]; |
348a81dc JG |
86 | enum lttng_trace_chunk_status status; |
87 | const int flags = O_RDWR | O_CREAT | O_TRUNC; | |
88 | const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
348a81dc JG |
89 | |
90 | ASSERT_LOCKED(stream->lock); | |
348a81dc | 91 | |
28ab034a JG |
92 | ret = utils_stream_file_path(stream->path_name, |
93 | stream->channel_name, | |
94 | stream->tracefile_size, | |
95 | stream->tracefile_current_index, | |
cd9adb8b | 96 | nullptr, |
28ab034a JG |
97 | stream_path, |
98 | sizeof(stream_path)); | |
348a81dc JG |
99 | if (ret < 0) { |
100 | goto end; | |
101 | } | |
102 | ||
c35f9726 JG |
103 | if (stream->tracefile_wrapped_around || force_unlink) { |
104 | /* | |
105 | * The on-disk ring-buffer has wrapped around. | |
106 | * Newly created stream files will replace existing files. Since | |
107 | * live clients may be consuming existing files, the file about | |
108 | * to be replaced is unlinked in order to not overwrite its | |
109 | * content. | |
110 | */ | |
ac497a37 | 111 | status = (lttng_trace_chunk_status) lttng_trace_chunk_unlink_file(trace_chunk, |
28ab034a | 112 | stream_path); |
c35f9726 JG |
113 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
114 | PERROR("Failed to unlink stream file \"%s\" during trace file rotation", | |
28ab034a | 115 | stream_path); |
c35f9726 JG |
116 | /* |
117 | * Don't abort if the file doesn't exist, it is | |
118 | * unexpected, but should not be a fatal error. | |
119 | */ | |
120 | if (errno != ENOENT) { | |
121 | ret = -1; | |
122 | goto end; | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
28ab034a JG |
127 | status = lttng_trace_chunk_open_fs_handle( |
128 | trace_chunk, stream_path, flags, mode, out_file, false); | |
348a81dc JG |
129 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
130 | ERR("Failed to open stream file \"%s\"", stream->channel_name); | |
131 | ret = -1; | |
132 | goto end; | |
133 | } | |
348a81dc JG |
134 | end: |
135 | return ret; | |
136 | } | |
137 | ||
c35f9726 JG |
138 | static int stream_rotate_data_file(struct relay_stream *stream) |
139 | { | |
140 | int ret = 0; | |
141 | ||
68c40154 | 142 | DBG("Rotating stream %" PRIu64 " data file with size %" PRIu64, |
28ab034a JG |
143 | stream->stream_handle, |
144 | stream->tracefile_size_current); | |
c35f9726 | 145 | |
8bb66c3c JG |
146 | if (stream->file) { |
147 | fs_handle_close(stream->file); | |
cd9adb8b | 148 | stream->file = nullptr; |
c35f9726 JG |
149 | } |
150 | ||
151 | stream->tracefile_wrapped_around = false; | |
152 | stream->tracefile_current_index = 0; | |
153 | ||
154 | if (stream->ongoing_rotation.value.next_trace_chunk) { | |
c35f9726 JG |
155 | enum lttng_trace_chunk_status chunk_status; |
156 | ||
157 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
28ab034a | 158 | stream->ongoing_rotation.value.next_trace_chunk, stream->path_name); |
c35f9726 JG |
159 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
160 | ret = -1; | |
161 | goto end; | |
162 | } | |
163 | ||
164 | /* Rotate the data file. */ | |
28ab034a JG |
165 | ret = stream_create_data_output_file_from_trace_chunk( |
166 | stream, | |
167 | stream->ongoing_rotation.value.next_trace_chunk, | |
168 | false, | |
169 | &stream->file); | |
c35f9726 JG |
170 | if (ret < 0) { |
171 | ERR("Failed to rotate stream data file"); | |
172 | goto end; | |
173 | } | |
174 | } | |
68c40154 | 175 | DBG("%s: reset tracefile_size_current for stream %" PRIu64 " was %" PRIu64, |
28ab034a JG |
176 | __func__, |
177 | stream->stream_handle, | |
178 | stream->tracefile_size_current); | |
c35f9726 JG |
179 | stream->tracefile_size_current = 0; |
180 | stream->pos_after_last_complete_data_index = 0; | |
181 | stream->ongoing_rotation.value.data_rotated = true; | |
182 | ||
183 | if (stream->ongoing_rotation.value.index_rotated) { | |
184 | /* Rotation completed; reset its state. */ | |
185 | stream_complete_rotation(stream); | |
186 | } | |
187 | end: | |
188 | return ret; | |
189 | } | |
190 | ||
f2aea36d JG |
191 | /* |
192 | * If too much data has been written in a tracefile before we received the | |
193 | * rotation command, we have to move the excess data to the new tracefile and | |
194 | * perform the rotation. This can happen because the control and data | |
195 | * connections are separate, the indexes as well as the commands arrive from | |
196 | * the control connection and we have no control over the order so we could be | |
197 | * in a situation where too much data has been received on the data connection | |
198 | * before the rotation command on the control connection arrives. | |
199 | */ | |
200 | static int rotate_truncate_stream(struct relay_stream *stream) | |
201 | { | |
202 | int ret; | |
203 | off_t lseek_ret, previous_stream_copy_origin; | |
204 | uint64_t copy_bytes_left, misplaced_data_size; | |
205 | bool acquired_reference; | |
cd9adb8b JG |
206 | struct fs_handle *previous_stream_file = nullptr; |
207 | struct lttng_trace_chunk *previous_chunk = nullptr; | |
f2aea36d | 208 | |
f3fe2a92 | 209 | if (!LTTNG_OPTIONAL_GET(stream->ongoing_rotation).next_trace_chunk) { |
4147107d | 210 | ERR("Protocol error encoutered in %s(): stream rotation " |
28ab034a JG |
211 | "sequence number is before the current sequence number " |
212 | "and the next trace chunk is unset. Honoring this " | |
213 | "rotation command would result in data loss", | |
214 | __FUNCTION__); | |
4147107d JG |
215 | ret = -1; |
216 | goto end; | |
217 | } | |
218 | ||
f2aea36d JG |
219 | ASSERT_LOCKED(stream->lock); |
220 | /* | |
221 | * Acquire a reference to the current trace chunk to ensure | |
222 | * it is not reclaimed when `stream_rotate_data_file` is called. | |
223 | * Failing to do so would violate the contract of the trace | |
224 | * chunk API as an active file descriptor would outlive the | |
225 | * trace chunk. | |
226 | */ | |
227 | acquired_reference = lttng_trace_chunk_get(stream->trace_chunk); | |
a0377dfe | 228 | LTTNG_ASSERT(acquired_reference); |
f2aea36d JG |
229 | previous_chunk = stream->trace_chunk; |
230 | ||
231 | /* | |
232 | * Steal the stream's reference to its stream_fd. A new | |
233 | * stream_fd will be created when the rotation completes and | |
234 | * the orinal stream_fd will be used to copy the "extra" data | |
235 | * to the new file. | |
236 | */ | |
a0377dfe | 237 | LTTNG_ASSERT(stream->file); |
8bb66c3c | 238 | previous_stream_file = stream->file; |
cd9adb8b | 239 | stream->file = nullptr; |
f2aea36d | 240 | |
a0377dfe | 241 | LTTNG_ASSERT(!stream->is_metadata); |
28ab034a JG |
242 | LTTNG_ASSERT(stream->tracefile_size_current > stream->pos_after_last_complete_data_index); |
243 | misplaced_data_size = | |
244 | stream->tracefile_size_current - stream->pos_after_last_complete_data_index; | |
f2aea36d JG |
245 | copy_bytes_left = misplaced_data_size; |
246 | previous_stream_copy_origin = stream->pos_after_last_complete_data_index; | |
247 | ||
248 | ret = stream_rotate_data_file(stream); | |
249 | if (ret) { | |
250 | goto end; | |
251 | } | |
252 | ||
a0377dfe | 253 | LTTNG_ASSERT(stream->file); |
f2aea36d JG |
254 | /* |
255 | * Seek the current tracefile to the position at which the rotation | |
256 | * should have occurred. | |
257 | */ | |
8bb66c3c | 258 | lseek_ret = fs_handle_seek(previous_stream_file, previous_stream_copy_origin, SEEK_SET); |
f2aea36d JG |
259 | if (lseek_ret < 0) { |
260 | PERROR("Failed to seek to offset %" PRIu64 | |
261 | " while copying extra data received before a stream rotation", | |
28ab034a | 262 | (uint64_t) previous_stream_copy_origin); |
f2aea36d JG |
263 | ret = -1; |
264 | goto end; | |
265 | } | |
266 | ||
267 | /* Move data from the old file to the new file. */ | |
268 | while (copy_bytes_left) { | |
269 | ssize_t io_ret; | |
270 | char copy_buffer[FILE_IO_STACK_BUFFER_SIZE]; | |
28ab034a JG |
271 | const off_t copy_size_this_pass = |
272 | std::min<uint64_t>(copy_bytes_left, sizeof(copy_buffer)); | |
f2aea36d | 273 | |
28ab034a | 274 | io_ret = fs_handle_read(previous_stream_file, copy_buffer, copy_size_this_pass); |
f2aea36d JG |
275 | if (io_ret < (ssize_t) copy_size_this_pass) { |
276 | if (io_ret == -1) { | |
277 | PERROR("Failed to read %" PRIu64 | |
8bb66c3c | 278 | " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64, |
28ab034a JG |
279 | copy_size_this_pass, |
280 | __FUNCTION__, | |
281 | io_ret, | |
282 | stream->stream_handle); | |
f2aea36d JG |
283 | } else { |
284 | ERR("Failed to read %" PRIu64 | |
28ab034a JG |
285 | " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64, |
286 | copy_size_this_pass, | |
287 | __FUNCTION__, | |
288 | io_ret, | |
289 | stream->stream_handle); | |
f2aea36d JG |
290 | } |
291 | ret = -1; | |
292 | goto end; | |
293 | } | |
294 | ||
28ab034a | 295 | io_ret = fs_handle_write(stream->file, copy_buffer, copy_size_this_pass); |
f2aea36d JG |
296 | if (io_ret < (ssize_t) copy_size_this_pass) { |
297 | if (io_ret == -1) { | |
298 | PERROR("Failed to write %" PRIu64 | |
8bb66c3c | 299 | " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64, |
28ab034a JG |
300 | copy_size_this_pass, |
301 | __FUNCTION__, | |
302 | io_ret, | |
303 | stream->stream_handle); | |
f2aea36d JG |
304 | } else { |
305 | ERR("Failed to write %" PRIu64 | |
28ab034a JG |
306 | " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64, |
307 | copy_size_this_pass, | |
308 | __FUNCTION__, | |
309 | io_ret, | |
310 | stream->stream_handle); | |
f2aea36d JG |
311 | } |
312 | ret = -1; | |
313 | goto end; | |
314 | } | |
315 | copy_bytes_left -= copy_size_this_pass; | |
316 | } | |
317 | ||
318 | /* Truncate the file to get rid of the excess data. */ | |
28ab034a | 319 | ret = fs_handle_truncate(previous_stream_file, previous_stream_copy_origin); |
f2aea36d JG |
320 | if (ret) { |
321 | PERROR("Failed to truncate current stream file to offset %" PRIu64, | |
28ab034a | 322 | previous_stream_copy_origin); |
f2aea36d JG |
323 | goto end; |
324 | } | |
325 | ||
326 | /* | |
327 | * Update the offset and FD of all the eventual indexes created by the | |
328 | * data connection before the rotation command arrived. | |
329 | */ | |
330 | ret = relay_index_switch_all_files(stream); | |
331 | if (ret < 0) { | |
332 | ERR("Failed to rotate index file"); | |
333 | goto end; | |
334 | } | |
335 | ||
336 | stream->tracefile_size_current = misplaced_data_size; | |
337 | /* Index and data contents are back in sync. */ | |
338 | stream->pos_after_last_complete_data_index = 0; | |
339 | ret = 0; | |
340 | end: | |
341 | lttng_trace_chunk_put(previous_chunk); | |
f2aea36d JG |
342 | return ret; |
343 | } | |
344 | ||
c35f9726 JG |
345 | /* |
346 | * Check if a stream's data file (as opposed to index) should be rotated | |
347 | * (for session rotation). | |
348 | * Must be called with the stream lock held. | |
349 | * | |
350 | * Return 0 on success, a negative value on error. | |
351 | */ | |
352 | static int try_rotate_stream_data(struct relay_stream *stream) | |
353 | { | |
354 | int ret = 0; | |
355 | ||
356 | if (caa_likely(!stream->ongoing_rotation.is_set)) { | |
357 | /* No rotation expected. */ | |
358 | goto end; | |
359 | } | |
360 | ||
361 | if (stream->ongoing_rotation.value.data_rotated) { | |
362 | /* Rotation of the data file has already occurred. */ | |
363 | goto end; | |
364 | } | |
365 | ||
28ab034a JG |
366 | DBG("%s: Stream %" PRIu64 " (rotate_at_index_packet_seq_num = %" PRIu64 |
367 | ", rotate_at_prev_data_net_seq = %" PRIu64 ", prev_data_seq = %" PRIu64 ")", | |
368 | __func__, | |
369 | stream->stream_handle, | |
370 | stream->ongoing_rotation.value.packet_seq_num, | |
371 | stream->ongoing_rotation.value.prev_data_net_seq, | |
372 | stream->prev_data_seq); | |
68c40154 | 373 | |
c35f9726 | 374 | if (stream->prev_data_seq == -1ULL || |
28ab034a JG |
375 | stream->ongoing_rotation.value.prev_data_net_seq == -1ULL || |
376 | stream->prev_data_seq < stream->ongoing_rotation.value.prev_data_net_seq) { | |
c35f9726 JG |
377 | /* |
378 | * The next packet that will be written is not part of the next | |
379 | * chunk yet. | |
380 | */ | |
0f83d1cc | 381 | DBG("Stream %" PRIu64 " data not yet ready for rotation " |
28ab034a JG |
382 | "(rotate_at_index_packet_seq_num = %" PRIu64 |
383 | ", rotate_at_prev_data_net_seq = %" PRIu64 ", prev_data_seq = %" PRIu64 ")", | |
384 | stream->stream_handle, | |
385 | stream->ongoing_rotation.value.packet_seq_num, | |
386 | stream->ongoing_rotation.value.prev_data_net_seq, | |
387 | stream->prev_data_seq); | |
c35f9726 | 388 | goto end; |
0f83d1cc | 389 | } else if (stream->prev_data_seq > stream->ongoing_rotation.value.prev_data_net_seq) { |
c35f9726 JG |
390 | /* |
391 | * prev_data_seq is checked here since indexes and rotation | |
392 | * commands are serialized with respect to each other. | |
393 | */ | |
394 | DBG("Rotation after too much data has been written in tracefile " | |
28ab034a JG |
395 | "for stream %" PRIu64 ", need to truncate before " |
396 | "rotating", | |
397 | stream->stream_handle); | |
c35f9726 JG |
398 | ret = rotate_truncate_stream(stream); |
399 | if (ret) { | |
400 | ERR("Failed to truncate stream"); | |
401 | goto end; | |
402 | } | |
403 | } else { | |
404 | ret = stream_rotate_data_file(stream); | |
405 | } | |
406 | ||
407 | end: | |
408 | return ret; | |
409 | } | |
410 | ||
411 | /* | |
412 | * Close the current index file if it is open, and create a new one. | |
413 | * | |
414 | * Return 0 on success, -1 on error. | |
415 | */ | |
28ab034a | 416 | static int create_index_file(struct relay_stream *stream, struct lttng_trace_chunk *chunk) |
c35f9726 JG |
417 | { |
418 | int ret; | |
419 | uint32_t major, minor; | |
cd9adb8b | 420 | char *index_subpath = nullptr; |
3a735fa7 | 421 | enum lttng_trace_chunk_status status; |
c35f9726 JG |
422 | |
423 | ASSERT_LOCKED(stream->lock); | |
424 | ||
425 | /* Put ref on previous index_file. */ | |
426 | if (stream->index_file) { | |
427 | lttng_index_file_put(stream->index_file); | |
cd9adb8b | 428 | stream->index_file = nullptr; |
c35f9726 JG |
429 | } |
430 | major = stream->trace->session->major; | |
431 | minor = stream->trace->session->minor; | |
432 | ||
433 | if (!chunk) { | |
434 | ret = 0; | |
435 | goto end; | |
436 | } | |
28ab034a | 437 | ret = asprintf(&index_subpath, "%s/%s", stream->path_name, DEFAULT_INDEX_DIR); |
c35f9726 JG |
438 | if (ret < 0) { |
439 | goto end; | |
440 | } | |
441 | ||
28ab034a | 442 | status = lttng_trace_chunk_create_subdirectory(chunk, index_subpath); |
c35f9726 | 443 | free(index_subpath); |
3a735fa7 MD |
444 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
445 | ret = -1; | |
c35f9726 JG |
446 | goto end; |
447 | } | |
28ab034a JG |
448 | status = lttng_index_file_create_from_trace_chunk(chunk, |
449 | stream->path_name, | |
450 | stream->channel_name, | |
451 | stream->tracefile_size, | |
452 | stream->tracefile_current_index, | |
453 | lttng_to_index_major(major, minor), | |
454 | lttng_to_index_minor(major, minor), | |
455 | true, | |
456 | &stream->index_file); | |
3ff5c5db | 457 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
c35f9726 JG |
458 | ret = -1; |
459 | goto end; | |
460 | } | |
461 | ||
462 | ret = 0; | |
463 | ||
464 | end: | |
465 | return ret; | |
466 | } | |
467 | ||
468 | /* | |
469 | * Check if a stream's index file should be rotated (for session rotation). | |
470 | * Must be called with the stream lock held. | |
471 | * | |
472 | * Return 0 on success, a negative value on error. | |
473 | */ | |
474 | static int try_rotate_stream_index(struct relay_stream *stream) | |
475 | { | |
476 | int ret = 0; | |
477 | ||
478 | if (!stream->ongoing_rotation.is_set) { | |
479 | /* No rotation expected. */ | |
480 | goto end; | |
481 | } | |
482 | ||
483 | if (stream->ongoing_rotation.value.index_rotated) { | |
484 | /* Rotation of the index has already occurred. */ | |
485 | goto end; | |
486 | } | |
487 | ||
28ab034a JG |
488 | DBG("%s: Stream %" PRIu64 " (rotate_at_packet_seq_num = %" PRIu64 |
489 | ", received_packet_seq_num = " | |
490 | "(value = %" PRIu64 ", is_set = %" PRIu8 "))", | |
491 | __func__, | |
492 | stream->stream_handle, | |
493 | stream->ongoing_rotation.value.packet_seq_num, | |
494 | stream->received_packet_seq_num.value, | |
495 | stream->received_packet_seq_num.is_set); | |
68c40154 | 496 | |
0f83d1cc | 497 | if (!stream->received_packet_seq_num.is_set || |
28ab034a JG |
498 | LTTNG_OPTIONAL_GET(stream->received_packet_seq_num) + 1 < |
499 | stream->ongoing_rotation.value.packet_seq_num) { | |
0f83d1cc | 500 | DBG("Stream %" PRIu64 " index not yet ready for rotation " |
28ab034a JG |
501 | "(rotate_at_packet_seq_num = %" PRIu64 ", received_packet_seq_num = " |
502 | "(value = %" PRIu64 ", is_set = %" PRIu8 "))", | |
503 | stream->stream_handle, | |
504 | stream->ongoing_rotation.value.packet_seq_num, | |
505 | stream->received_packet_seq_num.value, | |
506 | stream->received_packet_seq_num.is_set); | |
c35f9726 JG |
507 | goto end; |
508 | } else { | |
0f83d1cc MD |
509 | /* |
510 | * The next index belongs to the new trace chunk; rotate. | |
511 | * In overwrite mode, the packet seq num may jump over the | |
512 | * rotation position. | |
513 | */ | |
a0377dfe | 514 | LTTNG_ASSERT(LTTNG_OPTIONAL_GET(stream->received_packet_seq_num) + 1 >= |
28ab034a JG |
515 | stream->ongoing_rotation.value.packet_seq_num); |
516 | DBG("Rotating stream %" PRIu64 " index file", stream->stream_handle); | |
f607fc46 MD |
517 | if (stream->index_file) { |
518 | lttng_index_file_put(stream->index_file); | |
cd9adb8b | 519 | stream->index_file = nullptr; |
f607fc46 | 520 | } |
c35f9726 JG |
521 | stream->ongoing_rotation.value.index_rotated = true; |
522 | ||
0f83d1cc MD |
523 | /* |
524 | * Set the rotation pivot position for the data, now that we have the | |
525 | * net_seq_num matching the packet_seq_num index pivot position. | |
526 | */ | |
28ab034a | 527 | stream->ongoing_rotation.value.prev_data_net_seq = stream->prev_index_seq; |
c35f9726 | 528 | if (stream->ongoing_rotation.value.data_rotated && |
28ab034a | 529 | stream->ongoing_rotation.value.index_rotated) { |
c35f9726 | 530 | /* Rotation completed; reset its state. */ |
28ab034a | 531 | DBG("Rotation completed for stream %" PRIu64, stream->stream_handle); |
c35f9726 JG |
532 | stream_complete_rotation(stream); |
533 | } | |
534 | } | |
535 | ||
536 | end: | |
537 | return ret; | |
538 | } | |
539 | ||
28ab034a | 540 | static int stream_set_trace_chunk(struct relay_stream *stream, struct lttng_trace_chunk *chunk) |
348a81dc JG |
541 | { |
542 | int ret = 0; | |
543 | enum lttng_trace_chunk_status status; | |
544 | bool acquired_reference; | |
545 | ||
28ab034a | 546 | status = lttng_trace_chunk_create_subdirectory(chunk, stream->path_name); |
348a81dc JG |
547 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
548 | ret = -1; | |
549 | goto end; | |
550 | } | |
551 | ||
552 | lttng_trace_chunk_put(stream->trace_chunk); | |
553 | acquired_reference = lttng_trace_chunk_get(chunk); | |
a0377dfe | 554 | LTTNG_ASSERT(acquired_reference); |
348a81dc | 555 | stream->trace_chunk = chunk; |
c35f9726 | 556 | |
8bb66c3c JG |
557 | if (stream->file) { |
558 | fs_handle_close(stream->file); | |
cd9adb8b | 559 | stream->file = nullptr; |
c35f9726 | 560 | } |
28ab034a | 561 | ret = stream_create_data_output_file_from_trace_chunk(stream, chunk, false, &stream->file); |
348a81dc | 562 | end: |
348a81dc JG |
563 | return ret; |
564 | } | |
565 | ||
2a174661 | 566 | /* |
7591bab1 | 567 | * We keep ownership of path_name and channel_name. |
2a174661 | 568 | */ |
7591bab1 | 569 | struct relay_stream *stream_create(struct ctf_trace *trace, |
28ab034a JG |
570 | uint64_t stream_handle, |
571 | char *path_name, | |
572 | char *channel_name, | |
573 | uint64_t tracefile_size, | |
574 | uint64_t tracefile_count) | |
2a174661 | 575 | { |
7591bab1 | 576 | int ret; |
cd9adb8b | 577 | struct relay_stream *stream = nullptr; |
7591bab1 | 578 | struct relay_session *session = trace->session; |
348a81dc JG |
579 | bool acquired_reference = false; |
580 | struct lttng_trace_chunk *current_trace_chunk; | |
2a174661 | 581 | |
64803277 | 582 | stream = zmalloc<relay_stream>(); |
cd9adb8b | 583 | if (stream == nullptr) { |
7591bab1 | 584 | PERROR("relay stream zmalloc"); |
7591bab1 MD |
585 | goto error_no_alloc; |
586 | } | |
2a174661 | 587 | |
7591bab1 | 588 | stream->stream_handle = stream_handle; |
a8f9f353 | 589 | stream->prev_data_seq = -1ULL; |
7a45c7e6 | 590 | stream->prev_index_seq = -1ULL; |
bda7c7b9 | 591 | stream->last_net_seq_num = -1ULL; |
7591bab1 MD |
592 | stream->ctf_stream_id = -1ULL; |
593 | stream->tracefile_size = tracefile_size; | |
594 | stream->tracefile_count = tracefile_count; | |
595 | stream->path_name = path_name; | |
596 | stream->channel_name = channel_name; | |
2f9c3030 | 597 | stream->beacon_ts_end = -1ULL; |
7591bab1 | 598 | lttng_ht_node_init_u64(&stream->node, stream->stream_handle); |
cd9adb8b | 599 | pthread_mutex_init(&stream->lock, nullptr); |
7591bab1 MD |
600 | urcu_ref_init(&stream->ref); |
601 | ctf_trace_get(trace); | |
602 | stream->trace = trace; | |
2a174661 | 603 | |
348a81dc JG |
604 | pthread_mutex_lock(&trace->session->lock); |
605 | current_trace_chunk = trace->session->current_trace_chunk; | |
606 | if (current_trace_chunk) { | |
607 | acquired_reference = lttng_trace_chunk_get(current_trace_chunk); | |
608 | } | |
609 | pthread_mutex_unlock(&trace->session->lock); | |
610 | if (!acquired_reference) { | |
611 | ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired", | |
28ab034a | 612 | channel_name); |
7591bab1 MD |
613 | ret = -1; |
614 | goto end; | |
2a174661 DG |
615 | } |
616 | ||
348a81dc JG |
617 | stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
618 | if (!stream->indexes_ht) { | |
619 | ERR("Cannot created indexes_ht"); | |
620 | ret = -1; | |
7591bab1 MD |
621 | goto end; |
622 | } | |
2a174661 | 623 | |
c35f9726 | 624 | pthread_mutex_lock(&stream->lock); |
348a81dc | 625 | ret = stream_set_trace_chunk(stream, current_trace_chunk); |
c35f9726 | 626 | pthread_mutex_unlock(&stream->lock); |
348a81dc JG |
627 | if (ret) { |
628 | ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"", | |
28ab034a JG |
629 | trace->session->session_name, |
630 | stream->channel_name); | |
7591bab1 MD |
631 | ret = -1; |
632 | goto end; | |
2a174661 | 633 | } |
a44ca2ca MD |
634 | stream->tfa = tracefile_array_create(stream->tracefile_count); |
635 | if (!stream->tfa) { | |
636 | ret = -1; | |
637 | goto end; | |
638 | } | |
7591bab1 | 639 | |
28ab034a | 640 | stream->is_metadata = !strcmp(stream->channel_name, DEFAULT_METADATA_NAME); |
7591bab1 MD |
641 | stream->in_recv_list = true; |
642 | ||
643 | /* | |
644 | * Add the stream in the recv list of the session. Once the end stream | |
645 | * message is received, all session streams are published. | |
646 | */ | |
647 | pthread_mutex_lock(&session->recv_list_lock); | |
648 | cds_list_add_rcu(&stream->recv_node, &session->recv_list); | |
649 | session->stream_count++; | |
650 | pthread_mutex_unlock(&session->recv_list_lock); | |
651 | ||
652 | /* | |
653 | * Both in the ctf_trace object and the global stream ht since the data | |
654 | * side of the relayd does not have the concept of session. | |
655 | */ | |
656 | lttng_ht_add_unique_u64(relay_streams_ht, &stream->node); | |
77f7bd85 | 657 | stream->in_stream_ht = true; |
2a174661 | 658 | |
28ab034a JG |
659 | DBG("Relay new stream added %s with ID %" PRIu64, |
660 | stream->channel_name, | |
661 | stream->stream_handle); | |
7591bab1 MD |
662 | ret = 0; |
663 | ||
664 | end: | |
665 | if (ret) { | |
8bb66c3c | 666 | if (stream->file) { |
28ab034a | 667 | fs_handle_close(stream->file); |
cd9adb8b | 668 | stream->file = nullptr; |
2a174661 | 669 | } |
7591bab1 | 670 | stream_put(stream); |
cd9adb8b | 671 | stream = nullptr; |
2a174661 | 672 | } |
317eadef JG |
673 | if (acquired_reference) { |
674 | lttng_trace_chunk_put(current_trace_chunk); | |
675 | } | |
7591bab1 | 676 | return stream; |
2a174661 | 677 | |
7591bab1 MD |
678 | error_no_alloc: |
679 | /* | |
680 | * path_name and channel_name need to be freed explicitly here | |
681 | * because we cannot rely on stream_put(). | |
682 | */ | |
683 | free(path_name); | |
684 | free(channel_name); | |
cd9adb8b | 685 | return nullptr; |
7591bab1 MD |
686 | } |
687 | ||
688 | /* | |
689 | * Called with the session lock held. | |
690 | */ | |
691 | void stream_publish(struct relay_stream *stream) | |
692 | { | |
693 | struct relay_session *session; | |
694 | ||
695 | pthread_mutex_lock(&stream->lock); | |
696 | if (stream->published) { | |
697 | goto unlock; | |
2a174661 DG |
698 | } |
699 | ||
7591bab1 | 700 | session = stream->trace->session; |
2a174661 | 701 | |
7591bab1 MD |
702 | pthread_mutex_lock(&session->recv_list_lock); |
703 | if (stream->in_recv_list) { | |
704 | cds_list_del_rcu(&stream->recv_node); | |
705 | stream->in_recv_list = false; | |
706 | } | |
707 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 708 | |
7591bab1 MD |
709 | pthread_mutex_lock(&stream->trace->stream_list_lock); |
710 | cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list); | |
711 | pthread_mutex_unlock(&stream->trace->stream_list_lock); | |
2a174661 | 712 | |
7591bab1 MD |
713 | stream->published = true; |
714 | unlock: | |
2a174661 | 715 | pthread_mutex_unlock(&stream->lock); |
2a174661 DG |
716 | } |
717 | ||
7591bab1 | 718 | /* |
77f7bd85 | 719 | * Stream must be protected by holding the stream lock or by virtue of being |
ce4d4083 | 720 | * called from stream_destroy. |
7591bab1 MD |
721 | */ |
722 | static void stream_unpublish(struct relay_stream *stream) | |
2a174661 | 723 | { |
77f7bd85 MD |
724 | if (stream->in_stream_ht) { |
725 | struct lttng_ht_iter iter; | |
726 | int ret; | |
727 | ||
728 | iter.iter.node = &stream->node.node; | |
729 | ret = lttng_ht_del(relay_streams_ht, &iter); | |
a0377dfe | 730 | LTTNG_ASSERT(!ret); |
77f7bd85 MD |
731 | stream->in_stream_ht = false; |
732 | } | |
733 | if (stream->published) { | |
734 | pthread_mutex_lock(&stream->trace->stream_list_lock); | |
735 | cds_list_del_rcu(&stream->stream_node); | |
736 | pthread_mutex_unlock(&stream->trace->stream_list_lock); | |
737 | stream->published = false; | |
7591bab1 | 738 | } |
7591bab1 MD |
739 | } |
740 | ||
741 | static void stream_destroy(struct relay_stream *stream) | |
742 | { | |
743 | if (stream->indexes_ht) { | |
49e614cb MD |
744 | /* |
745 | * Calling lttng_ht_destroy in call_rcu worker thread so | |
746 | * we don't hold the RCU read-side lock while calling | |
747 | * it. | |
748 | */ | |
7591bab1 MD |
749 | lttng_ht_destroy(stream->indexes_ht); |
750 | } | |
a44ca2ca MD |
751 | if (stream->tfa) { |
752 | tracefile_array_destroy(stream->tfa); | |
753 | } | |
7591bab1 MD |
754 | free(stream->path_name); |
755 | free(stream->channel_name); | |
756 | free(stream); | |
757 | } | |
758 | ||
759 | static void stream_destroy_rcu(struct rcu_head *rcu_head) | |
760 | { | |
28ab034a | 761 | struct relay_stream *stream = lttng::utils::container_of(rcu_head, &relay_stream::rcu_node); |
7591bab1 MD |
762 | |
763 | stream_destroy(stream); | |
764 | } | |
765 | ||
766 | /* | |
767 | * No need to take stream->lock since this is only called on the final | |
768 | * stream_put which ensures that a single thread may act on the stream. | |
7591bab1 MD |
769 | */ |
770 | static void stream_release(struct urcu_ref *ref) | |
771 | { | |
28ab034a | 772 | struct relay_stream *stream = lttng::utils::container_of(ref, &relay_stream::ref); |
7591bab1 | 773 | struct relay_session *session; |
2a174661 | 774 | |
7591bab1 MD |
775 | session = stream->trace->session; |
776 | ||
777 | DBG("Releasing stream id %" PRIu64, stream->stream_handle); | |
778 | ||
779 | pthread_mutex_lock(&session->recv_list_lock); | |
780 | session->stream_count--; | |
781 | if (stream->in_recv_list) { | |
782 | cds_list_del_rcu(&stream->recv_node); | |
783 | stream->in_recv_list = false; | |
784 | } | |
785 | pthread_mutex_unlock(&session->recv_list_lock); | |
2a174661 | 786 | |
7591bab1 MD |
787 | stream_unpublish(stream); |
788 | ||
8bb66c3c JG |
789 | if (stream->file) { |
790 | fs_handle_close(stream->file); | |
cd9adb8b | 791 | stream->file = nullptr; |
7591bab1 | 792 | } |
f8f3885c MD |
793 | if (stream->index_file) { |
794 | lttng_index_file_put(stream->index_file); | |
cd9adb8b | 795 | stream->index_file = nullptr; |
7591bab1 MD |
796 | } |
797 | if (stream->trace) { | |
798 | ctf_trace_put(stream->trace); | |
cd9adb8b | 799 | stream->trace = nullptr; |
7591bab1 | 800 | } |
c35f9726 | 801 | stream_complete_rotation(stream); |
348a81dc | 802 | lttng_trace_chunk_put(stream->trace_chunk); |
cd9adb8b | 803 | stream->trace_chunk = nullptr; |
7591bab1 MD |
804 | |
805 | call_rcu(&stream->rcu_node, stream_destroy_rcu); | |
2a174661 DG |
806 | } |
807 | ||
7591bab1 | 808 | void stream_put(struct relay_stream *stream) |
2a174661 | 809 | { |
56047f5a | 810 | lttng::urcu::read_lock_guard read_lock; |
a0377dfe | 811 | LTTNG_ASSERT(stream->ref.refcount != 0); |
7591bab1 MD |
812 | /* |
813 | * Wait until we have processed all the stream packets before | |
814 | * actually putting our last stream reference. | |
815 | */ | |
7591bab1 | 816 | urcu_ref_put(&stream->ref, stream_release); |
7591bab1 MD |
817 | } |
818 | ||
c35f9726 | 819 | int stream_set_pending_rotation(struct relay_stream *stream, |
28ab034a JG |
820 | struct lttng_trace_chunk *next_trace_chunk, |
821 | uint64_t rotation_sequence_number) | |
c35f9726 JG |
822 | { |
823 | int ret = 0; | |
824 | const struct relay_stream_rotation rotation = { | |
0f83d1cc MD |
825 | .data_rotated = false, |
826 | .index_rotated = false, | |
827 | .packet_seq_num = rotation_sequence_number, | |
828 | .prev_data_net_seq = -1ULL, | |
c35f9726 JG |
829 | .next_trace_chunk = next_trace_chunk, |
830 | }; | |
831 | ||
832 | if (stream->ongoing_rotation.is_set) { | |
833 | ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)"); | |
834 | ret = -1; | |
835 | goto end; | |
836 | } | |
837 | ||
838 | if (next_trace_chunk) { | |
28ab034a | 839 | const bool reference_acquired = lttng_trace_chunk_get(next_trace_chunk); |
c35f9726 | 840 | |
a0377dfe | 841 | LTTNG_ASSERT(reference_acquired); |
c35f9726 JG |
842 | } |
843 | LTTNG_OPTIONAL_SET(&stream->ongoing_rotation, rotation); | |
844 | ||
0f83d1cc | 845 | DBG("Setting pending rotation: stream_id = %" PRIu64 |
28ab034a JG |
846 | ", rotate_at_packet_seq_num = %" PRIu64, |
847 | stream->stream_handle, | |
848 | rotation_sequence_number); | |
c35f9726 JG |
849 | if (stream->is_metadata) { |
850 | /* | |
851 | * A metadata stream has no index; consider it already rotated. | |
852 | */ | |
853 | stream->ongoing_rotation.value.index_rotated = true; | |
f607fc46 MD |
854 | if (next_trace_chunk) { |
855 | /* | |
856 | * The metadata will be received again in the new chunk. | |
857 | */ | |
858 | stream->metadata_received = 0; | |
859 | } | |
c35f9726 JG |
860 | ret = stream_rotate_data_file(stream); |
861 | } else { | |
0f83d1cc | 862 | ret = try_rotate_stream_index(stream); |
c35f9726 JG |
863 | if (ret < 0) { |
864 | goto end; | |
865 | } | |
866 | ||
0f83d1cc | 867 | ret = try_rotate_stream_data(stream); |
c35f9726 JG |
868 | if (ret < 0) { |
869 | goto end; | |
870 | } | |
871 | } | |
872 | end: | |
873 | return ret; | |
874 | } | |
875 | ||
bda7c7b9 | 876 | void try_stream_close(struct relay_stream *stream) |
7591bab1 | 877 | { |
98ba050e JR |
878 | bool session_aborted; |
879 | struct relay_session *session = stream->trace->session; | |
880 | ||
bda7c7b9 | 881 | DBG("Trying to close stream %" PRIu64, stream->stream_handle); |
98ba050e JR |
882 | |
883 | pthread_mutex_lock(&session->lock); | |
884 | session_aborted = session->aborted; | |
885 | pthread_mutex_unlock(&session->lock); | |
886 | ||
7591bab1 | 887 | pthread_mutex_lock(&stream->lock); |
bda7c7b9 JG |
888 | /* |
889 | * Can be called concurently by connection close and reception of last | |
890 | * pending data. | |
891 | */ | |
892 | if (stream->closed) { | |
893 | pthread_mutex_unlock(&stream->lock); | |
28ab034a JG |
894 | DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", |
895 | stream->stream_handle); | |
bda7c7b9 JG |
896 | return; |
897 | } | |
898 | ||
899 | stream->close_requested = true; | |
3d07a857 MD |
900 | |
901 | if (stream->last_net_seq_num == -1ULL) { | |
902 | /* | |
903 | * Handle connection close without explicit stream close | |
904 | * command. | |
905 | * | |
906 | * We can be clever about indexes partially received in | |
907 | * cases where we received the data socket part, but not | |
908 | * the control socket part: since we're currently closing | |
909 | * the stream on behalf of the control socket, we *know* | |
910 | * there won't be any more control information for this | |
911 | * socket. Therefore, we can destroy all indexes for | |
912 | * which we have received only the file descriptor (from | |
913 | * data socket). This takes care of consumerd crashes | |
914 | * between sending the data and control information for | |
915 | * a packet. Since those are sent in that order, we take | |
916 | * care of consumerd crashes. | |
917 | */ | |
5312a3ed | 918 | DBG("relay_index_close_partial_fd"); |
3d07a857 MD |
919 | relay_index_close_partial_fd(stream); |
920 | /* | |
921 | * Use the highest net_seq_num we currently have pending | |
922 | * As end of stream indicator. Leave last_net_seq_num | |
923 | * at -1ULL if we cannot find any index. | |
924 | */ | |
925 | stream->last_net_seq_num = relay_index_find_last(stream); | |
5312a3ed | 926 | DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num); |
3d07a857 MD |
927 | /* Fall-through into the next check. */ |
928 | } | |
929 | ||
bda7c7b9 | 930 | if (stream->last_net_seq_num != -1ULL && |
28ab034a JG |
931 | ((int64_t) (stream->prev_data_seq - stream->last_net_seq_num)) < 0 && |
932 | !session_aborted) { | |
3d07a857 MD |
933 | /* |
934 | * Don't close since we still have data pending. This | |
935 | * handles cases where an explicit close command has | |
936 | * been received for this stream, and cases where the | |
937 | * connection has been closed, and we are awaiting for | |
938 | * index information from the data socket. It is | |
939 | * therefore expected that all the index fd information | |
940 | * we need has already been received on the control | |
941 | * socket. Matching index information from data socket | |
942 | * should be Expected Soon(TM). | |
943 | * | |
944 | * TODO: We should implement a timer to garbage collect | |
945 | * streams after a timeout to be resilient against a | |
946 | * consumerd implementation that would not match this | |
947 | * expected behavior. | |
948 | */ | |
bda7c7b9 | 949 | pthread_mutex_unlock(&stream->lock); |
28ab034a JG |
950 | DBG("closing stream %" PRIu64 " aborted since it still has data pending", |
951 | stream->stream_handle); | |
bda7c7b9 JG |
952 | return; |
953 | } | |
3d07a857 MD |
954 | /* |
955 | * We received all the indexes we can expect. | |
956 | */ | |
77f7bd85 | 957 | stream_unpublish(stream); |
2229a09c | 958 | stream->closed = true; |
bda7c7b9 | 959 | /* Relay indexes are only used by the "consumer/sessiond" end. */ |
7591bab1 | 960 | relay_index_close_all(stream); |
d91bc249 MD |
961 | |
962 | /* | |
963 | * If we are closed by an application exiting (per-pid buffers), | |
964 | * we need to put our reference on the stream trace chunk right | |
965 | * away, because otherwise still holding the reference on the | |
966 | * trace chunk could allow a viewer stream (which holds a reference | |
967 | * to the stream) to postpone destroy waiting for the chunk to cease | |
968 | * to exist endlessly until the viewer is detached. | |
969 | */ | |
970 | ||
971 | /* Put stream fd before put chunk. */ | |
8bb66c3c JG |
972 | if (stream->file) { |
973 | fs_handle_close(stream->file); | |
cd9adb8b | 974 | stream->file = nullptr; |
d91bc249 MD |
975 | } |
976 | if (stream->index_file) { | |
977 | lttng_index_file_put(stream->index_file); | |
cd9adb8b | 978 | stream->index_file = nullptr; |
d91bc249 MD |
979 | } |
980 | lttng_trace_chunk_put(stream->trace_chunk); | |
cd9adb8b | 981 | stream->trace_chunk = nullptr; |
7591bab1 | 982 | pthread_mutex_unlock(&stream->lock); |
bda7c7b9 | 983 | DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle); |
7591bab1 MD |
984 | stream_put(stream); |
985 | } | |
986 | ||
28ab034a | 987 | int stream_init_packet(struct relay_stream *stream, size_t packet_size, bool *file_rotated) |
c35f9726 JG |
988 | { |
989 | int ret = 0; | |
990 | ||
991 | ASSERT_LOCKED(stream->lock); | |
1ad1103b | 992 | |
8bb66c3c | 993 | if (!stream->file || !stream->trace_chunk) { |
28ab034a JG |
994 | ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64 |
995 | ", channel_name = %s", | |
996 | stream->stream_handle, | |
997 | stream->channel_name); | |
1ad1103b JG |
998 | ret = -1; |
999 | goto end; | |
1000 | } | |
1001 | ||
c35f9726 JG |
1002 | if (caa_likely(stream->tracefile_size == 0)) { |
1003 | /* No size limit set; nothing to check. */ | |
1004 | goto end; | |
1005 | } | |
1006 | ||
1007 | /* | |
1008 | * Check if writing the new packet would exceed the maximal file size. | |
1009 | */ | |
28ab034a | 1010 | if (caa_unlikely((stream->tracefile_size_current + packet_size) > stream->tracefile_size)) { |
c35f9726 | 1011 | const uint64_t new_file_index = |
28ab034a | 1012 | (stream->tracefile_current_index + 1) % stream->tracefile_count; |
c35f9726 JG |
1013 | |
1014 | if (new_file_index < stream->tracefile_current_index) { | |
1015 | stream->tracefile_wrapped_around = true; | |
1016 | } | |
1017 | DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64 | |
28ab034a JG |
1018 | ", current_file_size = %" PRIu64 |
1019 | ", packet_size = %zu, current_file_index = %" PRIu64 | |
1020 | " new_file_index = %" PRIu64, | |
1021 | stream->stream_handle, | |
1022 | stream->tracefile_size_current, | |
1023 | packet_size, | |
1024 | stream->tracefile_current_index, | |
1025 | new_file_index); | |
78118e3b | 1026 | tracefile_array_file_rotate(stream->tfa, TRACEFILE_ROTATE_WRITE); |
c35f9726 JG |
1027 | stream->tracefile_current_index = new_file_index; |
1028 | ||
8bb66c3c | 1029 | if (stream->file) { |
28ab034a | 1030 | fs_handle_close(stream->file); |
cd9adb8b | 1031 | stream->file = nullptr; |
c35f9726 | 1032 | } |
28ab034a JG |
1033 | ret = stream_create_data_output_file_from_trace_chunk( |
1034 | stream, stream->trace_chunk, false, &stream->file); | |
c35f9726 JG |
1035 | if (ret) { |
1036 | ERR("Failed to perform trace file rotation of stream %" PRIu64, | |
28ab034a | 1037 | stream->stream_handle); |
c35f9726 JG |
1038 | goto end; |
1039 | } | |
1040 | ||
1041 | /* | |
1042 | * Reset current size because we just performed a stream | |
1043 | * rotation. | |
1044 | */ | |
68c40154 | 1045 | DBG("%s: reset tracefile_size_current for stream %" PRIu64 " was %" PRIu64, |
28ab034a JG |
1046 | __func__, |
1047 | stream->stream_handle, | |
1048 | stream->tracefile_size_current); | |
c35f9726 JG |
1049 | stream->tracefile_size_current = 0; |
1050 | *file_rotated = true; | |
1051 | } else { | |
1052 | *file_rotated = false; | |
1053 | } | |
1054 | end: | |
1055 | return ret; | |
1056 | } | |
1057 | ||
1058 | /* Note that the packet is not necessarily complete. */ | |
1059 | int stream_write(struct relay_stream *stream, | |
28ab034a JG |
1060 | const struct lttng_buffer_view *packet, |
1061 | size_t padding_len) | |
c35f9726 JG |
1062 | { |
1063 | int ret = 0; | |
1064 | ssize_t write_ret; | |
1065 | size_t padding_to_write = padding_len; | |
1066 | char padding_buffer[FILE_IO_STACK_BUFFER_SIZE]; | |
1067 | ||
1068 | ASSERT_LOCKED(stream->lock); | |
28ab034a | 1069 | memset(padding_buffer, 0, std::min(sizeof(padding_buffer), padding_to_write)); |
c35f9726 | 1070 | |
8bb66c3c | 1071 | if (!stream->file || !stream->trace_chunk) { |
28ab034a JG |
1072 | ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64 |
1073 | ", channel_name = %s", | |
1074 | stream->stream_handle, | |
1075 | stream->channel_name); | |
1ad1103b JG |
1076 | ret = -1; |
1077 | goto end; | |
1078 | } | |
c35f9726 | 1079 | if (packet) { |
28ab034a | 1080 | write_ret = fs_handle_write(stream->file, packet->data, packet->size); |
c35f9726 JG |
1081 | if (write_ret != packet->size) { |
1082 | PERROR("Failed to write to stream file of %sstream %" PRIu64, | |
28ab034a JG |
1083 | stream->is_metadata ? "metadata " : "", |
1084 | stream->stream_handle); | |
c35f9726 JG |
1085 | ret = -1; |
1086 | goto end; | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | while (padding_to_write > 0) { | |
1091 | const size_t padding_to_write_this_pass = | |
28ab034a | 1092 | std::min(padding_to_write, sizeof(padding_buffer)); |
c35f9726 | 1093 | |
28ab034a JG |
1094 | write_ret = |
1095 | fs_handle_write(stream->file, padding_buffer, padding_to_write_this_pass); | |
c35f9726 JG |
1096 | if (write_ret != padding_to_write_this_pass) { |
1097 | PERROR("Failed to write padding to file of %sstream %" PRIu64, | |
28ab034a JG |
1098 | stream->is_metadata ? "metadata " : "", |
1099 | stream->stream_handle); | |
c35f9726 JG |
1100 | ret = -1; |
1101 | goto end; | |
1102 | } | |
1103 | padding_to_write -= padding_to_write_this_pass; | |
1104 | } | |
1105 | ||
1106 | if (stream->is_metadata) { | |
94f73d08 MD |
1107 | size_t recv_len; |
1108 | ||
1109 | recv_len = packet ? packet->size : 0; | |
1110 | recv_len += padding_len; | |
1111 | stream->metadata_received += recv_len; | |
c35f9726 JG |
1112 | } |
1113 | ||
8c865d87 | 1114 | DBG("Wrote to %sstream %" PRIu64 ": data_length = %zu, padding_length = %zu", |
28ab034a JG |
1115 | stream->is_metadata ? "metadata " : "", |
1116 | stream->stream_handle, | |
1117 | packet ? packet->size : (size_t) 0, | |
1118 | padding_len); | |
c35f9726 JG |
1119 | end: |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | /* | |
1124 | * Update index after receiving a packet for a data stream. | |
1125 | * | |
1126 | * Called with the stream lock held. | |
1127 | * | |
1128 | * Return 0 on success else a negative value. | |
1129 | */ | |
28ab034a JG |
1130 | int stream_update_index(struct relay_stream *stream, |
1131 | uint64_t net_seq_num, | |
1132 | bool rotate_index, | |
1133 | bool *flushed, | |
1134 | uint64_t total_size) | |
c35f9726 JG |
1135 | { |
1136 | int ret = 0; | |
1137 | uint64_t data_offset; | |
1138 | struct relay_index *index; | |
1139 | ||
a0377dfe | 1140 | LTTNG_ASSERT(stream->trace_chunk); |
c35f9726 JG |
1141 | ASSERT_LOCKED(stream->lock); |
1142 | /* Get data offset because we are about to update the index. */ | |
1143 | data_offset = htobe64(stream->tracefile_size_current); | |
1144 | ||
1145 | DBG("handle_index_data: stream %" PRIu64 " net_seq_num %" PRIu64 " data offset %" PRIu64, | |
28ab034a JG |
1146 | stream->stream_handle, |
1147 | net_seq_num, | |
1148 | stream->tracefile_size_current); | |
c35f9726 JG |
1149 | |
1150 | /* | |
1151 | * Lookup for an existing index for that stream id/sequence | |
1152 | * number. If it exists, the control thread has already received the | |
1153 | * data for it, thus we need to write it to disk. | |
1154 | */ | |
1155 | index = relay_index_get_by_id_or_create(stream, net_seq_num); | |
1156 | if (!index) { | |
1157 | ret = -1; | |
1158 | goto end; | |
1159 | } | |
1160 | ||
1161 | if (rotate_index || !stream->index_file) { | |
1162 | ret = create_index_file(stream, stream->trace_chunk); | |
1163 | if (ret) { | |
1164 | ERR("Failed to create index file for stream %" PRIu64, | |
28ab034a | 1165 | stream->stream_handle); |
c35f9726 JG |
1166 | /* Put self-ref for this index due to error. */ |
1167 | relay_index_put(index); | |
cd9adb8b | 1168 | index = nullptr; |
c35f9726 JG |
1169 | goto end; |
1170 | } | |
1171 | } | |
1172 | ||
1173 | if (relay_index_set_file(index, stream->index_file, data_offset)) { | |
1174 | ret = -1; | |
1175 | /* Put self-ref for this index due to error. */ | |
1176 | relay_index_put(index); | |
cd9adb8b | 1177 | index = nullptr; |
c35f9726 JG |
1178 | goto end; |
1179 | } | |
1180 | ||
1181 | ret = relay_index_try_flush(index); | |
1182 | if (ret == 0) { | |
78118e3b | 1183 | tracefile_array_file_rotate(stream->tfa, TRACEFILE_ROTATE_READ); |
62f6e9ef | 1184 | tracefile_array_commit_seq(stream->tfa, stream->index_received_seqcount); |
c35f9726 | 1185 | stream->index_received_seqcount++; |
0f83d1cc | 1186 | LTTNG_OPTIONAL_SET(&stream->received_packet_seq_num, |
28ab034a | 1187 | be64toh(index->index_data.packet_seq_num)); |
c35f9726 JG |
1188 | *flushed = true; |
1189 | } else if (ret > 0) { | |
1190 | index->total_size = total_size; | |
1191 | /* No flush. */ | |
1192 | ret = 0; | |
1193 | } else { | |
1194 | /* | |
1195 | * ret < 0 | |
1196 | * | |
1197 | * relay_index_try_flush is responsible for the self-reference | |
1198 | * put of the index object on error. | |
1199 | */ | |
1200 | ERR("relay_index_try_flush error %d", ret); | |
1201 | ret = -1; | |
1202 | } | |
1203 | end: | |
1204 | return ret; | |
1205 | } | |
1206 | ||
28ab034a JG |
1207 | int stream_complete_packet(struct relay_stream *stream, |
1208 | size_t packet_total_size, | |
1209 | uint64_t sequence_number, | |
1210 | bool index_flushed) | |
c35f9726 JG |
1211 | { |
1212 | int ret = 0; | |
1213 | ||
1214 | ASSERT_LOCKED(stream->lock); | |
1215 | ||
1216 | stream->tracefile_size_current += packet_total_size; | |
1217 | if (index_flushed) { | |
28ab034a | 1218 | stream->pos_after_last_complete_data_index = stream->tracefile_size_current; |
c35f9726 JG |
1219 | stream->prev_index_seq = sequence_number; |
1220 | ret = try_rotate_stream_index(stream); | |
1221 | if (ret < 0) { | |
1222 | goto end; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | stream->prev_data_seq = sequence_number; | |
1227 | ret = try_rotate_stream_data(stream); | |
d2ec9b88 | 1228 | |
c35f9726 JG |
1229 | end: |
1230 | return ret; | |
1231 | } | |
1232 | ||
28ab034a | 1233 | int stream_add_index(struct relay_stream *stream, const struct lttcomm_relayd_index *index_info) |
c35f9726 JG |
1234 | { |
1235 | int ret = 0; | |
1236 | struct relay_index *index; | |
1237 | ||
1238 | ASSERT_LOCKED(stream->lock); | |
1239 | ||
68c40154 MD |
1240 | DBG("stream_add_index for stream %" PRIu64, stream->stream_handle); |
1241 | ||
c35f9726 JG |
1242 | /* Live beacon handling */ |
1243 | if (index_info->packet_size == 0) { | |
28ab034a | 1244 | DBG("Received live beacon for stream %" PRIu64, stream->stream_handle); |
c35f9726 JG |
1245 | |
1246 | /* | |
1247 | * Only flag a stream inactive when it has already | |
1248 | * received data and no indexes are in flight. | |
1249 | */ | |
28ab034a | 1250 | if (stream->index_received_seqcount > 0 && stream->indexes_in_flight == 0) { |
c35f9726 JG |
1251 | stream->beacon_ts_end = index_info->timestamp_end; |
1252 | } | |
1253 | ret = 0; | |
1254 | goto end; | |
1255 | } else { | |
1256 | stream->beacon_ts_end = -1ULL; | |
1257 | } | |
1258 | ||
1259 | if (stream->ctf_stream_id == -1ULL) { | |
1260 | stream->ctf_stream_id = index_info->stream_id; | |
1261 | } | |
1262 | ||
1263 | index = relay_index_get_by_id_or_create(stream, index_info->net_seq_num); | |
1264 | if (!index) { | |
1265 | ret = -1; | |
28ab034a | 1266 | ERR("Failed to get or create index %" PRIu64, index_info->net_seq_num); |
c35f9726 JG |
1267 | goto end; |
1268 | } | |
28ab034a | 1269 | if (relay_index_set_control_data(index, index_info, stream->trace->session->minor)) { |
c35f9726 JG |
1270 | ERR("set_index_control_data error"); |
1271 | relay_index_put(index); | |
1272 | ret = -1; | |
1273 | goto end; | |
1274 | } | |
1275 | ret = relay_index_try_flush(index); | |
1276 | if (ret == 0) { | |
78118e3b | 1277 | tracefile_array_file_rotate(stream->tfa, TRACEFILE_ROTATE_READ); |
62f6e9ef | 1278 | tracefile_array_commit_seq(stream->tfa, stream->index_received_seqcount); |
c35f9726 JG |
1279 | stream->index_received_seqcount++; |
1280 | stream->pos_after_last_complete_data_index += index->total_size; | |
1281 | stream->prev_index_seq = index_info->net_seq_num; | |
28ab034a | 1282 | LTTNG_OPTIONAL_SET(&stream->received_packet_seq_num, index_info->packet_seq_num); |
c35f9726 JG |
1283 | |
1284 | ret = try_rotate_stream_index(stream); | |
1285 | if (ret < 0) { | |
1286 | goto end; | |
1287 | } | |
0f83d1cc MD |
1288 | ret = try_rotate_stream_data(stream); |
1289 | if (ret < 0) { | |
1290 | goto end; | |
1291 | } | |
c35f9726 JG |
1292 | } else if (ret > 0) { |
1293 | /* no flush. */ | |
1294 | ret = 0; | |
1295 | } else { | |
1296 | /* | |
1297 | * ret < 0 | |
1298 | * | |
1299 | * relay_index_try_flush is responsible for the self-reference | |
1300 | * put of the index object on error. | |
1301 | */ | |
1302 | ERR("relay_index_try_flush error %d", ret); | |
1303 | ret = -1; | |
1304 | } | |
1305 | end: | |
1306 | return ret; | |
1307 | } | |
1308 | ||
da412cde MD |
1309 | static void print_stream_indexes(struct relay_stream *stream) |
1310 | { | |
1311 | struct lttng_ht_iter iter; | |
1312 | struct relay_index *index; | |
1313 | ||
56047f5a JG |
1314 | { |
1315 | lttng::urcu::read_lock_guard read_lock; | |
1316 | ||
1317 | cds_lfht_for_each_entry (stream->indexes_ht->ht, &iter.iter, index, index_n.node) { | |
1318 | DBG("index %p net_seq_num %" PRIu64 " refcount %ld" | |
1319 | " stream %" PRIu64 " trace %" PRIu64 " session %" PRIu64, | |
1320 | index, | |
1321 | index->index_n.key, | |
1322 | stream->ref.refcount, | |
1323 | index->stream->stream_handle, | |
1324 | index->stream->trace->id, | |
1325 | index->stream->trace->session->id); | |
1326 | } | |
1327 | } | |
da412cde MD |
1328 | } |
1329 | ||
c35f9726 JG |
1330 | int stream_reset_file(struct relay_stream *stream) |
1331 | { | |
1332 | ASSERT_LOCKED(stream->lock); | |
1333 | ||
8bb66c3c JG |
1334 | if (stream->file) { |
1335 | int ret; | |
1336 | ||
1337 | ret = fs_handle_close(stream->file); | |
1338 | if (ret) { | |
1339 | ERR("Failed to close stream file handle: channel name = \"%s\", id = %" PRIu64, | |
28ab034a JG |
1340 | stream->channel_name, |
1341 | stream->stream_handle); | |
8bb66c3c | 1342 | } |
cd9adb8b | 1343 | stream->file = nullptr; |
c35f9726 JG |
1344 | } |
1345 | ||
68c40154 | 1346 | DBG("%s: reset tracefile_size_current for stream %" PRIu64 " was %" PRIu64, |
28ab034a JG |
1347 | __func__, |
1348 | stream->stream_handle, | |
1349 | stream->tracefile_size_current); | |
c35f9726 JG |
1350 | stream->tracefile_size_current = 0; |
1351 | stream->prev_data_seq = 0; | |
1352 | stream->prev_index_seq = 0; | |
1353 | /* Note that this does not reset the tracefile array. */ | |
1354 | stream->tracefile_current_index = 0; | |
1355 | stream->pos_after_last_complete_data_index = 0; | |
1356 | ||
28ab034a JG |
1357 | return stream_create_data_output_file_from_trace_chunk( |
1358 | stream, stream->trace_chunk, true, &stream->file); | |
c35f9726 JG |
1359 | } |
1360 | ||
cd9adb8b | 1361 | void print_relay_streams() |
7591bab1 MD |
1362 | { |
1363 | struct lttng_ht_iter iter; | |
1364 | struct relay_stream *stream; | |
1365 | ||
ce3f3ba3 JG |
1366 | if (!relay_streams_ht) { |
1367 | return; | |
1368 | } | |
1369 | ||
56047f5a JG |
1370 | { |
1371 | lttng::urcu::read_lock_guard read_lock; | |
1372 | ||
1373 | cds_lfht_for_each_entry (relay_streams_ht->ht, &iter.iter, stream, node.node) { | |
1374 | if (!stream_get(stream)) { | |
1375 | continue; | |
1376 | } | |
1377 | ||
1378 | DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64 | |
1379 | " session %" PRIu64, | |
1380 | stream, | |
1381 | stream->ref.refcount, | |
1382 | stream->stream_handle, | |
1383 | stream->trace->id, | |
1384 | stream->trace->session->id); | |
1385 | print_stream_indexes(stream); | |
1386 | stream_put(stream); | |
7591bab1 | 1387 | } |
7591bab1 | 1388 | } |
2a174661 | 1389 | } |