blob: 97f034169e5677a1b38751a78328760714b4ecdd [file] [log] [blame]
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// 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"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000038#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000039#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000040#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000041
42namespace v8 {
43namespace internal {
44
45
46class PendingListNode : public Malloced {
47 public:
48 explicit PendingListNode(JSFunction* function);
49 ~PendingListNode() { Destroy(); }
50
51 PendingListNode* next() const { return next_; }
52 void set_next(PendingListNode* node) { next_ = node; }
53 Handle<JSFunction> function() { return Handle<JSFunction>::cast(function_); }
54
55 // If the function is garbage collected before we've had the chance
56 // to optimize it the weak handle will be null.
57 bool IsValid() { return !function_.is_null(); }
58
59 // Returns the number of microseconds this node has been pending.
60 int Delay() const { return static_cast<int>(OS::Ticks() - start_); }
61
62 private:
63 void Destroy();
64 static void WeakCallback(v8::Persistent<v8::Value> object, void* data);
65
66 PendingListNode* next_;
67 Handle<Object> function_; // Weak handle.
68 int64_t start_;
69};
70
71
72// Optimization sampler constants.
73static const int kSamplerFrameCount = 2;
74static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 };
kasperl@chromium.orga5551262010-12-07 12:49:48 +000075
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000076static const int kSamplerTicksBetweenThresholdAdjustment = 32;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000077
78static const int kSamplerThresholdInit = 3;
79static const int kSamplerThresholdMin = 1;
80static const int kSamplerThresholdDelta = 1;
81
82static const int kSamplerThresholdSizeFactorInit = 3;
83static const int kSamplerThresholdSizeFactorMin = 1;
84static const int kSamplerThresholdSizeFactorDelta = 1;
85
86static const int kSizeLimit = 1500;
87
kasperl@chromium.orga5551262010-12-07 12:49:48 +000088
89PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 GlobalHandles* global_handles = Isolate::Current()->global_handles();
91 function_ = global_handles->Create(function);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000092 start_ = OS::Ticks();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093 global_handles->MakeWeak(function_.location(), this, &WeakCallback);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000094}
95
96
97void PendingListNode::Destroy() {
98 if (!IsValid()) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000099 GlobalHandles* global_handles = Isolate::Current()->global_handles();
100 global_handles->Destroy(function_.location());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000101 function_= Handle<Object>::null();
102}
103
104
105void PendingListNode::WeakCallback(v8::Persistent<v8::Value>, void* data) {
106 reinterpret_cast<PendingListNode*>(data)->Destroy();
107}
108
109
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000110Atomic32 RuntimeProfiler::state_ = 0;
111// TODO(isolates): Create the semaphore lazily and clean it up when no
112// longer required.
113#ifdef ENABLE_LOGGING_AND_PROFILING
114Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0);
115#endif
116
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000117#ifdef DEBUG
118bool RuntimeProfiler::has_been_globally_setup_ = false;
119#endif
120bool RuntimeProfiler::enabled_ = false;
121
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000122
123RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
124 : isolate_(isolate),
125 sampler_threshold_(kSamplerThresholdInit),
126 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit),
127 sampler_ticks_until_threshold_adjustment_(
128 kSamplerTicksBetweenThresholdAdjustment),
129 js_ratio_(0),
130 sampler_window_position_(0),
131 optimize_soon_list_(NULL),
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000132 state_window_position_(0),
133 state_window_ticks_(0) {
134 state_counts_[IN_NON_JS_STATE] = kStateWindowSize;
135 state_counts_[IN_JS_STATE] = 0;
136 STATIC_ASSERT(IN_NON_JS_STATE == 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 memset(state_window_, 0, sizeof(state_window_));
138 ClearSampleBuffer();
139}
140
141
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000142void RuntimeProfiler::GlobalSetup() {
143 ASSERT(!has_been_globally_setup_);
144 enabled_ = V8::UseCrankshaft() && FLAG_opt;
145#ifdef DEBUG
146 has_been_globally_setup_ = true;
147#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148}
149
150
151void RuntimeProfiler::Optimize(JSFunction* function, bool eager, int delay) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000152 ASSERT(function->IsOptimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000153 if (FLAG_trace_opt) {
154 PrintF("[marking (%s) ", eager ? "eagerly" : "lazily");
155 function->PrintName();
156 PrintF(" for recompilation");
157 if (delay > 0) {
158 PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000);
159 }
160 PrintF("]\n");
161 }
162
163 // The next call to the function will trigger optimization.
164 function->MarkForLazyRecompilation();
165}
166
167
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000169 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
170 // Debug::has_break_points().
171 ASSERT(function->IsMarkedForLazyRecompilation());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000172 if (!FLAG_use_osr ||
173 isolate_->debug()->has_break_points() ||
174 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000175 return;
176 }
177
178 SharedFunctionInfo* shared = function->shared();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000179 // If the code is not optimizable or references context slots, don't try OSR.
180 if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) {
181 return;
182 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000183
184 // We are not prepared to do OSR for a function that already has an
185 // allocated arguments object. The optimized code would bypass it for
186 // arguments accesses, which is unsound. Don't try OSR.
187 if (shared->scope_info()->HasArgumentsShadow()) return;
188
189 // We're using on-stack replacement: patch the unoptimized code so that
190 // any back edge in any unoptimized frame will trigger on-stack
191 // replacement for that frame.
192 if (FLAG_trace_osr) {
193 PrintF("[patching stack checks in ");
194 function->PrintName();
195 PrintF(" for on-stack replacement]\n");
196 }
197
198 // Get the stack check stub code object to match against. We aren't
199 // prepared to generate it, but we don't expect to have to.
200 StackCheckStub check_stub;
201 Object* check_code;
202 MaybeObject* maybe_check_code = check_stub.TryGetCode();
203 if (maybe_check_code->ToObject(&check_code)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 Code* replacement_code =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000205 isolate_->builtins()->builtin(Builtins::kOnStackReplacement);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000206 Code* unoptimized_code = shared->code();
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000207 Deoptimizer::PatchStackCheckCode(unoptimized_code,
208 Code::cast(check_code),
209 replacement_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000210 }
211}
212
213
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000214void RuntimeProfiler::ClearSampleBuffer() {
215 memset(sampler_window_, 0, sizeof(sampler_window_));
216 memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000217}
218
219
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000220int RuntimeProfiler::LookupSample(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000221 int weight = 0;
222 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000223 Object* sample = sampler_window_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000224 if (sample != NULL) {
225 if (function == sample) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000226 weight += sampler_window_weight_[i];
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000227 }
228 }
229 }
230 return weight;
231}
232
233
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000234void RuntimeProfiler::AddSample(JSFunction* function, int weight) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000235 ASSERT(IsPowerOf2(kSamplerWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000236 sampler_window_[sampler_window_position_] = function;
237 sampler_window_weight_[sampler_window_position_] = weight;
238 sampler_window_position_ = (sampler_window_position_ + 1) &
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000239 (kSamplerWindowSize - 1);
240}
241
242
243void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244 HandleScope scope(isolate_);
245 PendingListNode* current = optimize_soon_list_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000246 while (current != NULL) {
247 PendingListNode* next = current->next();
248 if (current->IsValid()) {
249 Handle<JSFunction> function = current->function();
250 int delay = current->Delay();
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000251 if (function->IsOptimizable()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000252 Optimize(*function, true, delay);
253 }
254 }
255 delete current;
256 current = next;
257 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000258 optimize_soon_list_ = NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000259
260 // Run through the JavaScript frames and collect them. If we already
261 // have a sample of the function, we mark it for optimizations
262 // (eagerly or lazily).
263 JSFunction* samples[kSamplerFrameCount];
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000264 int sample_count = 0;
265 int frame_count = 0;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000266 for (JavaScriptFrameIterator it(isolate_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000267 frame_count++ < kSamplerFrameCount && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000268 it.Advance()) {
269 JavaScriptFrame* frame = it.frame();
270 JSFunction* function = JSFunction::cast(frame->function());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000271
272 // Adjust threshold each time we have processed
273 // a certain number of ticks.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274 if (sampler_ticks_until_threshold_adjustment_ > 0) {
275 sampler_ticks_until_threshold_adjustment_--;
276 if (sampler_ticks_until_threshold_adjustment_ <= 0) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000277 // If the threshold is not already at the minimum
278 // modify and reset the ticks until next adjustment.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 if (sampler_threshold_ > kSamplerThresholdMin) {
280 sampler_threshold_ -= kSamplerThresholdDelta;
281 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000282 kSamplerTicksBetweenThresholdAdjustment;
283 }
284 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000285 }
286
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000287 if (function->IsMarkedForLazyRecompilation()) {
288 Code* unoptimized = function->shared()->code();
289 int nesting = unoptimized->allow_osr_at_loop_nesting_level();
290 if (nesting == 0) AttemptOnStackReplacement(function);
291 int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker);
292 unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000293 }
294
295 // Do not record non-optimizable functions.
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000296 if (!function->IsOptimizable()) continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000297 samples[sample_count++] = function;
298
299 int function_size = function->shared()->SourceSize();
300 int threshold_size_factor = (function_size > kSizeLimit)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000301 ? sampler_threshold_size_factor_
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000302 : 1;
303
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000304 int threshold = sampler_threshold_ * threshold_size_factor;
305 int current_js_ratio = NoBarrier_Load(&js_ratio_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000306
307 // Adjust threshold depending on the ratio of time spent
308 // in JS code.
309 if (current_js_ratio < 20) {
310 // If we spend less than 20% of the time in JS code,
311 // do not optimize.
312 continue;
313 } else if (current_js_ratio < 75) {
314 // Below 75% of time spent in JS code, only optimize very
315 // frequently used functions.
316 threshold *= 3;
317 }
318
319 if (LookupSample(function) >= threshold) {
320 Optimize(function, false, 0);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000321 isolate_->compilation_cache()->MarkForEagerOptimizing(
322 Handle<JSFunction>(function));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000323 }
324 }
325
326 // Add the collected functions as samples. It's important not to do
327 // this as part of collecting them because this will interfere with
328 // the sample lookup in case of recursive functions.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000329 for (int i = 0; i < sample_count; i++) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000330 AddSample(samples[i], kSamplerFrameWeight[i]);
331 }
332}
333
334
335void RuntimeProfiler::OptimizeSoon(JSFunction* function) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000336 if (!function->IsOptimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000337 PendingListNode* node = new PendingListNode(function);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000338 node->set_next(optimize_soon_list_);
339 optimize_soon_list_ = node;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000340}
341
342
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000343#ifdef ENABLE_LOGGING_AND_PROFILING
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000344void RuntimeProfiler::UpdateStateRatio(SamplerState current_state) {
345 SamplerState old_state = state_window_[state_window_position_];
346 state_counts_[old_state]--;
347 state_window_[state_window_position_] = current_state;
348 state_counts_[current_state]++;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000349 ASSERT(IsPowerOf2(kStateWindowSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000350 state_window_position_ = (state_window_position_ + 1) &
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000351 (kStateWindowSize - 1);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000352 // Note: to calculate correct ratio we have to track how many valid
353 // ticks are actually in the state window, because on profiler
354 // startup this number can be less than the window size.
355 state_window_ticks_ = Min(kStateWindowSize, state_window_ticks_ + 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 NoBarrier_Store(&js_ratio_, state_counts_[IN_JS_STATE] * 100 /
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000357 state_window_ticks_);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000358}
359#endif
360
361
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000362void RuntimeProfiler::NotifyTick() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000363#ifdef ENABLE_LOGGING_AND_PROFILING
364 // Record state sample.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000365 SamplerState state = IsSomeIsolateInJS()
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000366 ? IN_JS_STATE
367 : IN_NON_JS_STATE;
368 UpdateStateRatio(state);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000369 isolate_->stack_guard()->RequestRuntimeProfilerTick();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000370#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371}
372
373
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000374void RuntimeProfiler::Setup() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000375 ASSERT(has_been_globally_setup_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376 ClearSampleBuffer();
377 // If the ticker hasn't already started, make sure to do so to get
378 // the ticks for the runtime profiler.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379 if (IsEnabled()) isolate_->logger()->EnsureTickerStarted();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000380}
381
382
383void RuntimeProfiler::Reset() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384 sampler_threshold_ = kSamplerThresholdInit;
385 sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit;
386 sampler_ticks_until_threshold_adjustment_ =
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000387 kSamplerTicksBetweenThresholdAdjustment;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000388}
389
390
391void RuntimeProfiler::TearDown() {
392 // Nothing to do.
393}
394
395
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000396int RuntimeProfiler::SamplerWindowSize() {
397 return kSamplerWindowSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000398}
399
400
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000401// Update the pointers in the sampler window after a GC.
402void RuntimeProfiler::UpdateSamplesAfterScavenge() {
403 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404 Object* function = sampler_window_[i];
405 if (function != NULL && isolate_->heap()->InNewSpace(function)) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000406 MapWord map_word = HeapObject::cast(function)->map_word();
407 if (map_word.IsForwardingAddress()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000408 sampler_window_[i] = map_word.ToForwardingAddress();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000409 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000411 }
412 }
413 }
414}
415
416
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417void RuntimeProfiler::HandleWakeUp(Isolate* isolate) {
418#ifdef ENABLE_LOGGING_AND_PROFILING
419 // The profiler thread must still be waiting.
420 ASSERT(NoBarrier_Load(&state_) >= 0);
421 // In IsolateEnteredJS we have already incremented the counter and
422 // undid the decrement done by the profiler thread. Increment again
423 // to get the right count of active isolates.
424 NoBarrier_AtomicIncrement(&state_, 1);
425 semaphore_->Signal();
426 isolate->ResetEagerOptimizingData();
427#endif
428}
429
430
431bool RuntimeProfiler::IsSomeIsolateInJS() {
432 return NoBarrier_Load(&state_) > 0;
433}
434
435
436bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() {
437#ifdef ENABLE_LOGGING_AND_PROFILING
438 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
439 ASSERT(old_state >= -1);
440 if (old_state != 0) return false;
441 semaphore_->Wait();
442#endif
443 return true;
444}
445
446
447void RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown() {
448#ifdef ENABLE_LOGGING_AND_PROFILING
449 semaphore_->Signal();
450#endif
451}
452
453
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000454void RuntimeProfiler::RemoveDeadSamples() {
455 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000456 Object* function = sampler_window_[i];
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000457 if (function != NULL && !HeapObject::cast(function)->IsMarked()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 sampler_window_[i] = NULL;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000459 }
460 }
461}
462
463
464void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
465 for (int i = 0; i < kSamplerWindowSize; i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000466 visitor->VisitPointer(&sampler_window_[i]);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000467 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000468}
469
470
471bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000472#ifdef ENABLE_LOGGING_AND_PROFILING
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000473 static const int kNonJSTicksThreshold = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000474 if (RuntimeProfiler::IsSomeIsolateInJS()) {
475 non_js_ticks_ = 0;
476 } else {
477 if (non_js_ticks_ < kNonJSTicksThreshold) {
478 ++non_js_ticks_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000479 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000480 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000481 }
482 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000483#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000484 return false;
485}
486
487
488} } // namespace v8::internal