blob: 520dd3989018b99903ad2cf8dca8206682e26bb3 [file] [log] [blame]
danno@chromium.org160a7b02011-04-18 15:51:38 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "runtime-profiler.h"
31
32#include "assembler.h"
33#include "code-stubs.h"
34#include "compilation-cache.h"
35#include "deoptimizer.h"
36#include "execution.h"
37#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000038#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000039#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000041#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042
43namespace v8 {
44namespace internal {
45
46
kasperl@chromium.orga5551262010-12-07 12:49:48 +000047// Optimization sampler constants.
48static const int kSamplerFrameCount = 2;
49static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000050
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000051static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052
53static const int kSamplerThresholdInit = 3;
54static const int kSamplerThresholdMin = 1;
55static const int kSamplerThresholdDelta = 1;
56
57static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000058
59static const int kSizeLimit = 1500;
60
kasperl@chromium.orga5551262010-12-07 12:49:48 +000061
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000062Atomic32 RuntimeProfiler::state_ = 0;
63// TODO(isolates): Create the semaphore lazily and clean it up when no
64// longer required.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000066
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000067#ifdef DEBUG
68bool RuntimeProfiler::has_been_globally_setup_ = false;
69#endif
70bool RuntimeProfiler::enabled_ = false;
71
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000072
73RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
74 : isolate_(isolate),
75 sampler_threshold_(kSamplerThresholdInit),
76 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
77 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +000078 kSamplerTicksBetweenThresholdAdjustment),
79 sampler_window_position_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000080 ClearSampleBuffer();
81}
82
83
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000084void RuntimeProfiler::GlobalSetup() {
85 ASSERT(!has_been_globally_setup_);
86 enabled_ = V8::UseCrankshaft() && FLAG_opt;
87#ifdef DEBUG
88 has_been_globally_setup_ = true;
89#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090}
91
92
ricow@chromium.org4f693d62011-07-04 14:01:31 +000093void RuntimeProfiler::Optimize(JSFunction* function) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000094 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +000095 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +000096 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +000097 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +000098 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +000099 PrintF(" for recompilation");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000100 PrintF("]\n");
101 }
102
103 // The next call to the function will trigger optimization.
104 function->MarkForLazyRecompilation();
105}
106
107
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000108void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000109 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
110 // Debug::has_break_points().
111 ASSERT(function->IsMarkedForLazyRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000113 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000114 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000115 return;
116 }
117
118 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000119 // If the code is not optimizable, don't try OSR.
120 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000121
122 // We are not prepared to do OSR for a function that already has an
123 // allocated arguments object. The optimized code would bypass it for
124 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000125 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000126
127 // We're using on-stack replacement: patch the unoptimized code so that
128 // any back edge in any unoptimized frame will trigger on-stack
129 // replacement for that frame.
130 if (FLAG_trace_osr) {
131 PrintF("[patching stack checks in ");
132 function->PrintName();
133 PrintF(" for on-stack replacement]\n");
134 }
135
136 // Get the stack check stub code object to match against. We aren't
137 // prepared to generate it, but we don't expect to have to.
138 StackCheckStub check_stub;
139 Object* check_code;
140 MaybeObject* maybe_check_code = check_stub.TryGetCode();
141 if (maybe_check_code->ToObject(&check_code)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000143 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000144 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000145 Deoptimizer::PatchStackCheckCode(unoptimized_code,
146 Code::cast(check_code),
147 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000148 }
149}
150
151
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000152void RuntimeProfiler::ClearSampleBuffer() {
153 memset(sampler_window_, 0, sizeof(sampler_window_));
154 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000155}
156
157
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000159 int weight = 0;
160 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000161 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000162 if (sample != NULL) {
163 if (function == sample) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000165 }
166 }
167 }
168 return weight;
169}
170
171
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000172void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000173 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000174 sampler_window_[sampler_window_position_] = function;
175 sampler_window_weight_[sampler_window_position_] = weight;
176 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000177 (kSamplerWindowSize - 1);
178}
179
180
181void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000182 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000183
184 // Run through the JavaScript frames and collect them. If we already
185 // have a sample of the function, we mark it for optimizations
186 // (eagerly or lazily).
187 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000188 int sample_count = 0;
189 int frame_count = 0;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000190 for (JavaScriptFrameIterator it(isolate_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000191 frame_count++ < kSamplerFrameCount && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000192 it.Advance()) {
193 JavaScriptFrame* frame = it.frame();
194 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000195
196 // Adjust threshold each time we have processed
197 // a certain number of ticks.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000198 if (sampler_ticks_until_threshold_adjustment_ > 0) {
199 sampler_ticks_until_threshold_adjustment_--;
200 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000201 // If the threshold is not already at the minimum
202 // modify and reset the ticks until next adjustment.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000203 if (sampler_threshold_ > kSamplerThresholdMin) {
204 sampler_threshold_ -= kSamplerThresholdDelta;
205 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000206 kSamplerTicksBetweenThresholdAdjustment;
207 }
208 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000209 }
210
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000211 if (function->IsMarkedForLazyRecompilation()) {
212 Code* unoptimized = function->shared()->code();
213 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
214 if (nesting == 0) AttemptOnStackReplacement(function);
215 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
216 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000217 }
218
219 // Do not record non-optimizable functions.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000220 if (!function->IsOptimizable()) continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000221 samples[sample_count++] = function;
222
223 int function_size = function->shared()->SourceSize();
224 int threshold_size_factor = (function_size > kSizeLimit)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000225 ? sampler_threshold_size_factor_
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000226 : 1;
227
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 int threshold = sampler_threshold_ * threshold_size_factor;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000229
230 if (LookupSample(function) >= threshold) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000231 Optimize(function);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000232 }
233 }
234
235 // Add the collected functions as samples. It's important not to do
236 // this as part of collecting them because this will interfere with
237 // the sample lookup in case of recursive functions.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000238 for (int i = 0; i < sample_count; i++) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000239 AddSample(samples[i], kSamplerFrameWeight[i]);
240 }
241}
242
243
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244void RuntimeProfiler::NotifyTick() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 isolate_->stack_guard()->RequestRuntimeProfilerTick();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000246}
247
248
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000249void RuntimeProfiler::Setup() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000250 ASSERT(has_been_globally_setup_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000251 ClearSampleBuffer();
252 // If the ticker hasn't already started, make sure to do so to get
253 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000254 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000255}
256
257
258void RuntimeProfiler::Reset() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000259 sampler_threshold_ = kSamplerThresholdInit;
260 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
261 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000262 kSamplerTicksBetweenThresholdAdjustment;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000263}
264
265
266void RuntimeProfiler::TearDown() {
267 // Nothing to do.
268}
269
270
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000271int RuntimeProfiler::SamplerWindowSize() {
272 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000273}
274
275
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000276// Update the pointers in the sampler window after a GC.
277void RuntimeProfiler::UpdateSamplesAfterScavenge() {
278 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 Object* function = sampler_window_[i];
280 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000281 MapWord map_word = HeapObject::cast(function)->map_word();
282 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000283 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000284 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000285 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000286 }
287 }
288 }
289}
290
291
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 // The profiler thread must still be waiting.
294 ASSERT(NoBarrier_Load(&state_) >= 0);
295 // In IsolateEnteredJS we have already incremented the counter and
296 // undid the decrement done by the profiler thread. Increment again
297 // to get the right count of active isolates.
298 NoBarrier_AtomicIncrement(&state_, 1);
299 semaphore_->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000300}
301
302
303bool RuntimeProfiler::IsSomeIsolateInJS() {
304 return NoBarrier_Load(&state_) > 0;
305}
306
307
308bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000309 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
310 ASSERT(old_state >= -1);
311 if (old_state != 0) return false;
312 semaphore_->Wait();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313 return true;
314}
315
316
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000317void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000318 // Do a fake increment. If the profiler is waiting on the semaphore,
319 // the returned state is 0, which can be left as an initial state in
320 // case profiling is restarted later. If the profiler is not
321 // waiting, the increment will prevent it from waiting, but has to
322 // be undone after the profiler is stopped.
323 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
324 ASSERT(new_state >= 0);
325 if (new_state == 0) {
326 // The profiler thread is waiting. Wake it up. It must check for
327 // stop conditions before attempting to wait again.
328 semaphore_->Signal();
329 }
330 thread->Join();
331 // The profiler thread is now stopped. Undo the increment in case it
332 // was not waiting.
333 if (new_state != 0) {
334 NoBarrier_AtomicIncrement(&state_, -1);
335 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000336}
337
338
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000339void RuntimeProfiler::RemoveDeadSamples() {
340 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000341 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000342 if (function != NULL &&
343 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000344 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000345 }
346 }
347}
348
349
350void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
351 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000353 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354}
355
356
357bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000358 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
359 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000360 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000361 return false;
362}
363
364
365} } // namespace v8::internal