blob: 7c900b37d90a6aa87c9ce2f2cb3c124f2268bd7b [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"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#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
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000082// Maximum size in bytes of generate code for a function to allow OSR.
83static const int kOSRCodeSizeAllowanceBase =
84 100 * FullCodeGenerator::kCodeSizeMultiplier;
85
86static const int kOSRCodeSizeAllowancePerTick =
87 3 * FullCodeGenerator::kCodeSizeMultiplier;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000088
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000089// Maximum size in bytes of generated code for a function to be optimized
90// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000091static const int kMaxSizeEarlyOpt =
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000092 5 * FullCodeGenerator::kCodeSizeMultiplier;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000093
kasperl@chromium.orga5551262010-12-07 12:49:48 +000094
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
96 : isolate_(isolate),
97 sampler_threshold_(kSamplerThresholdInit),
98 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
99 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000100 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000101 sampler_window_position_(0),
102 any_ic_changed_(false),
103 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000104 ClearSampleBuffer();
105}
106
107
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000108static void GetICCounts(Code* shared_code,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000109 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000110 int* ic_total_count,
111 int* percentage) {
112 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000113 *ic_with_type_info_count = 0;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000114 Object* raw_info = shared_code->type_feedback_info();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000115 if (raw_info->IsTypeFeedbackInfo()) {
116 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000117 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000118 *ic_total_count = info->ic_total_count();
119 }
120 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000121 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000122 : 100;
123}
124
125
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000126void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000127 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000128
jkummerow@chromium.org1e8da742013-08-26 17:13:35 +0000129 if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000130 PrintF("[marking ");
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000131 function->ShortPrint();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000132 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000133 if (FLAG_type_info_threshold > 0) {
134 int typeinfo, total, percentage;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000135 GetICCounts(function->shared()->code(), &typeinfo, &total, &percentage);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000136 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
137 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000138 PrintF("]\n");
139 }
140
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000141
rossberg@chromium.org92597162013-08-23 13:28:00 +0000142 if (FLAG_concurrent_recompilation && !isolate_->bootstrapper()->IsActive()) {
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000143 if (FLAG_concurrent_osr &&
144 isolate_->optimizing_compiler_thread()->IsQueuedForOSR(function)) {
145 // Do not attempt regular recompilation if we already queued this for OSR.
146 // TODO(yangguo): This is necessary so that we don't install optimized
147 // code on a function that is already optimized, since OSR and regular
148 // recompilation race. This goes away as soon as OSR becomes one-shot.
149 return;
150 }
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000151 ASSERT(!function->IsInRecompileQueue());
rossberg@chromium.org92597162013-08-23 13:28:00 +0000152 function->MarkForConcurrentRecompilation();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000153 } else {
154 // The next call to the function will trigger optimization.
155 function->MarkForLazyRecompilation();
156 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000157}
158
159
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000160void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000161 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
162 // Debug::has_break_points().
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000163 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000164 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000165 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000166 return;
167 }
168
169 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000170 // If the code is not optimizable, don't try OSR.
171 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000172
173 // We are not prepared to do OSR for a function that already has an
174 // allocated arguments object. The optimized code would bypass it for
175 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000176 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000177
178 // We're using on-stack replacement: patch the unoptimized code so that
179 // any back edge in any unoptimized frame will trigger on-stack
180 // replacement for that frame.
181 if (FLAG_trace_osr) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000182 PrintF("[OSR - patching back edges in ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000183 function->PrintName();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000184 PrintF("]\n");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000185 }
186
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000187 BackEdgeTable::Patch(isolate_, shared->code());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000188}
189
190
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000191void RuntimeProfiler::ClearSampleBuffer() {
192 memset(sampler_window_, 0, sizeof(sampler_window_));
193 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000194}
195
196
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000197int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000198 int weight = 0;
199 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000201 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000202 bool fits = FLAG_lookup_sample_by_shared
203 ? (function->shared() == JSFunction::cast(sample)->shared())
204 : (function == JSFunction::cast(sample));
205 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000207 }
208 }
209 }
210 return weight;
211}
212
213
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000214void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000215 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 sampler_window_[sampler_window_position_] = function;
217 sampler_window_weight_[sampler_window_position_] = weight;
218 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000219 (kSamplerWindowSize - 1);
220}
221
222
223void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000224 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000225
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000226 if (isolate_->DebuggerHasBreakPoints()) return;
227
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000228 DisallowHeapAllocation no_gc;
229
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000230 // Run through the JavaScript frames and collect them. If we already
231 // have a sample of the function, we mark it for optimizations
232 // (eagerly or lazily).
233 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000234 int sample_count = 0;
235 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000236 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
237 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000238 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000239 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000240 it.Advance()) {
241 JavaScriptFrame* frame = it.frame();
danno@chromium.org169691d2013-07-15 08:01:13 +0000242 JSFunction* function = frame->function();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000243
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000244 if (!FLAG_watch_ic_patching) {
245 // Adjust threshold each time we have processed
246 // a certain number of ticks.
247 if (sampler_ticks_until_threshold_adjustment_ > 0) {
248 sampler_ticks_until_threshold_adjustment_--;
249 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
250 // If the threshold is not already at the minimum
251 // modify and reset the ticks until next adjustment.
252 if (sampler_threshold_ > kSamplerThresholdMin) {
253 sampler_threshold_ -= kSamplerThresholdDelta;
254 sampler_ticks_until_threshold_adjustment_ =
255 kSamplerTicksBetweenThresholdAdjustment;
256 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000257 }
258 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000259 }
260
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000261 SharedFunctionInfo* shared = function->shared();
262 Code* shared_code = shared->code();
263
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000264 if (shared_code->kind() != Code::FUNCTION) continue;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000265 if (function->IsInRecompileQueue()) continue;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000266
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000267 if (FLAG_always_osr &&
268 shared_code->allow_osr_at_loop_nesting_level() == 0) {
269 // Testing mode: always try an OSR compile for every function.
270 for (int i = 0; i < Code::kMaxLoopNestingMarker; i++) {
271 // TODO(titzer): fix AttemptOnStackReplacement to avoid this dumb loop.
272 shared_code->set_allow_osr_at_loop_nesting_level(i);
273 AttemptOnStackReplacement(function);
274 }
275 // Fall through and do a normal optimized compile as well.
276 } else if (!frame->is_optimized() &&
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000277 (function->IsMarkedForLazyRecompilation() ||
rossberg@chromium.org92597162013-08-23 13:28:00 +0000278 function->IsMarkedForConcurrentRecompilation() ||
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000279 function->IsOptimized())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000280 // Attempt OSR if we are still running unoptimized code even though the
281 // the function has long been marked or even already been optimized.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000282 int ticks = shared_code->profiler_ticks();
283 int allowance = kOSRCodeSizeAllowanceBase +
284 ticks * kOSRCodeSizeAllowancePerTick;
285 if (shared_code->CodeSize() > allowance) {
286 if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1);
287 } else {
288 int nesting = shared_code->allow_osr_at_loop_nesting_level();
289 if (nesting < Code::kMaxLoopNestingMarker) {
290 int new_nesting = nesting + 1;
291 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
292 AttemptOnStackReplacement(function);
293 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000294 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000295 continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000296 }
297
yangguo@chromium.org56454712012-02-16 15:33:53 +0000298 // Only record top-level code on top of the execution stack and
299 // avoid optimizing excessively large scripts since top-level code
300 // will be executed only once.
301 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000302 if (shared->is_toplevel() &&
303 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000304 continue;
305 }
306
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000307 // Do not record non-optimizable functions.
308 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000309 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000310 // If optimization was disabled due to many deoptimizations,
311 // then check if the function is hot and try to reenable optimization.
312 int ticks = shared_code->profiler_ticks();
313 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
314 shared_code->set_profiler_ticks(0);
315 shared->TryReenableOptimization();
316 } else {
317 shared_code->set_profiler_ticks(ticks + 1);
318 }
319 }
320 continue;
321 }
322 if (!function->IsOptimizable()) continue;
323
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000324 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000325 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000326
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000327 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000328 int typeinfo, total, percentage;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000329 GetICCounts(shared_code, &typeinfo, &total, &percentage);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000330 if (percentage >= FLAG_type_info_threshold) {
331 // If this particular function hasn't had any ICs patched for enough
332 // ticks, optimize it now.
333 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000334 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000335 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000336 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000337 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000338 if (FLAG_trace_opt_verbose) {
339 PrintF("[not yet optimizing ");
340 function->PrintName();
341 PrintF(", not enough type info: %d/%d (%d%%)]\n",
342 typeinfo, total, percentage);
343 }
344 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000345 } else if (!any_ic_changed_ &&
danno@chromium.org129d3982012-07-25 15:01:47 +0000346 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000347 // If no IC was patched since the last tick and this function is very
348 // small, optimistically optimize it now.
349 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000350 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000351 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000352 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000353 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000354 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000355
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000356 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000357 int threshold_size_factor = (function_size > kSizeLimit)
358 ? sampler_threshold_size_factor_
359 : 1;
360
361 int threshold = sampler_threshold_ * threshold_size_factor;
362
363 if (LookupSample(function) >= threshold) {
364 Optimize(function, "sampler window lookup");
365 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000366 }
367 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000368 if (FLAG_watch_ic_patching) {
369 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000370 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000371 // Add the collected functions as samples. It's important not to do
372 // this as part of collecting them because this will interfere with
373 // the sample lookup in case of recursive functions.
374 for (int i = 0; i < sample_count; i++) {
375 AddSample(samples[i], kSamplerFrameWeight[i]);
376 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377 }
378}
379
380
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000381void RuntimeProfiler::SetUp() {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000382 if (!FLAG_watch_ic_patching) {
383 ClearSampleBuffer();
384 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000385}
386
387
388void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000389 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000390 sampler_threshold_ = kSamplerThresholdInit;
391 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
392 sampler_ticks_until_threshold_adjustment_ =
393 kSamplerTicksBetweenThresholdAdjustment;
394 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000395}
396
397
398void RuntimeProfiler::TearDown() {
399 // Nothing to do.
400}
401
402
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000403// Update the pointers in the sampler window after a GC.
404void RuntimeProfiler::UpdateSamplesAfterScavenge() {
405 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406 Object* function = sampler_window_[i];
407 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000408 MapWord map_word = HeapObject::cast(function)->map_word();
409 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000411 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000412 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000413 }
414 }
415 }
416}
417
418
419void RuntimeProfiler::RemoveDeadSamples() {
420 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000421 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000422 if (function != NULL &&
423 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000425 }
426 }
427}
428
429
430void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
431 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000433 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000434}
435
436
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000437} } // namespace v8::internal