blob: 6c997145937043e9088c3e4f19c44fd9ba85ab57 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/runtime-profiler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#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 Murdochb0fe1622011-05-05 13:52:32 +010020
21namespace v8 {
22namespace internal {
23
24
Ben Murdoch3ef787d2012-04-12 10:51:47 +010025// Number of times a function has to be seen on the stack before it is
26// optimized.
27static const int kProfilerTicksBeforeOptimization = 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028// 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.
31static const int kProfilerTicksBeforeReenablingOptimization = 250;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010032// 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.
35static const int kTicksWhenNotEnoughTypeInfo = 100;
36// We only have one byte to store the number of ticks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
38STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010039STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010040
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041// Maximum size in bytes of generate code for a function to allow OSR.
42static const int kOSRCodeSizeAllowanceBase =
43 100 * FullCodeGenerator::kCodeSizeMultiplier;
44
45static const int kOSRCodeSizeAllowancePerTick =
46 4 * FullCodeGenerator::kCodeSizeMultiplier;
47
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048// 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 Murdochb8a8cc12014-11-26 15:28:44 +000050static const int kMaxSizeEarlyOpt =
51 5 * FullCodeGenerator::kCodeSizeMultiplier;
Ben Murdoch8b112d22011-06-08 16:22:53 +010052
Steve Block44f0eee2011-05-26 01:26:41 +010053
54RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
55 : isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 any_ic_changed_(false) {
Steve Block44f0eee2011-05-26 01:26:41 +010057}
58
59
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060static 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 Murdoch3ef787d2012-04-12 10:51:47 +010065 *ic_total_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 *ic_generic_count = 0;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010067 *ic_with_type_info_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 Object* raw_info = shared_code->type_feedback_info();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010069 if (raw_info->IsTypeFeedbackInfo()) {
70 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010071 *ic_with_type_info_count = info->ic_with_type_info_count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 *ic_generic_count = info->ic_generic_count();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010073 *ic_total_count = info->ic_total_count();
74 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040075
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 Murdochb8a8cc12014-11-26 15:28:44 +000081 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 Murdoch3ef787d2012-04-12 10:51:47 +010088}
89
90
91void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 DCHECK(function->IsOptimizable());
93
94 if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000095 PrintF("[marking ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 function->ShortPrint();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010097 PrintF(" for recompilation, reason: %s", reason);
98 if (FLAG_type_info_threshold > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 int typeinfo, generic, total, type_percentage, generic_percentage;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400100 GetICCounts(function->shared(), &typeinfo, &generic, &total,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 &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 Murdoch3ef787d2012-04-12 10:51:47 +0100105 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100106 PrintF("]\n");
107 }
108
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400109 function->AttemptConcurrentOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100110}
111
112
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function,
114 int loop_nesting_levels) {
115 SharedFunctionInfo* shared = function->shared();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
117 // Debug::has_break_points().
Steve Block44f0eee2011-05-26 01:26:41 +0100118 if (!FLAG_use_osr ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000119 isolate_->DebuggerHasBreakPoints() ||
Steve Block44f0eee2011-05-26 01:26:41 +0100120 function->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100121 return;
122 }
123
Ben Murdoch589d6972011-11-30 16:04:58 +0000124 // If the code is not optimizable, don't try OSR.
125 if (!shared->code()->optimizable()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100126
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 Murdoch3fb3ca82011-12-02 17:19:32 +0000130 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100131
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 Murdochb8a8cc12014-11-26 15:28:44 +0000136 PrintF("[OSR - patching back edges in ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 function->PrintName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 PrintF("]\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100139 }
140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 for (int i = 0; i < loop_nesting_levels; i++) {
142 BackEdgeTable::Patch(isolate_, shared->code());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100144}
145
146
147void RuntimeProfiler::OptimizeNow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100148 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100149
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400150 if (!isolate_->use_crankshaft() || isolate_->DebuggerHasBreakPoints()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151
152 DisallowHeapAllocation no_gc;
153
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100157 int frame_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 int frame_count_limit = FLAG_frame_count;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100159 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100160 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100161 it.Advance()) {
162 JavaScriptFrame* frame = it.frame();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 JSFunction* function = frame->function();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100164
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 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 Murdochb0fe1622011-05-05 13:52:32 +0100175 }
176 }
177
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100178 if (shared_code->kind() != Code::FUNCTION) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 if (function->IsInOptimizationQueue()) continue;
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100180
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 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 Murdochb0fe1622011-05-05 13:52:32 +0100199 }
200
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100201 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000205 if (shared->is_toplevel() &&
206 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100207 continue;
208 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 // 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 Murdoch3ef787d2012-04-12 10:51:47 +0100219 } else {
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100220 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100221 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222 }
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 Bernierd0a1eb72015-03-24 16:35:39 -0400231 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 &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 Bernierd0a1eb72015-03-24 16:35:39 -0400254 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 &generic_percentage);
256 if (type_percentage >= FLAG_type_info_threshold &&
257 generic_percentage <= FLAG_generic_ic_threshold) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258 Optimize(function, "small function");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100259 } else {
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100260 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100261 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 } else {
263 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 }
265 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266 any_ic_changed_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100267}
268
269
270} } // namespace v8::internal