summaryrefslogtreecommitdiff
path: root/deps/v8/src/heap/marking-worklist.cc
blob: d05540e2f8377e3a4ac2f043668973187919dec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2019 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.

#include "src/heap/marking-worklist.h"

#include <algorithm>
#include <cstddef>
#include <map>

#include "src/heap/cppgc-js/cpp-heap.h"
#include "src/heap/cppgc-js/cpp-marking-state.h"
#include "src/heap/marking-worklist-inl.h"
#include "src/objects/heap-object-inl.h"
#include "src/objects/heap-object.h"
#include "src/objects/instance-type-inl.h"
#include "src/objects/instance-type.h"
#include "src/objects/map.h"
#include "src/objects/objects-definitions.h"

namespace v8 {
namespace internal {

void MarkingWorklists::Clear() {
  shared_.Clear();
  on_hold_.Clear();
  wrapper_.Clear();
  other_.Clear();
  for (auto& cw : context_worklists_) {
    cw.worklist->Clear();
  }
  ReleaseContextWorklists();
}

void MarkingWorklists::Print() {
  PrintWorklist("shared", &shared_);
  PrintWorklist("on_hold", &on_hold_);
}

void MarkingWorklists::CreateContextWorklists(
    const std::vector<Address>& contexts) {
  DCHECK(context_worklists_.empty());
  if (contexts.empty()) return;

  context_worklists_.reserve(contexts.size());
  for (Address context : contexts) {
    context_worklists_.push_back(
        {context, std::make_unique<MarkingWorklist>()});
  }
}

void MarkingWorklists::ReleaseContextWorklists() { context_worklists_.clear(); }

void MarkingWorklists::PrintWorklist(const char* worklist_name,
                                     MarkingWorklist* worklist) {
#ifdef DEBUG
  std::map<InstanceType, int> count;
  int total_count = 0;
  worklist->Iterate([&count, &total_count](HeapObject obj) {
    ++total_count;
    count[obj.map().instance_type()]++;
  });
  std::vector<std::pair<int, InstanceType>> rank;
  rank.reserve(count.size());
  for (const auto& i : count) {
    rank.emplace_back(i.second, i.first);
  }
  std::map<InstanceType, std::string> instance_type_name;
#define INSTANCE_TYPE_NAME(name) instance_type_name[name] = #name;
  INSTANCE_TYPE_LIST(INSTANCE_TYPE_NAME)
#undef INSTANCE_TYPE_NAME
  std::sort(rank.begin(), rank.end(),
            std::greater<std::pair<int, InstanceType>>());
  PrintF("Worklist %s: %d\n", worklist_name, total_count);
  for (auto i : rank) {
    PrintF("  [%s]: %d\n", instance_type_name[i.second].c_str(), i.first);
  }
#endif
}

constexpr Address MarkingWorklists::Local::kSharedContext;
constexpr Address MarkingWorklists::Local::kOtherContext;
constexpr std::nullptr_t MarkingWorklists::Local::kNoCppMarkingState;

namespace {

inline std::unordered_map<Address, std::unique_ptr<MarkingWorklist::Local>>
GetLocalPerContextMarkingWorklists(bool is_per_context_mode,
                                   MarkingWorklists* global) {
  if (!is_per_context_mode) return {};

  std::unordered_map<Address, std::unique_ptr<MarkingWorklist::Local>>
      worklist_by_context;
  worklist_by_context.reserve(global->context_worklists().size());
  for (auto& cw : global->context_worklists()) {
    worklist_by_context[cw.context] =
        std::make_unique<MarkingWorklist::Local>(*cw.worklist.get());
  }
  return worklist_by_context;
}

}  // namespace

MarkingWorklists::Local::Local(
    MarkingWorklists* global,
    std::unique_ptr<CppMarkingState> cpp_marking_state)
    : active_(&shared_),
      shared_(*global->shared()),
      on_hold_(*global->on_hold()),
      wrapper_(*global->wrapper()),
      active_context_(kSharedContext),
      is_per_context_mode_(!global->context_worklists().empty()),
      worklist_by_context_(
          GetLocalPerContextMarkingWorklists(is_per_context_mode_, global)),
      other_(*global->other()),
      cpp_marking_state_(std::move(cpp_marking_state)) {}

void MarkingWorklists::Local::Publish() {
  shared_.Publish();
  on_hold_.Publish();
  wrapper_.Publish();
  other_.Publish();
  if (is_per_context_mode_) {
    for (auto& cw : worklist_by_context_) {
      cw.second->Publish();
    }
  }
  PublishWrapper();
}

bool MarkingWorklists::Local::IsEmpty() {
  // This function checks the on_hold_ worklist, so it works only for the main
  // thread.
  if (!active_->IsLocalEmpty() || !on_hold_.IsLocalEmpty() ||
      !active_->IsGlobalEmpty() || !on_hold_.IsGlobalEmpty()) {
    return false;
  }
  if (!is_per_context_mode_) {
    return true;
  }
  if (!shared_.IsLocalEmpty() || !other_.IsLocalEmpty() ||
      !shared_.IsGlobalEmpty() || !other_.IsGlobalEmpty()) {
    return false;
  }
  for (auto& cw : worklist_by_context_) {
    if (cw.first != active_context_ &&
        !(cw.second->IsLocalEmpty() && cw.second->IsGlobalEmpty())) {
      SwitchToContextImpl(cw.first, cw.second.get());
      return false;
    }
  }
  return true;
}

bool MarkingWorklists::Local::IsWrapperEmpty() const {
  if (cpp_marking_state_) {
    DCHECK(wrapper_.IsLocalAndGlobalEmpty());
    return cpp_marking_state_->IsLocalEmpty();
  }
  return wrapper_.IsLocalAndGlobalEmpty();
}

void MarkingWorklists::Local::ShareWork() {
  if (!active_->IsLocalEmpty() && active_->IsGlobalEmpty()) {
    active_->Publish();
  }
  if (is_per_context_mode_ && active_context_ != kSharedContext) {
    if (!shared_.IsLocalEmpty() && shared_.IsGlobalEmpty()) {
      shared_.Publish();
    }
  }
}

void MarkingWorklists::Local::MergeOnHold() { shared_.Merge(on_hold_); }

bool MarkingWorklists::Local::PopContext(HeapObject* object) {
  DCHECK(is_per_context_mode_);
  // As an optimization we first check only the local segments to avoid locks.
  for (auto& cw : worklist_by_context_) {
    if (cw.first != active_context_ && !cw.second->IsLocalEmpty()) {
      SwitchToContextImpl(cw.first, cw.second.get());
      return active_->Pop(object);
    }
  }
  // All local segments are empty. Check global segments.
  for (auto& cw : worklist_by_context_) {
    if (cw.first != active_context_ && cw.second->Pop(object)) {
      SwitchToContextImpl(cw.first, cw.second.get());
      return true;
    }
  }
  // All worklists are empty. Switch to the default shared worklist.
  SwitchToContext(kSharedContext);
  return false;
}

Address MarkingWorklists::Local::SwitchToContextSlow(Address context) {
  const auto& it = worklist_by_context_.find(context);
  if (V8_UNLIKELY(it == worklist_by_context_.end())) {
    // The context passed is not an actual context:
    // - Shared context that should use the explicit worklist.
    // - This context was created during marking and should use the other
    // bucket.
    if (context == kSharedContext) {
      SwitchToContextImpl(kSharedContext, &shared_);
    } else {
      SwitchToContextImpl(kOtherContext, &other_);
    }
  } else {
    SwitchToContextImpl(it->first, it->second.get());
  }
  return active_context_;
}

Address MarkingWorklists::Local::SwitchToSharedForTesting() {
  return SwitchToContext(kSharedContext);
}

}  // namespace internal
}  // namespace v8