2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP (trace->fs_sink->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (trace->log_level)
9 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRACE"
10 #include "logging/comp-logging.h"
12 #include <babeltrace2/babeltrace.h>
16 #include "common/assert.h"
17 #include "ctfser/ctfser.h"
19 #include "translate-trace-ir-to-ctf-ir.hpp"
20 #include "translate-ctf-ir-to-tsdl.hpp"
21 #include "fs-sink.hpp"
22 #include "fs-sink-trace.hpp"
23 #include "fs-sink-stream.hpp"
26 * Sanitizes `path` so as to:
28 * * Replace `.` subdirectories with `_`.
29 * * Replace `..` subdirectories with `__`.
30 * * Remove trailing slashes.
32 static GString
*sanitize_trace_path(const char *path
)
34 GString
*san_path
= g_string_new(NULL
);
35 const char *ch
= path
;
36 bool dir_start
= true;
44 /* Start of directory */
46 g_string_append_c(san_path
, *ch
);
55 g_string_append_c(san_path
, '_');
63 g_string_append(san_path
, "__");
77 /* Not a special character */
78 g_string_append_c(san_path
, *ch
);
83 /* Remove trailing slashes */
84 while (san_path
->len
> 0 && san_path
->str
[san_path
->len
- 1] == '/') {
85 /* Remove trailing slash */
86 g_string_set_size(san_path
, san_path
->len
- 1);
89 if (san_path
->len
== 0) {
90 /* Looks like there's nothing left: just use `trace` */
91 g_string_assign(san_path
, "trace");
98 * Find a path based on `path` that doesn't exist yet. First, try `path`
99 * itself, then try with incrementing suffixes.
102 static GString
*make_unique_trace_path(const char *path
)
104 GString
*unique_path
;
105 unsigned int suffix
= 0;
107 unique_path
= g_string_new(path
);
109 while (g_file_test(unique_path
->str
, G_FILE_TEST_EXISTS
)) {
110 g_string_printf(unique_path
, "%s-%u", path
, suffix
);
118 * Disable `deprecated-declarations` warnings for
119 * lttng_validate_datetime() because we're using `GTimeVal` and
120 * g_time_val_from_iso8601() which are deprecated since GLib 2.56
121 * (Babeltrace supports older versions too).
123 #pragma GCC diagnostic push
124 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
127 * Validate that the input string `datetime` is an ISO8601-compliant string (the
128 * format used by LTTng in the metadata).
131 static int lttng_validate_datetime(const struct fs_sink_trace
*trace
, const char *datetime
)
137 * We are using g_time_val_from_iso8601, as the safer/more modern
138 * alternative, g_date_time_new_from_iso8601, is only available in
139 * glib >= 2.56, and this is sufficient for our use case of validating
142 if (!g_time_val_from_iso8601(datetime
, &tv
)) {
143 BT_COMP_LOGI("Couldn't parse datetime as ISO 8601: date=\"%s\"", datetime
);
153 #pragma GCC diagnostic pop
155 static int append_lttng_trace_path_ust_uid(const struct fs_sink_trace
*trace
, GString
*path
,
161 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "tracer_buffering_id");
162 if (!v
|| !bt_value_is_signed_integer(v
)) {
163 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_id\"");
167 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRId64
, bt_value_integer_signed_get(v
));
169 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "architecture_bit_width");
170 if (!v
|| !bt_value_is_signed_integer(v
)) {
171 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"architecture_bit_width\"");
175 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRIu64
"-bit",
176 bt_value_integer_signed_get(v
));
188 static int append_lttng_trace_path_ust_pid(const struct fs_sink_trace
*trace
, GString
*path
,
192 const char *datetime
;
195 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "procname");
196 if (!v
|| !bt_value_is_string(v
)) {
197 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"procname\"");
201 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
203 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid");
204 if (!v
|| !bt_value_is_signed_integer(v
)) {
205 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid\"");
209 g_string_append_printf(path
, "-%" PRId64
, bt_value_integer_signed_get(v
));
211 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid_datetime");
212 if (!v
|| !bt_value_is_string(v
)) {
213 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid_datetime\"");
217 datetime
= bt_value_string_get(v
);
219 if (lttng_validate_datetime(trace
, datetime
)) {
223 g_string_append_printf(path
, "-%s", datetime
);
236 * Try to build a trace path based on environment values put in the trace
237 * environment by the LTTng tracer, starting with version 2.11.
239 static GString
*make_lttng_trace_path_rel(const struct fs_sink_trace
*trace
)
242 const char *tracer_name
, *domain
, *datetime
;
243 int64_t tracer_major
, tracer_minor
;
246 path
= g_string_new(NULL
);
251 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_name");
252 if (!v
|| !bt_value_is_string(v
)) {
253 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_name\"");
257 tracer_name
= bt_value_string_get(v
);
259 if (!g_str_equal(tracer_name
, "lttng-ust") && !g_str_equal(tracer_name
, "lttng-modules")) {
260 BT_COMP_LOGI("Unrecognized tracer name: name=\"%s\"", tracer_name
);
264 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_major");
265 if (!v
|| !bt_value_is_signed_integer(v
)) {
266 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_major\"");
270 tracer_major
= bt_value_integer_signed_get(v
);
272 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_minor");
273 if (!v
|| !bt_value_is_signed_integer(v
)) {
274 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_minor\"");
278 tracer_minor
= bt_value_integer_signed_get(v
);
280 if (!(tracer_major
>= 3 || (tracer_major
== 2 && tracer_minor
>= 11))) {
281 BT_COMP_LOGI("Unsupported LTTng version for automatic trace path: major=%" PRId64
283 tracer_major
, tracer_minor
);
287 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "hostname");
288 if (!v
|| !bt_value_is_string(v
)) {
289 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_hostname\"");
293 g_string_assign(path
, bt_value_string_get(v
));
295 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "trace_name");
296 if (!v
|| !bt_value_is_string(v
)) {
297 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_name\"");
301 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
303 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
,
304 "trace_creation_datetime");
305 if (!v
|| !bt_value_is_string(v
)) {
306 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_creation_datetime\"");
310 datetime
= bt_value_string_get(v
);
312 if (lttng_validate_datetime(trace
, datetime
)) {
316 g_string_append_printf(path
, "-%s", datetime
);
318 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "domain");
319 if (!v
|| !bt_value_is_string(v
)) {
320 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"domain\"");
324 domain
= bt_value_string_get(v
);
325 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", domain
);
327 if (g_str_equal(domain
, "ust")) {
328 const char *tracer_buffering_scheme
;
330 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
,
331 "tracer_buffering_scheme");
332 if (!v
|| !bt_value_is_string(v
)) {
333 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_scheme\"");
337 tracer_buffering_scheme
= bt_value_string_get(v
);
338 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", tracer_buffering_scheme
);
340 if (g_str_equal(tracer_buffering_scheme
, "uid")) {
341 if (append_lttng_trace_path_ust_uid(trace
, path
, trace
->ir_trace
)) {
344 } else if (g_str_equal(tracer_buffering_scheme
, "pid")) {
345 if (append_lttng_trace_path_ust_pid(trace
, path
, trace
->ir_trace
)) {
349 /* Unknown buffering scheme. */
350 BT_COMP_LOGI("Unknown buffering scheme: tracer_buffering_scheme=\"%s\"",
351 tracer_buffering_scheme
);
354 } else if (!g_str_equal(domain
, "kernel")) {
355 /* Unknown domain. */
356 BT_COMP_LOGI("Unknown domain: domain=\"%s\"", domain
);
364 g_string_free(path
, TRUE
);
372 /* Build the relative output path for `trace`. */
374 static GString
*make_trace_path_rel(const struct fs_sink_trace
*trace
)
376 GString
*path
= NULL
;
377 const char *trace_name
;
379 BT_ASSERT(!trace
->fs_sink
->assume_single_trace
);
381 /* First, try to build a path using environment fields written by LTTng. */
382 path
= make_lttng_trace_path_rel(trace
);
387 /* Otherwise, use the trace name, if available. */
388 trace_name
= bt_trace_get_name(trace
->ir_trace
);
390 path
= g_string_new(trace_name
);
394 /* Otherwise, use "trace". */
395 path
= g_string_new("trace");
402 * Compute the trace output path for `trace`, rooted at `output_base_directory`.
405 static GString
*make_trace_path(const struct fs_sink_trace
*trace
,
406 const char *output_base_directory
)
408 GString
*rel_path
= NULL
;
409 GString
*rel_path_san
= NULL
;
410 GString
*full_path
= NULL
;
411 GString
*unique_full_path
= NULL
;
413 if (trace
->fs_sink
->assume_single_trace
) {
414 /* Use output directory directly */
415 unique_full_path
= g_string_new(output_base_directory
);
416 if (!unique_full_path
) {
420 rel_path
= make_trace_path_rel(trace
);
425 rel_path_san
= sanitize_trace_path(rel_path
->str
);
430 full_path
= g_string_new(NULL
);
435 g_string_printf(full_path
, "%s" G_DIR_SEPARATOR_S
"%s", output_base_directory
,
438 unique_full_path
= make_unique_trace_path(full_path
->str
);
443 g_string_free(rel_path
, TRUE
);
447 g_string_free(rel_path_san
, TRUE
);
451 g_string_free(full_path
, TRUE
);
454 return unique_full_path
;
457 void fs_sink_trace_destroy(struct fs_sink_trace
*trace
)
459 GString
*tsdl
= NULL
;
467 if (trace
->ir_trace_destruction_listener_id
!= UINT64_C(-1)) {
469 * Remove the destruction listener, otherwise it could
470 * be called in the future, and its private data is this
471 * CTF FS sink trace object which won't exist anymore.
473 (void) bt_trace_remove_destruction_listener(trace
->ir_trace
,
474 trace
->ir_trace_destruction_listener_id
);
475 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
478 if (trace
->streams
) {
479 g_hash_table_destroy(trace
->streams
);
480 trace
->streams
= NULL
;
483 tsdl
= g_string_new(NULL
);
485 translate_trace_ctf_ir_to_tsdl(trace
->trace
, tsdl
);
487 BT_ASSERT(trace
->metadata_path
);
488 fh
= fopen(trace
->metadata_path
->str
, "wb");
490 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
491 "cannot open metadata file for writing",
492 ": path=\"%s\"", trace
->metadata_path
->str
);
496 len
= fwrite(tsdl
->str
, sizeof(*tsdl
->str
), tsdl
->len
, fh
);
497 if (len
!= tsdl
->len
) {
498 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
499 "cannot write metadata file",
500 ": path=\"%s\"", trace
->metadata_path
->str
);
504 if (!trace
->fs_sink
->quiet
) {
505 printf("Created CTF trace `%s`.\n", trace
->path
->str
);
509 g_string_free(trace
->path
, TRUE
);
514 int ret
= fclose(fh
);
517 BT_COMP_LOGW_ERRNO("In trace destruction listener: "
518 "cannot close metadata file",
519 ": path=\"%s\"", trace
->metadata_path
->str
);
523 g_string_free(trace
->metadata_path
, TRUE
);
524 trace
->metadata_path
= NULL
;
526 fs_sink_ctf_trace_destroy(trace
->trace
);
530 g_string_free(tsdl
, TRUE
);
536 static void ir_trace_destruction_listener(const bt_trace
*ir_trace
, void *data
)
538 struct fs_sink_trace
*trace
= (fs_sink_trace
*) data
;
541 * Prevent bt_trace_remove_destruction_listener() from being
542 * called in fs_sink_trace_destroy(), which is called by
543 * g_hash_table_remove() below.
545 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
546 g_hash_table_remove(trace
->fs_sink
->traces
, ir_trace
);
549 struct fs_sink_trace
*fs_sink_trace_create(struct fs_sink_comp
*fs_sink
, const bt_trace
*ir_trace
)
552 struct fs_sink_trace
*trace
= g_new0(struct fs_sink_trace
, 1);
553 bt_trace_add_listener_status trace_status
;
559 trace
->log_level
= fs_sink
->log_level
;
560 trace
->fs_sink
= fs_sink
;
561 trace
->ir_trace
= ir_trace
;
562 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
563 trace
->trace
= translate_trace_trace_ir_to_ctf_ir(fs_sink
, ir_trace
);
568 trace
->path
= make_trace_path(trace
, fs_sink
->output_dir_path
->str
);
569 BT_ASSERT(trace
->path
);
570 ret
= g_mkdir_with_parents(trace
->path
->str
, 0755);
572 BT_COMP_LOGE_ERRNO("Cannot create directories for trace directory", ": path=\"%s\"",
577 trace
->metadata_path
= g_string_new(trace
->path
->str
);
578 BT_ASSERT(trace
->metadata_path
);
579 g_string_append(trace
->metadata_path
, "/metadata");
580 trace
->streams
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
581 (GDestroyNotify
) fs_sink_stream_destroy
);
582 BT_ASSERT(trace
->streams
);
583 trace_status
= bt_trace_add_destruction_listener(ir_trace
, ir_trace_destruction_listener
, trace
,
584 &trace
->ir_trace_destruction_listener_id
);
589 g_hash_table_insert(fs_sink
->traces
, (gpointer
) ir_trace
, trace
);
593 fs_sink_trace_destroy(trace
);