Commit 7e6d6285 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

[heap-profiler] Simplify snapshotting of the roots.

This replaces three passes over the roots with a single pass.
This also removes root synchronization logic.

The GC subroot index is computed from the |root| parameter of the visit
method. The new |description| parameter is used as an edge name.

Bug: chromium:811842
Change-Id: I03a9215d56b54b3eb5f7bc8b32d5b22ad091c68b
Reviewed-on: https://chromium-review.googlesource.com/916781Reviewed-by: 's avatarAlexei Filippov <alph@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#51350}
parent ba37a5f6
......@@ -86,8 +86,9 @@ Builtins::Name Builtins::GetBuiltinFromBailoutId(BailoutId id) {
void Builtins::TearDown() { initialized_ = false; }
void Builtins::IterateBuiltins(RootVisitor* v) {
v->VisitRootPointers(Root::kBuiltins, nullptr, &builtins_[0],
&builtins_[0] + builtin_count);
for (int i = 0; i < builtin_count; i++) {
v->VisitRootPointer(Root::kBuiltins, name(i), &builtins_[i]);
}
}
const char* Builtins::Lookup(byte* pc) {
......
......@@ -907,6 +907,13 @@ void GlobalHandles::IterateStrongRoots(RootVisitor* v) {
}
}
void GlobalHandles::IterateWeakRoots(RootVisitor* v) {
for (NodeIterator it(this); !it.done(); it.Advance()) {
if (it.node()->IsWeak()) {
v->VisitRootPointer(Root::kGlobalHandles, nullptr, it.node()->location());
}
}
}
DISABLE_CFI_PERF
void GlobalHandles::IterateAllRoots(RootVisitor* v) {
......
......@@ -108,10 +108,10 @@ class GlobalHandles {
int PostGarbageCollectionProcessing(
GarbageCollector collector, const v8::GCCallbackFlags gc_callback_flags);
// Iterates over all strong handles.
void IterateStrongRoots(RootVisitor* v);
// Iterates over all handles.
void IterateWeakRoots(RootVisitor* v);
void IterateAllRoots(RootVisitor* v);
void IterateAllNewSpaceRoots(RootVisitor* v);
......
......@@ -462,30 +462,6 @@ bool Heap::IsRetainingPathTarget(HeapObject* object,
return false;
}
namespace {
const char* RootToString(Root root) {
switch (root) {
#define ROOT_CASE(root_id, ignore, description) \
case Root::root_id: \
return description;
ROOT_ID_LIST(ROOT_CASE)
#undef ROOT_CASE
case Root::kCodeFlusher:
return "(Code flusher)";
case Root::kPartialSnapshotCache:
return "(Partial snapshot cache)";
case Root::kWeakCollections:
return "(Weak collections)";
case Root::kWrapperTracing:
return "(Wrapper tracing)";
case Root::kUnknown:
return "(Unknown)";
}
UNREACHABLE();
return nullptr;
}
} // namespace
void Heap::PrintRetainingPath(HeapObject* target, RetainingPathOption option) {
PrintF("\n\n\n");
PrintF("#################################################\n");
......@@ -528,7 +504,7 @@ void Heap::PrintRetainingPath(HeapObject* target, RetainingPathOption option) {
}
PrintF("\n");
PrintF("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
PrintF("Root: %s\n", RootToString(root));
PrintF("Root: %s\n", RootVisitor::RootName(root));
PrintF("-------------------------------------------------\n");
}
......@@ -5146,6 +5122,9 @@ void Heap::IterateStrongRoots(RootVisitor* v, VisitMode mode) {
}
}
void Heap::IterateWeakGlobalHandles(RootVisitor* v) {
isolate_->global_handles()->IterateWeakRoots(v);
}
// TODO(1236194): Since the heap size is configurable on the command line
// and through the API, we should gracefully handle the case that the heap
......
......@@ -1167,15 +1167,15 @@ class Heap {
// Iterators. ================================================================
// ===========================================================================
// Iterates over all roots in the heap.
void IterateRoots(RootVisitor* v, VisitMode mode);
// Iterates over all strong roots in the heap.
void IterateStrongRoots(RootVisitor* v, VisitMode mode);
// Iterates over entries in the smi roots list. Only interesting to the
// serializer/deserializer, since GC does not care about smis.
void IterateSmiRoots(RootVisitor* v);
// Iterates over all the other roots in the heap.
// Iterates over weak string tables.
void IterateWeakRoots(RootVisitor* v, VisitMode mode);
// Iterates over weak global handles.
void IterateWeakGlobalHandles(RootVisitor* v);
// ===========================================================================
// Store buffer API. =========================================================
......
......@@ -176,7 +176,7 @@ HeapSnapshot::HeapSnapshot(HeapProfiler* profiler)
((kPointerSize == 8) && (sizeof(HeapGraphEdge) == 24)));
STATIC_ASSERT(((kPointerSize == 4) && (sizeof(HeapEntry) == 28)) ||
((kPointerSize == 8) && (sizeof(HeapEntry) == 40)));
for (int i = 0; i < VisitorSynchronization::kNumberOfSyncTags; ++i) {
for (int i = 0; i < static_cast<int>(Root::kNumberOfRoots); ++i) {
gc_subroot_indexes_[i] = HeapEntry::kNoEntry;
}
}
......@@ -196,8 +196,8 @@ void HeapSnapshot::AddSyntheticRootEntries() {
AddRootEntry();
AddGcRootsEntry();
SnapshotObjectId id = HeapObjectsMap::kGcRootsFirstSubrootId;
for (int tag = 0; tag < VisitorSynchronization::kNumberOfSyncTags; tag++) {
AddGcSubrootEntry(tag, id);
for (int root = 0; root < static_cast<int>(Root::kNumberOfRoots); root++) {
AddGcSubrootEntry(static_cast<Root>(root), id);
id += HeapObjectsMap::kObjectIdStep;
}
DCHECK_EQ(HeapObjectsMap::kFirstAvailableObjectId, id);
......@@ -229,13 +229,11 @@ HeapEntry* HeapSnapshot::AddGcRootsEntry() {
return entry;
}
HeapEntry* HeapSnapshot::AddGcSubrootEntry(int tag, SnapshotObjectId id) {
DCHECK_EQ(gc_subroot_indexes_[tag], HeapEntry::kNoEntry);
DCHECK(0 <= tag && tag < VisitorSynchronization::kNumberOfSyncTags);
HeapEntry* entry = AddEntry(HeapEntry::kSynthetic,
VisitorSynchronization::kTagNames[tag], id, 0, 0);
gc_subroot_indexes_[tag] = entry->index();
HeapEntry* HeapSnapshot::AddGcSubrootEntry(Root root, SnapshotObjectId id) {
DCHECK_EQ(gc_subroot_indexes_[static_cast<int>(root)], HeapEntry::kNoEntry);
HeapEntry* entry =
AddEntry(HeapEntry::kSynthetic, RootVisitor::RootName(root), id, 0, 0);
gc_subroot_indexes_[static_cast<int>(root)] = entry->index();
return entry;
}
......@@ -306,7 +304,7 @@ const SnapshotObjectId HeapObjectsMap::kGcRootsFirstSubrootId =
HeapObjectsMap::kGcRootsObjectId + HeapObjectsMap::kObjectIdStep;
const SnapshotObjectId HeapObjectsMap::kFirstAvailableObjectId =
HeapObjectsMap::kGcRootsFirstSubrootId +
VisitorSynchronization::kNumberOfSyncTags * HeapObjectsMap::kObjectIdStep;
static_cast<int>(Root::kNumberOfRoots) * HeapObjectsMap::kObjectIdStep;
HeapObjectsMap::HeapObjectsMap(Heap* heap)
: next_id_(kFirstAvailableObjectId), heap_(heap) {
......@@ -732,15 +730,15 @@ class SnapshotFiller {
HeapEntry* parent_entry = &snapshot_->entries()[parent];
parent_entry->SetNamedReference(type, reference_name, child_entry);
}
void SetNamedAutoIndexReference(HeapGraphEdge::Type type,
int parent,
void SetNamedAutoIndexReference(HeapGraphEdge::Type type, int parent,
const char* description,
HeapEntry* child_entry) {
HeapEntry* parent_entry = &snapshot_->entries()[parent];
int index = parent_entry->children_count() + 1;
parent_entry->SetNamedReference(
type,
names_->GetName(index),
child_entry);
const char* name = description
? names_->GetFormatted("%d / %s", index, description)
: names_->GetName(index);
parent_entry->SetNamedReference(type, name, child_entry);
}
private:
......@@ -1480,74 +1478,30 @@ HeapEntry* V8HeapExplorer::GetEntry(Object* obj) {
}
class RootsReferencesExtractor : public RootVisitor {
private:
struct IndexTag {
IndexTag(size_t index, VisitorSynchronization::SyncTag tag)
: index(index), tag(tag) {}
size_t index;
VisitorSynchronization::SyncTag tag;
};
public:
explicit RootsReferencesExtractor(Heap* heap)
: collecting_all_references_(false),
previous_reference_count_(0),
heap_(heap) {
}
explicit RootsReferencesExtractor(V8HeapExplorer* explorer)
: explorer_(explorer), visiting_weak_roots_(false) {}
void VisitRootPointers(Root root, const char* description, Object** start,
Object** end) override {
if (collecting_all_references_) {
for (Object** p = start; p < end; p++) all_references_.push_back(*p);
} else {
for (Object** p = start; p < end; p++) strong_references_.push_back(*p);
}
}
void SetVisitingWeakRoots() { visiting_weak_roots_ = true; }
void SetCollectingAllReferences() { collecting_all_references_ = true; }
void FillReferences(V8HeapExplorer* explorer) {
DCHECK_LE(strong_references_.size(), all_references_.size());
Builtins* builtins = heap_->isolate()->builtins();
USE(builtins);
size_t strong_index = 0, all_index = 0, tags_index = 0;
int builtin_index = 0;
while (all_index < all_references_.size()) {
bool is_strong =
strong_index < strong_references_.size() &&
strong_references_[strong_index] == all_references_[all_index];
explorer->SetGcSubrootReference(reference_tags_[tags_index].tag,
!is_strong,
all_references_[all_index]);
if (reference_tags_[tags_index].tag ==
VisitorSynchronization::kBuiltins) {
DCHECK(all_references_[all_index]->IsCode());
explorer->TagBuiltinCodeObject(
Code::cast(all_references_[all_index]),
builtins->name(builtin_index++));
}
++all_index;
if (is_strong) ++strong_index;
if (reference_tags_[tags_index].index == all_index) ++tags_index;
void VisitRootPointer(Root root, const char* description,
Object** object) override {
if (root == Root::kBuiltins) {
explorer_->TagBuiltinCodeObject(Code::cast(*object), description);
}
CHECK_EQ(strong_index, strong_references_.size());
explorer_->SetGcSubrootReference(root, description, visiting_weak_roots_,
*object);
}
void Synchronize(VisitorSynchronization::SyncTag tag) override {
if (collecting_all_references_ &&
previous_reference_count_ != all_references_.size()) {
previous_reference_count_ = all_references_.size();
reference_tags_.emplace_back(previous_reference_count_, tag);
}
void VisitRootPointers(Root root, const char* description, Object** start,
Object** end) override {
for (Object** p = start; p < end; p++)
VisitRootPointer(root, description, p);
}
private:
bool collecting_all_references_;
std::vector<Object*> strong_references_;
std::vector<Object*> all_references_;
size_t previous_reference_count_;
std::vector<IndexTag> reference_tags_;
Heap* heap_;
V8HeapExplorer* explorer_;
bool visiting_weak_roots_;
};
......@@ -1557,18 +1511,17 @@ bool V8HeapExplorer::IterateAndExtractReferences(
// Create references to the synthetic roots.
SetRootGcRootsReference();
for (int tag = 0; tag < VisitorSynchronization::kNumberOfSyncTags; tag++) {
SetGcRootsReference(static_cast<VisitorSynchronization::SyncTag>(tag));
for (int root = 0; root < static_cast<int>(Root::kNumberOfRoots); root++) {
SetGcRootsReference(static_cast<Root>(root));
}
// Make sure builtin code objects get their builtin tags
// first. Otherwise a particular JSFunction object could set
// its custom name to a generic builtin.
RootsReferencesExtractor extractor(heap_);
RootsReferencesExtractor extractor(this);
heap_->IterateRoots(&extractor, VISIT_ONLY_STRONG);
extractor.SetCollectingAllReferences();
heap_->IterateRoots(&extractor, VISIT_ALL);
extractor.FillReferences(this);
extractor.SetVisitingWeakRoots();
heap_->IterateWeakGlobalHandles(&extractor);
// We have to do two passes as sometimes FixedArrays are used
// to weakly hold their items, and it's impossible to distinguish
......@@ -1829,45 +1782,37 @@ void V8HeapExplorer::SetRootGcRootsReference() {
void V8HeapExplorer::SetUserGlobalReference(Object* child_obj) {
HeapEntry* child_entry = GetEntry(child_obj);
DCHECK_NOT_NULL(child_entry);
filler_->SetNamedAutoIndexReference(
HeapGraphEdge::kShortcut,
snapshot_->root()->index(),
child_entry);
filler_->SetNamedAutoIndexReference(HeapGraphEdge::kShortcut,
snapshot_->root()->index(), nullptr,
child_entry);
}
void V8HeapExplorer::SetGcRootsReference(VisitorSynchronization::SyncTag tag) {
filler_->SetIndexedAutoIndexReference(
HeapGraphEdge::kElement,
snapshot_->gc_roots()->index(),
snapshot_->gc_subroot(tag));
void V8HeapExplorer::SetGcRootsReference(Root root) {
filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement,
snapshot_->gc_roots()->index(),
snapshot_->gc_subroot(root));
}
void V8HeapExplorer::SetGcSubrootReference(
VisitorSynchronization::SyncTag tag, bool is_weak, Object* child_obj) {
void V8HeapExplorer::SetGcSubrootReference(Root root, const char* description,
bool is_weak, Object* child_obj) {
HeapEntry* child_entry = GetEntry(child_obj);
if (child_entry == nullptr) return;
const char* name = GetStrongGcSubrootName(child_obj);
HeapGraphEdge::Type edge_type =
is_weak ? HeapGraphEdge::kWeak : HeapGraphEdge::kInternal;
if (name != nullptr) {
DCHECK(!is_weak);
filler_->SetNamedReference(HeapGraphEdge::kInternal,
snapshot_->gc_subroot(tag)->index(), name,
child_entry);
filler_->SetNamedReference(edge_type, snapshot_->gc_subroot(root)->index(),
name, child_entry);
} else {
if (is_weak) {
filler_->SetNamedAutoIndexReference(HeapGraphEdge::kWeak,
snapshot_->gc_subroot(tag)->index(),
child_entry);
} else {
filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement,
snapshot_->gc_subroot(tag)->index(),
child_entry);
}
filler_->SetNamedAutoIndexReference(edge_type,
snapshot_->gc_subroot(root)->index(),
description, child_entry);
}
// Add a shortcut to JS global object reference at snapshot root.
// That allows the user to easily find global objects. They are
// also used as starting points in distance calculations.
if (is_weak || !child_obj->IsNativeContext()) return;
if (!child_obj->IsNativeContext()) return;
JSGlobalObject* global = Context::cast(child_obj)->global_object();
if (!global->IsJSGlobalObject()) return;
......@@ -2325,9 +2270,7 @@ void NativeObjectsExplorer::SetNativeRootReference(
// potentially-stale pointer.
child_entry = filler_->FindEntry(info);
filler_->SetNamedAutoIndexReference(
HeapGraphEdge::kInternal,
group_entry->index(),
child_entry);
HeapGraphEdge::kInternal, group_entry->index(), nullptr, child_entry);
}
......
......@@ -165,8 +165,8 @@ class HeapSnapshot {
HeapProfiler* profiler() { return profiler_; }
HeapEntry* root() { return &entries_[root_index_]; }
HeapEntry* gc_roots() { return &entries_[gc_roots_index_]; }
HeapEntry* gc_subroot(int index) {
return &entries_[gc_subroot_indexes_[index]];
HeapEntry* gc_subroot(Root root) {
return &entries_[gc_subroot_indexes_[static_cast<int>(root)]];
}
std::vector<HeapEntry>& entries() { return entries_; }
std::deque<HeapGraphEdge>& edges() { return edges_; }
......@@ -191,12 +191,12 @@ class HeapSnapshot {
private:
HeapEntry* AddRootEntry();
HeapEntry* AddGcRootsEntry();
HeapEntry* AddGcSubrootEntry(int tag, SnapshotObjectId id);
HeapEntry* AddGcSubrootEntry(Root root, SnapshotObjectId id);
HeapProfiler* profiler_;
int root_index_;
int gc_roots_index_;
int gc_subroot_indexes_[VisitorSynchronization::kNumberOfSyncTags];
int gc_subroot_indexes_[static_cast<int>(Root::kNumberOfRoots)];
std::vector<HeapEntry> entries_;
std::deque<HeapGraphEdge> edges_;
std::deque<HeapGraphEdge*> children_;
......@@ -445,9 +445,9 @@ class V8HeapExplorer : public HeapEntriesAllocator {
void SetUserGlobalReference(Object* user_global);
void SetRootGcRootsReference();
void SetGcRootsReference(VisitorSynchronization::SyncTag tag);
void SetGcSubrootReference(
VisitorSynchronization::SyncTag tag, bool is_weak, Object* child);
void SetGcRootsReference(Root root);
void SetGcSubrootReference(Root root, const char* description, bool is_weak,
Object* child);
const char* GetStrongGcSubrootName(Object* object);
void TagObject(Object* obj, const char* tag);
void TagFixedArraySubType(const FixedArray* array,
......
......@@ -9,16 +9,19 @@
namespace v8 {
namespace internal {
#define DECLARE_TAG(ignore1, name, ignore2) name,
const char* const
VisitorSynchronization::kTags[VisitorSynchronization::kNumberOfSyncTags] = {
ROOT_ID_LIST(DECLARE_TAG)};
#undef DECLARE_TAG
#define DECLARE_TAG(ignore1, ignore2, name) name,
const char* const VisitorSynchronization::kTagNames
[VisitorSynchronization::kNumberOfSyncTags] = {ROOT_ID_LIST(DECLARE_TAG)};
#undef DECLARE_TAG
const char* RootVisitor::RootName(Root root) {
switch (root) {
#define ROOT_CASE(root_id, description) \
case Root::root_id: \
return description;
ROOT_ID_LIST(ROOT_CASE)
#undef ROOT_CASE
case Root::kNumberOfRoots:
break;
}
UNREACHABLE();
return nullptr;
}
} // namespace internal
} // namespace v8
......@@ -13,45 +13,42 @@ namespace internal {
class CodeDataContainer;
class Object;
#define ROOT_ID_LIST(V) \
V(kStringTable, "string_table", "(Internalized strings)") \
V(kExternalStringsTable, "external_strings_table", "(External strings)") \
V(kStrongRootList, "strong_root_list", "(Strong roots)") \
V(kSmiRootList, "smi_root_list", "(Smi roots)") \
V(kBootstrapper, "bootstrapper", "(Bootstrapper)") \
V(kTop, "top", "(Isolate)") \
V(kRelocatable, "relocatable", "(Relocatable)") \
V(kDebug, "debug", "(Debugger)") \
V(kCompilationCache, "compilationcache", "(Compilation cache)") \
V(kHandleScope, "handlescope", "(Handle scope)") \
V(kDispatchTable, "dispatchtable", "(Dispatch table)") \
V(kBuiltins, "builtins", "(Builtins)") \
V(kGlobalHandles, "globalhandles", "(Global handles)") \
V(kEternalHandles, "eternalhandles", "(Eternal handles)") \
V(kThreadManager, "threadmanager", "(Thread manager)") \
V(kStrongRoots, "strong roots", "(Strong roots)") \
V(kExtensions, "Extensions", "(Extensions)")
#define ROOT_ID_LIST(V) \
V(kStringTable, "(Internalized strings)") \
V(kExternalStringsTable, "(External strings)") \
V(kStrongRootList, "(Strong roots)") \
V(kSmiRootList, "(Smi roots)") \
V(kBootstrapper, "(Bootstrapper)") \
V(kTop, "(Isolate)") \
V(kRelocatable, "(Relocatable)") \
V(kDebug, "(Debugger)") \
V(kCompilationCache, "(Compilation cache)") \
V(kHandleScope, "(Handle scope)") \
V(kDispatchTable, "(Dispatch table)") \
V(kBuiltins, "(Builtins)") \
V(kGlobalHandles, "(Global handles)") \
V(kEternalHandles, "(Eternal handles)") \
V(kThreadManager, "(Thread manager)") \
V(kStrongRoots, "(Strong roots)") \
V(kExtensions, "(Extensions)") \
V(kCodeFlusher, "(Code flusher)") \
V(kPartialSnapshotCache, "(Partial snapshot cache)") \
V(kWeakCollections, "(Weak collections)") \
V(kWrapperTracing, "(Wrapper tracing)") \
V(kUnknown, "(Unknown)")
class VisitorSynchronization : public AllStatic {
public:
#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
#define DECLARE_ENUM(enum_item, ignore) enum_item,
enum SyncTag { ROOT_ID_LIST(DECLARE_ENUM) kNumberOfSyncTags };
#undef DECLARE_ENUM
static const char* const kTags[kNumberOfSyncTags];
static const char* const kTagNames[kNumberOfSyncTags];
};
enum class Root {
#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
#define DECLARE_ENUM(enum_item, ignore) enum_item,
ROOT_ID_LIST(DECLARE_ENUM)
#undef DECLARE_ENUM
// TODO(ulan): Merge with the ROOT_ID_LIST.
kCodeFlusher,
kPartialSnapshotCache,
kWeakCollections,
kWrapperTracing,
kUnknown
kNumberOfRoots
};
// Abstract base class for visiting, and optionally modifying, the
......@@ -76,6 +73,8 @@ class RootVisitor BASE_EMBEDDED {
// Also used for marking up GC roots in heap snapshots.
// TODO(ulan): Remove this.
virtual void Synchronize(VisitorSynchronization::SyncTag tag) {}
static const char* RootName(Root root);
};
// Abstract base class for visiting, and optionally modifying, the
......
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