blob: 6500b9acb83f06065a08ff6171b050c5c8973c9d [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/runtime-profiler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/assembler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/ast/scopeinfo.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/platform/platform.h"
10#include "src/bootstrapper.h"
11#include "src/code-stubs.h"
12#include "src/compilation-cache.h"
13#include "src/execution.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/frames-inl.h"
15#include "src/full-codegen/full-codegen.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "src/global-handles.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010017
18namespace v8 {
19namespace internal {
20
21
Ben Murdoch3ef787d2012-04-12 10:51:47 +010022// Number of times a function has to be seen on the stack before it is
Ben Murdochc5610432016-08-08 18:44:38 +010023// compiled for baseline.
Ben Murdoch61f157c2016-09-16 13:49:30 +010024static const int kProfilerTicksBeforeBaseline = 1;
Ben Murdochc5610432016-08-08 18:44:38 +010025// Number of times a function has to be seen on the stack before it is
Ben Murdoch3ef787d2012-04-12 10:51:47 +010026// 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
Ben Murdoch61f157c2016-09-16 13:49:30 +010059static void GetICCounts(JSFunction* function, int* ic_with_type_info_count,
60 int* ic_generic_count, int* ic_total_count,
61 int* type_info_percentage, int* generic_percentage) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062 *ic_total_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 *ic_generic_count = 0;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010064 *ic_with_type_info_count = 0;
Ben Murdoch61f157c2016-09-16 13:49:30 +010065 if (function->code()->kind() == Code::FUNCTION) {
66 Code* shared_code = function->shared()->code();
Ben Murdoch097c5b22016-05-18 11:27:45 +010067 Object* raw_info = shared_code->type_feedback_info();
68 if (raw_info->IsTypeFeedbackInfo()) {
69 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
70 *ic_with_type_info_count = info->ic_with_type_info_count();
71 *ic_generic_count = info->ic_generic_count();
72 *ic_total_count = info->ic_total_count();
73 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010074 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040075
76 // Harvest vector-ics as well
Ben Murdoch61f157c2016-09-16 13:49:30 +010077 TypeFeedbackVector* vector = function->feedback_vector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 int with = 0, gen = 0;
79 vector->ComputeCounts(&with, &gen);
80 *ic_with_type_info_count += with;
81 *ic_generic_count += gen;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040082
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 if (*ic_total_count > 0) {
84 *type_info_percentage = 100 * *ic_with_type_info_count / *ic_total_count;
85 *generic_percentage = 100 * *ic_generic_count / *ic_total_count;
86 } else {
87 *type_info_percentage = 100; // Compared against lower bound.
88 *generic_percentage = 0; // Compared against upper bound.
89 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010090}
91
Ben Murdochc5610432016-08-08 18:44:38 +010092static void TraceRecompile(JSFunction* function, const char* reason,
93 const char* type) {
Ben Murdochda12d292016-06-02 14:46:10 +010094 if (FLAG_trace_opt &&
95 function->shared()->PassesFilter(FLAG_hydrogen_filter)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000096 PrintF("[marking ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 function->ShortPrint();
Ben Murdochc5610432016-08-08 18:44:38 +010098 PrintF(" for %s recompilation, reason: %s", type, reason);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010099 if (FLAG_type_info_threshold > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 int typeinfo, generic, total, type_percentage, generic_percentage;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100101 GetICCounts(function, &typeinfo, &generic, &total, &type_percentage,
102 &generic_percentage);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total,
104 type_percentage);
105 PrintF(", generic ICs: %d/%d (%d%%)", generic, total, generic_percentage);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100107 PrintF("]\n");
108 }
Ben Murdochc5610432016-08-08 18:44:38 +0100109}
Ben Murdochb0fe1622011-05-05 13:52:32 +0100110
Ben Murdochc5610432016-08-08 18:44:38 +0100111void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
112 TraceRecompile(function, reason, "optimized");
113
114 // TODO(4280): Fix this to check function is compiled to baseline once we
115 // have a standard way to check that. For now, if baseline code doesn't have
116 // a bytecode array.
117 DCHECK(!function->shared()->HasBytecodeArray());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118 function->AttemptConcurrentOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119}
120
Ben Murdochc5610432016-08-08 18:44:38 +0100121void RuntimeProfiler::Baseline(JSFunction* function, const char* reason) {
122 TraceRecompile(function, reason, "baseline");
123
124 // TODO(4280): Fix this to check function is compiled for the interpreter
125 // once we have a standard way to check that. For now function will only
126 // have a bytecode array if compiled for the interpreter.
127 DCHECK(function->shared()->HasBytecodeArray());
128 function->MarkForBaseline();
129}
Ben Murdochb0fe1622011-05-05 13:52:32 +0100130
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function,
132 int loop_nesting_levels) {
133 SharedFunctionInfo* shared = function->shared();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 if (!FLAG_use_osr || function->shared()->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100135 return;
136 }
137
Ben Murdoch589d6972011-11-30 16:04:58 +0000138 // If the code is not optimizable, don't try OSR.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 if (shared->optimization_disabled()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100140
141 // We are not prepared to do OSR for a function that already has an
142 // allocated arguments object. The optimized code would bypass it for
143 // arguments accesses, which is unsound. Don't try OSR.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000144 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100145
146 // We're using on-stack replacement: patch the unoptimized code so that
147 // any back edge in any unoptimized frame will trigger on-stack
148 // replacement for that frame.
149 if (FLAG_trace_osr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 PrintF("[OSR - patching back edges in ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100151 function->PrintName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 PrintF("]\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100153 }
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 for (int i = 0; i < loop_nesting_levels; i++) {
156 BackEdgeTable::Patch(isolate_, shared->code());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100157 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100158}
159
Ben Murdoch097c5b22016-05-18 11:27:45 +0100160void RuntimeProfiler::MaybeOptimizeFullCodegen(JSFunction* function,
161 int frame_count,
162 bool frame_optimized) {
163 SharedFunctionInfo* shared = function->shared();
164 Code* shared_code = shared->code();
165 if (shared_code->kind() != Code::FUNCTION) return;
166 if (function->IsInOptimizationQueue()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100167
Ben Murdoch097c5b22016-05-18 11:27:45 +0100168 if (FLAG_always_osr) {
169 AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker);
170 // Fall through and do a normal optimized compile as well.
171 } else if (!frame_optimized &&
172 (function->IsMarkedForOptimization() ||
173 function->IsMarkedForConcurrentOptimization() ||
174 function->IsOptimized())) {
175 // Attempt OSR if we are still running unoptimized code even though the
176 // the function has long been marked or even already been optimized.
177 int ticks = shared_code->profiler_ticks();
178 int64_t allowance =
179 kOSRCodeSizeAllowanceBase +
180 static_cast<int64_t>(ticks) * kOSRCodeSizeAllowancePerTick;
181 if (shared_code->CodeSize() > allowance &&
182 ticks < Code::ProfilerTicksField::kMax) {
183 shared_code->set_profiler_ticks(ticks + 1);
184 } else {
185 AttemptOnStackReplacement(function);
186 }
187 return;
188 }
189
190 // Only record top-level code on top of the execution stack and
191 // avoid optimizing excessively large scripts since top-level code
192 // will be executed only once.
193 const int kMaxToplevelSourceSize = 10 * 1024;
194 if (shared->is_toplevel() &&
195 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
196 return;
197 }
198
199 // Do not record non-optimizable functions.
200 if (shared->optimization_disabled()) {
201 if (shared->deopt_count() >= FLAG_max_opt_count) {
202 // If optimization was disabled due to many deoptimizations,
203 // then check if the function is hot and try to reenable optimization.
204 int ticks = shared_code->profiler_ticks();
205 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
206 shared_code->set_profiler_ticks(0);
207 shared->TryReenableOptimization();
208 } else {
209 shared_code->set_profiler_ticks(ticks + 1);
210 }
211 }
212 return;
213 }
214 if (function->IsOptimized()) return;
215
216 int ticks = shared_code->profiler_ticks();
217
218 if (ticks >= kProfilerTicksBeforeOptimization) {
219 int typeinfo, generic, total, type_percentage, generic_percentage;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100220 GetICCounts(function, &typeinfo, &generic, &total, &type_percentage,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100221 &generic_percentage);
222 if (type_percentage >= FLAG_type_info_threshold &&
223 generic_percentage <= FLAG_generic_ic_threshold) {
224 // If this particular function hasn't had any ICs patched for enough
225 // ticks, optimize it now.
226 Optimize(function, "hot and stable");
227 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
228 Optimize(function, "not much type info but very hot");
229 } else {
230 shared_code->set_profiler_ticks(ticks + 1);
231 if (FLAG_trace_opt_verbose) {
232 PrintF("[not yet optimizing ");
233 function->PrintName();
234 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
235 type_percentage);
236 }
237 }
238 } else if (!any_ic_changed_ &&
239 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
240 // If no IC was patched since the last tick and this function is very
241 // small, optimistically optimize it now.
242 int typeinfo, generic, total, type_percentage, generic_percentage;
Ben Murdoch61f157c2016-09-16 13:49:30 +0100243 GetICCounts(function, &typeinfo, &generic, &total, &type_percentage,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100244 &generic_percentage);
245 if (type_percentage >= FLAG_type_info_threshold &&
246 generic_percentage <= FLAG_generic_ic_threshold) {
247 Optimize(function, "small function");
248 } else {
249 shared_code->set_profiler_ticks(ticks + 1);
250 }
251 } else {
252 shared_code->set_profiler_ticks(ticks + 1);
253 }
254}
255
Ben Murdochc5610432016-08-08 18:44:38 +0100256void RuntimeProfiler::MaybeOptimizeIgnition(JSFunction* function) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100257 if (function->IsInOptimizationQueue()) return;
258
259 SharedFunctionInfo* shared = function->shared();
260 int ticks = shared->profiler_ticks();
261
262 // TODO(rmcilroy): Also ensure we only OSR top-level code if it is smaller
263 // than kMaxToplevelSourceSize.
264 // TODO(rmcilroy): Consider whether we should optimize small functions when
265 // they are first seen on the stack (e.g., kMaxSizeEarlyOpt).
266
Ben Murdochc5610432016-08-08 18:44:38 +0100267 if (function->IsMarkedForBaseline() || function->IsMarkedForOptimization() ||
268 function->IsMarkedForConcurrentOptimization() ||
269 function->IsOptimized()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100270 // TODO(rmcilroy): Support OSR in these cases.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100271 return;
272 }
273
Ben Murdochc5610432016-08-08 18:44:38 +0100274 if (shared->optimization_disabled() &&
275 shared->disable_optimization_reason() == kOptimizationDisabledForTest) {
276 // Don't baseline functions which have been marked by NeverOptimizeFunction
277 // in a test.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100278 return;
279 }
280
Ben Murdochc5610432016-08-08 18:44:38 +0100281 if (ticks >= kProfilerTicksBeforeBaseline) {
282 Baseline(function, "hot enough for baseline");
Ben Murdoch097c5b22016-05-18 11:27:45 +0100283 }
284}
285
286void RuntimeProfiler::MarkCandidatesForOptimization() {
Steve Block44f0eee2011-05-26 01:26:41 +0100287 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289 if (!isolate_->use_crankshaft()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290
291 DisallowHeapAllocation no_gc;
292
Ben Murdochb0fe1622011-05-05 13:52:32 +0100293 // Run through the JavaScript frames and collect them. If we already
294 // have a sample of the function, we mark it for optimizations
295 // (eagerly or lazily).
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296 int frame_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297 int frame_count_limit = FLAG_frame_count;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100298 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100299 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100300 it.Advance()) {
301 JavaScriptFrame* frame = it.frame();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 JSFunction* function = frame->function();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100303
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 List<JSFunction*> functions(4);
305 frame->GetFunctions(&functions);
306 for (int i = functions.length(); --i >= 0; ) {
307 SharedFunctionInfo* shared_function_info = functions[i]->shared();
308 int ticks = shared_function_info->profiler_ticks();
309 if (ticks < Smi::kMaxValue) {
310 shared_function_info->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100311 }
312 }
313
Ben Murdochc5610432016-08-08 18:44:38 +0100314 if (frame->is_interpreted()) {
315 DCHECK(!frame->is_optimized());
316 MaybeOptimizeIgnition(function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100318 MaybeOptimizeFullCodegen(function, frame_count, frame->is_optimized());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100319 }
320 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 any_ic_changed_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100322}
323
324
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000325} // namespace internal
326} // namespace v8