blob: 2d4ee9c1a888254206aa602ffce735ea4468804c [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) {
61 Code* shared_code = shared->code();
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 Murdochb8a8cc12014-11-26 15:28:44 +000065 Object* raw_info = shared_code->type_feedback_info();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066 if (raw_info->IsTypeFeedbackInfo()) {
67 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
Ben Murdoch8f9999f2012-04-23 10:39:17 +010068 *ic_with_type_info_count = info->ic_with_type_info_count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 *ic_generic_count = info->ic_generic_count();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010070 *ic_total_count = info->ic_total_count();
71 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072
73 // Harvest vector-ics as well
74 TypeFeedbackVector* vector = shared->feedback_vector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 int with = 0, gen = 0;
76 vector->ComputeCounts(&with, &gen);
77 *ic_with_type_info_count += with;
78 *ic_generic_count += gen;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040079
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 if (*ic_total_count > 0) {
81 *type_info_percentage = 100 * *ic_with_type_info_count / *ic_total_count;
82 *generic_percentage = 100 * *ic_generic_count / *ic_total_count;
83 } else {
84 *type_info_percentage = 100; // Compared against lower bound.
85 *generic_percentage = 0; // Compared against upper bound.
86 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087}
88
89
90void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000092 PrintF("[marking ");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093 function->ShortPrint();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010094 PrintF(" for recompilation, reason: %s", reason);
95 if (FLAG_type_info_threshold > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 int typeinfo, generic, total, type_percentage, generic_percentage;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040097 GetICCounts(function->shared(), &typeinfo, &generic, &total,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 &type_percentage, &generic_percentage);
99 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total,
100 type_percentage);
101 PrintF(", generic ICs: %d/%d (%d%%)", generic, total, generic_percentage);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100102 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100103 PrintF("]\n");
104 }
105
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400106 function->AttemptConcurrentOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100107}
108
109
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function,
111 int loop_nesting_levels) {
112 SharedFunctionInfo* shared = function->shared();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 if (!FLAG_use_osr || function->shared()->IsBuiltin()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100114 return;
115 }
116
Ben Murdoch589d6972011-11-30 16:04:58 +0000117 // If the code is not optimizable, don't try OSR.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 if (shared->optimization_disabled()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119
120 // We are not prepared to do OSR for a function that already has an
121 // allocated arguments object. The optimized code would bypass it for
122 // arguments accesses, which is unsound. Don't try OSR.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 if (shared->uses_arguments()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100124
125 // We're using on-stack replacement: patch the unoptimized code so that
126 // any back edge in any unoptimized frame will trigger on-stack
127 // replacement for that frame.
128 if (FLAG_trace_osr) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 PrintF("[OSR - patching back edges in ");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100130 function->PrintName();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 PrintF("]\n");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 }
133
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134 for (int i = 0; i < loop_nesting_levels; i++) {
135 BackEdgeTable::Patch(isolate_, shared->code());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100136 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137}
138
139
140void RuntimeProfiler::OptimizeNow() {
Steve Block44f0eee2011-05-26 01:26:41 +0100141 HandleScope scope(isolate_);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 if (!isolate_->use_crankshaft()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144
145 DisallowHeapAllocation no_gc;
146
Ben Murdochb0fe1622011-05-05 13:52:32 +0100147 // Run through the JavaScript frames and collect them. If we already
148 // have a sample of the function, we mark it for optimizations
149 // (eagerly or lazily).
Ben Murdochb0fe1622011-05-05 13:52:32 +0100150 int frame_count = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 int frame_count_limit = FLAG_frame_count;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100152 for (JavaScriptFrameIterator it(isolate_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100153 frame_count++ < frame_count_limit && !it.done();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100154 it.Advance()) {
155 JavaScriptFrame* frame = it.frame();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 JSFunction* function = frame->function();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100157
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 SharedFunctionInfo* shared = function->shared();
159 Code* shared_code = shared->code();
160
161 List<JSFunction*> functions(4);
162 frame->GetFunctions(&functions);
163 for (int i = functions.length(); --i >= 0; ) {
164 SharedFunctionInfo* shared_function_info = functions[i]->shared();
165 int ticks = shared_function_info->profiler_ticks();
166 if (ticks < Smi::kMaxValue) {
167 shared_function_info->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100168 }
169 }
170
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100171 if (shared_code->kind() != Code::FUNCTION) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 if (function->IsInOptimizationQueue()) continue;
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100173
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 if (FLAG_always_osr) {
175 AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker);
176 // Fall through and do a normal optimized compile as well.
177 } else if (!frame->is_optimized() &&
178 (function->IsMarkedForOptimization() ||
179 function->IsMarkedForConcurrentOptimization() ||
180 function->IsOptimized())) {
181 // Attempt OSR if we are still running unoptimized code even though the
182 // the function has long been marked or even already been optimized.
183 int ticks = shared_code->profiler_ticks();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 int64_t allowance =
185 kOSRCodeSizeAllowanceBase +
186 static_cast<int64_t>(ticks) * kOSRCodeSizeAllowancePerTick;
187 if (shared_code->CodeSize() > allowance &&
188 ticks < Code::ProfilerTicksField::kMax) {
189 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190 } else {
191 AttemptOnStackReplacement(function);
192 }
193 continue;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100194 }
195
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196 // Only record top-level code on top of the execution stack and
197 // avoid optimizing excessively large scripts since top-level code
198 // will be executed only once.
199 const int kMaxToplevelSourceSize = 10 * 1024;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 if (shared->is_toplevel() &&
201 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100202 continue;
203 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100204
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 // Do not record non-optimizable functions.
206 if (shared->optimization_disabled()) {
207 if (shared->deopt_count() >= FLAG_max_opt_count) {
208 // If optimization was disabled due to many deoptimizations,
209 // then check if the function is hot and try to reenable optimization.
210 int ticks = shared_code->profiler_ticks();
211 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
212 shared_code->set_profiler_ticks(0);
213 shared->TryReenableOptimization();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100214 } else {
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100215 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100216 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000217 }
218 continue;
219 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220 if (function->IsOptimized()) continue;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221
222 int ticks = shared_code->profiler_ticks();
223
224 if (ticks >= kProfilerTicksBeforeOptimization) {
225 int typeinfo, generic, total, type_percentage, generic_percentage;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 &generic_percentage);
228 if (type_percentage >= FLAG_type_info_threshold &&
229 generic_percentage <= FLAG_generic_ic_threshold) {
230 // If this particular function hasn't had any ICs patched for enough
231 // ticks, optimize it now.
232 Optimize(function, "hot and stable");
233 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
234 Optimize(function, "not much type info but very hot");
235 } else {
236 shared_code->set_profiler_ticks(ticks + 1);
237 if (FLAG_trace_opt_verbose) {
238 PrintF("[not yet optimizing ");
239 function->PrintName();
240 PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
241 type_percentage);
242 }
243 }
244 } else if (!any_ic_changed_ &&
245 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
246 // If no IC was patched since the last tick and this function is very
247 // small, optimistically optimize it now.
248 int typeinfo, generic, total, type_percentage, generic_percentage;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400249 GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 &generic_percentage);
251 if (type_percentage >= FLAG_type_info_threshold &&
252 generic_percentage <= FLAG_generic_ic_threshold) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100253 Optimize(function, "small function");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100254 } else {
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100255 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100256 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 } else {
258 shared_code->set_profiler_ticks(ticks + 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100259 }
260 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 any_ic_changed_ = false;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100262}
263
264
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000265} // namespace internal
266} // namespace v8