Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1498)

Unified Diff: third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp

Issue 2625093002: [wrapper-tracing] Add heap snapshot generator infrastructure (Closed)
Patch Set: Rework and address comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp b/third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp
index 6ded783a981de097db3e6563388ac6f197bae47a..1820b624cb8a17dd015ff43dfc6009b71b65422d 100644
--- a/third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp
+++ b/third_party/WebKit/Source/bindings/core/v8/V8GCController.cpp
@@ -51,6 +51,8 @@
#include "wtf/Vector.h"
#include "wtf/allocator/Partitions.h"
#include <algorithm>
+#include <unordered_map>
+#include <unordered_set>
namespace blink {
@@ -148,6 +150,140 @@ class MinorGCUnmodifiedWrapperVisitor : public v8::PersistentHandleVisitor {
v8::Isolate* m_isolate;
};
+class HeapSnaphotWrapperVisitor : public ScriptWrappableVisitor,
+ public v8::PersistentHandleVisitor {
+ public:
+ explicit HeapSnaphotWrapperVisitor(v8::Isolate* isolate)
+ : ScriptWrappableVisitor(isolate),
+ m_currentParent(nullptr),
+ m_onlyTraceSingleLevel(false),
+ m_firstTracedScriptWrappable(false) {
+ m_nodesRequiringTracing.clear();
+ }
+
+ void VisitPersistentHandle(v8::Persistent<v8::Value>* value,
+ uint16_t classId) override {
+ if (classId != WrapperTypeInfo::NodeClassId)
+ return;
+
+ DCHECK(!value->IsIndependent());
+ v8::Local<v8::Object> wrapper = v8::Local<v8::Object>::New(
+ m_isolate, v8::Persistent<v8::Object>::Cast(*value));
+ DCHECK(V8Node::hasInstance(wrapper, m_isolate));
+ Node* node = V8Node::toImpl(wrapper);
+ Node* root = V8GCController::opaqueRootForGC(m_isolate, node);
+ m_nodesRequiringTracing[root].push_back(node);
haraken 2017/01/16 02:46:56 Why don't you need to push Nodes that are found du
Michael Lippautz 2017/01/16 10:45:06 VisitPersistentHandle visits all persistent handle
haraken 2017/01/16 12:48:13 What happens if there is a pending activity on a D
Michael Lippautz 2017/01/16 13:15:38 Then tracePendingActivities will catch it.
+ }
+
+ // Trace through the blink heap to find all V8 wrappers reachable from
+ // ActiveScriptWrappables. Also collect retainer edges on the way.
+ void tracePendingActivities() {
+ CHECK(m_tempFoundV8Wrappers.empty());
+ m_currentParent = nullptr;
+
+ TracePrologue();
+ ActiveScriptWrappableBase::traceActiveScriptWrappables(m_isolate, this);
+ AdvanceTracing(
+ 0,
+ v8::EmbedderHeapTracer::AdvanceTracingActions(
+ v8::EmbedderHeapTracer::ForceCompletionAction::FORCE_COMPLETION));
+ // Abort instead of Epilogue as we want to finish synchronously.
+ AbortTracing();
haraken 2017/01/16 02:46:56 What's a difference between AbortTracing and Epilo
Michael Lippautz 2017/01/16 10:45:06 Since we finished the line before with FORE_COMPLE
haraken 2017/01/16 12:48:12 Yeah, that looks tidier. (You can address it in a
Michael Lippautz 2017/01/16 13:15:38 Acknowledged.
+
+ m_groups.push_back(
+ std::make_pair(new SuspendableObjectsInfo(m_tempFoundV8Wrappers.size()),
+ std::move(m_tempFoundV8Wrappers)));
+ }
+
+ // Trace through the blink heap to find all V8 wrappers reachable from
+ // the nodes that require tracing. Also collect retainer edges on the way.
+ void traceNodesThatRequireTracing() {
haraken 2017/01/16 02:46:56 I'm not sure why we need the complexity of traceNo
Michael Lippautz 2017/01/16 10:45:06 We cannot do that, as we need a mapping from nodes
+ for (auto& groupPair : m_nodesRequiringTracing) {
+ v8::HeapProfiler::RetainerChildren groupChildren;
+ for (auto& node : groupPair.second) {
+ auto wrappers = findV8WrappersDirectlyReachableFrom(node);
+ groupChildren.insert(wrappers.begin(), wrappers.end());
+ }
+ m_groups.push_back(std::make_pair(new RetainedDOMInfo(groupPair.first),
+ std::move(groupChildren)));
+ }
+ }
+
+ v8::HeapProfiler::RetainerEdges edges() { return std::move(m_edges); }
+ v8::HeapProfiler::RetainerGroups groups() { return std::move(m_groups); }
+
+ void markWrapper(const v8::PersistentBase<v8::Value>* value) const override {
+ if (!m_tracingInProgress)
haraken 2017/01/16 02:46:56 Can this happen?
Michael Lippautz 2017/01/16 10:45:06 Removed as it cannot happen. This was copied over
+ return;
+ if (m_currentParent && m_currentParent != value)
+ m_edges.push_back(std::make_pair(m_currentParent, value));
+ m_tempFoundV8Wrappers.insert(value);
+ }
+
+ void dispatchTraceWrappers(const TraceWrapperBase* traceable) const override {
+ if (!m_onlyTraceSingleLevel || !traceable->isScriptWrappable() ||
+ m_firstTracedScriptWrappable) {
+ m_firstTracedScriptWrappable = false;
+ traceable->traceWrappers(this);
+ }
haraken 2017/01/16 02:46:56 Don't we need to reset m_firstTracedScriptWrappabl
Michael Lippautz 2017/01/16 10:45:06 Yes, that's the intention. It should find wrappers
haraken 2017/01/16 12:48:13 But what happens if there are multiple ScriptWrapp
Michael Lippautz 2017/01/16 13:15:38 They are traced and we insert edges from the v8 wr
haraken 2017/01/17 00:31:25 Imagine the following object graph: Node --> Tr
Michael Lippautz 2017/01/17 09:15:32 Works, and here is why :) My assumptions: - Node
haraken 2017/01/17 09:22:41 Ah, I was missing this point.
+ }
+
+ private:
+ v8::HeapProfiler::RetainerChildren findV8WrappersDirectlyReachableFrom(
+ Node* traceable) {
+ CHECK(m_tempFoundV8Wrappers.empty());
+ WTF::AutoReset<bool>(&m_onlyTraceSingleLevel, true);
+ m_firstTracedScriptWrappable = true;
haraken 2017/01/16 02:46:56 Maybe should we flip true/false? Initially the fi
Michael Lippautz 2017/01/16 10:45:06 Done.
+ m_currentParent =
+ &v8::Persistent<v8::Value>::Cast(*traceable->rawMainWorldWrapper());
+
+ TracePrologue();
+ traceable->wrapperTypeInfo()->traceWrappers(this, traceable);
+ AdvanceTracing(
+ 0,
+ v8::EmbedderHeapTracer::AdvanceTracingActions(
+ v8::EmbedderHeapTracer::ForceCompletionAction::FORCE_COMPLETION));
+ // Abort instead of Epilogue as we want to finish synchronously.
+ AbortTracing();
+
+ return std::move(m_tempFoundV8Wrappers);
+ }
+
+ // Input obtained from |VisitPersistentHandle|.
+ std::unordered_map<Node*, std::vector<Node*>> m_nodesRequiringTracing;
+
+ // Temporaries used for tracing a single Node.
+ const v8::PersistentBase<v8::Value>* m_currentParent;
+ bool m_onlyTraceSingleLevel;
+ mutable bool m_firstTracedScriptWrappable;
+ mutable v8::HeapProfiler::RetainerChildren m_tempFoundV8Wrappers;
haraken 2017/01/16 02:46:56 m_tempFoundV8Wrappers => m_foundV8Wrappers
Michael Lippautz 2017/01/16 10:45:06 Done.
+
+ // Out variables
+ mutable v8::HeapProfiler::RetainerEdges m_edges;
+ mutable v8::HeapProfiler::RetainerGroups m_groups;
+};
+
+// The function |getRetainerInfos| processing all handles on the blink heap,
+// logically grouping together DOM trees (attached and detached) and pending
+// activities, while at the same time finding parent to child relationships
+// for non-Node wrappers. Since we are processing *all* handles there is no
+// way we miss out on a handle. V8 will figure out the liveness information
+// for the provided information itself.
+v8::HeapProfiler::RetainerInfos V8GCController::getRetainerInfos(
+ v8::Isolate* isolate) {
+ HeapSnaphotWrapperVisitor tracer(isolate);
haraken 2017/01/16 02:46:56 I guess we should use: std::unique_ptr<HeapSnap
Michael Lippautz 2017/01/16 10:45:06 Done.
+ V8PerIsolateData::TemporaryScriptWrappableVisitorScope scope(isolate,
+ &tracer);
+
+ // Collect which handles need to be processed.
+ isolate->VisitHandlesWithClassIds(&tracer);
haraken 2017/01/16 02:46:56 Nit: I'd prefer writing this as tracer->traceV8Roo
Michael Lippautz 2017/01/16 10:51:01 Done: collectV8Roots.
+
+ tracer.tracePendingActivities();
+ tracer.traceNodesThatRequireTracing();
haraken 2017/01/16 02:46:56 Nit: tracer->traceBlinkRoots().
Michael Lippautz 2017/01/16 10:51:01 This is now traceV8Roots, as it traces exactly wha
+
+ return v8::HeapProfiler::RetainerInfos{tracer.groups(), tracer.edges()};
+}
+
class MajorGCWrapperVisitor : public v8::PersistentHandleVisitor {
public:
explicit MajorGCWrapperVisitor(v8::Isolate* isolate,

Powered by Google App Engine
This is Rietveld 408576698