File: recently_destroyed_hosts.cc

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 (195 lines) | stat: -rw-r--r-- 7,468 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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.

#include "content/browser/renderer_host/recently_destroyed_hosts.h"

#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/process_lock.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_process_host.h"

namespace content {

namespace {

// Maintains a list of recently destroyed processes to gather metrics on the
// potential for process reuse (crbug.com/894253).
const void* const kRecentlyDestroyedHostTrackerKey =
    "RecentlyDestroyedHostTrackerKey";

}  // namespace

constexpr base::TimeDelta RecentlyDestroyedHosts::kSubframeStorageTimeout;
constexpr base::TimeDelta RecentlyDestroyedHosts::kMainFrameStorageTimeout;

RecentlyDestroyedHosts::~RecentlyDestroyedHosts() = default;

// static
void RecentlyDestroyedHosts::RecordMetricIfReusableHostRecentlyDestroyed(
    Context context,
    const base::TimeTicks& reusable_host_lookup_time,
    const ProcessLock& process_lock,
    BrowserContext* browser_context) {
  auto* instance = GetInstance(browser_context);

  switch (context) {
    case Context::kMainFrame: {
      instance->RemoveExpiredHostsForMainFrameReuse();
      const auto iter =
          instance->recently_destroyed_hosts_for_main_frame_reuse_.find(
              process_lock);

      const bool found =
          iter !=
          instance->recently_destroyed_hosts_for_main_frame_reuse_.end();
      base::UmaHistogramBoolean(
          "SiteIsolation.MissedReuseOpportunity.Found.MainFrame", found);

      if (found) {
        const base::TimeDelta reuse_interval =
            reusable_host_lookup_time - iter->second;
        base::UmaHistogramCustomTimes(
            "SiteIsolation.ReusePendingOrCommittedSite."
            "TimeSinceReusableProcessDestroyed.MainFrame",
            reuse_interval, base::Milliseconds(1), kMainFrameStorageTimeout,
            50);
      }
      break;
    }
    case Context::kSubframe: {
      instance->RemoveExpiredHostsForSubframeReuse();
      const auto iter =
          instance->recently_destroyed_hosts_for_subframe_reuse_.find(
              process_lock);

      const bool found =
          iter != instance->recently_destroyed_hosts_for_subframe_reuse_.end();
      base::UmaHistogramBoolean(
          "SiteIsolation.MissedReuseOpportunity.Found.Subframe", found);

      if (found) {
        const base::TimeDelta reuse_interval =
            reusable_host_lookup_time - iter->second;
        base::UmaHistogramCustomTimes(
            "SiteIsolation.ReusePendingOrCommittedSite."
            "TimeSinceReusableProcessDestroyed.Subframe",
            reuse_interval, base::Milliseconds(1), kSubframeStorageTimeout, 50);
      }
      break;
    }
  }
  // Add zero to the list of recent reuse intervals to reduce the calculated
  // delay when no reuse has been possible for a while.
  instance->AddReuseInterval(base::TimeDelta());
}

// static
void RecentlyDestroyedHosts::Add(
    RenderProcessHost* host,
    const base::TimeDelta& time_spent_running_unload_handlers,
    BrowserContext* browser_context) {
  if (time_spent_running_unload_handlers > kSubframeStorageTimeout) {
    return;
  }

  ProcessLock process_lock = host->GetProcessLock();

  // Don't record sites with an empty process lock. This includes sites on
  // Android that are not isolated. These sites would not be affected by
  // increased process reuse, so are irrelevant for the metric being recorded.
  if (!process_lock.is_locked_to_site())
    return;

  // Record the time before |time_spent_running_unload_handlers| to exclude time
  // spent in delayed-shutdown state from the metric. This makes it consistent
  // across processes that were delayed by DelayProcessShutdown(), and those
  // that weren't.
  auto* instance = GetInstance(browser_context);
  const base::TimeTicks destruction_time =
      base::TimeTicks::Now() - time_spent_running_unload_handlers;

  instance->recently_destroyed_hosts_for_subframe_reuse_[process_lock] =
      destruction_time;
  instance->recently_destroyed_hosts_for_main_frame_reuse_[process_lock] =
      destruction_time;

  // Periodically clean up the tracking maps if they get large. This is a
  // fallback in case no lookups have occurred recently to trigger the main
  // cleanup logic. The main frame map has a larger size limit because its
  // entries are retained for a long time (2 hours) to track session-level
  // reuse. The subframe map has a much shorter retention period (60 seconds),
  // so a smaller size limit is sufficient.
  if (instance->recently_destroyed_hosts_for_subframe_reuse_.size() > 20) {
    instance->RemoveExpiredHostsForSubframeReuse();
  }
  if (instance->recently_destroyed_hosts_for_main_frame_reuse_.size() > 200) {
    instance->RemoveExpiredHostsForMainFrameReuse();
  }
}

RecentlyDestroyedHosts::RecentlyDestroyedHosts() = default;

RecentlyDestroyedHosts* RecentlyDestroyedHosts::GetInstance(
    BrowserContext* browser_context) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  RecentlyDestroyedHosts* recently_destroyed_hosts =
      static_cast<RecentlyDestroyedHosts*>(
          browser_context->GetUserData(kRecentlyDestroyedHostTrackerKey));
  if (recently_destroyed_hosts)
    return recently_destroyed_hosts;

  recently_destroyed_hosts = new RecentlyDestroyedHosts;
  browser_context->SetUserData(kRecentlyDestroyedHostTrackerKey,
                               base::WrapUnique(recently_destroyed_hosts));
  return recently_destroyed_hosts;
}

void RecentlyDestroyedHosts::RemoveExpiredHostsForSubframeReuse() {
  const auto expired_cutoff_time =
      base::TimeTicks::Now() - kSubframeStorageTimeout;
  for (auto iter = recently_destroyed_hosts_for_subframe_reuse_.begin();
       iter != recently_destroyed_hosts_for_subframe_reuse_.end();) {
    if (iter->second < expired_cutoff_time) {
      iter = recently_destroyed_hosts_for_subframe_reuse_.erase(iter);
    } else {
      ++iter;
    }
  }
}

void RecentlyDestroyedHosts::RemoveExpiredHostsForMainFrameReuse() {
  const auto expired_cutoff_time =
      base::TimeTicks::Now() - kMainFrameStorageTimeout;
  for (auto iter = recently_destroyed_hosts_for_main_frame_reuse_.begin();
       iter != recently_destroyed_hosts_for_main_frame_reuse_.end();) {
    if (iter->second < expired_cutoff_time) {
      iter = recently_destroyed_hosts_for_main_frame_reuse_.erase(iter);
    } else {
      ++iter;
    }
  }
}

void RecentlyDestroyedHosts::AddReuseInterval(const base::TimeDelta& interval) {
  // The maximum size of |reuse_intervals_|. Kept small to ensure that delay
  // calculations that use |reuse_intervals_| are based on recent patterns.
  static constexpr size_t kReuseIntervalsMaxSize = 5;

  reuse_intervals_.insert({interval, base::TimeTicks::Now()});
  if (reuse_intervals_.size() > kReuseIntervalsMaxSize) {
    auto oldest_entry =
        std::min_element(reuse_intervals_.begin(), reuse_intervals_.end(),
                         [](ReuseInterval a, ReuseInterval b) {
                           return a.time_added < b.time_added;
                         });
    reuse_intervals_.erase(oldest_entry);
  }
  DCHECK_LE(reuse_intervals_.size(), kReuseIntervalsMaxSize);
}

}  // namespace content