1/*
2 * Copyright (C) 2012-2018 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 "ProfilerCompilation.h"
28
29#include "JSGlobalObject.h"
30#include "ObjectConstructor.h"
31#include "JSCInlines.h"
32#include "ProfilerDatabase.h"
33#include "Watchpoint.h"
34#include <wtf/StringPrintStream.h>
35
36namespace JSC { namespace Profiler {
37
38Compilation::Compilation(Bytecodes* bytecodes, CompilationKind kind)
39 : m_kind(kind)
40 , m_bytecodes(bytecodes)
41 , m_numInlinedGetByIds(0)
42 , m_numInlinedPutByIds(0)
43 , m_numInlinedCalls(0)
44 , m_jettisonReason(NotJettisoned)
45 , m_uid(UID::create())
46{
47}
48
49Compilation::~Compilation() { }
50
51void Compilation::addProfiledBytecodes(Database& database, CodeBlock* profiledBlock)
52{
53 Bytecodes* bytecodes = database.ensureBytecodesFor(profiledBlock);
54
55 // First make sure that we haven't already added profiled bytecodes for this code
56 // block. We do this using an O(N) search because I suspect that this list will
57 // tend to be fairly small, and the additional space costs of having a HashMap/Set
58 // would be greater than the time cost of occasionally doing this search.
59
60 for (unsigned i = m_profiledBytecodes.size(); i--;) {
61 if (m_profiledBytecodes[i].bytecodes() == bytecodes)
62 return;
63 }
64
65 m_profiledBytecodes.append(ProfiledBytecodes(bytecodes, profiledBlock));
66}
67
68void Compilation::addDescription(const CompiledBytecode& compiledBytecode)
69{
70 m_descriptions.append(compiledBytecode);
71}
72
73void Compilation::addDescription(const OriginStack& stack, const CString& description)
74{
75 addDescription(CompiledBytecode(stack, description));
76}
77
78ExecutionCounter* Compilation::executionCounterFor(const OriginStack& origin)
79{
80 std::unique_ptr<ExecutionCounter>& counter = m_counters.add(origin, nullptr).iterator->value;
81 if (!counter)
82 counter = std::make_unique<ExecutionCounter>();
83
84 return counter.get();
85}
86
87void Compilation::addOSRExitSite(const Vector<MacroAssemblerCodePtr<JSInternalPtrTag>>& codeAddresses)
88{
89 m_osrExitSites.append(OSRExitSite(codeAddresses));
90}
91
92OSRExit* Compilation::addOSRExit(unsigned id, const OriginStack& originStack, ExitKind exitKind, bool isWatchpoint)
93{
94 m_osrExits.append(OSRExit(id, originStack, exitKind, isWatchpoint));
95 return &m_osrExits.last();
96}
97
98void Compilation::setJettisonReason(JettisonReason jettisonReason, const FireDetail* detail)
99{
100 if (m_jettisonReason != NotJettisoned)
101 return; // We only care about the original jettison reason.
102
103 m_jettisonReason = jettisonReason;
104 if (detail)
105 m_additionalJettisonReason = toCString(*detail);
106 else
107 m_additionalJettisonReason = CString();
108}
109
110void Compilation::dump(PrintStream& out) const
111{
112 out.print("Comp", m_uid);
113}
114
115JSValue Compilation::toJS(ExecState* exec) const
116{
117 VM& vm = exec->vm();
118 auto scope = DECLARE_THROW_SCOPE(vm);
119 JSObject* result = constructEmptyObject(exec);
120 RETURN_IF_EXCEPTION(scope, { });
121 result->putDirect(vm, vm.propertyNames->bytecodesID, jsNumber(m_bytecodes->id()));
122 result->putDirect(vm, vm.propertyNames->compilationKind, jsString(exec, String::fromUTF8(toCString(m_kind))));
123
124 JSArray* profiledBytecodes = constructEmptyArray(exec, 0);
125 RETURN_IF_EXCEPTION(scope, { });
126 for (unsigned i = 0; i < m_profiledBytecodes.size(); ++i) {
127 auto value = m_profiledBytecodes[i].toJS(exec);
128 RETURN_IF_EXCEPTION(scope, { });
129 profiledBytecodes->putDirectIndex(exec, i, value);
130 RETURN_IF_EXCEPTION(scope, { });
131 }
132 result->putDirect(vm, vm.propertyNames->profiledBytecodes, profiledBytecodes);
133
134 JSArray* descriptions = constructEmptyArray(exec, 0);
135 RETURN_IF_EXCEPTION(scope, { });
136 for (unsigned i = 0; i < m_descriptions.size(); ++i) {
137 auto value = m_descriptions[i].toJS(exec);
138 RETURN_IF_EXCEPTION(scope, { });
139 descriptions->putDirectIndex(exec, i, value);
140 RETURN_IF_EXCEPTION(scope, { });
141 }
142 result->putDirect(vm, vm.propertyNames->descriptions, descriptions);
143
144 JSArray* counters = constructEmptyArray(exec, 0);
145 RETURN_IF_EXCEPTION(scope, { });
146 for (auto it = m_counters.begin(), end = m_counters.end(); it != end; ++it) {
147 JSObject* counterEntry = constructEmptyObject(exec);
148 RETURN_IF_EXCEPTION(scope, { });
149 auto value = it->key.toJS(exec);
150 RETURN_IF_EXCEPTION(scope, { });
151 counterEntry->putDirect(vm, vm.propertyNames->origin, value);
152 counterEntry->putDirect(vm, vm.propertyNames->executionCount, jsNumber(it->value->count()));
153 counters->push(exec, counterEntry);
154 RETURN_IF_EXCEPTION(scope, { });
155 }
156 result->putDirect(vm, vm.propertyNames->counters, counters);
157
158 JSArray* exitSites = constructEmptyArray(exec, 0);
159 RETURN_IF_EXCEPTION(scope, { });
160 for (unsigned i = 0; i < m_osrExitSites.size(); ++i) {
161 auto value = m_osrExitSites[i].toJS(exec);
162 RETURN_IF_EXCEPTION(scope, { });
163 exitSites->putDirectIndex(exec, i, value);
164 RETURN_IF_EXCEPTION(scope, { });
165 }
166 result->putDirect(vm, vm.propertyNames->osrExitSites, exitSites);
167
168 JSArray* exits = constructEmptyArray(exec, 0);
169 RETURN_IF_EXCEPTION(scope, { });
170 for (unsigned i = 0; i < m_osrExits.size(); ++i) {
171 exits->putDirectIndex(exec, i, m_osrExits[i].toJS(exec));
172 RETURN_IF_EXCEPTION(scope, { });
173 }
174 result->putDirect(vm, vm.propertyNames->osrExits, exits);
175
176 result->putDirect(vm, vm.propertyNames->numInlinedGetByIds, jsNumber(m_numInlinedGetByIds));
177 result->putDirect(vm, vm.propertyNames->numInlinedPutByIds, jsNumber(m_numInlinedPutByIds));
178 result->putDirect(vm, vm.propertyNames->numInlinedCalls, jsNumber(m_numInlinedCalls));
179 result->putDirect(vm, vm.propertyNames->jettisonReason, jsString(exec, String::fromUTF8(toCString(m_jettisonReason))));
180 if (!m_additionalJettisonReason.isNull())
181 result->putDirect(vm, vm.propertyNames->additionalJettisonReason, jsString(exec, String::fromUTF8(m_additionalJettisonReason)));
182
183 result->putDirect(vm, vm.propertyNames->uid, m_uid.toJS(exec));
184
185 return result;
186}
187
188} } // namespace JSC::Profiler
189
190