ExecutionCounter.cpp 6.0 KB

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