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