blob: 8bd59d1de26de83a3c4cabadbdc7d181de50bac1 [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;
68
69// Maximum size in bytes of generated code for a function to be optimized
70// the very first time it is seen on the stack.
71static const int kMaxSizeEarlyOpt = 500;
72
kasperl@chromium.orga5551262010-12-07 12:49:48 +000073
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074Atomic32 RuntimeProfiler::state_ = 0;
75// TODO(isolates): Create the semaphore lazily and clean it up when no
76// longer required.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000077Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000079#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000080bool RuntimeProfiler::has_been_globally_set_up_ = false;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000081#endif
82bool RuntimeProfiler::enabled_ = false;
83
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000084
85RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
86 : isolate_(isolate),
87 sampler_threshold_(kSamplerThresholdInit),
88 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
89 sampler_ticks_until_threshold_adjustment_(
ricow@chromium.org4f693d62011-07-04 14:01:31 +000090 kSamplerTicksBetweenThresholdAdjustment),
91 sampler_window_position_(0) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 ClearSampleBuffer();
93}
94
95
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000096void RuntimeProfiler::GlobalSetup() {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000097 ASSERT(!has_been_globally_set_up_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000098 enabled_ = V8::UseCrankshaft() && FLAG_opt;
99#ifdef DEBUG
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000100 has_been_globally_set_up_ = true;
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000101#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102}
103
104
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000105static void GetICCounts(JSFunction* function,
106 int* ic_with_typeinfo_count,
107 int* ic_total_count,
108 int* percentage) {
109 *ic_total_count = 0;
110 *ic_with_typeinfo_count = 0;
111 Object* raw_info =
112 function->shared()->code()->type_feedback_info();
113 if (raw_info->IsTypeFeedbackInfo()) {
114 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
115 *ic_with_typeinfo_count = info->ic_with_typeinfo_count();
116 *ic_total_count = info->ic_total_count();
117 }
118 *percentage = *ic_total_count > 0
119 ? 100 * *ic_with_typeinfo_count / *ic_total_count
120 : 100;
121}
122
123
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000124void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000125 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000126 if (FLAG_trace_opt) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000127 PrintF("[marking ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000128 function->PrintName();
danno@chromium.org160a7b02011-04-18 15:51:38 +0000129 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000130 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000131 if (FLAG_type_info_threshold > 0) {
132 int typeinfo, total, percentage;
133 GetICCounts(function, &typeinfo, &total, &percentage);
134 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
135 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000136 PrintF("]\n");
137 }
138
139 // The next call to the function will trigger optimization.
140 function->MarkForLazyRecompilation();
141}
142
143
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000144void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000145 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
146 // Debug::has_break_points().
147 ASSERT(function->IsMarkedForLazyRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000149 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000150 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000151 return;
152 }
153
154 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000155 // If the code is not optimizable, don't try OSR.
156 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000157
158 // We are not prepared to do OSR for a function that already has an
159 // allocated arguments object. The optimized code would bypass it for
160 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000161 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000162
163 // We're using on-stack replacement: patch the unoptimized code so that
164 // any back edge in any unoptimized frame will trigger on-stack
165 // replacement for that frame.
166 if (FLAG_trace_osr) {
167 PrintF("[patching stack checks in ");
168 function->PrintName();
169 PrintF(" for on-stack replacement]\n");
170 }
171
172 // Get the stack check stub code object to match against. We aren't
173 // prepared to generate it, but we don't expect to have to.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000174 bool found_code = false;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000175 Code* stack_check_code = NULL;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000176#ifdef V8_TARGET_ARCH_IA32
177 if (FLAG_count_based_interrupts) {
178 InterruptStub interrupt_stub;
179 found_code = interrupt_stub.FindCodeInCache(&stack_check_code);
180 } else // NOLINT
181#endif
182 { // NOLINT
183 StackCheckStub check_stub;
184 found_code = check_stub.FindCodeInCache(&stack_check_code);
185 }
186 if (found_code) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000187 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000188 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000189 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000190 Deoptimizer::PatchStackCheckCode(unoptimized_code,
danno@chromium.orgc612e022011-11-10 11:38:15 +0000191 stack_check_code,
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000192 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000193 }
194}
195
196
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000197void RuntimeProfiler::ClearSampleBuffer() {
198 memset(sampler_window_, 0, sizeof(sampler_window_));
199 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000200}
201
202
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000203int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000204 int weight = 0;
205 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000207 if (sample != NULL) {
208 if (function == sample) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000209 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000210 }
211 }
212 }
213 return weight;
214}
215
216
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000217void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000218 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000219 sampler_window_[sampler_window_position_] = function;
220 sampler_window_weight_[sampler_window_position_] = weight;
221 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000222 (kSamplerWindowSize - 1);
223}
224
225
226void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000228
229 // Run through the JavaScript frames and collect them. If we already
230 // have a sample of the function, we mark it for optimizations
231 // (eagerly or lazily).
232 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000233 int sample_count = 0;
234 int frame_count = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000235 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
236 : kSamplerFrameCount;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000237 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000238 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000239 it.Advance()) {
240 JavaScriptFrame* frame = it.frame();
241 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000242
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000243 if (!FLAG_watch_ic_patching) {
244 // Adjust threshold each time we have processed
245 // a certain number of ticks.
246 if (sampler_ticks_until_threshold_adjustment_ > 0) {
247 sampler_ticks_until_threshold_adjustment_--;
248 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
249 // If the threshold is not already at the minimum
250 // modify and reset the ticks until next adjustment.
251 if (sampler_threshold_ > kSamplerThresholdMin) {
252 sampler_threshold_ -= kSamplerThresholdDelta;
253 sampler_ticks_until_threshold_adjustment_ =
254 kSamplerTicksBetweenThresholdAdjustment;
255 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000256 }
257 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000258 }
259
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000260 if (function->IsMarkedForLazyRecompilation()) {
261 Code* unoptimized = function->shared()->code();
262 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
263 if (nesting == 0) AttemptOnStackReplacement(function);
264 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
265 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000266 }
267
268 // Do not record non-optimizable functions.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000269 if (!function->IsOptimizable()) continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000270
yangguo@chromium.org56454712012-02-16 15:33:53 +0000271 // Only record top-level code on top of the execution stack and
272 // avoid optimizing excessively large scripts since top-level code
273 // will be executed only once.
274 const int kMaxToplevelSourceSize = 10 * 1024;
275 if (function->shared()->is_toplevel()
276 && (frame_count > 1
277 || function->shared()->SourceSize() > kMaxToplevelSourceSize)) {
278 continue;
279 }
280
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000281 if (FLAG_watch_ic_patching) {
282 int ticks = function->shared()->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000283
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000284 if (ticks >= kProfilerTicksBeforeOptimization) {
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000285 int typeinfo, total, percentage;
286 GetICCounts(function, &typeinfo, &total, &percentage);
287 if (percentage >= FLAG_type_info_threshold) {
288 // If this particular function hasn't had any ICs patched for enough
289 // ticks, optimize it now.
290 Optimize(function, "hot and stable");
291 } else {
292 if (FLAG_trace_opt_verbose) {
293 PrintF("[not yet optimizing ");
294 function->PrintName();
295 PrintF(", not enough type info: %d/%d (%d%%)]\n",
296 typeinfo, total, percentage);
297 }
298 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000299 } else if (!any_ic_changed_ &&
300 function->shared()->code()->instruction_size() < kMaxSizeEarlyOpt) {
301 // If no IC was patched since the last tick and this function is very
302 // small, optimistically optimize it now.
303 Optimize(function, "small function");
304 } else if (!code_generated_ &&
305 !any_ic_changed_ &&
306 total_code_generated_ > 0 &&
307 total_code_generated_ < 2000) {
308 // If no code was generated and no IC was patched since the last tick,
309 // but a little code has already been generated since last Reset(),
310 // then type info might already be stable and we can optimize now.
311 Optimize(function, "stable on startup");
312 } else {
313 function->shared()->set_profiler_ticks(ticks + 1);
314 }
yangguo@chromium.org56454712012-02-16 15:33:53 +0000315 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000316 samples[sample_count++] = function;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000317
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000318 int function_size = function->shared()->SourceSize();
319 int threshold_size_factor = (function_size > kSizeLimit)
320 ? sampler_threshold_size_factor_
321 : 1;
322
323 int threshold = sampler_threshold_ * threshold_size_factor;
324
325 if (LookupSample(function) >= threshold) {
326 Optimize(function, "sampler window lookup");
327 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000328 }
329 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000330 if (FLAG_watch_ic_patching) {
331 any_ic_changed_ = false;
332 code_generated_ = false;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000333 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000334 // Add the collected functions as samples. It's important not to do
335 // this as part of collecting them because this will interfere with
336 // the sample lookup in case of recursive functions.
337 for (int i = 0; i < sample_count; i++) {
338 AddSample(samples[i], kSamplerFrameWeight[i]);
339 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000340 }
341}
342
343
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000344void RuntimeProfiler::NotifyTick() {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000345#ifdef V8_TARGET_ARCH_IA32
346 if (FLAG_count_based_interrupts) return;
347#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000348 isolate_->stack_guard()->RequestRuntimeProfilerTick();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349}
350
351
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000352void RuntimeProfiler::SetUp() {
353 ASSERT(has_been_globally_set_up_);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000354 if (!FLAG_watch_ic_patching) {
355 ClearSampleBuffer();
356 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000357 // If the ticker hasn't already started, make sure to do so to get
358 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000359 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000360}
361
362
363void RuntimeProfiler::Reset() {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000364 if (FLAG_watch_ic_patching) {
365 total_code_generated_ = 0;
yangguo@chromium.org56454712012-02-16 15:33:53 +0000366 } else { // !FLAG_watch_ic_patching
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000367 sampler_threshold_ = kSamplerThresholdInit;
368 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
369 sampler_ticks_until_threshold_adjustment_ =
370 kSamplerTicksBetweenThresholdAdjustment;
371 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372}
373
374
375void RuntimeProfiler::TearDown() {
376 // Nothing to do.
377}
378
379
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000380int RuntimeProfiler::SamplerWindowSize() {
381 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382}
383
384
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000385// Update the pointers in the sampler window after a GC.
386void RuntimeProfiler::UpdateSamplesAfterScavenge() {
387 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 Object* function = sampler_window_[i];
389 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000390 MapWord map_word = HeapObject::cast(function)->map_word();
391 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000393 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000395 }
396 }
397 }
398}
399
400
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000401void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 // The profiler thread must still be waiting.
403 ASSERT(NoBarrier_Load(&state_) >= 0);
404 // In IsolateEnteredJS we have already incremented the counter and
405 // undid the decrement done by the profiler thread. Increment again
406 // to get the right count of active isolates.
407 NoBarrier_AtomicIncrement(&state_, 1);
408 semaphore_->Signal();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000409}
410
411
412bool RuntimeProfiler::IsSomeIsolateInJS() {
413 return NoBarrier_Load(&state_) > 0;
414}
415
416
417bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000418 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
419 ASSERT(old_state >= -1);
420 if (old_state != 0) return false;
421 semaphore_->Wait();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422 return true;
423}
424
425
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000426void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +0000427 // Do a fake increment. If the profiler is waiting on the semaphore,
428 // the returned state is 0, which can be left as an initial state in
429 // case profiling is restarted later. If the profiler is not
430 // waiting, the increment will prevent it from waiting, but has to
431 // be undone after the profiler is stopped.
432 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
433 ASSERT(new_state >= 0);
434 if (new_state == 0) {
435 // The profiler thread is waiting. Wake it up. It must check for
436 // stop conditions before attempting to wait again.
437 semaphore_->Signal();
438 }
439 thread->Join();
440 // The profiler thread is now stopped. Undo the increment in case it
441 // was not waiting.
442 if (new_state != 0) {
443 NoBarrier_AtomicIncrement(&state_, -1);
444 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000445}
446
447
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000448void RuntimeProfiler::RemoveDeadSamples() {
449 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000450 Object* function = sampler_window_[i];
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000451 if (function != NULL &&
452 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000454 }
455 }
456}
457
458
459void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
460 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000461 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000462 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000463}
464
465
466bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000467 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
468 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000469 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000470 return false;
471}
472
473
474} } // namespace v8::internal