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