1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*
* Copyright (C) 2012-2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ExecutionCounter.h"
#include "CodeBlock.h"
#include "ExecutableAllocator.h"
#include "VMInlines.h"
#include <wtf/TZoneMallocInlines.h>
namespace JSC {
template<CountingVariant countingVariant>
ExecutionCounter<countingVariant>::ExecutionCounter()
{
reset();
}
template<CountingVariant countingVariant>
void ExecutionCounter<countingVariant>::forceSlowPathConcurrently()
{
m_counter = 0;
}
template<CountingVariant countingVariant>
bool ExecutionCounter<countingVariant>::checkIfThresholdCrossedAndSet(CodeBlock* codeBlock)
{
if (hasCrossedThreshold(codeBlock))
return true;
if (setThreshold(codeBlock))
return true;
return false;
}
template<CountingVariant countingVariant>
void ExecutionCounter<countingVariant>::setNewThreshold(int32_t threshold, CodeBlock* codeBlock)
{
reset();
m_activeThreshold = threshold;
setThreshold(codeBlock);
}
template<CountingVariant countingVariant>
void ExecutionCounter<countingVariant>::deferIndefinitely()
{
m_totalCount = 0;
m_activeThreshold = std::numeric_limits<int32_t>::max();
m_counter = std::numeric_limits<int32_t>::min();
}
double applyMemoryUsageHeuristics(int32_t value, CodeBlock* codeBlock)
{
double multiplier = 1.0;
if (codeBlock) {
#if ENABLE(JIT)
multiplier =
ExecutableAllocator::memoryPressureMultiplier(
codeBlock->baselineAlternative()->predictedMachineCodeSize());
#endif
}
ASSERT(multiplier >= 1.0);
return multiplier * value;
}
int32_t applyMemoryUsageHeuristicsAndConvertToInt(int32_t value, CodeBlock* codeBlock)
{
double doubleResult = applyMemoryUsageHeuristics(value, codeBlock);
ASSERT(doubleResult >= 0);
if (doubleResult > std::numeric_limits<int32_t>::max())
return std::numeric_limits<int32_t>::max();
return static_cast<int32_t>(doubleResult);
}
int32_t maximumExecutionCountsBetweenCheckpoints(CountingVariant countingVariant, CodeBlock* codeBlock)
{
switch (countingVariant) {
case CountingForBaseline: {
int32_t threshold = Options::maximumExecutionCountsBetweenCheckpointsForBaseline();
UNUSED_PARAM(codeBlock);
#if ENABLE(JIT)
if (codeBlock) {
// If the CodeBlock becomes particularly mega sized, then updating profiles become huge cost.
// We would like to avoid updating profiles repeatedly for that function, so we relax checkpoint period longer.
// We do not need to make checkpoint period longer for CountingForUpperTiers since they do not update profiles.
if (static_cast<int32_t>(codeBlock->bytecodeCost()) >= Options::highCostBaselineProfilingFunctionBytecodeCost()) {
double factor = std::max(std::sqrt(codeBlock->optimizationThresholdScalingFactor()), 1.0);
return toInt32(threshold * factor);
}
}
#endif
return threshold;
}
case CountingForUpperTiers:
return Options::maximumExecutionCountsBetweenCheckpointsForUpperTiers();
default:
RELEASE_ASSERT_NOT_REACHED();
return 0;
}
}
template<CountingVariant countingVariant>
bool ExecutionCounter<countingVariant>::hasCrossedThreshold(CodeBlock* codeBlock) const
{
// This checks if the current count rounded up to the threshold we were targeting.
// For example, if we are using half of available executable memory and have
// m_activeThreshold = 1000, applyMemoryUsageHeuristics(m_activeThreshold) will be
// 2000, but we will pretend as if the threshold was crossed if we reach 2000 -
// 1000 / 2, or 1500. The reasoning here is that we want to avoid thrashing. If
// this method returns false, then the JIT's threshold for when it will again call
// into the slow path (which will call this method a second time) will be set
// according to the difference between the current count and the target count
// according to *current* memory usage. But by the time we call into this again, we
// may have JIT'ed more code, and so the target count will increase slightly. This
// may lead to a repeating pattern where the target count is slightly incremented,
// the JIT immediately matches that increase, calls into the slow path again, and
// again the target count is slightly incremented. Instead of having this vicious
// cycle, we declare victory a bit early if the difference between the current
// total and our target according to memory heuristics is small. Our definition of
// small is arbitrarily picked to be half of the original threshold (i.e.
// m_activeThreshold).
double modifiedThreshold = applyMemoryUsageHeuristics(m_activeThreshold, codeBlock);
double actualCount = static_cast<double>(m_totalCount) + m_counter;
double desiredCount = modifiedThreshold - static_cast<double>(
std::min(m_activeThreshold, maximumExecutionCountsBetweenCheckpoints(countingVariant, codeBlock))) / 2;
bool result = actualCount >= desiredCount;
CODEBLOCK_LOG_EVENT(codeBlock, "thresholdCheck", ("activeThreshold = ", m_activeThreshold, ", modifiedThreshold = ", modifiedThreshold, ", actualCount = ", actualCount, ", desiredCount = ", desiredCount));
return result;
}
template<CountingVariant countingVariant>
bool ExecutionCounter<countingVariant>::setThreshold(CodeBlock* codeBlock)
{
if (m_activeThreshold == std::numeric_limits<int32_t>::max()) {
deferIndefinitely();
return false;
}
// Compute the true total count.
double trueTotalCount = count();
// Correct the threshold for current memory usage.
double threshold = applyMemoryUsageHeuristics(m_activeThreshold, codeBlock);
// Threshold must be non-negative and not NaN.
ASSERT(threshold >= 0);
// Adjust the threshold according to the number of executions we have already
// seen. This shouldn't go negative, but it might, because of round-off errors.
threshold -= trueTotalCount;
if (threshold <= 0) {
m_counter = 0;
m_totalCount = trueTotalCount;
return true;
}
threshold = clippedThreshold(codeBlock, threshold);
m_counter = static_cast<int32_t>(-threshold);
m_totalCount = trueTotalCount + threshold;
return false;
}
template<CountingVariant countingVariant>
void ExecutionCounter<countingVariant>::reset()
{
m_counter = 0;
m_totalCount = 0;
m_activeThreshold = 0;
}
template<CountingVariant countingVariant>
void ExecutionCounter<countingVariant>::dump(PrintStream& out) const
{
out.printf("%lf/%lf, %d", count(), static_cast<double>(m_activeThreshold), m_counter);
}
template class ExecutionCounter<CountingForBaseline>;
template class ExecutionCounter<CountingForUpperTiers>;
} // namespace JSC
|