Commit 787bd978 authored by Jakob Gruber's avatar Jakob Gruber Committed by Commit Bot

[snapshot] Simplify counter handling in mksnapshot

In mksnapshot, we only need to ensure we generate counter code in
(embedded) builtins, if needed. The counter function does not need to do
anything useful as long as it returns unique pointers for each counter,
and we don't need to dump counters.

Tbr: petermarshall@chromium.org
Change-Id: I94a53ef5193b89365948d0395e1908e6d3c6e396
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1549159Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60694}
parent 84853ad1
......@@ -245,10 +245,32 @@ void WriteEmbeddedFile(i::EmbeddedFileWriter* writer) {
i::EmbeddedData embedded_blob = i::EmbeddedData::FromBlob();
writer->WriteEmbedded(&embedded_blob);
}
} // namespace
typedef std::map<std::string, int> CounterMap;
CounterMap* counter_map_;
CounterMap* counter_map_ = nullptr;
void MaybeSetCounterFunction(v8::Isolate* isolate) {
// If --native-code-counters is on then we enable all counters to make
// sure we generate code to increment them from the snapshot.
//
// Note: For the sake of the mksnapshot, the counter function must only
// return distinct addresses for each counter s.t. the serializer can properly
// distinguish between them. In theory it should be okay to just return an
// incremented int value each time this function is called, but we play it
// safe and return a real distinct memory location tied to every counter name.
if (i::FLAG_native_code_counters) {
counter_map_ = new CounterMap();
isolate->SetCounterFunction([](const char* name) -> int* {
auto map_entry = counter_map_->find(name);
if (map_entry == counter_map_->end()) {
counter_map_->emplace(name, 0);
}
return &counter_map_->at(name);
});
}
}
} // namespace
int main(int argc, char** argv) {
v8::base::EnsureConsoleOutput();
......@@ -290,21 +312,11 @@ int main(int argc, char** argv) {
i::DisableEmbeddedBlobRefcounting();
v8::StartupData blob;
counter_map_ = new CounterMap();
{
v8::Isolate* isolate = v8::Isolate::Allocate();
// If --native-code-counters is on then we enable all counters to make
// sure we generate code to increment them from the snapshot.
if (i::FLAG_native_code_counters || i::FLAG_dump_counters ||
i::FLAG_dump_counters_nvp) {
isolate->SetCounterFunction([](const char* name) -> int* {
auto map_entry = counter_map_->find(name);
if (map_entry == counter_map_->end()) {
counter_map_->emplace(name, 0);
}
return &counter_map_->at(name);
});
}
MaybeSetCounterFunction(isolate);
if (i::FLAG_embedded_builtins) {
// Set code range such that relative jumps for builtins to
// builtin calls in the snapshot are possible.
......@@ -326,37 +338,6 @@ int main(int argc, char** argv) {
blob = CreateSnapshotDataBlob(&snapshot_creator, embed_script.get());
}
if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp) {
if (i::FLAG_dump_counters_nvp) {
// Dump counters as name-value pairs.
for (auto entry : *counter_map_) {
std::string key = entry.first;
int counter = entry.second;
std::cout << "\"" << key << "\"=" << counter << "\n";
}
} else {
// Dump counters in formatted boxes.
constexpr int kNameBoxSize = 64;
constexpr int kValueBoxSize = 13;
std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
<< std::string(kValueBoxSize, '-') << "+\n";
std::cout << "| Name" << std::string(kNameBoxSize - 5, ' ') << "| Value"
<< std::string(kValueBoxSize - 6, ' ') << "|\n";
std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
<< std::string(kValueBoxSize, '-') << "+\n";
for (auto entry : *counter_map_) {
std::string key = entry.first;
int counter = entry.second;
std::cout << "| " << std::setw(kNameBoxSize - 2) << std::left << key
<< " | " << std::setw(kValueBoxSize - 2) << std::right
<< counter << " |\n";
}
std::cout << "+" << std::string(kNameBoxSize, '-') << "+"
<< std::string(kValueBoxSize, '-') << "+\n";
}
}
delete counter_map_;
if (warmup_script) {
CHECK(blob.raw_size > 0 && blob.data != nullptr);
v8::StartupData cold = blob;
......@@ -365,6 +346,8 @@ int main(int argc, char** argv) {
delete[] cold.data;
}
delete counter_map_;
CHECK(blob.data);
snapshot_writer.WriteSnapshot(blob);
delete[] blob.data;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment