blob: c0eaf98779d4fff6636828f9c844af0b2fb9df3f [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"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000038#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000039#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000040#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000041
42namespace v8 {
43namespace internal {
44
45
kasperl@chromium.orga5551262010-12-07 12:49:48 +000046// Optimization sampler constants.
47static const int kSamplerFrameCount = 2;
48static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000049
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000050static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000051
52static const int kSamplerThresholdInit = 3;
53static const int kSamplerThresholdMin = 1;
54static const int kSamplerThresholdDelta = 1;
55
56static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000057
58static const int kSizeLimit = 1500;
59
kasperl@chromium.orga5551262010-12-07 12:49:48 +000060
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000061Atomic32 RuntimeProfiler::state_ = 0;
62// TODO(isolates): Create the semaphore lazily and clean it up when no
63// longer required.
64#ifdef ENABLE_LOGGING_AND_PROFILING
65Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0);
66#endif
67
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000068#ifdef DEBUG
69bool RuntimeProfiler::has_been_globally_setup_ = false;
70#endif
71bool RuntimeProfiler::enabled_ = false;
72
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073
74RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
75 : isolate_(isolate),
76 sampler_threshold_(kSamplerThresholdInit),
77 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
78 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +000079 kSamplerTicksBetweenThresholdAdjustment),
80 sampler_window_position_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 ClearSampleBuffer();
82}
83
84
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000085void RuntimeProfiler::GlobalSetup() {
86 ASSERT(!has_been_globally_setup_);
87 enabled_ = V8::UseCrankshaft() && FLAG_opt;
88#ifdef DEBUG
89 has_been_globally_setup_ = true;
90#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000091}
92
93
ricow@chromium.org4f693d62011-07-04 14:01:31 +000094void RuntimeProfiler::Optimize(JSFunction* function) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000095 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +000096 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +000097 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +000098 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +000099 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000100 PrintF(" for recompilation");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000101 PrintF("]\n");
102 }
103
104 // The next call to the function will trigger optimization.
105 function->MarkForLazyRecompilation();
106}
107
108
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000110 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
111 // Debug::has_break_points().
112 ASSERT(function->IsMarkedForLazyRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000113 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000114 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000115 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000116 return;
117 }
118
119 SharedFunctionInfo* shared = function->shared();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000120 // If the code is not optimizable or references context slots, don't try OSR.
121 if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) {
122 return;
123 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000124
125 // We are not prepared to do OSR for a function that already has an
126 // allocated arguments object. The optimized code would bypass it for
127 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000128 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000129
130 // We're using on-stack replacement: patch the unoptimized code so that
131 // any back edge in any unoptimized frame will trigger on-stack
132 // replacement for that frame.
133 if (FLAG_trace_osr) {
134 PrintF("[patching stack checks in ");
135 function->PrintName();
136 PrintF(" for on-stack replacement]\n");
137 }
138
139 // Get the stack check stub code object to match against. We aren't
140 // prepared to generate it, but we don't expect to have to.
141 StackCheckStub check_stub;
142 Object* check_code;
143 MaybeObject* maybe_check_code = check_stub.TryGetCode();
144 if (maybe_check_code->ToObject(&check_code)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000145 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000146 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000147 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000148 Deoptimizer::PatchStackCheckCode(unoptimized_code,
149 Code::cast(check_code),
150 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000151 }
152}
153
154
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155void RuntimeProfiler::ClearSampleBuffer() {
156 memset(sampler_window_, 0, sizeof(sampler_window_));
157 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000158}
159
160
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000161int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000162 int weight = 0;
163 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000165 if (sample != NULL) {
166 if (function == sample) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000168 }
169 }
170 }
171 return weight;
172}
173
174
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000176 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000177 sampler_window_[sampler_window_position_] = function;
178 sampler_window_weight_[sampler_window_position_] = weight;
179 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000180 (kSamplerWindowSize - 1);
181}
182
183
184void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000185 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000186
187 // Run through the JavaScript frames and collect them. If we already
188 // have a sample of the function, we mark it for optimizations
189 // (eagerly or lazily).
190 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000191 int sample_count = 0;
192 int frame_count = 0;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000193 for (JavaScriptFrameIterator it(isolate_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000194 frame_count++ < kSamplerFrameCount && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000195 it.Advance()) {
196 JavaScriptFrame* frame = it.frame();
197 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000198
199 // Adjust threshold each time we have processed
200 // a certain number of ticks.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 if (sampler_ticks_until_threshold_adjustment_ > 0) {
202 sampler_ticks_until_threshold_adjustment_--;
203 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000204 // If the threshold is not already at the minimum
205 // modify and reset the ticks until next adjustment.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 if (sampler_threshold_ > kSamplerThresholdMin) {
207 sampler_threshold_ -= kSamplerThresholdDelta;
208 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000209 kSamplerTicksBetweenThresholdAdjustment;
210 }
211 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000212 }
213
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000214 if (function->IsMarkedForLazyRecompilation()) {
215 Code* unoptimized = function->shared()->code();
216 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
217 if (nesting == 0) AttemptOnStackReplacement(function);
218 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
219 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000220 }
221
222 // Do not record non-optimizable functions.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000223 if (!function->IsOptimizable()) continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000224 samples[sample_count++] = function;
225
226 int function_size = function->shared()->SourceSize();
227 int threshold_size_factor = (function_size > kSizeLimit)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 ? sampler_threshold_size_factor_
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000229 : 1;
230
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000231 int threshold = sampler_threshold_ * threshold_size_factor;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000232
233 if (LookupSample(function) >= threshold) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000234 Optimize(function);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000235 }
236 }
237
238 // Add the collected functions as samples. It's important not to do
239 // this as part of collecting them because this will interfere with
240 // the sample lookup in case of recursive functions.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000241 for (int i = 0; i < sample_count; i++) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000242 AddSample(samples[i], kSamplerFrameWeight[i]);
243 }
244}
245
246
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000247void RuntimeProfiler::NotifyTick() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000248#ifdef ENABLE_LOGGING_AND_PROFILING
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249 isolate_->stack_guard()->RequestRuntimeProfilerTick();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000250#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000251}
252
253
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000254void RuntimeProfiler::Setup() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000255 ASSERT(has_been_globally_setup_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000256 ClearSampleBuffer();
257 // If the ticker hasn't already started, make sure to do so to get
258 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000259 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000260}
261
262
263void RuntimeProfiler::Reset() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264 sampler_threshold_ = kSamplerThresholdInit;
265 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
266 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000267 kSamplerTicksBetweenThresholdAdjustment;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000268}
269
270
271void RuntimeProfiler::TearDown() {
272 // Nothing to do.
273}
274
275
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000276int RuntimeProfiler::SamplerWindowSize() {
277 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278}
279
280
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000281// Update the pointers in the sampler window after a GC.
282void RuntimeProfiler::UpdateSamplesAfterScavenge() {
283 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 Object* function = sampler_window_[i];
285 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000286 MapWord map_word = HeapObject::cast(function)->map_word();
287 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000288 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000289 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000290 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000291 }
292 }
293 }
294}
295
296
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
298#ifdef ENABLE_LOGGING_AND_PROFILING
299 // The profiler thread must still be waiting.
300 ASSERT(NoBarrier_Load(&state_) >= 0);
301 // In IsolateEnteredJS we have already incremented the counter and
302 // undid the decrement done by the profiler thread. Increment again
303 // to get the right count of active isolates.
304 NoBarrier_AtomicIncrement(&state_, 1);
305 semaphore_->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000306#endif
307}
308
309
310bool RuntimeProfiler::IsSomeIsolateInJS() {
311 return NoBarrier_Load(&state_) > 0;
312}
313
314
315bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
316#ifdef ENABLE_LOGGING_AND_PROFILING
317 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
318 ASSERT(old_state >= -1);
319 if (old_state != 0) return false;
320 semaphore_->Wait();
321#endif
322 return true;
323}
324
325
326void RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown() {
327#ifdef ENABLE_LOGGING_AND_PROFILING
328 semaphore_->Signal();
329#endif
330}
331
332
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000333void RuntimeProfiler::RemoveDeadSamples() {
334 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000335 Object* function = sampler_window_[i];
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000336 if (function != NULL && !HeapObject::cast(function)->IsMarked()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000337 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000338 }
339 }
340}
341
342
343void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
344 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000345 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000346 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000347}
348
349
350bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000351#ifdef ENABLE_LOGGING_AND_PROFILING
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000352 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
353 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000355#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000356 return false;
357}
358
359
360} } // namespace v8::internal