blob: b76785deeb7c7b67a05b0fdae8e3228d77a47e8e [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
23// optimized.
24static const int kProfilerTicksBeforeOptimization = 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025// If the function optimization was disabled due to high deoptimization count,
26// but the function is hot and has been seen on the stack this number of times,
27// then we try to reenable optimization for this function.
28static const int kProfilerTicksBeforeReenablingOptimization = 250;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010029// If a function does not have enough type info (according to
30// FLAG_type_info_threshold), but has seen a huge number of ticks,
31// optimize it as it is.
32static const int kTicksWhenNotEnoughTypeInfo = 100;
33// We only have one byte to store the number of ticks.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
35STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010036STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038// Maximum size in bytes of generate code for a function to allow OSR.
39static const int kOSRCodeSizeAllowanceBase =
40 100 * FullCodeGenerator::kCodeSizeMultiplier;
41
42static const int kOSRCodeSizeAllowancePerTick =
43 4 * FullCodeGenerator::kCodeSizeMultiplier;
44
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045// Maximum size in bytes of generated code for a function to be optimized
46// the very first time it is seen on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047static const int kMaxSizeEarlyOpt =
48 5 * FullCodeGenerator::kCodeSizeMultiplier;
Ben Murdoch8b112d22011-06-08 16:22:53 +010049
Steve Block44f0eee2011-05-26 01:26:41 +010050
51RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
52 : isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 any_ic_changed_(false) {
Steve Block44f0eee2011-05-26 01:26:41 +010054}
55
56
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057static void GetICCounts(SharedFunctionInfo* shared,
58 int* ic_with_type_info_count, int* ic_generic_count,
59 int* ic_total_count, int* type_info_percentage,
60 int* generic_percentage) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010061 *ic_total_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062 *ic_generic_count = 0;
Ben Murdoch8f9999f2012-04-23 10:39:17 +010063 *ic_with_type_info_count = 0;
Ben Murdoch097c5b22016-05-18 11:27:45 +010064 if (shared->code()->kind() == Code::FUNCTION) {
65 Code* shared_code = shared->code();
66 Object* raw_info = shared_code->type_feedback_info();
67 if (raw_info->IsTypeFeedbackInfo()) {
68 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
69 *ic_with_type_info_count = info->ic_with_type_info_count();
70 *ic_generic_count = info->ic_generic_count();
71 *ic_total_count = info->ic_total_count();
72 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010073 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040074
75 // Harvest vector-ics as well
76 TypeFeedbackVector* vector = shared->feedback_vector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 int with = 0, gen = 0;
78 vector->ComputeCounts(&with, &gen);
79 *ic_with_type_info_count += with;
80 *ic_generic_count += gen;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040081
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 if (*ic_total_count > 0) {
83 *type_info_percentage = 100 * *ic_with_type_info_count / *ic_total_count;
84 *generic_percentage = 100 * *ic_generic_count / *ic_total_count;
85 } else {
86 *type_info_percentage = 100; // Compared against lower bound.
87 *generic_percentage = 0; // Compared against upper bound.
88 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010089}
90
91
92void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
Ben Murdochda12d292016-06-02 14:46:10 +010093 if (FLAG_trace_opt &&
94 function->shared()->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 Murdoch4a90d5f2016-03-22 12:00:34 +0000116 if (!FLAG_use_osr || function->shared()->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100117 return;
118 }
119
Ben Murdoch589d6972011-11-30 16:04:58 +0000120 // If the code is not optimizable, don't try OSR.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121 if (shared->optimization_disabled()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100122
123 // We are not prepared to do OSR for a function that already has an
124 // allocated arguments object. The optimized code would bypass it for
125 // arguments accesses, which is unsound. Don't try OSR.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000126 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100127
128 // We're using on-stack replacement: patch the unoptimized code so that
129 // any back edge in any unoptimized frame will trigger on-stack
130 // replacement for that frame.
131 if (FLAG_trace_osr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 PrintF("[OSR - patching back edges in ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100133 function->PrintName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 PrintF("]\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100135 }
136
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 for (int i = 0; i < loop_nesting_levels; i++) {
138 BackEdgeTable::Patch(isolate_, shared->code());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100139 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100140}
141
Ben Murdoch097c5b22016-05-18 11:27:45 +0100142void RuntimeProfiler::MaybeOptimizeFullCodegen(JSFunction* function,
143 int frame_count,
144 bool frame_optimized) {
145 SharedFunctionInfo* shared = function->shared();
146 Code* shared_code = shared->code();
147 if (shared_code->kind() != Code::FUNCTION) return;
148 if (function->IsInOptimizationQueue()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100149
Ben Murdoch097c5b22016-05-18 11:27:45 +0100150 if (FLAG_always_osr) {
151 AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker);
152 // Fall through and do a normal optimized compile as well.
153 } else if (!frame_optimized &&
154 (function->IsMarkedForOptimization() ||
155 function->IsMarkedForConcurrentOptimization() ||
156 function->IsOptimized())) {
157 // Attempt OSR if we are still running unoptimized code even though the
158 // the function has long been marked or even already been optimized.
159 int ticks = shared_code->profiler_ticks();
160 int64_t allowance =
161 kOSRCodeSizeAllowanceBase +
162 static_cast<int64_t>(ticks) * kOSRCodeSizeAllowancePerTick;
163 if (shared_code->CodeSize() > allowance &&
164 ticks < Code::ProfilerTicksField::kMax) {
165 shared_code->set_profiler_ticks(ticks + 1);
166 } else {
167 AttemptOnStackReplacement(function);
168 }
169 return;
170 }
171
172 // Only record top-level code on top of the execution stack and
173 // avoid optimizing excessively large scripts since top-level code
174 // will be executed only once.
175 const int kMaxToplevelSourceSize = 10 * 1024;
176 if (shared->is_toplevel() &&
177 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
178 return;
179 }
180
181 // Do not record non-optimizable functions.
182 if (shared->optimization_disabled()) {
183 if (shared->deopt_count() >= FLAG_max_opt_count) {
184 // If optimization was disabled due to many deoptimizations,
185 // then check if the function is hot and try to reenable optimization.
186 int ticks = shared_code->profiler_ticks();
187 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
188 shared_code->set_profiler_ticks(0);
189 shared->TryReenableOptimization();
190 } else {
191 shared_code->set_profiler_ticks(ticks + 1);
192 }
193 }
194 return;
195 }
196 if (function->IsOptimized()) return;
197
198 int ticks = shared_code->profiler_ticks();
199
200 if (ticks >= kProfilerTicksBeforeOptimization) {
201 int typeinfo, generic, total, type_percentage, generic_percentage;
202 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
203 &generic_percentage);
204 if (type_percentage >= FLAG_type_info_threshold &&
205 generic_percentage <= FLAG_generic_ic_threshold) {
206 // If this particular function hasn't had any ICs patched for enough
207 // ticks, optimize it now.
208 Optimize(function, "hot and stable");
209 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
210 Optimize(function, "not much type info but very hot");
211 } else {
212 shared_code->set_profiler_ticks(ticks + 1);
213 if (FLAG_trace_opt_verbose) {
214 PrintF("[not yet optimizing ");
215 function->PrintName();
216 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
217 type_percentage);
218 }
219 }
220 } else if (!any_ic_changed_ &&
221 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
222 // If no IC was patched since the last tick and this function is very
223 // small, optimistically optimize it now.
224 int typeinfo, generic, total, type_percentage, generic_percentage;
225 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
226 &generic_percentage);
227 if (type_percentage >= FLAG_type_info_threshold &&
228 generic_percentage <= FLAG_generic_ic_threshold) {
229 Optimize(function, "small function");
230 } else {
231 shared_code->set_profiler_ticks(ticks + 1);
232 }
233 } else {
234 shared_code->set_profiler_ticks(ticks + 1);
235 }
236}
237
238void RuntimeProfiler::MaybeOptimizeIgnition(JSFunction* function,
239 bool frame_optimized) {
240 if (function->IsInOptimizationQueue()) return;
241
242 SharedFunctionInfo* shared = function->shared();
243 int ticks = shared->profiler_ticks();
244
245 // TODO(rmcilroy): Also ensure we only OSR top-level code if it is smaller
246 // than kMaxToplevelSourceSize.
247 // TODO(rmcilroy): Consider whether we should optimize small functions when
248 // they are first seen on the stack (e.g., kMaxSizeEarlyOpt).
249
250 if (!frame_optimized && (function->IsMarkedForOptimization() ||
251 function->IsMarkedForConcurrentOptimization() ||
252 function->IsOptimized())) {
253 // TODO(rmcilroy): Support OSR in these cases.
254
255 return;
256 }
257
258 // Do not optimize non-optimizable functions.
259 if (shared->optimization_disabled()) {
260 if (shared->deopt_count() >= FLAG_max_opt_count) {
261 // If optimization was disabled due to many deoptimizations,
262 // then check if the function is hot and try to reenable optimization.
263 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
264 shared->set_profiler_ticks(0);
265 shared->TryReenableOptimization();
266 }
267 }
268 return;
269 }
270
271 if (function->IsOptimized()) return;
272
273 if (ticks >= kProfilerTicksBeforeOptimization) {
274 int typeinfo, generic, total, type_percentage, generic_percentage;
275 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
276 &generic_percentage);
277 if (type_percentage >= FLAG_type_info_threshold &&
278 generic_percentage <= FLAG_generic_ic_threshold) {
279 // If this particular function hasn't had any ICs patched for enough
280 // ticks, optimize it now.
281 Optimize(function, "hot and stable");
282 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
283 Optimize(function, "not much type info but very hot");
284 } else {
285 if (FLAG_trace_opt_verbose) {
286 PrintF("[not yet optimizing ");
287 function->PrintName();
288 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
289 type_percentage);
290 }
291 }
292 }
293}
294
295void RuntimeProfiler::MarkCandidatesForOptimization() {
Steve Block44f0eee2011-05-26 01:26:41 +0100296 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100297
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 if (!isolate_->use_crankshaft()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299
300 DisallowHeapAllocation no_gc;
301
Ben Murdochb0fe1622011-05-05 13:52:32 +0100302 // Run through the JavaScript frames and collect them. If we already
303 // have a sample of the function, we mark it for optimizations
304 // (eagerly or lazily).
Ben Murdochb0fe1622011-05-05 13:52:32 +0100305 int frame_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 int frame_count_limit = FLAG_frame_count;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100307 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100308 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100309 it.Advance()) {
310 JavaScriptFrame* frame = it.frame();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 JSFunction* function = frame->function();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100312
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 List<JSFunction*> functions(4);
314 frame->GetFunctions(&functions);
315 for (int i = functions.length(); --i >= 0; ) {
316 SharedFunctionInfo* shared_function_info = functions[i]->shared();
317 int ticks = shared_function_info->profiler_ticks();
318 if (ticks < Smi::kMaxValue) {
319 shared_function_info->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100320 }
321 }
322
Ben Murdoch097c5b22016-05-18 11:27:45 +0100323 if (FLAG_ignition) {
324 MaybeOptimizeIgnition(function, frame->is_optimized());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100326 MaybeOptimizeFullCodegen(function, frame_count, frame->is_optimized());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100327 }
328 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 any_ic_changed_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330}
331
332
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000333} // namespace internal
334} // namespace v8