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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/performance_manager/graph/process_node_impl_describer.h"
#include "base/i18n/time_formatting.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/task_traits.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/graph/node_data_describer_registry.h"
#include "components/performance_manager/public/graph/process_node.h"
#include "content/public/browser/child_process_host.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/win_util.h"
#endif
namespace performance_manager {
namespace {
const char kDescriberName[] = "ProcessNodeImpl";
std::string ContentTypeToString(ProcessNode::ContentType content_type) {
switch (content_type) {
case ProcessNode::ContentType::kExtension:
return "Extension";
case ProcessNode::ContentType::kMainFrame:
return "Main frame";
case ProcessNode::ContentType::kSubframe:
return "Subframe";
case ProcessNode::ContentType::kNavigatedFrame:
return "Navigated Frame";
case ProcessNode::ContentType::kAd:
return "Ad";
case ProcessNode::ContentType::kWorker:
return "Worker";
}
}
std::string HostedProcessTypesToString(
ProcessNode::ContentTypes hosted_content_types) {
std::vector<std::string> content_types_vector;
content_types_vector.reserve(hosted_content_types.size());
for (ProcessNode::ContentType content_type : hosted_content_types)
content_types_vector.push_back(ContentTypeToString(content_type));
std::string str = base::JoinString(content_types_vector, ", ");
if (str.empty())
str = "none";
return str;
}
#if !BUILDFLAG(IS_APPLE)
const char* GetProcessPriorityString(const base::Process& process) {
switch (process.GetPriority()) {
case base::Process::Priority::kBestEffort:
return "Best effort";
case base::Process::Priority::kUserVisible:
return "User visible";
case base::Process::Priority::kUserBlocking:
return "User blocking";
}
NOTREACHED();
}
#endif
base::Value GetProcessValueDict(const base::Process& process) {
base::Value::Dict ret;
// On Windows, handle is a void *. On Fuchsia it's an int. On other platforms
// it is equal to the pid, so don't bother to record it.
#if BUILDFLAG(IS_WIN)
ret.Set("handle",
static_cast<int>(base::win::HandleToUint32(process.Handle())));
#elif BUILDFLAG(IS_FUCHSIA)
ret.Set("handle", static_cast<int>(process.Handle()));
#endif
// Most processes are not current, so only show the outliers.
if (process.is_current()) {
ret.Set("is_current", true);
}
#if BUILDFLAG(IS_CHROMEOS)
if (process.GetPidInNamespace() != base::kNullProcessId) {
ret.Set("pid_in_namespace", process.GetPidInNamespace());
}
#endif
#if BUILDFLAG(IS_WIN)
// Creation time is always available on Windows, even for dead processes.
// On other platforms it is available only for valid processes (see below).
ret.Set("creation_time",
base::TimeFormatTimeOfDayWithMilliseconds(process.CreationTime()));
#endif
if (process.IsValid()) {
// These properties can only be accessed for valid processes.
ret.Set("os_priority", process.GetOSPriority());
#if !BUILDFLAG(IS_APPLE)
ret.Set("priority", GetProcessPriorityString(process));
#endif
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_WIN)
ret.Set("creation_time",
base::TimeFormatTimeOfDayWithMilliseconds(process.CreationTime()));
#endif
#if BUILDFLAG(IS_WIN)
// Most processes are running, so only show the outliers.
if (!process.IsRunning()) {
ret.Set("is_running", false);
}
#endif
} else {
ret.Set("is_valid", false);
}
return base::Value(std::move(ret));
}
// Converts TimeTicks to Time. The conversion will be incorrect if system
// time is adjusted between `ticks` and now.
base::Time TicksToTime(base::TimeTicks ticks) {
base::Time now_time = base::Time::Now();
base::TimeTicks now_ticks = base::TimeTicks::Now();
base::TimeDelta elapsed_since_ticks = now_ticks - ticks;
return now_time - elapsed_since_ticks;
}
} // namespace
void ProcessNodeImplDescriber::OnPassedToGraph(Graph* graph) {
graph->GetNodeDataDescriberRegistry()->RegisterDescriber(this,
kDescriberName);
}
void ProcessNodeImplDescriber::OnTakenFromGraph(Graph* graph) {
graph->GetNodeDataDescriberRegistry()->UnregisterDescriber(this);
}
base::Value::Dict ProcessNodeImplDescriber::DescribeProcessNodeData(
const ProcessNode* node) const {
const ProcessNodeImpl* impl = ProcessNodeImpl::FromNode(node);
base::Value::Dict ret;
ret.Set("pid", base::NumberToString(impl->GetProcessId()));
ret.Set("process", GetProcessValueDict(impl->GetProcess()));
ret.Set("launch_time", base::TimeFormatTimeOfDayWithMilliseconds(
TicksToTime(impl->GetLaunchTime())));
ret.Set("resource_context", impl->GetResourceContext().ToString());
if (impl->GetExitStatus()) {
ret.Set("exit_status", impl->GetExitStatus().value());
}
if (!impl->GetMetricsName().empty()) {
ret.Set("metrics_name", impl->GetMetricsName());
}
ret.Set("priority", base::TaskPriorityToString(impl->GetPriority()));
if (impl->GetPrivateFootprintKb()) {
ret.Set("private_footprint_kb",
base::saturated_cast<int>(impl->GetPrivateFootprintKb()));
}
if (impl->GetResidentSetKb()) {
ret.Set("resident_set_kb",
base::saturated_cast<int>(impl->GetResidentSetKb()));
}
// The content function returns "Tab" for renderers - whereas "Renderer" is
// the common vernacular here.
std::string process_type =
content::GetProcessTypeNameInEnglish(impl->GetProcessType());
if (impl->GetProcessType() == content::PROCESS_TYPE_RENDERER) {
process_type = "Renderer";
}
ret.Set("process_type", process_type);
if (impl->GetProcessType() == content::PROCESS_TYPE_RENDERER) {
// Renderer-only properties.
ret.Set("render_process_id", impl->GetRenderProcessHostId().value());
ret.Set("main_thread_task_load_is_low", impl->GetMainThreadTaskLoadIsLow());
ret.Set("hosted_content_types",
HostedProcessTypesToString(impl->GetHostedContentTypes()));
} else if (impl->GetProcessType() != content::PROCESS_TYPE_BROWSER) {
// Non-renderer child process properties.
ret.Set("browser_child_process_host_id",
impl->GetBrowserChildProcessHostProxy()
.browser_child_process_host_id()
.value());
}
return ret;
}
} // namespace performance_manager
|