marking.h 15 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
#ifndef V8_HEAP_MARKING_H_
#define V8_HEAP_MARKING_H_
7

8
#include "src/base/atomic-utils.h"
9
#include "src/utils/utils.h"
10 11 12 13 14 15

namespace v8 {
namespace internal {

class MarkBit {
 public:
16
  using CellType = uint32_t;
17
  STATIC_ASSERT(sizeof(CellType) == sizeof(base::Atomic32));
18

19
  inline MarkBit(CellType* cell, CellType mask) : cell_(cell), mask_(mask) {}
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#ifdef DEBUG
  bool operator==(const MarkBit& other) {
    return cell_ == other.cell_ && mask_ == other.mask_;
  }
#endif

 private:
  inline MarkBit Next() {
    CellType new_mask = mask_ << 1;
    if (new_mask == 0) {
      return MarkBit(cell_ + 1, 1);
    } else {
      return MarkBit(cell_, new_mask);
    }
  }

37 38
  // The function returns true if it succeeded to
  // transition the bit from 0 to 1.
39
  template <AccessMode mode = AccessMode::NON_ATOMIC>
40
  inline bool Set();
41

42
  template <AccessMode mode = AccessMode::NON_ATOMIC>
43
  inline bool Get();
44

45 46
  // The function returns true if it succeeded to
  // transition the bit from 1 to 0.
47
  template <AccessMode mode = AccessMode::NON_ATOMIC>
48
  inline bool Clear();
49

50 51
  CellType* cell_;
  CellType mask_;
52 53

  friend class IncrementalMarking;
54
  friend class ConcurrentMarkingMarkbits;
55 56 57
  friend class Marking;
};

58
template <>
59
inline bool MarkBit::Set<AccessMode::NON_ATOMIC>() {
60
  CellType old_value = *cell_;
61
  if ((old_value & mask_) == mask_) return false;
62
  *cell_ = old_value | mask_;
63
  return true;
64 65 66
}

template <>
67
inline bool MarkBit::Set<AccessMode::ATOMIC>() {
68
  return base::AsAtomic32::SetBits(cell_, mask_, mask_);
69 70 71
}

template <>
72
inline bool MarkBit::Get<AccessMode::NON_ATOMIC>() {
73
  return (*cell_ & mask_) != 0;
74 75 76
}

template <>
77
inline bool MarkBit::Get<AccessMode::ATOMIC>() {
78
  return (base::AsAtomic32::Acquire_Load(cell_) & mask_) != 0;
79 80 81
}

template <>
82
inline bool MarkBit::Clear<AccessMode::NON_ATOMIC>() {
83
  CellType old_value = *cell_;
84 85
  *cell_ = old_value & ~mask_;
  return (old_value & mask_) == mask_;
86 87 88
}

template <>
89
inline bool MarkBit::Clear<AccessMode::ATOMIC>() {
90
  return base::AsAtomic32::SetBits(cell_, 0u, mask_);
91 92
}

93
// Bitmap is a sequence of cells each containing fixed number of bits.
94
class V8_EXPORT_PRIVATE Bitmap {
95 96 97 98 99 100 101
 public:
  static const uint32_t kBitsPerCell = 32;
  static const uint32_t kBitsPerCellLog2 = 5;
  static const uint32_t kBitIndexMask = kBitsPerCell - 1;
  static const uint32_t kBytesPerCell = kBitsPerCell / kBitsPerByte;
  static const uint32_t kBytesPerCellLog2 = kBitsPerCellLog2 - kBitsPerByteLog2;

102 103 104 105 106 107 108 109
  // The length is the number of bits in this bitmap. (+1) accounts for
  // the case where the markbits are queried for a one-word filler at the
  // end of the page.
  static const size_t kLength = ((1 << kPageSizeBits) >> kTaggedSizeLog2) + 1;
  // The size of the bitmap in bytes is CellsCount() * kBytesPerCell.
  static const size_t kSize;

  static constexpr size_t CellsForLength(int length) {
110 111 112
    return (length + kBitsPerCell - 1) >> kBitsPerCellLog2;
  }

113
  static constexpr size_t CellsCount() { return CellsForLength(kLength); }
114

115
  V8_INLINE static uint32_t IndexToCell(uint32_t index) {
116 117 118 119 120 121 122
    return index >> kBitsPerCellLog2;
  }

  V8_INLINE static uint32_t IndexInCell(uint32_t index) {
    return index & kBitIndexMask;
  }

123 124 125
  // Retrieves the cell containing the provided markbit index.
  V8_INLINE static uint32_t CellAlignIndex(uint32_t index) {
    return index & ~kBitIndexMask;
126 127
  }

128
  V8_INLINE MarkBit::CellType* cells() {
129 130 131
    return reinterpret_cast<MarkBit::CellType*>(this);
  }

132
  V8_INLINE static Bitmap* FromAddress(Address addr) {
133 134 135 136 137 138
    return reinterpret_cast<Bitmap*>(addr);
  }

  inline MarkBit MarkBitFromIndex(uint32_t index) {
    MarkBit::CellType mask = 1u << IndexInCell(index);
    MarkBit::CellType* cell = this->cells() + (index >> kBitsPerCellLog2);
139
    return MarkBit(cell, mask);
140
  }
141
};
142

143 144 145
template <AccessMode mode>
class ConcurrentBitmap : public Bitmap {
 public:
146
  void Clear();
147

148 149
  void MarkAllBits();

150 151 152 153 154 155 156 157
  // Clears bits in the given cell. The mask specifies bits to clear: if a
  // bit is set in the mask then the corresponding bit is cleared in the cell.
  void ClearBitsInCell(uint32_t cell_index, uint32_t mask);

  // Sets bits in the given cell. The mask specifies bits to set: if a
  // bit is set in the mask then the corresponding bit is set in the cell.
  void SetBitsInCell(uint32_t cell_index, uint32_t mask);

158 159 160
  // Sets all bits in the range [start_index, end_index). If the access is
  // atomic, the cells at the boundary of the range are updated with atomic
  // compare and swap operation. The inner cells are updated with relaxed write.
161
  void SetRange(uint32_t start_index, uint32_t end_index);
162

163 164 165
  // Clears all bits in the range [start_index, end_index). If the access is
  // atomic, the cells at the boundary of the range are updated with atomic
  // compare and swap operation. The inner cells are updated with relaxed write.
166
  void ClearRange(uint32_t start_index, uint32_t end_index);
167

168 169 170
  // The following methods are *not* safe to use in a concurrent context so they
  // are not implemented for `AccessMode::ATOMIC`.

171
  // Returns true if all bits in the range [start_index, end_index) are set.
172
  bool AllBitsSetInRange(uint32_t start_index, uint32_t end_index);
173 174

  // Returns true if all bits in the range [start_index, end_index) are cleared.
175
  bool AllBitsClearInRange(uint32_t start_index, uint32_t end_index);
176

177
  void Print();
178

179
  bool IsClean();
180 181 182 183 184 185 186 187 188 189

 private:
  // Clear all bits in the cell range [start_cell_index, end_cell_index). If the
  // access is atomic then *still* use a relaxed memory ordering.
  void ClearCellRangeRelaxed(uint32_t start_cell_index,
                             uint32_t end_cell_index);

  // Set all bits in the cell range [start_cell_index, end_cell_index). If the
  // access is atomic then *still* use a relaxed memory ordering.
  void SetCellRangeRelaxed(uint32_t start_cell_index, uint32_t end_cell_index);
190 191
};

192
template <>
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
inline void ConcurrentBitmap<AccessMode::ATOMIC>::ClearCellRangeRelaxed(
    uint32_t start_cell_index, uint32_t end_cell_index) {
  base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
  for (uint32_t i = start_cell_index; i < end_cell_index; i++) {
    base::Relaxed_Store(cell_base + i, 0);
  }
}

template <>
inline void ConcurrentBitmap<AccessMode::NON_ATOMIC>::ClearCellRangeRelaxed(
    uint32_t start_cell_index, uint32_t end_cell_index) {
  for (uint32_t i = start_cell_index; i < end_cell_index; i++) {
    cells()[i] = 0;
  }
}

template <>
inline void ConcurrentBitmap<AccessMode::ATOMIC>::SetCellRangeRelaxed(
    uint32_t start_cell_index, uint32_t end_cell_index) {
  base::Atomic32* cell_base = reinterpret_cast<base::Atomic32*>(cells());
  for (uint32_t i = start_cell_index; i < end_cell_index; i++) {
    base::Relaxed_Store(cell_base + i, 0xffffffff);
  }
}

template <>
inline void ConcurrentBitmap<AccessMode::NON_ATOMIC>::SetCellRangeRelaxed(
    uint32_t start_cell_index, uint32_t end_cell_index) {
  for (uint32_t i = start_cell_index; i < end_cell_index; i++) {
    cells()[i] = 0xffffffff;
  }
}

template <AccessMode mode>
inline void ConcurrentBitmap<mode>::Clear() {
  ClearCellRangeRelaxed(0, CellsCount());
  if (mode == AccessMode::ATOMIC) {
    // This fence prevents re-ordering of publishing stores with the mark-bit
    // setting stores.
    base::SeqCst_MemoryFence();
  }
}

template <AccessMode mode>
inline void ConcurrentBitmap<mode>::MarkAllBits() {
  SetCellRangeRelaxed(0, CellsCount());
  if (mode == AccessMode::ATOMIC) {
    // This fence prevents re-ordering of publishing stores with the mark-bit
    // setting stores.
    base::SeqCst_MemoryFence();
  }
}

template <>
inline void ConcurrentBitmap<AccessMode::NON_ATOMIC>::SetBitsInCell(
    uint32_t cell_index, uint32_t mask) {
249 250 251 252
  cells()[cell_index] |= mask;
}

template <>
253 254
inline void ConcurrentBitmap<AccessMode::ATOMIC>::SetBitsInCell(
    uint32_t cell_index, uint32_t mask) {
255
  base::AsAtomic32::SetBits(cells() + cell_index, mask, mask);
256 257 258
}

template <>
259 260
inline void ConcurrentBitmap<AccessMode::NON_ATOMIC>::ClearBitsInCell(
    uint32_t cell_index, uint32_t mask) {
261 262 263 264
  cells()[cell_index] &= ~mask;
}

template <>
265 266
inline void ConcurrentBitmap<AccessMode::ATOMIC>::ClearBitsInCell(
    uint32_t cell_index, uint32_t mask) {
267
  base::AsAtomic32::SetBits(cells() + cell_index, 0u, mask);
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
template <AccessMode mode>
void ConcurrentBitmap<mode>::SetRange(uint32_t start_index,
                                      uint32_t end_index) {
  if (start_index >= end_index) return;
  end_index--;

  unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
  MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);

  unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
  MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);

  if (start_cell_index != end_cell_index) {
    // Firstly, fill all bits from the start address to the end of the first
    // cell with 1s.
    SetBitsInCell(start_cell_index, ~(start_index_mask - 1));
    // Then fill all in between cells with 1s.
    SetCellRangeRelaxed(start_cell_index + 1, end_cell_index);
    // Finally, fill all bits until the end address in the last cell with 1s.
    SetBitsInCell(end_cell_index, end_index_mask | (end_index_mask - 1));
  } else {
    SetBitsInCell(start_cell_index,
                  end_index_mask | (end_index_mask - start_index_mask));
  }
  if (mode == AccessMode::ATOMIC) {
    // This fence prevents re-ordering of publishing stores with the mark-bit
    // setting stores.
    base::SeqCst_MemoryFence();
  }
}

template <AccessMode mode>
void ConcurrentBitmap<mode>::ClearRange(uint32_t start_index,
                                        uint32_t end_index) {
  if (start_index >= end_index) return;
  end_index--;

  unsigned int start_cell_index = start_index >> Bitmap::kBitsPerCellLog2;
  MarkBit::CellType start_index_mask = 1u << Bitmap::IndexInCell(start_index);

  unsigned int end_cell_index = end_index >> Bitmap::kBitsPerCellLog2;
  MarkBit::CellType end_index_mask = 1u << Bitmap::IndexInCell(end_index);

  if (start_cell_index != end_cell_index) {
    // Firstly, fill all bits from the start address to the end of the first
    // cell with 0s.
    ClearBitsInCell(start_cell_index, ~(start_index_mask - 1));
    // Then fill all in between cells with 0s.
    ClearCellRangeRelaxed(start_cell_index + 1, end_cell_index);
    // Finally, set all bits until the end address in the last cell with 0s.
    ClearBitsInCell(end_cell_index, end_index_mask | (end_index_mask - 1));
  } else {
    ClearBitsInCell(start_cell_index,
                    end_index_mask | (end_index_mask - start_index_mask));
  }
  if (mode == AccessMode::ATOMIC) {
    // This fence prevents re-ordering of publishing stores with the mark-bit
    // clearing stores.
    base::SeqCst_MemoryFence();
  }
}

template <>
V8_EXPORT_PRIVATE bool
ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsSetInRange(
    uint32_t start_index, uint32_t end_index);

template <>
V8_EXPORT_PRIVATE bool
ConcurrentBitmap<AccessMode::NON_ATOMIC>::AllBitsClearInRange(
    uint32_t start_index, uint32_t end_index);

template <>
void ConcurrentBitmap<AccessMode::NON_ATOMIC>::Print();

template <>
V8_EXPORT_PRIVATE bool ConcurrentBitmap<AccessMode::NON_ATOMIC>::IsClean();

348 349
class Marking : public AllStatic {
 public:
350 351 352 353
  // TODO(hpayer): The current mark bit operations use as default NON_ATOMIC
  // mode for access. We should remove the default value or switch it with
  // ATOMIC as soon we add concurrency.

354 355
  // Impossible markbits: 01
  static const char* kImpossibleBitPattern;
356
  template <AccessMode mode = AccessMode::NON_ATOMIC>
357
  V8_INLINE static bool IsImpossible(MarkBit mark_bit) {
358
    if (mode == AccessMode::NON_ATOMIC) {
359 360 361 362 363 364 365 366 367 368 369
      return !mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
    }
    // If we are in concurrent mode we can only tell if an object has the
    // impossible bit pattern if we read the first bit again after reading
    // the first and the second bit. If the first bit is till zero and the
    // second bit is one then the object has the impossible bit pattern.
    bool is_impossible = !mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
    if (is_impossible) {
      return !mark_bit.Get<mode>();
    }
    return false;
370 371 372 373
  }

  // Black markbits: 11
  static const char* kBlackBitPattern;
374
  template <AccessMode mode = AccessMode::NON_ATOMIC>
375
  V8_INLINE static bool IsBlack(MarkBit mark_bit) {
376
    return mark_bit.Get<mode>() && mark_bit.Next().Get<mode>();
377 378 379 380
  }

  // White markbits: 00 - this is required by the mark bit clearer.
  static const char* kWhiteBitPattern;
381
  template <AccessMode mode = AccessMode::NON_ATOMIC>
382
  V8_INLINE static bool IsWhite(MarkBit mark_bit) {
383
    DCHECK(!IsImpossible<mode>(mark_bit));
384
    return !mark_bit.Get<mode>();
385 386 387 388
  }

  // Grey markbits: 10
  static const char* kGreyBitPattern;
389
  template <AccessMode mode = AccessMode::NON_ATOMIC>
390
  V8_INLINE static bool IsGrey(MarkBit mark_bit) {
391
    return mark_bit.Get<mode>() && !mark_bit.Next().Get<mode>();
392 393 394 395
  }

  // IsBlackOrGrey assumes that the first bit is set for black or grey
  // objects.
396
  template <AccessMode mode = AccessMode::NON_ATOMIC>
397
  V8_INLINE static bool IsBlackOrGrey(MarkBit mark_bit) {
398 399
    return mark_bit.Get<mode>();
  }
400

401
  template <AccessMode mode = AccessMode::NON_ATOMIC>
402
  V8_INLINE static void MarkWhite(MarkBit markbit) {
403
    STATIC_ASSERT(mode == AccessMode::NON_ATOMIC);
404 405
    markbit.Clear<mode>();
    markbit.Next().Clear<mode>();
406 407
  }

408 409 410
  // Warning: this method is not safe in general in concurrent scenarios.
  // If you know that nobody else will change the bits on the given location
  // then you may use it.
411
  template <AccessMode mode = AccessMode::NON_ATOMIC>
412
  V8_INLINE static void MarkBlack(MarkBit markbit) {
413 414
    markbit.Set<mode>();
    markbit.Next().Set<mode>();
415 416
  }

417
  template <AccessMode mode = AccessMode::NON_ATOMIC>
418
  V8_INLINE static bool WhiteToGrey(MarkBit markbit) {
419
    return markbit.Set<mode>();
420 421
  }

422
  template <AccessMode mode = AccessMode::NON_ATOMIC>
423
  V8_INLINE static bool WhiteToBlack(MarkBit markbit) {
424
    return markbit.Set<mode>() && markbit.Next().Set<mode>();
425 426
  }

427
  template <AccessMode mode = AccessMode::NON_ATOMIC>
428
  V8_INLINE static bool GreyToBlack(MarkBit markbit) {
429
    return markbit.Get<mode>() && markbit.Next().Set<mode>();
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  }

  enum ObjectColor {
    BLACK_OBJECT,
    WHITE_OBJECT,
    GREY_OBJECT,
    IMPOSSIBLE_COLOR
  };

  static const char* ColorName(ObjectColor color) {
    switch (color) {
      case BLACK_OBJECT:
        return "black";
      case WHITE_OBJECT:
        return "white";
      case GREY_OBJECT:
        return "grey";
      case IMPOSSIBLE_COLOR:
        return "impossible";
    }
    return "error";
  }

  static ObjectColor Color(MarkBit mark_bit) {
    if (IsBlack(mark_bit)) return BLACK_OBJECT;
    if (IsWhite(mark_bit)) return WHITE_OBJECT;
    if (IsGrey(mark_bit)) return GREY_OBJECT;
    UNREACHABLE();
  }

 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(Marking);
};

}  // namespace internal
}  // namespace v8

467
#endif  // V8_HEAP_MARKING_H_