v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
code-data-source.h
Go to the documentation of this file.
1// Copyright 2024 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_TRACING_CODE_DATA_SOURCE_H_
6#define V8_TRACING_CODE_DATA_SOURCE_H_
7
8#include <cstdint>
9#include <string>
10#include <unordered_map>
11
12#include "perfetto/protozero/scattered_heap_buffer.h"
13#include "perfetto/tracing/data_source.h"
14#include "protos/perfetto/config/chrome/v8_config.gen.h"
15#include "protos/perfetto/trace/interned_data/interned_data.pbzero.h"
16#include "src/base/hashing.h"
17#include "src/handles/handles.h"
19#include "src/objects/tagged.h"
21
22namespace v8 {
23namespace internal {
24
25class CodeDataSourceIncrementalState;
26class Isolate;
27class Script;
28class SharedFunctionInfo;
29
30#if V8_ENABLE_WEBASSEMBLY
31namespace wasm {
32class NativeModule;
33}
34#endif // V8_ENABLE_WEBASSEMBLY
35
36struct CodeDataSourceTraits : public perfetto::DefaultDataSourceTraits {
38 using TlsStateType = void;
39};
40
42 : public perfetto::DataSource<CodeDataSource, CodeDataSourceTraits> {
43 public:
44 static void Register();
45
46 void OnSetup(const SetupArgs&) override;
47 void OnStart(const StartArgs&) override;
48 void OnStop(const StopArgs&) override;
49
50 const perfetto::protos::gen::V8Config& config() const { return config_; }
51
52 private:
53 using Base = DataSource<CodeDataSource, CodeDataSourceTraits>;
54
55 perfetto::protos::gen::V8Config config_;
56};
57
59 public:
61 void Init(const CodeDataSource::TraceContext& context);
62
64 return !serialized_interned_data_.empty();
65 }
66
68 CodeDataSource::TraceContext::TracePacketHandle& packet);
69
70 uint64_t InternIsolate(Isolate& isolate);
71 uint64_t InternJsScript(Isolate& isolate, Tagged<Script> script);
72 uint64_t InternJsFunction(Isolate& isolate,
74 uint64_t v8_js_script_iid, int line_num,
75 int column_num);
76#if V8_ENABLE_WEBASSEMBLY
77 uint64_t InternWasmScript(Isolate& isolate, int script_id,
78 const std::string& url,
79 wasm::NativeModule* native_module);
80
81#endif // V8_ENABLE_WEBASSEMBLY
82
83 bool is_initialized() const { return initialized_; }
84 bool log_script_sources() const { return log_script_sources_; }
85 bool log_instructions() const { return log_instructions_; }
86
87 private:
88 using JsFunctionNameIid = uint64_t;
89 struct Function {
93
94 bool operator==(const Function& other) const {
95 return v8_js_script_iid == other.v8_js_script_iid &&
96 is_toplevel == other.is_toplevel &&
97 start_position == other.start_position;
98 }
99
100 bool operator!=(const Function& other) const { return !(*this == other); }
101
102 struct Hash {
103 size_t operator()(const Function& f) const {
106 }
107 };
108 };
109
113 bool operator==(const ScriptUniqueId& other) const {
114 return isolate_id == other.isolate_id && script_id == other.script_id;
115 }
116
117 bool operator!=(const ScriptUniqueId& other) const {
118 return !(*this == other);
119 }
120
121 struct Hash {
122 size_t operator()(const ScriptUniqueId& id) const {
124 }
125 };
126 };
127
128 uint64_t InternJsFunctionName(Tagged<String> function_name);
129
130 uint64_t next_isolate_iid() const { return isolates_.size() + 1; }
131
132 uint64_t next_script_iid() const { return scripts_.size() + 1; }
133
134 uint64_t next_function_iid() const { return functions_.size() + 1; }
135
136 uint64_t next_js_function_name_iid() const {
137 return js_function_names_.size() + 1;
138 }
139
140 // Stores newly seen interned data while in the middle of writing a new
141 // TracePacket. Interned data is serialized into this buffer and then flushed
142 // to the actual trace stream when the packet ends.
143 // This data is cached as part of the incremental state to reuse the
144 // underlying buffer allocation.
145 protozero::HeapBuffered<perfetto::protos::pbzero::InternedData>
147
148 std::unordered_map<int, uint64_t> isolates_;
149 std::unordered_map<ScriptUniqueId, uint64_t, ScriptUniqueId::Hash> scripts_;
150 std::unordered_map<Function, uint64_t, Function::Hash> functions_;
151 std::unordered_map<PerfettoV8String, uint64_t, PerfettoV8String::Hasher>
153 std::unordered_map<std::string, uint64_t> two_byte_function_names_;
154
156 bool log_instructions_ = false;
157 bool initialized_ = false;
158};
159
160} // namespace internal
161} // namespace v8
162
165
166#endif // V8_TRACING_CODE_DATA_SOURCE_H_
static constexpr size_t Combine(const T &... ts)
Definition hashing.h:144
void Init(const CodeDataSource::TraceContext &context)
uint64_t InternJsFunctionName(Tagged< String > function_name)
uint64_t InternJsScript(Isolate &isolate, Tagged< Script > script)
void FlushInternedData(CodeDataSource::TraceContext::TracePacketHandle &packet)
std::unordered_map< std::string, uint64_t > two_byte_function_names_
std::unordered_map< ScriptUniqueId, uint64_t, ScriptUniqueId::Hash > scripts_
std::unordered_map< Function, uint64_t, Function::Hash > functions_
uint64_t InternJsFunction(Isolate &isolate, DirectHandle< SharedFunctionInfo > info, uint64_t v8_js_script_iid, int line_num, int column_num)
std::unordered_map< int, uint64_t > isolates_
protozero::HeapBuffered< perfetto::protos::pbzero::InternedData > serialized_interned_data_
std::unordered_map< PerfettoV8String, uint64_t, PerfettoV8String::Hasher > js_function_names_
perfetto::protos::gen::V8Config config_
DataSource< CodeDataSource, CodeDataSourceTraits > Base
void OnSetup(const SetupArgs &) override
void OnStart(const StartArgs &) override
void OnStop(const StopArgs &) override
const perfetto::protos::gen::V8Config & config() const
PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(v8::internal::CodeDataSource, v8::internal::CodeDataSourceTraits)
Definition c-api.cc:87