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 | RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
| 90 | : isolate_(isolate), |
| 91 | sampler_threshold_(kSamplerThresholdInit), |
| 92 | sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), |
| 93 | sampler_ticks_until_threshold_adjustment_( |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 94 | kSamplerTicksBetweenThresholdAdjustment), |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 95 | sampler_window_position_(0), |
| 96 | any_ic_changed_(false), |
| 97 | code_generated_(false) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 98 | ClearSampleBuffer(); |
| 99 | } |
| 100 | |
| 101 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 102 | static void GetICCounts(JSFunction* function, |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 103 | int* ic_with_type_info_count, |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 104 | int* ic_total_count, |
| 105 | int* percentage) { |
| 106 | *ic_total_count = 0; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 107 | *ic_with_type_info_count = 0; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 108 | Object* raw_info = |
| 109 | function->shared()->code()->type_feedback_info(); |
| 110 | if (raw_info->IsTypeFeedbackInfo()) { |
| 111 | TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 112 | *ic_with_type_info_count = info->ic_with_type_info_count(); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 113 | *ic_total_count = info->ic_total_count(); |
| 114 | } |
| 115 | *percentage = *ic_total_count > 0 |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 116 | ? 100 * *ic_with_type_info_count / *ic_total_count |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 117 | : 100; |
| 118 | } |
| 119 | |
| 120 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 121 | void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 122 | ASSERT(function->IsOptimizable()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 123 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 124 | if (FLAG_trace_opt) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 125 | PrintF("[marking "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 126 | function->PrintName(); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 127 | PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address())); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 128 | PrintF(" for recompilation, reason: %s", reason); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 129 | if (FLAG_type_info_threshold > 0) { |
| 130 | int typeinfo, total, percentage; |
| 131 | GetICCounts(function, &typeinfo, &total, &percentage); |
| 132 | PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage); |
| 133 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 134 | PrintF("]\n"); |
| 135 | } |
| 136 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 137 | if (FLAG_parallel_recompilation) { |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 138 | ASSERT(!function->IsMarkedForInstallingRecompiledCode()); |
| 139 | ASSERT(!function->IsInRecompileQueue()); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 140 | function->MarkForParallelRecompilation(); |
| 141 | } else { |
| 142 | // The next call to the function will trigger optimization. |
| 143 | function->MarkForLazyRecompilation(); |
| 144 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 148 | void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 149 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 150 | // Debug::has_break_points(). |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 151 | ASSERT(function->IsMarkedForLazyRecompilation() || |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 152 | function->IsMarkedForParallelRecompilation() || |
| 153 | function->IsOptimized()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 154 | if (!FLAG_use_osr || |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 155 | isolate_->DebuggerHasBreakPoints() || |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 156 | function->IsBuiltin()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
| 160 | SharedFunctionInfo* shared = function->shared(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 161 | // If the code is not optimizable, don't try OSR. |
| 162 | if (!shared->code()->optimizable()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 163 | |
| 164 | // We are not prepared to do OSR for a function that already has an |
| 165 | // allocated arguments object. The optimized code would bypass it for |
| 166 | // arguments accesses, which is unsound. Don't try OSR. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 167 | if (shared->uses_arguments()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 168 | |
| 169 | // We're using on-stack replacement: patch the unoptimized code so that |
| 170 | // any back edge in any unoptimized frame will trigger on-stack |
| 171 | // replacement for that frame. |
| 172 | if (FLAG_trace_osr) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 173 | PrintF("[patching back edges in "); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 174 | function->PrintName(); |
| 175 | PrintF(" for on-stack replacement]\n"); |
| 176 | } |
| 177 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 178 | // 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] | 179 | // 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^] | 180 | Code* interrupt_code = NULL; |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 181 | InterruptStub interrupt_stub; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 182 | bool found_code = interrupt_stub.FindCodeInCache(&interrupt_code, isolate_); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 183 | if (found_code) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 184 | Code* replacement_code = |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 185 | isolate_->builtins()->builtin(Builtins::kOnStackReplacement); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 186 | Code* unoptimized_code = shared->code(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 187 | Deoptimizer::PatchInterruptCode( |
| 188 | unoptimized_code, interrupt_code, replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 193 | void RuntimeProfiler::ClearSampleBuffer() { |
| 194 | memset(sampler_window_, 0, sizeof(sampler_window_)); |
| 195 | memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 199 | int RuntimeProfiler::LookupSample(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 200 | int weight = 0; |
| 201 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | Object* sample = sampler_window_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 203 | if (sample != NULL) { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 204 | bool fits = FLAG_lookup_sample_by_shared |
| 205 | ? (function->shared() == JSFunction::cast(sample)->shared()) |
| 206 | : (function == JSFunction::cast(sample)); |
| 207 | if (fits) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 208 | weight += sampler_window_weight_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | return weight; |
| 213 | } |
| 214 | |
| 215 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 216 | void RuntimeProfiler::AddSample(JSFunction* function, int weight) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 217 | ASSERT(IsPowerOf2(kSamplerWindowSize)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 218 | sampler_window_[sampler_window_position_] = function; |
| 219 | sampler_window_weight_[sampler_window_position_] = weight; |
| 220 | sampler_window_position_ = (sampler_window_position_ + 1) & |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 221 | (kSamplerWindowSize - 1); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void RuntimeProfiler::OptimizeNow() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 226 | HandleScope scope(isolate_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 227 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 228 | if (FLAG_parallel_recompilation) { |
| 229 | // Take this as opportunity to process the optimizing compiler thread's |
| 230 | // output queue so that it does not unnecessarily keep objects alive. |
| 231 | isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions(); |
| 232 | } |
| 233 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 234 | // Run through the JavaScript frames and collect them. If we already |
| 235 | // have a sample of the function, we mark it for optimizations |
| 236 | // (eagerly or lazily). |
| 237 | JSFunction* samples[kSamplerFrameCount]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 238 | int sample_count = 0; |
| 239 | int frame_count = 0; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 240 | int frame_count_limit = FLAG_watch_ic_patching ? FLAG_frame_count |
| 241 | : kSamplerFrameCount; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 242 | for (JavaScriptFrameIterator it(isolate_); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 243 | frame_count++ < frame_count_limit && !it.done(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 244 | it.Advance()) { |
| 245 | JavaScriptFrame* frame = it.frame(); |
| 246 | JSFunction* function = JSFunction::cast(frame->function()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 247 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 248 | if (!FLAG_watch_ic_patching) { |
| 249 | // Adjust threshold each time we have processed |
| 250 | // a certain number of ticks. |
| 251 | if (sampler_ticks_until_threshold_adjustment_ > 0) { |
| 252 | sampler_ticks_until_threshold_adjustment_--; |
| 253 | if (sampler_ticks_until_threshold_adjustment_ <= 0) { |
| 254 | // If the threshold is not already at the minimum |
| 255 | // modify and reset the ticks until next adjustment. |
| 256 | if (sampler_threshold_ > kSamplerThresholdMin) { |
| 257 | sampler_threshold_ -= kSamplerThresholdDelta; |
| 258 | sampler_ticks_until_threshold_adjustment_ = |
| 259 | kSamplerTicksBetweenThresholdAdjustment; |
| 260 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 263 | } |
| 264 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 265 | SharedFunctionInfo* shared = function->shared(); |
| 266 | Code* shared_code = shared->code(); |
| 267 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 268 | if (shared_code->kind() != Code::FUNCTION) continue; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 269 | if (function->IsInRecompileQueue()) continue; |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 270 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 271 | // Attempt OSR if we are still running unoptimized code even though the |
| 272 | // the function has long been marked or even already been optimized. |
| 273 | if (!frame->is_optimized() && |
| 274 | (function->IsMarkedForLazyRecompilation() || |
| 275 | function->IsMarkedForParallelRecompilation() || |
| 276 | function->IsOptimized())) { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 277 | int nesting = shared_code->allow_osr_at_loop_nesting_level(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 278 | if (nesting < Code::kMaxLoopNestingMarker) { |
| 279 | int new_nesting = nesting + 1; |
| 280 | shared_code->set_allow_osr_at_loop_nesting_level(new_nesting); |
| 281 | AttemptOnStackReplacement(function); |
| 282 | } |
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()) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 296 | if (shared->deopt_count() >= FLAG_max_opt_count) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 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 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 311 | if (FLAG_watch_ic_patching) { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 312 | int ticks = shared_code->profiler_ticks(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 313 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 314 | if (ticks >= kProfilerTicksBeforeOptimization) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 315 | int typeinfo, total, percentage; |
| 316 | GetICCounts(function, &typeinfo, &total, &percentage); |
| 317 | if (percentage >= FLAG_type_info_threshold) { |
| 318 | // If this particular function hasn't had any ICs patched for enough |
| 319 | // ticks, optimize it now. |
| 320 | Optimize(function, "hot and stable"); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 321 | } else if (ticks >= kTicksWhenNotEnoughTypeInfo) { |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 322 | Optimize(function, "not much type info but very hot"); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 323 | } else { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 324 | shared_code->set_profiler_ticks(ticks + 1); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 325 | if (FLAG_trace_opt_verbose) { |
| 326 | PrintF("[not yet optimizing "); |
| 327 | function->PrintName(); |
| 328 | PrintF(", not enough type info: %d/%d (%d%%)]\n", |
| 329 | typeinfo, total, percentage); |
| 330 | } |
| 331 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 332 | } else if (!any_ic_changed_ && |
danno@chromium.org | 129d398 | 2012-07-25 15:01:47 +0000 | [diff] [blame] | 333 | shared_code->instruction_size() < kMaxSizeEarlyOpt) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 334 | // If no IC was patched since the last tick and this function is very |
| 335 | // small, optimistically optimize it now. |
| 336 | Optimize(function, "small function"); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 337 | } else { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 338 | shared_code->set_profiler_ticks(ticks + 1); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 339 | } |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 340 | } else { // !FLAG_watch_ic_patching |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 341 | samples[sample_count++] = function; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 342 | |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 343 | int function_size = function->shared()->SourceSize(); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 344 | int threshold_size_factor = (function_size > kSizeLimit) |
| 345 | ? sampler_threshold_size_factor_ |
| 346 | : 1; |
| 347 | |
| 348 | int threshold = sampler_threshold_ * threshold_size_factor; |
| 349 | |
| 350 | if (LookupSample(function) >= threshold) { |
| 351 | Optimize(function, "sampler window lookup"); |
| 352 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 355 | if (FLAG_watch_ic_patching) { |
| 356 | any_ic_changed_ = false; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 357 | } else { // !FLAG_watch_ic_patching |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 358 | // Add the collected functions as samples. It's important not to do |
| 359 | // this as part of collecting them because this will interfere with |
| 360 | // the sample lookup in case of recursive functions. |
| 361 | for (int i = 0; i < sample_count; i++) { |
| 362 | AddSample(samples[i], kSamplerFrameWeight[i]); |
| 363 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 368 | void RuntimeProfiler::SetUp() { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 369 | if (!FLAG_watch_ic_patching) { |
| 370 | ClearSampleBuffer(); |
| 371 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
| 375 | void RuntimeProfiler::Reset() { |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 376 | if (!FLAG_watch_ic_patching) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 377 | sampler_threshold_ = kSamplerThresholdInit; |
| 378 | sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit; |
| 379 | sampler_ticks_until_threshold_adjustment_ = |
| 380 | kSamplerTicksBetweenThresholdAdjustment; |
| 381 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
| 385 | void RuntimeProfiler::TearDown() { |
| 386 | // Nothing to do. |
| 387 | } |
| 388 | |
| 389 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 390 | int RuntimeProfiler::SamplerWindowSize() { |
| 391 | return kSamplerWindowSize; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 395 | // Update the pointers in the sampler window after a GC. |
| 396 | void RuntimeProfiler::UpdateSamplesAfterScavenge() { |
| 397 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 398 | Object* function = sampler_window_[i]; |
| 399 | if (function != NULL && isolate_->heap()->InNewSpace(function)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 400 | MapWord map_word = HeapObject::cast(function)->map_word(); |
| 401 | if (map_word.IsForwardingAddress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 402 | sampler_window_[i] = map_word.ToForwardingAddress(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 403 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 404 | sampler_window_[i] = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | void RuntimeProfiler::RemoveDeadSamples() { |
| 412 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 413 | Object* function = sampler_window_[i]; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 414 | if (function != NULL && |
| 415 | !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) { |
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 | void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
| 423 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 424 | visitor->VisitPointer(&sampler_window_[i]); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 425 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 429 | } } // namespace v8::internal |