blob: 6ed4ff483aec20951510385c25288f7b272a7778 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010038#include "isolate-inl.h"
Ben Murdoche0cee9b2011-05-25 10:26:03 +010039#include "mark-compact.h"
Steve Block44f0eee2011-05-26 01:26:41 +010040#include "platform.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010041#include "scopeinfo.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010042
43namespace v8 {
44namespace internal {
45
46
Ben Murdochb0fe1622011-05-05 13:52:32 +010047// Optimization sampler constants.
48static const int kSamplerFrameCount = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010049
50// Constants for statistical profiler.
Ben Murdochb0fe1622011-05-05 13:52:32 +010051static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
Ben Murdochb0fe1622011-05-05 13:52:32 +010052
53static const int kSamplerTicksBetweenThresholdAdjustment = 32;
54
55static const int kSamplerThresholdInit = 3;
56static const int kSamplerThresholdMin = 1;
57static const int kSamplerThresholdDelta = 1;
58
59static const int kSamplerThresholdSizeFactorInit = 3;
Ben Murdochb0fe1622011-05-05 13:52:32 +010060
61static const int kSizeLimit = 1500;
62
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063// 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
Ben Murdochb0fe1622011-05-05 13:52:32 +010073
Steve Block44f0eee2011-05-26 01:26:41 +010074Atomic32 RuntimeProfiler::state_ = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075
76// TODO(isolates): Clean up the semaphore when it is no longer required.
77static LazySemaphore<0>::type semaphore = LAZY_SEMAPHORE_INITIALIZER;
Steve Block44f0eee2011-05-26 01:26:41 +010078
Ben Murdoch8b112d22011-06-08 16:22:53 +010079#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +010080bool RuntimeProfiler::has_been_globally_set_up_ = false;
Ben Murdoch8b112d22011-06-08 16:22:53 +010081#endif
82bool RuntimeProfiler::enabled_ = false;
83
Steve Block44f0eee2011-05-26 01:26:41 +010084
85RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
86 : isolate_(isolate),
87 sampler_threshold_(kSamplerThresholdInit),
88 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
89 sampler_ticks_until_threshold_adjustment_(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090 kSamplerTicksBetweenThresholdAdjustment),
91 sampler_window_position_(0) {
Steve Block44f0eee2011-05-26 01:26:41 +010092 ClearSampleBuffer();
93}
94
95
Ben Murdoch8b112d22011-06-08 16:22:53 +010096void RuntimeProfiler::GlobalSetup() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010097 ASSERT(!has_been_globally_set_up_);
Ben Murdoch8b112d22011-06-08 16:22:53 +010098 enabled_ = V8::UseCrankshaft() && FLAG_opt;
99#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100100 has_been_globally_set_up_ = true;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100101#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100102}
103
104
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100105static 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
124void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100125 ASSERT(function->IsOptimizable());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100126 if (FLAG_trace_opt) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000127 PrintF("[marking ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100128 function->PrintName();
Ben Murdoch257744e2011-11-30 15:57:28 +0000129 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100130 PrintF(" for recompilation, reason: %s", reason);
131 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 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100136 PrintF("]\n");
137 }
138
139 // The next call to the function will trigger optimization.
140 function->MarkForLazyRecompilation();
141}
142
143
Steve Block44f0eee2011-05-26 01:26:41 +0100144void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100145 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
146 // Debug::has_break_points().
147 ASSERT(function->IsMarkedForLazyRecompilation());
Steve Block44f0eee2011-05-26 01:26:41 +0100148 if (!FLAG_use_osr ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000149 isolate_->DebuggerHasBreakPoints() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100150 function->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151 return;
152 }
153
154 SharedFunctionInfo* shared = function->shared();
Ben Murdoch589d6972011-11-30 16:04:58 +0000155 // If the code is not optimizable, don't try OSR.
156 if (!shared->code()->optimizable()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100157
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.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000161 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100162
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.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100174 bool found_code = false;
175 Code* stack_check_code = NULL;
176#if defined(V8_TARGET_ARCH_IA32) || \
177 defined(V8_TARGET_ARCH_ARM) || \
178 defined(V8_TARGET_ARCH_MIPS)
179 if (FLAG_count_based_interrupts) {
180 InterruptStub interrupt_stub;
181 found_code = interrupt_stub.FindCodeInCache(&stack_check_code);
182 } else // NOLINT
183#endif
184 { // NOLINT
185 StackCheckStub check_stub;
186 found_code = check_stub.FindCodeInCache(&stack_check_code);
187 }
188 if (found_code) {
Steve Block44f0eee2011-05-26 01:26:41 +0100189 Code* replacement_code =
190 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100191 Code* unoptimized_code = shared->code();
Steve Block1e0659c2011-05-24 12:43:12 +0100192 Deoptimizer::PatchStackCheckCode(unoptimized_code,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100193 stack_check_code,
Steve Block1e0659c2011-05-24 12:43:12 +0100194 replacement_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100195 }
196}
197
198
Steve Block44f0eee2011-05-26 01:26:41 +0100199void RuntimeProfiler::ClearSampleBuffer() {
200 memset(sampler_window_, 0, sizeof(sampler_window_));
201 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100202}
203
204
Steve Block44f0eee2011-05-26 01:26:41 +0100205int RuntimeProfiler::LookupSample(JSFunction* function) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100206 int weight = 0;
207 for (int i = 0; i < kSamplerWindowSize; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100208 Object* sample = sampler_window_[i];
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 if (sample != NULL) {
210 if (function == sample) {
Steve Block44f0eee2011-05-26 01:26:41 +0100211 weight += sampler_window_weight_[i];
Ben Murdochb0fe1622011-05-05 13:52:32 +0100212 }
213 }
214 }
215 return weight;
216}
217
218
Steve Block44f0eee2011-05-26 01:26:41 +0100219void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100220 ASSERT(IsPowerOf2(kSamplerWindowSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100221 sampler_window_[sampler_window_position_] = function;
222 sampler_window_weight_[sampler_window_position_] = weight;
223 sampler_window_position_ = (sampler_window_position_ + 1) &
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 (kSamplerWindowSize - 1);
225}
226
227
228void RuntimeProfiler::OptimizeNow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100229 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100230
231 // Run through the JavaScript frames and collect them. If we already
232 // have a sample of the function, we mark it for optimizations
233 // (eagerly or lazily).
234 JSFunction* samples[kSamplerFrameCount];
235 int sample_count = 0;
236 int frame_count = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100237 int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count
238 : kSamplerFrameCount;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100241 it.Advance()) {
242 JavaScriptFrame* frame = it.frame();
243 JSFunction* function = JSFunction::cast(frame->function());
244
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100245 if (!FLAG_watch_ic_patching) {
246 // Adjust threshold each time we have processed
247 // a certain number of ticks.
248 if (sampler_ticks_until_threshold_adjustment_ > 0) {
249 sampler_ticks_until_threshold_adjustment_--;
250 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
251 // If the threshold is not already at the minimum
252 // modify and reset the ticks until next adjustment.
253 if (sampler_threshold_ > kSamplerThresholdMin) {
254 sampler_threshold_ -= kSamplerThresholdDelta;
255 sampler_ticks_until_threshold_adjustment_ =
256 kSamplerTicksBetweenThresholdAdjustment;
257 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100258 }
259 }
260 }
261
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100262 if (function->IsMarkedForLazyRecompilation() &&
263 function->shared()->code()->kind() == Code::FUNCTION) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 Code* unoptimized = function->shared()->code();
265 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
266 if (nesting == 0) AttemptOnStackReplacement(function);
267 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
268 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
269 }
270
271 // Do not record non-optimizable functions.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100272 if (!function->IsOptimizable()) continue;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100273 if (function->shared()->optimization_disabled()) continue;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100274
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100275 // Only record top-level code on top of the execution stack and
276 // avoid optimizing excessively large scripts since top-level code
277 // will be executed only once.
278 const int kMaxToplevelSourceSize = 10 * 1024;
279 if (function->shared()->is_toplevel()
280 && (frame_count > 1
281 || function->shared()->SourceSize() > kMaxToplevelSourceSize)) {
282 continue;
283 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100284
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100285 if (FLAG_watch_ic_patching) {
286 int ticks = function->shared()->profiler_ticks();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100287
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100288 if (ticks >= kProfilerTicksBeforeOptimization) {
289 int typeinfo, total, percentage;
290 GetICCounts(function, &typeinfo, &total, &percentage);
291 if (percentage >= FLAG_type_info_threshold) {
292 // If this particular function hasn't had any ICs patched for enough
293 // ticks, optimize it now.
294 Optimize(function, "hot and stable");
295 } else if (ticks >= 100) {
296 // If this function does not have enough type info, but has
297 // seen a huge number of ticks, optimize it as it is.
298 Optimize(function, "not much type info but very hot");
299 } else {
300 function->shared()->set_profiler_ticks(ticks + 1);
301 if (FLAG_trace_opt_verbose) {
302 PrintF("[not yet optimizing ");
303 function->PrintName();
304 PrintF(", not enough type info: %d/%d (%d%%)]\n",
305 typeinfo, total, percentage);
306 }
307 }
308 } else if (!any_ic_changed_ &&
309 function->shared()->code()->instruction_size() < kMaxSizeEarlyOpt) {
310 // If no IC was patched since the last tick and this function is very
311 // small, optimistically optimize it now.
312 Optimize(function, "small function");
313 } else if (!code_generated_ &&
314 !any_ic_changed_ &&
315 total_code_generated_ > 0 &&
316 total_code_generated_ < 2000) {
317 // If no code was generated and no IC was patched since the last tick,
318 // but a little code has already been generated since last Reset(),
319 // then type info might already be stable and we can optimize now.
320 Optimize(function, "stable on startup");
321 } else {
322 function->shared()->set_profiler_ticks(ticks + 1);
323 }
324 } else { // !FLAG_watch_ic_patching
325 samples[sample_count++] = function;
326
327 int function_size = function->shared()->SourceSize();
328 int threshold_size_factor = (function_size > kSizeLimit)
329 ? sampler_threshold_size_factor_
330 : 1;
331
332 int threshold = sampler_threshold_ * threshold_size_factor;
333
334 if (LookupSample(function) >= threshold) {
335 Optimize(function, "sampler window lookup");
336 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100337 }
338 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100339 if (FLAG_watch_ic_patching) {
340 any_ic_changed_ = false;
341 code_generated_ = false;
342 } else { // !FLAG_watch_ic_patching
343 // Add the collected functions as samples. It's important not to do
344 // this as part of collecting them because this will interfere with
345 // the sample lookup in case of recursive functions.
346 for (int i = 0; i < sample_count; i++) {
347 AddSample(samples[i], kSamplerFrameWeight[i]);
348 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100349 }
350}
351
352
Ben Murdochb0fe1622011-05-05 13:52:32 +0100353void RuntimeProfiler::NotifyTick() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100354#if defined(V8_TARGET_ARCH_IA32) || \
355 defined(V8_TARGET_ARCH_ARM) || \
356 defined(V8_TARGET_ARCH_MIPS)
357 if (FLAG_count_based_interrupts) return;
358#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100359 isolate_->stack_guard()->RequestRuntimeProfilerTick();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100360}
361
362
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100363void RuntimeProfiler::SetUp() {
364 ASSERT(has_been_globally_set_up_);
365 if (!FLAG_watch_ic_patching) {
366 ClearSampleBuffer();
367 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100368 // If the ticker hasn't already started, make sure to do so to get
369 // the ticks for the runtime profiler.
Steve Block44f0eee2011-05-26 01:26:41 +0100370 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100371}
372
373
374void RuntimeProfiler::Reset() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100375 if (FLAG_watch_ic_patching) {
376 total_code_generated_ = 0;
377 } else { // !FLAG_watch_ic_patching
378 sampler_threshold_ = kSamplerThresholdInit;
379 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
380 sampler_ticks_until_threshold_adjustment_ =
381 kSamplerTicksBetweenThresholdAdjustment;
382 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100383}
384
385
386void RuntimeProfiler::TearDown() {
387 // Nothing to do.
388}
389
390
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100391int RuntimeProfiler::SamplerWindowSize() {
392 return kSamplerWindowSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100393}
394
395
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100396// Update the pointers in the sampler window after a GC.
397void RuntimeProfiler::UpdateSamplesAfterScavenge() {
398 for (int i = 0; i < kSamplerWindowSize; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100399 Object* function = sampler_window_[i];
400 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100401 MapWord map_word = HeapObject::cast(function)->map_word();
402 if (map_word.IsForwardingAddress()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100403 sampler_window_[i] = map_word.ToForwardingAddress();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100404 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100405 sampler_window_[i] = NULL;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100406 }
407 }
408 }
409}
410
411
Steve Block44f0eee2011-05-26 01:26:41 +0100412void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +0100413 // The profiler thread must still be waiting.
414 ASSERT(NoBarrier_Load(&state_) >= 0);
415 // In IsolateEnteredJS we have already incremented the counter and
416 // undid the decrement done by the profiler thread. Increment again
417 // to get the right count of active isolates.
418 NoBarrier_AtomicIncrement(&state_, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100419 semaphore.Pointer()->Signal();
Steve Block44f0eee2011-05-26 01:26:41 +0100420}
421
422
423bool RuntimeProfiler::IsSomeIsolateInJS() {
424 return NoBarrier_Load(&state_) > 0;
425}
426
427
428bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
Steve Block44f0eee2011-05-26 01:26:41 +0100429 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
430 ASSERT(old_state >= -1);
431 if (old_state != 0) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100432 semaphore.Pointer()->Wait();
Steve Block44f0eee2011-05-26 01:26:41 +0100433 return true;
434}
435
436
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000437void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
438 // Do a fake increment. If the profiler is waiting on the semaphore,
439 // the returned state is 0, which can be left as an initial state in
440 // case profiling is restarted later. If the profiler is not
441 // waiting, the increment will prevent it from waiting, but has to
442 // be undone after the profiler is stopped.
443 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
444 ASSERT(new_state >= 0);
445 if (new_state == 0) {
446 // The profiler thread is waiting. Wake it up. It must check for
447 // stop conditions before attempting to wait again.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 semaphore.Pointer()->Signal();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000449 }
450 thread->Join();
451 // The profiler thread is now stopped. Undo the increment in case it
452 // was not waiting.
453 if (new_state != 0) {
454 NoBarrier_AtomicIncrement(&state_, -1);
455 }
Steve Block44f0eee2011-05-26 01:26:41 +0100456}
457
458
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100459void RuntimeProfiler::RemoveDeadSamples() {
460 for (int i = 0; i < kSamplerWindowSize; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100461 Object* function = sampler_window_[i];
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100462 if (function != NULL &&
463 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100464 sampler_window_[i] = NULL;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100465 }
466 }
467}
468
469
470void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
471 for (int i = 0; i < kSamplerWindowSize; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100472 visitor->VisitPointer(&sampler_window_[i]);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100473 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100474}
475
476
477bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000478 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
479 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100480 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100481 return false;
482}
483
484
485} } // namespace v8::internal