MarkStack.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2009, 2011 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 "MarkStack.h"
  27. #include "MarkStackInlines.h"
  28. #include "ConservativeRoots.h"
  29. #include "CopiedSpace.h"
  30. #include "CopiedSpaceInlines.h"
  31. #include "Heap.h"
  32. #include "JSArray.h"
  33. #include "JSCell.h"
  34. #include "JSObject.h"
  35. #include "SlotVisitorInlines.h"
  36. #include "Structure.h"
  37. #include "WriteBarrier.h"
  38. #include <wtf/Atomics.h>
  39. #include <wtf/DataLog.h>
  40. #include <wtf/MainThread.h>
  41. namespace JSC {
  42. COMPILE_ASSERT(MarkStackSegment::blockSize == WeakBlock::blockSize, blockSizeMatch);
  43. MarkStackArray::MarkStackArray(BlockAllocator& blockAllocator)
  44. : m_blockAllocator(blockAllocator)
  45. , m_top(0)
  46. , m_numberOfSegments(0)
  47. {
  48. m_segments.push(MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>()));
  49. m_numberOfSegments++;
  50. }
  51. MarkStackArray::~MarkStackArray()
  52. {
  53. ASSERT(m_numberOfSegments == 1 && m_segments.size() == 1);
  54. m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
  55. }
  56. void MarkStackArray::expand()
  57. {
  58. ASSERT(m_segments.head()->m_top == s_segmentCapacity);
  59. MarkStackSegment* nextSegment = MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>());
  60. m_numberOfSegments++;
  61. #if !ASSERT_DISABLED
  62. nextSegment->m_top = 0;
  63. #endif
  64. m_segments.push(nextSegment);
  65. setTopForEmptySegment();
  66. validatePrevious();
  67. }
  68. bool MarkStackArray::refill()
  69. {
  70. validatePrevious();
  71. if (top())
  72. return true;
  73. m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
  74. ASSERT(m_numberOfSegments > 1);
  75. m_numberOfSegments--;
  76. setTopForFullSegment();
  77. validatePrevious();
  78. return true;
  79. }
  80. void MarkStackArray::donateSomeCellsTo(MarkStackArray& other)
  81. {
  82. // Try to donate about 1 / 2 of our cells. To reduce copying costs,
  83. // we prefer donating whole segments over donating individual cells,
  84. // even if this skews away from our 1 / 2 target.
  85. size_t segmentsToDonate = m_numberOfSegments / 2; // If we only have one segment (our head) we don't donate any segments.
  86. if (!segmentsToDonate) {
  87. size_t cellsToDonate = m_top / 2; // Round down to donate 0 / 1 cells.
  88. while (cellsToDonate--) {
  89. ASSERT(m_top);
  90. other.append(removeLast());
  91. }
  92. return;
  93. }
  94. validatePrevious();
  95. other.validatePrevious();
  96. // Remove our head and the head of the other list before we start moving segments around.
  97. // We'll add them back on once we're done donating.
  98. MarkStackSegment* myHead = m_segments.removeHead();
  99. MarkStackSegment* otherHead = other.m_segments.removeHead();
  100. while (segmentsToDonate--) {
  101. MarkStackSegment* current = m_segments.removeHead();
  102. ASSERT(current);
  103. ASSERT(m_numberOfSegments > 1);
  104. other.m_segments.push(current);
  105. m_numberOfSegments--;
  106. other.m_numberOfSegments++;
  107. }
  108. // Put the original heads back in their places.
  109. m_segments.push(myHead);
  110. other.m_segments.push(otherHead);
  111. validatePrevious();
  112. other.validatePrevious();
  113. }
  114. void MarkStackArray::stealSomeCellsFrom(MarkStackArray& other, size_t idleThreadCount)
  115. {
  116. // Try to steal 1 / Nth of the shared array, where N is the number of idle threads.
  117. // To reduce copying costs, we prefer stealing a whole segment over stealing
  118. // individual cells, even if this skews away from our 1 / N target.
  119. validatePrevious();
  120. other.validatePrevious();
  121. // If other has an entire segment, steal it and return.
  122. if (other.m_numberOfSegments > 1) {
  123. // Move the heads of the lists aside. We'll push them back on after.
  124. MarkStackSegment* otherHead = other.m_segments.removeHead();
  125. MarkStackSegment* myHead = m_segments.removeHead();
  126. ASSERT(other.m_segments.head()->m_top == s_segmentCapacity);
  127. m_segments.push(other.m_segments.removeHead());
  128. m_numberOfSegments++;
  129. other.m_numberOfSegments--;
  130. m_segments.push(myHead);
  131. other.m_segments.push(otherHead);
  132. validatePrevious();
  133. other.validatePrevious();
  134. return;
  135. }
  136. size_t numberOfCellsToSteal = (other.size() + idleThreadCount - 1) / idleThreadCount; // Round up to steal 1 / 1.
  137. while (numberOfCellsToSteal-- > 0 && other.canRemoveLast())
  138. append(other.removeLast());
  139. }
  140. } // namespace JSC