blob: e17cbb1d6b2d28524421dc5293c155fc9a847fdd [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 Murdochb8a8cc12014-11-26 15:28:44 +000093 if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000094 PrintF("[marking ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095 function->ShortPrint();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010096 PrintF(" for recompilation, reason: %s", reason);
97 if (FLAG_type_info_threshold > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 int typeinfo, generic, total, type_percentage, generic_percentage;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040099 GetICCounts(function->shared(), &typeinfo, &generic, &total,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 &type_percentage, &generic_percentage);
101 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total,
102 type_percentage);
103 PrintF(", generic ICs: %d/%d (%d%%)", generic, total, generic_percentage);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100104 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100105 PrintF("]\n");
106 }
107
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400108 function->AttemptConcurrentOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109}
110
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function,
113 int loop_nesting_levels) {
114 SharedFunctionInfo* shared = function->shared();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 if (!FLAG_use_osr || function->shared()->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116 return;
117 }
118
Ben Murdoch589d6972011-11-30 16:04:58 +0000119 // If the code is not optimizable, don't try OSR.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000120 if (shared->optimization_disabled()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100121
122 // We are not prepared to do OSR for a function that already has an
123 // allocated arguments object. The optimized code would bypass it for
124 // arguments accesses, which is unsound. Don't try OSR.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000125 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100126
127 // We're using on-stack replacement: patch the unoptimized code so that
128 // any back edge in any unoptimized frame will trigger on-stack
129 // replacement for that frame.
130 if (FLAG_trace_osr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 PrintF("[OSR - patching back edges in ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 function->PrintName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 PrintF("]\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100134 }
135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 for (int i = 0; i < loop_nesting_levels; i++) {
137 BackEdgeTable::Patch(isolate_, shared->code());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100138 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100139}
140
Ben Murdoch097c5b22016-05-18 11:27:45 +0100141void RuntimeProfiler::MaybeOptimizeFullCodegen(JSFunction* function,
142 int frame_count,
143 bool frame_optimized) {
144 SharedFunctionInfo* shared = function->shared();
145 Code* shared_code = shared->code();
146 if (shared_code->kind() != Code::FUNCTION) return;
147 if (function->IsInOptimizationQueue()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100148
Ben Murdoch097c5b22016-05-18 11:27:45 +0100149 if (FLAG_always_osr) {
150 AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker);
151 // Fall through and do a normal optimized compile as well.
152 } else if (!frame_optimized &&
153 (function->IsMarkedForOptimization() ||
154 function->IsMarkedForConcurrentOptimization() ||
155 function->IsOptimized())) {
156 // Attempt OSR if we are still running unoptimized code even though the
157 // the function has long been marked or even already been optimized.
158 int ticks = shared_code->profiler_ticks();
159 int64_t allowance =
160 kOSRCodeSizeAllowanceBase +
161 static_cast<int64_t>(ticks) * kOSRCodeSizeAllowancePerTick;
162 if (shared_code->CodeSize() > allowance &&
163 ticks < Code::ProfilerTicksField::kMax) {
164 shared_code->set_profiler_ticks(ticks + 1);
165 } else {
166 AttemptOnStackReplacement(function);
167 }
168 return;
169 }
170
171 // Only record top-level code on top of the execution stack and
172 // avoid optimizing excessively large scripts since top-level code
173 // will be executed only once.
174 const int kMaxToplevelSourceSize = 10 * 1024;
175 if (shared->is_toplevel() &&
176 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
177 return;
178 }
179
180 // Do not record non-optimizable functions.
181 if (shared->optimization_disabled()) {
182 if (shared->deopt_count() >= FLAG_max_opt_count) {
183 // If optimization was disabled due to many deoptimizations,
184 // then check if the function is hot and try to reenable optimization.
185 int ticks = shared_code->profiler_ticks();
186 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
187 shared_code->set_profiler_ticks(0);
188 shared->TryReenableOptimization();
189 } else {
190 shared_code->set_profiler_ticks(ticks + 1);
191 }
192 }
193 return;
194 }
195 if (function->IsOptimized()) return;
196
197 int ticks = shared_code->profiler_ticks();
198
199 if (ticks >= kProfilerTicksBeforeOptimization) {
200 int typeinfo, generic, total, type_percentage, generic_percentage;
201 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
202 &generic_percentage);
203 if (type_percentage >= FLAG_type_info_threshold &&
204 generic_percentage <= FLAG_generic_ic_threshold) {
205 // If this particular function hasn't had any ICs patched for enough
206 // ticks, optimize it now.
207 Optimize(function, "hot and stable");
208 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
209 Optimize(function, "not much type info but very hot");
210 } else {
211 shared_code->set_profiler_ticks(ticks + 1);
212 if (FLAG_trace_opt_verbose) {
213 PrintF("[not yet optimizing ");
214 function->PrintName();
215 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
216 type_percentage);
217 }
218 }
219 } else if (!any_ic_changed_ &&
220 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
221 // If no IC was patched since the last tick and this function is very
222 // small, optimistically optimize it now.
223 int typeinfo, generic, total, type_percentage, generic_percentage;
224 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
225 &generic_percentage);
226 if (type_percentage >= FLAG_type_info_threshold &&
227 generic_percentage <= FLAG_generic_ic_threshold) {
228 Optimize(function, "small function");
229 } else {
230 shared_code->set_profiler_ticks(ticks + 1);
231 }
232 } else {
233 shared_code->set_profiler_ticks(ticks + 1);
234 }
235}
236
237void RuntimeProfiler::MaybeOptimizeIgnition(JSFunction* function,
238 bool frame_optimized) {
239 if (function->IsInOptimizationQueue()) return;
240
241 SharedFunctionInfo* shared = function->shared();
242 int ticks = shared->profiler_ticks();
243
244 // TODO(rmcilroy): Also ensure we only OSR top-level code if it is smaller
245 // than kMaxToplevelSourceSize.
246 // TODO(rmcilroy): Consider whether we should optimize small functions when
247 // they are first seen on the stack (e.g., kMaxSizeEarlyOpt).
248
249 if (!frame_optimized && (function->IsMarkedForOptimization() ||
250 function->IsMarkedForConcurrentOptimization() ||
251 function->IsOptimized())) {
252 // TODO(rmcilroy): Support OSR in these cases.
253
254 return;
255 }
256
257 // Do not optimize non-optimizable functions.
258 if (shared->optimization_disabled()) {
259 if (shared->deopt_count() >= FLAG_max_opt_count) {
260 // If optimization was disabled due to many deoptimizations,
261 // then check if the function is hot and try to reenable optimization.
262 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
263 shared->set_profiler_ticks(0);
264 shared->TryReenableOptimization();
265 }
266 }
267 return;
268 }
269
270 if (function->IsOptimized()) return;
271
272 if (ticks >= kProfilerTicksBeforeOptimization) {
273 int typeinfo, generic, total, type_percentage, generic_percentage;
274 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
275 &generic_percentage);
276 if (type_percentage >= FLAG_type_info_threshold &&
277 generic_percentage <= FLAG_generic_ic_threshold) {
278 // If this particular function hasn't had any ICs patched for enough
279 // ticks, optimize it now.
280 Optimize(function, "hot and stable");
281 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
282 Optimize(function, "not much type info but very hot");
283 } else {
284 if (FLAG_trace_opt_verbose) {
285 PrintF("[not yet optimizing ");
286 function->PrintName();
287 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
288 type_percentage);
289 }
290 }
291 }
292}
293
294void RuntimeProfiler::MarkCandidatesForOptimization() {
Steve Block44f0eee2011-05-26 01:26:41 +0100295 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100296
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 if (!isolate_->use_crankshaft()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298
299 DisallowHeapAllocation no_gc;
300
Ben Murdochb0fe1622011-05-05 13:52:32 +0100301 // Run through the JavaScript frames and collect them. If we already
302 // have a sample of the function, we mark it for optimizations
303 // (eagerly or lazily).
Ben Murdochb0fe1622011-05-05 13:52:32 +0100304 int frame_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 int frame_count_limit = FLAG_frame_count;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100306 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100307 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100308 it.Advance()) {
309 JavaScriptFrame* frame = it.frame();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 JSFunction* function = frame->function();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100311
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 List<JSFunction*> functions(4);
313 frame->GetFunctions(&functions);
314 for (int i = functions.length(); --i >= 0; ) {
315 SharedFunctionInfo* shared_function_info = functions[i]->shared();
316 int ticks = shared_function_info->profiler_ticks();
317 if (ticks < Smi::kMaxValue) {
318 shared_function_info->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100319 }
320 }
321
Ben Murdoch097c5b22016-05-18 11:27:45 +0100322 if (FLAG_ignition) {
323 MaybeOptimizeIgnition(function, frame->is_optimized());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100325 MaybeOptimizeFullCodegen(function, frame_count, frame->is_optimized());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326 }
327 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 any_ic_changed_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100329}
330
331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332} // namespace internal
333} // namespace v8