File: recently_destroyed_hosts.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,601 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_RECENTLY_DESTROYED_HOSTS_H_
#define CONTENT_BROWSER_RENDERER_HOST_RECENTLY_DESTROYED_HOSTS_H_

#include "base/containers/flat_set.h"
#include "base/supports_user_data.h"
#include "base/time/time.h"
#include "content/common/content_export.h"

namespace base {
class TimeDelta;
class TimeTicks;
}  // namespace base

namespace content {

class BrowserContext;
class ProcessLock;
class RenderProcessHost;

// Stores information about recently destroyed RenderProcessHosts to gather
// metrics on process reuse potential. It maintains separate tracking for
// short-term subframe reuse and long-term main frame reuse.
class CONTENT_EXPORT RecentlyDestroyedHosts
    : public base::SupportsUserData::Data {
 public:
  // Describes the context for which a metric is being recorded.
  enum class Context {
    kMainFrame,
    kSubframe,
  };

  // Storage time for tracking recently destroyed processes for subframe reuse.
  // Intended to be long enough to capture a large portion of the process-reuse
  // opportunity.
  static constexpr base::TimeDelta kSubframeStorageTimeout = base::Seconds(60);

  // Long-duration storage time for the sites of recently destroyed processes,
  // specifically for the main frame reuse metric.
  // Intended to be long enough to capture session-level reuse patterns (e.g., a
  // user navigating away from and back to a site within a couple of hours).
  static constexpr base::TimeDelta kMainFrameStorageTimeout = base::Hours(2);

  ~RecentlyDestroyedHosts() override;
  RecentlyDestroyedHosts(const RecentlyDestroyedHosts& other) = delete;
  RecentlyDestroyedHosts& operator=(const RecentlyDestroyedHosts& other) =
      delete;

  // Looks for a recently destroyed host matching |process_lock| and records
  // metrics based on the |context|. For both main frames and subframes, this
  // records two histograms: a boolean for whether a potential reuse candidate
  // was found, and a timing histogram for the delay if one was found.
  static void RecordMetricIfReusableHostRecentlyDestroyed(
      Context context,
      const base::TimeTicks& reusable_host_lookup_time,
      const ProcessLock& process_lock,
      BrowserContext* browser_context);

  // Adds |host|'s process lock to the list of recently destroyed hosts, or
  // updates its time if it's already present.
  static void Add(RenderProcessHost* host,
                  const base::TimeDelta& time_spent_running_unload_handlers,
                  BrowserContext* browser_context);

 private:
  friend class RecentlyDestroyedHostsTest;

  // The |interval| between a renderer process's destruction and the creation of
  // another renderer process for the same domain.
  struct ReuseInterval {
    base::TimeDelta interval;
    base::TimeTicks time_added;
    bool operator<(const ReuseInterval& rhs) const {
      return std::tie(interval, time_added) <
             std::tie(rhs.interval, rhs.time_added);
    }
  };

  RecentlyDestroyedHosts();

  // Returns the RecentlyDestroyedHosts instance stored in |browser_context|, or
  // creates an instance in |browser_context| and returns it if none exists.
  static RecentlyDestroyedHosts* GetInstance(BrowserContext* browser_context);

  // Removes all entries older than |kSubframeStorageTimeout| from
  // |recently_destroyed_hosts_for_subframe_reuse_|.
  void RemoveExpiredHostsForSubframeReuse();

  // Removes all entries older than |kMainFrameStorageTimeout| from
  // |recently_destroyed_hosts_for_main_frame_reuse_|.
  void RemoveExpiredHostsForMainFrameReuse();

  void AddReuseInterval(const base::TimeDelta& interval);

  // List of recent intervals between renderer-process destruction and the
  // creation of a renderer process for the same site. Sorted by interval, from
  // shortest to longest. Only a limited number are stored.
  base::flat_set<ReuseInterval> reuse_intervals_;

  // Map of ProcessLock to destruction time, for RenderProcessHosts destroyed
  // in the last |kSubframeStorageTimeout| seconds.
  std::map<ProcessLock, base::TimeTicks>
      recently_destroyed_hosts_for_subframe_reuse_;

  // Map of ProcessLock to destruction time for main frame reuse tracking.
  // Entries are kept for a long duration defined by |kMainFrameStorageTimeout|.
  std::map<ProcessLock, base::TimeTicks>
      recently_destroyed_hosts_for_main_frame_reuse_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_RECENTLY_DESTROYED_HOSTS_H_