blob: 5b2733356d243ce28dc366cafb66f08b37c9b10f [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 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"
danno@chromium.org129d3982012-07-25 15:01:47 +000037#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000039#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000040#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000041#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043
44namespace v8 {
45namespace internal {
46
47
kasperl@chromium.orga5551262010-12-07 12:49:48 +000048// Optimization sampler constants.
49static const int kSamplerFrameCount = 2;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000050
51// Constants for statistical profiler.
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000053
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000054static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000055
56static const int kSamplerThresholdInit = 3;
57static const int kSamplerThresholdMin = 1;
58static const int kSamplerThresholdDelta = 1;
59
60static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000061
62static const int kSizeLimit = 1500;
63
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000064// Constants for counter based profiler.
65
66// Number of times a function has to be seen on the stack before it is
67// optimized.
68static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000069// If the function optimization was disabled due to high deoptimization count,
70// but the function is hot and has been seen on the stack this number of times,
71// then we try to reenable optimization for this function.
72static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000073// If a function does not have enough type info (according to
74// FLAG_type_info_threshold), but has seen a huge number of ticks,
75// optimize it as it is.
76static const int kTicksWhenNotEnoughTypeInfo = 100;
77// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000078STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
79STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000080STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000081
mmassi@chromium.org7028c052012-06-13 11:51:58 +000082
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000083// Maximum size in bytes of generated code for a function to be optimized
84// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000085static const int kMaxSizeEarlyOpt =
86 5 * FullCodeGenerator::kBackEdgeDistanceUnit;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000087
kasperl@chromium.orga5551262010-12-07 12:49:48 +000088
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089Atomic32 RuntimeProfiler::state_ = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000090
91// TODO(isolates): Clean up the semaphore when it is no longer required.
92static LazySemaphore<0>::type semaphore = LAZY_SEMAPHORE_INITIALIZER;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000094#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000095bool RuntimeProfiler::has_been_globally_set_up_ = false;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000096#endif
97bool RuntimeProfiler::enabled_ = false;
98
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099
100RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
101 : isolate_(isolate),
102 sampler_threshold_(kSamplerThresholdInit),
103 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
104 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000105 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000106 sampler_window_position_(0),
107 any_ic_changed_(false),
108 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000109 ClearSampleBuffer();
110}
111
112
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000113void RuntimeProfiler::GlobalSetUp() {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000114 ASSERT(!has_been_globally_set_up_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000115 enabled_ = V8::UseCrankshaft() && FLAG_opt;
116#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000117 has_been_globally_set_up_ = true;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000118#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000119}
120
121
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000122static void GetICCounts(JSFunction* function,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000123 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000124 int* ic_total_count,
125 int* percentage) {
126 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000127 *ic_with_type_info_count = 0;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000128 Object* raw_info =
129 function->shared()->code()->type_feedback_info();
130 if (raw_info->IsTypeFeedbackInfo()) {
131 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000132 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000133 *ic_total_count = info->ic_total_count();
134 }
135 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000136 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000137 : 100;
138}
139
140
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000141void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000142 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000143 // If we are in manual mode, don't auto-optimize anything.
144 if (FLAG_manual_parallel_recompilation) return;
145
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000146 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000147 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000148 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000149 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000150 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000151 if (FLAG_type_info_threshold > 0) {
152 int typeinfo, total, percentage;
153 GetICCounts(function, &typeinfo, &total, &percentage);
154 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
155 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000156 PrintF("]\n");
157 }
158
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000159 if (FLAG_parallel_recompilation) {
160 function->MarkForParallelRecompilation();
161 } else {
162 // The next call to the function will trigger optimization.
163 function->MarkForLazyRecompilation();
164 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000165}
166
167
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000169 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
170 // Debug::has_break_points().
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000171 ASSERT(function->IsMarkedForLazyRecompilation() ||
172 function->IsMarkedForParallelRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000173 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000174 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000175 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000176 return;
177 }
178
179 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000180 // If the code is not optimizable, don't try OSR.
181 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000182
183 // We are not prepared to do OSR for a function that already has an
184 // allocated arguments object. The optimized code would bypass it for
185 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000186 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000187
188 // We're using on-stack replacement: patch the unoptimized code so that
189 // any back edge in any unoptimized frame will trigger on-stack
190 // replacement for that frame.
191 if (FLAG_trace_osr) {
192 PrintF("[patching stack checks in ");
193 function->PrintName();
194 PrintF(" for on-stack replacement]\n");
195 }
196
197 // Get the stack check stub code object to match against. We aren't
198 // prepared to generate it, but we don't expect to have to.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000199 bool found_code = false;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000200 Code* stack_check_code = NULL;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000201 if (FLAG_count_based_interrupts) {
202 InterruptStub interrupt_stub;
203 found_code = interrupt_stub.FindCodeInCache(&stack_check_code);
204 } else // NOLINT
yangguo@chromium.org56454712012-02-16 15:33:53 +0000205 { // NOLINT
206 StackCheckStub check_stub;
207 found_code = check_stub.FindCodeInCache(&stack_check_code);
208 }
209 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000211 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000212 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000213 Deoptimizer::PatchStackCheckCode(unoptimized_code,
danno@chromium.orgc612e022011-11-10 11:38:15 +0000214 stack_check_code,
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000215 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000216 }
217}
218
219
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220void RuntimeProfiler::ClearSampleBuffer() {
221 memset(sampler_window_, 0, sizeof(sampler_window_));
222 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000223}
224
225
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000227 int weight = 0;
228 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000229 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000230 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000231 bool fits = FLAG_lookup_sample_by_shared
232 ? (function->shared() == JSFunction::cast(sample)->shared())
233 : (function == JSFunction::cast(sample));
234 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000236 }
237 }
238 }
239 return weight;
240}
241
242
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000243void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 sampler_window_[sampler_window_position_] = function;
246 sampler_window_weight_[sampler_window_position_] = weight;
247 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000248 (kSamplerWindowSize - 1);
249}
250
251
252void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000254
255 // Run through the JavaScript frames and collect them. If we already
256 // have a sample of the function, we mark it for optimizations
257 // (eagerly or lazily).
258 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000259 int sample_count = 0;
260 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000261 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
262 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000263 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000264 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000265 it.Advance()) {
266 JavaScriptFrame* frame = it.frame();
267 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000268
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000269 if (!FLAG_watch_ic_patching) {
270 // Adjust threshold each time we have processed
271 // a certain number of ticks.
272 if (sampler_ticks_until_threshold_adjustment_ > 0) {
273 sampler_ticks_until_threshold_adjustment_--;
274 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
275 // If the threshold is not already at the minimum
276 // modify and reset the ticks until next adjustment.
277 if (sampler_threshold_ > kSamplerThresholdMin) {
278 sampler_threshold_ -= kSamplerThresholdDelta;
279 sampler_ticks_until_threshold_adjustment_ =
280 kSamplerTicksBetweenThresholdAdjustment;
281 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000282 }
283 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000284 }
285
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000286 SharedFunctionInfo* shared = function->shared();
287 Code* shared_code = shared->code();
288
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000289 if (shared_code->kind() != Code::FUNCTION) continue;
290
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000291 if (function->IsMarkedForLazyRecompilation() ||
292 function->IsMarkedForParallelRecompilation()) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000293 int nesting = shared_code->allow_osr_at_loop_nesting_level();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000294 if (nesting == 0) AttemptOnStackReplacement(function);
295 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000296 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000297 }
298
yangguo@chromium.org56454712012-02-16 15:33:53 +0000299 // Only record top-level code on top of the execution stack and
300 // avoid optimizing excessively large scripts since top-level code
301 // will be executed only once.
302 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000303 if (shared->is_toplevel() &&
304 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000305 continue;
306 }
307
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000308 // Do not record non-optimizable functions.
309 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000310 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000311 // If optimization was disabled due to many deoptimizations,
312 // then check if the function is hot and try to reenable optimization.
313 int ticks = shared_code->profiler_ticks();
314 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
315 shared_code->set_profiler_ticks(0);
316 shared->TryReenableOptimization();
317 } else {
318 shared_code->set_profiler_ticks(ticks + 1);
319 }
320 }
321 continue;
322 }
323 if (!function->IsOptimizable()) continue;
324
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000325 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000326 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000327
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000328 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000329 int typeinfo, total, percentage;
330 GetICCounts(function, &typeinfo, &total, &percentage);
331 if (percentage >= FLAG_type_info_threshold) {
332 // If this particular function hasn't had any ICs patched for enough
333 // ticks, optimize it now.
334 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000335 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000336 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000337 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000338 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000339 if (FLAG_trace_opt_verbose) {
340 PrintF("[not yet optimizing ");
341 function->PrintName();
342 PrintF(", not enough type info: %d/%d (%d%%)]\n",
343 typeinfo, total, percentage);
344 }
345 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000346 } else if (!any_ic_changed_ &&
danno@chromium.org129d3982012-07-25 15:01:47 +0000347 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000348 // If no IC was patched since the last tick and this function is very
349 // small, optimistically optimize it now.
350 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000351 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000352 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000353 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000354 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000355 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000356
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000357 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000358 int threshold_size_factor = (function_size > kSizeLimit)
359 ? sampler_threshold_size_factor_
360 : 1;
361
362 int threshold = sampler_threshold_ * threshold_size_factor;
363
364 if (LookupSample(function) >= threshold) {
365 Optimize(function, "sampler window lookup");
366 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000367 }
368 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000369 if (FLAG_watch_ic_patching) {
370 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000371 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000372 // Add the collected functions as samples. It's important not to do
373 // this as part of collecting them because this will interfere with
374 // the sample lookup in case of recursive functions.
375 for (int i = 0; i < sample_count; i++) {
376 AddSample(samples[i], kSamplerFrameWeight[i]);
377 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000378 }
379}
380
381
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382void RuntimeProfiler::NotifyTick() {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000383 if (FLAG_count_based_interrupts) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 isolate_->stack_guard()->RequestRuntimeProfilerTick();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385}
386
387
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000388void RuntimeProfiler::SetUp() {
389 ASSERT(has_been_globally_set_up_);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000390 if (!FLAG_watch_ic_patching) {
391 ClearSampleBuffer();
392 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000393 // If the ticker hasn't already started, make sure to do so to get
394 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000395 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396}
397
398
399void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000400 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000401 sampler_threshold_ = kSamplerThresholdInit;
402 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
403 sampler_ticks_until_threshold_adjustment_ =
404 kSamplerTicksBetweenThresholdAdjustment;
405 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000406}
407
408
409void RuntimeProfiler::TearDown() {
410 // Nothing to do.
411}
412
413
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000414int RuntimeProfiler::SamplerWindowSize() {
415 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000416}
417
418
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000419// Update the pointers in the sampler window after a GC.
420void RuntimeProfiler::UpdateSamplesAfterScavenge() {
421 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 Object* function = sampler_window_[i];
423 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000424 MapWord map_word = HeapObject::cast(function)->map_word();
425 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000426 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000427 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000429 }
430 }
431 }
432}
433
434
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000435void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000436 // The profiler thread must still be waiting.
437 ASSERT(NoBarrier_Load(&state_) >= 0);
438 // In IsolateEnteredJS we have already incremented the counter and
439 // undid the decrement done by the profiler thread. Increment again
440 // to get the right count of active isolates.
441 NoBarrier_AtomicIncrement(&state_, 1);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000442 semaphore.Pointer()->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443}
444
445
446bool RuntimeProfiler::IsSomeIsolateInJS() {
447 return NoBarrier_Load(&state_) > 0;
448}
449
450
451bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
453 ASSERT(old_state >= -1);
454 if (old_state != 0) return false;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000455 semaphore.Pointer()->Wait();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 return true;
457}
458
459
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000460void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000461 // Do a fake increment. If the profiler is waiting on the semaphore,
462 // the returned state is 0, which can be left as an initial state in
463 // case profiling is restarted later. If the profiler is not
464 // waiting, the increment will prevent it from waiting, but has to
465 // be undone after the profiler is stopped.
466 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
467 ASSERT(new_state >= 0);
468 if (new_state == 0) {
469 // The profiler thread is waiting. Wake it up. It must check for
470 // stop conditions before attempting to wait again.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000471 semaphore.Pointer()->Signal();
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000472 }
473 thread->Join();
474 // The profiler thread is now stopped. Undo the increment in case it
475 // was not waiting.
476 if (new_state != 0) {
477 NoBarrier_AtomicIncrement(&state_, -1);
478 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479}
480
481
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000482void RuntimeProfiler::RemoveDeadSamples() {
483 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000485 if (function != NULL &&
486 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000487 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000488 }
489 }
490}
491
492
493void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
494 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000495 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000496 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000497}
498
499
500bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000501 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
502 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000503 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000504 return false;
505}
506
507
508} } // namespace v8::internal