• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 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/scavenge-job.h"
6 
7 #include "src/base/platform/time.h"
8 #include "src/execution/isolate.h"
9 #include "src/execution/vm-state-inl.h"
10 #include "src/heap/heap-inl.h"
11 #include "src/heap/heap.h"
12 #include "src/init/v8.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class ScavengeJob::Task : public CancelableTask {
18  public:
Task(Isolate * isolate,ScavengeJob * job)19   Task(Isolate* isolate, ScavengeJob* job)
20       : CancelableTask(isolate), isolate_(isolate), job_(job) {}
21 
22   // CancelableTask overrides.
23   void RunInternal() override;
24 
isolate() const25   Isolate* isolate() const { return isolate_; }
26 
27  private:
28   Isolate* const isolate_;
29   ScavengeJob* const job_;
30 };
31 
YoungGenerationTaskTriggerSize(Heap * heap)32 size_t ScavengeJob::YoungGenerationTaskTriggerSize(Heap* heap) {
33   return heap->new_space()->Capacity() * FLAG_scavenge_task_trigger / 100;
34 }
35 
YoungGenerationSizeTaskTriggerReached(Heap * heap)36 bool ScavengeJob::YoungGenerationSizeTaskTriggerReached(Heap* heap) {
37   return heap->new_space()->Size() >= YoungGenerationTaskTriggerSize(heap);
38 }
39 
ScheduleTaskIfNeeded(Heap * heap)40 void ScavengeJob::ScheduleTaskIfNeeded(Heap* heap) {
41   if (FLAG_scavenge_task && !task_pending_ && !heap->IsTearingDown() &&
42       YoungGenerationSizeTaskTriggerReached(heap)) {
43     v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap->isolate());
44     auto taskrunner =
45         V8::GetCurrentPlatform()->GetForegroundTaskRunner(isolate);
46     if (taskrunner->NonNestableTasksEnabled()) {
47       taskrunner->PostNonNestableTask(
48           std::make_unique<Task>(heap->isolate(), this));
49       task_pending_ = true;
50     }
51   }
52 }
53 
RunInternal()54 void ScavengeJob::Task::RunInternal() {
55   VMState<GC> state(isolate());
56   TRACE_EVENT_CALL_STATS_SCOPED(isolate(), "v8", "V8.Task");
57 
58   if (ScavengeJob::YoungGenerationSizeTaskTriggerReached(isolate()->heap())) {
59     isolate()->heap()->CollectGarbage(NEW_SPACE,
60                                       GarbageCollectionReason::kTask);
61   }
62 
63   job_->set_task_pending(false);
64 }
65 
66 }  // namespace internal
67 }  // namespace v8
68