blob: ff41432b284a4c3a2d9ea9231b51b5ca5dd8c752 [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
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000083// Maximum size in bytes of generate code for a function to allow OSR.
84static const int kOSRCodeSizeAllowanceBase =
85 100 * FullCodeGenerator::kCodeSizeMultiplier;
86
87static const int kOSRCodeSizeAllowancePerTick =
88 3 * FullCodeGenerator::kCodeSizeMultiplier;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000089
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000090// Maximum size in bytes of generated code for a function to be optimized
91// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000092static const int kMaxSizeEarlyOpt =
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000093 5 * FullCodeGenerator::kCodeSizeMultiplier;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000094
kasperl@chromium.orga5551262010-12-07 12:49:48 +000095
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000096RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
97 : isolate_(isolate),
98 sampler_threshold_(kSamplerThresholdInit),
99 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
100 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000101 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000102 sampler_window_position_(0),
103 any_ic_changed_(false),
104 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000105 ClearSampleBuffer();
106}
107
108
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000109static void GetICCounts(Code* shared_code,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000110 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000111 int* ic_total_count,
112 int* percentage) {
113 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000114 *ic_with_type_info_count = 0;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000115 Object* raw_info = shared_code->type_feedback_info();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000116 if (raw_info->IsTypeFeedbackInfo()) {
117 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000118 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000119 *ic_total_count = info->ic_total_count();
120 }
121 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000122 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000123 : 100;
124}
125
126
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000127void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000128 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000129
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000130 if (FLAG_trace_opt && function->PassesHydrogenFilter()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000131 PrintF("[marking ");
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000132 function->ShortPrint();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000133 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000134 if (FLAG_type_info_threshold > 0) {
135 int typeinfo, total, percentage;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000136 GetICCounts(function->shared()->code(), &typeinfo, &total, &percentage);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000137 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
138 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000139 PrintF("]\n");
140 }
141
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000142 if (FLAG_parallel_recompilation && !isolate_->bootstrapper()->IsActive()) {
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000143 ASSERT(!function->IsMarkedForInstallingRecompiledCode());
144 ASSERT(!function->IsInRecompileQueue());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000145 function->MarkForParallelRecompilation();
146 } else {
147 // The next call to the function will trigger optimization.
148 function->MarkForLazyRecompilation();
149 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000150}
151
152
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000153void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000154 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
155 // Debug::has_break_points().
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000157 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000158 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000159 return;
160 }
161
162 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000163 // If the code is not optimizable, don't try OSR.
164 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000165
166 // We are not prepared to do OSR for a function that already has an
167 // allocated arguments object. The optimized code would bypass it for
168 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000169 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000170
171 // We're using on-stack replacement: patch the unoptimized code so that
172 // any back edge in any unoptimized frame will trigger on-stack
173 // replacement for that frame.
174 if (FLAG_trace_osr) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000175 PrintF("[patching back edges in ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000176 function->PrintName();
177 PrintF(" for on-stack replacement]\n");
178 }
179
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000180 // Get the interrupt stub code object to match against. We aren't
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000181 // prepared to generate it, but we don't expect to have to.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000182 Code* interrupt_code = NULL;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000183 InterruptStub interrupt_stub;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000184 bool found_code = interrupt_stub.FindCodeInCache(&interrupt_code, isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000185 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000186 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000187 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000188 Code* unoptimized_code = shared->code();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000189 Deoptimizer::PatchInterruptCode(
190 unoptimized_code, interrupt_code, replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000191 }
192}
193
194
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000195void RuntimeProfiler::ClearSampleBuffer() {
196 memset(sampler_window_, 0, sizeof(sampler_window_));
197 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000198}
199
200
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000202 int weight = 0;
203 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000205 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000206 bool fits = FLAG_lookup_sample_by_shared
207 ? (function->shared() == JSFunction::cast(sample)->shared())
208 : (function == JSFunction::cast(sample));
209 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000211 }
212 }
213 }
214 return weight;
215}
216
217
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000219 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220 sampler_window_[sampler_window_position_] = function;
221 sampler_window_weight_[sampler_window_position_] = weight;
222 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000223 (kSamplerWindowSize - 1);
224}
225
226
227void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000229
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000230 if (isolate_->DebuggerHasBreakPoints()) return;
231
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000232 if (FLAG_parallel_recompilation) {
233 // Take this as opportunity to process the optimizing compiler thread's
234 // output queue so that it does not unnecessarily keep objects alive.
235 isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions();
236 }
237
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000238 // Run through the JavaScript frames and collect them. If we already
239 // have a sample of the function, we mark it for optimizations
240 // (eagerly or lazily).
241 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000242 int sample_count = 0;
243 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000244 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
245 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000246 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000247 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000248 it.Advance()) {
249 JavaScriptFrame* frame = it.frame();
danno@chromium.org169691d2013-07-15 08:01:13 +0000250 JSFunction* function = frame->function();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000251
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000252 if (!FLAG_watch_ic_patching) {
253 // Adjust threshold each time we have processed
254 // a certain number of ticks.
255 if (sampler_ticks_until_threshold_adjustment_ > 0) {
256 sampler_ticks_until_threshold_adjustment_--;
257 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
258 // If the threshold is not already at the minimum
259 // modify and reset the ticks until next adjustment.
260 if (sampler_threshold_ > kSamplerThresholdMin) {
261 sampler_threshold_ -= kSamplerThresholdDelta;
262 sampler_ticks_until_threshold_adjustment_ =
263 kSamplerTicksBetweenThresholdAdjustment;
264 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000265 }
266 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000267 }
268
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000269 SharedFunctionInfo* shared = function->shared();
270 Code* shared_code = shared->code();
271
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000272 if (shared_code->kind() != Code::FUNCTION) continue;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000273 if (function->IsInRecompileQueue()) continue;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000274
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000275 if (FLAG_always_osr &&
276 shared_code->allow_osr_at_loop_nesting_level() == 0) {
277 // Testing mode: always try an OSR compile for every function.
278 for (int i = 0; i < Code::kMaxLoopNestingMarker; i++) {
279 // TODO(titzer): fix AttemptOnStackReplacement to avoid this dumb loop.
280 shared_code->set_allow_osr_at_loop_nesting_level(i);
281 AttemptOnStackReplacement(function);
282 }
283 // Fall through and do a normal optimized compile as well.
284 } else if (!frame->is_optimized() &&
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000285 (function->IsMarkedForLazyRecompilation() ||
286 function->IsMarkedForParallelRecompilation() ||
287 function->IsOptimized())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000288 // Attempt OSR if we are still running unoptimized code even though the
289 // the function has long been marked or even already been optimized.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000290 int ticks = shared_code->profiler_ticks();
291 int allowance = kOSRCodeSizeAllowanceBase +
292 ticks * kOSRCodeSizeAllowancePerTick;
293 if (shared_code->CodeSize() > allowance) {
294 if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1);
295 } else {
296 int nesting = shared_code->allow_osr_at_loop_nesting_level();
297 if (nesting < Code::kMaxLoopNestingMarker) {
298 int new_nesting = nesting + 1;
299 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
300 AttemptOnStackReplacement(function);
301 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000302 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000303 continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000304 }
305
yangguo@chromium.org56454712012-02-16 15:33:53 +0000306 // Only record top-level code on top of the execution stack and
307 // avoid optimizing excessively large scripts since top-level code
308 // will be executed only once.
309 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000310 if (shared->is_toplevel() &&
311 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000312 continue;
313 }
314
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000315 // Do not record non-optimizable functions.
316 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000317 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000318 // If optimization was disabled due to many deoptimizations,
319 // then check if the function is hot and try to reenable optimization.
320 int ticks = shared_code->profiler_ticks();
321 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
322 shared_code->set_profiler_ticks(0);
323 shared->TryReenableOptimization();
324 } else {
325 shared_code->set_profiler_ticks(ticks + 1);
326 }
327 }
328 continue;
329 }
330 if (!function->IsOptimizable()) continue;
331
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000332 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000333 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000334
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000335 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000336 int typeinfo, total, percentage;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000337 GetICCounts(shared_code, &typeinfo, &total, &percentage);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000338 if (percentage >= FLAG_type_info_threshold) {
339 // If this particular function hasn't had any ICs patched for enough
340 // ticks, optimize it now.
341 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000342 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000343 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000344 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000345 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000346 if (FLAG_trace_opt_verbose) {
347 PrintF("[not yet optimizing ");
348 function->PrintName();
349 PrintF(", not enough type info: %d/%d (%d%%)]\n",
350 typeinfo, total, percentage);
351 }
352 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000353 } else if (!any_ic_changed_ &&
danno@chromium.org129d3982012-07-25 15:01:47 +0000354 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000355 // If no IC was patched since the last tick and this function is very
356 // small, optimistically optimize it now.
357 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000358 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000359 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000360 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000361 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000362 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000363
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000364 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000365 int threshold_size_factor = (function_size > kSizeLimit)
366 ? sampler_threshold_size_factor_
367 : 1;
368
369 int threshold = sampler_threshold_ * threshold_size_factor;
370
371 if (LookupSample(function) >= threshold) {
372 Optimize(function, "sampler window lookup");
373 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374 }
375 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000376 if (FLAG_watch_ic_patching) {
377 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000378 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000379 // Add the collected functions as samples. It's important not to do
380 // this as part of collecting them because this will interfere with
381 // the sample lookup in case of recursive functions.
382 for (int i = 0; i < sample_count; i++) {
383 AddSample(samples[i], kSamplerFrameWeight[i]);
384 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385 }
386}
387
388
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000389void RuntimeProfiler::SetUp() {
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}
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
432void RuntimeProfiler::RemoveDeadSamples() {
433 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000435 if (function != NULL &&
436 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000437 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000438 }
439 }
440}
441
442
443void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
444 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000445 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000446 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000447}
448
449
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000450} } // namespace v8::internal