Add the test runner for a new a graphics benchmark
[WebKit-https.git] / Source / JavaScriptCore / inspector / agents / InspectorHeapAgent.cpp
1 /*
2  * Copyright (C) 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "InspectorHeapAgent.h"
28
29 #include "InspectorEnvironment.h"
30 #include "VM.h"
31 #include <wtf/Stopwatch.h>
32
33 using namespace JSC;
34
35 namespace Inspector {
36
37 InspectorHeapAgent::InspectorHeapAgent(AgentContext& context)
38     : InspectorAgentBase(ASCIILiteral("Heap"))
39     , m_frontendDispatcher(std::make_unique<HeapFrontendDispatcher>(context.frontendRouter))
40     , m_backendDispatcher(HeapBackendDispatcher::create(context.backendDispatcher, this))
41     , m_environment(context.environment)
42 {
43 }
44
45 InspectorHeapAgent::~InspectorHeapAgent()
46 {
47     ErrorString ignored;
48     disable(ignored);
49 }
50
51 void InspectorHeapAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
52 {
53 }
54
55 void InspectorHeapAgent::willDestroyFrontendAndBackend(DisconnectReason)
56 {
57 }
58
59 void InspectorHeapAgent::enable(ErrorString&)
60 {
61     if (m_enabled)
62         return;
63
64     m_enabled = true;
65
66     m_environment.vm().heap.addObserver(this);
67 }
68
69 void InspectorHeapAgent::disable(ErrorString&)
70 {
71     if (!m_enabled)
72         return;
73
74     m_enabled = false;
75
76     m_environment.vm().heap.removeObserver(this);
77 }
78
79 void InspectorHeapAgent::gc(ErrorString&)
80 {
81     VM& vm = m_environment.vm();
82     JSLockHolder lock(vm);
83     sanitizeStackForVM(&vm);
84     vm.heap.collectAllGarbage();
85 }
86
87 static Inspector::Protocol::Heap::GarbageCollection::Type protocolTypeForHeapOperation(HeapOperation operation)
88 {
89     switch (operation) {
90     case FullCollection:
91         return Inspector::Protocol::Heap::GarbageCollection::Type::Full;
92     case EdenCollection:
93         return Inspector::Protocol::Heap::GarbageCollection::Type::Partial;
94     default:
95         ASSERT_NOT_REACHED();
96         return Inspector::Protocol::Heap::GarbageCollection::Type::Full;
97     }
98 }
99
100 void InspectorHeapAgent::willGarbageCollect()
101 {
102     ASSERT(m_enabled);
103     ASSERT(std::isnan(m_gcStartTime));
104
105     m_gcStartTime = m_environment.executionStopwatch()->elapsedTime();
106 }
107
108 void InspectorHeapAgent::didGarbageCollect(HeapOperation operation)
109 {
110     ASSERT(m_enabled);
111     ASSERT(!std::isnan(m_gcStartTime));
112
113     // FIXME: Include number of bytes freed by collection.
114
115     double endTime = m_environment.executionStopwatch()->elapsedTime();
116     auto collection = Inspector::Protocol::Heap::GarbageCollection::create()
117         .setType(protocolTypeForHeapOperation(operation))
118         .setStartTime(m_gcStartTime)
119         .setEndTime(endTime)
120         .release();
121
122     m_frontendDispatcher->garbageCollected(WTF::move(collection));
123     m_gcStartTime = NAN;
124 }
125
126 } // namespace Inspector