Fix: print usage even if omit arguments are received
[babeltrace.git] / plugins / ctf / fs / data-stream.c
CommitLineData
e98a2d6e
PP
1/*
2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
fc9a526c 3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
e98a2d6e
PP
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
e98a2d6e
PP
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdint.h>
27#include <stdlib.h>
28#include <stdbool.h>
29#include <glib.h>
30#include <inttypes.h>
31#include <sys/mman.h>
32#include <babeltrace/ctf-ir/stream.h>
78586d8a
JG
33#include <babeltrace/plugin/notification/iterator.h>
34#include "file.h"
35#include "metadata.h"
36#include "../common/notif-iter/notif-iter.h"
37#include <assert.h>
5b29e799 38#include "data-stream.h"
e98a2d6e
PP
39
40#define PRINT_ERR_STREAM ctf_fs->error_fp
41#define PRINT_PREFIX "ctf-fs-data-stream"
42#include "print.h"
43
5b29e799
JG
44static
45size_t remaining_mmap_bytes(struct ctf_fs_stream *stream)
e98a2d6e 46{
fc9a526c 47 return stream->mmap_valid_len - stream->request_offset;
e98a2d6e
PP
48}
49
5b29e799
JG
50static
51int stream_munmap(struct ctf_fs_stream *stream)
e98a2d6e 52{
fc9a526c 53 int ret = 0;
56a1cced 54 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
55
56 if (munmap(stream->mmap_addr, stream->mmap_len)) {
57 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
58 stream->mmap_addr, stream->mmap_len,
59 stream->file->path->str, stream->file->fp,
60 strerror(errno));
fc9a526c
JG
61 ret = -1;
62 goto end;
e98a2d6e 63 }
fc9a526c
JG
64end:
65 return ret;
e98a2d6e
PP
66}
67
5b29e799
JG
68static
69int mmap_next(struct ctf_fs_stream *stream)
e98a2d6e 70{
e98a2d6e 71 int ret = 0;
fc9a526c 72 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
73
74 /* Unmap old region */
75 if (stream->mmap_addr) {
76 if (stream_munmap(stream)) {
77 goto error;
78 }
79
fc9a526c
JG
80 stream->mmap_offset += stream->mmap_valid_len;
81 stream->request_offset = 0;
e98a2d6e
PP
82 }
83
fc9a526c
JG
84 stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset,
85 stream->mmap_max_len);
86 /* Round up to next page, assuming page size being a power of 2. */
87 stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1)
88 & ~(ctf_fs->page_size - 1);
e98a2d6e
PP
89 /* Map new region */
90 stream->mmap_addr = mmap((void *) 0, stream->mmap_len,
78586d8a
JG
91 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
92 stream->mmap_offset);
e98a2d6e
PP
93 if (stream->mmap_addr == MAP_FAILED) {
94 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
78586d8a
JG
95 stream->mmap_len, stream->file->path->str,
96 stream->file->fp, stream->mmap_offset,
97 strerror(errno));
e98a2d6e
PP
98 goto error;
99 }
100
101 goto end;
e98a2d6e
PP
102error:
103 stream_munmap(stream);
104 ret = -1;
e98a2d6e
PP
105end:
106 return ret;
107}
108
5b29e799
JG
109static
110enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
111 size_t request_sz, uint8_t **buffer_addr,
112 size_t *buffer_sz, void *data)
113{
114 enum bt_ctf_notif_iter_medium_status status =
115 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
116 struct ctf_fs_stream *stream = data;
56a1cced 117 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
118
119 if (request_sz == 0) {
120 goto end;
121 }
122
e98a2d6e
PP
123 /* Check if we have at least one memory-mapped byte left */
124 if (remaining_mmap_bytes(stream) == 0) {
125 /* Are we at the end of the file? */
fc9a526c 126 if (stream->request_offset >= stream->file->size) {
e98a2d6e
PP
127 PDBG("Reached end of file \"%s\" (%p)\n",
128 stream->file->path->str, stream->file->fp);
129 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
130 goto end;
131 }
132
133 if (mmap_next(stream)) {
134 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
135 stream->file->path->str, stream->file->fp);
136 goto error;
137 }
138 }
139
140 *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz);
fc9a526c 141 *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset;
e98a2d6e
PP
142 stream->request_offset += *buffer_sz;
143 goto end;
144
145error:
146 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
147
148end:
149 return status;
150}
151
5b29e799
JG
152static
153struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
154 struct bt_ctf_stream_class *stream_class, void *data)
155{
156 struct ctf_fs_stream *fs_stream = data;
56a1cced 157 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
e98a2d6e
PP
158
159 if (!fs_stream->stream) {
160 int64_t id = bt_ctf_stream_class_get_id(stream_class);
161
162 PDBG("Creating stream out of stream class %" PRId64 "\n", id);
163 fs_stream->stream = bt_ctf_stream_create(stream_class,
fc9a526c 164 fs_stream->file->path->str);
e98a2d6e
PP
165 if (!fs_stream->stream) {
166 PERR("Cannot create stream (stream class %" PRId64 ")\n",
fc9a526c 167 id);
e98a2d6e
PP
168 }
169 }
170
171 return fs_stream->stream;
172}
173
174static struct bt_ctf_notif_iter_medium_ops medops = {
175 .request_bytes = medop_request_bytes,
176 .get_stream = medop_get_stream,
177};
178
b6c3dcb2
JG
179static
180int build_index_from_idx_file(struct ctf_fs_stream *stream)
181{
182 int ret = 0;
183 gchar *directory = NULL;
184 gchar *basename = NULL;
185 GString *index_basename = NULL;
186 gchar *index_file_path = NULL;
187 GMappedFile *mapped_file = NULL;
188 gsize filesize;
189 const struct ctf_packet_index_file_hdr *header;
190 const char *mmap_begin, *file_pos;
191 struct index_entry *index;
192 uint64_t total_packets_size = 0;
193 size_t file_index_entry_size;
194 size_t file_entry_count;
195 size_t i;
196
197 /* Look for index file in relative path index/name.idx. */
198 basename = g_path_get_basename(stream->file->path->str);
199 if (!basename) {
200 ret = -1;
201 goto end;
202 }
203
204 directory = g_path_get_dirname(stream->file->path->str);
205 if (!directory) {
206 ret = -1;
207 goto end;
208 }
209
210 index_basename = g_string_new(basename);
211 if (!index_basename) {
212 ret = -1;
213 goto end;
214 }
215
216 g_string_append(index_basename, ".idx");
217 index_file_path = g_build_filename(directory, "index",
218 index_basename->str, NULL);
219 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3
JG
220 if (!mapped_file) {
221 ret = -1;
222 goto end;
223 }
b6c3dcb2
JG
224 filesize = g_mapped_file_get_length(mapped_file);
225 if (filesize < sizeof(*header)) {
226 printf_error("Invalid LTTng trace index: file size < header size");
227 ret = -1;
228 goto end;
229 }
230
231 mmap_begin = g_mapped_file_get_contents(mapped_file);
232 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
233
234 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
235 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
236 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
237 ret = -1;
238 goto end;
239 }
b6c3dcb2
JG
240
241 file_index_entry_size = be32toh(header->packet_index_len);
242 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
243 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
244 printf_error("Invalid index file size; not a multiple of index entry size");
245 ret = -1;
246 goto end;
247 }
248
249 stream->index.entries = g_array_sized_new(FALSE, TRUE,
250 sizeof(struct index_entry), file_entry_count);
251 if (!stream->index.entries) {
252 ret = -1;
253 goto end;
254 }
255 index = (struct index_entry *) stream->index.entries->data;
256 for (i = 0; i < file_entry_count; i++) {
257 struct ctf_packet_index *file_index =
258 (struct ctf_packet_index *) file_pos;
259 uint64_t packet_size = be64toh(file_index->packet_size);
260
261 if (packet_size % CHAR_BIT) {
262 ret = -1;
263 printf_error("Invalid packet size encountered in index file");
264 goto invalid_index;
265 }
266
267 /* Convert size in bits to bytes. */
268 packet_size /= CHAR_BIT;
269 index->packet_size = packet_size;
270
271 index->offset = be64toh(file_index->offset);
272 if (i != 0 && index->offset < (index - 1)->offset) {
273 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
274 ret = -1;
275 goto invalid_index;
276 }
277
278 index->timestamp_begin = be64toh(file_index->timestamp_begin);
279 index->timestamp_end = be64toh(file_index->timestamp_end);
280 if (index->timestamp_end < index->timestamp_begin) {
281 printf_error("Invalid packet time bounds encountered in index file");
282 ret = -1;
283 goto invalid_index;
284 }
285
286 total_packets_size += packet_size;
287 file_pos += file_index_entry_size;
288 index++;
289 }
290
291 /* Validate that the index addresses the complete stream. */
292 if (stream->file->size != total_packets_size) {
293 printf_error("Invalid index; indexed size != stream file size");
294 ret = -1;
295 goto invalid_index;
296 }
297end:
298 g_free(directory);
299 g_free(basename);
300 g_free(index_file_path);
06367fa3
JG
301 if (index_basename) {
302 g_string_free(index_basename, TRUE);
303 }
304 if (mapped_file) {
305 g_mapped_file_unref(mapped_file);
306 }
b6c3dcb2
JG
307 return ret;
308invalid_index:
309 g_array_free(stream->index.entries, TRUE);
310 goto end;
311}
312
313static
314int build_index_from_stream(struct ctf_fs_stream *stream)
315{
316 int ret = 0;
317end:
318 return ret;
319}
320
321static
322int init_stream_index(struct ctf_fs_stream *stream)
323{
324 int ret;
325
326 ret = build_index_from_idx_file(stream);
327 if (!ret) {
328 goto end;
329 }
330
331 ret = build_index_from_stream(stream);
332end:
333 return ret;
334}
335
e7a4393b
JG
336BT_HIDDEN
337struct ctf_fs_stream *ctf_fs_stream_create(
56a1cced 338 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
e98a2d6e 339{
b6c3dcb2 340 int ret;
e98a2d6e
PP
341 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
342
343 if (!stream) {
344 goto error;
345 }
346
347 stream->file = file;
5b29e799 348 stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata->trace,
e7a4393b 349 ctf_fs->page_size, medops, stream, ctf_fs->error_fp);
e98a2d6e
PP
350 if (!stream->notif_iter) {
351 goto error;
352 }
5b29e799 353
fc9a526c 354 stream->mmap_max_len = ctf_fs->page_size * 2048;
b6c3dcb2
JG
355 ret = init_stream_index(stream);
356 if (ret) {
357 goto error;
358 }
e98a2d6e 359 goto end;
e98a2d6e 360error:
78586d8a 361 /* Do not touch "borrowed" file. */
e98a2d6e
PP
362 stream->file = NULL;
363 ctf_fs_stream_destroy(stream);
364 stream = NULL;
e98a2d6e
PP
365end:
366 return stream;
367}
368
5b29e799
JG
369BT_HIDDEN
370void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
e98a2d6e 371{
5b29e799
JG
372 if (!stream) {
373 return;
043e2020
JG
374 }
375
5b29e799
JG
376 if (stream->file) {
377 ctf_fs_file_destroy(stream->file);
e98a2d6e
PP
378 }
379
5b29e799
JG
380 if (stream->stream) {
381 BT_PUT(stream->stream);
043e2020 382 }
5b29e799
JG
383
384 if (stream->notif_iter) {
385 bt_ctf_notif_iter_destroy(stream->notif_iter);
78586d8a 386 }
5b29e799 387
b6c3dcb2
JG
388 if (stream->index.entries) {
389 g_array_free(stream->index.entries, TRUE);
390 }
391
5b29e799 392 g_free(stream);
e98a2d6e 393}
This page took 0.04181 seconds and 4 git commands to generate.