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#ifndef V8_HEAP_SCAVENGE_JOB_H_
6#define V8_HEAP_SCAVENGE_JOB_H_
7
8#include "src/cancelable-task.h"
9#include "src/heap/gc-tracer.h"
10
11namespace v8 {
12namespace internal {
13
14class Heap;
15class Isolate;
16
17
18// This class posts idle tasks and performs scavenges in the idle tasks.
19class ScavengeJob {
20 public:
21  class IdleTask : public CancelableIdleTask {
22   public:
23    explicit IdleTask(Isolate* isolate, ScavengeJob* job)
24        : CancelableIdleTask(isolate), job_(job) {}
25    // CancelableIdleTask overrides.
26    void RunInternal(double deadline_in_seconds) override;
27
28   private:
29    ScavengeJob* job_;
30  };
31
32  ScavengeJob()
33      : idle_task_pending_(false),
34        idle_task_rescheduled_(false),
35        bytes_allocated_since_the_last_task_(0) {}
36
37  // Posts an idle task if the cumulative bytes allocated since the last
38  // idle task exceed kBytesAllocatedBeforeNextIdleTask.
39  void ScheduleIdleTaskIfNeeded(Heap* heap, int bytes_allocated);
40
41  // Posts an idle task ignoring the bytes allocated, but makes sure
42  // that the new idle task cannot reschedule again.
43  // This prevents infinite rescheduling.
44  void RescheduleIdleTask(Heap* heap);
45
46  bool IdleTaskPending() { return idle_task_pending_; }
47  void NotifyIdleTask() { idle_task_pending_ = false; }
48  bool IdleTaskRescheduled() { return idle_task_rescheduled_; }
49
50  static bool ReachedIdleAllocationLimit(double scavenge_speed_in_bytes_per_ms,
51                                         size_t new_space_size,
52                                         size_t new_space_capacity);
53
54  static bool EnoughIdleTimeForScavenge(double idle_time_ms,
55                                        double scavenge_speed_in_bytes_per_ms,
56                                        size_t new_space_size);
57
58  // If we haven't recorded any scavenger events yet, we use a conservative
59  // lower bound for the scavenger speed.
60  static const int kInitialScavengeSpeedInBytesPerMs = 256 * KB;
61  // Estimate of the average idle time that an idle task gets.
62  static const int kAverageIdleTimeMs = 5;
63  // The number of bytes to be allocated in new space before the next idle
64  // task is posted.
65  static const size_t kBytesAllocatedBeforeNextIdleTask = 512 * KB;
66  // The minimum size of allocated new space objects to trigger a scavenge.
67  static const size_t kMinAllocationLimit = 512 * KB;
68  // The allocation limit cannot exceed this fraction of the new space capacity.
69  static const double kMaxAllocationLimitAsFractionOfNewSpace;
70
71 private:
72  void ScheduleIdleTask(Heap* heap);
73  bool idle_task_pending_;
74  bool idle_task_rescheduled_;
75  int bytes_allocated_since_the_last_task_;
76};
77}  // namespace internal
78}  // namespace v8
79
80#endif  // V8_HEAP_SCAVENGE_JOB_H_
81