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