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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
base / android / trace_event_binding.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/android/trace_event_binding.h"
#include <jni.h>
#include <set>
#include "base/android/jni_string.h"
#include "base/compiler_specific.h"
#include "base/metrics/histogram_macros.h"
#include "base/trace_event/base_tracing.h"
#include "base/tracing_buildflags.h"
#include "build/robolectric_buildflags.h"
#if BUILDFLAG(IS_ROBOLECTRIC)
#include "base/base_robolectric_jni/TraceEvent_jni.h" // nogncheck
#else
#include "base/tasks_jni/TraceEvent_jni.h"
#endif
#if BUILDFLAG(ENABLE_BASE_TRACING)
#include "base/trace_event/trace_event_impl.h" // no-presubmit-check
#include "third_party/perfetto/include/perfetto/tracing/track.h" // no-presubmit-check nogncheck
#include "third_party/perfetto/protos/perfetto/config/chrome/chrome_config.gen.h" // nogncheck
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
namespace base {
namespace android {
#if BUILDFLAG(ENABLE_BASE_TRACING)
namespace {
constexpr const char kAndroidViewHierarchyTraceCategory[] =
TRACE_DISABLED_BY_DEFAULT("android_view_hierarchy");
constexpr const char kAndroidViewHierarchyEventName[] = "AndroidView";
class TraceEnabledObserver : public perfetto::TrackEventSessionObserver {
public:
static TraceEnabledObserver* GetInstance() {
static base::NoDestructor<TraceEnabledObserver> instance;
return instance.get();
}
// perfetto::TrackEventSessionObserver implementation
void OnSetup(const perfetto::DataSourceBase::SetupArgs& args) override {
trace_event::TraceConfig trace_config(
args.config->chrome_config().trace_config());
event_name_filtering_per_session_[args.internal_instance_index] =
trace_config.IsEventPackageNameFilterEnabled();
}
void OnStart(const perfetto::DataSourceBase::StartArgs&) override {
JNIEnv* env = jni_zero::AttachCurrentThread();
base::android::Java_TraceEvent_setEnabled(env, true);
base::android::Java_TraceEvent_setEventNameFilteringEnabled(
env, EventNameFilteringEnabled());
}
void OnStop(const perfetto::DataSourceBase::StopArgs& args) override {
event_name_filtering_per_session_.erase(args.internal_instance_index);
JNIEnv* env = jni_zero::AttachCurrentThread();
base::android::Java_TraceEvent_setEnabled(
env, !event_name_filtering_per_session_.empty());
base::android::Java_TraceEvent_setEventNameFilteringEnabled(
env, EventNameFilteringEnabled());
}
private:
friend class base::NoDestructor<TraceEnabledObserver>;
TraceEnabledObserver() = default;
~TraceEnabledObserver() override = default;
// Return true if event name filtering is requested by at least one tracing
// session.
bool EventNameFilteringEnabled() const {
bool event_name_filtering_enabled = false;
for (const auto& entry : event_name_filtering_per_session_) {
if (entry.second) {
event_name_filtering_enabled = true;
}
}
return event_name_filtering_enabled;
}
std::unordered_map<uint32_t, bool> event_name_filtering_per_session_;
};
} // namespace
static void JNI_TraceEvent_RegisterEnabledObserver(JNIEnv* env) {
base::android::Java_TraceEvent_setEnabled(env, base::TrackEvent::IsEnabled());
base::TrackEvent::AddSessionObserver(TraceEnabledObserver::GetInstance());
}
static jboolean JNI_TraceEvent_ViewHierarchyDumpEnabled(JNIEnv* env) {
static const unsigned char* enabled =
TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
kAndroidViewHierarchyTraceCategory);
return *enabled;
}
static void JNI_TraceEvent_InitViewHierarchyDump(
JNIEnv* env,
jlong id,
const JavaParamRef<jobject>& obj) {
TRACE_EVENT(
kAndroidViewHierarchyTraceCategory, kAndroidViewHierarchyEventName,
perfetto::TerminatingFlow::ProcessScoped(static_cast<uint64_t>(id)),
[&](perfetto::EventContext ctx) {
auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
auto* dump = event->set_android_view_dump();
Java_TraceEvent_dumpViewHierarchy(env, reinterpret_cast<jlong>(dump),
obj);
});
}
static jlong JNI_TraceEvent_StartActivityDump(JNIEnv* env,
const JavaParamRef<jstring>& name,
jlong dump_proto_ptr) {
auto* dump = reinterpret_cast<perfetto::protos::pbzero::AndroidViewDump*>(
dump_proto_ptr);
auto* activity = dump->add_activity();
activity->set_name(ConvertJavaStringToUTF8(env, name));
return reinterpret_cast<jlong>(activity);
}
static void JNI_TraceEvent_AddViewDump(
JNIEnv* env,
jint id,
jint parent_id,
jboolean is_shown,
jboolean is_dirty,
const JavaParamRef<jstring>& class_name,
const JavaParamRef<jstring>& resource_name,
jlong activity_proto_ptr) {
auto* activity = reinterpret_cast<perfetto::protos::pbzero::AndroidActivity*>(
activity_proto_ptr);
auto* view = activity->add_view();
view->set_id(id);
view->set_parent_id(parent_id);
view->set_is_shown(is_shown);
view->set_is_dirty(is_dirty);
view->set_class_name(ConvertJavaStringToUTF8(env, class_name));
view->set_resource_name(ConvertJavaStringToUTF8(env, resource_name));
}
#else // BUILDFLAG(ENABLE_BASE_TRACING)
// Empty implementations when TraceLog isn't available.
static void JNI_TraceEvent_RegisterEnabledObserver(JNIEnv* env) {
base::android::Java_TraceEvent_setEnabled(env, false);
// This code should not be reached when base tracing is disabled. Calling
// setEventNameFilteringEnabled to avoid "unused function" warning.
base::android::Java_TraceEvent_setEventNameFilteringEnabled(env, false);
}
static jboolean JNI_TraceEvent_ViewHierarchyDumpEnabled(JNIEnv* env) {
return false;
}
static void JNI_TraceEvent_InitViewHierarchyDump(
JNIEnv* env,
jlong id,
const JavaParamRef<jobject>& obj) {
DCHECK(false);
// This code should not be reached when base tracing is disabled. Calling
// dumpViewHierarchy to avoid "unused function" warning.
Java_TraceEvent_dumpViewHierarchy(env, 0, obj);
}
static jlong JNI_TraceEvent_StartActivityDump(JNIEnv* env,
const JavaParamRef<jstring>& name,
jlong dump_proto_ptr) {
return 0;
}
static void JNI_TraceEvent_AddViewDump(
JNIEnv* env,
jint id,
jint parent_id,
jboolean is_shown,
jboolean is_dirty,
const JavaParamRef<jstring>& class_name,
const JavaParamRef<jstring>& resource_name,
jlong activity_proto_ptr) {}
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
namespace {
// Boilerplate for safely converting Java data to TRACE_EVENT data.
class TraceEventDataConverter {
public:
TraceEventDataConverter(JNIEnv* env, jstring jarg)
: has_arg_(jarg != nullptr),
arg_(jarg ? ConvertJavaStringToUTF8(env, jarg) : "") {}
TraceEventDataConverter(const TraceEventDataConverter&) = delete;
TraceEventDataConverter& operator=(const TraceEventDataConverter&) = delete;
~TraceEventDataConverter() = default;
// Return saved values to pass to TRACE_EVENT macros.
const char* arg_name() { return has_arg_ ? "arg" : nullptr; }
const std::string& arg() const LIFETIME_BOUND { return arg_; }
private:
bool has_arg_;
std::string arg_;
};
} // namespace
static void JNI_TraceEvent_Instant(JNIEnv* env,
const JavaParamRef<jstring>& jname,
const JavaParamRef<jstring>& jarg) {
TraceEventDataConverter converter(env, jarg);
if (converter.arg_name()) {
TRACE_EVENT_INSTANT(
internal::kJavaTraceCategory, nullptr, converter.arg_name(),
converter.arg(), [&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
} else {
TRACE_EVENT_INSTANT(
internal::kJavaTraceCategory, nullptr,
[&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
}
}
static void JNI_TraceEvent_InstantAndroidIPC(JNIEnv* env,
const JavaParamRef<jstring>& jname,
jlong jdur) {
TRACE_EVENT_INSTANT(
internal::kJavaTraceCategory, "AndroidIPC",
[&](perfetto::EventContext ctx) {
auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
auto* android_ipc = event->set_android_ipc();
android_ipc->set_name(ConvertJavaStringToUTF8(env, jname));
android_ipc->set_dur_ms(jdur);
});
}
#if BUILDFLAG(ENABLE_BASE_TRACING)
static void JNI_TraceEvent_InstantAndroidToolbar(JNIEnv* env,
jint block_reason,
jint allow_reason,
jint snapshot_diff) {
using AndroidToolbar = perfetto::protos::pbzero::AndroidToolbar;
TRACE_EVENT_INSTANT(
internal::kJavaTraceCategory, "AndroidToolbar",
[&](perfetto::EventContext ctx) {
auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
auto* android_toolbar = event->set_android_toolbar();
if (block_reason >= 0) {
android_toolbar->set_block_capture_reason(
static_cast<AndroidToolbar::BlockCaptureReason>(block_reason));
}
if (allow_reason >= 0) {
android_toolbar->set_allow_capture_reason(
static_cast<AndroidToolbar::AllowCaptureReason>(allow_reason));
}
if (snapshot_diff >= 0) {
android_toolbar->set_snapshot_difference(
static_cast<AndroidToolbar::SnapshotDifference>(snapshot_diff));
}
});
}
#else // BUILDFLAG(ENABLE_BASE_TRACING)
// Empty implementations when TraceLog isn't available.
static void JNI_TraceEvent_InstantAndroidToolbar(JNIEnv* env,
jint block_reason,
jint allow_reason,
jint snapshot_diff) {}
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
static void JNI_TraceEvent_WebViewStartupTotalFactoryInit(JNIEnv* env,
jlong start_time_ms,
jlong duration_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
auto t = perfetto::Track::ThreadScoped(
reinterpret_cast<void*>(trace_event::GetNextGlobalTraceId()));
TRACE_EVENT_BEGIN("android_webview.timeline",
"WebView.Startup.CreationTime.TotalFactoryInitTime", t,
TimeTicks() + Milliseconds(start_time_ms));
TRACE_EVENT_END("android_webview.timeline", t,
TimeTicks() + Milliseconds(start_time_ms + duration_ms));
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_WebViewStartupStage1(JNIEnv* env,
jlong start_time_ms,
jlong duration_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
auto t = perfetto::Track::ThreadScoped(
reinterpret_cast<void*>(trace_event::GetNextGlobalTraceId()));
TRACE_EVENT_BEGIN("android_webview.timeline",
"WebView.Startup.CreationTime.Stage1.FactoryInit", t,
TimeTicks() + Milliseconds(start_time_ms));
TRACE_EVENT_END("android_webview.timeline", t,
TimeTicks() + Milliseconds(start_time_ms + duration_ms));
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_WebViewStartupStage2(JNIEnv* env,
jlong start_time_ms,
jlong duration_ms,
jboolean is_cold_startup) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
auto t = perfetto::Track::ThreadScoped(
reinterpret_cast<void*>(trace_event::GetNextGlobalTraceId()));
if (is_cold_startup) {
TRACE_EVENT_BEGIN("android_webview.timeline",
"WebView.Startup.CreationTime.Stage2.ProviderInit.Cold",
t, TimeTicks() + Milliseconds(start_time_ms));
} else {
TRACE_EVENT_BEGIN("android_webview.timeline",
"WebView.Startup.CreationTime.Stage2.ProviderInit.Warm",
t, TimeTicks() + Milliseconds(start_time_ms));
}
TRACE_EVENT_END("android_webview.timeline", t,
TimeTicks() + Milliseconds(start_time_ms + duration_ms));
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_WebViewStartupStartChromiumLocked(
JNIEnv* env,
jlong start_time_ms,
jlong duration_ms,
jint call_site,
jboolean from_ui_thread) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
auto t = perfetto::Track::ThreadScoped(
reinterpret_cast<void*>(trace_event::GetNextGlobalTraceId()));
TRACE_EVENT_BEGIN(
"android_webview.timeline",
"WebView.Startup.CreationTime.StartChromiumLocked", t,
TimeTicks() + Milliseconds(start_time_ms),
[&](perfetto::EventContext ctx) {
auto* webview_startup =
ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
->set_webview_startup();
webview_startup->set_from_ui_thread((bool)from_ui_thread);
webview_startup->set_call_site(
(perfetto::protos::pbzero::perfetto_pbzero_enum_WebViewStartup::
CallSite)call_site);
});
TRACE_EVENT_END("android_webview.timeline", t,
TimeTicks() + Milliseconds(start_time_ms + duration_ms));
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_StartupActivityStart(JNIEnv* env,
jlong activity_id,
jlong start_time_ms) {
TRACE_EVENT_INSTANT(
"interactions", "Startup.ActivityStart",
TimeTicks() + Milliseconds(start_time_ms),
[&](perfetto::EventContext ctx) {
auto* start_up = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
->set_startup();
start_up->set_activity_id(activity_id);
});
}
static void JNI_TraceEvent_StartupLaunchCause(JNIEnv* env,
jlong activity_id,
jlong start_time_ms,
jint cause) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
using Startup = perfetto::protos::pbzero::StartUp;
auto launchType = Startup::OTHER;
switch (cause) {
case Startup::CUSTOM_TAB:
launchType = Startup::CUSTOM_TAB;
break;
case Startup::TWA:
launchType = Startup::TWA;
break;
case Startup::RECENTS:
launchType = Startup::RECENTS;
break;
case Startup::RECENTS_OR_BACK:
launchType = Startup::RECENTS_OR_BACK;
break;
case Startup::FOREGROUND_WHEN_LOCKED:
launchType = Startup::FOREGROUND_WHEN_LOCKED;
break;
case Startup::MAIN_LAUNCHER_ICON:
launchType = Startup::MAIN_LAUNCHER_ICON;
break;
case Startup::MAIN_LAUNCHER_ICON_SHORTCUT:
launchType = Startup::MAIN_LAUNCHER_ICON_SHORTCUT;
break;
case Startup::HOME_SCREEN_WIDGET:
launchType = Startup::HOME_SCREEN_WIDGET;
break;
case Startup::OPEN_IN_BROWSER_FROM_MENU:
launchType = Startup::OPEN_IN_BROWSER_FROM_MENU;
break;
case Startup::EXTERNAL_SEARCH_ACTION_INTENT:
launchType = Startup::EXTERNAL_SEARCH_ACTION_INTENT;
break;
case Startup::NOTIFICATION:
launchType = Startup::NOTIFICATION;
break;
case Startup::EXTERNAL_VIEW_INTENT:
launchType = Startup::EXTERNAL_VIEW_INTENT;
break;
case Startup::OTHER_CHROME:
launchType = Startup::OTHER_CHROME;
break;
case Startup::WEBAPK_CHROME_DISTRIBUTOR:
launchType = Startup::WEBAPK_CHROME_DISTRIBUTOR;
break;
case Startup::WEBAPK_OTHER_DISTRIBUTOR:
launchType = Startup::WEBAPK_OTHER_DISTRIBUTOR;
break;
case Startup::HOME_SCREEN_SHORTCUT:
launchType = Startup::HOME_SCREEN_SHORTCUT;
break;
case Startup::SHARE_INTENT:
launchType = Startup::SHARE_INTENT;
break;
case Startup::NFC:
launchType = Startup::NFC;
break;
default:
break;
}
TRACE_EVENT_INSTANT(
"interactions,startup", "Startup.LaunchCause",
TimeTicks() + Milliseconds(start_time_ms),
[&](perfetto::EventContext ctx) {
auto* start_up = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
->set_startup();
start_up->set_activity_id(activity_id);
start_up->set_launch_cause(launchType);
});
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_StartupTimeToFirstVisibleContent2(
JNIEnv* env,
jlong activity_id,
jlong start_time_ms,
jlong duration_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
[[maybe_unused]] const perfetto::Track track(
base::trace_event::GetNextGlobalTraceId(),
perfetto::ProcessTrack::Current());
TRACE_EVENT_BEGIN(
"interactions,startup", "Startup.TimeToFirstVisibleContent2", track,
TimeTicks() + Milliseconds(start_time_ms),
[&](perfetto::EventContext ctx) {
auto* start_up = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>()
->set_startup();
start_up->set_activity_id(activity_id);
});
TRACE_EVENT_END("interactions,startup", track,
TimeTicks() + Milliseconds(start_time_ms + duration_ms));
#endif // BUILDFLAG(ENABLE_BASE_TRACING)
}
static void JNI_TraceEvent_Begin(JNIEnv* env,
const JavaParamRef<jstring>& jname,
const JavaParamRef<jstring>& jarg) {
TraceEventDataConverter converter(env, jarg);
if (converter.arg_name()) {
TRACE_EVENT_BEGIN(
internal::kJavaTraceCategory, nullptr, converter.arg_name(),
converter.arg(), [&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
} else {
TRACE_EVENT_BEGIN(
internal::kJavaTraceCategory, nullptr,
[&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
}
}
static void JNI_TraceEvent_BeginWithIntArg(JNIEnv* env,
const JavaParamRef<jstring>& jname,
jint jarg) {
TRACE_EVENT_BEGIN(
internal::kJavaTraceCategory, nullptr, "arg", jarg,
[&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
}
static void JNI_TraceEvent_End(JNIEnv* env,
const JavaParamRef<jstring>& jarg,
jlong jflow) {
TraceEventDataConverter converter(env, jarg);
bool has_arg = converter.arg_name();
bool has_flow = jflow != 0;
if (has_arg && has_flow) {
TRACE_EVENT_END(internal::kJavaTraceCategory,
perfetto::Flow::ProcessScoped(static_cast<uint64_t>(jflow)),
converter.arg_name(), converter.arg());
} else if (has_arg) {
TRACE_EVENT_END(internal::kJavaTraceCategory, converter.arg_name(),
converter.arg());
} else if (has_flow) {
TRACE_EVENT_END(
internal::kJavaTraceCategory,
perfetto::Flow::ProcessScoped(static_cast<uint64_t>(jflow)));
} else {
TRACE_EVENT_END(internal::kJavaTraceCategory);
}
}
static void JNI_TraceEvent_BeginToplevel(JNIEnv* env,
const JavaParamRef<jstring>& jtarget) {
TRACE_EVENT_BEGIN(
internal::kToplevelTraceCategory, nullptr,
[&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jtarget));
});
}
static void JNI_TraceEvent_EndToplevel(JNIEnv* env) {
TRACE_EVENT_END(internal::kToplevelTraceCategory);
}
static void JNI_TraceEvent_StartAsync(JNIEnv* env,
const JavaParamRef<jstring>& jname,
jlong jid) {
TRACE_EVENT_BEGIN(
internal::kJavaTraceCategory, nullptr,
perfetto::Track(static_cast<uint64_t>(jid)),
[&](::perfetto::EventContext& ctx) {
ctx.event()->set_name(ConvertJavaStringToUTF8(env, jname));
});
}
static void JNI_TraceEvent_FinishAsync(JNIEnv* env,
jlong jid) {
TRACE_EVENT_END(internal::kJavaTraceCategory,
perfetto::Track(static_cast<uint64_t>(jid)));
}
} // namespace android
} // namespace base