This source file includes following definitions.
- addReferencesForNodeWithEventListeners
- opaqueRootForGC
- VisitPersistentHandle
- notifyFinished
- traverseTree
- gcTree
- m_constructRetainedObjectInfos
- VisitPersistentHandle
- notifyFinished
- liveRootId
- gcPrologue
- minorGCPrologue
- majorGCPrologue
- gcEpilogue
- minorGCEpilogue
- majorGCEpilogue
- collectGarbage
#include "config.h"
#include "bindings/v8/V8GCController.h"
#include <algorithm>
#include "V8MutationObserver.h"
#include "V8Node.h"
#include "V8ScriptRunner.h"
#include "bindings/v8/RetainedDOMInfo.h"
#include "bindings/v8/V8AbstractEventListener.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/WrapperTypeInfo.h"
#include "core/dom/Attr.h"
#include "core/dom/NodeTraversal.h"
#include "core/dom/TemplateContentDocumentFragment.h"
#include "core/dom/shadow/ElementShadow.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLTemplateElement.h"
#include "core/svg/SVGElement.h"
#include "platform/TraceEvent.h"
namespace WebCore {
static void addReferencesForNodeWithEventListeners(v8::Isolate* isolate, Node* node, const v8::Persistent<v8::Object>& wrapper)
{
ASSERT(node->hasEventListeners());
EventListenerIterator iterator(node);
while (EventListener* listener = iterator.nextListener()) {
if (listener->type() != EventListener::JSEventListenerType)
continue;
V8AbstractEventListener* v8listener = static_cast<V8AbstractEventListener*>(listener);
if (!v8listener->hasExistingListenerObject())
continue;
isolate->SetReference(wrapper, v8::Persistent<v8::Value>::Cast(v8listener->existingListenerObjectPersistentHandle()));
}
}
Node* V8GCController::opaqueRootForGC(Node* node, v8::Isolate*)
{
ASSERT(node);
if (node->inDocument() || (isHTMLImageElement(*node) && toHTMLImageElement(*node).hasPendingActivity()))
return &node->document();
if (node->isAttributeNode()) {
Node* ownerElement = toAttr(node)->ownerElement();
if (!ownerElement)
return node;
node = ownerElement;
}
while (Node* parent = node->parentOrShadowHostOrTemplateHostNode())
node = parent;
return node;
}
class MinorGCWrapperVisitor : public v8::PersistentHandleVisitor {
public:
explicit MinorGCWrapperVisitor(v8::Isolate* isolate)
: m_isolate(isolate)
{ }
virtual void VisitPersistentHandle(v8::Persistent<v8::Value>* value, uint16_t classId) OVERRIDE
{
if (classId != v8DOMNodeClassId)
return;
const unsigned wrappersHandledByEachMinorGC = 10000;
if (m_nodesInNewSpace.size() >= wrappersHandledByEachMinorGC)
return;
ASSERT((*reinterpret_cast<v8::Handle<v8::Value>*>(value))->IsObject());
v8::Handle<v8::Object>* wrapper = reinterpret_cast<v8::Handle<v8::Object>*>(value);
ASSERT(V8DOMWrapper::isDOMWrapper(*wrapper));
ASSERT(V8Node::hasInstance(*wrapper, m_isolate));
Node* node = V8Node::toNative(*wrapper);
if (node->containsWrapper()) {
const WrapperTypeInfo* type = toWrapperTypeInfo(*wrapper);
ActiveDOMObject* activeDOMObject = type->toActiveDOMObject(*wrapper);
if (activeDOMObject && activeDOMObject->hasPendingActivity())
return;
if (isHTMLImageElement(*node) && toHTMLImageElement(*node).hasPendingActivity())
return;
if (node->isSVGElement() && toSVGElement(node)->isContextElement())
return;
m_nodesInNewSpace.append(node);
node->markV8CollectableDuringMinorGC();
}
}
void notifyFinished()
{
Node** nodeIterator = m_nodesInNewSpace.begin();
Node** nodeIteratorEnd = m_nodesInNewSpace.end();
for (; nodeIterator < nodeIteratorEnd; ++nodeIterator) {
Node* node = *nodeIterator;
ASSERT(node->containsWrapper());
if (node->isV8CollectableDuringMinorGC()) {
gcTree(m_isolate, node);
node->clearV8CollectableDuringMinorGC();
}
}
}
private:
bool traverseTree(Node* rootNode, Vector<Node*, initialNodeVectorSize>* partiallyDependentNodes)
{
for (Node* node = rootNode; node; node = NodeTraversal::next(*node)) {
if (node->containsWrapper()) {
if (!node->isV8CollectableDuringMinorGC()) {
return false;
}
node->clearV8CollectableDuringMinorGC();
partiallyDependentNodes->append(node);
}
if (ShadowRoot* shadowRoot = node->youngestShadowRoot()) {
if (!traverseTree(shadowRoot, partiallyDependentNodes))
return false;
} else if (node->isShadowRoot()) {
if (ShadowRoot* shadowRoot = toShadowRoot(node)->olderShadowRoot()) {
if (!traverseTree(shadowRoot, partiallyDependentNodes))
return false;
}
}
if (isHTMLTemplateElement(*node)) {
if (!traverseTree(toHTMLTemplateElement(*node).content(), partiallyDependentNodes))
return false;
}
}
return true;
}
void gcTree(v8::Isolate* isolate, Node* startNode)
{
Vector<Node*, initialNodeVectorSize> partiallyDependentNodes;
Node* node = startNode;
while (Node* parent = node->parentOrShadowHostOrTemplateHostNode())
node = parent;
if (!traverseTree(node, &partiallyDependentNodes))
return;
Node** nodeIterator = partiallyDependentNodes.begin();
Node** const nodeIteratorEnd = partiallyDependentNodes.end();
if (nodeIterator == nodeIteratorEnd)
return;
v8::UniqueId id(reinterpret_cast<intptr_t>((*nodeIterator)->unsafePersistent().value()));
for (; nodeIterator != nodeIteratorEnd; ++nodeIterator) {
UnsafePersistent<v8::Object> unsafeWrapper = (*nodeIterator)->unsafePersistent();
v8::Persistent<v8::Object>* wrapper = unsafeWrapper.persistent();
wrapper->MarkPartiallyDependent();
isolate->SetObjectGroupId(v8::Persistent<v8::Value>::Cast(*wrapper), id);
}
}
Vector<Node*> m_nodesInNewSpace;
v8::Isolate* m_isolate;
};
class MajorGCWrapperVisitor : public v8::PersistentHandleVisitor {
public:
explicit MajorGCWrapperVisitor(v8::Isolate* isolate, bool constructRetainedObjectInfos)
: m_isolate(isolate)
, m_liveRootGroupIdSet(false)
, m_constructRetainedObjectInfos(constructRetainedObjectInfos)
{
}
virtual void VisitPersistentHandle(v8::Persistent<v8::Value>* value, uint16_t classId) OVERRIDE
{
if (classId != v8DOMNodeClassId && classId != v8DOMObjectClassId)
return;
ASSERT((*reinterpret_cast<v8::Handle<v8::Value>*>(value))->IsObject());
v8::Handle<v8::Object>* wrapper = reinterpret_cast<v8::Handle<v8::Object>*>(value);
ASSERT(V8DOMWrapper::isDOMWrapper(*wrapper));
if (value->IsIndependent())
return;
const WrapperTypeInfo* type = toWrapperTypeInfo(*wrapper);
void* object = toNative(*wrapper);
ActiveDOMObject* activeDOMObject = type->toActiveDOMObject(*wrapper);
if (activeDOMObject && activeDOMObject->hasPendingActivity())
m_isolate->SetObjectGroupId(*value, liveRootId());
if (classId == v8DOMNodeClassId) {
ASSERT(V8Node::hasInstance(*wrapper, m_isolate));
Node* node = static_cast<Node*>(object);
if (node->hasEventListeners())
addReferencesForNodeWithEventListeners(m_isolate, node, v8::Persistent<v8::Object>::Cast(*value));
Node* root = V8GCController::opaqueRootForGC(node, m_isolate);
m_isolate->SetObjectGroupId(*value, v8::UniqueId(reinterpret_cast<intptr_t>(root)));
if (m_constructRetainedObjectInfos)
m_groupsWhichNeedRetainerInfo.append(root);
} else if (classId == v8DOMObjectClassId) {
type->visitDOMWrapper(object, v8::Persistent<v8::Object>::Cast(*value), m_isolate);
} else {
ASSERT_NOT_REACHED();
}
}
void notifyFinished()
{
if (!m_constructRetainedObjectInfos)
return;
std::sort(m_groupsWhichNeedRetainerInfo.begin(), m_groupsWhichNeedRetainerInfo.end());
Node* alreadyAdded = 0;
v8::HeapProfiler* profiler = m_isolate->GetHeapProfiler();
for (size_t i = 0; i < m_groupsWhichNeedRetainerInfo.size(); ++i) {
Node* root = m_groupsWhichNeedRetainerInfo[i];
if (root != alreadyAdded) {
profiler->SetRetainedObjectInfo(v8::UniqueId(reinterpret_cast<intptr_t>(root)), new RetainedDOMInfo(root));
alreadyAdded = root;
}
}
}
private:
v8::UniqueId liveRootId()
{
const v8::Persistent<v8::Value>& liveRoot = V8PerIsolateData::from(m_isolate)->ensureLiveRoot();
const intptr_t* idPointer = reinterpret_cast<const intptr_t*>(&liveRoot);
v8::UniqueId id(*idPointer);
if (!m_liveRootGroupIdSet) {
m_isolate->SetObjectGroupId(liveRoot, id);
m_liveRootGroupIdSet = true;
}
return id;
}
v8::Isolate* m_isolate;
Vector<Node*> m_groupsWhichNeedRetainerInfo;
bool m_liveRootGroupIdSet;
bool m_constructRetainedObjectInfos;
};
void V8GCController::gcPrologue(v8::GCType type, v8::GCCallbackFlags flags)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (type == v8::kGCTypeScavenge)
minorGCPrologue(isolate);
else if (type == v8::kGCTypeMarkSweepCompact)
majorGCPrologue(flags & v8::kGCCallbackFlagConstructRetainedObjectInfos, isolate);
}
void V8GCController::minorGCPrologue(v8::Isolate* isolate)
{
TRACE_EVENT_BEGIN0("v8", "minorGC");
if (isMainThread()) {
{
TRACE_EVENT_SCOPED_SAMPLING_STATE("Blink", "DOMMinorGC");
v8::HandleScope scope(isolate);
MinorGCWrapperVisitor visitor(isolate);
v8::V8::VisitHandlesForPartialDependence(isolate, &visitor);
visitor.notifyFinished();
}
V8PerIsolateData::from(isolate)->setPreviousSamplingState(TRACE_EVENT_GET_SAMPLING_STATE());
TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8MinorGC");
}
}
void V8GCController::majorGCPrologue(bool constructRetainedObjectInfos, v8::Isolate* isolate)
{
v8::HandleScope scope(isolate);
TRACE_EVENT_BEGIN0("v8", "majorGC");
if (isMainThread()) {
{
TRACE_EVENT_SCOPED_SAMPLING_STATE("Blink", "DOMMajorGC");
MajorGCWrapperVisitor visitor(isolate, constructRetainedObjectInfos);
v8::V8::VisitHandlesWithClassIds(&visitor);
visitor.notifyFinished();
}
V8PerIsolateData::from(isolate)->setPreviousSamplingState(TRACE_EVENT_GET_SAMPLING_STATE());
TRACE_EVENT_SET_SAMPLING_STATE("V8", "V8MajorGC");
} else {
MajorGCWrapperVisitor visitor(isolate, constructRetainedObjectInfos);
v8::V8::VisitHandlesWithClassIds(&visitor);
visitor.notifyFinished();
}
}
void V8GCController::gcEpilogue(v8::GCType type, v8::GCCallbackFlags flags)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (type == v8::kGCTypeScavenge)
minorGCEpilogue(isolate);
else if (type == v8::kGCTypeMarkSweepCompact)
majorGCEpilogue(isolate);
if (flags & v8::kGCCallbackFlagForced) {
Heap::collectAllGarbage(ThreadState::HeapPointersOnStack, Heap::ForcedForTesting);
}
}
void V8GCController::minorGCEpilogue(v8::Isolate* isolate)
{
TRACE_EVENT_END0("v8", "minorGC");
if (isMainThread())
TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)->previousSamplingState());
}
void V8GCController::majorGCEpilogue(v8::Isolate* isolate)
{
v8::HandleScope scope(isolate);
TRACE_EVENT_END0("v8", "majorGC");
if (isMainThread())
TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)->previousSamplingState());
}
void V8GCController::collectGarbage(v8::Isolate* isolate)
{
V8ExecutionScope scope(isolate);
V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, "if (gc) gc();"), isolate);
}
}