ExecutionCounter.cpp [plain text]
#include "config.h"
#include "ExecutionCounter.h"
#include "CodeBlock.h"
#include "ExecutableAllocator.h"
#include "JSCInlines.h"
#include "VMInlines.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)
{
#if ENABLE(JIT)
double multiplier =
ExecutableAllocator::memoryPressureMultiplier(
codeBlock->baselineAlternative()->predictedMachineCodeSize());
#else
double multiplier = 1.0;
UNUSED_PARAM(codeBlock);
#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);
}
template<CountingVariant countingVariant>
bool ExecutionCounter<countingVariant>::hasCrossedThreshold(CodeBlock* codeBlock) const
{
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())) / 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;
}
double trueTotalCount = count();
double threshold = applyMemoryUsageHeuristics(m_activeThreshold, codeBlock);
ASSERT(threshold >= 0);
threshold -= trueTotalCount;
if (threshold <= 0) {
m_counter = 0;
m_totalCount = trueTotalCount;
return true;
}
threshold = clippedThreshold(codeBlock->globalObject(), 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>;
}