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