blob: 752d79c982a781655d1984e773b0245424e19085 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "runtime-profiler.h"
31
32#include "assembler.h"
33#include "code-stubs.h"
34#include "compilation-cache.h"
35#include "deoptimizer.h"
36#include "execution.h"
danno@chromium.org129d3982012-07-25 15:01:47 +000037#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000039#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000040#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000041#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043
44namespace v8 {
45namespace internal {
46
47
kasperl@chromium.orga5551262010-12-07 12:49:48 +000048// Optimization sampler constants.
49static const int kSamplerFrameCount = 2;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000050
51// Constants for statistical profiler.
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000053
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000054static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000055
56static const int kSamplerThresholdInit = 3;
57static const int kSamplerThresholdMin = 1;
58static const int kSamplerThresholdDelta = 1;
59
60static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000061
62static const int kSizeLimit = 1500;
63
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000064// Constants for counter based profiler.
65
66// Number of times a function has to be seen on the stack before it is
67// optimized.
68static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000069// If the function optimization was disabled due to high deoptimization count,
70// but the function is hot and has been seen on the stack this number of times,
71// then we try to reenable optimization for this function.
72static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000073// If a function does not have enough type info (according to
74// FLAG_type_info_threshold), but has seen a huge number of ticks,
75// optimize it as it is.
76static const int kTicksWhenNotEnoughTypeInfo = 100;
77// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000078STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
79STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000080STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000081
mmassi@chromium.org7028c052012-06-13 11:51:58 +000082
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000083// Maximum size in bytes of generated code for a function to be optimized
84// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000085static const int kMaxSizeEarlyOpt =
86 5 * FullCodeGenerator::kBackEdgeDistanceUnit;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000087
kasperl@chromium.orga5551262010-12-07 12:49:48 +000088
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
90 : isolate_(isolate),
91 sampler_threshold_(kSamplerThresholdInit),
92 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
93 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +000094 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000095 sampler_window_position_(0),
96 any_ic_changed_(false),
97 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000098 ClearSampleBuffer();
99}
100
101
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000102static void GetICCounts(JSFunction* function,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000103 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000104 int* ic_total_count,
105 int* percentage) {
106 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000107 *ic_with_type_info_count = 0;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000108 Object* raw_info =
109 function->shared()->code()->type_feedback_info();
110 if (raw_info->IsTypeFeedbackInfo()) {
111 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000112 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000113 *ic_total_count = info->ic_total_count();
114 }
115 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000116 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000117 : 100;
118}
119
120
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000121void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000122 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000123
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000124 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000125 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000126 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000127 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000128 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000129 if (FLAG_type_info_threshold > 0) {
130 int typeinfo, total, percentage;
131 GetICCounts(function, &typeinfo, &total, &percentage);
132 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
133 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000134 PrintF("]\n");
135 }
136
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000137 if (FLAG_parallel_recompilation) {
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000138 ASSERT(!function->IsMarkedForInstallingRecompiledCode());
139 ASSERT(!function->IsInRecompileQueue());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000140 function->MarkForParallelRecompilation();
141 } else {
142 // The next call to the function will trigger optimization.
143 function->MarkForLazyRecompilation();
144 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000145}
146
147
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000149 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
150 // Debug::has_break_points().
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000151 ASSERT(function->IsMarkedForLazyRecompilation() ||
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000152 function->IsMarkedForParallelRecompilation() ||
153 function->IsOptimized());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000154 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000155 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000157 return;
158 }
159
160 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000161 // If the code is not optimizable, don't try OSR.
162 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000163
164 // We are not prepared to do OSR for a function that already has an
165 // allocated arguments object. The optimized code would bypass it for
166 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000167 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000168
169 // We're using on-stack replacement: patch the unoptimized code so that
170 // any back edge in any unoptimized frame will trigger on-stack
171 // replacement for that frame.
172 if (FLAG_trace_osr) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000173 PrintF("[patching back edges in ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000174 function->PrintName();
175 PrintF(" for on-stack replacement]\n");
176 }
177
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000178 // Get the interrupt stub code object to match against. We aren't
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000179 // prepared to generate it, but we don't expect to have to.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000180 Code* interrupt_code = NULL;
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000181 InterruptStub interrupt_stub;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000182 bool found_code = interrupt_stub.FindCodeInCache(&interrupt_code, isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000183 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000185 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000186 Code* unoptimized_code = shared->code();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000187 Deoptimizer::PatchInterruptCode(
188 unoptimized_code, interrupt_code, replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000189 }
190}
191
192
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000193void RuntimeProfiler::ClearSampleBuffer() {
194 memset(sampler_window_, 0, sizeof(sampler_window_));
195 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000196}
197
198
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000200 int weight = 0;
201 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000202 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000203 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000204 bool fits = FLAG_lookup_sample_by_shared
205 ? (function->shared() == JSFunction::cast(sample)->shared())
206 : (function == JSFunction::cast(sample));
207 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000208 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000209 }
210 }
211 }
212 return weight;
213}
214
215
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000217 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000218 sampler_window_[sampler_window_position_] = function;
219 sampler_window_weight_[sampler_window_position_] = weight;
220 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000221 (kSamplerWindowSize - 1);
222}
223
224
225void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000227
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000228 if (FLAG_parallel_recompilation) {
229 // Take this as opportunity to process the optimizing compiler thread's
230 // output queue so that it does not unnecessarily keep objects alive.
231 isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions();
232 }
233
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000234 // Run through the JavaScript frames and collect them. If we already
235 // have a sample of the function, we mark it for optimizations
236 // (eagerly or lazily).
237 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000238 int sample_count = 0;
239 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000240 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
241 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000242 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000243 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244 it.Advance()) {
245 JavaScriptFrame* frame = it.frame();
246 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000247
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000248 if (!FLAG_watch_ic_patching) {
249 // Adjust threshold each time we have processed
250 // a certain number of ticks.
251 if (sampler_ticks_until_threshold_adjustment_ > 0) {
252 sampler_ticks_until_threshold_adjustment_--;
253 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
254 // If the threshold is not already at the minimum
255 // modify and reset the ticks until next adjustment.
256 if (sampler_threshold_ > kSamplerThresholdMin) {
257 sampler_threshold_ -= kSamplerThresholdDelta;
258 sampler_ticks_until_threshold_adjustment_ =
259 kSamplerTicksBetweenThresholdAdjustment;
260 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000261 }
262 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000263 }
264
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000265 SharedFunctionInfo* shared = function->shared();
266 Code* shared_code = shared->code();
267
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000268 if (shared_code->kind() != Code::FUNCTION) continue;
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000269 if (function->IsInRecompileQueue()) continue;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000270
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000271 // Attempt OSR if we are still running unoptimized code even though the
272 // the function has long been marked or even already been optimized.
273 if (!frame->is_optimized() &&
274 (function->IsMarkedForLazyRecompilation() ||
275 function->IsMarkedForParallelRecompilation() ||
276 function->IsOptimized())) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000277 int nesting = shared_code->allow_osr_at_loop_nesting_level();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000278 if (nesting < Code::kMaxLoopNestingMarker) {
279 int new_nesting = nesting + 1;
280 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
281 AttemptOnStackReplacement(function);
282 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000283 }
284
yangguo@chromium.org56454712012-02-16 15:33:53 +0000285 // Only record top-level code on top of the execution stack and
286 // avoid optimizing excessively large scripts since top-level code
287 // will be executed only once.
288 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000289 if (shared->is_toplevel() &&
290 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000291 continue;
292 }
293
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000294 // Do not record non-optimizable functions.
295 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000296 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000297 // If optimization was disabled due to many deoptimizations,
298 // then check if the function is hot and try to reenable optimization.
299 int ticks = shared_code->profiler_ticks();
300 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
301 shared_code->set_profiler_ticks(0);
302 shared->TryReenableOptimization();
303 } else {
304 shared_code->set_profiler_ticks(ticks + 1);
305 }
306 }
307 continue;
308 }
309 if (!function->IsOptimizable()) continue;
310
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000311 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000312 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000313
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000314 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000315 int typeinfo, total, percentage;
316 GetICCounts(function, &typeinfo, &total, &percentage);
317 if (percentage >= FLAG_type_info_threshold) {
318 // If this particular function hasn't had any ICs patched for enough
319 // ticks, optimize it now.
320 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000321 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000322 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000323 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000324 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000325 if (FLAG_trace_opt_verbose) {
326 PrintF("[not yet optimizing ");
327 function->PrintName();
328 PrintF(", not enough type info: %d/%d (%d%%)]\n",
329 typeinfo, total, percentage);
330 }
331 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000332 } else if (!any_ic_changed_ &&
danno@chromium.org129d3982012-07-25 15:01:47 +0000333 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000334 // If no IC was patched since the last tick and this function is very
335 // small, optimistically optimize it now.
336 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000337 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000338 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000339 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000340 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000341 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000342
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000343 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000344 int threshold_size_factor = (function_size > kSizeLimit)
345 ? sampler_threshold_size_factor_
346 : 1;
347
348 int threshold = sampler_threshold_ * threshold_size_factor;
349
350 if (LookupSample(function) >= threshold) {
351 Optimize(function, "sampler window lookup");
352 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000353 }
354 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000355 if (FLAG_watch_ic_patching) {
356 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000357 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000358 // Add the collected functions as samples. It's important not to do
359 // this as part of collecting them because this will interfere with
360 // the sample lookup in case of recursive functions.
361 for (int i = 0; i < sample_count; i++) {
362 AddSample(samples[i], kSamplerFrameWeight[i]);
363 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000364 }
365}
366
367
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000368void RuntimeProfiler::SetUp() {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000369 if (!FLAG_watch_ic_patching) {
370 ClearSampleBuffer();
371 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372}
373
374
375void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000376 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000377 sampler_threshold_ = kSamplerThresholdInit;
378 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
379 sampler_ticks_until_threshold_adjustment_ =
380 kSamplerTicksBetweenThresholdAdjustment;
381 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382}
383
384
385void RuntimeProfiler::TearDown() {
386 // Nothing to do.
387}
388
389
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000390int RuntimeProfiler::SamplerWindowSize() {
391 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000392}
393
394
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000395// Update the pointers in the sampler window after a GC.
396void RuntimeProfiler::UpdateSamplesAfterScavenge() {
397 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000398 Object* function = sampler_window_[i];
399 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000400 MapWord map_word = HeapObject::cast(function)->map_word();
401 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000403 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000405 }
406 }
407 }
408}
409
410
411void RuntimeProfiler::RemoveDeadSamples() {
412 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000414 if (function != NULL &&
415 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000417 }
418 }
419}
420
421
422void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
423 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000425 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000426}
427
428
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000429} } // namespace v8::internal