InspectorProfilerAgent.cpp 15.6 KB
Newer Older
1 2 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 32
/*
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  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.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") 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 APPLE AND ITS 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 APPLE OR ITS 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"
#include "InspectorProfilerAgent.h"

33
#if ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)
34 35

#include "Console.h"
36
#include "InjectedScript.h"
37
#include "InjectedScriptHost.h"
38
#include "InspectorConsoleAgent.h"
39
#include "InspectorFrontend.h"
40
#include "InspectorState.h"
41
#include "InspectorValues.h"
42
#include "InstrumentingAgents.h"
43 44
#include "KURL.h"
#include "Page.h"
45
#include "PageScriptDebugServer.h"
46
#include "ScriptHeapSnapshot.h"
47
#include "ScriptObject.h"
48 49 50
#include "ScriptProfile.h"
#include "ScriptProfiler.h"
#include <wtf/OwnPtr.h>
51
#include <wtf/text/StringConcatenate.h>
52 53 54

namespace WebCore {

55 56 57 58 59
namespace ProfilerAgentState {
static const char userInitiatedProfiling[] = "userInitiatedProfiling";
static const char profilerEnabled[] = "profilerEnabled";
}

60 61
static const char* const UserInitiatedProfileName = "org.webkit.profiles.user-initiated";
static const char* const CPUProfileType = "CPU";
62
static const char* const HeapProfileType = "HEAP";
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

class PageProfilerAgent : public InspectorProfilerAgent {
public:
    PageProfilerAgent(InstrumentingAgents* instrumentingAgents, InspectorConsoleAgent* consoleAgent, Page* inspectedPage, InspectorState* state, InjectedScriptManager* injectedScriptManager)
        : InspectorProfilerAgent(instrumentingAgents, consoleAgent, state, injectedScriptManager), m_inspectedPage(inspectedPage) { }
    virtual ~PageProfilerAgent() { }

private:
    virtual void startProfiling(const String& title)
    {
        ScriptProfiler::startForPage(m_inspectedPage, title);
    }

    virtual PassRefPtr<ScriptProfile> stopProfiling(const String& title)
    {
        return ScriptProfiler::stopForPage(m_inspectedPage, title);
    }

    Page* m_inspectedPage;
};

85
PassOwnPtr<InspectorProfilerAgent> InspectorProfilerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorConsoleAgent* consoleAgent, Page* inspectedPage, InspectorState* inspectorState, InjectedScriptManager* injectedScriptManager)
86
{
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    return adoptPtr(new PageProfilerAgent(instrumentingAgents, consoleAgent, inspectedPage, inspectorState, injectedScriptManager));
}


#if ENABLE(WORKERS)
class WorkerProfilerAgent : public InspectorProfilerAgent {
public:
    WorkerProfilerAgent(InstrumentingAgents* instrumentingAgents, InspectorConsoleAgent* consoleAgent, WorkerContext* workerContext, InspectorState* state, InjectedScriptManager* injectedScriptManager)
        : InspectorProfilerAgent(instrumentingAgents, consoleAgent, state, injectedScriptManager), m_workerContext(workerContext) { }
    virtual ~WorkerProfilerAgent() { }

private:
    virtual void startProfiling(const String& title)
    {
        ScriptProfiler::startForWorkerContext(m_workerContext, title);
    }

    virtual PassRefPtr<ScriptProfile> stopProfiling(const String& title)
    {
        return ScriptProfiler::stopForWorkerContext(m_workerContext, title);
    }

    WorkerContext* m_workerContext;
};

PassOwnPtr<InspectorProfilerAgent> InspectorProfilerAgent::create(InstrumentingAgents* instrumentingAgents, InspectorConsoleAgent* consoleAgent, WorkerContext* workerContext, InspectorState* inspectorState, InjectedScriptManager* injectedScriptManager)
{
    return adoptPtr(new WorkerProfilerAgent(instrumentingAgents, consoleAgent, workerContext, inspectorState, injectedScriptManager));
115
}
116
#endif
117

118
InspectorProfilerAgent::InspectorProfilerAgent(InstrumentingAgents* instrumentingAgents, InspectorConsoleAgent* consoleAgent, InspectorState* inspectorState, InjectedScriptManager* injectedScriptManager)
119
    : InspectorBaseAgent<InspectorProfilerAgent>("Profiler", instrumentingAgents, inspectorState)
120
    , m_consoleAgent(consoleAgent)
121
    , m_injectedScriptManager(injectedScriptManager)
122
    , m_frontend(0)
123
    , m_enabled(false)
124 125 126
    , m_recordingUserInitiatedProfile(false)
    , m_currentUserInitiatedProfileNumber(-1)
    , m_nextUserInitiatedProfileNumber(1)
127
    , m_nextUserInitiatedHeapSnapshotNumber(1)
128
{
129
    m_instrumentingAgents->setInspectorProfilerAgent(this);
130 131 132 133
}

InspectorProfilerAgent::~InspectorProfilerAgent()
{
134
    m_instrumentingAgents->setInspectorProfilerAgent(0);
135 136 137 138 139 140
}

void InspectorProfilerAgent::addProfile(PassRefPtr<ScriptProfile> prpProfile, unsigned lineNumber, const String& sourceURL)
{
    RefPtr<ScriptProfile> profile = prpProfile;
    m_profiles.add(profile->uid(), profile);
141 142
    if (m_frontend)
        m_frontend->addProfileHeader(createProfileHeader(*profile));
143 144 145 146 147
    addProfileFinishedMessageToConsole(profile, lineNumber, sourceURL);
}

void InspectorProfilerAgent::addProfileFinishedMessageToConsole(PassRefPtr<ScriptProfile> prpProfile, unsigned lineNumber, const String& sourceURL)
{
148 149
    if (!m_frontend)
        return;
150 151
    RefPtr<ScriptProfile> profile = prpProfile;
    String title = profile->title();
152
    String message = makeString("Profile \"webkit-profile://", CPUProfileType, '/', encodeWithURLEscapeSequences(title), '#', String::number(profile->uid()), "\" finished.");
153
    m_consoleAgent->addMessageToConsole(JSMessageSource, LogMessageType, LogMessageLevel, message, sourceURL, lineNumber);
154 155 156 157
}

void InspectorProfilerAgent::addStartProfilingMessageToConsole(const String& title, unsigned lineNumber, const String& sourceURL)
{
158 159
    if (!m_frontend)
        return;
160
    String message = makeString("Profile \"webkit-profile://", CPUProfileType, '/', encodeWithURLEscapeSequences(title), "#0\" started.");
161
    m_consoleAgent->addMessageToConsole(JSMessageSource, LogMessageType, LogMessageLevel, message, sourceURL, lineNumber);
162 163
}

164 165 166 167 168
void InspectorProfilerAgent::collectGarbage(WebCore::ErrorString*)
{
    ScriptProfiler::collectGarbage();
}

169 170 171 172 173 174 175 176 177
PassRefPtr<InspectorObject> InspectorProfilerAgent::createProfileHeader(const ScriptProfile& profile)
{
    RefPtr<InspectorObject> header = InspectorObject::create();
    header->setString("title", profile.title());
    header->setNumber("uid", profile.uid());
    header->setString("typeId", String(CPUProfileType));
    return header;
}

178 179 180 181 182 183 184 185 186
PassRefPtr<InspectorObject> InspectorProfilerAgent::createSnapshotHeader(const ScriptHeapSnapshot& snapshot)
{
    RefPtr<InspectorObject> header = InspectorObject::create();
    header->setString("title", snapshot.title());
    header->setNumber("uid", snapshot.uid());
    header->setString("typeId", String(HeapProfileType));
    return header;
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

void InspectorProfilerAgent::causesRecompilation(ErrorString*, bool* result)
{
    *result = ScriptProfiler::causesRecompilation();
}

void InspectorProfilerAgent::isSampling(ErrorString*, bool* result)
{
    *result = ScriptProfiler::isSampling();
}

void InspectorProfilerAgent::hasHeapProfiler(ErrorString*, bool* result)
{
    *result = ScriptProfiler::hasHeapProfiler();
}

203 204 205 206
void InspectorProfilerAgent::enable(ErrorString*)
{
    if (enabled())
        return;
207
    m_state->setBoolean(ProfilerAgentState::profilerEnabled, true);
208 209 210 211 212
    enable(false);
}

void InspectorProfilerAgent::disable(ErrorString*)
{
213
    m_state->setBoolean(ProfilerAgentState::profilerEnabled, false);
214 215 216
    disable();
}

217 218 219 220 221
void InspectorProfilerAgent::disable()
{
    if (!m_enabled)
        return;
    m_enabled = false;
222
    PageScriptDebugServer::shared().recompileAllJSFunctionsSoon();
223 224 225 226 227 228 229 230
}

void InspectorProfilerAgent::enable(bool skipRecompile)
{
    if (m_enabled)
        return;
    m_enabled = true;
    if (!skipRecompile)
231
        PageScriptDebugServer::shared().recompileAllJSFunctionsSoon();
232 233 234 235 236 237 238
}

String InspectorProfilerAgent::getCurrentUserInitiatedProfileName(bool incrementProfileNumber)
{
    if (incrementProfileNumber)
        m_currentUserInitiatedProfileNumber = m_nextUserInitiatedProfileNumber++;

239
    return makeString(UserInitiatedProfileName, '.', String::number(m_currentUserInitiatedProfileNumber));
240 241
}

242
void InspectorProfilerAgent::getProfileHeaders(ErrorString*, RefPtr<InspectorArray>& headers)
243 244 245
{
    ProfilesMap::iterator profilesEnd = m_profiles.end();
    for (ProfilesMap::iterator it = m_profiles.begin(); it != profilesEnd; ++it)
246
        headers->pushObject(createProfileHeader(*it->second));
247 248
    HeapSnapshotsMap::iterator snapshotsEnd = m_snapshots.end();
    for (HeapSnapshotsMap::iterator it = m_snapshots.begin(); it != snapshotsEnd; ++it)
249
        headers->pushObject(createSnapshotHeader(*it->second));
250 251
}

252 253 254 255
namespace {

class OutputStream : public ScriptHeapSnapshot::OutputStream {
public:
256
    OutputStream(InspectorFrontend::Profiler* frontend, unsigned uid)
257 258 259 260
        : m_frontend(frontend), m_uid(uid) { }
    void Write(const String& chunk) { m_frontend->addHeapSnapshotChunk(m_uid, chunk); }
    void Close() { m_frontend->finishHeapSnapshot(m_uid); }
private:
261
    InspectorFrontend::Profiler* m_frontend;
262
    int m_uid;
263 264 265 266
};

} // namespace

267
void InspectorProfilerAgent::getProfile(ErrorString*, const String& type, int rawUid, RefPtr<InspectorObject>& profileObject)
268
{
269
    unsigned uid = static_cast<unsigned>(rawUid);
270 271 272
    if (type == CPUProfileType) {
        ProfilesMap::iterator it = m_profiles.find(uid);
        if (it != m_profiles.end()) {
273 274
            profileObject = createProfileHeader(*it->second);
            profileObject->setObject("head", it->second->buildInspectorObjectForHead());
275
            if (it->second->bottomUpHead())
276
                profileObject->setObject("bottomUpHead", it->second->buildInspectorObjectForBottomUpHead());
277 278 279 280
        }
    } else if (type == HeapProfileType) {
        HeapSnapshotsMap::iterator it = m_snapshots.find(uid);
        if (it != m_snapshots.end()) {
281
            RefPtr<ScriptHeapSnapshot> snapshot = it->second;
282
            profileObject = createSnapshotHeader(*snapshot);
283 284 285 286
            if (m_frontend) {
                OutputStream stream(m_frontend, uid);
                snapshot->writeJSON(&stream);
            }
287
        }
288 289 290
    }
}

291
void InspectorProfilerAgent::removeProfile(ErrorString*, const String& type, int rawUid)
292
{
293
    unsigned uid = static_cast<unsigned>(rawUid);
294 295 296 297 298 299 300
    if (type == CPUProfileType) {
        if (m_profiles.contains(uid))
            m_profiles.remove(uid);
    } else if (type == HeapProfileType) {
        if (m_snapshots.contains(uid))
            m_snapshots.remove(uid);
    }
301 302 303 304
}

void InspectorProfilerAgent::resetState()
{
305
    stop();
306
    m_profiles.clear();
307
    m_snapshots.clear();
308 309
    m_currentUserInitiatedProfileNumber = 1;
    m_nextUserInitiatedProfileNumber = 1;
310
    m_nextUserInitiatedHeapSnapshotNumber = 1;
311
    resetFrontendProfiles();
312
    m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
313 314 315 316
}

void InspectorProfilerAgent::resetFrontendProfiles()
{
317 318 319
    if (m_frontend
        && m_profiles.begin() == m_profiles.end()
        && m_snapshots.begin() == m_snapshots.end())
320
        m_frontend->resetProfiles();
321 322
}

323 324 325 326 327 328 329 330
void InspectorProfilerAgent::setFrontend(InspectorFrontend* frontend)
{
    m_frontend = frontend->profiler();
}

void InspectorProfilerAgent::clearFrontend()
{
    m_frontend = 0;
331
    stop();
332 333
    ErrorString error;
    disable(&error);
334 335 336 337
}

void InspectorProfilerAgent::restore()
{
338
    // Need to restore enablement state here as in setFrontend m_state wasn't loaded yet.
339 340 341 342
    restoreEnablement();

    // Revisit this.
    resetFrontendProfiles();
343
    if (m_state->getBoolean(ProfilerAgentState::userInitiatedProfiling))
344
        start();
345 346 347 348
}

void InspectorProfilerAgent::restoreEnablement()
{
349
    if (m_state->getBoolean(ProfilerAgentState::profilerEnabled)) {
350 351 352 353 354
        ErrorString error;
        enable(&error);
    }
}

355
void InspectorProfilerAgent::start(ErrorString*)
356
{
357 358
    if (m_recordingUserInitiatedProfile)
        return;
359
    if (!enabled()) {
360
        enable(true);
361
        PageScriptDebugServer::shared().recompileAllJSFunctions(0);
362 363 364
    }
    m_recordingUserInitiatedProfile = true;
    String title = getCurrentUserInitiatedProfileName(true);
365
    startProfiling(title);
366 367
    addStartProfilingMessageToConsole(title, 0, String());
    toggleRecordButton(true);
368
    m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true);
369 370
}

371
void InspectorProfilerAgent::stop(ErrorString*)
372
{
373 374
    if (!m_recordingUserInitiatedProfile)
        return;
375 376
    m_recordingUserInitiatedProfile = false;
    String title = getCurrentUserInitiatedProfileName();
377
    RefPtr<ScriptProfile> profile = stopProfiling(title);
378 379
    if (profile)
        addProfile(profile, 0, String());
380
    toggleRecordButton(false);
381
    m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false);
382 383
}

384 385 386 387
namespace {

class HeapSnapshotProgress: public ScriptProfiler::HeapSnapshotProgress {
public:
388
    explicit HeapSnapshotProgress(InspectorFrontend::Profiler* frontend)
389 390 391 392 393 394 395 396 397 398 399 400 401
        : m_frontend(frontend) { }
    void Start(int totalWork)
    {
        m_totalWork = totalWork;
    }
    void Worked(int workDone)
    {
        if (m_frontend)
            m_frontend->reportHeapSnapshotProgress(workDone, m_totalWork);
    }
    void Done() { }
    bool isCanceled() { return false; }
private:
402
    InspectorFrontend::Profiler* m_frontend;
403 404 405 406 407
    int m_totalWork;
};

};

408
void InspectorProfilerAgent::takeHeapSnapshot(ErrorString*)
409
{
410 411 412
    String title = makeString(UserInitiatedProfileName, '.', String::number(m_nextUserInitiatedHeapSnapshotNumber));
    ++m_nextUserInitiatedHeapSnapshotNumber;

413
    HeapSnapshotProgress progress(m_frontend);
414
    RefPtr<ScriptHeapSnapshot> snapshot = ScriptProfiler::takeHeapSnapshot(title, &progress);
415 416 417 418 419 420 421
    if (snapshot) {
        m_snapshots.add(snapshot->uid(), snapshot);
        if (m_frontend)
            m_frontend->addProfileHeader(createSnapshotHeader(*snapshot));
    }
}

422 423
void InspectorProfilerAgent::toggleRecordButton(bool isProfiling)
{
424 425
    if (m_frontend)
        m_frontend->setRecordingProfile(isProfiling);
426 427
}

428
void InspectorProfilerAgent::getObjectByHeapObjectId(ErrorString* error, int id, const String* objectGroup, RefPtr<InspectorObject>& result)
429
{
430 431
    ScriptObject heapObject = ScriptProfiler::objectByHeapObjectId(id);
    if (heapObject.hasNoValue()) {
432
        *error = "Object is not available.";
433 434 435 436 437 438 439 440 441 442
        return;
    }
    InjectedScript injectedScript = m_injectedScriptManager->injectedScriptFor(heapObject.scriptState());
    if (injectedScript.hasNoValue()) {
        *error = "Object is not available. Inspected context is gone.";
        return;
    }
    result = injectedScript.wrapObject(heapObject, objectGroup ? *objectGroup : "");
    if (!result)
        *error = "Failed to wrap object.";
443 444
}

445 446
} // namespace WebCore

447
#endif // ENABLE(JAVASCRIPT_DEBUGGER) && ENABLE(INSPECTOR)