sweeper.h 8.01 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_HEAP_SWEEPER_H_
#define V8_HEAP_SWEEPER_H_

#include <deque>
9
#include <map>
10 11 12
#include <vector>

#include "src/base/platform/semaphore.h"
13 14
#include "src/common/globals.h"
#include "src/tasks/cancelable-task.h"
15 16 17 18

namespace v8 {
namespace internal {

19
class InvalidatedSlotsCleanup;
20 21 22
class MajorNonAtomicMarkingState;
class Page;
class PagedSpace;
23
class Space;
24 25 26 27 28

enum FreeSpaceTreatmentMode { IGNORE_FREE_SPACE, ZAP_FREE_SPACE };

class Sweeper {
 public:
29
  using IterabilityList = std::vector<Page*>;
30
  using SweepingList = std::vector<Page*>;
31
  using SweptList = std::vector<Page*>;
32
  using FreeRangesMap = std::map<uint32_t, uint32_t>;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  // Pauses the sweeper tasks or completes sweeping.
  class PauseOrCompleteScope final {
   public:
    explicit PauseOrCompleteScope(Sweeper* sweeper);
    ~PauseOrCompleteScope();

   private:
    Sweeper* const sweeper_;
  };

  // Temporary filters old space sweeping lists. Requires the concurrent
  // sweeper to be paused. Allows for pages to be added to the sweeper while
  // in this scope. Note that the original list of sweeping pages is restored
  // after exiting this scope.
  class FilterSweepingPagesScope final {
   public:
50
    FilterSweepingPagesScope(
51 52 53 54 55 56 57
        Sweeper* sweeper, const PauseOrCompleteScope& pause_or_complete_scope);
    ~FilterSweepingPagesScope();

    template <typename Callback>
    void FilterOldSpaceSweepingPages(Callback callback) {
      if (!sweeping_in_progress_) return;

58 59
      SweepingList* sweeper_list =
          &sweeper_->sweeping_list_[GetSweepSpaceIndex(OLD_SPACE)];
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
      // Iteration here is from most free space to least free space.
      for (auto it = old_space_sweeping_list_.begin();
           it != old_space_sweeping_list_.end(); it++) {
        if (callback(*it)) {
          sweeper_list->push_back(*it);
        }
      }
    }

   private:
    Sweeper* const sweeper_;
    SweepingList old_space_sweeping_list_;
    const PauseOrCompleteScope& pause_or_complete_scope_;
    bool sweeping_in_progress_;
  };

  enum FreeListRebuildingMode { REBUILD_FREE_LIST, IGNORE_FREE_LIST };
  enum AddPageMode { REGULAR, READD_TEMPORARY_REMOVED_PAGE };
78
  enum class FreeSpaceMayContainInvalidatedSlots { kYes, kNo };
79

80
  Sweeper(Heap* heap, MajorNonAtomicMarkingState* marking_state);
81 82 83 84 85

  bool sweeping_in_progress() const { return sweeping_in_progress_; }

  void AddPage(AllocationSpace space, Page* page, AddPageMode mode);

86 87 88 89 90 91 92 93
  int ParallelSweepSpace(
      AllocationSpace identity, int required_freed_bytes, int max_pages = 0,
      FreeSpaceMayContainInvalidatedSlots invalidated_slots_in_free_space =
          FreeSpaceMayContainInvalidatedSlots::kNo);
  int ParallelSweepPage(
      Page* page, AllocationSpace identity,
      FreeSpaceMayContainInvalidatedSlots invalidated_slots_in_free_space =
          FreeSpaceMayContainInvalidatedSlots::kNo);
94 95 96

  void ScheduleIncrementalSweepingTask();

97 98 99
  int RawSweep(
      Page* p, FreeListRebuildingMode free_list_mode,
      FreeSpaceTreatmentMode free_space_mode,
100 101
      FreeSpaceMayContainInvalidatedSlots invalidated_slots_in_free_space,
      const base::MutexGuard& page_guard);
102 103 104 105 106

  // After calling this function sweeping is considered to be in progress
  // and the main thread can sweep lazily, but the background sweeper tasks
  // are not running yet.
  void StartSweeping();
107
  V8_EXPORT_PRIVATE void StartSweeperTasks();
108
  void EnsureCompleted();
109
  void DrainSweepingWorklists();
110
  void DrainSweepingWorklistForSpace(AllocationSpace space);
111 112
  bool AreSweeperTasksRunning();

113 114 115
  // Support concurrent sweepers from main thread
  void SupportConcurrentSweeping();

116 117
  Page* GetSweptPageSafe(PagedSpace* space);

118 119 120
  void AddPageForIterability(Page* page);
  void StartIterabilityTasks();
  void EnsureIterabilityCompleted();
121
  void MergeOldToNewRememberedSetsForSweptPages();
122

123 124
 private:
  class IncrementalSweeperTask;
125
  class IterabilityTask;
126 127
  class SweeperTask;

128 129
  static const int kNumberOfSweepingSpaces =
      LAST_GROWABLE_PAGED_SPACE - FIRST_GROWABLE_PAGED_SPACE + 1;
130
  static const int kMaxSweeperTasks = 3;
131 132

  template <typename Callback>
133 134 135 136
  void ForAllSweepingSpaces(Callback callback) const {
    callback(OLD_SPACE);
    callback(CODE_SPACE);
    callback(MAP_SPACE);
137 138
  }

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  // Helper function for RawSweep. Depending on the FreeListRebuildingMode and
  // FreeSpaceTreatmentMode this function may add the free memory to a free
  // list, make the memory iterable, clear it, and return the free memory to
  // the operating system.
  size_t FreeAndProcessFreedMemory(Address free_start, Address free_end,
                                   Page* page, Space* space,
                                   bool non_empty_typed_slots,
                                   FreeListRebuildingMode free_list_mode,
                                   FreeSpaceTreatmentMode free_space_mode);

  // Helper function for RawSweep. Handle remembered set entries in the freed
  // memory which require clearing.
  void CleanupRememberedSetEntriesForFreedMemory(
      Address free_start, Address free_end, Page* page,
      bool non_empty_typed_slots, FreeRangesMap* free_ranges_map,
      InvalidatedSlotsCleanup* old_to_new_cleanup);

  // Helper function for RawSweep. Clears invalid typed slots in the given free
  // ranges.
  void CleanupInvalidTypedSlotsOfFreeRanges(
      Page* page, const FreeRangesMap& free_ranges_map);

  // Helper function for RawSweep. Clears the mark bits and ensures consistency
  // of live bytes.
  void ClearMarkBitsAndHandleLivenessStatistics(
      Page* page, size_t live_bytes, FreeListRebuildingMode free_list_mode);

166 167
  // Can only be called on the main thread when no tasks are running.
  bool IsDoneSweeping() const {
168 169
    bool is_done = true;
    ForAllSweepingSpaces([this, &is_done](AllocationSpace space) {
170
      if (!sweeping_list_[GetSweepSpaceIndex(space)].empty()) is_done = false;
171 172
    });
    return is_done;
173 174 175 176 177 178 179 180 181 182 183 184 185 186
  }

  void SweepSpaceFromTask(AllocationSpace identity);

  // Sweeps incrementally one page from the given space. Returns true if
  // there are no more pages to sweep in the given space.
  bool SweepSpaceIncrementallyFromTask(AllocationSpace identity);

  void AbortAndWaitForTasks();

  Page* GetSweepingPageSafe(AllocationSpace space);

  void PrepareToBeSweptPage(AllocationSpace space, Page* page);

187 188 189
  void MakeIterable(Page* page);

  bool IsValidIterabilitySpace(AllocationSpace space) {
190
    return space == NEW_SPACE || space == RO_SPACE;
191 192
  }

193 194 195 196 197
  static bool IsValidSweepingSpace(AllocationSpace space) {
    return space >= FIRST_GROWABLE_PAGED_SPACE &&
           space <= LAST_GROWABLE_PAGED_SPACE;
  }

198 199 200 201 202
  static int GetSweepSpaceIndex(AllocationSpace space) {
    DCHECK(IsValidSweepingSpace(space));
    return space - FIRST_GROWABLE_PAGED_SPACE;
  }

203 204 205
  Heap* const heap_;
  MajorNonAtomicMarkingState* marking_state_;
  int num_tasks_;
206
  CancelableTaskManager::Id task_ids_[kNumberOfSweepingSpaces];
207 208
  base::Semaphore pending_sweeper_tasks_semaphore_;
  base::Mutex mutex_;
209 210
  SweptList swept_list_[kNumberOfSweepingSpaces];
  SweepingList sweeping_list_[kNumberOfSweepingSpaces];
211
  bool incremental_sweeper_pending_;
212 213 214
  // Main thread can finalize sweeping, while background threads allocation slow
  // path checks this flag to see whether it could support concurrent sweeping.
  std::atomic<bool> sweeping_in_progress_;
215 216
  // Counter is actively maintained by the concurrent tasks to avoid querying
  // the semaphore for maintaining a task counter on the main thread.
217
  std::atomic<intptr_t> num_sweeping_tasks_;
218
  // Used by PauseOrCompleteScope to signal early bailout to tasks.
219
  std::atomic<bool> stop_sweeper_tasks_;
220 221 222 223 224 225 226

  // Pages that are only made iterable but have their free lists ignored.
  IterabilityList iterability_list_;
  CancelableTaskManager::Id iterability_task_id_;
  base::Semaphore iterability_task_semaphore_;
  bool iterability_in_progress_;
  bool iterability_task_started_;
227
  bool should_reduce_memory_;
228 229 230 231 232 233
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_SWEEPER_H_