ScriptProfiler.cpp 6.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, Google Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
32
#include "ScriptProfiler.h"
33

34
#include "DOMWrapperVisitor.h"
35
#include "RetainedDOMInfo.h"
36
#include "ScriptObject.h"
37
#include "V8Binding.h"
38
#include "V8DOMMap.h"
39
#include "V8Node.h"
40 41

#include <v8-profiler.h>
42 43 44

namespace WebCore {

45
#if ENABLE(INSPECTOR)
46
void ScriptProfiler::start(ScriptState* state, const String& title)
47
{
48 49
    v8::HandleScope hs;
    v8::CpuProfiler::StartProfiling(v8String(title));
50 51
}

52 53 54 55 56 57 58 59 60 61 62 63
void ScriptProfiler::startForPage(Page*, const String& title)
{
    return start(0, title);
}

#if ENABLE(WORKERS)
void ScriptProfiler::startForWorkerContext(WorkerContext*, const String& title)
{
    return start(0, title);
}
#endif

64
PassRefPtr<ScriptProfile> ScriptProfiler::stop(ScriptState* state, const String& title)
65
{
66
    v8::HandleScope hs;
67 68 69
    const v8::CpuProfile* profile = state ?
        v8::CpuProfiler::StopProfiling(v8String(title), state->context()->GetSecurityToken()) :
        v8::CpuProfiler::StopProfiling(v8String(title));
70
    return profile ? ScriptProfile::create(profile) : 0;
71 72
}

73 74 75 76 77 78 79 80 81 82 83 84 85 86
PassRefPtr<ScriptProfile> ScriptProfiler::stopForPage(Page*, const String& title)
{
    // Use null script state to avoid filtering by context security token.
    // All functions from all iframes should be visible from Inspector UI.
    return stop(0, title);
}

#if ENABLE(WORKERS)
PassRefPtr<ScriptProfile> ScriptProfiler::stopForWorkerContext(WorkerContext*, const String& title)
{
    return stop(0, title);
}
#endif

87 88
void ScriptProfiler::collectGarbage()
{
89
    v8::V8::LowMemoryNotification();
90 91
}

92
ScriptObject ScriptProfiler::objectByHeapObjectId(unsigned id)
93 94 95 96 97 98 99 100 101 102 103
{
    // As ids are unique, it doesn't matter which HeapSnapshot owns HeapGraphNode.
    // We need to find first HeapSnapshot containing a node with the specified id.
    const v8::HeapGraphNode* node = 0;
    for (int i = 0, l = v8::HeapProfiler::GetSnapshotsCount(); i < l; ++i) {
        const v8::HeapSnapshot* snapshot = v8::HeapProfiler::GetSnapshot(i);
        node = snapshot->GetNodeById(id);
        if (node)
            break;
    }
    if (!node)
104
        return ScriptObject();
105 106 107 108

    v8::HandleScope scope;
    v8::Handle<v8::Value> value = node->GetHeapValue();
    if (!value->IsObject())
109
        return ScriptObject();
110

111 112 113
    v8::Handle<v8::Object> object = value.As<v8::Object>();
    ScriptState* scriptState = ScriptState::forContext(object->CreationContext());
    return ScriptObject(scriptState, object);
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
namespace {

class ActivityControlAdapter : public v8::ActivityControl {
public:
    ActivityControlAdapter(ScriptProfiler::HeapSnapshotProgress* progress)
            : m_progress(progress), m_firstReport(true) { }
    ControlOption ReportProgressValue(int done, int total)
    {
        ControlOption result = m_progress->isCanceled() ? kAbort : kContinue;
        if (m_firstReport) {
            m_firstReport = false;
            m_progress->Start(total);
        } else
            m_progress->Worked(done);
        if (done >= total)
            m_progress->Done();
        return result;
    }
private:
    ScriptProfiler::HeapSnapshotProgress* m_progress;
    bool m_firstReport;
};

} // namespace

PassRefPtr<ScriptHeapSnapshot> ScriptProfiler::takeHeapSnapshot(const String& title, HeapSnapshotProgress* control)
142
{
143
    v8::HandleScope hs;
144 145 146
    ASSERT(control);
    ActivityControlAdapter adapter(control);
    const v8::HeapSnapshot* snapshot = v8::HeapProfiler::TakeSnapshot(v8String(title), v8::HeapSnapshot::kFull, &adapter);
147
    return snapshot ? ScriptHeapSnapshot::create(snapshot) : 0;
148 149
}

150 151 152 153 154 155 156 157
static v8::RetainedObjectInfo* retainedDOMInfo(uint16_t classId, v8::Handle<v8::Value> wrapper)
{
    ASSERT(classId == v8DOMSubtreeClassId);
    if (!wrapper->IsObject())
        return 0;
    Node* node = V8Node::toNative(wrapper.As<v8::Object>());
    return node ? new RetainedDOMInfo(node) : 0;
}
158
#endif // ENABLE(INSPECTOR)
159 160 161

void ScriptProfiler::initialize()
{
162
#if ENABLE(INSPECTOR)
163
    v8::HeapProfiler::DefineWrapperClass(v8DOMSubtreeClassId, &retainedDOMInfo);
164
#endif // ENABLE(INSPECTOR)
165 166
}

167 168 169 170 171
void ScriptProfiler::visitJSDOMWrappers(DOMWrapperVisitor* visitor)
{
    class VisitorAdapter : public DOMWrapperMap<Node>::Visitor {
    public:
        VisitorAdapter(DOMWrapperVisitor* visitor) : m_visitor(visitor) { }
172

173 174 175
        virtual void visitDOMWrapper(DOMDataStore*, Node* node, v8::Persistent<v8::Object>)
        {
            m_visitor->visitNode(node);
176
        }
177 178 179 180
    private:
        DOMWrapperVisitor* m_visitor;
    } adapter(visitor);
    visitDOMNodes(&adapter);
181 182
}

183 184 185 186 187
void ScriptProfiler::visitExternalJSStrings(DOMWrapperVisitor* visitor)
{
    V8BindingPerIsolateData::current()->visitJSExternalStrings(visitor);
}

188
} // namespace WebCore