blob: 95e86b119c303ac8794e97a4416d8c5a6008cf49 [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"
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +000033#include "bootstrapper.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "code-stubs.h"
35#include "compilation-cache.h"
36#include "deoptimizer.h"
37#include "execution.h"
danno@chromium.org129d3982012-07-25 15:01:47 +000038#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000039#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000040#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000041#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000042#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000044
45namespace v8 {
46namespace internal {
47
48
kasperl@chromium.orga5551262010-12-07 12:49:48 +000049// Optimization sampler constants.
50static const int kSamplerFrameCount = 2;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000051
52// Constants for statistical profiler.
kasperl@chromium.orga5551262010-12-07 12:49:48 +000053static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000054
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000055static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000056
57static const int kSamplerThresholdInit = 3;
58static const int kSamplerThresholdMin = 1;
59static const int kSamplerThresholdDelta = 1;
60
61static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000062
63static const int kSizeLimit = 1500;
64
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000065// Constants for counter based profiler.
66
67// Number of times a function has to be seen on the stack before it is
68// optimized.
69static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000070// If the function optimization was disabled due to high deoptimization count,
71// but the function is hot and has been seen on the stack this number of times,
72// then we try to reenable optimization for this function.
73static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000074// If a function does not have enough type info (according to
75// FLAG_type_info_threshold), but has seen a huge number of ticks,
76// optimize it as it is.
77static const int kTicksWhenNotEnoughTypeInfo = 100;
78// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000079STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
80STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000081STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000082
mmassi@chromium.org7028c052012-06-13 11:51:58 +000083
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000084// Maximum size in bytes of generated code for a function to be optimized
85// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000086static const int kMaxSizeEarlyOpt =
87 5 * FullCodeGenerator::kBackEdgeDistanceUnit;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000088
kasperl@chromium.orga5551262010-12-07 12:49:48 +000089
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
91 : isolate_(isolate),
92 sampler_threshold_(kSamplerThresholdInit),
93 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
94 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +000095 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000096 sampler_window_position_(0),
97 any_ic_changed_(false),
98 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099 ClearSampleBuffer();
100}
101
102
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000103static void GetICCounts(JSFunction* function,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000104 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000105 int* ic_total_count,
106 int* percentage) {
107 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000108 *ic_with_type_info_count = 0;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000109 Object* raw_info =
110 function->shared()->code()->type_feedback_info();
111 if (raw_info->IsTypeFeedbackInfo()) {
112 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000113 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000114 *ic_total_count = info->ic_total_count();
115 }
116 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000117 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000118 : 100;
119}
120
121
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000122void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000123 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000124
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000125 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000126 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000127 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000128 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000129 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000130 if (FLAG_type_info_threshold > 0) {
131 int typeinfo, total, percentage;
132 GetICCounts(function, &typeinfo, &total, &percentage);
133 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
134 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000135 PrintF("]\n");
136 }
137
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000138 if (FLAG_parallel_recompilation && !isolate_->bootstrapper()->IsActive()) {
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000139 ASSERT(!function->IsMarkedForInstallingRecompiledCode());
140 ASSERT(!function->IsInRecompileQueue());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000141 function->MarkForParallelRecompilation();
142 } else {
143 // The next call to the function will trigger optimization.
144 function->MarkForLazyRecompilation();
145 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000146}
147
148
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000149void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000150 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
151 // Debug::has_break_points().
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000152 ASSERT(function->IsMarkedForLazyRecompilation() ||
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000153 function->IsMarkedForParallelRecompilation() ||
154 function->IsOptimized());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000156 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000157 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000158 return;
159 }
160
161 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000162 // If the code is not optimizable, don't try OSR.
163 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000164
165 // We are not prepared to do OSR for a function that already has an
166 // allocated arguments object. The optimized code would bypass it for
167 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000168 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000169
170 // We're using on-stack replacement: patch the unoptimized code so that
171 // any back edge in any unoptimized frame will trigger on-stack
172 // replacement for that frame.
173 if (FLAG_trace_osr) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000174 PrintF("[patching back edges in ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000175 function->PrintName();
176 PrintF(" for on-stack replacement]\n");
177 }
178
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000179 // Get the interrupt stub code object to match against. We aren't
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000180 // prepared to generate it, but we don't expect to have to.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000181 Code* interrupt_code = NULL;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000182 InterruptStub interrupt_stub;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000183 bool found_code = interrupt_stub.FindCodeInCache(&interrupt_code, isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000184 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000185 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000186 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000187 Code* unoptimized_code = shared->code();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000188 Deoptimizer::PatchInterruptCode(
189 unoptimized_code, interrupt_code, replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000190 }
191}
192
193
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000194void RuntimeProfiler::ClearSampleBuffer() {
195 memset(sampler_window_, 0, sizeof(sampler_window_));
196 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000197}
198
199
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000201 int weight = 0;
202 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000203 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000204 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000205 bool fits = FLAG_lookup_sample_by_shared
206 ? (function->shared() == JSFunction::cast(sample)->shared())
207 : (function == JSFunction::cast(sample));
208 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000209 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000210 }
211 }
212 }
213 return weight;
214}
215
216
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000218 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 sampler_window_[sampler_window_position_] = function;
220 sampler_window_weight_[sampler_window_position_] = weight;
221 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000222 (kSamplerWindowSize - 1);
223}
224
225
226void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000228
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000229 if (FLAG_parallel_recompilation) {
230 // Take this as opportunity to process the optimizing compiler thread's
231 // output queue so that it does not unnecessarily keep objects alive.
232 isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions();
233 }
234
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000235 // Run through the JavaScript frames and collect them. If we already
236 // have a sample of the function, we mark it for optimizations
237 // (eagerly or lazily).
238 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000239 int sample_count = 0;
240 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000241 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
242 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000243 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000244 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000245 it.Advance()) {
246 JavaScriptFrame* frame = it.frame();
247 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000248
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000249 if (!FLAG_watch_ic_patching) {
250 // Adjust threshold each time we have processed
251 // a certain number of ticks.
252 if (sampler_ticks_until_threshold_adjustment_ > 0) {
253 sampler_ticks_until_threshold_adjustment_--;
254 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
255 // If the threshold is not already at the minimum
256 // modify and reset the ticks until next adjustment.
257 if (sampler_threshold_ > kSamplerThresholdMin) {
258 sampler_threshold_ -= kSamplerThresholdDelta;
259 sampler_ticks_until_threshold_adjustment_ =
260 kSamplerTicksBetweenThresholdAdjustment;
261 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000262 }
263 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000264 }
265
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000266 SharedFunctionInfo* shared = function->shared();
267 Code* shared_code = shared->code();
268
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000269 if (shared_code->kind() != Code::FUNCTION) continue;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000270 if (function->IsInRecompileQueue()) continue;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000271
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000272 // Attempt OSR if we are still running unoptimized code even though the
273 // the function has long been marked or even already been optimized.
274 if (!frame->is_optimized() &&
275 (function->IsMarkedForLazyRecompilation() ||
276 function->IsMarkedForParallelRecompilation() ||
277 function->IsOptimized())) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000278 int nesting = shared_code->allow_osr_at_loop_nesting_level();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000279 if (nesting < Code::kMaxLoopNestingMarker) {
280 int new_nesting = nesting + 1;
281 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
282 AttemptOnStackReplacement(function);
283 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000284 }
285
yangguo@chromium.org56454712012-02-16 15:33:53 +0000286 // Only record top-level code on top of the execution stack and
287 // avoid optimizing excessively large scripts since top-level code
288 // will be executed only once.
289 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000290 if (shared->is_toplevel() &&
291 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000292 continue;
293 }
294
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000295 // Do not record non-optimizable functions.
296 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000297 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000298 // If optimization was disabled due to many deoptimizations,
299 // then check if the function is hot and try to reenable optimization.
300 int ticks = shared_code->profiler_ticks();
301 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
302 shared_code->set_profiler_ticks(0);
303 shared->TryReenableOptimization();
304 } else {
305 shared_code->set_profiler_ticks(ticks + 1);
306 }
307 }
308 continue;
309 }
310 if (!function->IsOptimizable()) continue;
311
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000312 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000313 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000314
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000315 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000316 int typeinfo, total, percentage;
317 GetICCounts(function, &typeinfo, &total, &percentage);
318 if (percentage >= FLAG_type_info_threshold) {
319 // If this particular function hasn't had any ICs patched for enough
320 // ticks, optimize it now.
321 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000322 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000323 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000324 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000325 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000326 if (FLAG_trace_opt_verbose) {
327 PrintF("[not yet optimizing ");
328 function->PrintName();
329 PrintF(", not enough type info: %d/%d (%d%%)]\n",
330 typeinfo, total, percentage);
331 }
332 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000333 } else if (!any_ic_changed_ &&
danno@chromium.org129d3982012-07-25 15:01:47 +0000334 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000335 // If no IC was patched since the last tick and this function is very
336 // small, optimistically optimize it now.
337 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000338 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000339 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000340 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000341 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000342 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000343
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000344 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000345 int threshold_size_factor = (function_size > kSizeLimit)
346 ? sampler_threshold_size_factor_
347 : 1;
348
349 int threshold = sampler_threshold_ * threshold_size_factor;
350
351 if (LookupSample(function) >= threshold) {
352 Optimize(function, "sampler window lookup");
353 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354 }
355 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000356 if (FLAG_watch_ic_patching) {
357 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000358 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000359 // Add the collected functions as samples. It's important not to do
360 // this as part of collecting them because this will interfere with
361 // the sample lookup in case of recursive functions.
362 for (int i = 0; i < sample_count; i++) {
363 AddSample(samples[i], kSamplerFrameWeight[i]);
364 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000365 }
366}
367
368
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000369void RuntimeProfiler::SetUp() {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000370 if (!FLAG_watch_ic_patching) {
371 ClearSampleBuffer();
372 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373}
374
375
376void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000377 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000378 sampler_threshold_ = kSamplerThresholdInit;
379 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
380 sampler_ticks_until_threshold_adjustment_ =
381 kSamplerTicksBetweenThresholdAdjustment;
382 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000383}
384
385
386void RuntimeProfiler::TearDown() {
387 // Nothing to do.
388}
389
390
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000391int RuntimeProfiler::SamplerWindowSize() {
392 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000393}
394
395
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000396// Update the pointers in the sampler window after a GC.
397void RuntimeProfiler::UpdateSamplesAfterScavenge() {
398 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000399 Object* function = sampler_window_[i];
400 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000401 MapWord map_word = HeapObject::cast(function)->map_word();
402 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000403 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000404 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000406 }
407 }
408 }
409}
410
411
412void RuntimeProfiler::RemoveDeadSamples() {
413 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000415 if (function != NULL &&
416 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000418 }
419 }
420}
421
422
423void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
424 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000425 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000426 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427}
428
429
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000430} } // namespace v8::internal