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