LCOV - code coverage report
Current view: top level - src/heap - store-buffer.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 101 104 97.1 %
Date: 2019-01-20 Functions: 15 15 100.0 %

          Line data    Source code
       1             : // Copyright 2011 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/heap/store-buffer.h"
       6             : 
       7             : #include <algorithm>
       8             : 
       9             : #include "src/base/bits.h"
      10             : #include "src/base/macros.h"
      11             : #include "src/base/template-utils.h"
      12             : #include "src/counters.h"
      13             : #include "src/heap/incremental-marking.h"
      14             : #include "src/heap/store-buffer-inl.h"
      15             : #include "src/isolate.h"
      16             : #include "src/objects-inl.h"
      17             : #include "src/v8.h"
      18             : 
      19             : namespace v8 {
      20             : namespace internal {
      21             : 
      22       62883 : StoreBuffer::StoreBuffer(Heap* heap)
      23       62883 :     : heap_(heap), top_(nullptr), current_(0), mode_(NOT_IN_GC) {
      24      188649 :   for (int i = 0; i < kStoreBuffers; i++) {
      25      125766 :     start_[i] = nullptr;
      26      125766 :     limit_[i] = nullptr;
      27      125766 :     lazy_top_[i] = nullptr;
      28             :   }
      29       62883 :   task_running_ = false;
      30       62883 :   insertion_callback = &InsertDuringRuntime;
      31       62883 :   deletion_callback = &DeleteDuringRuntime;
      32       62883 : }
      33             : 
      34       62883 : void StoreBuffer::SetUp() {
      35       62883 :   v8::PageAllocator* page_allocator = GetPlatformPageAllocator();
      36             :   // Round up the requested size in order to fulfill the VirtualMemory's
      37             :   // requrements on the requested size alignment. This may cause a bit of
      38             :   // memory wastage if the actual CommitPageSize() will be bigger than the
      39             :   // kMinExpectedOSPageSize value but this is a trade-off for keeping the
      40             :   // store buffer overflow check in write barriers cheap.
      41             :   const size_t requested_size = RoundUp(kStoreBufferSize * kStoreBuffers,
      42      125766 :                                         page_allocator->CommitPageSize());
      43             :   // Allocate buffer memory aligned at least to kStoreBufferSize. This lets us
      44             :   // use a bit test to detect the ends of the buffers.
      45             :   STATIC_ASSERT(base::bits::IsPowerOfTwo(kStoreBufferSize));
      46             :   const size_t alignment =
      47      125766 :       std::max<size_t>(kStoreBufferSize, page_allocator->AllocatePageSize());
      48             :   void* hint = AlignedAddress(heap_->GetRandomMmapAddr(), alignment);
      49       62883 :   VirtualMemory reservation(page_allocator, requested_size, hint, alignment);
      50       62883 :   if (!reservation.IsReserved()) {
      51           0 :     heap_->FatalProcessOutOfMemory("StoreBuffer::SetUp");
      52             :   }
      53             : 
      54             :   Address start = reservation.address();
      55             :   const size_t allocated_size = reservation.size();
      56             : 
      57       62883 :   start_[0] = reinterpret_cast<Address*>(start);
      58       62883 :   limit_[0] = start_[0] + (kStoreBufferSize / kSystemPointerSize);
      59       62883 :   start_[1] = limit_[0];
      60       62883 :   limit_[1] = start_[1] + (kStoreBufferSize / kSystemPointerSize);
      61             : 
      62             :   // Sanity check the buffers.
      63             :   Address* vm_limit = reinterpret_cast<Address*>(start + allocated_size);
      64             :   USE(vm_limit);
      65             :   for (int i = 0; i < kStoreBuffers; i++) {
      66             :     DCHECK(reinterpret_cast<Address>(start_[i]) >= reservation.address());
      67             :     DCHECK(reinterpret_cast<Address>(limit_[i]) >= reservation.address());
      68             :     DCHECK(start_[i] <= vm_limit);
      69             :     DCHECK(limit_[i] <= vm_limit);
      70             :     DCHECK_EQ(0, reinterpret_cast<Address>(limit_[i]) & kStoreBufferMask);
      71             :   }
      72             : 
      73             :   // Set RW permissions only on the pages we use.
      74       62883 :   const size_t used_size = RoundUp(requested_size, CommitPageSize());
      75       62883 :   if (!reservation.SetPermissions(start, used_size,
      76       62883 :                                   PageAllocator::kReadWrite)) {
      77           0 :     heap_->FatalProcessOutOfMemory("StoreBuffer::SetUp");
      78             :   }
      79       62883 :   current_ = 0;
      80       62883 :   top_ = start_[current_];
      81       62883 :   virtual_memory_.TakeControl(&reservation);
      82       62883 : }
      83             : 
      84       62868 : void StoreBuffer::TearDown() {
      85       62868 :   if (virtual_memory_.IsReserved()) virtual_memory_.Free();
      86       62868 :   top_ = nullptr;
      87      188604 :   for (int i = 0; i < kStoreBuffers; i++) {
      88      125736 :     start_[i] = nullptr;
      89      125736 :     limit_[i] = nullptr;
      90      125736 :     lazy_top_[i] = nullptr;
      91             :   }
      92       62868 : }
      93             : 
      94       17092 : void StoreBuffer::DeleteDuringRuntime(StoreBuffer* store_buffer, Address start,
      95             :                                       Address end) {
      96             :   DCHECK(store_buffer->mode() == StoreBuffer::NOT_IN_GC);
      97       17092 :   store_buffer->InsertDeletionIntoStoreBuffer(start, end);
      98       17092 : }
      99             : 
     100   106743136 : void StoreBuffer::InsertDuringRuntime(StoreBuffer* store_buffer, Address slot) {
     101             :   DCHECK(store_buffer->mode() == StoreBuffer::NOT_IN_GC);
     102   106743136 :   store_buffer->InsertIntoStoreBuffer(slot);
     103   106743064 : }
     104             : 
     105       17717 : void StoreBuffer::DeleteDuringGarbageCollection(StoreBuffer* store_buffer,
     106             :                                                 Address start, Address end) {
     107             :   // In GC the store buffer has to be empty at any time.
     108             :   DCHECK(store_buffer->Empty());
     109             :   DCHECK(store_buffer->mode() != StoreBuffer::NOT_IN_GC);
     110             :   Page* page = Page::FromAddress(start);
     111       17717 :   if (end) {
     112             :     RememberedSet<OLD_TO_NEW>::RemoveRange(page, start, end,
     113       17717 :                                            SlotSet::PREFREE_EMPTY_BUCKETS);
     114             :   } else {
     115           0 :     RememberedSet<OLD_TO_NEW>::Remove(page, start);
     116             :   }
     117       17717 : }
     118             : 
     119           5 : void StoreBuffer::InsertDuringGarbageCollection(StoreBuffer* store_buffer,
     120             :                                                 Address slot) {
     121             :   DCHECK(store_buffer->mode() != StoreBuffer::NOT_IN_GC);
     122           5 :   RememberedSet<OLD_TO_NEW>::Insert(Page::FromAddress(slot), slot);
     123           5 : }
     124             : 
     125      214172 : void StoreBuffer::SetMode(StoreBufferMode mode) {
     126      214172 :   mode_ = mode;
     127      214172 :   if (mode == NOT_IN_GC) {
     128      107086 :     insertion_callback = &InsertDuringRuntime;
     129      107086 :     deletion_callback = &DeleteDuringRuntime;
     130             :   } else {
     131      107086 :     insertion_callback = &InsertDuringGarbageCollection;
     132      107086 :     deletion_callback = &DeleteDuringGarbageCollection;
     133             :   }
     134      214172 : }
     135             : 
     136       44800 : int StoreBuffer::StoreBufferOverflow(Isolate* isolate) {
     137       74051 :   isolate->heap()->store_buffer()->FlipStoreBuffers();
     138       74051 :   isolate->counters()->store_buffer_overflows()->Increment();
     139             :   // Called by RecordWriteCodeStubAssembler, which doesnt accept void type
     140       44800 :   return 0;
     141             : }
     142             : 
     143       74051 : void StoreBuffer::FlipStoreBuffers() {
     144       74051 :   base::MutexGuard guard(&mutex_);
     145       74051 :   int other = (current_ + 1) % kStoreBuffers;
     146       74051 :   MoveEntriesToRememberedSet(other);
     147       74051 :   lazy_top_[current_] = top_;
     148       74051 :   current_ = other;
     149       74051 :   top_ = start_[current_];
     150             : 
     151       74051 :   if (!task_running_ && FLAG_concurrent_store_buffer) {
     152       66298 :     task_running_ = true;
     153       66298 :     V8::GetCurrentPlatform()->CallOnWorkerThread(
     154      331490 :         base::make_unique<Task>(heap_->isolate(), this));
     155             :   }
     156       74051 : }
     157             : 
     158      354491 : void StoreBuffer::MoveEntriesToRememberedSet(int index) {
     159      708982 :   if (!lazy_top_[index]) return;
     160             :   DCHECK_GE(index, 0);
     161             :   DCHECK_LT(index, kStoreBuffers);
     162             :   Address last_inserted_addr = kNullAddress;
     163             :   MemoryChunk* chunk = nullptr;
     164             : 
     165   180999956 :   for (Address* current = start_[index]; current < lazy_top_[index];
     166             :        current++) {
     167   180818849 :     Address addr = *current;
     168   361495496 :     if (chunk == nullptr ||
     169             :         MemoryChunk::BaseAddress(addr) != chunk->address()) {
     170             :       chunk = MemoryChunk::FromAnyPointerAddress(addr);
     171             :     }
     172   180818849 :     if (IsDeletionAddress(addr)) {
     173             :       last_inserted_addr = kNullAddress;
     174       11131 :       current++;
     175       11131 :       Address end = *current;
     176             :       DCHECK(!IsDeletionAddress(end));
     177             :       addr = UnmarkDeletionAddress(addr);
     178       11131 :       if (end) {
     179             :         RememberedSet<OLD_TO_NEW>::RemoveRange(chunk, addr, end,
     180       10285 :                                                SlotSet::PREFREE_EMPTY_BUCKETS);
     181             :       } else {
     182         846 :         RememberedSet<OLD_TO_NEW>::Remove(chunk, addr);
     183             :       }
     184             :     } else {
     185             :       DCHECK(!IsDeletionAddress(addr));
     186   180807718 :       if (addr != last_inserted_addr) {
     187   144020878 :         RememberedSet<OLD_TO_NEW>::Insert(chunk, addr);
     188             :         last_inserted_addr = addr;
     189             :       }
     190             :     }
     191             :   }
     192      181107 :   lazy_top_[index] = nullptr;
     193             : }
     194             : 
     195      107086 : void StoreBuffer::MoveAllEntriesToRememberedSet() {
     196      107086 :   base::MutexGuard guard(&mutex_);
     197      107086 :   int other = (current_ + 1) % kStoreBuffers;
     198      107086 :   MoveEntriesToRememberedSet(other);
     199      107086 :   lazy_top_[current_] = top_;
     200      107086 :   MoveEntriesToRememberedSet(current_);
     201      107086 :   top_ = start_[current_];
     202      107086 : }
     203             : 
     204       66268 : void StoreBuffer::ConcurrentlyProcessStoreBuffer() {
     205       66268 :   base::MutexGuard guard(&mutex_);
     206       66268 :   int other = (current_ + 1) % kStoreBuffers;
     207       66268 :   MoveEntriesToRememberedSet(other);
     208       66268 :   task_running_ = false;
     209       66268 : }
     210             : 
     211             : }  // namespace internal
     212      183867 : }  // namespace v8

Generated by: LCOV version 1.10