File: stability_metrics_helper.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 (359 lines) | stat: -rw-r--r-- 12,729 bytes parent folder | download | duplicates (3)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// 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 "components/metrics/stability_metrics_helper.h"

#include <stdint.h>

#include <string>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/variations/hashing.h"
#include "extensions/buildflags/buildflags.h"
#include "third_party/metrics_proto/system_profile.pb.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>  // Needed for STATUS_* codes
#endif

#if BUILDFLAG(IS_ANDROID)
#include "base/android/application_status_listener.h"
#endif

namespace metrics {
namespace {

#if !BUILDFLAG(IS_ANDROID)
// Converts an exit code into something that can be inserted into our
// histograms (which expect non-negative numbers less than MAX_INT).
int MapCrashExitCodeForHistogram(int exit_code) {
#if BUILDFLAG(IS_WIN)
  // Since |abs(STATUS_GUARD_PAGE_VIOLATION) == MAX_INT| it causes problems in
  // histograms.cc. Solve this by remapping it to a smaller value, which
  // hopefully doesn't conflict with other codes.
  if (static_cast<DWORD>(exit_code) == STATUS_GUARD_PAGE_VIOLATION)
    return 0x1FCF7EC3;  // Randomly picked number.
#endif

  return std::abs(exit_code);
}
#endif  // !BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
const char* HostedContentTypeToString(
    RendererHostedContentType hosted_content_type) {
  switch (hosted_content_type) {
    case metrics::RendererHostedContentType::kExtension:
      return "Extension";
    case metrics::RendererHostedContentType::kForegroundMainFrame:
      return "ForegroundMainFrame";
    case metrics::RendererHostedContentType::kForegroundSubframe:
      return "ForegroundSubframe";
    case metrics::RendererHostedContentType::kBackgroundFrame:
      return "BackgroundFrame";
    case metrics::RendererHostedContentType::kInactiveFrame:
      return "InactiveFrame";
    case metrics::RendererHostedContentType::kNoFrameOrExtension:
      return "NoFrameOrExtension";
  }
}

void RecordRendererAbnormalTerminationByHostedContentType(
    RendererHostedContentType hosted_content_type,
    base::TerminationStatus status) {
  if (status == base::TERMINATION_STATUS_NORMAL_TERMINATION) {
    return;
  }

  base::UmaHistogramEnumeration(
      "Stability.RendererAbnormalTermination2.HostedContentType",
      hosted_content_type);
  base::UmaHistogramEnumeration(
      base::StrCat({"Stability.RendererAbnormalTermination2.",
                    HostedContentTypeToString(hosted_content_type)}),
      status, base::TERMINATION_STATUS_MAX_ENUM);
}
#endif  // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)

std::string CdmMetricsNameToUmaPrefix(const std::string& metrics_name) {
  const std::string uma_prefix = "Stability.Media.";

  if (metrics_name == "media.mojom.CdmServiceBroker") {
    return uma_prefix + "CdmServiceBroker.";
  } else if (metrics_name == "media.mojom.MediaFoundationServiceBroker") {
    return uma_prefix + "MediaFoundationServiceBroker.";
  } else if (metrics_name == "media.mojom.MediaDrmSupport") {
    return uma_prefix + "MediaDrmSupport.";
  }

  NOTREACHED();
}

}  // namespace

StabilityMetricsHelper::StabilityMetricsHelper(PrefService* local_state)
    : local_state_(local_state) {
  DCHECK(local_state_);
}

StabilityMetricsHelper::~StabilityMetricsHelper() = default;

#if BUILDFLAG(IS_ANDROID)
void StabilityMetricsHelper::ProvideStabilityMetrics(
    SystemProfileProto* system_profile_proto) {
  SystemProfileProto_Stability* stability_proto =
      system_profile_proto->mutable_stability();

  int count = local_state_->GetInteger(prefs::kStabilityPageLoadCount);
  if (count) {
    stability_proto->set_page_load_count(count);
    local_state_->SetInteger(prefs::kStabilityPageLoadCount, 0);
  }
  count = local_state_->GetInteger(prefs::kStabilityRendererLaunchCount);
  if (count) {
    stability_proto->set_renderer_launch_count(count);
    local_state_->SetInteger(prefs::kStabilityRendererLaunchCount, 0);
  }
}

void StabilityMetricsHelper::ClearSavedStabilityMetrics() {
  local_state_->SetInteger(prefs::kStabilityPageLoadCount, 0);
  local_state_->SetInteger(prefs::kStabilityRendererLaunchCount, 0);
}
#endif  // BUILDFLAG(IS_ANDROID)

// static
void StabilityMetricsHelper::RegisterPrefs(PrefRegistrySimple* registry) {
#if BUILDFLAG(IS_ANDROID)
  registry->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0);
  registry->RegisterIntegerPref(prefs::kStabilityRendererLaunchCount, 0);
#endif  // BUILDFLAG(IS_ANDROID)
}

void StabilityMetricsHelper::IncreaseRendererCrashCount() {
  RecordStabilityEvent(StabilityEventType::kRendererCrash);
}

void StabilityMetricsHelper::IncreaseGpuCrashCount() {
  RecordStabilityEvent(StabilityEventType::kGpuCrash);
}

void StabilityMetricsHelper::BrowserUtilityProcessLaunched(
    const std::string& metrics_name) {
  uint32_t hash = variations::HashName(metrics_name);
  base::UmaHistogramSparse("ChildProcess.Launched.UtilityProcessHash", hash);
  RecordStabilityEvent(StabilityEventType::kUtilityLaunch);
}

void StabilityMetricsHelper::BrowserUtilityProcessCrashed(
    const std::string& metrics_name,
    int exit_code) {
  uint32_t hash = variations::HashName(metrics_name);
  base::UmaHistogramSparse("ChildProcess.Crashed.UtilityProcessHash", hash);
  base::UmaHistogramSparse("ChildProcess.Crashed.UtilityProcessExitCode",
                           exit_code);
  RecordStabilityEvent(StabilityEventType::kUtilityCrash);
}

void StabilityMetricsHelper::BrowserUtilityProcessLaunchFailed(
    const std::string& metrics_name,
    int launch_error_code
#if BUILDFLAG(IS_WIN)
    ,
    DWORD last_error
#endif
) {
  uint32_t hash = variations::HashName(metrics_name);
  base::UmaHistogramSparse("ChildProcess.LaunchFailed.UtilityProcessHash",
                           hash);
  base::UmaHistogramSparse("ChildProcess.LaunchFailed.UtilityProcessErrorCode",
                           launch_error_code);
#if BUILDFLAG(IS_WIN)
  base::UmaHistogramSparse("ChildProcess.LaunchFailed.WinLastError",
                           last_error);
#endif
  // TODO(wfh): Decide if this utility process launch failure should also
  // trigger a Stability Event.
}

void StabilityMetricsHelper::CdmUtilityProcessLaunched(
    const std::string& metrics_name) {
  DVLOG(3) << __func__ << ": metrics_name=" << metrics_name;

  // If failed to launch, we will record the same UMA as False in
  // `CdmUtilityProcessLaunchFailed()` method.
  base::UmaHistogramBoolean(CdmMetricsNameToUmaPrefix(metrics_name) + "Launch",
                            true);
}

void StabilityMetricsHelper::CdmUtilityProcessCrashed(
    const std::string& metrics_name,
    int exit_code) {
  DVLOG(3) << __func__ << ": metrics_name=" << metrics_name
           << ", exit_code=" << exit_code;

  base::UmaHistogramSparse(
      CdmMetricsNameToUmaPrefix(metrics_name) + "Crash.ExitCode", exit_code);
}

void StabilityMetricsHelper::CdmUtilityProcessLaunchFailed(
    const std::string& metrics_name,
    int launch_error_code
#if BUILDFLAG(IS_WIN)
    ,
    DWORD last_error
#endif
) {
  DVLOG(3) << __func__ << ": metrics_name=" << metrics_name
#if BUILDFLAG(IS_WIN)
           << ", launch_error_code=" << launch_error_code
           << ", last_error=" << last_error;
#else
           << ", launch_error_code=" << launch_error_code;
#endif

  base::UmaHistogramBoolean(CdmMetricsNameToUmaPrefix(metrics_name) + "Launch",
                            false);
  base::UmaHistogramSparse(
      CdmMetricsNameToUmaPrefix(metrics_name) + "Launch.LaunchErrorCode",
      launch_error_code);
#if BUILDFLAG(IS_WIN)
  base::UmaHistogramSparse(
      CdmMetricsNameToUmaPrefix(metrics_name) + "Launch.WinLastError",
      last_error);
#endif
}

void StabilityMetricsHelper::LogLoadStarted() {
#if BUILDFLAG(IS_ANDROID)
  IncrementPrefValue(prefs::kStabilityPageLoadCount);
#endif
  RecordStabilityEvent(StabilityEventType::kPageLoad);
}

#if BUILDFLAG(IS_IOS)
void StabilityMetricsHelper::LogRendererCrash() {
  // The actual exit code isn't provided on iOS; use a dummy value.
  constexpr int kDummyExitCode = 105;
  LogRendererCrashImpl(CoarseRendererType::kRenderer, kDummyExitCode);
}
#elif !BUILDFLAG(IS_ANDROID)
void StabilityMetricsHelper::LogRendererCrash(
    RendererHostedContentType hosted_content_type,
    base::TerminationStatus status,
    int exit_code) {
  RecordRendererAbnormalTerminationByHostedContentType(hosted_content_type,
                                                       status);

  CoarseRendererType coarse_renderer_type =
      hosted_content_type == RendererHostedContentType::kExtension
          ? CoarseRendererType::kExtension
          : CoarseRendererType::kRenderer;

  switch (status) {
    case base::TERMINATION_STATUS_NORMAL_TERMINATION:
      break;
    case base::TERMINATION_STATUS_PROCESS_CRASHED:
    case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
    case base::TERMINATION_STATUS_OOM:
      LogRendererCrashImpl(coarse_renderer_type, exit_code);
      break;
#if BUILDFLAG(IS_CHROMEOS)
    case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
      base::UmaHistogramEnumeration("BrowserRenderProcessHost.ChildKills.OOM",
                                    coarse_renderer_type);
      [[fallthrough]];
#endif  // BUILDFLAG(IS_CHROMEOS)
    case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
      base::UmaHistogramEnumeration("BrowserRenderProcessHost.ChildKills",
                                    coarse_renderer_type);
      break;
    case base::TERMINATION_STATUS_STILL_RUNNING:
      base::UmaHistogramEnumeration(
          "BrowserRenderProcessHost.DisconnectedAlive", coarse_renderer_type);
      break;
    case base::TERMINATION_STATUS_LAUNCH_FAILED:
      // TODO(rkaplow): See if we can remove this histogram as we have
      // Stability.Counts2 which has the same metrics.
      base::UmaHistogramEnumeration(
          "BrowserRenderProcessHost.ChildLaunchFailures", coarse_renderer_type);
      base::UmaHistogramSparse(
          "BrowserRenderProcessHost.ChildLaunchFailureCodes", exit_code);
      RecordStabilityEvent(
          hosted_content_type == RendererHostedContentType::kExtension
              ? StabilityEventType::kExtensionRendererFailedLaunch
              : StabilityEventType::kRendererFailedLaunch);
      break;
#if BUILDFLAG(IS_WIN)
    case base::TERMINATION_STATUS_INTEGRITY_FAILURE:
      base::UmaHistogramEnumeration(
          "BrowserRenderProcessHost.ChildCodeIntegrityFailures",
          coarse_renderer_type);
      break;
#endif
    case base::TERMINATION_STATUS_MAX_ENUM:
      NOTREACHED();
  }
}
#endif  // !BUILDFLAG(IS_ANDROID)

void StabilityMetricsHelper::LogRendererLaunched(bool was_extension_process) {
  auto metric = was_extension_process
                    ? StabilityEventType::kExtensionRendererLaunch
                    : StabilityEventType::kRendererLaunch;
  RecordStabilityEvent(metric);
#if BUILDFLAG(IS_ANDROID)
  if (!was_extension_process)
    IncrementPrefValue(prefs::kStabilityRendererLaunchCount);
#endif  // BUILDFLAG(IS_ANDROID)
}

void StabilityMetricsHelper::IncrementPrefValue(const char* path) {
  int value = local_state_->GetInteger(path);
  local_state_->SetInteger(path, value + 1);
}

// static
void StabilityMetricsHelper::RecordStabilityEvent(
    StabilityEventType stability_event_type) {
  UMA_STABILITY_HISTOGRAM_ENUMERATION("Stability.Counts2",
                                      stability_event_type);
}

#if !BUILDFLAG(IS_ANDROID)
void StabilityMetricsHelper::LogRendererCrashImpl(
    CoarseRendererType renderer_type,
    int exit_code) {
  if (renderer_type == CoarseRendererType::kExtension) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
    RecordStabilityEvent(StabilityEventType::kExtensionCrash);
    base::UmaHistogramSparse("CrashExitCodes.Extension",
                             MapCrashExitCodeForHistogram(exit_code));
#else
    NOTREACHED();
#endif
  } else {
    IncreaseRendererCrashCount();
    base::UmaHistogramSparse("CrashExitCodes.Renderer",
                             MapCrashExitCodeForHistogram(exit_code));
  }

  base::UmaHistogramEnumeration("BrowserRenderProcessHost.ChildCrashes",
                                renderer_type);
}
#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace metrics