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