blob: 5784e4d5aaad5f682317ce8e20cb9dbc4a684a22 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "runtime-profiler.h"
31
32#include "assembler.h"
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +000033#include "bootstrapper.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000034#include "code-stubs.h"
35#include "compilation-cache.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000036#include "execution.h"
danno@chromium.org129d3982012-07-25 15:01:47 +000037#include "full-codegen.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "global-handles.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000039#include "isolate-inl.h"
ager@chromium.org9ee27ae2011-03-02 13:43:26 +000040#include "mark-compact.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000041#include "platform.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000042#include "scopeinfo.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043
44namespace v8 {
45namespace internal {
46
47
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000048// Number of times a function has to be seen on the stack before it is
49// optimized.
50static const int kProfilerTicksBeforeOptimization = 2;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000051// If the function optimization was disabled due to high deoptimization count,
52// but the function is hot and has been seen on the stack this number of times,
53// then we try to reenable optimization for this function.
54static const int kProfilerTicksBeforeReenablingOptimization = 250;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000055// If a function does not have enough type info (according to
56// FLAG_type_info_threshold), but has seen a huge number of ticks,
57// optimize it as it is.
58static const int kTicksWhenNotEnoughTypeInfo = 100;
59// We only have one byte to store the number of ticks.
mmassi@chromium.org7028c052012-06-13 11:51:58 +000060STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
61STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000062STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000063
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000064// Maximum size in bytes of generate code for a function to allow OSR.
65static const int kOSRCodeSizeAllowanceBase =
66 100 * FullCodeGenerator::kCodeSizeMultiplier;
67
68static const int kOSRCodeSizeAllowancePerTick =
hpayer@chromium.org4f99be92013-12-18 16:23:55 +000069 4 * FullCodeGenerator::kCodeSizeMultiplier;
mmassi@chromium.org7028c052012-06-13 11:51:58 +000070
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000071// Maximum size in bytes of generated code for a function to be optimized
72// the very first time it is seen on the stack.
danno@chromium.org129d3982012-07-25 15:01:47 +000073static const int kMaxSizeEarlyOpt =
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000074 5 * FullCodeGenerator::kCodeSizeMultiplier;
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +000075
kasperl@chromium.orga5551262010-12-07 12:49:48 +000076
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000077RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
78 : isolate_(isolate),
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +000079 any_ic_changed_(false) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000080}
81
82
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000083static void GetICCounts(Code* shared_code,
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000084 int* ic_with_type_info_count,
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000085 int* ic_total_count,
86 int* percentage) {
87 *ic_total_count = 0;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000088 *ic_with_type_info_count = 0;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000089 Object* raw_info = shared_code->type_feedback_info();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000090 if (raw_info->IsTypeFeedbackInfo()) {
91 TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000092 *ic_with_type_info_count = info->ic_with_type_info_count();
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000093 *ic_total_count = info->ic_total_count();
94 }
95 *percentage = *ic_total_count > 0
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000096 ? 100 * *ic_with_type_info_count / *ic_total_count
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +000097 : 100;
98}
99
100
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000101void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000102 ASSERT(function->IsOptimizable());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000103
jkummerow@chromium.org1e8da742013-08-26 17:13:35 +0000104 if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000105 PrintF("[marking ");
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000106 function->ShortPrint();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000107 PrintF(" for recompilation, reason: %s", reason);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000108 if (FLAG_type_info_threshold > 0) {
109 int typeinfo, total, percentage;
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000110 GetICCounts(function->shared()->code(), &typeinfo, &total, &percentage);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000111 PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
112 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000113 PrintF("]\n");
114 }
115
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000116
machenbach@chromium.org9af454f2013-11-20 09:25:57 +0000117 if (isolate_->concurrent_recompilation_enabled() &&
118 !isolate_->bootstrapper()->IsActive()) {
119 if (isolate_->concurrent_osr_enabled() &&
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000120 isolate_->optimizing_compiler_thread()->IsQueuedForOSR(function)) {
121 // Do not attempt regular recompilation if we already queued this for OSR.
122 // TODO(yangguo): This is necessary so that we don't install optimized
123 // code on a function that is already optimized, since OSR and regular
124 // recompilation race. This goes away as soon as OSR becomes one-shot.
125 return;
126 }
yangguo@chromium.org49546742013-12-23 16:17:49 +0000127 ASSERT(!function->IsInOptimizationQueue());
128 function->MarkForConcurrentOptimization();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000129 } else {
130 // The next call to the function will trigger optimization.
yangguo@chromium.org49546742013-12-23 16:17:49 +0000131 function->MarkForOptimization();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000132 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000133}
134
135
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000137 // See AlwaysFullCompiler (in compiler.cc) comment on why we need
138 // Debug::has_break_points().
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 if (!FLAG_use_osr ||
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000140 isolate_->DebuggerHasBreakPoints() ||
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 function->IsBuiltin()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000142 return;
143 }
144
145 SharedFunctionInfo* shared = function->shared();
fschneider@chromium.org1805e212011-09-05 10:49:12 +0000146 // If the code is not optimizable, don't try OSR.
147 if (!shared->code()->optimizable()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000148
149 // We are not prepared to do OSR for a function that already has an
150 // allocated arguments object. The optimized code would bypass it for
151 // arguments accesses, which is unsound. Don't try OSR.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000152 if (shared->uses_arguments()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000153
154 // We're using on-stack replacement: patch the unoptimized code so that
155 // any back edge in any unoptimized frame will trigger on-stack
156 // replacement for that frame.
157 if (FLAG_trace_osr) {
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000158 PrintF("[OSR - patching back edges in ");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000159 function->PrintName();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000160 PrintF("]\n");
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000161 }
162
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000163 BackEdgeTable::Patch(isolate_, shared->code());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000164}
165
166
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000167void RuntimeProfiler::OptimizeNow() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000168 HandleScope scope(isolate_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000169
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000170 if (isolate_->DebuggerHasBreakPoints()) return;
171
dslomov@chromium.orge97852d2013-09-12 09:02:59 +0000172 DisallowHeapAllocation no_gc;
173
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000174 // Run through the JavaScript frames and collect them. If we already
175 // have a sample of the function, we mark it for optimizations
176 // (eagerly or lazily).
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000177 int frame_count = 0;
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000178 int frame_count_limit = FLAG_frame_count;
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000179 for (JavaScriptFrameIterator it(isolate_);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000180 frame_count++ < frame_count_limit && !it.done();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000181 it.Advance()) {
182 JavaScriptFrame* frame = it.frame();
danno@chromium.org169691d2013-07-15 08:01:13 +0000183 JSFunction* function = frame->function();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000184
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000185 SharedFunctionInfo* shared = function->shared();
186 Code* shared_code = shared->code();
187
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000188 if (shared_code->kind() != Code::FUNCTION) continue;
yangguo@chromium.org49546742013-12-23 16:17:49 +0000189 if (function->IsInOptimizationQueue()) continue;
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000190
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000191 if (FLAG_always_osr &&
192 shared_code->allow_osr_at_loop_nesting_level() == 0) {
193 // Testing mode: always try an OSR compile for every function.
194 for (int i = 0; i < Code::kMaxLoopNestingMarker; i++) {
195 // TODO(titzer): fix AttemptOnStackReplacement to avoid this dumb loop.
196 shared_code->set_allow_osr_at_loop_nesting_level(i);
197 AttemptOnStackReplacement(function);
198 }
199 // Fall through and do a normal optimized compile as well.
200 } else if (!frame->is_optimized() &&
yangguo@chromium.org49546742013-12-23 16:17:49 +0000201 (function->IsMarkedForOptimization() ||
202 function->IsMarkedForConcurrentOptimization() ||
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000203 function->IsOptimized())) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000204 // Attempt OSR if we are still running unoptimized code even though the
205 // the function has long been marked or even already been optimized.
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000206 int ticks = shared_code->profiler_ticks();
207 int allowance = kOSRCodeSizeAllowanceBase +
208 ticks * kOSRCodeSizeAllowancePerTick;
209 if (shared_code->CodeSize() > allowance) {
210 if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1);
211 } else {
212 int nesting = shared_code->allow_osr_at_loop_nesting_level();
213 if (nesting < Code::kMaxLoopNestingMarker) {
214 int new_nesting = nesting + 1;
215 shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
216 AttemptOnStackReplacement(function);
217 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000218 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +0000219 continue;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000220 }
221
yangguo@chromium.org56454712012-02-16 15:33:53 +0000222 // Only record top-level code on top of the execution stack and
223 // avoid optimizing excessively large scripts since top-level code
224 // will be executed only once.
225 const int kMaxToplevelSourceSize = 10 * 1024;
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000226 if (shared->is_toplevel() &&
227 (frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000228 continue;
229 }
230
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000231 // Do not record non-optimizable functions.
232 if (shared->optimization_disabled()) {
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000233 if (shared->deopt_count() >= FLAG_max_opt_count) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000234 // If optimization was disabled due to many deoptimizations,
235 // then check if the function is hot and try to reenable optimization.
236 int ticks = shared_code->profiler_ticks();
237 if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
238 shared_code->set_profiler_ticks(0);
239 shared->TryReenableOptimization();
240 } else {
241 shared_code->set_profiler_ticks(ticks + 1);
242 }
243 }
244 continue;
245 }
246 if (!function->IsOptimizable()) continue;
247
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000248 int ticks = shared_code->profiler_ticks();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000249
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000250 if (ticks >= kProfilerTicksBeforeOptimization) {
251 int typeinfo, total, percentage;
252 GetICCounts(shared_code, &typeinfo, &total, &percentage);
253 if (percentage >= FLAG_type_info_threshold) {
254 // If this particular function hasn't had any ICs patched for enough
255 // ticks, optimize it now.
256 Optimize(function, "hot and stable");
257 } else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
258 Optimize(function, "not much type info but very hot");
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000259 } else {
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000260 shared_code->set_profiler_ticks(ticks + 1);
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000261 if (FLAG_trace_opt_verbose) {
262 PrintF("[not yet optimizing ");
263 function->PrintName();
264 PrintF(", not enough type info: %d/%d (%d%%)]\n",
265 typeinfo, total, percentage);
266 }
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000267 }
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000268 } else if (!any_ic_changed_ &&
269 shared_code->instruction_size() < kMaxSizeEarlyOpt) {
270 // If no IC was patched since the last tick and this function is very
271 // small, optimistically optimize it now.
272 Optimize(function, "small function");
273 } else {
274 shared_code->set_profiler_ticks(ticks + 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000275 }
276 }
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +0000277 any_ic_changed_ = false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000278}
279
280
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000281} } // namespace v8::internal