blob: 1856359f764143c686fd8d68a10ac83ebf03f4b9 [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"
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;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000049
50// Constants for statistical profiler.
kasperl@chromium.orga5551262010-12-07 12:49:48 +000051static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000053static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000054
55static const int kSamplerThresholdInit = 3;
56static const int kSamplerThresholdMin = 1;
57static const int kSamplerThresholdDelta = 1;
58
59static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000060
61static const int kSizeLimit = 1500;
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063// Constants for counter based profiler.
64
65// Number of times a function has to be seen on the stack before it is
66// optimized.
67static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000068// If the function optimization was disabled due to high deoptimization count,
69// but the function is hot and has been seen on the stack this number of times,
70// then we try to reenable optimization for this function.
71static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000072// If a function does not have enough type info (according to
73// FLAG_type_info_threshold), but has seen a huge number of ticks,
74// optimize it as it is.
75static const int kTicksWhenNotEnoughTypeInfo = 100;
76// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000077STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
78STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000079STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000080
mmassi@chromium.org7028c052012-06-13 11:51:58 +000081
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000082// Maximum size in bytes of generated code for a function to be optimized
83// the very first time it is seen on the stack.
84static const int kMaxSizeEarlyOpt = 500;
85
kasperl@chromium.orga5551262010-12-07 12:49:48 +000086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087Atomic32 RuntimeProfiler::state_ = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000088
89// TODO(isolates): Clean up the semaphore when it is no longer required.
90static LazySemaphore<0>::type semaphore = LAZY_SEMAPHORE_INITIALIZER;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000091
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000092#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000093bool RuntimeProfiler::has_been_globally_set_up_ = false;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000094#endif
95bool RuntimeProfiler::enabled_ = false;
96
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097
98RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
99 : isolate_(isolate),
100 sampler_threshold_(kSamplerThresholdInit),
101 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
102 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000103 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000104 sampler_window_position_(0),
105 any_ic_changed_(false),
106 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 ClearSampleBuffer();
108}
109
110
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000111void RuntimeProfiler::GlobalSetUp() {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000112 ASSERT(!has_been_globally_set_up_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000113 enabled_ = V8::UseCrankshaft() && FLAG_opt;
114#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000115 has_been_globally_set_up_ = true;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000116#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117}
118
119
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000120static void GetICCounts(JSFunction* function,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000121 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000122 int* ic_total_count,
123 int* percentage) {
124 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000125 *ic_with_type_info_count = 0;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000126 Object* raw_info =
127 function->shared()->code()->type_feedback_info();
128 if (raw_info->IsTypeFeedbackInfo()) {
129 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000130 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000131 *ic_total_count = info->ic_total_count();
132 }
133 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000134 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000135 : 100;
136}
137
138
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000139void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000140 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000141 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000142 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000143 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000144 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000145 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000146 if (FLAG_type_info_threshold > 0) {
147 int typeinfo, total, percentage;
148 GetICCounts(function, &typeinfo, &total, &percentage);
149 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
150 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000151 PrintF("]\n");
152 }
153
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000154 if (FLAG_parallel_recompilation) {
155 function->MarkForParallelRecompilation();
156 } else {
157 // The next call to the function will trigger optimization.
158 function->MarkForLazyRecompilation();
159 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000160}
161
162
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000163void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000164 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
165 // Debug::has_break_points().
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000166 ASSERT(function->IsMarkedForLazyRecompilation() ||
167 function->IsMarkedForParallelRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000169 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000171 return;
172 }
173
174 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000175 // If the code is not optimizable, don't try OSR.
176 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000177
178 // We are not prepared to do OSR for a function that already has an
179 // allocated arguments object. The optimized code would bypass it for
180 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000181 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000182
183 // We're using on-stack replacement: patch the unoptimized code so that
184 // any back edge in any unoptimized frame will trigger on-stack
185 // replacement for that frame.
186 if (FLAG_trace_osr) {
187 PrintF("[patching stack checks in ");
188 function->PrintName();
189 PrintF(" for on-stack replacement]\n");
190 }
191
192 // Get the stack check stub code object to match against. We aren't
193 // prepared to generate it, but we don't expect to have to.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000194 bool found_code = false;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000195 Code* stack_check_code = NULL;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000196 if (FLAG_count_based_interrupts) {
197 InterruptStub interrupt_stub;
198 found_code = interrupt_stub.FindCodeInCache(&stack_check_code);
199 } else // NOLINT
yangguo@chromium.org56454712012-02-16 15:33:53 +0000200 { // NOLINT
201 StackCheckStub check_stub;
202 found_code = check_stub.FindCodeInCache(&stack_check_code);
203 }
204 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000205 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000206 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000207 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000208 Deoptimizer::PatchStackCheckCode(unoptimized_code,
danno@chromium.orgc612e022011-11-10 11:38:15 +0000209 stack_check_code,
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000210 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000211 }
212}
213
214
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000215void RuntimeProfiler::ClearSampleBuffer() {
216 memset(sampler_window_, 0, sizeof(sampler_window_));
217 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000218}
219
220
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000222 int weight = 0;
223 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000224 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000225 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000226 bool fits = FLAG_lookup_sample_by_shared
227 ? (function->shared() == JSFunction::cast(sample)->shared())
228 : (function == JSFunction::cast(sample));
229 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000230 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000231 }
232 }
233 }
234 return weight;
235}
236
237
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000238void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000239 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000240 sampler_window_[sampler_window_position_] = function;
241 sampler_window_weight_[sampler_window_position_] = weight;
242 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000243 (kSamplerWindowSize - 1);
244}
245
246
247void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000248 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000249
250 // Run through the JavaScript frames and collect them. If we already
251 // have a sample of the function, we mark it for optimizations
252 // (eagerly or lazily).
253 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000254 int sample_count = 0;
255 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000256 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
257 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000258 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000259 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000260 it.Advance()) {
261 JavaScriptFrame* frame = it.frame();
262 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000263
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000264 if (!FLAG_watch_ic_patching) {
265 // Adjust threshold each time we have processed
266 // a certain number of ticks.
267 if (sampler_ticks_until_threshold_adjustment_ > 0) {
268 sampler_ticks_until_threshold_adjustment_--;
269 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
270 // If the threshold is not already at the minimum
271 // modify and reset the ticks until next adjustment.
272 if (sampler_threshold_ > kSamplerThresholdMin) {
273 sampler_threshold_ -= kSamplerThresholdDelta;
274 sampler_ticks_until_threshold_adjustment_ =
275 kSamplerTicksBetweenThresholdAdjustment;
276 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000277 }
278 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000279 }
280
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000281 SharedFunctionInfo* shared = function->shared();
282 Code* shared_code = shared->code();
283
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000284 if (shared_code->kind() != Code::FUNCTION) continue;
285
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000286 if (function->IsMarkedForLazyRecompilation() ||
287 function->IsMarkedForParallelRecompilation()) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000288 int nesting = shared_code->allow_osr_at_loop_nesting_level();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000289 if (nesting == 0) AttemptOnStackReplacement(function);
290 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000291 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000292 }
293
yangguo@chromium.org56454712012-02-16 15:33:53 +0000294 // Only record top-level code on top of the execution stack and
295 // avoid optimizing excessively large scripts since top-level code
296 // will be executed only once.
297 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000298 if (shared->is_toplevel() &&
299 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000300 continue;
301 }
302
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000303 // Do not record non-optimizable functions.
304 if (shared->optimization_disabled()) {
305 if (shared->deopt_count() >= Compiler::kDefaultMaxOptCount) {
306 // If optimization was disabled due to many deoptimizations,
307 // then check if the function is hot and try to reenable optimization.
308 int ticks = shared_code->profiler_ticks();
309 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
310 shared_code->set_profiler_ticks(0);
311 shared->TryReenableOptimization();
312 } else {
313 shared_code->set_profiler_ticks(ticks + 1);
314 }
315 }
316 continue;
317 }
318 if (!function->IsOptimizable()) continue;
319
320
321
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000322 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000323 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000324
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000325 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000326 int typeinfo, total, percentage;
327 GetICCounts(function, &typeinfo, &total, &percentage);
328 if (percentage >= FLAG_type_info_threshold) {
329 // If this particular function hasn't had any ICs patched for enough
330 // ticks, optimize it now.
331 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000332 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000333 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000334 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000335 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000336 if (FLAG_trace_opt_verbose) {
337 PrintF("[not yet optimizing ");
338 function->PrintName();
339 PrintF(", not enough type info: %d/%d (%d%%)]\n",
340 typeinfo, total, percentage);
341 }
342 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000343 } else if (!any_ic_changed_ &&
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000344 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000345 // If no IC was patched since the last tick and this function is very
346 // small, optimistically optimize it now.
347 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000348 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000349 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000350 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000351 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000352 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000353
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000354 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000355 int threshold_size_factor = (function_size > kSizeLimit)
356 ? sampler_threshold_size_factor_
357 : 1;
358
359 int threshold = sampler_threshold_ * threshold_size_factor;
360
361 if (LookupSample(function) >= threshold) {
362 Optimize(function, "sampler window lookup");
363 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000364 }
365 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000366 if (FLAG_watch_ic_patching) {
367 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000368 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000369 // Add the collected functions as samples. It's important not to do
370 // this as part of collecting them because this will interfere with
371 // the sample lookup in case of recursive functions.
372 for (int i = 0; i < sample_count; i++) {
373 AddSample(samples[i], kSamplerFrameWeight[i]);
374 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000375 }
376}
377
378
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000379void RuntimeProfiler::NotifyTick() {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000380 if (FLAG_count_based_interrupts) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000381 isolate_->stack_guard()->RequestRuntimeProfilerTick();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382}
383
384
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000385void RuntimeProfiler::SetUp() {
386 ASSERT(has_been_globally_set_up_);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000387 if (!FLAG_watch_ic_patching) {
388 ClearSampleBuffer();
389 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000390 // If the ticker hasn't already started, make sure to do so to get
391 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000393}
394
395
396void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000397 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000398 sampler_threshold_ = kSamplerThresholdInit;
399 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
400 sampler_ticks_until_threshold_adjustment_ =
401 kSamplerTicksBetweenThresholdAdjustment;
402 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000403}
404
405
406void RuntimeProfiler::TearDown() {
407 // Nothing to do.
408}
409
410
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000411int RuntimeProfiler::SamplerWindowSize() {
412 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000413}
414
415
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000416// Update the pointers in the sampler window after a GC.
417void RuntimeProfiler::UpdateSamplesAfterScavenge() {
418 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000419 Object* function = sampler_window_[i];
420 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000421 MapWord map_word = HeapObject::cast(function)->map_word();
422 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000424 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000426 }
427 }
428 }
429}
430
431
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 // The profiler thread must still be waiting.
434 ASSERT(NoBarrier_Load(&state_) >= 0);
435 // In IsolateEnteredJS we have already incremented the counter and
436 // undid the decrement done by the profiler thread. Increment again
437 // to get the right count of active isolates.
438 NoBarrier_AtomicIncrement(&state_, 1);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000439 semaphore.Pointer()->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000440}
441
442
443bool RuntimeProfiler::IsSomeIsolateInJS() {
444 return NoBarrier_Load(&state_) > 0;
445}
446
447
448bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000449 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
450 ASSERT(old_state >= -1);
451 if (old_state != 0) return false;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000452 semaphore.Pointer()->Wait();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 return true;
454}
455
456
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000457void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000458 // Do a fake increment. If the profiler is waiting on the semaphore,
459 // the returned state is 0, which can be left as an initial state in
460 // case profiling is restarted later. If the profiler is not
461 // waiting, the increment will prevent it from waiting, but has to
462 // be undone after the profiler is stopped.
463 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
464 ASSERT(new_state >= 0);
465 if (new_state == 0) {
466 // The profiler thread is waiting. Wake it up. It must check for
467 // stop conditions before attempting to wait again.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000468 semaphore.Pointer()->Signal();
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000469 }
470 thread->Join();
471 // The profiler thread is now stopped. Undo the increment in case it
472 // was not waiting.
473 if (new_state != 0) {
474 NoBarrier_AtomicIncrement(&state_, -1);
475 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476}
477
478
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000479void RuntimeProfiler::RemoveDeadSamples() {
480 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000482 if (function != NULL &&
483 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000485 }
486 }
487}
488
489
490void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
491 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000493 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494}
495
496
497bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000498 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
499 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000500 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000501 return false;
502}
503
504
505} } // namespace v8::internal