| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// This header contains basic definitions required to create marker types, and |
| 8 |
// to add markers to the profiler buffers. |
| 9 |
// |
| 10 |
// In most cases, #include "mozilla/BaseProfilerMarkers.h" instead, or |
| 11 |
// #include "mozilla/BaseProfilerMarkerTypes.h" for common marker types. |
| 12 |
|
| 13 |
#ifndef BaseProfilerMarkersPrerequisites_h |
| 14 |
#define BaseProfilerMarkersPrerequisites_h |
| 15 |
|
| 16 |
namespace mozilla { |
| 17 |
|
| 18 |
enum class StackCaptureOptions { |
| 19 |
NoStack, // No stack captured. |
| 20 |
Full, // Capture a full stack, including label frames, JS frames and |
| 21 |
// native frames. |
| 22 |
NonNative, // Capture a stack without native frames for reduced overhead. |
| 23 |
}; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
#include "BaseProfileJSONWriter.h" |
| 28 |
#include "BaseProfilingCategory.h" |
| 29 |
#include "mozilla/Maybe.h" |
| 30 |
#include "mozilla/ProfileChunkedBuffer.h" |
| 31 |
#include "mozilla/BaseProfilerState.h" |
| 32 |
#include "mozilla/TimeStamp.h" |
| 33 |
#include "mozilla/UniquePtr.h" |
| 34 |
#include "mozilla/Variant.h" |
| 35 |
|
| 36 |
#include <initializer_list> |
| 37 |
#include <string_view> |
| 38 |
#include <string> |
| 39 |
#include <type_traits> |
| 40 |
#include <utility> |
| 41 |
#include <vector> |
| 42 |
|
| 43 |
// The header <X11/X.h> defines "None" as a macro that expands to "0L". |
| 44 |
// This is terrible because we have an enum variant named "None" too in this |
| 45 |
// file. To work around this, we undefine the macro "None". |
| 46 |
#ifdef None |
| 47 |
# undef None |
| 48 |
#endif |
| 49 |
|
| 50 |
namespace mozilla { |
| 51 |
|
| 52 |
// Return a NotNull<const CHAR*> pointing at the literal empty string `""`. |
| 53 |
template <typename CHAR> |
| 54 |
constexpr const CHAR* LiteralEmptyStringPointer() { |
| 55 |
static_assert(std::is_same_v<CHAR, char> || std::is_same_v<CHAR, char16_t>, |
| 56 |
"Only char and char16_t are supported in Firefox"); |
| 57 |
if constexpr (std::is_same_v<CHAR, char>) { |
| 58 |
return ""; |
| 59 |
} |
| 60 |
if constexpr (std::is_same_v<CHAR, char16_t>) { |
| 61 |
return u""; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Return a string_view<CHAR> pointing at the literal empty string. |
| 66 |
template <typename CHAR> |
| 67 |
constexpr std::basic_string_view<CHAR> LiteralEmptyStringView() { |
| 68 |
static_assert(std::is_same_v<CHAR, char> || std::is_same_v<CHAR, char16_t>, |
| 69 |
"Only char and char16_t are supported in Firefox"); |
| 70 |
// Use `operator""sv()` from <string_view>. |
| 71 |
using namespace std::literals::string_view_literals; |
| 72 |
if constexpr (std::is_same_v<CHAR, char>) { |
| 73 |
return ""sv; |
| 74 |
} |
| 75 |
if constexpr (std::is_same_v<CHAR, char16_t>) { |
| 76 |
return u""sv; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// General string view, optimized for short on-stack life before serialization, |
| 81 |
// and between deserialization and JSON-streaming. |
| 82 |
template <typename CHAR> |
| 83 |
class MOZ_STACK_CLASS ProfilerStringView { |
| 84 |
public: |
| 85 |
// Default constructor points at "" (literal empty string). |
| 86 |
constexpr ProfilerStringView() = default; |
| 87 |
|
| 88 |
// Don't allow copy. |
| 89 |
ProfilerStringView(const ProfilerStringView&) = delete; |
| 90 |
ProfilerStringView& operator=(const ProfilerStringView&) = delete; |
| 91 |
|
| 92 |
// Allow move. For consistency the moved-from string is always reset to "". |
| 93 |
constexpr ProfilerStringView(ProfilerStringView&& aOther) |
| 94 |
: mStringView(std::move(aOther.mStringView)), |
| 95 |
mOwnership(aOther.mOwnership) { |
| 96 |
if (mOwnership == Ownership::OwnedThroughStringView) { |
| 97 |
// We now own the buffer, make the other point at the literal "". |
| 98 |
aOther.mStringView = LiteralEmptyStringView<CHAR>(); |
| 99 |
aOther.mOwnership = Ownership::Literal; |
| 100 |
} |
| 101 |
} |
| 102 |
constexpr ProfilerStringView& operator=(ProfilerStringView&& aOther) { |
| 103 |
mStringView = std::move(aOther.mStringView); |
| 104 |
mOwnership = aOther.mOwnership; |
| 105 |
if (mOwnership == Ownership::OwnedThroughStringView) { |
| 106 |
// We now own the buffer, make the other point at the literal "". |
| 107 |
aOther.mStringView = LiteralEmptyStringView<CHAR>(); |
| 108 |
aOther.mOwnership = Ownership::Literal; |
| 109 |
} |
| 110 |
return *this; |
| 111 |
} |
| 112 |
|
| 113 |
~ProfilerStringView() { |
| 114 |
if (MOZ_UNLIKELY(mOwnership == Ownership::OwnedThroughStringView)) { |
| 115 |
// We own the buffer pointed at by mStringView, destroy it. |
| 116 |
// This is only used between deserialization and streaming. |
| 117 |
delete mStringView.data(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// Implicit construction from nullptr, points at "" (literal empty string). |
| 122 |
constexpr MOZ_IMPLICIT ProfilerStringView(decltype(nullptr)) {} |
| 123 |
|
| 124 |
// Implicit constructor from a literal string. |
| 125 |
template <size_t Np1> |
| 126 |
constexpr MOZ_IMPLICIT ProfilerStringView(const CHAR (&aLiteralString)[Np1]) |
| 127 |
: ProfilerStringView(aLiteralString, Np1 - 1, Ownership::Literal) {} |
| 128 |
|
| 129 |
// Constructor from a non-literal string. |
| 130 |
constexpr ProfilerStringView(const CHAR* aString, size_t aLength) |
| 131 |
: ProfilerStringView(aString, aLength, Ownership::Reference) {} |
| 132 |
|
| 133 |
// Implicit constructor from a string_view. |
| 134 |
constexpr MOZ_IMPLICIT ProfilerStringView( |
| 135 |
const std::basic_string_view<CHAR>& aStringView) |
| 136 |
: ProfilerStringView(aStringView.data(), aStringView.length(), |
| 137 |
Ownership::Reference) {} |
| 138 |
|
| 139 |
// Implicit constructor from an expiring string_view. We assume that the |
| 140 |
// pointed-at string will outlive this ProfilerStringView. |
| 141 |
constexpr MOZ_IMPLICIT ProfilerStringView( |
| 142 |
std::basic_string_view<CHAR>&& aStringView) |
| 143 |
: ProfilerStringView(aStringView.data(), aStringView.length(), |
| 144 |
Ownership::Reference) {} |
| 145 |
|
| 146 |
// Implicit constructor from std::string. |
| 147 |
constexpr MOZ_IMPLICIT ProfilerStringView( |
| 148 |
const std::basic_string<CHAR>& aString) |
| 149 |
: ProfilerStringView(aString.data(), aString.length(), |
| 150 |
Ownership::Reference) {} |
| 151 |
|
| 152 |
// Construction from a raw pointer to a null-terminated string. |
| 153 |
// This is a named class-static function to make it more obvious where work is |
| 154 |
// being done (to determine the string length), and encourage users to instead |
| 155 |
// provide a length, if already known. |
| 156 |
// TODO: Find callers and convert them to constructor instead if possible. |
| 157 |
static constexpr ProfilerStringView WrapNullTerminatedString( |
| 158 |
const CHAR* aString) { |
| 159 |
return ProfilerStringView( |
| 160 |
aString, aString ? std::char_traits<CHAR>::length(aString) : 0, |
| 161 |
Ownership::Reference); |
| 162 |
} |
| 163 |
|
| 164 |
// Implicit constructor for an object with member functions `Data()` |
| 165 |
// `Length()`, and `IsLiteral()`, common in xpcom strings. |
| 166 |
template < |
| 167 |
typename String, |
| 168 |
typename DataReturnType = decltype(std::declval<const String>().Data()), |
| 169 |
typename LengthReturnType = |
| 170 |
decltype(std::declval<const String>().Length()), |
| 171 |
typename IsLiteralReturnType = |
| 172 |
decltype(std::declval<const String>().IsLiteral()), |
| 173 |
typename = |
| 174 |
std::enable_if_t<std::is_convertible_v<DataReturnType, const CHAR*> && |
| 175 |
std::is_integral_v<LengthReturnType> && |
| 176 |
std::is_same_v<IsLiteralReturnType, bool>>> |
| 177 |
constexpr MOZ_IMPLICIT ProfilerStringView(const String& aString) |
| 178 |
: ProfilerStringView( |
| 179 |
static_cast<const CHAR*>(aString.Data()), aString.Length(), |
| 180 |
aString.IsLiteral() ? Ownership::Literal : Ownership::Reference) {} |
| 181 |
|
| 182 |
[[nodiscard]] constexpr const std::basic_string_view<CHAR>& StringView() |
| 183 |
const { |
| 184 |
return mStringView; |
| 185 |
} |
| 186 |
|
| 187 |
[[nodiscard]] constexpr size_t Length() const { return mStringView.length(); } |
| 188 |
|
| 189 |
[[nodiscard]] constexpr bool IsLiteral() const { |
| 190 |
return mOwnership == Ownership::Literal; |
| 191 |
} |
| 192 |
[[nodiscard]] constexpr bool IsReference() const { |
| 193 |
return mOwnership == Ownership::Reference; |
| 194 |
} |
| 195 |
// No `IsOwned...()` because it's a secret, only used internally! |
| 196 |
|
| 197 |
[[nodiscard]] Span<const CHAR> AsSpan() const { |
| 198 |
return Span<const CHAR>(mStringView.data(), mStringView.length()); |
| 199 |
} |
| 200 |
[[nodiscard]] operator Span<const CHAR>() const { return AsSpan(); } |
| 201 |
|
| 202 |
private: |
| 203 |
enum class Ownership { Literal, Reference, OwnedThroughStringView }; |
| 204 |
|
| 205 |
// Allow deserializer to store anything here. |
| 206 |
friend ProfileBufferEntryReader::Deserializer<ProfilerStringView>; |
| 207 |
|
| 208 |
constexpr ProfilerStringView(const CHAR* aString, size_t aLength, |
| 209 |
Ownership aOwnership) |
| 210 |
: mStringView(aString ? std::basic_string_view<CHAR>(aString, aLength) |
| 211 |
: LiteralEmptyStringView<CHAR>()), |
| 212 |
mOwnership(aString ? aOwnership : Ownership::Literal) {} |
| 213 |
|
| 214 |
// String view to an outside string (literal or reference). |
| 215 |
// We may actually own the pointed-at buffer, but it is only used internally |
| 216 |
// between deserialization and JSON streaming. |
| 217 |
std::basic_string_view<CHAR> mStringView = LiteralEmptyStringView<CHAR>(); |
| 218 |
|
| 219 |
Ownership mOwnership = Ownership::Literal; |
| 220 |
}; |
| 221 |
|
| 222 |
using ProfilerString8View = ProfilerStringView<char>; |
| 223 |
using ProfilerString16View = ProfilerStringView<char16_t>; |
| 224 |
|
| 225 |
// This compulsory marker parameter contains the required category information. |
| 226 |
class MarkerCategory { |
| 227 |
public: |
| 228 |
// Constructor from category pair (includes both super- and sub-categories). |
| 229 |
constexpr explicit MarkerCategory( |
| 230 |
baseprofiler::ProfilingCategoryPair aCategoryPair) |
| 231 |
: mCategoryPair(aCategoryPair) {} |
| 232 |
|
| 233 |
// Returns the stored category pair. |
| 234 |
constexpr baseprofiler::ProfilingCategoryPair CategoryPair() const { |
| 235 |
return mCategoryPair; |
| 236 |
} |
| 237 |
|
| 238 |
// Returns the super-category from the stored category pair. |
| 239 |
baseprofiler::ProfilingCategory GetCategory() const { |
| 240 |
return GetProfilingCategoryPairInfo(mCategoryPair).mCategory; |
| 241 |
} |
| 242 |
|
| 243 |
private: |
| 244 |
baseprofiler::ProfilingCategoryPair mCategoryPair = |
| 245 |
baseprofiler::ProfilingCategoryPair::OTHER; |
| 246 |
}; |
| 247 |
|
| 248 |
namespace baseprofiler::category { |
| 249 |
|
| 250 |
// Each category pair name constructs a MarkerCategory. |
| 251 |
// E.g.: mozilla::baseprofiler::category::OTHER_Profiling |
| 252 |
// Profiler macros will take the category name alone without namespace. |
| 253 |
// E.g.: `PROFILER_MARKER_UNTYPED("name", OTHER_Profiling)` |
| 254 |
#define CATEGORY_ENUM_BEGIN_CATEGORY(name, labelAsString, color) |
| 255 |
#define CATEGORY_ENUM_SUBCATEGORY(supercategory, name, labelAsString) \ |
| 256 |
static constexpr MarkerCategory name{ProfilingCategoryPair::name}; |
| 257 |
#define CATEGORY_ENUM_END_CATEGORY |
| 258 |
MOZ_PROFILING_CATEGORY_LIST(CATEGORY_ENUM_BEGIN_CATEGORY, |
| 259 |
CATEGORY_ENUM_SUBCATEGORY, |
| 260 |
CATEGORY_ENUM_END_CATEGORY) |
| 261 |
#undef CATEGORY_ENUM_BEGIN_CATEGORY |
| 262 |
#undef CATEGORY_ENUM_SUBCATEGORY |
| 263 |
#undef CATEGORY_ENUM_END_CATEGORY |
| 264 |
|
| 265 |
// Import `MarkerCategory` into this namespace. This will allow using this type |
| 266 |
// dynamically in macros that prepend `::mozilla::baseprofiler::category::` to |
| 267 |
// the given category, e.g.: |
| 268 |
// `PROFILER_MARKER_UNTYPED("name", MarkerCategory(...))` |
| 269 |
using MarkerCategory = ::mozilla::MarkerCategory; |
| 270 |
|
| 271 |
} // namespace baseprofiler::category |
| 272 |
|
| 273 |
// The classes below are all embedded in a `MarkerOptions` object. |
| 274 |
class MarkerOptions; |
| 275 |
|
| 276 |
// This marker option captures a given thread id. |
| 277 |
// If left unspecified (by default construction) during the add-marker call, the |
| 278 |
// current thread id will be used then. |
| 279 |
class MarkerThreadId { |
| 280 |
public: |
| 281 |
// Default constructor, keeps the thread id unspecified. |
| 282 |
constexpr MarkerThreadId() = default; |
| 283 |
|
| 284 |
// Constructor from a given thread id. |
| 285 |
constexpr explicit MarkerThreadId( |
| 286 |
baseprofiler::BaseProfilerThreadId aThreadId) |
| 287 |
: mThreadId(aThreadId) {} |
| 288 |
|
| 289 |
// Use the current thread's id. |
| 290 |
static MarkerThreadId CurrentThread() { |
| 291 |
return MarkerThreadId(baseprofiler::profiler_current_thread_id()); |
| 292 |
} |
| 293 |
|
| 294 |
// Use the main thread's id. This can be useful to record a marker from a |
| 295 |
// possibly-unregistered thread, and display it in the main thread track. |
| 296 |
static MarkerThreadId MainThread() { |
| 297 |
return MarkerThreadId(baseprofiler::profiler_main_thread_id()); |
| 298 |
} |
| 299 |
|
| 300 |
[[nodiscard]] constexpr baseprofiler::BaseProfilerThreadId ThreadId() const { |
| 301 |
return mThreadId; |
| 302 |
} |
| 303 |
|
| 304 |
[[nodiscard]] constexpr bool IsUnspecified() const { |
| 305 |
return !mThreadId.IsSpecified(); |
| 306 |
} |
| 307 |
|
| 308 |
private: |
| 309 |
baseprofiler::BaseProfilerThreadId mThreadId; |
| 310 |
}; |
| 311 |
|
| 312 |
// This marker option contains marker timing information. |
| 313 |
// This class encapsulates the logic for correctly storing a marker based on its |
| 314 |
// Use the static methods to create the MarkerTiming. This is a transient object |
| 315 |
// that is being used to enforce the constraints of the combinations of the |
| 316 |
// data. |
| 317 |
class MarkerTiming { |
| 318 |
public: |
| 319 |
// The following static methods are used to create the MarkerTiming based on |
| 320 |
// the type that it is. |
| 321 |
|
| 322 |
static MarkerTiming InstantAt(const TimeStamp& aTime) { |
| 323 |
MOZ_ASSERT(!aTime.IsNull(), "Time is null for an instant marker."); |
| 324 |
return MarkerTiming{aTime, TimeStamp{}, MarkerTiming::Phase::Instant}; |
| 325 |
} |
| 326 |
|
| 327 |
static MarkerTiming InstantNow() { return InstantAt(TimeStamp::Now()); } |
| 328 |
|
| 329 |
static MarkerTiming Interval(const TimeStamp& aStartTime, |
| 330 |
const TimeStamp& aEndTime) { |
| 331 |
MOZ_ASSERT(!aStartTime.IsNull(), |
| 332 |
"Start time is null for an interval marker."); |
| 333 |
MOZ_ASSERT(!aEndTime.IsNull(), "End time is null for an interval marker."); |
| 334 |
return MarkerTiming{aStartTime, aEndTime, MarkerTiming::Phase::Interval}; |
| 335 |
} |
| 336 |
|
| 337 |
static MarkerTiming IntervalUntilNowFrom(const TimeStamp& aStartTime) { |
| 338 |
return Interval(aStartTime, TimeStamp::Now()); |
| 339 |
} |
| 340 |
|
| 341 |
static MarkerTiming IntervalStart(const TimeStamp& aTime = TimeStamp::Now()) { |
| 342 |
MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval start marker."); |
| 343 |
return MarkerTiming{aTime, TimeStamp{}, MarkerTiming::Phase::IntervalStart}; |
| 344 |
} |
| 345 |
|
| 346 |
static MarkerTiming IntervalEnd(const TimeStamp& aTime = TimeStamp::Now()) { |
| 347 |
MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval end marker."); |
| 348 |
return MarkerTiming{TimeStamp{}, aTime, MarkerTiming::Phase::IntervalEnd}; |
| 349 |
} |
| 350 |
|
| 351 |
// Set the interval end in this timing. |
| 352 |
// If there was already a start time, this makes it a full interval. |
| 353 |
void SetIntervalEnd(const TimeStamp& aTime = TimeStamp::Now()) { |
| 354 |
MOZ_ASSERT(!aTime.IsNull(), "Time is null for an interval end marker."); |
| 355 |
mEndTime = aTime; |
| 356 |
mPhase = mStartTime.IsNull() ? Phase::IntervalEnd : Phase::Interval; |
| 357 |
} |
| 358 |
|
| 359 |
[[nodiscard]] const TimeStamp& StartTime() const { return mStartTime; } |
| 360 |
[[nodiscard]] const TimeStamp& EndTime() const { return mEndTime; } |
| 361 |
|
| 362 |
// The phase differentiates Instant markers from Interval markers. |
| 363 |
// Interval markers can either carry both timestamps on a single marker, |
| 364 |
// or they can be split into individual Start and End markers, which are |
| 365 |
// associated with each other via the marker name. |
| 366 |
// |
| 367 |
// The numeric representation of this enum value is also exposed in the |
| 368 |
// ETW trace event's Phase field. |
| 369 |
enum class Phase : uint8_t { |
| 370 |
Instant = 0, |
| 371 |
Interval = 1, |
| 372 |
IntervalStart = 2, |
| 373 |
IntervalEnd = 3, |
| 374 |
}; |
| 375 |
|
| 376 |
[[nodiscard]] Phase MarkerPhase() const { |
| 377 |
MOZ_ASSERT(!IsUnspecified()); |
| 378 |
return mPhase; |
| 379 |
} |
| 380 |
|
| 381 |
// The following getter methods are used to put the value into the buffer for |
| 382 |
// storage. |
| 383 |
[[nodiscard]] double GetStartTime() const { |
| 384 |
MOZ_ASSERT(!IsUnspecified()); |
| 385 |
// If mStartTime is null (e.g., for IntervalEnd), this will output 0.0 as |
| 386 |
// expected. |
| 387 |
return MarkerTiming::timeStampToDouble(mStartTime); |
| 388 |
} |
| 389 |
|
| 390 |
[[nodiscard]] double GetEndTime() const { |
| 391 |
MOZ_ASSERT(!IsUnspecified()); |
| 392 |
// If mEndTime is null (e.g., for Instant or IntervalStart), this will |
| 393 |
// output 0.0 as expected. |
| 394 |
return MarkerTiming::timeStampToDouble(mEndTime); |
| 395 |
} |
| 396 |
|
| 397 |
[[nodiscard]] uint8_t GetPhase() const { |
| 398 |
MOZ_ASSERT(!IsUnspecified()); |
| 399 |
return static_cast<uint8_t>(mPhase); |
| 400 |
} |
| 401 |
|
| 402 |
// This is a constructor for Rust FFI bindings. It must not be used outside of |
| 403 |
// this! Please see the other static constructors above. |
| 404 |
static void UnsafeConstruct(MarkerTiming* aMarkerTiming, |
| 405 |
const TimeStamp& aStartTime, |
| 406 |
const TimeStamp& aEndTime, Phase aPhase) { |
| 407 |
new (aMarkerTiming) MarkerTiming{aStartTime, aEndTime, aPhase}; |
| 408 |
} |
| 409 |
|
| 410 |
private: |
| 411 |
friend ProfileBufferEntryWriter::Serializer<MarkerTiming>; |
| 412 |
friend ProfileBufferEntryReader::Deserializer<MarkerTiming>; |
| 413 |
friend MarkerOptions; |
| 414 |
|
| 415 |
// Default timing leaves it internally "unspecified", serialization getters |
| 416 |
// and add-marker functions will default to `InstantNow()`. |
| 417 |
constexpr MarkerTiming() = default; |
| 418 |
|
| 419 |
// This should only be used by internal profiler code. |
| 420 |
[[nodiscard]] bool IsUnspecified() const { |
| 421 |
return mStartTime.IsNull() && mEndTime.IsNull(); |
| 422 |
} |
| 423 |
|
| 424 |
// Full constructor, used by static factory functions. |
| 425 |
constexpr MarkerTiming(const TimeStamp& aStartTime, const TimeStamp& aEndTime, |
| 426 |
Phase aPhase) |
| 427 |
: mStartTime(aStartTime), mEndTime(aEndTime), mPhase(aPhase) {} |
| 428 |
|
| 429 |
static double timeStampToDouble(const TimeStamp& time) { |
| 430 |
if (time.IsNull()) { |
| 431 |
// The Phase lets us know not to use this value. |
| 432 |
return 0; |
| 433 |
} |
| 434 |
return (time - TimeStamp::ProcessCreation()).ToMilliseconds(); |
| 435 |
} |
| 436 |
|
| 437 |
TimeStamp mStartTime; |
| 438 |
TimeStamp mEndTime; |
| 439 |
Phase mPhase = Phase::Instant; |
| 440 |
}; |
| 441 |
|
| 442 |
// This marker option allows three cases: |
| 443 |
// - By default, no stacks are captured. |
| 444 |
// - The caller can request a stack capture, and the add-marker code will take |
| 445 |
// care of it in the most efficient way. |
| 446 |
// - The caller can still provide an existing backtrace, for cases where a |
| 447 |
// marker reports something that happened elsewhere. |
| 448 |
class MarkerStack { |
| 449 |
public: |
| 450 |
// Default constructor, no capture. |
| 451 |
constexpr MarkerStack() = default; |
| 452 |
|
| 453 |
// Disallow copy. |
| 454 |
MarkerStack(const MarkerStack&) = delete; |
| 455 |
MarkerStack& operator=(const MarkerStack&) = delete; |
| 456 |
|
| 457 |
// Allow move. |
| 458 |
MarkerStack(MarkerStack&& aOther) |
| 459 |
: mCaptureOptions(aOther.mCaptureOptions), |
| 460 |
mOptionalChunkedBufferStorage( |
| 461 |
std::move(aOther.mOptionalChunkedBufferStorage)), |
| 462 |
mChunkedBuffer(aOther.mChunkedBuffer) { |
| 463 |
AssertInvariants(); |
| 464 |
aOther.Clear(); |
| 465 |
} |
| 466 |
MarkerStack& operator=(MarkerStack&& aOther) { |
| 467 |
mCaptureOptions = aOther.mCaptureOptions; |
| 468 |
mOptionalChunkedBufferStorage = |
| 469 |
std::move(aOther.mOptionalChunkedBufferStorage); |
| 470 |
mChunkedBuffer = aOther.mChunkedBuffer; |
| 471 |
AssertInvariants(); |
| 472 |
aOther.Clear(); |
| 473 |
return *this; |
| 474 |
} |
| 475 |
|
| 476 |
// Take ownership of a backtrace. If null or empty, equivalent to NoStack(). |
| 477 |
explicit MarkerStack(UniquePtr<ProfileChunkedBuffer>&& aExternalChunkedBuffer) |
| 478 |
: mOptionalChunkedBufferStorage( |
| 479 |
(!aExternalChunkedBuffer || aExternalChunkedBuffer->IsEmpty()) |
| 480 |
? nullptr |
| 481 |
: std::move(aExternalChunkedBuffer)), |
| 482 |
mChunkedBuffer(mOptionalChunkedBufferStorage.get()) { |
| 483 |
AssertInvariants(); |
| 484 |
} |
| 485 |
|
| 486 |
// Use an existing backtrace stored elsewhere, which the user must guarantee |
| 487 |
// is alive during the add-marker call. If empty, equivalent to NoStack(). |
| 488 |
explicit MarkerStack(ProfileChunkedBuffer& aExternalChunkedBuffer) |
| 489 |
: mChunkedBuffer(aExternalChunkedBuffer.IsEmpty() |
| 490 |
? nullptr |
| 491 |
: &aExternalChunkedBuffer) { |
| 492 |
AssertInvariants(); |
| 493 |
} |
| 494 |
|
| 495 |
// Don't capture a stack in this marker. |
| 496 |
static MarkerStack NoStack() { |
| 497 |
return MarkerStack(StackCaptureOptions::NoStack); |
| 498 |
} |
| 499 |
|
| 500 |
// Capture a stack when adding this marker. |
| 501 |
static MarkerStack Capture( |
| 502 |
StackCaptureOptions aCaptureOptions = StackCaptureOptions::Full) { |
| 503 |
// Actual capture will be handled inside profiler_add_marker. |
| 504 |
return MarkerStack(aCaptureOptions); |
| 505 |
} |
| 506 |
|
| 507 |
// Optionally capture a stack, useful for avoiding long-winded ternaries. |
| 508 |
static MarkerStack MaybeCapture(bool aDoCapture) { |
| 509 |
return aDoCapture ? Capture() : NoStack(); |
| 510 |
} |
| 511 |
|
| 512 |
// Use an existing backtrace stored elsewhere, which the user must guarantee |
| 513 |
// is alive during the add-marker call. If empty, equivalent to NoStack(). |
| 514 |
static MarkerStack UseBacktrace( |
| 515 |
ProfileChunkedBuffer& aExternalChunkedBuffer) { |
| 516 |
return MarkerStack(aExternalChunkedBuffer); |
| 517 |
} |
| 518 |
|
| 519 |
// Take ownership of a backtrace previously captured with |
| 520 |
// `profiler_capture_backtrace()`. If null, equivalent to NoStack(). |
| 521 |
static MarkerStack TakeBacktrace( |
| 522 |
UniquePtr<ProfileChunkedBuffer>&& aExternalChunkedBuffer) { |
| 523 |
return MarkerStack(std::move(aExternalChunkedBuffer)); |
| 524 |
} |
| 525 |
|
| 526 |
// Construct with the given capture options. |
| 527 |
static MarkerStack WithCaptureOptions(StackCaptureOptions aCaptureOptions) { |
| 528 |
return MarkerStack(aCaptureOptions); |
| 529 |
} |
| 530 |
|
| 531 |
[[nodiscard]] StackCaptureOptions CaptureOptions() const { |
| 532 |
return mCaptureOptions; |
| 533 |
} |
| 534 |
|
| 535 |
ProfileChunkedBuffer* GetChunkedBuffer() const { return mChunkedBuffer; } |
| 536 |
|
| 537 |
// Use backtrace after a request. If null, equivalent to NoStack(). |
| 538 |
void UseRequestedBacktrace(ProfileChunkedBuffer* aExternalChunkedBuffer) { |
| 539 |
MOZ_RELEASE_ASSERT(mCaptureOptions != StackCaptureOptions::NoStack); |
| 540 |
mCaptureOptions = StackCaptureOptions::NoStack; |
| 541 |
if (aExternalChunkedBuffer && !aExternalChunkedBuffer->IsEmpty()) { |
| 542 |
// We only need to use the provided buffer if it is not empty. |
| 543 |
mChunkedBuffer = aExternalChunkedBuffer; |
| 544 |
} |
| 545 |
AssertInvariants(); |
| 546 |
} |
| 547 |
|
| 548 |
void Clear() { |
| 549 |
mCaptureOptions = StackCaptureOptions::NoStack; |
| 550 |
mOptionalChunkedBufferStorage.reset(); |
| 551 |
mChunkedBuffer = nullptr; |
| 552 |
AssertInvariants(); |
| 553 |
} |
| 554 |
|
| 555 |
private: |
| 556 |
explicit MarkerStack(StackCaptureOptions aCaptureOptions) |
| 557 |
: mCaptureOptions(aCaptureOptions) { |
| 558 |
AssertInvariants(); |
| 559 |
} |
| 560 |
|
| 561 |
// This should be called after every constructor and non-const function. |
| 562 |
void AssertInvariants() const { |
| 563 |
#ifdef DEBUG |
| 564 |
if (mCaptureOptions != StackCaptureOptions::NoStack) { |
| 565 |
MOZ_ASSERT(!mOptionalChunkedBufferStorage, |
| 566 |
"We should not hold a buffer when capture is requested"); |
| 567 |
MOZ_ASSERT(!mChunkedBuffer, |
| 568 |
"We should not point at a buffer when capture is requested"); |
| 569 |
} else { |
| 570 |
if (mOptionalChunkedBufferStorage) { |
| 571 |
MOZ_ASSERT(mChunkedBuffer == mOptionalChunkedBufferStorage.get(), |
| 572 |
"Non-null mOptionalChunkedBufferStorage must be pointed-at " |
| 573 |
"by mChunkedBuffer"); |
| 574 |
} |
| 575 |
if (mChunkedBuffer) { |
| 576 |
MOZ_ASSERT(!mChunkedBuffer->IsEmpty(), |
| 577 |
"Non-null mChunkedBuffer must not be empty"); |
| 578 |
} |
| 579 |
} |
| 580 |
#endif // DEBUG |
| 581 |
} |
| 582 |
|
| 583 |
StackCaptureOptions mCaptureOptions = StackCaptureOptions::NoStack; |
| 584 |
|
| 585 |
// Optional storage for the backtrace, in case it was captured before the |
| 586 |
// add-marker call. |
| 587 |
UniquePtr<ProfileChunkedBuffer> mOptionalChunkedBufferStorage; |
| 588 |
|
| 589 |
// If not null, this points to the backtrace. It may point to a backtrace |
| 590 |
// temporarily stored on the stack, or to mOptionalChunkedBufferStorage. |
| 591 |
ProfileChunkedBuffer* mChunkedBuffer = nullptr; |
| 592 |
}; |
| 593 |
|
| 594 |
// This marker option captures a given inner window id. |
| 595 |
class MarkerInnerWindowId { |
| 596 |
public: |
| 597 |
// Default constructor, it leaves the id unspecified. |
| 598 |
constexpr MarkerInnerWindowId() = default; |
| 599 |
|
| 600 |
// Constructor with a specified inner window id. |
| 601 |
constexpr explicit MarkerInnerWindowId(uint64_t i) : mInnerWindowId(i) {} |
| 602 |
|
| 603 |
// Constructor with either specified inner window id or Nothing. |
| 604 |
constexpr explicit MarkerInnerWindowId(const Maybe<uint64_t>& i) |
| 605 |
: mInnerWindowId(i.valueOr(scNoId)) {} |
| 606 |
|
| 607 |
// Explicit option with unspecified id. |
| 608 |
constexpr static MarkerInnerWindowId NoId() { return MarkerInnerWindowId{}; } |
| 609 |
|
| 610 |
[[nodiscard]] bool IsUnspecified() const { return mInnerWindowId == scNoId; } |
| 611 |
|
| 612 |
[[nodiscard]] constexpr uint64_t Id() const { return mInnerWindowId; } |
| 613 |
|
| 614 |
private: |
| 615 |
static constexpr uint64_t scNoId = 0; |
| 616 |
uint64_t mInnerWindowId = scNoId; |
| 617 |
}; |
| 618 |
|
| 619 |
// This class combines each of the possible marker options above. |
| 620 |
class MarkerOptions { |
| 621 |
public: |
| 622 |
// Constructor from individual options (including none). |
| 623 |
// Implicit to allow `{}` and one option type as-is. |
| 624 |
// Options that are not provided here are defaulted. In particular, timing |
| 625 |
// defaults to `MarkerTiming::InstantNow()` when the marker is recorded. |
| 626 |
template <typename... Options> |
| 627 |
MOZ_IMPLICIT MarkerOptions(Options&&... aOptions) { |
| 628 |
(Set(std::forward<Options>(aOptions)), ...); |
| 629 |
} |
| 630 |
|
| 631 |
// Disallow copy. |
| 632 |
MarkerOptions(const MarkerOptions&) = delete; |
| 633 |
MarkerOptions& operator=(const MarkerOptions&) = delete; |
| 634 |
|
| 635 |
// Allow move. |
| 636 |
MarkerOptions(MarkerOptions&&) = default; |
| 637 |
MarkerOptions& operator=(MarkerOptions&&) = default; |
| 638 |
|
| 639 |
// The embedded `MarkerTiming` hasn't been specified yet. |
| 640 |
[[nodiscard]] bool IsTimingUnspecified() const { |
| 641 |
return mTiming.IsUnspecified(); |
| 642 |
} |
| 643 |
|
| 644 |
// Each option may be added in a chain by e.g.: |
| 645 |
// `options.Set(MarkerThreadId(123)).Set(MarkerTiming::IntervalEnd())`. |
| 646 |
// When passed to an add-marker function, it must be an rvalue, either created |
| 647 |
// on the spot, or `std::move`d from storage, e.g.: |
| 648 |
// `PROFILER_MARKER_UNTYPED("...", std::move(options).Set(...))`; |
| 649 |
// |
| 650 |
// Options can be read by their name (without "Marker"), e.g.: `o.ThreadId()`. |
| 651 |
// Add "Ref" for a non-const reference, e.g.: `o.ThreadIdRef() = ...;` |
| 652 |
#define FUNCTIONS_ON_MEMBER(NAME) \ |
| 653 |
MarkerOptions& Set(Marker##NAME&& a##NAME)& { \ |
| 654 |
m##NAME = std::move(a##NAME); \ |
| 655 |
return *this; \ |
| 656 |
} \ |
| 657 |
\ |
| 658 |
MarkerOptions&& Set(Marker##NAME&& a##NAME)&& { \ |
| 659 |
m##NAME = std::move(a##NAME); \ |
| 660 |
return std::move(*this); \ |
| 661 |
} \ |
| 662 |
\ |
| 663 |
const Marker##NAME& NAME() const { return m##NAME; } \ |
| 664 |
\ |
| 665 |
Marker##NAME& NAME##Ref() { return m##NAME; } |
| 666 |
|
| 667 |
FUNCTIONS_ON_MEMBER(ThreadId); |
| 668 |
FUNCTIONS_ON_MEMBER(Timing); |
| 669 |
FUNCTIONS_ON_MEMBER(Stack); |
| 670 |
FUNCTIONS_ON_MEMBER(InnerWindowId); |
| 671 |
#undef FUNCTIONS_ON_MEMBER |
| 672 |
|
| 673 |
private: |
| 674 |
friend ProfileBufferEntryReader::Deserializer<MarkerOptions>; |
| 675 |
|
| 676 |
MarkerThreadId mThreadId; |
| 677 |
MarkerTiming mTiming; |
| 678 |
MarkerStack mStack; |
| 679 |
MarkerInnerWindowId mInnerWindowId; |
| 680 |
}; |
| 681 |
|
| 682 |
} // namespace mozilla |
| 683 |
|
| 684 |
namespace mozilla::baseprofiler::markers { |
| 685 |
|
| 686 |
// Default marker payload types, with no extra information, not even a marker |
| 687 |
// type and payload. This is intended for label-only markers. |
| 688 |
struct NoPayload final {}; |
| 689 |
|
| 690 |
} // namespace mozilla::baseprofiler::markers |
| 691 |
|
| 692 |
namespace mozilla { |
| 693 |
|
| 694 |
class JSONWriter; |
| 695 |
|
| 696 |
// This class collects all the information necessary to stream the JSON schema |
| 697 |
// that informs the front-end how to display a type of markers. |
| 698 |
// It will be created and populated in `MarkerTypeDisplay()` functions in each |
| 699 |
// marker type definition, see Add/Set functions. |
| 700 |
class MarkerSchema { |
| 701 |
public: |
| 702 |
// This is used to describe a C++ type that is expected to be specified to |
| 703 |
// the marker and used in PayloadField. This type is the expected input type |
| 704 |
// to the marker data. |
| 705 |
enum class InputType { |
| 706 |
Uint64, |
| 707 |
Uint32, |
| 708 |
Uint8, |
| 709 |
Boolean, |
| 710 |
CString, |
| 711 |
String, |
| 712 |
TimeStamp, |
| 713 |
TimeDuration |
| 714 |
}; |
| 715 |
|
| 716 |
enum class Location : unsigned { |
| 717 |
MarkerChart, |
| 718 |
MarkerTable, |
| 719 |
// This adds markers to the main marker timeline in the header. |
| 720 |
TimelineOverview, |
| 721 |
// In the timeline, this is a section that breaks out markers that are |
| 722 |
// related to memory. When memory counters are enabled, this is its own |
| 723 |
// track, otherwise it is displayed with the main thread. |
| 724 |
TimelineMemory, |
| 725 |
// This adds markers to the IPC timeline area in the header. |
| 726 |
TimelineIPC, |
| 727 |
// This adds markers to the FileIO timeline area in the header. |
| 728 |
TimelineFileIO, |
| 729 |
// TODO - This is not supported yet. |
| 730 |
StackChart |
| 731 |
}; |
| 732 |
|
| 733 |
// Used as constructor parameter, to explicitly specify that the location (and |
| 734 |
// other display options) are handled as a special case in the front-end. |
| 735 |
// In this case, *no* schema will be output for this type. |
| 736 |
struct SpecialFrontendLocation {}; |
| 737 |
|
| 738 |
enum class Format { |
| 739 |
// ---------------------------------------------------- |
| 740 |
// String types. |
| 741 |
|
| 742 |
// Show the URL, and handle PII sanitization |
| 743 |
Url, |
| 744 |
// Show the file path, and handle PII sanitization. |
| 745 |
FilePath, |
| 746 |
// Show arbitrary string and handle PII sanitization |
| 747 |
SanitizedString, |
| 748 |
// Important, do not put URL or file path information here, as it will not |
| 749 |
// be sanitized. Please be careful with including other types of PII here as |
| 750 |
// well. |
| 751 |
// e.g. "Label: Some String" |
| 752 |
String, |
| 753 |
|
| 754 |
// Show a string from a UniqueStringArray given an index in the profile. |
| 755 |
// e.g. 1, given string table ["hello", "world"] will show "world" |
| 756 |
UniqueString, |
| 757 |
|
| 758 |
// ---------------------------------------------------- |
| 759 |
// Numeric types |
| 760 |
|
| 761 |
// For time data that represents a duration of time. |
| 762 |
// e.g. "Label: 5s, 5ms, 5μs" |
| 763 |
Duration, |
| 764 |
// Data that happened at a specific time, relative to the start of the |
| 765 |
// profile. e.g. "Label: 15.5s, 20.5ms, 30.5μs" |
| 766 |
Time, |
| 767 |
// The following are alternatives to display a time only in a specific unit |
| 768 |
// of time. |
| 769 |
Seconds, // "Label: 5s" |
| 770 |
Milliseconds, // "Label: 5ms" |
| 771 |
Microseconds, // "Label: 5μs" |
| 772 |
Nanoseconds, // "Label: 5ns" |
| 773 |
// e.g. "Label: 5.55mb, 5 bytes, 312.5kb" |
| 774 |
Bytes, |
| 775 |
// This should be a value between 0 and 1. |
| 776 |
// "Label: 50%" |
| 777 |
Percentage, |
| 778 |
// The integer should be used for generic representations of numbers. |
| 779 |
// Do not use it for time information. |
| 780 |
// "Label: 52, 5,323, 1,234,567" |
| 781 |
Integer, |
| 782 |
// The decimal should be used for generic representations of numbers. |
| 783 |
// Do not use it for time information. |
| 784 |
// "Label: 52.23, 0.0054, 123,456.78" |
| 785 |
Decimal, |
| 786 |
|
| 787 |
// A flow is a u64 identifier that's unique across processes. All of |
| 788 |
// the markers with same flow id before a terminating flow id will be |
| 789 |
// considered part of the same "flow" and linked together. |
| 790 |
Flow, |
| 791 |
// A terminating flow ends a flow of a particular id and allows that id |
| 792 |
// to be reused again. It often makes sense for destructors to create |
| 793 |
// a marker with a field of this type. |
| 794 |
TerminatingFlow |
| 795 |
}; |
| 796 |
|
| 797 |
// This represents groups of markers which MarkerTypes can expose to indicate |
| 798 |
// what group they belong to (multiple groups are allowed combined in bitwise |
| 799 |
// or). This is currently only used for ETW filtering. In the long run this |
| 800 |
// should be generalized to gecko markers. |
| 801 |
enum class ETWMarkerGroup : uint64_t { |
| 802 |
Generic = 1, |
| 803 |
UserMarkers = 1 << 1, |
| 804 |
Memory = 1 << 2, |
| 805 |
Scheduling = 1 << 3, |
| 806 |
Text = 1 << 4, |
| 807 |
Tracing = 1 << 5 |
| 808 |
}; |
| 809 |
|
| 810 |
// Flags which describe additional information for a PayloadField. |
| 811 |
enum class PayloadFlags : uint32_t { None = 0, Searchable = 1 }; |
| 812 |
|
| 813 |
// This is one field of payload to be used for additional marker data. |
| 814 |
struct PayloadField { |
| 815 |
// Key identifying the marker. |
| 816 |
const char* Key; |
| 817 |
// Input type, this represents the data type specified. |
| 818 |
InputType InputTy; |
| 819 |
// Label, additional description. |
| 820 |
const char* Label = nullptr; |
| 821 |
// Format as written to the JSON. |
| 822 |
Format Fmt = Format::String; |
| 823 |
// Optional PayloadFlags. |
| 824 |
PayloadFlags Flags = PayloadFlags::None; |
| 825 |
}; |
| 826 |
|
| 827 |
enum class Searchable { NotSearchable, Searchable }; |
| 828 |
enum class GraphType { Line, Bar, FilledLine }; |
| 829 |
enum class GraphColor { |
| 830 |
Blue, |
| 831 |
Green, |
| 832 |
Grey, |
| 833 |
Ink, |
| 834 |
Magenta, |
| 835 |
Orange, |
| 836 |
Purple, |
| 837 |
Red, |
| 838 |
Teal, |
| 839 |
Yellow |
| 840 |
}; |
| 841 |
|
| 842 |
// Marker schema, with a non-empty list of locations where markers should be |
| 843 |
// shown. |
| 844 |
// Tech note: Even though `aLocations` are templated arguments, they are |
| 845 |
// assigned to an `enum class` object, so they can only be of that enum type. |
| 846 |
template <typename... Locations> |
| 847 |
explicit MarkerSchema(Location aLocation, Locations... aLocations) |
| 848 |
: mLocations{aLocation, aLocations...} {} |
| 849 |
|
| 850 |
// Alternative constructor for MarkerSchema. |
| 851 |
explicit MarkerSchema(const mozilla::MarkerSchema::Location* aLocations, |
| 852 |
size_t aLength) |
| 853 |
: mLocations(aLocations, aLocations + aLength) {} |
| 854 |
|
| 855 |
// Marker schema for types that have special frontend handling. |
| 856 |
// Nothing else should be set in this case. |
| 857 |
// Implicit to allow quick return from MarkerTypeDisplay functions. |
| 858 |
MOZ_IMPLICIT MarkerSchema(SpecialFrontendLocation) {} |
| 859 |
|
| 860 |
// Caller must specify location(s) or SpecialFrontendLocation above. |
| 861 |
MarkerSchema() = delete; |
| 862 |
|
| 863 |
// Optional labels in the marker chart, the chart tooltip, and the marker |
| 864 |
// table. If not provided, the marker "name" will be used. The given string |
| 865 |
// can contain element keys in braces to include data elements streamed by |
| 866 |
// `StreamJSONMarkerData()`. E.g.: "This is {text}" |
| 867 |
|
| 868 |
#define LABEL_SETTER(name) \ |
| 869 |
MarkerSchema& Set##name(std::string a##name) { \ |
| 870 |
m##name = std::move(a##name); \ |
| 871 |
return *this; \ |
| 872 |
} |
| 873 |
|
| 874 |
LABEL_SETTER(ChartLabel) |
| 875 |
LABEL_SETTER(TooltipLabel) |
| 876 |
LABEL_SETTER(TableLabel) |
| 877 |
|
| 878 |
#undef LABEL_SETTER |
| 879 |
|
| 880 |
MarkerSchema& SetAllLabels(std::string aText) { |
| 881 |
// Here we set the same text in each label. |
| 882 |
// TODO: Move to a single "label" field once the front-end allows it. |
| 883 |
SetChartLabel(aText); |
| 884 |
SetTooltipLabel(aText); |
| 885 |
SetTableLabel(std::move(aText)); |
| 886 |
return *this; |
| 887 |
} |
| 888 |
|
| 889 |
MarkerSchema& SetIsStackBased() { |
| 890 |
mIsStackBased = true; |
| 891 |
return *this; |
| 892 |
} |
| 893 |
|
| 894 |
// Each data element that is streamed by `StreamJSONMarkerData()` can be |
| 895 |
// displayed as indicated by using one of the `Add...` function below. |
| 896 |
// Each `Add...` will add a line in the full marker description. Parameters: |
| 897 |
// - `aKey`: Element property name as streamed by `StreamJSONMarkerData()`. |
| 898 |
// - `aLabel`: Optional prefix. Defaults to the key name. |
| 899 |
// - `aFormat`: How to format the data element value, see `Format` above. |
| 900 |
// - `aSearchable`: Optional, indicates if the value is used in searches, |
| 901 |
// defaults to false. |
| 902 |
|
| 903 |
MarkerSchema& AddKeyFormat(std::string aKey, Format aFormat) { |
| 904 |
mData.emplace_back(mozilla::VariantType<DynamicData>{}, |
| 905 |
DynamicData{std::move(aKey), mozilla::Nothing{}, aFormat, |
| 906 |
mozilla::Nothing{}}); |
| 907 |
return *this; |
| 908 |
} |
| 909 |
|
| 910 |
MarkerSchema& AddKeyLabelFormat(std::string aKey, std::string aLabel, |
| 911 |
Format aFormat) { |
| 912 |
mData.emplace_back( |
| 913 |
mozilla::VariantType<DynamicData>{}, |
| 914 |
DynamicData{std::move(aKey), mozilla::Some(std::move(aLabel)), aFormat, |
| 915 |
mozilla::Nothing{}}); |
| 916 |
return *this; |
| 917 |
} |
| 918 |
|
| 919 |
MarkerSchema& AddKeyFormatSearchable(std::string aKey, Format aFormat, |
| 920 |
Searchable aSearchable) { |
| 921 |
mData.emplace_back(mozilla::VariantType<DynamicData>{}, |
| 922 |
DynamicData{std::move(aKey), mozilla::Nothing{}, aFormat, |
| 923 |
mozilla::Some(aSearchable)}); |
| 924 |
return *this; |
| 925 |
} |
| 926 |
|
| 927 |
MarkerSchema& AddKeyLabelFormatSearchable(std::string aKey, |
| 928 |
std::string aLabel, Format aFormat, |
| 929 |
Searchable aSearchable) { |
| 930 |
mData.emplace_back( |
| 931 |
mozilla::VariantType<DynamicData>{}, |
| 932 |
DynamicData{std::move(aKey), mozilla::Some(std::move(aLabel)), aFormat, |
| 933 |
mozilla::Some(aSearchable)}); |
| 934 |
return *this; |
| 935 |
} |
| 936 |
|
| 937 |
// The display may also include static rows. |
| 938 |
|
| 939 |
MarkerSchema& AddStaticLabelValue(std::string aLabel, std::string aValue) { |
| 940 |
mData.emplace_back(mozilla::VariantType<StaticData>{}, |
| 941 |
StaticData{std::move(aLabel), std::move(aValue)}); |
| 942 |
return *this; |
| 943 |
} |
| 944 |
|
| 945 |
// Markers can be shown as timeline tracks. |
| 946 |
|
| 947 |
MarkerSchema& AddChart(std::string aKey, GraphType aType) { |
| 948 |
mGraphs.emplace_back(GraphData{std::move(aKey), aType, mozilla::Nothing{}}); |
| 949 |
return *this; |
| 950 |
} |
| 951 |
|
| 952 |
MarkerSchema& AddChartColor(std::string aKey, GraphType aType, |
| 953 |
GraphColor aColor) { |
| 954 |
mGraphs.emplace_back( |
| 955 |
GraphData{std::move(aKey), aType, mozilla::Some(aColor)}); |
| 956 |
return *this; |
| 957 |
} |
| 958 |
|
| 959 |
// Internal streaming function. |
| 960 |
MFBT_API void Stream(JSONWriter& aWriter, const Span<const char>& aName) &&; |
| 961 |
|
| 962 |
private: |
| 963 |
MFBT_API static Span<const char> LocationToStringSpan(Location aLocation); |
| 964 |
MFBT_API static Span<const char> FormatToStringSpan(Format aFormat); |
| 965 |
MFBT_API static Span<const char> GraphTypeToStringSpan(GraphType aType); |
| 966 |
MFBT_API static Span<const char> GraphColorToStringSpan(GraphColor aColor); |
| 967 |
|
| 968 |
// List of marker display locations. Empty for SpecialFrontendLocation. |
| 969 |
std::vector<Location> mLocations; |
| 970 |
// Labels for different places. |
| 971 |
std::string mChartLabel; |
| 972 |
std::string mTooltipLabel; |
| 973 |
std::string mTableLabel; |
| 974 |
bool mIsStackBased = false; |
| 975 |
// Main display, made of zero or more rows of key+label+format or label+value. |
| 976 |
private: |
| 977 |
struct DynamicData { |
| 978 |
std::string mKey; |
| 979 |
mozilla::Maybe<std::string> mLabel; |
| 980 |
Format mFormat; |
| 981 |
mozilla::Maybe<Searchable> mSearchable; |
| 982 |
}; |
| 983 |
struct StaticData { |
| 984 |
std::string mLabel; |
| 985 |
std::string mValue; |
| 986 |
}; |
| 987 |
using DataRow = mozilla::Variant<DynamicData, StaticData>; |
| 988 |
using DataRowVector = std::vector<DataRow>; |
| 989 |
|
| 990 |
DataRowVector mData; |
| 991 |
|
| 992 |
struct GraphData { |
| 993 |
std::string mKey; |
| 994 |
GraphType mType; |
| 995 |
mozilla::Maybe<GraphColor> mColor; |
| 996 |
}; |
| 997 |
std::vector<GraphData> mGraphs; |
| 998 |
}; |
| 999 |
|
| 1000 |
namespace detail { |
| 1001 |
// GCC doesn't allow this to live inside the class. |
| 1002 |
template <typename PayloadType> |
| 1003 |
static void StreamPayload(baseprofiler::SpliceableJSONWriter& aWriter, |
| 1004 |
const Span<const char> aKey, |
| 1005 |
const PayloadType& aPayload) { |
| 1006 |
aWriter.StringProperty(aKey, aPayload); |
| 1007 |
} |
| 1008 |
|
| 1009 |
template <typename PayloadType> |
| 1010 |
inline void StreamPayload(baseprofiler::SpliceableJSONWriter& aWriter, |
| 1011 |
const Span<const char> aKey, |
| 1012 |
const Maybe<PayloadType>& aPayload) { |
| 1013 |
if (aPayload.isSome()) { |
| 1014 |
StreamPayload(aWriter, aKey, *aPayload); |
| 1015 |
} else { |
| 1016 |
aWriter.NullProperty(aKey); |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
template <> |
| 1021 |
inline void StreamPayload<bool>(baseprofiler::SpliceableJSONWriter& aWriter, |
| 1022 |
const Span<const char> aKey, |
| 1023 |
const bool& aPayload) { |
| 1024 |
aWriter.BoolProperty(aKey, aPayload); |
| 1025 |
} |
| 1026 |
|
| 1027 |
template <> |
| 1028 |
inline void StreamPayload<ProfilerString8View>( |
| 1029 |
baseprofiler::SpliceableJSONWriter& aWriter, const Span<const char> aKey, |
| 1030 |
const ProfilerString8View& aPayload) { |
| 1031 |
aWriter.StringProperty(aKey, aPayload); |
| 1032 |
} |
| 1033 |
|
| 1034 |
template <> |
| 1035 |
inline void StreamPayload<Flow>(baseprofiler::SpliceableJSONWriter& aWriter, |
| 1036 |
const Span<const char> aKey, |
| 1037 |
const Flow& aPayload) { |
| 1038 |
aWriter.FlowProperty(aKey, aPayload); |
| 1039 |
} |
| 1040 |
|
| 1041 |
} // namespace detail |
| 1042 |
|
| 1043 |
// This helper class is used by MarkerTypes that want to support the general |
| 1044 |
// MarkerType object schema. When using this the markers will also transmit |
| 1045 |
// their payload to the ETW tracer as well as requiring less inline code. |
| 1046 |
// This is a curiously recurring template, the template argument is the child |
| 1047 |
// class itself. |
| 1048 |
template <typename T> |
| 1049 |
struct BaseMarkerType { |
| 1050 |
static constexpr const char* Description = nullptr; |
| 1051 |
|
| 1052 |
static constexpr const char* AllLabels = nullptr; |
| 1053 |
static constexpr const char* ChartLabel = nullptr; |
| 1054 |
static constexpr const char* TableLabel = nullptr; |
| 1055 |
static constexpr const char* TooltipLabel = nullptr; |
| 1056 |
|
| 1057 |
// Setting this property to true is a promise that the the marker will nest |
| 1058 |
// properly. i.e. it can't have a partially overlapping time range with any |
| 1059 |
// other stack based markers on the same thread. |
| 1060 |
static constexpr bool IsStackBased = false; |
| 1061 |
|
| 1062 |
// This indicates whether this marker type wants the names passed to the |
| 1063 |
// individual marker calls stores along with the marker. |
| 1064 |
static constexpr bool StoreName = false; |
| 1065 |
|
| 1066 |
static constexpr MarkerSchema::ETWMarkerGroup Group = |
| 1067 |
MarkerSchema::ETWMarkerGroup::Generic; |
| 1068 |
|
| 1069 |
static MarkerSchema MarkerTypeDisplay() { |
| 1070 |
using MS = MarkerSchema; |
| 1071 |
MS schema{T::Locations, std::size(T::Locations)}; |
| 1072 |
if (T::AllLabels) { |
| 1073 |
schema.SetAllLabels(T::AllLabels); |
| 1074 |
} |
| 1075 |
if (T::ChartLabel) { |
| 1076 |
schema.SetChartLabel(T::ChartLabel); |
| 1077 |
} |
| 1078 |
if (T::TableLabel) { |
| 1079 |
schema.SetTableLabel(T::TableLabel); |
| 1080 |
} |
| 1081 |
if (T::TooltipLabel) { |
| 1082 |
schema.SetTooltipLabel(T::TooltipLabel); |
| 1083 |
} |
| 1084 |
if (T::IsStackBased) { |
| 1085 |
schema.SetIsStackBased(); |
| 1086 |
} |
| 1087 |
for (const MS::PayloadField field : T::PayloadFields) { |
| 1088 |
if (field.Label) { |
| 1089 |
if (uint32_t(field.Flags) & uint32_t(MS::PayloadFlags::Searchable)) { |
| 1090 |
schema.AddKeyLabelFormatSearchable(field.Key, field.Label, field.Fmt, |
| 1091 |
MS::Searchable::Searchable); |
| 1092 |
} else { |
| 1093 |
schema.AddKeyLabelFormat(field.Key, field.Label, field.Fmt); |
| 1094 |
} |
| 1095 |
} else { |
| 1096 |
if (uint32_t(field.Flags) & uint32_t(MS::PayloadFlags::Searchable)) { |
| 1097 |
schema.AddKeyFormatSearchable(field.Key, field.Fmt, |
| 1098 |
MS::Searchable::Searchable); |
| 1099 |
} else { |
| 1100 |
schema.AddKeyFormat(field.Key, field.Fmt); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
if (T::Description) { |
| 1105 |
schema.AddStaticLabelValue("Description", T::Description); |
| 1106 |
} |
| 1107 |
return schema; |
| 1108 |
} |
| 1109 |
|
| 1110 |
static constexpr Span<const char> MarkerTypeName() { |
| 1111 |
return MakeStringSpan(T::Name); |
| 1112 |
} |
| 1113 |
|
| 1114 |
// This is called by the child class since the child class version of this |
| 1115 |
// function is used to infer the argument types by the profile buffer and |
| 1116 |
// allows the child to do any special data conversion it needs to do. |
| 1117 |
// Optionally the child can opt not to use this at all and write the data |
| 1118 |
// out itself. |
| 1119 |
template <typename... PayloadArguments> |
| 1120 |
static void StreamJSONMarkerDataImpl( |
| 1121 |
baseprofiler::SpliceableJSONWriter& aWriter, |
| 1122 |
const PayloadArguments&... aPayloadArguments) { |
| 1123 |
size_t i = 0; |
| 1124 |
(detail::StreamPayload(aWriter, MakeStringSpan(T::PayloadFields[i++].Key), |
| 1125 |
aPayloadArguments), |
| 1126 |
...); |
| 1127 |
} |
| 1128 |
}; |
| 1129 |
} // namespace mozilla |
| 1130 |
|
| 1131 |
#endif // BaseProfilerMarkersPrerequisites_h |