tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AutoProfilerStyleMarker.h (3853B)


      1 /* -*- Mode: C++; tab-width: 8; 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 #ifndef mozilla_AutoProfilerStyleMarker_h
      8 #define mozilla_AutoProfilerStyleMarker_h
      9 
     10 #include "mozilla/Attributes.h"
     11 #include "mozilla/ProfilerMarkers.h"
     12 #include "mozilla/ServoTraversalStatistics.h"
     13 #include "mozilla/TimeStamp.h"
     14 
     15 namespace mozilla {
     16 
     17 class MOZ_RAII AutoProfilerStyleMarker {
     18 public:
     19  explicit AutoProfilerStyleMarker(UniquePtr<ProfileChunkedBuffer> aCause,
     20                                   const Maybe<uint64_t>& aInnerWindowID)
     21      : mActive(profiler_thread_is_being_profiled_for_markers()),
     22        mCause(std::move(aCause)),
     23        mInnerWindowID(aInnerWindowID) {
     24    if (!mActive) {
     25      return;
     26    }
     27    MOZ_ASSERT(!ServoTraversalStatistics::sActive,
     28               "Nested AutoProfilerStyleMarker");
     29    ServoTraversalStatistics::sSingleton = ServoTraversalStatistics();
     30    ServoTraversalStatistics::sActive = true;
     31 
     32    mStartTime = TimeStamp::Now();
     33  }
     34 
     35  ~AutoProfilerStyleMarker() {
     36    if (!mActive) {
     37      return;
     38    }
     39 
     40    struct StyleMarker {
     41      static constexpr mozilla::Span<const char> MarkerTypeName() {
     42        return mozilla::MakeStringSpan("Styles");
     43      }
     44      static void StreamJSONMarkerData(
     45          baseprofiler::SpliceableJSONWriter& aWriter,
     46          uint32_t aElementsTraversed, uint32_t aElementsStyled,
     47          uint32_t aElementsMatched, uint32_t aStylesShared,
     48          uint32_t aStylesReused) {
     49        aWriter.IntProperty("elementsTraversed", aElementsTraversed);
     50        aWriter.IntProperty("elementsStyled", aElementsStyled);
     51        aWriter.IntProperty("elementsMatched", aElementsMatched);
     52        aWriter.IntProperty("stylesShared", aStylesShared);
     53        aWriter.IntProperty("stylesReused", aStylesReused);
     54      }
     55      static MarkerSchema MarkerTypeDisplay() {
     56        using MS = MarkerSchema;
     57        MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable,
     58                  MS::Location::TimelineOverview};
     59        schema.AddKeyLabelFormat("elementsTraversed", "Elements traversed",
     60                                 MS::Format::Integer);
     61        schema.AddKeyLabelFormat("elementsStyled", "Elements styled",
     62                                 MS::Format::Integer);
     63        schema.AddKeyLabelFormat("elementsMatched", "Elements matched",
     64                                 MS::Format::Integer);
     65        schema.AddKeyLabelFormat("stylesShared", "Styles shared",
     66                                 MS::Format::Integer);
     67        schema.AddKeyLabelFormat("stylesReused", "Styles reused",
     68                                 MS::Format::Integer);
     69        return schema;
     70      }
     71    };
     72 
     73    ServoTraversalStatistics::sActive = false;
     74    profiler_add_marker("Styles", geckoprofiler::category::LAYOUT,
     75                        {MarkerTiming::IntervalUntilNowFrom(mStartTime),
     76                         MarkerStack::TakeBacktrace(std::move(mCause)),
     77                         MarkerInnerWindowId(mInnerWindowID)},
     78                        StyleMarker{},
     79                        ServoTraversalStatistics::sSingleton.mElementsTraversed,
     80                        ServoTraversalStatistics::sSingleton.mElementsStyled,
     81                        ServoTraversalStatistics::sSingleton.mElementsMatched,
     82                        ServoTraversalStatistics::sSingleton.mStylesShared,
     83                        ServoTraversalStatistics::sSingleton.mStylesReused);
     84  }
     85 
     86 private:
     87  bool mActive;
     88  TimeStamp mStartTime;
     89  UniquePtr<ProfileChunkedBuffer> mCause;
     90  Maybe<uint64_t> mInnerWindowID;
     91 };
     92 
     93 }  // namespace mozilla
     94 
     95 #endif  // mozilla_AutoProfilerStyleMarker_h