summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/v8_metrics.cc
blob: 69711164e4fb2e8c5baf311998f6a45583f4df04 (plain)
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
// Copyright 2020 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 "third_party/blink/renderer/bindings/core/v8/v8_metrics.h"

#include <cstdint>

#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/instrumentation/histogram.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"

namespace blink {

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::WasmModuleDecoded& event,
    v8::metrics::Recorder::ContextId context_id) {
  auto ukm = GetUkmRecorderAndSourceId(context_id);
  if (!ukm)
    return;
  ukm::builders::V8_Wasm_ModuleDecoded(ukm->source_id)
      .SetStreamed(event.streamed ? 1 : 0)
      .SetSuccess(event.success ? 1 : 0)
      .SetModuleSize(
          ukm::GetExponentialBucketMinForBytes(event.module_size_in_bytes))
      .SetFunctionCount(event.function_count)
      .SetWallClockDuration(event.wall_clock_duration_in_us)
      .Record(ukm->recorder);
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::WasmModuleCompiled& event,
    v8::metrics::Recorder::ContextId context_id) {
  auto ukm = GetUkmRecorderAndSourceId(context_id);
  if (!ukm)
    return;
  ukm::builders::V8_Wasm_ModuleCompiled(ukm->source_id)
      .SetAsync(event.async ? 1 : 0)
      .SetCached(event.cached ? 1 : 0)
      .SetDeserialized(event.deserialized ? 1 : 0)
      .SetLazy(event.lazy ? 1 : 0)
      .SetStreamed(event.streamed ? 1 : 0)
      .SetSuccess(event.success ? 1 : 0)
      .SetCodeSize(
          ukm::GetExponentialBucketMinForBytes(event.code_size_in_bytes))
      .SetLiftoffBailoutCount(event.liftoff_bailout_count)
      .SetWallClockDuration(event.wall_clock_duration_in_us)
      .Record(ukm->recorder);
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::WasmModuleInstantiated& event,
    v8::metrics::Recorder::ContextId context_id) {
  auto ukm = GetUkmRecorderAndSourceId(context_id);
  if (!ukm)
    return;
  ukm::builders::V8_Wasm_ModuleInstantiated(ukm->source_id)
      .SetSuccess(event.success ? 1 : 0)
      .SetImportedFunctionCount(event.imported_function_count)
      .SetWallClockDuration(event.wall_clock_duration_in_us)
      .Record(ukm->recorder);
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::WasmModuleTieredUp& event,
    v8::metrics::Recorder::ContextId context_id) {
  auto ukm = GetUkmRecorderAndSourceId(context_id);
  if (!ukm)
    return;
  ukm::builders::V8_Wasm_ModuleTieredUp(ukm->source_id)
      .SetLazy(event.lazy ? 1 : 0)
      .SetCodeSize(
          ukm::GetExponentialBucketMinForBytes(event.code_size_in_bytes))
      .SetWallClockDuration(event.wall_clock_duration_in_us)
      .Record(ukm->recorder);
}

namespace {

#if BUILDFLAG(USE_V8_OILPAN)
void ReportAtomicLatencyEvent(int64_t duration_us) {
  UMA_HISTOGRAM_TIMES("V8.GC.Event.MainThread.Full.Atomic.Cpp",
                      base::TimeDelta::FromMicroseconds(duration_us));
}

// Helper function to convert a byte count to a KB count, capping at
// INT_MAX if the number is larger than that.
constexpr int32_t CappedSizeInKB(int64_t size_in_bytes) {
  return base::saturated_cast<int32_t>(size_in_bytes / 1024);
}

// Helper function to convert a B/us count to a KB/ms count, capping at
// INT_MAX if the number is larger than that.
constexpr int32_t CappedEfficacyInKBPerMs(double efficacy_in_bytes_per_us) {
  return base::saturated_cast<int32_t>(efficacy_in_bytes_per_us * 1000 / 1024);
}

void CheckCppEvents(const v8::metrics::GarbageCollectionFullCycle& event) {
  // Check that all used values have been initialized.
  DCHECK_NE(-1, event.total_cpp.mark_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.total_cpp.weak_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.total_cpp.compact_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.total_cpp.sweep_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_cpp.mark_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_cpp.weak_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_cpp.compact_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_cpp.sweep_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_atomic_cpp.mark_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_atomic_cpp.weak_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_atomic_cpp.compact_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.main_thread_atomic_cpp.sweep_wall_clock_duration_in_us);
  DCHECK_NE(-1, event.objects_cpp.bytes_before);
  DCHECK_NE(-1, event.objects_cpp.bytes_after);
  DCHECK_NE(-1, event.objects_cpp.bytes_freed);
  DCHECK_NE(-1, event.memory_cpp.bytes_freed);
  DCHECK_NE(-1, event.efficiency_cpp_in_bytes_per_us);
  DCHECK_NE(-1, event.main_thread_efficiency_cpp_in_bytes_per_us);
  DCHECK_NE(-1, event.collection_rate_cpp_in_percent);
}
#endif  // USE_V8_OILPAN

}  // namespace

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::GarbageCollectionFullCycle& event,
    ContextId context_id) {
#if BUILDFLAG(USE_V8_OILPAN)
  // Cpp events should always be populated when building with USE_V8_OILPAN.
  CheckCppEvents(event);
  // Report throughput metrics:
  UMA_HISTOGRAM_TIMES("V8.GC.Cycle.Full.Cpp",
                      base::TimeDelta::FromMicroseconds(
                          event.total_cpp.mark_wall_clock_duration_in_us +
                          event.total_cpp.weak_wall_clock_duration_in_us +
                          event.total_cpp.compact_wall_clock_duration_in_us +
                          event.total_cpp.sweep_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES("V8.GC.Cycle.Full.Mark.Cpp",
                      base::TimeDelta::FromMicroseconds(
                          event.total_cpp.mark_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES("V8.GC.Cycle.Full.Weak.Cpp",
                      base::TimeDelta::FromMicroseconds(
                          event.total_cpp.weak_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES("V8.GC.Cycle.Full.Compact.Cpp",
                      base::TimeDelta::FromMicroseconds(
                          event.total_cpp.compact_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES("V8.GC.Cycle.Full.Sweep.Cpp",
                      base::TimeDelta::FromMicroseconds(
                          event.total_cpp.sweep_wall_clock_duration_in_us));

  UMA_HISTOGRAM_TIMES(
      "V8.GC.Cycle.MainThread.Full.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_cpp.mark_wall_clock_duration_in_us +
          event.main_thread_cpp.weak_wall_clock_duration_in_us +
          event.main_thread_cpp.compact_wall_clock_duration_in_us +
          event.main_thread_cpp.sweep_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Cycle.MainThread.Full.Mark.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_cpp.mark_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Cycle.MainThread.Full.Weak.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_cpp.weak_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Cycle.MainThread.Full.Compact.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_cpp.compact_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Cycle.MainThread.Full.Sweep.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_cpp.sweep_wall_clock_duration_in_us));

  // Report latency metrics:
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Event.MainThread.Full.Atomic.Mark.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_atomic_cpp.mark_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Event.MainThread.Full.Atomic.Weak.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_atomic_cpp.weak_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Event.MainThread.Full.Atomic.Compact.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_atomic_cpp.compact_wall_clock_duration_in_us));
  UMA_HISTOGRAM_TIMES(
      "V8.GC.Event.MainThread.Full.Atomic.Sweep.Cpp",
      base::TimeDelta::FromMicroseconds(
          event.main_thread_atomic_cpp.sweep_wall_clock_duration_in_us));
  ReportAtomicLatencyEvent(
      event.main_thread_atomic_cpp.mark_wall_clock_duration_in_us);
  ReportAtomicLatencyEvent(
      event.main_thread_atomic_cpp.weak_wall_clock_duration_in_us);
  ReportAtomicLatencyEvent(
      event.main_thread_atomic_cpp.compact_wall_clock_duration_in_us);
  ReportAtomicLatencyEvent(
      event.main_thread_atomic_cpp.sweep_wall_clock_duration_in_us);

  // Report size metrics:
  static constexpr size_t kMinSize = 1;
  static constexpr size_t kMaxSize = 4 * 1024 * 1024;
  static constexpr size_t kNumBuckets = 50;

  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, object_size_before_histogram,
      ("V8.GC.Cycle.Objects.Before.Full.Cpp", kMinSize, kMaxSize, kNumBuckets));
  object_size_before_histogram.Count(
      CappedSizeInKB(event.objects_cpp.bytes_before));

  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, object_size_after_histogram,
      ("V8.GC.Cycle.Objects.After.Full.Cpp", kMinSize, kMaxSize, kNumBuckets));
  object_size_after_histogram.Count(
      CappedSizeInKB(event.objects_cpp.bytes_after));

  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, object_size_freed_histogram,
      ("V8.GC.Cycle.Objects.Freed.Full.Cpp", kMinSize, kMaxSize, kNumBuckets));
  object_size_freed_histogram.Count(
      CappedSizeInKB(event.objects_cpp.bytes_freed));

  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, memory_size_freed_histogram,
      ("V8.GC.Cycle.Memory.Freed.Full.Cpp", kMinSize, kMaxSize, kNumBuckets));
  memory_size_freed_histogram.Count(
      CappedSizeInKB(event.memory_cpp.bytes_freed));

  // Report efficacy metrics:
  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, efficacy_histogram,
      ("V8.GC.Cycle.Efficiency.Full.Cpp", kMinSize, kMaxSize, kNumBuckets));
  efficacy_histogram.Count(
      CappedEfficacyInKBPerMs(event.efficiency_cpp_in_bytes_per_us));

  DEFINE_STATIC_LOCAL(CustomCountHistogram, efficacy_main_thread_cpp_histogram,
                      ("V8.GC.Cycle.Efficiency.MainThread.Full.Cpp", kMinSize,
                       kMaxSize, kNumBuckets));
  efficacy_main_thread_cpp_histogram.Count(CappedEfficacyInKBPerMs(
      event.main_thread_efficiency_cpp_in_bytes_per_us));

  DEFINE_STATIC_LOCAL(CustomCountHistogram, collection_rate_histogram,
                      ("V8.GC.Cycle.CollectionRate.Full.Cpp", 0, 100, 20));
  collection_rate_histogram.Count(base::saturated_cast<base::Histogram::Sample>(
      100 * event.collection_rate_cpp_in_percent));
#endif  // USE_V8_OILPAN
}

namespace {

void ReportCppIncrementalLatencyEvent(int64_t duration_us) {
  UMA_HISTOGRAM_TIMES("V8.GC.Event.MainThread.Full.Incremental.Cpp",
                      base::TimeDelta::FromMicroseconds(duration_us));
}

}  // namespace

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::GarbageCollectionFullMainThreadIncrementalMark& event,
    ContextId context_id) {
  if (event.cpp_wall_clock_duration_in_us != -1) {
    // This is only a latency event.
    UMA_HISTOGRAM_TIMES(
        "V8.GC.Event.MainThread.Full.Incremental.Mark.Cpp",
        base::TimeDelta::FromMicroseconds(event.cpp_wall_clock_duration_in_us));
    ReportCppIncrementalLatencyEvent(event.cpp_wall_clock_duration_in_us);
  }
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::GarbageCollectionFullMainThreadBatchedIncrementalMark&
        batched_events,
    ContextId context_id) {
  for (auto event : batched_events.events) {
    AddMainThreadEvent(event, context_id);
  }
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::GarbageCollectionFullMainThreadIncrementalSweep& event,
    ContextId context_id) {
  if (event.cpp_wall_clock_duration_in_us != -1) {
    // This is only a latency event.
    UMA_HISTOGRAM_TIMES(
        "V8.GC.Event.MainThread.Full.Incremental.Sweep.Cpp",
        base::TimeDelta::FromMicroseconds(event.cpp_wall_clock_duration_in_us));
    ReportCppIncrementalLatencyEvent(event.cpp_wall_clock_duration_in_us);
  }
}

void V8MetricsRecorder::AddMainThreadEvent(
    const v8::metrics::GarbageCollectionFullMainThreadBatchedIncrementalSweep&
        batched_events,
    ContextId context_id) {
  for (auto event : batched_events.events) {
    AddMainThreadEvent(event, context_id);
  }
}

void V8MetricsRecorder::NotifyIsolateDisposal() {
  v8::metrics::Recorder::NotifyIsolateDisposal();
  isolate_ = nullptr;
}

Document* V8MetricsRecorder::GetDocument(
    v8::metrics::Recorder::ContextId context_id) {
  if (!isolate_)
    return nullptr;
  v8::HandleScope handle_scope(isolate_);
  v8::MaybeLocal<v8::Context> maybe_context =
      v8::metrics::Recorder::GetContext(isolate_, context_id);
  if (maybe_context.IsEmpty())
    return nullptr;
  return To<LocalDOMWindow>(
             ExecutionContext::From(maybe_context.ToLocalChecked()))
      ->document();
}

absl::optional<V8MetricsRecorder::UkmRecorderAndSourceId>
V8MetricsRecorder::GetUkmRecorderAndSourceId(
    v8::metrics::Recorder::ContextId context_id) {
  DCHECK(IsMainThread());
  if (!isolate_)
    return absl::optional<UkmRecorderAndSourceId>();
  Document* document = GetDocument(context_id);
  if (!document)
    return absl::optional<UkmRecorderAndSourceId>();
  ukm::UkmRecorder* ukm_recorder = document->UkmRecorder();
  if (!ukm_recorder)
    return absl::optional<UkmRecorderAndSourceId>();
  return absl::optional<UkmRecorderAndSourceId>(absl::in_place, ukm_recorder,
                                                document->UkmSourceID());
}

}  // namespace blink