tor-browser

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

CheckerboardEvent.h (6610B)


      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_layers_CheckerboardEvent_h
      8 #define mozilla_layers_CheckerboardEvent_h
      9 
     10 #include "mozilla/DefineEnum.h"
     11 #include "mozilla/Monitor.h"
     12 #include "mozilla/TimeStamp.h"
     13 #include <sstream>
     14 #include "Units.h"
     15 #include <vector>
     16 
     17 namespace mozilla {
     18 namespace layers {
     19 
     20 /**
     21 * This class records information relevant to one "checkerboard event", which is
     22 * a contiguous set of frames where a given APZC was checkerboarding. The intent
     23 * of this class is to record enough information that it can provide actionable
     24 * steps to reduce the occurrence of checkerboarding. Furthermore, it records
     25 * information about the severity of the checkerboarding so as to allow
     26 * prioritizing the debugging of some checkerboarding events over others.
     27 */
     28 class CheckerboardEvent final {
     29 public:
     30  // clang-format off
     31  MOZ_DEFINE_ENUM_AT_CLASS_SCOPE(
     32    RendertraceProperty, (
     33      Page,
     34      PaintedDisplayPort,
     35      RequestedDisplayPort,
     36      UserVisible
     37  ));
     38  // clang-format on
     39 
     40  static const char* sDescriptions[sRendertracePropertyCount];
     41  static const char* sColors[sRendertracePropertyCount];
     42 
     43 public:
     44  explicit CheckerboardEvent(bool aRecordTrace);
     45 
     46  /**
     47   * Gets the "severity" of the checkerboard event. This doesn't have units,
     48   * it's just useful for comparing two checkerboard events to see which one
     49   * is worse, for some implementation-specific definition of "worse".
     50   */
     51  uint32_t GetSeverity();
     52 
     53  /**
     54   * Gets the number of CSS pixels that were checkerboarded at the peak of the
     55   * checkerboard event.
     56   */
     57  uint32_t GetPeak();
     58 
     59  /**
     60   * Gets the length of the checkerboard event.
     61   */
     62  TimeDuration GetDuration();
     63 
     64  /**
     65   * Gets the raw log of the checkerboard event. This can be called any time,
     66   * although it really only makes sense to pull once the event is done, after
     67   * RecordFrameInfo returns true.
     68   */
     69  std::string GetLog();
     70 
     71  /**
     72   * Returns true iff this event is recording a detailed trace of the event.
     73   * This is the argument passed in to the constructor.
     74   */
     75  bool IsRecordingTrace();
     76 
     77  /**
     78   * Provide a new value for one of the rects that is tracked for
     79   * checkerboard events.
     80   */
     81  void UpdateRendertraceProperty(RendertraceProperty aProperty,
     82                                 const CSSRect& aRect,
     83                                 const std::string& aExtraInfo = std::string());
     84 
     85  /**
     86   * Provide the number of CSS pixels that are checkerboarded in a composite
     87   * at the current time.
     88   * @return true if the checkerboard event has completed. The caller should
     89   * stop updating this object once this happens.
     90   */
     91  bool RecordFrameInfo(uint32_t aCssPixelsCheckerboarded);
     92 
     93 private:
     94  /**
     95   * Helper method to do stuff when checkeboarding starts.
     96   */
     97  void StartEvent();
     98  /**
     99   * Helper method to do stuff when checkerboarding stops.
    100   */
    101  void StopEvent();
    102 
    103  /**
    104   * Helper method to log a rendertrace property and its value to the
    105   * rendertrace info buffer (mRendertraceInfo).
    106   */
    107  void LogInfo(RendertraceProperty aProperty, const TimeStamp& aTimestamp,
    108               const CSSRect& aRect, const std::string& aExtraInfo,
    109               const MonitorAutoLock& aProofOfLock)
    110      MOZ_REQUIRES(mRendertraceLock);
    111 
    112  /**
    113   * Helper struct that holds a single rendertrace property value.
    114   */
    115  struct PropertyValue {
    116    RendertraceProperty mProperty;
    117    TimeStamp mTimeStamp;
    118    CSSRect mRect;
    119    std::string mExtraInfo;
    120 
    121    bool operator<(const PropertyValue& aOther) const;
    122  };
    123 
    124  /**
    125   * A circular buffer that stores the most recent BUFFER_SIZE values of a
    126   * given property.
    127   */
    128  class PropertyBuffer {
    129   public:
    130    PropertyBuffer();
    131    /**
    132     * Add a new value to the buffer, overwriting the oldest one if needed.
    133     */
    134    void Update(RendertraceProperty aProperty, const CSSRect& aRect,
    135                const std::string& aExtraInfo,
    136                const MonitorAutoLock& aProofOfLock);
    137    /**
    138     * Dump the recorded values, oldest to newest, to the given vector, and
    139     * remove them from this buffer.
    140     */
    141    void Flush(std::vector<PropertyValue>& aOut,
    142               const MonitorAutoLock& aProofOfLock);
    143 
    144   private:
    145    static const uint32_t BUFFER_SIZE = 5;
    146 
    147    /**
    148     * The index of the oldest value in the buffer. This is the next index
    149     * that will be written to.
    150     */
    151    uint32_t mIndex;
    152    PropertyValue mValues[BUFFER_SIZE];
    153  };
    154 
    155 private:
    156  /**
    157   * If true, we should log the various properties during the checkerboard
    158   * event. If false, we only need to record things we need for telemetry
    159   * measures.
    160   */
    161  const bool mRecordTrace;
    162  /**
    163   * A base time so that the other timestamps can be turned into durations.
    164   */
    165  const TimeStamp mOriginTime;
    166  /**
    167   * Whether or not a checkerboard event is currently occurring.
    168   */
    169  bool mCheckerboardingActive;
    170 
    171  /**
    172   * The start time of the checkerboard event.
    173   */
    174  TimeStamp mStartTime;
    175  /**
    176   * The end time of the checkerboard event.
    177   */
    178  TimeStamp mEndTime;
    179  /**
    180   * The sample time of the last frame recorded.
    181   */
    182  TimeStamp mLastSampleTime;
    183  /**
    184   * The number of contiguous frames with checkerboard.
    185   */
    186  uint32_t mFrameCount;
    187  /**
    188   * The total number of pixel-milliseconds of checkerboarding visible to
    189   * the user during the checkerboarding event.
    190   */
    191  uint64_t mTotalPixelMs;
    192  /**
    193   * The largest number of pixels of checkerboarding visible to the user
    194   * during any one frame, during this checkerboarding event.
    195   */
    196  uint32_t mPeakPixels;
    197 
    198  /**
    199   * Monitor that needs to be acquired before touching mBufferedProperties
    200   * or mRendertraceInfo.
    201   */
    202  mutable Monitor mRendertraceLock;
    203  /**
    204   * A circular buffer to store some properties. This is used before the
    205   * checkerboarding actually starts, so that we have some data on what
    206   * was happening before the checkerboarding started.
    207   */
    208  PropertyBuffer mBufferedProperties[sRendertracePropertyCount] MOZ_GUARDED_BY(
    209      mRendertraceLock);
    210  /**
    211   * The rendertrace info buffer that gives us info on what was happening
    212   * during the checkerboard event.
    213   */
    214  std::ostringstream mRendertraceInfo MOZ_GUARDED_BY(mRendertraceLock);
    215 };
    216 
    217 }  // namespace layers
    218 }  // namespace mozilla
    219 
    220 #endif  // mozilla_layers_CheckerboardEvent_h