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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/gpu_process_host.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram.h"
#include "base/process_util.h"
#include "base/string_piece.h"
#include "chrome/browser/gpu_process_host_ui_shim.h"
#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_widget_host.h"
#include "content/browser/renderer_host/render_widget_host_view.h"
#include "content/common/gpu_messages.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_switches.h"
#include "media/base/media_switches.h"
#include "ui/gfx/gl/gl_switches.h"
#if defined(OS_LINUX)
#include "ui/gfx/gtk_native_view_id_manager.h"
#endif // defined(OS_LINUX)
namespace {
enum GPUProcessLifetimeEvent {
LAUNCHED,
DIED_FIRST_TIME,
DIED_SECOND_TIME,
DIED_THIRD_TIME,
DIED_FOURTH_TIME,
GPU_PROCESS_LIFETIME_EVENT_MAX
};
// A global map from GPU process host ID to GpuProcessHost.
static IDMap<GpuProcessHost> g_hosts_by_id;
// Number of times the gpu process has crashed in the current browser session.
static int g_gpu_crash_count = 0;
// Maximum number of times the gpu process is allowed to crash in a session.
// Once this limit is reached, any request to launch the gpu process will fail.
static const int kGpuMaxCrashCount = 3;
} // anonymous namespace
// static
GpuProcessHost* GpuProcessHost::Create(
int host_id,
const GpuFeatureFlags& gpu_feature_flags,
content::CauseForGpuLaunch cause_for_gpu_launch) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
GpuProcessHost* host = new GpuProcessHost(host_id,
gpu_feature_flags,
cause_for_gpu_launch);
if (!host->Init()) {
delete host;
return NULL;
}
return host;
}
// static
GpuProcessHost* GpuProcessHost::FromID(int host_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (host_id == 0)
return NULL;
return g_hosts_by_id.Lookup(host_id);
}
GpuProcessHost::GpuProcessHost(
int host_id,
const GpuFeatureFlags& gpu_feature_flags,
content::CauseForGpuLaunch cause_for_gpu_launch)
: BrowserChildProcessHost(GPU_PROCESS),
host_id_(host_id),
gpu_feature_flags_(gpu_feature_flags) {
g_hosts_by_id.AddWithID(this, host_id_);
UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLaunchCause",
cause_for_gpu_launch,
content::CAUSE_FOR_GPU_LAUNCH_MAX_ENUM);
}
GpuProcessHost::~GpuProcessHost() {
DCHECK(CalledOnValidThread());
g_hosts_by_id.Remove(host_id_);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
NewRunnableFunction(GpuProcessHostUIShim::Destroy,
host_id_));
}
bool GpuProcessHost::Init() {
if (!CreateChannel())
return false;
if (!LaunchGpuProcess())
return false;
return Send(new GpuMsg_Initialize());
}
void GpuProcessHost::RouteOnUIThread(const IPC::Message& message) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
new RouteToGpuProcessHostUIShimTask(host_id_, message));
}
bool GpuProcessHost::Send(IPC::Message* msg) {
DCHECK(CalledOnValidThread());
return BrowserChildProcessHost::Send(msg);
}
bool GpuProcessHost::OnMessageReceived(const IPC::Message& message) {
DCHECK(CalledOnValidThread());
RouteOnUIThread(message);
return true;
}
bool GpuProcessHost::CanShutdown() {
return true;
}
void GpuProcessHost::OnProcessLaunched() {
// Send the GPU process handle to the UI thread before it has to
// respond to any requests to establish a GPU channel. The response
// to such requests require that the GPU process handle be known.
base::ProcessHandle child_handle;
#if defined(OS_WIN)
DuplicateHandle(base::GetCurrentProcessHandle(),
handle(),
base::GetCurrentProcessHandle(),
&child_handle,
PROCESS_DUP_HANDLE,
FALSE,
0);
#else
child_handle = handle();
#endif
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
NewRunnableFunction(&GpuProcessHostUIShim::NotifyGpuProcessLaunched,
host_id_,
child_handle));
}
namespace {
void SendOutstandingRepliesDispatcher(int host_id) {
GpuProcessHostUIShim *ui_shim = GpuProcessHostUIShim::FromID(host_id);
DCHECK(ui_shim);
ui_shim->SendOutstandingReplies();
}
void SendOutstandingReplies(int host_id) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
NewRunnableFunction(&SendOutstandingRepliesDispatcher, host_id));
}
} // namespace
void GpuProcessHost::OnChildDied() {
SendOutstandingReplies(host_id_);
// Located in OnChildDied because OnProcessCrashed suffers from a race
// condition on Linux.
UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLifetimeEvents",
DIED_FIRST_TIME + g_gpu_crash_count,
GPU_PROCESS_LIFETIME_EVENT_MAX);
base::TerminationStatus status = GetChildTerminationStatus(NULL);
UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessTerminationStatus",
status,
base::TERMINATION_STATUS_MAX_ENUM);
BrowserChildProcessHost::OnChildDied();
}
void GpuProcessHost::OnProcessCrashed(int exit_code) {
SendOutstandingReplies(host_id_);
if (++g_gpu_crash_count >= kGpuMaxCrashCount) {
// The gpu process is too unstable to use. Disable it for current session.
RenderViewHostDelegateHelper::set_gpu_enabled(false);
}
BrowserChildProcessHost::OnProcessCrashed(exit_code);
}
bool GpuProcessHost::LaunchGpuProcess() {
if (!RenderViewHostDelegateHelper::gpu_enabled() ||
g_gpu_crash_count >= kGpuMaxCrashCount)
{
SendOutstandingReplies(host_id_);
RenderViewHostDelegateHelper::set_gpu_enabled(false);
return false;
}
const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
CommandLine::StringType gpu_launcher =
browser_command_line.GetSwitchValueNative(switches::kGpuLauncher);
FilePath exe_path = ChildProcessHost::GetChildPath(gpu_launcher.empty());
if (exe_path.empty())
return false;
CommandLine* cmd_line = new CommandLine(exe_path);
cmd_line->AppendSwitchASCII(switches::kProcessType, switches::kGpuProcess);
cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id());
SetCrashReporterCommandLine(cmd_line);
// Propagate relevant command line switches.
static const char* const kSwitchNames[] = {
switches::kUseGL,
switches::kDisableGpuSandbox,
switches::kDisableGpuVsync,
switches::kDisableGpuWatchdog,
switches::kDisableLogging,
switches::kEnableAcceleratedDecoding,
switches::kEnableLogging,
#if defined(OS_MACOSX)
switches::kEnableSandboxLogging,
#endif
switches::kGpuStartupDialog,
switches::kLoggingLevel,
switches::kNoSandbox,
switches::kDisableGLMultisampling,
};
cmd_line->CopySwitchesFrom(browser_command_line, kSwitchNames,
arraysize(kSwitchNames));
if (gpu_feature_flags_.flags() & GpuFeatureFlags::kGpuFeatureMultisampling) {
cmd_line->AppendSwitch(switches::kDisableGLMultisampling);
}
// If specified, prepend a launcher program to the command line.
if (!gpu_launcher.empty())
cmd_line->PrependWrapper(gpu_launcher);
Launch(
#if defined(OS_WIN)
FilePath(),
#elif defined(OS_POSIX)
false, // Never use the zygote (GPU plugin can't be sandboxed).
base::environment_vector(),
#endif
cmd_line);
UMA_HISTOGRAM_ENUMERATION("GPU.GPUProcessLifetimeEvents",
LAUNCHED, GPU_PROCESS_LIFETIME_EVENT_MAX);
return true;
}
|