1 | /* |
2 | * Copyright (C) 2012-2019 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 | |
26 | #include "config.h" |
27 | #include "IncrementalSweeper.h" |
28 | |
29 | #include "Heap.h" |
30 | #include "JSObject.h" |
31 | #include "JSString.h" |
32 | #include "MarkedBlock.h" |
33 | #include "JSCInlines.h" |
34 | |
35 | namespace JSC { |
36 | |
37 | static constexpr Seconds sweepTimeSlice = 10_ms; |
38 | static constexpr double sweepTimeTotal = .10; |
39 | static constexpr double sweepTimeMultiplier = 1.0 / sweepTimeTotal; |
40 | |
41 | void IncrementalSweeper::scheduleTimer() |
42 | { |
43 | setTimeUntilFire(sweepTimeSlice * sweepTimeMultiplier); |
44 | } |
45 | |
46 | IncrementalSweeper::IncrementalSweeper(Heap* heap) |
47 | : Base(heap->vm()) |
48 | , m_currentDirectory(nullptr) |
49 | { |
50 | } |
51 | |
52 | void IncrementalSweeper::doWork(VM& vm) |
53 | { |
54 | doSweep(vm, MonotonicTime::now()); |
55 | } |
56 | |
57 | void IncrementalSweeper::doSweep(VM& vm, MonotonicTime sweepBeginTime) |
58 | { |
59 | while (sweepNextBlock(vm)) { |
60 | Seconds elapsedTime = MonotonicTime::now() - sweepBeginTime; |
61 | if (elapsedTime < sweepTimeSlice) |
62 | continue; |
63 | |
64 | scheduleTimer(); |
65 | return; |
66 | } |
67 | |
68 | if (m_shouldFreeFastMallocMemoryAfterSweeping) { |
69 | WTF::releaseFastMallocFreeMemory(); |
70 | m_shouldFreeFastMallocMemoryAfterSweeping = false; |
71 | } |
72 | cancelTimer(); |
73 | } |
74 | |
75 | bool IncrementalSweeper::sweepNextBlock(VM& vm) |
76 | { |
77 | vm.heap.stopIfNecessary(); |
78 | |
79 | MarkedBlock::Handle* block = nullptr; |
80 | |
81 | for (; m_currentDirectory; m_currentDirectory = m_currentDirectory->nextDirectory()) { |
82 | block = m_currentDirectory->findBlockToSweep(); |
83 | if (block) |
84 | break; |
85 | } |
86 | |
87 | if (block) { |
88 | DeferGCForAWhile deferGC(vm.heap); |
89 | block->sweep(nullptr); |
90 | vm.heap.objectSpace().freeOrShrinkBlock(block); |
91 | return true; |
92 | } |
93 | |
94 | return vm.heap.sweepNextLogicallyEmptyWeakBlock(); |
95 | } |
96 | |
97 | void IncrementalSweeper::startSweeping(Heap& heap) |
98 | { |
99 | scheduleTimer(); |
100 | m_currentDirectory = heap.objectSpace().firstDirectory(); |
101 | } |
102 | |
103 | void IncrementalSweeper::stopSweeping() |
104 | { |
105 | m_currentDirectory = nullptr; |
106 | cancelTimer(); |
107 | } |
108 | |
109 | } // namespace JSC |
110 | |