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