From b587e35ada5d54e7768474a3f3703d92f4eaaa61 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Tue, 1 Aug 2023 15:33:17 -0700 Subject: [PATCH] libsysprof: add test to convert .ninja_log to trace marks --- src/libsysprof/tests/meson.build | 2 + src/libsysprof/tests/ninja-to-marks.c | 95 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/libsysprof/tests/ninja-to-marks.c diff --git a/src/libsysprof/tests/meson.build b/src/libsysprof/tests/meson.build index 75c97256..45c6bdcd 100644 --- a/src/libsysprof/tests/meson.build +++ b/src/libsysprof/tests/meson.build @@ -12,6 +12,7 @@ libsysprof_testsuite_c_args = [ libsysprof_testsuite = { 'read-build-id' : {'skip': true}, + 'ninja-to-marks' : {'skip': true}, 'test-allocs-by-func' : {'skip': true}, 'test-callgraph' : {'skip': true}, 'test-capture-model' : {'skip': true}, @@ -34,6 +35,7 @@ libsysprof_testsuite = { libsysprof_testsuite_deps = [ libsysprof_static_dep, + liblinereader_static_dep, ] foreach test, params: libsysprof_testsuite diff --git a/src/libsysprof/tests/ninja-to-marks.c b/src/libsysprof/tests/ninja-to-marks.c new file mode 100644 index 00000000..3189d23a --- /dev/null +++ b/src/libsysprof/tests/ninja-to-marks.c @@ -0,0 +1,95 @@ +/* ninja-to-marks.c + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include + +#include "line-reader-private.h" + +int +main (int argc, + char *argv[]) +{ + SysprofCaptureWriter *writer; + GError *error = NULL; + gint64 start_time; + gint64 max_end_time; + + sysprof_clock_init (); + + if (!(writer = sysprof_capture_writer_new ("ninja.log.syscap", 0))) + g_error ("Failed to open syscap"); + + start_time = max_end_time = g_get_monotonic_time (); + + for (int i = 1; i < argc; i++) + { + g_autofree char *contents = NULL; + LineReader reader; + char *line; + gsize len; + gsize line_len; + + if (!g_file_get_contents (argv[i], &contents, &len, &error)) + { + g_printerr ("Error: %s\n", error->message); + return 1; + } + + line_reader_init (&reader, contents, len); + while ((line = line_reader_next (&reader, &line_len))) + { + g_auto(GStrv) parts = NULL; + gint64 begin_time; + gint64 end_time; + + line[line_len] = 0; + + if (line[0] == '#') + continue; + + parts = g_strsplit (line, "\t", 5); + if (g_strv_length (parts) != 5) + continue; + + begin_time = g_ascii_strtoll (parts[0], NULL, 10) * (SYSPROF_NSEC_PER_SEC/1000); + end_time = g_ascii_strtoll (parts[1], NULL, 10) * (SYSPROF_NSEC_PER_SEC/1000); + + if (end_time > max_end_time) + max_end_time = end_time; + + sysprof_capture_writer_add_mark (writer, + start_time + begin_time, + -1, + -1, + end_time - begin_time, + "ninja", parts[3], parts[4]); + + } + } + + _sysprof_capture_writer_set_time_range (writer, + start_time, + start_time + max_end_time); + + sysprof_capture_writer_flush (writer); + sysprof_capture_writer_unref (writer); + + return 0; +}