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