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