scavenge-job.cc 4.08 KB
Newer Older
ulan's avatar
ulan committed
1 2 3 4 5 6 7
// Copyright 2015 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/scavenge-job.h"

#include "src/base/platform/time.h"
8
#include "src/heap/gc-tracer.h"
ulan's avatar
ulan committed
9 10
#include "src/heap/heap-inl.h"
#include "src/heap/heap.h"
11
#include "src/heap/spaces.h"
ulan's avatar
ulan committed
12 13
#include "src/isolate.h"
#include "src/v8.h"
14
#include "src/vm-state-inl.h"
ulan's avatar
ulan committed
15 16 17 18 19 20 21 22

namespace v8 {
namespace internal {


const double ScavengeJob::kMaxAllocationLimitAsFractionOfNewSpace = 0.8;

void ScavengeJob::IdleTask::RunInternal(double deadline_in_seconds) {
23
  VMState<GC> state(isolate());
24
  TRACE_EVENT_CALL_STATS_SCOPED(isolate(), "v8", "V8.Task");
25
  Heap* heap = isolate()->heap();
ulan's avatar
ulan committed
26 27 28 29 30
  double deadline_in_ms =
      deadline_in_seconds *
      static_cast<double>(base::Time::kMillisecondsPerSecond);
  double start_ms = heap->MonotonicallyIncreasingTimeInMs();
  double idle_time_in_ms = deadline_in_ms - start_ms;
31 32
  double scavenge_speed_in_bytes_per_ms =
      heap->tracer()->ScavengeSpeedInBytesPerMillisecond();
ulan's avatar
ulan committed
33 34 35 36 37 38 39 40 41
  size_t new_space_size = heap->new_space()->Size();
  size_t new_space_capacity = heap->new_space()->Capacity();

  job_->NotifyIdleTask();

  if (ReachedIdleAllocationLimit(scavenge_speed_in_bytes_per_ms, new_space_size,
                                 new_space_capacity)) {
    if (EnoughIdleTimeForScavenge(
            idle_time_in_ms, scavenge_speed_in_bytes_per_ms, new_space_size)) {
42
      heap->CollectGarbage(NEW_SPACE, GarbageCollectionReason::kIdleTask);
ulan's avatar
ulan committed
43 44 45 46 47 48 49 50
    } else {
      // Immediately request another idle task that can get larger idle time.
      job_->RescheduleIdleTask(heap);
    }
  }
}

bool ScavengeJob::ReachedIdleAllocationLimit(
51
    double scavenge_speed_in_bytes_per_ms, size_t new_space_size,
ulan's avatar
ulan committed
52 53 54 55 56 57 58
    size_t new_space_capacity) {
  if (scavenge_speed_in_bytes_per_ms == 0) {
    scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs;
  }

  // Set the allocation limit to the number of bytes we can scavenge in an
  // average idle task.
59
  double allocation_limit = kAverageIdleTimeMs * scavenge_speed_in_bytes_per_ms;
ulan's avatar
ulan committed
60 61 62

  // Keep the limit smaller than the new space capacity.
  allocation_limit =
63 64
      Min<double>(allocation_limit,
                  new_space_capacity * kMaxAllocationLimitAsFractionOfNewSpace);
ulan's avatar
ulan committed
65
  // Adjust the limit to take into account bytes that will be allocated until
66 67 68 69 70
  // the next check and keep the limit large enough to avoid scavenges in tiny
  // new space.
  allocation_limit =
      Max<double>(allocation_limit - kBytesAllocatedBeforeNextIdleTask,
                  kMinAllocationLimit);
ulan's avatar
ulan committed
71 72 73 74 75

  return allocation_limit <= new_space_size;
}

bool ScavengeJob::EnoughIdleTimeForScavenge(
76
    double idle_time_in_ms, double scavenge_speed_in_bytes_per_ms,
ulan's avatar
ulan committed
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
    size_t new_space_size) {
  if (scavenge_speed_in_bytes_per_ms == 0) {
    scavenge_speed_in_bytes_per_ms = kInitialScavengeSpeedInBytesPerMs;
  }
  return new_space_size <= idle_time_in_ms * scavenge_speed_in_bytes_per_ms;
}


void ScavengeJob::RescheduleIdleTask(Heap* heap) {
  // Make sure that we don't reschedule more than one time.
  // Otherwise, we might spam the scheduler with idle tasks.
  if (!idle_task_rescheduled_) {
    ScheduleIdleTask(heap);
    idle_task_rescheduled_ = true;
  }
}


void ScavengeJob::ScheduleIdleTaskIfNeeded(Heap* heap, int bytes_allocated) {
  bytes_allocated_since_the_last_task_ += bytes_allocated;
  if (bytes_allocated_since_the_last_task_ >=
      static_cast<int>(kBytesAllocatedBeforeNextIdleTask)) {
    ScheduleIdleTask(heap);
    bytes_allocated_since_the_last_task_ = 0;
    idle_task_rescheduled_ = false;
  }
}


void ScavengeJob::ScheduleIdleTask(Heap* heap) {
107
  if (!idle_task_pending_ && !heap->IsTearingDown()) {
ulan's avatar
ulan committed
108 109 110
    v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate());
    if (V8::GetCurrentPlatform()->IdleTasksEnabled(isolate)) {
      idle_task_pending_ = true;
111 112 113
      auto task = base::make_unique<IdleTask>(heap->isolate(), this);
      V8::GetCurrentPlatform()->GetForegroundTaskRunner(isolate)->PostIdleTask(
          std::move(task));
ulan's avatar
ulan committed
114 115 116
    }
  }
}
117 118
}  // namespace internal
}  // namespace v8