blob: cdbc77a54087ebbab74ed00ce7242b90186d2be2 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "runtime-profiler.h"
31
32#include "assembler.h"
33#include "code-stubs.h"
34#include "compilation-cache.h"
35#include "deoptimizer.h"
36#include "execution.h"
37#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000038#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000039#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000041#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042
43namespace v8 {
44namespace internal {
45
46
kasperl@chromium.orga5551262010-12-07 12:49:48 +000047// Optimization sampler constants.
48static const int kSamplerFrameCount = 2;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000049
50// Constants for statistical profiler.
kasperl@chromium.orga5551262010-12-07 12:49:48 +000051static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000052
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000053static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000054
55static const int kSamplerThresholdInit = 3;
56static const int kSamplerThresholdMin = 1;
57static const int kSamplerThresholdDelta = 1;
58
59static const int kSamplerThresholdSizeFactorInit = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000060
61static const int kSizeLimit = 1500;
62
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063// Constants for counter based profiler.
64
65// Number of times a function has to be seen on the stack before it is
66// optimized.
67static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000068// If the function optimization was disabled due to high deoptimization count,
69// but the function is hot and has been seen on the stack this number of times,
70// then we try to reenable optimization for this function.
71static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000072// If a function does not have enough type info (according to
73// FLAG_type_info_threshold), but has seen a huge number of ticks,
74// optimize it as it is.
75static const int kTicksWhenNotEnoughTypeInfo = 100;
76// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000077STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
78STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000079STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000080
mmassi@chromium.org7028c052012-06-13 11:51:58 +000081
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000082// Maximum size in bytes of generated code for a function to be optimized
83// the very first time it is seen on the stack.
84static const int kMaxSizeEarlyOpt = 500;
85
kasperl@chromium.orga5551262010-12-07 12:49:48 +000086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087Atomic32 RuntimeProfiler::state_ = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000088
89// TODO(isolates): Clean up the semaphore when it is no longer required.
90static LazySemaphore<0>::type semaphore = LAZY_SEMAPHORE_INITIALIZER;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000091
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000092#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000093bool RuntimeProfiler::has_been_globally_set_up_ = false;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000094#endif
95bool RuntimeProfiler::enabled_ = false;
96
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000097
98RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
99 : isolate_(isolate),
100 sampler_threshold_(kSamplerThresholdInit),
101 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
102 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000103 kSamplerTicksBetweenThresholdAdjustment),
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000104 sampler_window_position_(0),
105 any_ic_changed_(false),
106 code_generated_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000107 ClearSampleBuffer();
108}
109
110
fschneider@chromium.org7d10be52012-04-10 12:30:14 +0000111void RuntimeProfiler::GlobalSetUp() {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000112 ASSERT(!has_been_globally_set_up_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000113 enabled_ = V8::UseCrankshaft() && FLAG_opt;
114#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000115 has_been_globally_set_up_ = true;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000116#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117}
118
119
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000120static void GetICCounts(JSFunction* function,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000121 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000122 int* ic_total_count,
123 int* percentage) {
124 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000125 *ic_with_type_info_count = 0;
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000126 Object* raw_info =
127 function->shared()->code()->type_feedback_info();
128 if (raw_info->IsTypeFeedbackInfo()) {
129 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000130 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000131 *ic_total_count = info->ic_total_count();
132 }
133 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000134 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000135 : 100;
136}
137
138
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000139void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000140 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000141 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000142 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000143 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000144 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000145 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000146 if (FLAG_type_info_threshold > 0) {
147 int typeinfo, total, percentage;
148 GetICCounts(function, &typeinfo, &total, &percentage);
149 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
150 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000151 PrintF("]\n");
152 }
153
154 // The next call to the function will trigger optimization.
155 function->MarkForLazyRecompilation();
156}
157
158
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000159void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000160 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
161 // Debug::has_break_points().
162 ASSERT(function->IsMarkedForLazyRecompilation());
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) {
182 PrintF("[patching stack checks in ");
183 function->PrintName();
184 PrintF(" for on-stack replacement]\n");
185 }
186
187 // Get the stack check stub code object to match against. We aren't
188 // prepared to generate it, but we don't expect to have to.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000189 bool found_code = false;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000190 Code* stack_check_code = NULL;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000191 if (FLAG_count_based_interrupts) {
192 InterruptStub interrupt_stub;
193 found_code = interrupt_stub.FindCodeInCache(&stack_check_code);
194 } else // NOLINT
yangguo@chromium.org56454712012-02-16 15:33:53 +0000195 { // NOLINT
196 StackCheckStub check_stub;
197 found_code = check_stub.FindCodeInCache(&stack_check_code);
198 }
199 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000200 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000201 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000202 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000203 Deoptimizer::PatchStackCheckCode(unoptimized_code,
danno@chromium.orgc612e022011-11-10 11:38:15 +0000204 stack_check_code,
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000205 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000206 }
207}
208
209
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210void RuntimeProfiler::ClearSampleBuffer() {
211 memset(sampler_window_, 0, sizeof(sampler_window_));
212 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000213}
214
215
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000217 int weight = 0;
218 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000220 if (sample != NULL) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000221 bool fits = FLAG_lookup_sample_by_shared
222 ? (function->shared() == JSFunction::cast(sample)->shared())
223 : (function == JSFunction::cast(sample));
224 if (fits) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000225 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000226 }
227 }
228 }
229 return weight;
230}
231
232
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000233void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000234 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 sampler_window_[sampler_window_position_] = function;
236 sampler_window_weight_[sampler_window_position_] = weight;
237 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000238 (kSamplerWindowSize - 1);
239}
240
241
242void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000243 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244
245 // Run through the JavaScript frames and collect them. If we already
246 // have a sample of the function, we mark it for optimizations
247 // (eagerly or lazily).
248 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000249 int sample_count = 0;
250 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000251 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
252 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000253 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000254 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000255 it.Advance()) {
256 JavaScriptFrame* frame = it.frame();
257 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000258
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000259 if (!FLAG_watch_ic_patching) {
260 // Adjust threshold each time we have processed
261 // a certain number of ticks.
262 if (sampler_ticks_until_threshold_adjustment_ > 0) {
263 sampler_ticks_until_threshold_adjustment_--;
264 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
265 // If the threshold is not already at the minimum
266 // modify and reset the ticks until next adjustment.
267 if (sampler_threshold_ > kSamplerThresholdMin) {
268 sampler_threshold_ -= kSamplerThresholdDelta;
269 sampler_ticks_until_threshold_adjustment_ =
270 kSamplerTicksBetweenThresholdAdjustment;
271 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000272 }
273 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000274 }
275
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000276 SharedFunctionInfo* shared = function->shared();
277 Code* shared_code = shared->code();
278
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000279 if (shared_code->kind() != Code::FUNCTION) continue;
280
281 if (function->IsMarkedForLazyRecompilation()) {
282 int nesting = shared_code->allow_osr_at_loop_nesting_level();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000283 if (nesting == 0) AttemptOnStackReplacement(function);
284 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000285 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000286 }
287
yangguo@chromium.org56454712012-02-16 15:33:53 +0000288 // Only record top-level code on top of the execution stack and
289 // avoid optimizing excessively large scripts since top-level code
290 // will be executed only once.
291 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000292 if (shared->is_toplevel() &&
293 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000294 continue;
295 }
296
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000297 // Do not record non-optimizable functions.
298 if (shared->optimization_disabled()) {
299 if (shared->deopt_count() >= Compiler::kDefaultMaxOptCount) {
300 // If optimization was disabled due to many deoptimizations,
301 // then check if the function is hot and try to reenable optimization.
302 int ticks = shared_code->profiler_ticks();
303 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
304 shared_code->set_profiler_ticks(0);
305 shared->TryReenableOptimization();
306 } else {
307 shared_code->set_profiler_ticks(ticks + 1);
308 }
309 }
310 continue;
311 }
312 if (!function->IsOptimizable()) continue;
313
314
315
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000316 if (FLAG_watch_ic_patching) {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000317 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000318
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000319 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000320 int typeinfo, total, percentage;
321 GetICCounts(function, &typeinfo, &total, &percentage);
322 if (percentage >= FLAG_type_info_threshold) {
323 // If this particular function hasn't had any ICs patched for enough
324 // ticks, optimize it now.
325 Optimize(function, "hot and stable");
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000326 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
danno@chromium.org88aa0582012-03-23 15:11:57 +0000327 Optimize(function, "not much type info but very hot");
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000328 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000329 shared_code->set_profiler_ticks(ticks + 1);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000330 if (FLAG_trace_opt_verbose) {
331 PrintF("[not yet optimizing ");
332 function->PrintName();
333 PrintF(", not enough type info: %d/%d (%d%%)]\n",
334 typeinfo, total, percentage);
335 }
336 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000337 } else if (!any_ic_changed_ &&
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000338 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000339 // If no IC was patched since the last tick and this function is very
340 // small, optimistically optimize it now.
341 Optimize(function, "small function");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000342 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000343 shared_code->set_profiler_ticks(ticks + 1);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000344 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000345 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000346 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000347
danno@chromium.org1044a4d2012-04-30 12:34:39 +0000348 int function_size = function->shared()->SourceSize();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000349 int threshold_size_factor = (function_size > kSizeLimit)
350 ? sampler_threshold_size_factor_
351 : 1;
352
353 int threshold = sampler_threshold_ * threshold_size_factor;
354
355 if (LookupSample(function) >= threshold) {
356 Optimize(function, "sampler window lookup");
357 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000358 }
359 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000360 if (FLAG_watch_ic_patching) {
361 any_ic_changed_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000362 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000363 // Add the collected functions as samples. It's important not to do
364 // this as part of collecting them because this will interfere with
365 // the sample lookup in case of recursive functions.
366 for (int i = 0; i < sample_count; i++) {
367 AddSample(samples[i], kSamplerFrameWeight[i]);
368 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000369 }
370}
371
372
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373void RuntimeProfiler::NotifyTick() {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000374 if (FLAG_count_based_interrupts) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000375 isolate_->stack_guard()->RequestRuntimeProfilerTick();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376}
377
378
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000379void RuntimeProfiler::SetUp() {
380 ASSERT(has_been_globally_set_up_);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000381 if (!FLAG_watch_ic_patching) {
382 ClearSampleBuffer();
383 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000384 // If the ticker hasn't already started, make sure to do so to get
385 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000387}
388
389
390void RuntimeProfiler::Reset() {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000391 if (!FLAG_watch_ic_patching) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000392 sampler_threshold_ = kSamplerThresholdInit;
393 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
394 sampler_ticks_until_threshold_adjustment_ =
395 kSamplerTicksBetweenThresholdAdjustment;
396 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000397}
398
399
400void RuntimeProfiler::TearDown() {
401 // Nothing to do.
402}
403
404
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000405int RuntimeProfiler::SamplerWindowSize() {
406 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000407}
408
409
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000410// Update the pointers in the sampler window after a GC.
411void RuntimeProfiler::UpdateSamplesAfterScavenge() {
412 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000413 Object* function = sampler_window_[i];
414 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000415 MapWord map_word = HeapObject::cast(function)->map_word();
416 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000418 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000419 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000420 }
421 }
422 }
423}
424
425
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000426void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 // The profiler thread must still be waiting.
428 ASSERT(NoBarrier_Load(&state_) >= 0);
429 // In IsolateEnteredJS we have already incremented the counter and
430 // undid the decrement done by the profiler thread. Increment again
431 // to get the right count of active isolates.
432 NoBarrier_AtomicIncrement(&state_, 1);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000433 semaphore.Pointer()->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434}
435
436
437bool RuntimeProfiler::IsSomeIsolateInJS() {
438 return NoBarrier_Load(&state_) > 0;
439}
440
441
442bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000443 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
444 ASSERT(old_state >= -1);
445 if (old_state != 0) return false;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000446 semaphore.Pointer()->Wait();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 return true;
448}
449
450
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000451void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000452 // Do a fake increment. If the profiler is waiting on the semaphore,
453 // the returned state is 0, which can be left as an initial state in
454 // case profiling is restarted later. If the profiler is not
455 // waiting, the increment will prevent it from waiting, but has to
456 // be undone after the profiler is stopped.
457 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
458 ASSERT(new_state >= 0);
459 if (new_state == 0) {
460 // The profiler thread is waiting. Wake it up. It must check for
461 // stop conditions before attempting to wait again.
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000462 semaphore.Pointer()->Signal();
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000463 }
464 thread->Join();
465 // The profiler thread is now stopped. Undo the increment in case it
466 // was not waiting.
467 if (new_state != 0) {
468 NoBarrier_AtomicIncrement(&state_, -1);
469 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000470}
471
472
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000473void RuntimeProfiler::RemoveDeadSamples() {
474 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000475 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000476 if (function != NULL &&
477 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000478 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000479 }
480 }
481}
482
483
484void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
485 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000486 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000487 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000488}
489
490
491bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000492 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
493 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000494 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000495 return false;
496}
497
498
499} } // namespace v8::internal