v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
basic-block-profiler.cc
Go to the documentation of this file.
1// Copyright 2014 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
6
7#include <algorithm>
8#include <numeric>
9#include <sstream>
10
13#include "src/heap/heap-inl.h"
15
16namespace v8 {
17namespace internal {
18
21
23 : block_ids_(n_blocks), counts_(n_blocks, 0) {}
24
25void BasicBlockProfilerData::SetCode(const std::ostringstream& os) {
26 code_ = os.str();
27}
28
29void BasicBlockProfilerData::SetFunctionName(std::unique_ptr<char[]> name) {
30 function_name_ = name.get();
31}
32
33void BasicBlockProfilerData::SetSchedule(const std::ostringstream& os) {
34 schedule_ = os.str();
35}
36
39 block_ids_[offset] = id;
40}
41
42void BasicBlockProfilerData::SetHash(int hash) { hash_ = hash; }
43
45 for (size_t i = 0; i < n_blocks(); ++i) {
46 counts_[i] = 0;
47 }
48}
49
50void BasicBlockProfilerData::AddBranch(int32_t true_block_id,
51 int32_t false_block_id) {
52 branches_.emplace_back(true_block_id, false_block_id);
53}
54
57 auto data = std::make_unique<BasicBlockProfilerData>(n_blocks);
58 BasicBlockProfilerData* data_ptr = data.get();
59 data_list_.push_back(std::move(data));
60 return data_ptr;
61}
62
63namespace {
64DirectHandle<String> CopyStringToJSHeap(const std::string& source,
65 Isolate* isolate) {
66 return isolate->factory()->NewStringFromAsciiChecked(source.c_str(),
68}
69
70constexpr int kBlockIdSlotSize = kInt32Size;
71constexpr int kBlockCountSlotSize = kInt32Size;
72} // namespace
73
79
84
87 function_name_ = js_heap_data->name()->ToCString().get();
88 schedule_ = js_heap_data->schedule()->ToCString().get();
89 code_ = js_heap_data->code()->ToCString().get();
91 Cast<FixedUInt32Array>(js_heap_data->counts());
92 for (int i = 0; i < counts->length() / kBlockCountSlotSize; ++i) {
93 counts_.push_back(counts->get(i));
94 }
95 Tagged<FixedInt32Array> block_ids(js_heap_data->block_ids());
96 for (int i = 0; i < block_ids->length() / kBlockIdSlotSize; ++i) {
97 block_ids_.push_back(block_ids->get(i));
98 }
100 js_heap_data->branches();
101 for (int i = 0; i < branches->length(); ++i) {
102 branches_.push_back(branches->get(i));
103 }
104 CHECK_EQ(block_ids_.size(), counts_.size());
105 hash_ = js_heap_data->hash();
106}
107
109 Isolate* isolate) {
110 int id_array_size_in_bytes = static_cast<int>(n_blocks() * kBlockIdSlotSize);
111 CHECK(id_array_size_in_bytes >= 0 &&
112 static_cast<size_t>(id_array_size_in_bytes) / kBlockIdSlotSize ==
113 n_blocks()); // Overflow
115 isolate, id_array_size_in_bytes, AllocationType::kOld);
116 for (int i = 0; i < static_cast<int>(n_blocks()); ++i) {
117 block_ids->set(i, block_ids_[i]);
118 }
119
120 int counts_array_size_in_bytes =
121 static_cast<int>(n_blocks() * kBlockCountSlotSize);
122 CHECK(counts_array_size_in_bytes >= 0 &&
123 static_cast<size_t>(counts_array_size_in_bytes) / kBlockCountSlotSize ==
124 n_blocks()); // Overflow
126 isolate, counts_array_size_in_bytes, AllocationType::kOld);
127 for (int i = 0; i < static_cast<int>(n_blocks()); ++i) {
128 counts->set(i, counts_[i]);
129 }
130
133 isolate, static_cast<int>(branches_.size()), AllocationType::kOld);
134 for (int i = 0; i < static_cast<int>(branches_.size()); ++i) {
135 branches->set(i, branches_[i]);
136 }
137 DirectHandle<String> name = CopyStringToJSHeap(function_name_, isolate);
138 DirectHandle<String> schedule = CopyStringToJSHeap(schedule_, isolate);
139 DirectHandle<String> code = CopyStringToJSHeap(code_, isolate);
140
141 return isolate->factory()->NewOnHeapBasicBlockProfilerData(
142 block_ids, counts, branches, name, schedule, code, hash_,
144}
145
147 for (const auto& data : data_list_) {
148 data->ResetCounts();
149 }
150 HandleScope scope(isolate);
151 DirectHandle<ArrayList> list(isolate->heap()->basic_block_profiling_data(),
152 isolate);
153 for (int i = 0; i < list->length(); ++i) {
155 Cast<OnHeapBasicBlockProfilerData>(list->get(i))->counts(), isolate);
156 for (int j = 0; j < counts->length() / kBlockCountSlotSize; ++j) {
157 counts->set(j, 0);
158 }
159 }
160}
161
163 return !data_list_.empty() ||
164 isolate->heap()->basic_block_profiling_data()->length() > 0;
165}
166
167void BasicBlockProfiler::Print(Isolate* isolate, std::ostream& os) {
168 os << "---- Start Profiling Data ----" << '\n';
169 for (const auto& data : data_list_) {
170 os << *data;
171 }
172 HandleScope scope(isolate);
173 DirectHandle<ArrayList> list(isolate->heap()->basic_block_profiling_data(),
174 isolate);
175 std::unordered_set<std::string> builtin_names;
176 for (int i = 0; i < list->length(); ++i) {
179 isolate),
180 isolate);
181 os << data;
182 // Ensure that all builtin names are unique; otherwise profile-guided
183 // optimization might get confused.
184 CHECK(builtin_names.insert(data.function_name_).second);
185 }
186 os << "---- End Profiling Data ----" << '\n';
187}
188
189void BasicBlockProfiler::Log(Isolate* isolate, std::ostream& os) {
190 HandleScope scope(isolate);
191 DirectHandle<ArrayList> list(isolate->heap()->basic_block_profiling_data(),
192 isolate);
193 std::unordered_set<std::string> builtin_names;
194 for (int i = 0; i < list->length(); ++i) {
197 isolate),
198 isolate);
199 data.Log(isolate, os);
200 // Ensure that all builtin names are unique; otherwise profile-guided
201 // optimization might get confused.
202 CHECK(builtin_names.insert(data.function_name_).second);
203 }
204}
205
208 Tagged<ArrayList> list(isolate->heap()->basic_block_profiling_data());
209 std::vector<bool> out;
210 int list_length = list->length();
211 for (int i = 0; i < list_length; ++i) {
214 for (size_t j = 0; j < data.n_blocks(); ++j) {
215 out.push_back(data.counts_[j] > 0);
216 }
217 }
218 return out;
219}
220
221void BasicBlockProfilerData::Log(Isolate* isolate, std::ostream& os) {
222 bool any_nonzero_counter = false;
223 constexpr char kNext[] = "\t";
224 for (size_t i = 0; i < n_blocks(); ++i) {
225 if (counts_[i] > 0) {
226 any_nonzero_counter = true;
228 << function_name_.c_str() << kNext << block_ids_[i] << kNext
229 << counts_[i] << '\n';
230 }
231 }
232 if (any_nonzero_counter) {
233 for (size_t i = 0; i < branches_.size(); ++i) {
235 << function_name_.c_str() << kNext << branches_[i].first << kNext
236 << branches_[i].second << '\n';
237 }
239 << function_name_.c_str() << kNext << hash_ << '\n';
240 }
241}
242
243std::ostream& operator<<(std::ostream& os, const BasicBlockProfilerData& d) {
244 if (std::all_of(d.counts_.cbegin(), d.counts_.cend(),
245 [](uint32_t count) { return count == 0; })) {
246 // No data was collected for this function.
247 return os;
248 }
249 const char* name = "unknown function";
250 if (!d.function_name_.empty()) {
251 name = d.function_name_.c_str();
252 }
253 if (!d.schedule_.empty()) {
254 os << "schedule for " << name << " (B0 entered " << d.counts_[0]
255 << " times)" << '\n';
256 os << d.schedule_.c_str() << '\n';
257 }
258 os << "block counts for " << name << ":" << '\n';
259 std::vector<std::pair<size_t, uint32_t>> pairs;
260 pairs.reserve(d.n_blocks());
261 for (size_t i = 0; i < d.n_blocks(); ++i) {
262 pairs.push_back(std::make_pair(i, d.counts_[i]));
263 }
264 std::sort(
265 pairs.begin(), pairs.end(),
266 [=](std::pair<size_t, uint32_t> left, std::pair<size_t, uint32_t> right) {
267 if (right.second == left.second) return left.first < right.first;
268 return right.second < left.second;
269 });
270 for (auto it : pairs) {
271 if (it.second == 0) break;
272 os << "block B" << it.first << " : " << it.second << '\n';
273 }
274 os << '\n';
275 if (!d.code_.empty()) {
276 os << d.code_.c_str() << '\n';
277 }
278 return os;
279}
280
281BuiltinsCallGraph::BuiltinsCallGraph() : all_hash_matched_(true) {}
282
284 int32_t block_id) {
285 if (builtin_call_map_.count(caller) == 0) {
286 builtin_call_map_.emplace(caller, BuiltinCallees());
287 }
288 BuiltinCallees& callees = builtin_call_map_.at(caller);
289 if (callees.count(block_id) == 0) {
290 callees.emplace(block_id, BlockCallees());
291 }
292 BlockCallees& block_callees = callees.at(block_id);
293 if (block_callees.count(callee) == 0) {
294 block_callees.emplace(callee);
295 }
296}
297
299 if (builtin_call_map_.count(builtin) == 0) return nullptr;
300 return &builtin_call_map_.at(builtin);
301}
302
303} // namespace internal
304} // namespace v8
Schedule * schedule
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
void SetFunctionName(std::unique_ptr< char[]> name)
DirectHandle< OnHeapBasicBlockProfilerData > CopyToJSHeap(Isolate *isolate)
void SetCode(const std::ostringstream &os)
void SetBlockId(size_t offset, int32_t id)
std::vector< std::pair< int32_t, int32_t > > branches_
void CopyFromJSHeap(Tagged< OnHeapBasicBlockProfilerData > js_heap_data)
void SetSchedule(const std::ostringstream &os)
void AddBranch(int32_t true_block_id, int32_t false_block_id)
void Log(Isolate *isolate, std::ostream &os)
V8_EXPORT_PRIVATE bool HasData(Isolate *isolate)
static V8_EXPORT_PRIVATE BasicBlockProfiler * Get()
BasicBlockProfilerData * NewData(size_t n_blocks)
V8_EXPORT_PRIVATE void Log(Isolate *isolate, std::ostream &os)
V8_EXPORT_PRIVATE std::vector< bool > GetCoverageBitmap(Isolate *isolate)
V8_EXPORT_PRIVATE void ResetCounts(Isolate *isolate)
V8_EXPORT_PRIVATE void Print(Isolate *isolate, std::ostream &os)
const BuiltinCallees * GetBuiltinCallees(Builtin builtin)
void AddBuiltinCall(Builtin caller, Builtin callee, int32_t block_id)
static BuiltinsCallGraph * Get()
static Handle< FixedIntegerArrayBase< T, Base > > New(Isolate *isolate, int length, MoreArgs &&... more_args)
int32_t offset
std::vector< PatternMap > pairs
#define DEFINE_LAZY_LEAKY_OBJECT_GETTER(T, FunctionName,...)
V8_INLINE DirectHandle< T > direct_handle(Tagged< T > object, Isolate *isolate)
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
constexpr int kInt32Size
Definition globals.h:401
std::set< Builtin > BlockCallees
std::unordered_map< int32_t, BlockCallees > BuiltinCallees
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define CHECK(condition)
Definition logging.h:124
#define CHECK_EQ(lhs, rhs)
#define DCHECK(condition)
Definition logging.h:482