2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_COMP_LOG_SELF_COMP (trace->fs_sink->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (trace->log_level)
25 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRACE"
26 #include "logging/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
32 #include "common/assert.h"
33 #include "ctfser/ctfser.h"
35 #include "translate-trace-ir-to-ctf-ir.h"
36 #include "translate-ctf-ir-to-tsdl.h"
38 #include "fs-sink-trace.h"
39 #include "fs-sink-stream.h"
42 * Sanitizes `path` so as to:
44 * * Replace `.` subdirectories with `_`.
45 * * Replace `..` subdirectories with `__`.
46 * * Remove trailing slashes.
49 GString
*sanitize_trace_path(const char *path
)
51 GString
*san_path
= g_string_new(NULL
);
52 const char *ch
= path
;
53 bool dir_start
= true;
61 /* Start of directory */
63 g_string_append_c(san_path
, *ch
);
72 g_string_append_c(san_path
, '_');
80 g_string_append(san_path
, "__");
94 /* Not a special character */
95 g_string_append_c(san_path
, *ch
);
100 /* Remove trailing slashes */
101 while (san_path
->len
> 0 &&
102 san_path
->str
[san_path
->len
- 1] == '/') {
103 /* Remove trailing slash */
104 g_string_set_size(san_path
, san_path
->len
- 1);
107 if (san_path
->len
== 0) {
108 /* Looks like there's nothing left: just use `trace` */
109 g_string_assign(san_path
, "trace");
116 * Find a path based on `path` that doesn't exist yet. First, try `path`
117 * itself, then try with incrementing suffixes.
121 GString
*make_unique_trace_path(const char *path
)
123 GString
*unique_path
;
124 unsigned int suffix
= 0;
126 unique_path
= g_string_new(path
);
128 while (g_file_test(unique_path
->str
, G_FILE_TEST_EXISTS
)) {
129 g_string_printf(unique_path
, "%s-%u", path
, suffix
);
137 * Disable `deprecated-declarations` warnings for
138 * lttng_validate_datetime() because we're using `GTimeVal` and
139 * g_time_val_from_iso8601() which are deprecated since GLib 2.56
140 * (Babeltrace supports older versions too).
142 #pragma GCC diagnostic push
143 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146 * Validate that the input string `datetime` is an ISO8601-compliant string (the
147 * format used by LTTng in the metadata).
151 int lttng_validate_datetime(const struct fs_sink_trace
*trace
,
152 const char *datetime
)
158 * We are using g_time_val_from_iso8601, as the safer/more modern
159 * alternative, g_date_time_new_from_iso8601, is only available in
160 * glib >= 2.56, and this is sufficient for our use case of validating
163 if (!g_time_val_from_iso8601(datetime
, &tv
)) {
164 BT_COMP_LOGI("Couldn't parse datetime as ISO 8601: date=\"%s\"", datetime
);
174 #pragma GCC diagnostic pop
177 int append_lttng_trace_path_ust_uid(const struct fs_sink_trace
*trace
,
178 GString
*path
, const bt_trace
*tc
)
183 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "tracer_buffering_id");
184 if (!v
|| !bt_value_is_signed_integer(v
)) {
185 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_id\"");
189 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRId64
,
190 bt_value_integer_signed_get(v
));
192 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "architecture_bit_width");
193 if (!v
|| !bt_value_is_signed_integer(v
)) {
194 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"architecture_bit_width\"");
198 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRIu64
"-bit",
199 bt_value_integer_signed_get(v
));
212 int append_lttng_trace_path_ust_pid(const struct fs_sink_trace
*trace
,
213 GString
*path
, const bt_trace
*tc
)
216 const char *datetime
;
219 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "procname");
220 if (!v
|| !bt_value_is_string(v
)) {
221 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"procname\"");
225 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
227 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid");
228 if (!v
|| !bt_value_is_signed_integer(v
)) {
229 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid\"");
233 g_string_append_printf(path
, "-%" PRId64
, bt_value_integer_signed_get(v
));
235 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid_datetime");
236 if (!v
|| !bt_value_is_string(v
)) {
237 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid_datetime\"");
241 datetime
= bt_value_string_get(v
);
243 if (lttng_validate_datetime(trace
, datetime
)) {
247 g_string_append_printf(path
, "-%s", datetime
);
260 * Try to build a trace path based on environment values put in the trace
261 * environment by the LTTng tracer, starting with version 2.11.
264 GString
*make_lttng_trace_path_rel(const struct fs_sink_trace
*trace
)
267 const char *tracer_name
, *domain
, *datetime
;
268 int64_t tracer_major
, tracer_minor
;
271 path
= g_string_new(NULL
);
276 v
= bt_trace_borrow_environment_entry_value_by_name_const(
277 trace
->ir_trace
, "tracer_name");
278 if (!v
|| !bt_value_is_string(v
)) {
279 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_name\"");
283 tracer_name
= bt_value_string_get(v
);
285 if (!g_str_equal(tracer_name
, "lttng-ust")
286 && !g_str_equal(tracer_name
, "lttng-modules")) {
287 BT_COMP_LOGI("Unrecognized tracer name: name=\"%s\"", tracer_name
);
291 v
= bt_trace_borrow_environment_entry_value_by_name_const(
292 trace
->ir_trace
, "tracer_major");
293 if (!v
|| !bt_value_is_signed_integer(v
)) {
294 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_major\"");
298 tracer_major
= bt_value_integer_signed_get(v
);
300 v
= bt_trace_borrow_environment_entry_value_by_name_const(
301 trace
->ir_trace
, "tracer_minor");
302 if (!v
|| !bt_value_is_signed_integer(v
)) {
303 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_minor\"");
307 tracer_minor
= bt_value_integer_signed_get(v
);
309 if (!(tracer_major
>= 3 || (tracer_major
== 2 && tracer_minor
>= 11))) {
310 BT_COMP_LOGI("Unsupported LTTng version for automatic trace path: major=%" PRId64
", minor=%" PRId64
,
311 tracer_major
, tracer_minor
);
315 v
= bt_trace_borrow_environment_entry_value_by_name_const(
316 trace
->ir_trace
, "hostname");
317 if (!v
|| !bt_value_is_string(v
)) {
318 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_hostname\"");
322 g_string_assign(path
, bt_value_string_get(v
));
324 v
= bt_trace_borrow_environment_entry_value_by_name_const(
325 trace
->ir_trace
, "trace_name");
326 if (!v
|| !bt_value_is_string(v
)) {
327 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_name\"");
331 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
333 v
= bt_trace_borrow_environment_entry_value_by_name_const(
334 trace
->ir_trace
, "trace_creation_datetime");
335 if (!v
|| !bt_value_is_string(v
)) {
336 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_creation_datetime\"");
340 datetime
= bt_value_string_get(v
);
342 if (lttng_validate_datetime(trace
, datetime
)) {
346 g_string_append_printf(path
, "-%s", datetime
);
348 v
= bt_trace_borrow_environment_entry_value_by_name_const(
349 trace
->ir_trace
, "domain");
350 if (!v
|| !bt_value_is_string(v
)) {
351 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"domain\"");
355 domain
= bt_value_string_get(v
);
356 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", domain
);
358 if (g_str_equal(domain
, "ust")) {
359 const char *tracer_buffering_scheme
;
361 v
= bt_trace_borrow_environment_entry_value_by_name_const(
362 trace
->ir_trace
, "tracer_buffering_scheme");
363 if (!v
|| !bt_value_is_string(v
)) {
364 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_scheme\"");
368 tracer_buffering_scheme
= bt_value_string_get(v
);
369 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", tracer_buffering_scheme
);
371 if (g_str_equal(tracer_buffering_scheme
, "uid")) {
372 if (append_lttng_trace_path_ust_uid(trace
, path
,
376 } else if (g_str_equal(tracer_buffering_scheme
, "pid")){
377 if (append_lttng_trace_path_ust_pid(trace
, path
,
382 /* Unknown buffering scheme. */
383 BT_COMP_LOGI("Unknown buffering scheme: tracer_buffering_scheme=\"%s\"", tracer_buffering_scheme
);
386 } else if (!g_str_equal(domain
, "kernel")) {
387 /* Unknown domain. */
388 BT_COMP_LOGI("Unknown domain: domain=\"%s\"", domain
);
396 g_string_free(path
, TRUE
);
404 /* Build the relative output path for `trace`. */
407 GString
*make_trace_path_rel(const struct fs_sink_trace
*trace
)
409 GString
*path
= NULL
;
410 const char *trace_name
;
412 if (trace
->fs_sink
->assume_single_trace
) {
413 /* Use output directory directly */
414 path
= g_string_new("");
418 /* First, try to build a path using environment fields written by LTTng. */
419 path
= make_lttng_trace_path_rel(trace
);
424 /* Otherwise, use the trace name, if available. */
425 trace_name
= bt_trace_get_name(trace
->ir_trace
);
427 path
= g_string_new(trace_name
);
431 /* Otherwise, use "trace". */
432 path
= g_string_new("trace");
439 * Compute the trace output path for `trace`, rooted at `output_base_directory`.
443 GString
*make_trace_path(const struct fs_sink_trace
*trace
, const char *output_base_directory
)
445 GString
*rel_path
= NULL
;
446 GString
*rel_path_san
= NULL
;
447 GString
*full_path
= NULL
;
448 GString
*unique_full_path
= NULL
;
450 rel_path
= make_trace_path_rel(trace
);
455 rel_path_san
= sanitize_trace_path(rel_path
->str
);
460 full_path
= g_string_new(NULL
);
465 g_string_printf(full_path
, "%s" G_DIR_SEPARATOR_S
"%s",
466 output_base_directory
, rel_path_san
->str
);
468 unique_full_path
= make_unique_trace_path(full_path
->str
);
472 g_string_free(rel_path
, TRUE
);
476 g_string_free(rel_path_san
, TRUE
);
480 g_string_free(full_path
, TRUE
);
483 return unique_full_path
;
487 void fs_sink_trace_destroy(struct fs_sink_trace
*trace
)
489 GString
*tsdl
= NULL
;
497 if (trace
->ir_trace_destruction_listener_id
!= UINT64_C(-1)) {
499 * Remove the destruction listener, otherwise it could
500 * be called in the future, and its private data is this
501 * CTF FS sink trace object which won't exist anymore.
503 (void) bt_trace_remove_destruction_listener(trace
->ir_trace
,
504 trace
->ir_trace_destruction_listener_id
);
505 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
508 if (trace
->streams
) {
509 g_hash_table_destroy(trace
->streams
);
510 trace
->streams
= NULL
;
513 tsdl
= g_string_new(NULL
);
515 translate_trace_ctf_ir_to_tsdl(trace
->trace
, tsdl
);
517 BT_ASSERT(trace
->metadata_path
);
518 fh
= fopen(trace
->metadata_path
->str
, "wb");
520 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
521 "cannot open metadata file for writing",
522 ": path=\"%s\"", trace
->metadata_path
->str
);
526 len
= fwrite(tsdl
->str
, sizeof(*tsdl
->str
), tsdl
->len
, fh
);
527 if (len
!= tsdl
->len
) {
528 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
529 "cannot write metadata file",
530 ": path=\"%s\"", trace
->metadata_path
->str
);
534 if (!trace
->fs_sink
->quiet
) {
535 printf("Created CTF trace `%s`.\n", trace
->path
->str
);
539 g_string_free(trace
->path
, TRUE
);
544 int ret
= fclose(fh
);
547 BT_COMP_LOGW_ERRNO("In trace destruction listener: "
548 "cannot close metadata file",
549 ": path=\"%s\"", trace
->metadata_path
->str
);
553 g_string_free(trace
->metadata_path
, TRUE
);
554 trace
->metadata_path
= NULL
;
556 fs_sink_ctf_trace_destroy(trace
->trace
);
560 g_string_free(tsdl
, TRUE
);
567 void ir_trace_destruction_listener(const bt_trace
*ir_trace
, void *data
)
569 struct fs_sink_trace
*trace
= data
;
572 * Prevent bt_trace_remove_destruction_listener() from being
573 * called in fs_sink_trace_destroy(), which is called by
574 * g_hash_table_remove() below.
576 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
577 g_hash_table_remove(trace
->fs_sink
->traces
, ir_trace
);
581 struct fs_sink_trace
*fs_sink_trace_create(struct fs_sink_comp
*fs_sink
,
582 const bt_trace
*ir_trace
)
585 struct fs_sink_trace
*trace
= g_new0(struct fs_sink_trace
, 1);
586 bt_trace_add_listener_status trace_status
;
592 trace
->log_level
= fs_sink
->log_level
;
593 trace
->fs_sink
= fs_sink
;
594 trace
->ir_trace
= ir_trace
;
595 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
596 trace
->trace
= translate_trace_trace_ir_to_ctf_ir(fs_sink
, ir_trace
);
601 trace
->path
= make_trace_path(trace
, fs_sink
->output_dir_path
->str
);
602 BT_ASSERT(trace
->path
);
603 ret
= g_mkdir_with_parents(trace
->path
->str
, 0755);
605 BT_COMP_LOGE_ERRNO("Cannot create directories for trace directory",
606 ": path=\"%s\"", trace
->path
->str
);
610 trace
->metadata_path
= g_string_new(trace
->path
->str
);
611 BT_ASSERT(trace
->metadata_path
);
612 g_string_append(trace
->metadata_path
, "/metadata");
613 trace
->streams
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
614 NULL
, (GDestroyNotify
) fs_sink_stream_destroy
);
615 BT_ASSERT(trace
->streams
);
616 trace_status
= bt_trace_add_destruction_listener(ir_trace
,
617 ir_trace_destruction_listener
, trace
,
618 &trace
->ir_trace_destruction_listener_id
);
623 g_hash_table_insert(fs_sink
->traces
, (gpointer
) ir_trace
, trace
);
627 fs_sink_trace_destroy(trace
);