Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/runtime-profiler.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/assembler.h" |
| 10 | #include "src/base/platform/platform.h" |
| 11 | #include "src/bootstrapper.h" |
| 12 | #include "src/code-stubs.h" |
| 13 | #include "src/compilation-cache.h" |
| 14 | #include "src/execution.h" |
| 15 | #include "src/full-codegen.h" |
| 16 | #include "src/global-handles.h" |
| 17 | #include "src/heap/mark-compact.h" |
| 18 | #include "src/isolate-inl.h" |
| 19 | #include "src/scopeinfo.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 20 | |
| 21 | namespace v8 { |
| 22 | namespace internal { |
| 23 | |
| 24 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 25 | // Number of times a function has to be seen on the stack before it is |
| 26 | // optimized. |
| 27 | static const int kProfilerTicksBeforeOptimization = 2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | // If the function optimization was disabled due to high deoptimization count, |
| 29 | // but the function is hot and has been seen on the stack this number of times, |
| 30 | // then we try to reenable optimization for this function. |
| 31 | static const int kProfilerTicksBeforeReenablingOptimization = 250; |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 32 | // If a function does not have enough type info (according to |
| 33 | // FLAG_type_info_threshold), but has seen a huge number of ticks, |
| 34 | // optimize it as it is. |
| 35 | static const int kTicksWhenNotEnoughTypeInfo = 100; |
| 36 | // We only have one byte to store the number of ticks. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 37 | STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256); |
| 38 | STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 39 | STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 40 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | // Maximum size in bytes of generate code for a function to allow OSR. |
| 42 | static const int kOSRCodeSizeAllowanceBase = |
| 43 | 100 * FullCodeGenerator::kCodeSizeMultiplier; |
| 44 | |
| 45 | static const int kOSRCodeSizeAllowancePerTick = |
| 46 | 4 * FullCodeGenerator::kCodeSizeMultiplier; |
| 47 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 48 | // Maximum size in bytes of generated code for a function to be optimized |
| 49 | // the very first time it is seen on the stack. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | static const int kMaxSizeEarlyOpt = |
| 51 | 5 * FullCodeGenerator::kCodeSizeMultiplier; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 52 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 53 | |
| 54 | RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
| 55 | : isolate_(isolate), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | any_ic_changed_(false) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 60 | static void GetICCounts(SharedFunctionInfo* shared, |
| 61 | int* ic_with_type_info_count, int* ic_generic_count, |
| 62 | int* ic_total_count, int* type_info_percentage, |
| 63 | int* generic_percentage) { |
| 64 | Code* shared_code = shared->code(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 65 | *ic_total_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | *ic_generic_count = 0; |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 67 | *ic_with_type_info_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | Object* raw_info = shared_code->type_feedback_info(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 69 | if (raw_info->IsTypeFeedbackInfo()) { |
| 70 | TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 71 | *ic_with_type_info_count = info->ic_with_type_info_count(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 72 | *ic_generic_count = info->ic_generic_count(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 73 | *ic_total_count = info->ic_total_count(); |
| 74 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 75 | |
| 76 | // Harvest vector-ics as well |
| 77 | TypeFeedbackVector* vector = shared->feedback_vector(); |
| 78 | *ic_with_type_info_count += vector->ic_with_type_info_count(); |
| 79 | *ic_generic_count += vector->ic_generic_count(); |
| 80 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 81 | if (*ic_total_count > 0) { |
| 82 | *type_info_percentage = 100 * *ic_with_type_info_count / *ic_total_count; |
| 83 | *generic_percentage = 100 * *ic_generic_count / *ic_total_count; |
| 84 | } else { |
| 85 | *type_info_percentage = 100; // Compared against lower bound. |
| 86 | *generic_percentage = 0; // Compared against upper bound. |
| 87 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
| 91 | void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | DCHECK(function->IsOptimizable()); |
| 93 | |
| 94 | if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 95 | PrintF("[marking "); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 96 | function->ShortPrint(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 97 | PrintF(" for recompilation, reason: %s", reason); |
| 98 | if (FLAG_type_info_threshold > 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 99 | int typeinfo, generic, total, type_percentage, generic_percentage; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 100 | GetICCounts(function->shared(), &typeinfo, &generic, &total, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 101 | &type_percentage, &generic_percentage); |
| 102 | PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, |
| 103 | type_percentage); |
| 104 | PrintF(", generic ICs: %d/%d (%d%%)", generic, total, generic_percentage); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 105 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 106 | PrintF("]\n"); |
| 107 | } |
| 108 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 109 | function->AttemptConcurrentOptimization(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 113 | void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function, |
| 114 | int loop_nesting_levels) { |
| 115 | SharedFunctionInfo* shared = function->shared(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 116 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 117 | // Debug::has_break_points(). |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | if (!FLAG_use_osr || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 119 | isolate_->DebuggerHasBreakPoints() || |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 120 | function->IsBuiltin()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 124 | // If the code is not optimizable, don't try OSR. |
| 125 | if (!shared->code()->optimizable()) return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 126 | |
| 127 | // We are not prepared to do OSR for a function that already has an |
| 128 | // allocated arguments object. The optimized code would bypass it for |
| 129 | // arguments accesses, which is unsound. Don't try OSR. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 130 | if (shared->uses_arguments()) return; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 131 | |
| 132 | // We're using on-stack replacement: patch the unoptimized code so that |
| 133 | // any back edge in any unoptimized frame will trigger on-stack |
| 134 | // replacement for that frame. |
| 135 | if (FLAG_trace_osr) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 136 | PrintF("[OSR - patching back edges in "); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 137 | function->PrintName(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 138 | PrintF("]\n"); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | for (int i = 0; i < loop_nesting_levels; i++) { |
| 142 | BackEdgeTable::Patch(isolate_, shared->code()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 143 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | void RuntimeProfiler::OptimizeNow() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 148 | HandleScope scope(isolate_); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 149 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 150 | if (!isolate_->use_crankshaft() || isolate_->DebuggerHasBreakPoints()) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | |
| 152 | DisallowHeapAllocation no_gc; |
| 153 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 154 | // Run through the JavaScript frames and collect them. If we already |
| 155 | // have a sample of the function, we mark it for optimizations |
| 156 | // (eagerly or lazily). |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 157 | int frame_count = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | int frame_count_limit = FLAG_frame_count; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 159 | for (JavaScriptFrameIterator it(isolate_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 160 | frame_count++ < frame_count_limit && !it.done(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 161 | it.Advance()) { |
| 162 | JavaScriptFrame* frame = it.frame(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 163 | JSFunction* function = frame->function(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 164 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 165 | SharedFunctionInfo* shared = function->shared(); |
| 166 | Code* shared_code = shared->code(); |
| 167 | |
| 168 | List<JSFunction*> functions(4); |
| 169 | frame->GetFunctions(&functions); |
| 170 | for (int i = functions.length(); --i >= 0; ) { |
| 171 | SharedFunctionInfo* shared_function_info = functions[i]->shared(); |
| 172 | int ticks = shared_function_info->profiler_ticks(); |
| 173 | if (ticks < Smi::kMaxValue) { |
| 174 | shared_function_info->set_profiler_ticks(ticks + 1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 178 | if (shared_code->kind() != Code::FUNCTION) continue; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | if (function->IsInOptimizationQueue()) continue; |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | if (FLAG_always_osr) { |
| 182 | AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker); |
| 183 | // Fall through and do a normal optimized compile as well. |
| 184 | } else if (!frame->is_optimized() && |
| 185 | (function->IsMarkedForOptimization() || |
| 186 | function->IsMarkedForConcurrentOptimization() || |
| 187 | function->IsOptimized())) { |
| 188 | // Attempt OSR if we are still running unoptimized code even though the |
| 189 | // the function has long been marked or even already been optimized. |
| 190 | int ticks = shared_code->profiler_ticks(); |
| 191 | int allowance = kOSRCodeSizeAllowanceBase + |
| 192 | ticks * kOSRCodeSizeAllowancePerTick; |
| 193 | if (shared_code->CodeSize() > allowance) { |
| 194 | if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1); |
| 195 | } else { |
| 196 | AttemptOnStackReplacement(function); |
| 197 | } |
| 198 | continue; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 201 | // Only record top-level code on top of the execution stack and |
| 202 | // avoid optimizing excessively large scripts since top-level code |
| 203 | // will be executed only once. |
| 204 | const int kMaxToplevelSourceSize = 10 * 1024; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 205 | if (shared->is_toplevel() && |
| 206 | (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 207 | continue; |
| 208 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 209 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 210 | // Do not record non-optimizable functions. |
| 211 | if (shared->optimization_disabled()) { |
| 212 | if (shared->deopt_count() >= FLAG_max_opt_count) { |
| 213 | // If optimization was disabled due to many deoptimizations, |
| 214 | // then check if the function is hot and try to reenable optimization. |
| 215 | int ticks = shared_code->profiler_ticks(); |
| 216 | if (ticks >= kProfilerTicksBeforeReenablingOptimization) { |
| 217 | shared_code->set_profiler_ticks(0); |
| 218 | shared->TryReenableOptimization(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 219 | } else { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 220 | shared_code->set_profiler_ticks(ticks + 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 221 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 222 | } |
| 223 | continue; |
| 224 | } |
| 225 | if (!function->IsOptimizable()) continue; |
| 226 | |
| 227 | int ticks = shared_code->profiler_ticks(); |
| 228 | |
| 229 | if (ticks >= kProfilerTicksBeforeOptimization) { |
| 230 | int typeinfo, generic, total, type_percentage, generic_percentage; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 231 | GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 232 | &generic_percentage); |
| 233 | if (type_percentage >= FLAG_type_info_threshold && |
| 234 | generic_percentage <= FLAG_generic_ic_threshold) { |
| 235 | // If this particular function hasn't had any ICs patched for enough |
| 236 | // ticks, optimize it now. |
| 237 | Optimize(function, "hot and stable"); |
| 238 | } else if (ticks >= kTicksWhenNotEnoughTypeInfo) { |
| 239 | Optimize(function, "not much type info but very hot"); |
| 240 | } else { |
| 241 | shared_code->set_profiler_ticks(ticks + 1); |
| 242 | if (FLAG_trace_opt_verbose) { |
| 243 | PrintF("[not yet optimizing "); |
| 244 | function->PrintName(); |
| 245 | PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total, |
| 246 | type_percentage); |
| 247 | } |
| 248 | } |
| 249 | } else if (!any_ic_changed_ && |
| 250 | shared_code->instruction_size() < kMaxSizeEarlyOpt) { |
| 251 | // If no IC was patched since the last tick and this function is very |
| 252 | // small, optimistically optimize it now. |
| 253 | int typeinfo, generic, total, type_percentage, generic_percentage; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 254 | GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 255 | &generic_percentage); |
| 256 | if (type_percentage >= FLAG_type_info_threshold && |
| 257 | generic_percentage <= FLAG_generic_ic_threshold) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 258 | Optimize(function, "small function"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 259 | } else { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 260 | shared_code->set_profiler_ticks(ticks + 1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 261 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 262 | } else { |
| 263 | shared_code->set_profiler_ticks(ticks + 1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 266 | any_ic_changed_ = false; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | } } // namespace v8::internal |