File: metrics_memory_details.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 5,243 bytes parent folder | download | duplicates (6)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/metrics_memory_details.h"

#include <stddef.h>

#include <vector>

#include "base/location.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/site_isolation/site_details.h"
#include "components/nacl/common/nacl_process_type.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/process_type.h"
#include "ppapi/buildflags/buildflags.h"

namespace {

void CountRenderProcessHosts(size_t* initialized_and_not_dead, size_t* all) {
  *initialized_and_not_dead = *all = 0;

  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  for (auto iter = content::RenderProcessHost::AllHostsIterator();
       !iter.IsAtEnd(); iter.Advance()) {
    content::RenderProcessHost& render_process_host = *iter.GetCurrentValue();
    ++*all;
    if (render_process_host.IsInitializedAndNotDead())
      ++*initialized_and_not_dead;
  }
}

}  // namespace

MetricsMemoryDetails::MetricsMemoryDetails(base::OnceClosure callback)
    : callback_(std::move(callback)) {}

MetricsMemoryDetails::~MetricsMemoryDetails() = default;

void MetricsMemoryDetails::OnDetailsAvailable() {
  UpdateHistograms();
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, std::move(callback_));
}

void MetricsMemoryDetails::UpdateHistograms() {
  // Reports a set of memory metrics to UMA.

  const ProcessData& browser = *ChromeBrowser();
  int renderer_count = 0;
  for (auto& process : browser.processes) {
    if (process.process_type == content::PROCESS_TYPE_RENDERER) {
      ProcessMemoryInformation::RendererProcessType renderer_type =
          process.renderer_type;
      switch (renderer_type) {
        case ProcessMemoryInformation::RENDERER_EXTENSION:
        case ProcessMemoryInformation::RENDERER_CHROME:
          continue;
        case ProcessMemoryInformation::RENDERER_UNKNOWN:
          NOTREACHED() << "Unknown renderer process type.";
        case ProcessMemoryInformation::RENDERER_NORMAL:
        default:
          renderer_count++;
          continue;
      }
    }
  }
#if BUILDFLAG(IS_CHROMEOS)
  // Chrome OS exposes system-wide graphics driver memory which has historically
  // been a source of leak/bloat.
  base::GraphicsMemoryInfoKB meminfo;
  if (base::GetGraphicsMemoryInfo(&meminfo)) {
    UMA_HISTOGRAM_MEMORY_MB("Memory.Graphics",
                            meminfo.gpu_memory_size / 1024 / 1024);
  }
#endif

  size_t initialized_and_not_dead_rphs;
  size_t all_rphs;
  CountRenderProcessHosts(&initialized_and_not_dead_rphs, &all_rphs);
  UpdateSiteIsolationMetrics(initialized_and_not_dead_rphs);

  UMA_HISTOGRAM_COUNTS_1000("Memory.ProcessCount2",
                            static_cast<int>(browser.processes.size()));
  UMA_HISTOGRAM_COUNTS_1000("Memory.RendererProcessCount2", renderer_count);

  UMA_HISTOGRAM_COUNTS_1000("Memory.RenderProcessHost.Count2.All", all_rphs);
  UMA_HISTOGRAM_COUNTS_1000(
      "Memory.RenderProcessHost.Count2.InitializedAndNotDead",
      initialized_and_not_dead_rphs);
}

void MetricsMemoryDetails::UpdateSiteIsolationMetrics(
    size_t live_process_count) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  // Track site data for predicting process counts with out-of-process iframes.
  // See site_details.h.
  BrowserContextSiteDataMap site_data_map;

  // First pass, collate the widgets by process ID.
  std::unique_ptr<content::RenderWidgetHostIterator> widget_it(
      content::RenderWidgetHost::GetRenderWidgetHosts());
  while (content::RenderWidgetHost* widget = widget_it->GetNextHost()) {
    // Ignore processes that don't have a connection, such as crashed tabs,
    // or processes that are still launching.
    if (!widget->GetProcess()->IsReady())
      continue;

    content::RenderViewHost* rvh = content::RenderViewHost::From(widget);
    if (!rvh)
      continue;

    content::WebContents* contents =
        content::WebContents::FromRenderViewHost(rvh);
    if (!contents)
      continue;

    // Skip if this is not the RVH for the primary main frame of |contents|, as
    // we want to call SiteDetails::CollectSiteInfo() once per WebContents.
    if (rvh !=
        contents->GetPrimaryPage().GetMainDocument().GetRenderViewHost()) {
      // |rvh| is for a subframe document or a non primary page main document.
      continue;
    }

    // The rest of this block will happen only once per WebContents.
    SiteData& site_data = site_data_map[contents->GetBrowserContext()];
    SiteDetails::CollectSiteInfo(contents->GetPrimaryPage(), &site_data);
  }
  SiteDetails::UpdateHistograms(site_data_map, live_process_count);
}