From 8dd16259287f58f9273002717ec4d27e97127719 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:43:14 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- tools/performance/Perfetto.cpp | 19 +++++++ tools/performance/Perfetto.h | 117 +++++++++++++++++++++++++++++++++++++++++ tools/performance/moz.build | 5 ++ 3 files changed, 141 insertions(+) create mode 100644 tools/performance/Perfetto.cpp create mode 100644 tools/performance/Perfetto.h (limited to 'tools/performance') diff --git a/tools/performance/Perfetto.cpp b/tools/performance/Perfetto.cpp new file mode 100644 index 0000000000..1fef9c3b78 --- /dev/null +++ b/tools/performance/Perfetto.cpp @@ -0,0 +1,19 @@ +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/Perfetto.h" +#include + +PERFETTO_TRACK_EVENT_STATIC_STORAGE(); + +void InitPerfetto() { + if (!getenv("MOZ_DISABLE_PERFETTO")) { + perfetto::TracingInitArgs args; + args.backends |= perfetto::kSystemBackend; + perfetto::Tracing::Initialize(args); + perfetto::TrackEvent::Register(); + } +} diff --git a/tools/performance/Perfetto.h b/tools/performance/Perfetto.h new file mode 100644 index 0000000000..5a2989ce95 --- /dev/null +++ b/tools/performance/Perfetto.h @@ -0,0 +1,117 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_Perfetto_h +#define mozilla_Perfetto_h + +#ifdef MOZ_PERFETTO +# include "perfetto/perfetto.h" +# include "mozilla/TimeStamp.h" + +// Initialization is called when a content process is created. +// This can be called multiple times. +extern void InitPerfetto(); + +/* Perfetto Tracing: + * + * This file provides an interface to the perfetto tracing API. The API from + * the perfetto sdk can be used directly, but an additional set of macros + * prefixed with PERFETTO_* have been defined to help minimize the use of + * ifdef's. + * + * The common perfetto macros require a category and name at the very least. + * These must be static strings, or wrapped with perfetto::DynamicString if + * dynamic. If the string is static, but provided through a runtime pointer, + * then it must be wrapped with perfetto::StaticString. + * + * You can also provide additional parameters such as a timestamp, + * or a lambda to add additional information to the trace marker. + * For more info, see https://perfetto.dev/docs/instrumentation/tracing-sdk + * + * Examples: + * + * // Add a trace event to measure work inside a block, + * // using static strings only. + * + * { + * PERFETTO_TRACE_EVENT("js", "JS::RunScript"); + * run_script(); + * } + * + * // Add a trace event to measure work inside a block, + * // using a dynamic string. + * + * void runScript(nsCString& scriptName) + * { + * PERFETTO_TRACE_EVENT("js", perfetto::DynamicString{scriptName.get()}); + * run_script(); + * } + * + * // Add a trace event using a dynamic category and name. + * + * void runScript(nsCString& categoryName, nsCString& scriptName) + * { + * perfetto::DynamicCategory category{category.get()}; + * PERFETTO_TRACE_EVENT(category, perfetto::DynamicString{scriptName.get()}); + * run_script(); + * } + * + * // Add a trace event to measure two arbitrary points of code. + * // Events in the same category must always be nested. + * + * void startWork() { + * PERFETTO_TRACE_EVENT_BEGIN("js", "StartWork"); + * ... + * PERFETTO_TRACE_EVENT_END("js"); + * } + * + * // Create a trace marker for an event that has already occurred + * // using previously saved timestamps. + * + * void record_event(TimeStamp startTimeStamp, TimeStamp endTimeStamp) + * { + * PERFETTO_TRACE_EVENT_BEGIN("js", "Some Event", startTimeStamp); + * PERFETTO_TRACE_EVENT_END("js", endTimeStamp); + * } + */ + +// Wrap the common trace event macros from perfetto so +// they can be called without #ifdef's. +# define PERFETTO_TRACE_EVENT(...) TRACE_EVENT(__VA_ARGS__) +# define PERFETTO_TRACE_EVENT_BEGIN(...) TRACE_EVENT_BEGIN(__VA_ARGS__) +# define PERFETTO_TRACE_EVENT_END(...) TRACE_EVENT_END(__VA_ARGS__) + +namespace perfetto { +// Specialize custom timestamps for mozilla::TimeStamp. +template <> +struct TraceTimestampTraits { + static inline TraceTimestamp ConvertTimestampToTraceTimeNs( + const mozilla::TimeStamp& timestamp) { + return {protos::gen::BuiltinClock::BUILTIN_CLOCK_MONOTONIC, + timestamp.RawClockMonotonicNanosecondsSinceBoot()}; + } +}; +} // namespace perfetto + +// Categories can be added dynamically, but to minimize overhead +// all categories should be pre-defined here whenever possible. +PERFETTO_DEFINE_CATEGORIES(perfetto::Category("task"), + perfetto::Category("usertiming")); + +#else // !defined(MOZ_PERFETTO) +# define PERFETTO_TRACE_EVENT(...) \ + do { \ + } while (0) +# define PERFETTO_TRACE_EVENT_BEGIN(...) \ + do { \ + } while (0) +# define PERFETTO_TRACE_EVENT_END(...) \ + do { \ + } while (0) +inline void InitPerfetto() {} +#endif // MOZ_PERFETTO + +#endif // mozilla_Perfetto_h diff --git a/tools/performance/moz.build b/tools/performance/moz.build index ea82c83126..10f2b69cf2 100644 --- a/tools/performance/moz.build +++ b/tools/performance/moz.build @@ -9,9 +9,14 @@ UNIFIED_SOURCES += [ ] EXPORTS.mozilla += [ + "Perfetto.h", "PerfStats.h", ] include("/ipc/chromium/chromium-config.mozbuild") +if CONFIG["MOZ_PERFETTO"]: + UNIFIED_SOURCES += ["Perfetto.cpp"] + DIRS += ["/third_party/perfetto/"] + FINAL_LIBRARY = "xul" -- cgit v1.2.3