IncrementalSweeper.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "IncrementalSweeper.h"
  27. #include "APIShims.h"
  28. #include "Heap.h"
  29. #include "JSObject.h"
  30. #include "JSString.h"
  31. #include "MarkedBlock.h"
  32. #include <wtf/HashSet.h>
  33. #include <wtf/WTFThreadData.h>
  34. namespace JSC {
  35. #if USE(CF) || PLATFORM(BLACKBERRY) || PLATFORM(QT)
  36. static const double sweepTimeSlice = .01; // seconds
  37. static const double sweepTimeTotal = .10;
  38. static const double sweepTimeMultiplier = 1.0 / sweepTimeTotal;
  39. #if USE(CF)
  40. IncrementalSweeper::IncrementalSweeper(Heap* heap, CFRunLoopRef runLoop)
  41. : HeapTimer(heap->vm(), runLoop)
  42. , m_currentBlockToSweepIndex(0)
  43. , m_blocksToSweep(heap->m_blockSnapshot)
  44. {
  45. }
  46. PassOwnPtr<IncrementalSweeper> IncrementalSweeper::create(Heap* heap)
  47. {
  48. return adoptPtr(new IncrementalSweeper(heap, CFRunLoopGetCurrent()));
  49. }
  50. void IncrementalSweeper::scheduleTimer()
  51. {
  52. CFRunLoopTimerSetNextFireDate(m_timer.get(), CFAbsoluteTimeGetCurrent() + (sweepTimeSlice * sweepTimeMultiplier));
  53. }
  54. void IncrementalSweeper::cancelTimer()
  55. {
  56. CFRunLoopTimerSetNextFireDate(m_timer.get(), CFAbsoluteTimeGetCurrent() + s_decade);
  57. }
  58. #elif PLATFORM(BLACKBERRY) || PLATFORM(QT)
  59. IncrementalSweeper::IncrementalSweeper(Heap* heap)
  60. : HeapTimer(heap->vm())
  61. , m_currentBlockToSweepIndex(0)
  62. , m_blocksToSweep(heap->m_blockSnapshot)
  63. {
  64. }
  65. PassOwnPtr<IncrementalSweeper> IncrementalSweeper::create(Heap* heap)
  66. {
  67. return adoptPtr(new IncrementalSweeper(heap));
  68. }
  69. void IncrementalSweeper::scheduleTimer()
  70. {
  71. #if PLATFORM(QT)
  72. m_timer.start(sweepTimeSlice * sweepTimeMultiplier * 1000, this);
  73. #else
  74. m_timer.start(sweepTimeSlice * sweepTimeMultiplier);
  75. #endif
  76. }
  77. void IncrementalSweeper::cancelTimer()
  78. {
  79. m_timer.stop();
  80. }
  81. #endif
  82. void IncrementalSweeper::doWork()
  83. {
  84. doSweep(WTF::monotonicallyIncreasingTime());
  85. }
  86. void IncrementalSweeper::doSweep(double sweepBeginTime)
  87. {
  88. while (m_currentBlockToSweepIndex < m_blocksToSweep.size()) {
  89. sweepNextBlock();
  90. double elapsedTime = WTF::monotonicallyIncreasingTime() - sweepBeginTime;
  91. if (elapsedTime < sweepTimeSlice)
  92. continue;
  93. scheduleTimer();
  94. return;
  95. }
  96. m_blocksToSweep.clear();
  97. cancelTimer();
  98. }
  99. void IncrementalSweeper::sweepNextBlock()
  100. {
  101. while (m_currentBlockToSweepIndex < m_blocksToSweep.size()) {
  102. MarkedBlock* block = m_blocksToSweep[m_currentBlockToSweepIndex++];
  103. if (!block->needsSweeping())
  104. continue;
  105. block->sweep();
  106. m_vm->heap.objectSpace().freeOrShrinkBlock(block);
  107. return;
  108. }
  109. }
  110. void IncrementalSweeper::startSweeping(Vector<MarkedBlock*>& blockSnapshot)
  111. {
  112. m_blocksToSweep = blockSnapshot;
  113. m_currentBlockToSweepIndex = 0;
  114. scheduleTimer();
  115. }
  116. void IncrementalSweeper::willFinishSweeping()
  117. {
  118. m_currentBlockToSweepIndex = 0;
  119. m_blocksToSweep.clear();
  120. if (m_vm)
  121. cancelTimer();
  122. }
  123. #else
  124. IncrementalSweeper::IncrementalSweeper(VM* vm)
  125. : HeapTimer(vm)
  126. {
  127. }
  128. void IncrementalSweeper::doWork()
  129. {
  130. }
  131. PassOwnPtr<IncrementalSweeper> IncrementalSweeper::create(Heap* heap)
  132. {
  133. return adoptPtr(new IncrementalSweeper(heap->vm()));
  134. }
  135. void IncrementalSweeper::startSweeping(Vector<MarkedBlock*>&)
  136. {
  137. }
  138. void IncrementalSweeper::willFinishSweeping()
  139. {
  140. }
  141. void IncrementalSweeper::sweepNextBlock()
  142. {
  143. }
  144. #endif
  145. } // namespace JSC