Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 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" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 38 | #include "mark-compact.h" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 39 | #include "platform.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 40 | #include "scopeinfo.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
| 45 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 46 | // Optimization sampler constants. |
| 47 | static const int kSamplerFrameCount = 2; |
| 48 | static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 49 | |
| 50 | static const int kSamplerTicksBetweenThresholdAdjustment = 32; |
| 51 | |
| 52 | static const int kSamplerThresholdInit = 3; |
| 53 | static const int kSamplerThresholdMin = 1; |
| 54 | static const int kSamplerThresholdDelta = 1; |
| 55 | |
| 56 | static const int kSamplerThresholdSizeFactorInit = 3; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 57 | |
| 58 | static const int kSizeLimit = 1500; |
| 59 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 60 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 61 | Atomic32 RuntimeProfiler::state_ = 0; |
| 62 | // TODO(isolates): Create the semaphore lazily and clean it up when no |
| 63 | // longer required. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 64 | Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 65 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 66 | #ifdef DEBUG |
| 67 | bool RuntimeProfiler::has_been_globally_setup_ = false; |
| 68 | #endif |
| 69 | bool RuntimeProfiler::enabled_ = false; |
| 70 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 71 | |
| 72 | RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
| 73 | : isolate_(isolate), |
| 74 | sampler_threshold_(kSamplerThresholdInit), |
| 75 | sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), |
| 76 | sampler_ticks_until_threshold_adjustment_( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 77 | kSamplerTicksBetweenThresholdAdjustment), |
| 78 | sampler_window_position_(0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 79 | ClearSampleBuffer(); |
| 80 | } |
| 81 | |
| 82 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 83 | void RuntimeProfiler::GlobalSetup() { |
| 84 | ASSERT(!has_been_globally_setup_); |
| 85 | enabled_ = V8::UseCrankshaft() && FLAG_opt; |
| 86 | #ifdef DEBUG |
| 87 | has_been_globally_setup_ = true; |
| 88 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 92 | void RuntimeProfiler::Optimize(JSFunction* function) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 93 | ASSERT(function->IsOptimizable()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 94 | if (FLAG_trace_opt) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 95 | PrintF("[marking "); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 96 | function->PrintName(); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 97 | PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address())); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 98 | PrintF(" for recompilation"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 99 | PrintF("]\n"); |
| 100 | } |
| 101 | |
| 102 | // The next call to the function will trigger optimization. |
| 103 | function->MarkForLazyRecompilation(); |
| 104 | } |
| 105 | |
| 106 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 108 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 109 | // Debug::has_break_points(). |
| 110 | ASSERT(function->IsMarkedForLazyRecompilation()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 111 | if (!FLAG_use_osr || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 112 | isolate_->DebuggerHasBreakPoints() || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 113 | function->IsBuiltin()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
| 117 | SharedFunctionInfo* shared = function->shared(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 118 | // If the code is not optimizable, don't try OSR. |
| 119 | if (!shared->code()->optimizable()) return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 120 | |
| 121 | // We are not prepared to do OSR for a function that already has an |
| 122 | // allocated arguments object. The optimized code would bypass it for |
| 123 | // arguments accesses, which is unsound. Don't try OSR. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 124 | if (shared->uses_arguments()) return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 125 | |
| 126 | // We're using on-stack replacement: patch the unoptimized code so that |
| 127 | // any back edge in any unoptimized frame will trigger on-stack |
| 128 | // replacement for that frame. |
| 129 | if (FLAG_trace_osr) { |
| 130 | PrintF("[patching stack checks in "); |
| 131 | function->PrintName(); |
| 132 | PrintF(" for on-stack replacement]\n"); |
| 133 | } |
| 134 | |
| 135 | // Get the stack check stub code object to match against. We aren't |
| 136 | // prepared to generate it, but we don't expect to have to. |
| 137 | StackCheckStub check_stub; |
| 138 | Object* check_code; |
| 139 | MaybeObject* maybe_check_code = check_stub.TryGetCode(); |
| 140 | if (maybe_check_code->ToObject(&check_code)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 141 | Code* replacement_code = |
| 142 | isolate_->builtins()->builtin(Builtins::kOnStackReplacement); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 143 | Code* unoptimized_code = shared->code(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 144 | Deoptimizer::PatchStackCheckCode(unoptimized_code, |
| 145 | Code::cast(check_code), |
| 146 | replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 151 | void RuntimeProfiler::ClearSampleBuffer() { |
| 152 | memset(sampler_window_, 0, sizeof(sampler_window_)); |
| 153 | memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 157 | int RuntimeProfiler::LookupSample(JSFunction* function) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 158 | int weight = 0; |
| 159 | for (int i = 0; i < kSamplerWindowSize; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | Object* sample = sampler_window_[i]; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 161 | if (sample != NULL) { |
| 162 | if (function == sample) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 163 | weight += sampler_window_weight_[i]; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | return weight; |
| 168 | } |
| 169 | |
| 170 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 171 | void RuntimeProfiler::AddSample(JSFunction* function, int weight) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 172 | ASSERT(IsPowerOf2(kSamplerWindowSize)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 173 | sampler_window_[sampler_window_position_] = function; |
| 174 | sampler_window_weight_[sampler_window_position_] = weight; |
| 175 | sampler_window_position_ = (sampler_window_position_ + 1) & |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 176 | (kSamplerWindowSize - 1); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void RuntimeProfiler::OptimizeNow() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 181 | HandleScope scope(isolate_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 182 | |
| 183 | // Run through the JavaScript frames and collect them. If we already |
| 184 | // have a sample of the function, we mark it for optimizations |
| 185 | // (eagerly or lazily). |
| 186 | JSFunction* samples[kSamplerFrameCount]; |
| 187 | int sample_count = 0; |
| 188 | int frame_count = 0; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 189 | for (JavaScriptFrameIterator it(isolate_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 190 | frame_count++ < kSamplerFrameCount && !it.done(); |
| 191 | it.Advance()) { |
| 192 | JavaScriptFrame* frame = it.frame(); |
| 193 | JSFunction* function = JSFunction::cast(frame->function()); |
| 194 | |
| 195 | // Adjust threshold each time we have processed |
| 196 | // a certain number of ticks. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 197 | if (sampler_ticks_until_threshold_adjustment_ > 0) { |
| 198 | sampler_ticks_until_threshold_adjustment_--; |
| 199 | if (sampler_ticks_until_threshold_adjustment_ <= 0) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 200 | // If the threshold is not already at the minimum |
| 201 | // modify and reset the ticks until next adjustment. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 202 | if (sampler_threshold_ > kSamplerThresholdMin) { |
| 203 | sampler_threshold_ -= kSamplerThresholdDelta; |
| 204 | sampler_ticks_until_threshold_adjustment_ = |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 205 | kSamplerTicksBetweenThresholdAdjustment; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (function->IsMarkedForLazyRecompilation()) { |
| 211 | Code* unoptimized = function->shared()->code(); |
| 212 | int nesting = unoptimized->allow_osr_at_loop_nesting_level(); |
| 213 | if (nesting == 0) AttemptOnStackReplacement(function); |
| 214 | int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker); |
| 215 | unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting); |
| 216 | } |
| 217 | |
| 218 | // Do not record non-optimizable functions. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 219 | if (!function->IsOptimizable()) continue; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 220 | samples[sample_count++] = function; |
| 221 | |
| 222 | int function_size = function->shared()->SourceSize(); |
| 223 | int threshold_size_factor = (function_size > kSizeLimit) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 224 | ? sampler_threshold_size_factor_ |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 225 | : 1; |
| 226 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 227 | int threshold = sampler_threshold_ * threshold_size_factor; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 228 | |
| 229 | if (LookupSample(function) >= threshold) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 230 | Optimize(function); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | // Add the collected functions as samples. It's important not to do |
| 235 | // this as part of collecting them because this will interfere with |
| 236 | // the sample lookup in case of recursive functions. |
| 237 | for (int i = 0; i < sample_count; i++) { |
| 238 | AddSample(samples[i], kSamplerFrameWeight[i]); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 243 | void RuntimeProfiler::NotifyTick() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 244 | isolate_->stack_guard()->RequestRuntimeProfilerTick(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 248 | void RuntimeProfiler::Setup() { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 249 | ASSERT(has_been_globally_setup_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 250 | ClearSampleBuffer(); |
| 251 | // If the ticker hasn't already started, make sure to do so to get |
| 252 | // the ticks for the runtime profiler. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 253 | if (IsEnabled()) isolate_->logger()->EnsureTickerStarted(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | |
| 257 | void RuntimeProfiler::Reset() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 258 | sampler_threshold_ = kSamplerThresholdInit; |
| 259 | sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit; |
| 260 | sampler_ticks_until_threshold_adjustment_ = |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 261 | kSamplerTicksBetweenThresholdAdjustment; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | void RuntimeProfiler::TearDown() { |
| 266 | // Nothing to do. |
| 267 | } |
| 268 | |
| 269 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 270 | int RuntimeProfiler::SamplerWindowSize() { |
| 271 | return kSamplerWindowSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 275 | // Update the pointers in the sampler window after a GC. |
| 276 | void RuntimeProfiler::UpdateSamplesAfterScavenge() { |
| 277 | for (int i = 0; i < kSamplerWindowSize; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 278 | Object* function = sampler_window_[i]; |
| 279 | if (function != NULL && isolate_->heap()->InNewSpace(function)) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 280 | MapWord map_word = HeapObject::cast(function)->map_word(); |
| 281 | if (map_word.IsForwardingAddress()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 282 | sampler_window_[i] = map_word.ToForwardingAddress(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 283 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 284 | sampler_window_[i] = NULL; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 291 | void RuntimeProfiler::HandleWakeUp(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | // The profiler thread must still be waiting. |
| 293 | ASSERT(NoBarrier_Load(&state_) >= 0); |
| 294 | // In IsolateEnteredJS we have already incremented the counter and |
| 295 | // undid the decrement done by the profiler thread. Increment again |
| 296 | // to get the right count of active isolates. |
| 297 | NoBarrier_AtomicIncrement(&state_, 1); |
| 298 | semaphore_->Signal(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
| 302 | bool RuntimeProfiler::IsSomeIsolateInJS() { |
| 303 | return NoBarrier_Load(&state_) > 0; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 308 | Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1); |
| 309 | ASSERT(old_state >= -1); |
| 310 | if (old_state != 0) return false; |
| 311 | semaphore_->Wait(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
| 315 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 316 | void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) { |
| 317 | // Do a fake increment. If the profiler is waiting on the semaphore, |
| 318 | // the returned state is 0, which can be left as an initial state in |
| 319 | // case profiling is restarted later. If the profiler is not |
| 320 | // waiting, the increment will prevent it from waiting, but has to |
| 321 | // be undone after the profiler is stopped. |
| 322 | Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1); |
| 323 | ASSERT(new_state >= 0); |
| 324 | if (new_state == 0) { |
| 325 | // The profiler thread is waiting. Wake it up. It must check for |
| 326 | // stop conditions before attempting to wait again. |
| 327 | semaphore_->Signal(); |
| 328 | } |
| 329 | thread->Join(); |
| 330 | // The profiler thread is now stopped. Undo the increment in case it |
| 331 | // was not waiting. |
| 332 | if (new_state != 0) { |
| 333 | NoBarrier_AtomicIncrement(&state_, -1); |
| 334 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 338 | void RuntimeProfiler::RemoveDeadSamples() { |
| 339 | for (int i = 0; i < kSamplerWindowSize; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 340 | Object* function = sampler_window_[i]; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 341 | if (function != NULL && !HeapObject::cast(function)->IsMarked()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 342 | sampler_window_[i] = NULL; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
| 349 | for (int i = 0; i < kSamplerWindowSize; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 350 | visitor->VisitPointer(&sampler_window_[i]); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 351 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
| 355 | bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 356 | if (!RuntimeProfiler::IsSomeIsolateInJS()) { |
| 357 | return RuntimeProfiler::WaitForSomeIsolateToEnterJS(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 358 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | } } // namespace v8::internal |