]> git.saurik.com Git - apple/javascriptcore.git/blame - bytecode/ExecutionCounter.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / bytecode / ExecutionCounter.cpp
CommitLineData
6fe7ccc8 1/*
81345200 2 * Copyright (C) 2012, 2014 Apple Inc. All rights reserved.
6fe7ccc8
A
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 "ExecutionCounter.h"
28
29#include "CodeBlock.h"
30#include "ExecutableAllocator.h"
81345200 31#include "JSCInlines.h"
93a37866 32#include <wtf/StringExtras.h>
6fe7ccc8
A
33
34namespace JSC {
35
81345200
A
36template<CountingVariant countingVariant>
37ExecutionCounter<countingVariant>::ExecutionCounter()
6fe7ccc8
A
38{
39 reset();
40}
41
81345200
A
42template<CountingVariant countingVariant>
43void ExecutionCounter<countingVariant>::forceSlowPathConcurrently()
44{
45 m_counter = 0;
46}
47
48template<CountingVariant countingVariant>
49bool ExecutionCounter<countingVariant>::checkIfThresholdCrossedAndSet(CodeBlock* codeBlock)
6fe7ccc8
A
50{
51 if (hasCrossedThreshold(codeBlock))
52 return true;
53
54 if (setThreshold(codeBlock))
55 return true;
56
57 return false;
58}
59
81345200
A
60template<CountingVariant countingVariant>
61void ExecutionCounter<countingVariant>::setNewThreshold(int32_t threshold, CodeBlock* codeBlock)
6fe7ccc8
A
62{
63 reset();
64 m_activeThreshold = threshold;
65 setThreshold(codeBlock);
66}
67
81345200
A
68template<CountingVariant countingVariant>
69void ExecutionCounter<countingVariant>::deferIndefinitely()
6fe7ccc8
A
70{
71 m_totalCount = 0;
72 m_activeThreshold = std::numeric_limits<int32_t>::max();
73 m_counter = std::numeric_limits<int32_t>::min();
74}
75
81345200 76double applyMemoryUsageHeuristics(int32_t value, CodeBlock* codeBlock)
6fe7ccc8
A
77{
78#if ENABLE(JIT)
79 double multiplier =
80 ExecutableAllocator::memoryPressureMultiplier(
81 codeBlock->predictedMachineCodeSize());
82#else
83 // This code path will probably not be taken, but if it is, we fake it.
84 double multiplier = 1.0;
85 UNUSED_PARAM(codeBlock);
86#endif
87 ASSERT(multiplier >= 1.0);
88 return multiplier * value;
89}
90
81345200 91int32_t applyMemoryUsageHeuristicsAndConvertToInt(int32_t value, CodeBlock* codeBlock)
6fe7ccc8
A
92{
93 double doubleResult = applyMemoryUsageHeuristics(value, codeBlock);
94
95 ASSERT(doubleResult >= 0);
96
97 if (doubleResult > std::numeric_limits<int32_t>::max())
98 return std::numeric_limits<int32_t>::max();
99
100 return static_cast<int32_t>(doubleResult);
101}
102
81345200
A
103template<CountingVariant countingVariant>
104bool ExecutionCounter<countingVariant>::hasCrossedThreshold(CodeBlock* codeBlock) const
6fe7ccc8
A
105{
106 // This checks if the current count rounded up to the threshold we were targeting.
107 // For example, if we are using half of available executable memory and have
108 // m_activeThreshold = 1000, applyMemoryUsageHeuristics(m_activeThreshold) will be
109 // 2000, but we will pretend as if the threshold was crossed if we reach 2000 -
110 // 1000 / 2, or 1500. The reasoning here is that we want to avoid thrashing. If
111 // this method returns false, then the JIT's threshold for when it will again call
112 // into the slow path (which will call this method a second time) will be set
113 // according to the difference between the current count and the target count
114 // according to *current* memory usage. But by the time we call into this again, we
115 // may have JIT'ed more code, and so the target count will increase slightly. This
116 // may lead to a repeating pattern where the target count is slightly incremented,
117 // the JIT immediately matches that increase, calls into the slow path again, and
118 // again the target count is slightly incremented. Instead of having this vicious
119 // cycle, we declare victory a bit early if the difference between the current
120 // total and our target according to memory heuristics is small. Our definition of
121 // small is arbitrarily picked to be half of the original threshold (i.e.
122 // m_activeThreshold).
123
124 double modifiedThreshold = applyMemoryUsageHeuristics(m_activeThreshold, codeBlock);
125
126 return static_cast<double>(m_totalCount) + m_counter >=
93a37866 127 modifiedThreshold - static_cast<double>(
81345200 128 std::min(m_activeThreshold, maximumExecutionCountsBetweenCheckpoints())) / 2;
6fe7ccc8
A
129}
130
81345200
A
131template<CountingVariant countingVariant>
132bool ExecutionCounter<countingVariant>::setThreshold(CodeBlock* codeBlock)
6fe7ccc8
A
133{
134 if (m_activeThreshold == std::numeric_limits<int32_t>::max()) {
135 deferIndefinitely();
136 return false;
137 }
138
6fe7ccc8 139 // Compute the true total count.
93a37866 140 double trueTotalCount = count();
81345200 141
6fe7ccc8
A
142 // Correct the threshold for current memory usage.
143 double threshold = applyMemoryUsageHeuristics(m_activeThreshold, codeBlock);
144
145 // Threshold must be non-negative and not NaN.
146 ASSERT(threshold >= 0);
147
148 // Adjust the threshold according to the number of executions we have already
149 // seen. This shouldn't go negative, but it might, because of round-off errors.
150 threshold -= trueTotalCount;
151
152 if (threshold <= 0) {
153 m_counter = 0;
154 m_totalCount = trueTotalCount;
155 return true;
156 }
157
93a37866
A
158 threshold = clippedThreshold(codeBlock->globalObject(), threshold);
159
6fe7ccc8
A
160 m_counter = static_cast<int32_t>(-threshold);
161
162 m_totalCount = trueTotalCount + threshold;
163
164 return false;
165}
166
81345200
A
167template<CountingVariant countingVariant>
168void ExecutionCounter<countingVariant>::reset()
6fe7ccc8
A
169{
170 m_counter = 0;
171 m_totalCount = 0;
172 m_activeThreshold = 0;
173}
174
81345200
A
175template<CountingVariant countingVariant>
176void ExecutionCounter<countingVariant>::dump(PrintStream& out) const
93a37866
A
177{
178 out.printf("%lf/%lf, %d", count(), static_cast<double>(m_activeThreshold), m_counter);
179}
180
81345200
A
181template class ExecutionCounter<CountingForBaseline>;
182template class ExecutionCounter<CountingForUpperTiers>;
183
6fe7ccc8
A
184} // namespace JSC
185