tor-browser

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

nsWindowMemoryReporter.h (6045B)


      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 nsWindowMemoryReporter_h__
      8 #define nsWindowMemoryReporter_h__
      9 
     10 #include "mozilla/TimeStamp.h"
     11 #include "nsIMemoryReporter.h"
     12 #include "nsIObserver.h"
     13 #include "nsITimer.h"
     14 #include "nsTHashMap.h"
     15 #include "nsTHashSet.h"
     16 #include "nsWeakReference.h"
     17 
     18 class nsGlobalWindowInner;
     19 
     20 /**
     21 * nsWindowMemoryReporter is responsible for the 'explicit/window-objects'
     22 * memory reporter.
     23 *
     24 * We classify DOM window objects into one of three categories:
     25 *
     26 *   - "active" windows, which are displayed in a tab (as the top-level window
     27 *     or an iframe),
     28 *
     29 *   - "cached" windows, which are in the fastback cache (aka the bfcache), and
     30 *
     31 *   - "detached" windows, which have a null docshell.  A window becomes
     32 *     detached when its <iframe> or tab containing the window is destroyed --
     33 *     i.e., when the window is no longer active or cached.
     34 *
     35 * Additionally, we classify a subset of detached windows as "ghost" windows.
     36 * Although ghost windows can happen legitimately (a page can hold a reference
     37 * to a cross-domain window and then close its container), the presence of
     38 * ghost windows is often indicative of a memory leak.
     39 *
     40 * A window is a ghost if it meets the following three criteria:
     41 *
     42 *   1) The window is detached.
     43 *
     44 *   2) There exist no non-detached windows with the same base domain as
     45 *      the window's principal.  (For example, the base domain of
     46 *      "wiki.mozilla.co.uk" is "mozilla.co.uk".)  This criterion makes us less
     47 *      likely to flag a legitimately held-alive detached window as a ghost.
     48 *
     49 *   3) The window has met criteria (1) and (2) above for at least
     50 *      memory.ghost_window_timeout_seconds.  This criterion is in place so we
     51 *      don't immediately declare a window a ghost before the GC/CC has had a
     52 *      chance to run.
     53 *
     54 * nsWindowMemoryReporter observes window detachment and uses mDetachedWindows
     55 * to remember when a window first met criteria (1) and (2).  When we generate
     56 * a memory report, we use this accounting to determine which windows are
     57 * ghosts.
     58 *
     59 *
     60 * We use the following memory reporter path for active and cached windows:
     61 *
     62 *   explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/
     63 *       <category>/window(<window-uri>)/...
     64 *
     65 * For detached and ghost windows, we use
     66 *
     67 *   explicit/window-objects/top(none)/<category>/window(<window-uri>)/...
     68 *
     69 * Where
     70 *
     71 * - <category> is "active", "cached", "detached", or "ghost", as described
     72 *   above.
     73 *
     74 * - <top-outer-id> is the window id of the top outer window (i.e. the tab, or
     75 *   the top level chrome window).  Exposing this ensures that each tab gets
     76 *   its own sub-tree, even if multiple tabs are showing the same URI.
     77 *
     78 * - <top-uri> is the URI of the top window.  Excepting special windows (such
     79 *   as browser.xhtml or hiddenWindowMac.xhtml) it's what the address bar shows
     80 *   for the tab.
     81 *
     82 */
     83 class nsWindowMemoryReporter final : public nsIMemoryReporter,
     84                                     public nsIObserver,
     85                                     public nsSupportsWeakReference {
     86 public:
     87  NS_DECL_ISUPPORTS
     88  NS_DECL_NSIMEMORYREPORTER
     89  NS_DECL_NSIOBSERVER
     90 
     91  static void Init();
     92 
     93 #ifdef DEBUG
     94  /**
     95   * Unlink all known ghost windows, to enable investigating what caused them
     96   * to become ghost windows in the first place.
     97   */
     98  static void UnlinkGhostWindows();
     99 #endif
    100 
    101  static nsWindowMemoryReporter* Get();
    102  void ObserveDOMWindowDetached(nsGlobalWindowInner* aWindow);
    103 
    104  static int64_t GhostWindowsDistinguishedAmount();
    105 
    106 private:
    107  ~nsWindowMemoryReporter();
    108 
    109  // Protect ctor, use Init() instead.
    110  nsWindowMemoryReporter();
    111 
    112  /**
    113   * Get the number of seconds for which a window must satisfy ghost criteria
    114   * (1) and (2) before we deem that it satisfies criterion (3).
    115   */
    116  uint32_t GetGhostTimeout();
    117 
    118  void ObserveAfterMinimizeMemoryUsage();
    119 
    120  /**
    121   * Iterate over all weak window pointers in mDetachedWindows and update our
    122   * accounting of which windows meet ghost criterion (2).
    123   *
    124   * This method also cleans up mDetachedWindows, removing entries for windows
    125   * which have been destroyed or are no longer detached.
    126   *
    127   * If aOutGhostIDs is non-null, we populate it with the Window IDs of the
    128   * ghost windows.
    129   *
    130   * This is called asynchronously after we observe a DOM window being detached
    131   * from its docshell, and also right before we generate a memory report.
    132   */
    133  void CheckForGhostWindows(nsTHashSet<uint64_t>* aOutGhostIDs = nullptr);
    134 
    135  /**
    136   * Eventually do a check for ghost windows, if we haven't done one recently
    137   * and we aren't already planning to do one soon.
    138   */
    139  void AsyncCheckForGhostWindows();
    140 
    141  /**
    142   * Kill the check timer, if it exists.
    143   */
    144  void KillCheckTimer();
    145 
    146  static void CheckTimerFired(nsITimer* aTimer, void* aClosure);
    147 
    148  /**
    149   * Maps a weak reference to a detached window (nsIWeakReference) to the time
    150   * when we observed that the window met ghost criterion (2) above.
    151   *
    152   * If the window has not yet met criterion (2) it maps to the null timestamp.
    153   *
    154   * (Although windows are not added to this table until they're detached, it's
    155   * possible for a detached window to become non-detached, and we won't
    156   * remove it from the table until CheckForGhostWindows runs.)
    157   */
    158  nsTHashMap<nsISupportsHashKey, mozilla::TimeStamp> mDetachedWindows;
    159 
    160  /**
    161   * Track the last time we ran CheckForGhostWindows(), to avoid running it
    162   * too often after a DOM window is detached.
    163   */
    164  mozilla::TimeStamp mLastCheckForGhostWindows;
    165 
    166  nsCOMPtr<nsITimer> mCheckTimer;
    167 
    168  bool mCycleCollectorIsRunning;
    169 
    170  bool mCheckTimerWaitingForCCEnd;
    171 
    172  int64_t mGhostWindowCount;
    173 };
    174 
    175 #endif  // nsWindowMemoryReporter_h__