Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / v8 / src / optimizing-compiler-thread.h
1 // Copyright 2012 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_OPTIMIZING_COMPILER_THREAD_H_
6 #define V8_OPTIMIZING_COMPILER_THREAD_H_
7
8 #include "src/base/atomicops.h"
9 #include "src/base/platform/mutex.h"
10 #include "src/base/platform/platform.h"
11 #include "src/base/platform/time.h"
12 #include "src/flags.h"
13 #include "src/list.h"
14 #include "src/unbound-queue-inl.h"
15
16 namespace v8 {
17 namespace internal {
18
19 class HOptimizedGraphBuilder;
20 class OptimizedCompileJob;
21 class SharedFunctionInfo;
22
23 class OptimizingCompilerThread : public base::Thread {
24  public:
25   explicit OptimizingCompilerThread(Isolate* isolate)
26       : Thread(Options("OptimizingCompilerThread")),
27 #ifdef DEBUG
28         thread_id_(0),
29 #endif
30         isolate_(isolate),
31         stop_semaphore_(0),
32         input_queue_semaphore_(0),
33         input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
34         input_queue_length_(0),
35         input_queue_shift_(0),
36         osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
37         osr_buffer_cursor_(0),
38         osr_hits_(0),
39         osr_attempts_(0),
40         blocked_jobs_(0),
41         tracing_enabled_(FLAG_trace_concurrent_recompilation),
42         job_based_recompilation_(FLAG_job_based_recompilation) {
43     base::NoBarrier_Store(&stop_thread_,
44                           static_cast<base::AtomicWord>(CONTINUE));
45     input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
46     if (FLAG_concurrent_osr) {
47       // Allocate and mark OSR buffer slots as empty.
48       osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
49       for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
50     }
51   }
52
53   ~OptimizingCompilerThread();
54
55   void Run();
56   void Stop();
57   void Flush();
58   void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
59   void Unblock();
60   void InstallOptimizedFunctions();
61   OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
62                                              BailoutId osr_ast_id);
63   bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
64
65   bool IsQueuedForOSR(JSFunction* function);
66
67   inline bool IsQueueAvailable() {
68     base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
69     return input_queue_length_ < input_queue_capacity_;
70   }
71
72   inline void AgeBufferedOsrJobs() {
73     // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
74     // Dispose said OSR job in the latter case.  Calling this on every GC
75     // should make sure that we do not hold onto stale jobs indefinitely.
76     AddToOsrBuffer(NULL);
77   }
78
79   static bool Enabled(int max_available) {
80     return (FLAG_concurrent_recompilation && max_available > 1);
81   }
82
83 #ifdef DEBUG
84   static bool IsOptimizerThread(Isolate* isolate);
85   bool IsOptimizerThread();
86 #endif
87
88  private:
89   class CompileTask;
90
91   enum StopFlag { CONTINUE, STOP, FLUSH };
92
93   void FlushInputQueue(bool restore_function_code);
94   void FlushOutputQueue(bool restore_function_code);
95   void FlushOsrBuffer(bool restore_function_code);
96   void CompileNext();
97   OptimizedCompileJob* NextInput();
98
99   // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
100   // Tasks evicted from the cyclic buffer are discarded.
101   void AddToOsrBuffer(OptimizedCompileJob* compiler);
102
103   inline int InputQueueIndex(int i) {
104     int result = (i + input_queue_shift_) % input_queue_capacity_;
105     DCHECK_LE(0, result);
106     DCHECK_LT(result, input_queue_capacity_);
107     return result;
108   }
109
110 #ifdef DEBUG
111   int thread_id_;
112   base::Mutex thread_id_mutex_;
113 #endif
114
115   Isolate* isolate_;
116   base::Semaphore stop_semaphore_;
117   base::Semaphore input_queue_semaphore_;
118
119   // Circular queue of incoming recompilation tasks (including OSR).
120   OptimizedCompileJob** input_queue_;
121   int input_queue_capacity_;
122   int input_queue_length_;
123   int input_queue_shift_;
124   base::Mutex input_queue_mutex_;
125
126   // Queue of recompilation tasks ready to be installed (excluding OSR).
127   UnboundQueue<OptimizedCompileJob*> output_queue_;
128   // Used for job based recompilation which has multiple producers on
129   // different threads.
130   base::Mutex output_queue_mutex_;
131
132   // Cyclic buffer of recompilation tasks for OSR.
133   OptimizedCompileJob** osr_buffer_;
134   int osr_buffer_capacity_;
135   int osr_buffer_cursor_;
136
137   volatile base::AtomicWord stop_thread_;
138   base::TimeDelta time_spent_compiling_;
139   base::TimeDelta time_spent_total_;
140
141   int osr_hits_;
142   int osr_attempts_;
143
144   int blocked_jobs_;
145
146   // Copies of FLAG_trace_concurrent_recompilation and
147   // FLAG_job_based_recompilation that will be used from the background thread.
148   //
149   // Since flags might get modified while the background thread is running, it
150   // is not safe to access them directly.
151   bool tracing_enabled_;
152   bool job_based_recompilation_;
153 };
154
155 } }  // namespace v8::internal
156
157 #endif  // V8_OPTIMIZING_COMPILER_THREAD_H_