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