optimizing-compile-dispatcher.h 4.28 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6 7 8
#ifndef V8_OPTIMIZING_COMPILE_DISPATCHER_H_
#define V8_OPTIMIZING_COMPILE_DISPATCHER_H_

#include <queue>
9

10
#include "src/base/atomicops.h"
11
#include "src/base/platform/condition-variable.h"
12 13
#include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h"
14 15
#include "src/flags.h"
#include "src/list.h"
16 17 18 19

namespace v8 {
namespace internal {

20
class HOptimizedGraphBuilder;
21
class OptimizedCompileJob;
22
class SharedFunctionInfo;
23

24
class OptimizingCompileDispatcher {
25
 public:
26 27
  explicit OptimizingCompileDispatcher(Isolate* isolate)
      : isolate_(isolate),
28 29 30 31 32 33 34
        input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
        input_queue_length_(0),
        input_queue_shift_(0),
        osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
        osr_buffer_cursor_(0),
        osr_hits_(0),
        osr_attempts_(0),
35
        blocked_jobs_(0),
36
        ref_count_(0),
37
        recompilation_delay_(FLAG_concurrent_recompilation_delay) {
38
    base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE));
39
    input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
40 41
    if (FLAG_concurrent_osr) {
      // Allocate and mark OSR buffer slots as empty.
42
      osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
43
      for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
44
    }
45 46
  }

47
  ~OptimizingCompileDispatcher();
48

49 50
  void Run();
  void Stop();
51
  void Flush();
52
  void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
53
  void Unblock();
54
  void InstallOptimizedFunctions();
55 56 57
  OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
                                             BailoutId osr_ast_id);
  bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
58

59
  bool IsQueuedForOSR(JSFunction* function);
60 61

  inline bool IsQueueAvailable() {
62
    base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
63 64 65 66 67 68 69 70
    return input_queue_length_ < input_queue_capacity_;
  }

  inline void AgeBufferedOsrJobs() {
    // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
    // Dispose said OSR job in the latter case.  Calling this on every GC
    // should make sure that we do not hold onto stale jobs indefinitely.
    AddToOsrBuffer(NULL);
71 72
  }

73
  static bool Enabled() { return FLAG_concurrent_recompilation; }
74

75
 private:
76 77
  class CompileTask;

78
  enum ModeFlag { COMPILE, FLUSH };
79 80

  void FlushOutputQueue(bool restore_function_code);
81
  void FlushOsrBuffer(bool restore_function_code);
82
  void CompileNext(OptimizedCompileJob* job);
83
  OptimizedCompileJob* NextInput(bool check_if_flushing = false);
84

85 86
  // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
  // Tasks evicted from the cyclic buffer are discarded.
87
  void AddToOsrBuffer(OptimizedCompileJob* compiler);
88 89 90

  inline int InputQueueIndex(int i) {
    int result = (i + input_queue_shift_) % input_queue_capacity_;
91 92
    DCHECK_LE(0, result);
    DCHECK_LT(result, input_queue_capacity_);
93
    return result;
94 95
  }

96
  Isolate* isolate_;
97

98
  // Circular queue of incoming recompilation tasks (including OSR).
99
  OptimizedCompileJob** input_queue_;
100 101 102
  int input_queue_capacity_;
  int input_queue_length_;
  int input_queue_shift_;
103
  base::Mutex input_queue_mutex_;
104

105
  // Queue of recompilation tasks ready to be installed (excluding OSR).
106
  std::queue<OptimizedCompileJob*> output_queue_;
107 108 109
  // Used for job based recompilation which has multiple producers on
  // different threads.
  base::Mutex output_queue_mutex_;
110

111
  // Cyclic buffer of recompilation tasks for OSR.
112
  OptimizedCompileJob** osr_buffer_;
113 114
  int osr_buffer_capacity_;
  int osr_buffer_cursor_;
115

116
  volatile base::AtomicWord mode_;
117 118 119

  int osr_hits_;
  int osr_attempts_;
120 121

  int blocked_jobs_;
122

123 124 125
  int ref_count_;
  base::Mutex ref_count_mutex_;
  base::ConditionVariable ref_count_zero_;
126

127 128
  // Copy of FLAG_concurrent_recompilation_delay that will be used from the
  // background thread.
129 130 131
  //
  // Since flags might get modified while the background thread is running, it
  // is not safe to access them directly.
132
  int recompilation_delay_;
133
};
134 135
}
}  // namespace v8::internal
136

137
#endif  // V8_OPTIMIZING_COMPILE_DISPATCHER_H_