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" |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 34 | #include "code-stubs.h" |
| 35 | #include "compilation-cache.h" |
| 36 | #include "deoptimizer.h" |
| 37 | #include "execution.h" |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 38 | #include "full-codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 39 | #include "global-handles.h" |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 40 | #include "isolate-inl.h" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 41 | #include "mark-compact.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 42 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 43 | #include "scopeinfo.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 44 | |
| 45 | namespace v8 { |
| 46 | namespace internal { |
| 47 | |
| 48 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 49 | // Optimization sampler constants. |
| 50 | static const int kSamplerFrameCount = 2; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 51 | |
| 52 | // Constants for statistical profiler. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 53 | static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 54 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 55 | static const int kSamplerTicksBetweenThresholdAdjustment = 32; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 56 | |
| 57 | static const int kSamplerThresholdInit = 3; |
| 58 | static const int kSamplerThresholdMin = 1; |
| 59 | static const int kSamplerThresholdDelta = 1; |
| 60 | |
| 61 | static const int kSamplerThresholdSizeFactorInit = 3; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 62 | |
| 63 | static const int kSizeLimit = 1500; |
| 64 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 65 | // Constants for counter based profiler. |
| 66 | |
| 67 | // Number of times a function has to be seen on the stack before it is |
| 68 | // optimized. |
| 69 | static const int kProfilerTicksBeforeOptimization = 2; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 70 | // If the function optimization was disabled due to high deoptimization count, |
| 71 | // but the function is hot and has been seen on the stack this number of times, |
| 72 | // then we try to reenable optimization for this function. |
| 73 | static const int kProfilerTicksBeforeReenablingOptimization = 250; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 74 | // If a function does not have enough type info (according to |
| 75 | // FLAG_type_info_threshold), but has seen a huge number of ticks, |
| 76 | // optimize it as it is. |
| 77 | static const int kTicksWhenNotEnoughTypeInfo = 100; |
| 78 | // We only have one byte to store the number of ticks. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 79 | STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256); |
| 80 | STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 81 | STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 82 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 83 | // Maximum size in bytes of generate code for a function to allow OSR. |
| 84 | static const int kOSRCodeSizeAllowanceBase = |
| 85 | 100 * FullCodeGenerator::kCodeSizeMultiplier; |
| 86 | |
| 87 | static const int kOSRCodeSizeAllowancePerTick = |
| 88 | 3 * FullCodeGenerator::kCodeSizeMultiplier; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 89 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 90 | // Maximum size in bytes of generated code for a function to be optimized |
| 91 | // the very first time it is seen on the stack. |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 92 | static const int kMaxSizeEarlyOpt = |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 93 | 5 * FullCodeGenerator::kCodeSizeMultiplier; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 94 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 95 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 96 | RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
| 97 | : isolate_(isolate), |
| 98 | sampler_threshold_(kSamplerThresholdInit), |
| 99 | sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), |
| 100 | sampler_ticks_until_threshold_adjustment_( |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 101 | kSamplerTicksBetweenThresholdAdjustment), |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 102 | sampler_window_position_(0), |
| 103 | any_ic_changed_(false), |
| 104 | code_generated_(false) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 105 | ClearSampleBuffer(); |
| 106 | } |
| 107 | |
| 108 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 109 | static void GetICCounts(Code* shared_code, |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 110 | int* ic_with_type_info_count, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 111 | int* ic_total_count, |
| 112 | int* percentage) { |
| 113 | *ic_total_count = 0; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 114 | *ic_with_type_info_count = 0; |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 115 | Object* raw_info = shared_code->type_feedback_info(); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 116 | if (raw_info->IsTypeFeedbackInfo()) { |
| 117 | TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 118 | *ic_with_type_info_count = info->ic_with_type_info_count(); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 119 | *ic_total_count = info->ic_total_count(); |
| 120 | } |
| 121 | *percentage = *ic_total_count > 0 |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 122 | ? 100 * *ic_with_type_info_count / *ic_total_count |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 123 | : 100; |
| 124 | } |
| 125 | |
| 126 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 127 | void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 128 | ASSERT(function->IsOptimizable()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 129 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 130 | if (FLAG_trace_opt && function->PassesHydrogenFilter()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 131 | PrintF("[marking "); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 132 | function->ShortPrint(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 133 | PrintF(" for recompilation, reason: %s", reason); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 134 | if (FLAG_type_info_threshold > 0) { |
| 135 | int typeinfo, total, percentage; |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 136 | GetICCounts(function->shared()->code(), &typeinfo, &total, &percentage); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 137 | PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage); |
| 138 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 139 | PrintF("]\n"); |
| 140 | } |
| 141 | |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 142 | if (FLAG_parallel_recompilation && !isolate_->bootstrapper()->IsActive()) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 143 | ASSERT(!function->IsMarkedForInstallingRecompiledCode()); |
| 144 | ASSERT(!function->IsInRecompileQueue()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 145 | function->MarkForParallelRecompilation(); |
| 146 | } else { |
| 147 | // The next call to the function will trigger optimization. |
| 148 | function->MarkForLazyRecompilation(); |
| 149 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 153 | void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 154 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 155 | // Debug::has_break_points(). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 156 | if (!FLAG_use_osr || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 157 | isolate_->DebuggerHasBreakPoints() || |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | function->IsBuiltin()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | SharedFunctionInfo* shared = function->shared(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 163 | // If the code is not optimizable, don't try OSR. |
| 164 | if (!shared->code()->optimizable()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 165 | |
| 166 | // We are not prepared to do OSR for a function that already has an |
| 167 | // allocated arguments object. The optimized code would bypass it for |
| 168 | // arguments accesses, which is unsound. Don't try OSR. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 169 | if (shared->uses_arguments()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 170 | |
| 171 | // We're using on-stack replacement: patch the unoptimized code so that |
| 172 | // any back edge in any unoptimized frame will trigger on-stack |
| 173 | // replacement for that frame. |
| 174 | if (FLAG_trace_osr) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 175 | PrintF("[patching back edges in "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 176 | function->PrintName(); |
| 177 | PrintF(" for on-stack replacement]\n"); |
| 178 | } |
| 179 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 180 | // Get the interrupt stub code object to match against. We aren't |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 181 | // prepared to generate it, but we don't expect to have to. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 182 | Code* interrupt_code = NULL; |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 183 | InterruptStub interrupt_stub; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 184 | bool found_code = interrupt_stub.FindCodeInCache(&interrupt_code, isolate_); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 185 | if (found_code) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 186 | Code* replacement_code = |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 187 | isolate_->builtins()->builtin(Builtins::kOnStackReplacement); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 188 | Code* unoptimized_code = shared->code(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 189 | Deoptimizer::PatchInterruptCode( |
| 190 | unoptimized_code, interrupt_code, replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 195 | void RuntimeProfiler::ClearSampleBuffer() { |
| 196 | memset(sampler_window_, 0, sizeof(sampler_window_)); |
| 197 | memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 201 | int RuntimeProfiler::LookupSample(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 202 | int weight = 0; |
| 203 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 204 | Object* sample = sampler_window_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 205 | if (sample != NULL) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 206 | bool fits = FLAG_lookup_sample_by_shared |
| 207 | ? (function->shared() == JSFunction::cast(sample)->shared()) |
| 208 | : (function == JSFunction::cast(sample)); |
| 209 | if (fits) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 210 | weight += sampler_window_weight_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } |
| 214 | return weight; |
| 215 | } |
| 216 | |
| 217 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 218 | void RuntimeProfiler::AddSample(JSFunction* function, int weight) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 219 | ASSERT(IsPowerOf2(kSamplerWindowSize)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 220 | sampler_window_[sampler_window_position_] = function; |
| 221 | sampler_window_weight_[sampler_window_position_] = weight; |
| 222 | sampler_window_position_ = (sampler_window_position_ + 1) & |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 223 | (kSamplerWindowSize - 1); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void RuntimeProfiler::OptimizeNow() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 228 | HandleScope scope(isolate_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 229 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 230 | if (FLAG_parallel_recompilation) { |
| 231 | // Take this as opportunity to process the optimizing compiler thread's |
| 232 | // output queue so that it does not unnecessarily keep objects alive. |
| 233 | isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions(); |
| 234 | } |
| 235 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 236 | // Run through the JavaScript frames and collect them. If we already |
| 237 | // have a sample of the function, we mark it for optimizations |
| 238 | // (eagerly or lazily). |
| 239 | JSFunction* samples[kSamplerFrameCount]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 240 | int sample_count = 0; |
| 241 | int frame_count = 0; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 242 | int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count |
| 243 | : kSamplerFrameCount; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 244 | for (JavaScriptFrameIterator it(isolate_); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 245 | frame_count++ < frame_count_limit && !it.done(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 246 | it.Advance()) { |
| 247 | JavaScriptFrame* frame = it.frame(); |
| 248 | JSFunction* function = JSFunction::cast(frame->function()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 249 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 250 | if (!FLAG_watch_ic_patching) { |
| 251 | // Adjust threshold each time we have processed |
| 252 | // a certain number of ticks. |
| 253 | if (sampler_ticks_until_threshold_adjustment_ > 0) { |
| 254 | sampler_ticks_until_threshold_adjustment_--; |
| 255 | if (sampler_ticks_until_threshold_adjustment_ <= 0) { |
| 256 | // If the threshold is not already at the minimum |
| 257 | // modify and reset the ticks until next adjustment. |
| 258 | if (sampler_threshold_ > kSamplerThresholdMin) { |
| 259 | sampler_threshold_ -= kSamplerThresholdDelta; |
| 260 | sampler_ticks_until_threshold_adjustment_ = |
| 261 | kSamplerTicksBetweenThresholdAdjustment; |
| 262 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 265 | } |
| 266 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 267 | SharedFunctionInfo* shared = function->shared(); |
| 268 | Code* shared_code = shared->code(); |
| 269 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 270 | if (shared_code->kind() != Code::FUNCTION) continue; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 271 | if (function->IsInRecompileQueue()) continue; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 272 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 273 | if (FLAG_always_osr && |
| 274 | shared_code->allow_osr_at_loop_nesting_level() == 0) { |
| 275 | // Testing mode: always try an OSR compile for every function. |
| 276 | for (int i = 0; i < Code::kMaxLoopNestingMarker; i++) { |
| 277 | // TODO(titzer): fix AttemptOnStackReplacement to avoid this dumb loop. |
| 278 | shared_code->set_allow_osr_at_loop_nesting_level(i); |
| 279 | AttemptOnStackReplacement(function); |
| 280 | } |
| 281 | // Fall through and do a normal optimized compile as well. |
| 282 | } else if (!frame->is_optimized() && |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 283 | (function->IsMarkedForLazyRecompilation() || |
| 284 | function->IsMarkedForParallelRecompilation() || |
| 285 | function->IsOptimized())) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame^] | 286 | // Attempt OSR if we are still running unoptimized code even though the |
| 287 | // the function has long been marked or even already been optimized. |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 288 | int ticks = shared_code->profiler_ticks(); |
| 289 | int allowance = kOSRCodeSizeAllowanceBase + |
| 290 | ticks * kOSRCodeSizeAllowancePerTick; |
| 291 | if (shared_code->CodeSize() > allowance) { |
| 292 | if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1); |
| 293 | } else { |
| 294 | int nesting = shared_code->allow_osr_at_loop_nesting_level(); |
| 295 | if (nesting < Code::kMaxLoopNestingMarker) { |
| 296 | int new_nesting = nesting + 1; |
| 297 | shared_code->set_allow_osr_at_loop_nesting_level(new_nesting); |
| 298 | AttemptOnStackReplacement(function); |
| 299 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 300 | } |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 301 | continue; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 302 | } |
| 303 | |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 304 | // Only record top-level code on top of the execution stack and |
| 305 | // avoid optimizing excessively large scripts since top-level code |
| 306 | // will be executed only once. |
| 307 | const int kMaxToplevelSourceSize = 10 * 1024; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 308 | if (shared->is_toplevel() && |
| 309 | (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) { |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 310 | continue; |
| 311 | } |
| 312 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 313 | // Do not record non-optimizable functions. |
| 314 | if (shared->optimization_disabled()) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 315 | if (shared->deopt_count() >= FLAG_max_opt_count) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 316 | // If optimization was disabled due to many deoptimizations, |
| 317 | // then check if the function is hot and try to reenable optimization. |
| 318 | int ticks = shared_code->profiler_ticks(); |
| 319 | if (ticks >= kProfilerTicksBeforeReenablingOptimization) { |
| 320 | shared_code->set_profiler_ticks(0); |
| 321 | shared->TryReenableOptimization(); |
| 322 | } else { |
| 323 | shared_code->set_profiler_ticks(ticks + 1); |
| 324 | } |
| 325 | } |
| 326 | continue; |
| 327 | } |
| 328 | if (!function->IsOptimizable()) continue; |
| 329 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 330 | if (FLAG_watch_ic_patching) { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 331 | int ticks = shared_code->profiler_ticks(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 332 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 333 | if (ticks >= kProfilerTicksBeforeOptimization) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 334 | int typeinfo, total, percentage; |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 335 | GetICCounts(shared_code, &typeinfo, &total, &percentage); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 336 | if (percentage >= FLAG_type_info_threshold) { |
| 337 | // If this particular function hasn't had any ICs patched for enough |
| 338 | // ticks, optimize it now. |
| 339 | Optimize(function, "hot and stable"); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 340 | } else if (ticks >= kTicksWhenNotEnoughTypeInfo) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 341 | Optimize(function, "not much type info but very hot"); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 342 | } else { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 343 | shared_code->set_profiler_ticks(ticks + 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 344 | if (FLAG_trace_opt_verbose) { |
| 345 | PrintF("[not yet optimizing "); |
| 346 | function->PrintName(); |
| 347 | PrintF(", not enough type info: %d/%d (%d%%)]\n", |
| 348 | typeinfo, total, percentage); |
| 349 | } |
| 350 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 351 | } else if (!any_ic_changed_ && |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 352 | shared_code->instruction_size() < kMaxSizeEarlyOpt) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 353 | // If no IC was patched since the last tick and this function is very |
| 354 | // small, optimistically optimize it now. |
| 355 | Optimize(function, "small function"); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 356 | } else { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 357 | shared_code->set_profiler_ticks(ticks + 1); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 358 | } |
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 | samples[sample_count++] = function; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 361 | |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 362 | int function_size = function->shared()->SourceSize(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 363 | int threshold_size_factor = (function_size > kSizeLimit) |
| 364 | ? sampler_threshold_size_factor_ |
| 365 | : 1; |
| 366 | |
| 367 | int threshold = sampler_threshold_ * threshold_size_factor; |
| 368 | |
| 369 | if (LookupSample(function) >= threshold) { |
| 370 | Optimize(function, "sampler window lookup"); |
| 371 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 374 | if (FLAG_watch_ic_patching) { |
| 375 | any_ic_changed_ = false; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 376 | } else { // !FLAG_watch_ic_patching |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 377 | // Add the collected functions as samples. It's important not to do |
| 378 | // this as part of collecting them because this will interfere with |
| 379 | // the sample lookup in case of recursive functions. |
| 380 | for (int i = 0; i < sample_count; i++) { |
| 381 | AddSample(samples[i], kSamplerFrameWeight[i]); |
| 382 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 387 | void RuntimeProfiler::SetUp() { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 388 | if (!FLAG_watch_ic_patching) { |
| 389 | ClearSampleBuffer(); |
| 390 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
| 394 | void RuntimeProfiler::Reset() { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 395 | if (!FLAG_watch_ic_patching) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 396 | sampler_threshold_ = kSamplerThresholdInit; |
| 397 | sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit; |
| 398 | sampler_ticks_until_threshold_adjustment_ = |
| 399 | kSamplerTicksBetweenThresholdAdjustment; |
| 400 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | |
| 404 | void RuntimeProfiler::TearDown() { |
| 405 | // Nothing to do. |
| 406 | } |
| 407 | |
| 408 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 409 | int RuntimeProfiler::SamplerWindowSize() { |
| 410 | return kSamplerWindowSize; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 414 | // Update the pointers in the sampler window after a GC. |
| 415 | void RuntimeProfiler::UpdateSamplesAfterScavenge() { |
| 416 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 417 | Object* function = sampler_window_[i]; |
| 418 | if (function != NULL && isolate_->heap()->InNewSpace(function)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 419 | MapWord map_word = HeapObject::cast(function)->map_word(); |
| 420 | if (map_word.IsForwardingAddress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 421 | sampler_window_[i] = map_word.ToForwardingAddress(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 422 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 423 | sampler_window_[i] = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
| 430 | void RuntimeProfiler::RemoveDeadSamples() { |
| 431 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 432 | Object* function = sampler_window_[i]; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 433 | if (function != NULL && |
| 434 | !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 435 | sampler_window_[i] = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | |
| 441 | void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
| 442 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 443 | visitor->VisitPointer(&sampler_window_[i]); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 444 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 448 | } } // namespace v8::internal |