Skip to content

Commit 9dbab9b

Browse files
skyostilCommit Bot
authored andcommitted
[tracing] Migrate tracing to Perfetto track events
This patch replaces V8's tracing implementation (i.e., the TRACE_EVENT macros) with the track event base implementation from Perfetto. The advantages of doing this are: 1) This allows us to remove most tracing-related backend code from V8. 2) V8 can start writing strongly typed trace event arguments, which are more compact, easier to process and more extensible than legacy JSON-based trace arguments. For the time being, we still support the old trace macros when V8 is embedded into Chrome and other embedders. Design doc: https://docs.google.com/document/d/1f7tt4cb-JcA5bQFR1oXk60ncJPpkL02_Hi_Bc6MfTQk/edit#heading=h.398p6b4eaen2 Bug: chromium:1006766 Change-Id: Ie71474fbe065821772b13d851487ebbca680c4ae Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1947688 Commit-Queue: Sami Kyöstilä <[email protected]> Auto-Submit: Sami Kyöstilä <[email protected]> Reviewed-by: Peter Marshall <[email protected]> Reviewed-by: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#67217}
1 parent 8c8ff95 commit 9dbab9b

32 files changed

+761
-738
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
!/third_party/antlr4
6363
!/third_party/binutils
6464
!/third_party/inspector_protocol
65+
!/third_party/jsoncpp
6566
!/third_party/colorama
6667
/third_party/colorama/src
6768
!/third_party/googletest

BUILD.gn

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ v8_toolset_for_shell = "host"
297297
config("internal_config_base") {
298298
visibility = [ ":*" ] # Only targets in this file can depend on this.
299299

300+
configs = [ ":v8_tracing_config" ]
301+
300302
include_dirs = [
301303
".",
302304
"$target_gen_dir",
@@ -311,7 +313,6 @@ config("internal_config") {
311313
"//build/config/compiler:wexit_time_destructors",
312314
":internal_config_base",
313315
":v8_header_features",
314-
":v8_tracing_config",
315316
]
316317

317318
if (is_component_build) {
@@ -1282,6 +1283,7 @@ v8_source_set("torque_generated_initializers") {
12821283
deps = [
12831284
":generate_bytecode_builtins_list",
12841285
":run_torque",
1286+
":v8_tracing",
12851287
]
12861288

12871289
public_deps = [ ":v8_maybe_icu" ]
@@ -1310,6 +1312,7 @@ v8_source_set("torque_generated_definitions") {
13101312
deps = [
13111313
":generate_bytecode_builtins_list",
13121314
":run_torque",
1315+
":v8_tracing",
13131316
]
13141317

13151318
public_deps = [ ":v8_maybe_icu" ]
@@ -2054,6 +2057,18 @@ group("v8_compiler_for_mksnapshot") {
20542057
}
20552058
}
20562059

2060+
# Any target using trace events must directly or indirectly depend on
2061+
# v8_tracing.
2062+
group("v8_tracing") {
2063+
if (v8_use_perfetto) {
2064+
if (build_with_chromium) {
2065+
public_deps = [ "//third_party/perfetto:libperfetto" ]
2066+
} else {
2067+
public_deps = [ ":v8_libperfetto" ]
2068+
}
2069+
}
2070+
}
2071+
20572072
v8_source_set("v8_base_without_compiler") {
20582073
visibility = [ ":*" ] # Only targets in this file can depend on this.
20592074

@@ -3439,6 +3454,7 @@ v8_source_set("v8_base_without_compiler") {
34393454
":v8_libbase",
34403455
":v8_libsampler",
34413456
":v8_shared_internal_headers",
3457+
":v8_tracing",
34423458
":v8_version",
34433459
"src/inspector:inspector",
34443460
]
@@ -3530,6 +3546,14 @@ v8_source_set("v8_base_without_compiler") {
35303546
]
35313547
deps += [ "src/third_party/vtune:v8_vtune_trace_mark" ]
35323548
}
3549+
3550+
if (v8_use_perfetto) {
3551+
sources -= [ "//base/trace_event/common/trace_event_common.h" ]
3552+
sources += [
3553+
"src/tracing/trace-categories.cc",
3554+
"src/tracing/trace-categories.h",
3555+
]
3556+
}
35333557
}
35343558

35353559
group("v8_base") {
@@ -3892,20 +3916,25 @@ v8_component("v8_libplatform") {
38923916
deps = [
38933917
":v8_headers",
38943918
":v8_libbase",
3919+
":v8_tracing",
38953920
]
3921+
38963922
if (v8_use_perfetto) {
3923+
sources -= [
3924+
"//base/trace_event/common/trace_event_common.h",
3925+
"src/libplatform/tracing/trace-buffer.cc",
3926+
"src/libplatform/tracing/trace-buffer.h",
3927+
"src/libplatform/tracing/trace-object.cc",
3928+
"src/libplatform/tracing/trace-writer.cc",
3929+
"src/libplatform/tracing/trace-writer.h",
3930+
]
38973931
sources += [
3898-
"src/libplatform/tracing/json-trace-event-listener.cc",
3899-
"src/libplatform/tracing/json-trace-event-listener.h",
39003932
"src/libplatform/tracing/trace-event-listener.cc",
39013933
"src/libplatform/tracing/trace-event-listener.h",
39023934
]
39033935
deps += [
3936+
# TODO(skyostil): Switch TraceEventListener to protozero.
39043937
"//third_party/perfetto/protos/perfetto/trace:lite",
3905-
"//third_party/perfetto/protos/perfetto/trace/chrome:minimal_complete_lite",
3906-
"//third_party/perfetto/protos/perfetto/trace/chrome:zero",
3907-
"//third_party/perfetto/src/tracing:client_api",
3908-
"//third_party/perfetto/src/tracing:platform_posix",
39093938
]
39103939
}
39113940
}
@@ -3933,9 +3962,8 @@ v8_source_set("fuzzer_support") {
39333962

39343963
configs = [ ":internal_config_base" ]
39353964

3936-
deps = [ ":v8" ]
3937-
39383965
public_deps = [
3966+
":v8",
39393967
":v8_libbase",
39403968
":v8_libplatform",
39413969
":v8_maybe_icu",
@@ -4443,6 +4471,7 @@ v8_executable("d8") {
44434471
":v8",
44444472
":v8_libbase",
44454473
":v8_libplatform",
4474+
":v8_tracing",
44464475
"//build/win:default_exe_manifest",
44474476
]
44484477

@@ -4461,10 +4490,6 @@ v8_executable("d8") {
44614490
if (v8_enable_vtunejit) {
44624491
deps += [ "src/third_party/vtune:v8_vtune" ]
44634492
}
4464-
4465-
if (v8_use_perfetto) {
4466-
deps += [ "//third_party/perfetto/src/tracing:in_process_backend" ]
4467-
}
44684493
}
44694494

44704495
v8_executable("v8_hello_world") {
@@ -4620,6 +4645,7 @@ v8_source_set("wasm_module_runner") {
46204645
deps = [
46214646
":generate_bytecode_builtins_list",
46224647
":run_torque",
4648+
":v8_tracing",
46234649
]
46244650

46254651
public_deps = [ ":v8_maybe_icu" ]
@@ -4696,6 +4722,7 @@ v8_source_set("lib_wasm_fuzzer_common") {
46964722
deps = [
46974723
":generate_bytecode_builtins_list",
46984724
":run_torque",
4725+
":v8_tracing",
46994726
]
47004727

47014728
public_deps = [ ":v8_maybe_icu" ]
@@ -4778,7 +4805,7 @@ if (!build_with_chromium && v8_use_perfetto) {
47784805
"-Wno-tautological-constant-compare",
47794806
]
47804807
}
4781-
if (is_win) {
4808+
if (is_win && is_clang) {
47824809
cflags += [ "-Wno-microsoft-unqualified-friend" ]
47834810
}
47844811
}
@@ -4937,4 +4964,21 @@ if (!build_with_chromium && v8_use_perfetto) {
49374964
configs += [ "//build/config/compiler:no_chromium_code" ]
49384965
}
49394966
} # host_toolchain
4967+
4968+
v8_component("v8_libperfetto") {
4969+
configs = [ ":v8_tracing_config" ]
4970+
public_configs = [ "//third_party/perfetto/gn:public_config" ]
4971+
deps = [
4972+
"//third_party/perfetto/src/trace_processor:export_json",
4973+
"//third_party/perfetto/src/trace_processor:storage_minimal",
4974+
"//third_party/perfetto/src/tracing:client_api",
4975+
"//third_party/perfetto/src/tracing/core",
4976+
4977+
# TODO(skyostil): Support non-POSIX platforms.
4978+
"//third_party/perfetto/protos/perfetto/config:cpp",
4979+
"//third_party/perfetto/protos/perfetto/trace/track_event:zero",
4980+
"//third_party/perfetto/src/tracing:in_process_backend",
4981+
"//third_party/perfetto/src/tracing:platform_posix",
4982+
]
4983+
}
49404984
} # if (!build_with_chromium && v8_use_perfetto)

DEPS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ deps = {
266266
'dep_type': 'cipd',
267267
},
268268
'v8/third_party/perfetto':
269-
Var('android_url') + '/platform/external/perfetto.git' + '@' + 'b9b24d1b0b80aafec393af085067e9eae829412f',
269+
Var('android_url') + '/platform/external/perfetto.git' + '@' + '9bd480acdcad450c5ed5d2aa329eb603c53e5f40',
270270
'v8/third_party/protobuf':
271271
Var('chromium_url') + '/external/github.com/google/protobuf'+ '@' + 'b68a347f56137b4b1a746e8c7438495a6ac1bd91',
272272
'v8/third_party/zlib':
273273
Var('chromium_url') + '/chromium/src/third_party/zlib.git'+ '@' + '156be8c52f80cde343088b4a69a80579101b6e67',
274+
'v8/third_party/jsoncpp/source':
275+
Var('chromium_url') + '/external/github.com/open-source-parsers/jsoncpp.git'+ '@' + '645250b6690785be60ab6780ce4b58698d884d11',
274276
'v8/third_party/ittapi': {
275277
# Force checkout ittapi libraries to pass v8 header includes check on
276278
# bots that has check_v8_header_includes enabled.

build_overrides/build.gni

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ perfetto_build_with_embedder = true
1616
perfetto_protobuf_target_prefix = "//"
1717
perfetto_protobuf_gni = "//gni/proto_library.gni"
1818

19+
# We use Perfetto's Trace Processor to convert traces to the legacy JSON
20+
# format.
21+
enable_perfetto_trace_processor = true
22+
1923
# Uncomment these to specify a different NDK location and version in
2024
# non-Chromium builds.
2125
# default_android_ndk_root = "//third_party/android_ndk"

gni/v8.gni

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ declare_args() {
5454
# Expose symbols for dynamic linking.
5555
v8_expose_symbols = false
5656

57-
# Use Perfetto (https://perfetto.dev) as the default TracingController. Not
58-
# currently implemented.
57+
# Implement tracing using Perfetto (https://perfetto.dev).
5958
v8_use_perfetto = false
6059

6160
# Override global symbol level setting for v8

include/libplatform/v8-tracing.h

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "v8-platform.h" // NOLINT(build/include)
1616

1717
namespace perfetto {
18+
namespace trace_processor {
19+
class TraceProcessorStorage;
20+
}
1821
class TracingSession;
1922
}
2023

@@ -28,7 +31,6 @@ namespace platform {
2831
namespace tracing {
2932

3033
class TraceEventListener;
31-
class JSONTraceEventListener;
3234

3335
const int kTraceMaxNumArgs = 2;
3436

@@ -197,6 +199,9 @@ class V8_PLATFORM_EXPORT TraceConfig {
197199

198200
TraceConfig() : enable_systrace_(false), enable_argument_filter_(false) {}
199201
TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
202+
const StringList& GetEnabledCategories() const {
203+
return included_categories_;
204+
}
200205
bool IsSystraceEnabled() const { return enable_systrace_; }
201206
bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
202207

@@ -229,6 +234,17 @@ class V8_PLATFORM_EXPORT TraceConfig {
229234
class V8_PLATFORM_EXPORT TracingController
230235
: public V8_PLATFORM_NON_EXPORTED_BASE(v8::TracingController) {
231236
public:
237+
TracingController();
238+
~TracingController() override;
239+
240+
#if defined(V8_USE_PERFETTO)
241+
// Must be called before StartTracing() if V8_USE_PERFETTO is true. Provides
242+
// the output stream for the JSON trace data.
243+
void InitializeForPerfetto(std::ostream* output_stream);
244+
// Provide an optional listener for testing that will receive trace events.
245+
// Must be called before StartTracing().
246+
void SetTraceEventListenerForTesting(TraceEventListener* listener);
247+
#else // defined(V8_USE_PERFETTO)
232248
// The pointer returned from GetCategoryGroupEnabled() points to a value with
233249
// zero or more of the following bits. Used in this class only. The
234250
// TRACE_EVENT macros should only use the value as a bool. These values must
@@ -242,19 +258,8 @@ class V8_PLATFORM_EXPORT TracingController
242258
ENABLED_FOR_ETW_EXPORT = 1 << 3
243259
};
244260

245-
TracingController();
246-
~TracingController() override;
247-
248261
// Takes ownership of |trace_buffer|.
249262
void Initialize(TraceBuffer* trace_buffer);
250-
#ifdef V8_USE_PERFETTO
251-
// Must be called before StartTracing() if V8_USE_PERFETTO is true. Provides
252-
// the output stream for the JSON trace data.
253-
void InitializeForPerfetto(std::ostream* output_stream);
254-
// Provide an optional listener for testing that will receive trace events.
255-
// Must be called before StartTracing().
256-
void SetTraceEventListenerForTesting(TraceEventListener* listener);
257-
#endif
258263

259264
// v8::TracingController implementation.
260265
const uint8_t* GetCategoryGroupEnabled(const char* category_group) override;
@@ -274,6 +279,10 @@ class V8_PLATFORM_EXPORT TracingController
274279
unsigned int flags, int64_t timestamp) override;
275280
void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
276281
const char* name, uint64_t handle) override;
282+
283+
static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
284+
#endif // !defined(V8_USE_PERFETTO)
285+
277286
void AddTraceStateObserver(
278287
v8::TracingController::TraceStateObserver* observer) override;
279288
void RemoveTraceStateObserver(
@@ -282,27 +291,32 @@ class V8_PLATFORM_EXPORT TracingController
282291
void StartTracing(TraceConfig* trace_config);
283292
void StopTracing();
284293

285-
static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
286-
287294
protected:
295+
#if !defined(V8_USE_PERFETTO)
288296
virtual int64_t CurrentTimestampMicroseconds();
289297
virtual int64_t CurrentCpuTimestampMicroseconds();
298+
#endif // !defined(V8_USE_PERFETTO)
290299

291300
private:
301+
#if !defined(V8_USE_PERFETTO)
292302
void UpdateCategoryGroupEnabledFlag(size_t category_index);
293303
void UpdateCategoryGroupEnabledFlags();
304+
#endif // !defined(V8_USE_PERFETTO)
294305

295-
std::unique_ptr<TraceBuffer> trace_buffer_;
296-
std::unique_ptr<TraceConfig> trace_config_;
297306
std::unique_ptr<base::Mutex> mutex_;
298-
std::unordered_set<v8::TracingController::TraceStateObserver*> observers_;
307+
std::unique_ptr<TraceConfig> trace_config_;
299308
std::atomic_bool recording_{false};
300-
#ifdef V8_USE_PERFETTO
309+
std::unordered_set<v8::TracingController::TraceStateObserver*> observers_;
310+
311+
#if defined(V8_USE_PERFETTO)
301312
std::ostream* output_stream_ = nullptr;
302-
std::unique_ptr<JSONTraceEventListener> json_listener_;
313+
std::unique_ptr<perfetto::trace_processor::TraceProcessorStorage>
314+
trace_processor_;
303315
TraceEventListener* listener_for_testing_ = nullptr;
304316
std::unique_ptr<perfetto::TracingSession> tracing_session_;
305-
#endif
317+
#else // !defined(V8_USE_PERFETTO)
318+
std::unique_ptr<TraceBuffer> trace_buffer_;
319+
#endif // !defined(V8_USE_PERFETTO)
306320

307321
// Disallow copy and assign
308322
TracingController(const TracingController&) = delete;

include/v8-platform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ class TracingController {
138138
public:
139139
virtual ~TracingController() = default;
140140

141+
// In Perfetto mode, trace events are written using Perfetto's Track Event
142+
// API directly without going through the embedder. However, it is still
143+
// possible to observe tracing being enabled and disabled.
144+
#if !defined(V8_USE_PERFETTO)
141145
/**
142146
* Called by TRACE_EVENT* macros, don't call this directly.
143147
* The name parameter is a category group for example:
@@ -183,6 +187,7 @@ class TracingController {
183187
**/
184188
virtual void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
185189
const char* name, uint64_t handle) {}
190+
#endif // !defined(V8_USE_PERFETTO)
186191

187192
class TraceStateObserver {
188193
public:

src/DEPS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ specific_include_rules = {
5555
"+include/libplatform/v8-tracing.h",
5656
"+perfetto/tracing.h"
5757
],
58+
"builtins-trace\.cc": [
59+
"+protos/perfetto",
60+
],
5861
}

0 commit comments

Comments
 (0)