2012-02-09 10:19:46 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2010-12-07 11:31:57 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "runtime-profiler.h"
|
|
|
|
|
|
|
|
#include "assembler.h"
|
2013-04-18 14:34:34 +00:00
|
|
|
#include "bootstrapper.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "code-stubs.h"
|
|
|
|
#include "compilation-cache.h"
|
|
|
|
#include "execution.h"
|
2012-07-25 13:51:29 +00:00
|
|
|
#include "full-codegen.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "global-handles.h"
|
2011-10-03 11:13:20 +00:00
|
|
|
#include "isolate-inl.h"
|
2011-03-02 10:16:47 +00:00
|
|
|
#include "mark-compact.h"
|
2011-03-18 20:35:07 +00:00
|
|
|
#include "platform.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "scopeinfo.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
2012-02-09 10:19:46 +00:00
|
|
|
// Number of times a function has to be seen on the stack before it is
|
|
|
|
// optimized.
|
|
|
|
static const int kProfilerTicksBeforeOptimization = 2;
|
2012-06-11 16:57:27 +00:00
|
|
|
// If the function optimization was disabled due to high deoptimization count,
|
|
|
|
// but the function is hot and has been seen on the stack this number of times,
|
|
|
|
// then we try to reenable optimization for this function.
|
|
|
|
static const int kProfilerTicksBeforeReenablingOptimization = 250;
|
2012-03-27 12:19:50 +00:00
|
|
|
// If a function does not have enough type info (according to
|
|
|
|
// FLAG_type_info_threshold), but has seen a huge number of ticks,
|
|
|
|
// optimize it as it is.
|
|
|
|
static const int kTicksWhenNotEnoughTypeInfo = 100;
|
|
|
|
// We only have one byte to store the number of ticks.
|
2012-06-11 16:57:27 +00:00
|
|
|
STATIC_ASSERT(kProfilerTicksBeforeOptimization < 256);
|
|
|
|
STATIC_ASSERT(kProfilerTicksBeforeReenablingOptimization < 256);
|
2012-03-27 12:19:50 +00:00
|
|
|
STATIC_ASSERT(kTicksWhenNotEnoughTypeInfo < 256);
|
2012-02-09 10:19:46 +00:00
|
|
|
|
2013-06-14 11:35:00 +00:00
|
|
|
// Maximum size in bytes of generate code for a function to allow OSR.
|
|
|
|
static const int kOSRCodeSizeAllowanceBase =
|
|
|
|
100 * FullCodeGenerator::kCodeSizeMultiplier;
|
|
|
|
|
|
|
|
static const int kOSRCodeSizeAllowancePerTick =
|
2013-12-18 16:17:42 +00:00
|
|
|
4 * FullCodeGenerator::kCodeSizeMultiplier;
|
2012-06-11 16:57:27 +00:00
|
|
|
|
2012-02-09 10:19:46 +00:00
|
|
|
// Maximum size in bytes of generated code for a function to be optimized
|
|
|
|
// the very first time it is seen on the stack.
|
2012-07-25 13:51:29 +00:00
|
|
|
static const int kMaxSizeEarlyOpt =
|
2013-06-14 11:35:00 +00:00
|
|
|
5 * FullCodeGenerator::kCodeSizeMultiplier;
|
2012-02-09 10:19:46 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
RuntimeProfiler::RuntimeProfiler(Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
2013-12-06 09:52:40 +00:00
|
|
|
any_ic_changed_(false) {
|
2011-03-18 20:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-14 11:35:00 +00:00
|
|
|
static void GetICCounts(Code* shared_code,
|
2012-03-27 12:19:50 +00:00
|
|
|
int* ic_with_type_info_count,
|
2012-02-20 12:57:23 +00:00
|
|
|
int* ic_total_count,
|
|
|
|
int* percentage) {
|
|
|
|
*ic_total_count = 0;
|
2012-03-27 12:19:50 +00:00
|
|
|
*ic_with_type_info_count = 0;
|
2013-06-14 11:35:00 +00:00
|
|
|
Object* raw_info = shared_code->type_feedback_info();
|
2012-02-20 12:57:23 +00:00
|
|
|
if (raw_info->IsTypeFeedbackInfo()) {
|
|
|
|
TypeFeedbackInfo* info = TypeFeedbackInfo::cast(raw_info);
|
2012-03-27 12:19:50 +00:00
|
|
|
*ic_with_type_info_count = info->ic_with_type_info_count();
|
2012-02-20 12:57:23 +00:00
|
|
|
*ic_total_count = info->ic_total_count();
|
|
|
|
}
|
|
|
|
*percentage = *ic_total_count > 0
|
2012-03-27 12:19:50 +00:00
|
|
|
? 100 * *ic_with_type_info_count / *ic_total_count
|
2012-02-20 12:57:23 +00:00
|
|
|
: 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 10:19:46 +00:00
|
|
|
void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
|
2011-04-11 13:24:50 +00:00
|
|
|
ASSERT(function->IsOptimizable());
|
2012-11-16 10:57:50 +00:00
|
|
|
|
2013-08-23 13:30:02 +00:00
|
|
|
if (FLAG_trace_opt && function->PassesFilter(FLAG_hydrogen_filter)) {
|
2011-06-29 14:56:08 +00:00
|
|
|
PrintF("[marking ");
|
2013-05-13 11:10:31 +00:00
|
|
|
function->ShortPrint();
|
2012-02-09 10:19:46 +00:00
|
|
|
PrintF(" for recompilation, reason: %s", reason);
|
2012-02-20 12:57:23 +00:00
|
|
|
if (FLAG_type_info_threshold > 0) {
|
|
|
|
int typeinfo, total, percentage;
|
2013-06-14 11:35:00 +00:00
|
|
|
GetICCounts(function->shared()->code(), &typeinfo, &total, &percentage);
|
2012-02-20 12:57:23 +00:00
|
|
|
PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total, percentage);
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
PrintF("]\n");
|
|
|
|
}
|
|
|
|
|
2013-09-11 12:39:00 +00:00
|
|
|
|
2013-11-19 11:52:47 +00:00
|
|
|
if (isolate_->concurrent_recompilation_enabled() &&
|
|
|
|
!isolate_->bootstrapper()->IsActive()) {
|
|
|
|
if (isolate_->concurrent_osr_enabled() &&
|
2013-09-11 12:39:00 +00:00
|
|
|
isolate_->optimizing_compiler_thread()->IsQueuedForOSR(function)) {
|
|
|
|
// Do not attempt regular recompilation if we already queued this for OSR.
|
|
|
|
// TODO(yangguo): This is necessary so that we don't install optimized
|
|
|
|
// code on a function that is already optimized, since OSR and regular
|
|
|
|
// recompilation race. This goes away as soon as OSR becomes one-shot.
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 14:30:35 +00:00
|
|
|
ASSERT(!function->IsInOptimizationQueue());
|
|
|
|
function->MarkForConcurrentOptimization();
|
2012-07-19 18:58:23 +00:00
|
|
|
} else {
|
|
|
|
// The next call to the function will trigger optimization.
|
2013-12-23 14:30:35 +00:00
|
|
|
function->MarkForOptimization();
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) {
|
2010-12-07 11:31:57 +00:00
|
|
|
// See AlwaysFullCompiler (in compiler.cc) comment on why we need
|
|
|
|
// Debug::has_break_points().
|
2011-03-18 20:35:07 +00:00
|
|
|
if (!FLAG_use_osr ||
|
2011-04-26 13:53:19 +00:00
|
|
|
isolate_->DebuggerHasBreakPoints() ||
|
2011-03-18 20:35:07 +00:00
|
|
|
function->IsBuiltin()) {
|
2010-12-07 11:31:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedFunctionInfo* shared = function->shared();
|
2011-09-02 09:29:21 +00:00
|
|
|
// If the code is not optimizable, don't try OSR.
|
|
|
|
if (!shared->code()->optimizable()) return;
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// We are not prepared to do OSR for a function that already has an
|
|
|
|
// allocated arguments object. The optimized code would bypass it for
|
|
|
|
// arguments accesses, which is unsound. Don't try OSR.
|
2011-06-16 14:12:58 +00:00
|
|
|
if (shared->uses_arguments()) return;
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// We're using on-stack replacement: patch the unoptimized code so that
|
|
|
|
// any back edge in any unoptimized frame will trigger on-stack
|
|
|
|
// replacement for that frame.
|
|
|
|
if (FLAG_trace_osr) {
|
2013-09-04 12:55:59 +00:00
|
|
|
PrintF("[OSR - patching back edges in ");
|
2010-12-07 11:31:57 +00:00
|
|
|
function->PrintName();
|
2013-09-04 12:55:59 +00:00
|
|
|
PrintF("]\n");
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 09:08:08 +00:00
|
|
|
BackEdgeTable::Patch(isolate_, shared->code());
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RuntimeProfiler::OptimizeNow() {
|
2011-03-18 20:35:07 +00:00
|
|
|
HandleScope scope(isolate_);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2013-07-02 15:30:33 +00:00
|
|
|
if (isolate_->DebuggerHasBreakPoints()) return;
|
|
|
|
|
2013-09-11 12:39:00 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Run through the JavaScript frames and collect them. If we already
|
|
|
|
// have a sample of the function, we mark it for optimizations
|
|
|
|
// (eagerly or lazily).
|
2010-12-10 12:00:26 +00:00
|
|
|
int frame_count = 0;
|
2013-12-06 09:52:40 +00:00
|
|
|
int frame_count_limit = FLAG_frame_count;
|
Simplify isolates access during stack iteration (WAS: Move SafeStackFrameIterator::active_count_...)
While trying to fix Mac and Windows versions for this change:
http://codereview.chromium.org/6771047/, I figured out, that we
already store an isolate in StackFrameIterator, so we can use it in
frame objects, instead of requiring it from caller.
I've changed iterators usage to the following scheme: whenever a
caller maintains an isolate pointer, it just passes it to stack
iterator, and no more worries about passing it to frame content
accessors. If a caller uses current isolate, it can omit passing it
to iterator, in this case, an iterator will use the current isolate,
too.
There was a special case with LiveEdit, which creates
detached copies of frame objects.
R=vitalyr@chromium.org
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6794019
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7499 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2011-04-05 09:01:47 +00:00
|
|
|
for (JavaScriptFrameIterator it(isolate_);
|
2012-02-14 14:28:37 +00:00
|
|
|
frame_count++ < frame_count_limit && !it.done();
|
2010-12-07 11:31:57 +00:00
|
|
|
it.Advance()) {
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
2013-07-11 16:45:58 +00:00
|
|
|
JSFunction* function = frame->function();
|
2010-12-09 13:12:23 +00:00
|
|
|
|
2012-06-11 16:57:27 +00:00
|
|
|
SharedFunctionInfo* shared = function->shared();
|
|
|
|
Code* shared_code = shared->code();
|
|
|
|
|
2014-04-23 07:07:54 +00:00
|
|
|
List<JSFunction*> functions(4);
|
|
|
|
frame->GetFunctions(&functions);
|
|
|
|
for (int i = functions.length(); --i >= 0; ) {
|
|
|
|
SharedFunctionInfo* shared_function_info = functions[i]->shared();
|
|
|
|
int ticks = shared_function_info->profiler_ticks();
|
|
|
|
if (ticks < Smi::kMaxValue) {
|
|
|
|
shared_function_info->set_profiler_ticks(ticks + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 12:19:50 +00:00
|
|
|
if (shared_code->kind() != Code::FUNCTION) continue;
|
2013-12-23 14:30:35 +00:00
|
|
|
if (function->IsInOptimizationQueue()) continue;
|
2013-03-12 18:03:18 +00:00
|
|
|
|
2013-06-26 08:43:27 +00:00
|
|
|
if (FLAG_always_osr &&
|
|
|
|
shared_code->allow_osr_at_loop_nesting_level() == 0) {
|
|
|
|
// Testing mode: always try an OSR compile for every function.
|
|
|
|
for (int i = 0; i < Code::kMaxLoopNestingMarker; i++) {
|
|
|
|
// TODO(titzer): fix AttemptOnStackReplacement to avoid this dumb loop.
|
|
|
|
shared_code->set_allow_osr_at_loop_nesting_level(i);
|
|
|
|
AttemptOnStackReplacement(function);
|
|
|
|
}
|
|
|
|
// Fall through and do a normal optimized compile as well.
|
|
|
|
} else if (!frame->is_optimized() &&
|
2013-12-23 14:30:35 +00:00
|
|
|
(function->IsMarkedForOptimization() ||
|
|
|
|
function->IsMarkedForConcurrentOptimization() ||
|
2013-03-12 18:03:18 +00:00
|
|
|
function->IsOptimized())) {
|
2013-06-26 08:43:27 +00:00
|
|
|
// Attempt OSR if we are still running unoptimized code even though the
|
|
|
|
// the function has long been marked or even already been optimized.
|
2013-06-14 11:35:00 +00:00
|
|
|
int ticks = shared_code->profiler_ticks();
|
|
|
|
int allowance = kOSRCodeSizeAllowanceBase +
|
|
|
|
ticks * kOSRCodeSizeAllowancePerTick;
|
|
|
|
if (shared_code->CodeSize() > allowance) {
|
|
|
|
if (ticks < 255) shared_code->set_profiler_ticks(ticks + 1);
|
|
|
|
} else {
|
|
|
|
int nesting = shared_code->allow_osr_at_loop_nesting_level();
|
|
|
|
if (nesting < Code::kMaxLoopNestingMarker) {
|
|
|
|
int new_nesting = nesting + 1;
|
|
|
|
shared_code->set_allow_osr_at_loop_nesting_level(new_nesting);
|
|
|
|
AttemptOnStackReplacement(function);
|
|
|
|
}
|
2013-04-10 09:24:31 +00:00
|
|
|
}
|
2013-06-14 11:35:00 +00:00
|
|
|
continue;
|
2010-12-09 13:12:23 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 14:14:51 +00:00
|
|
|
// Only record top-level code on top of the execution stack and
|
|
|
|
// avoid optimizing excessively large scripts since top-level code
|
|
|
|
// will be executed only once.
|
|
|
|
const int kMaxToplevelSourceSize = 10 * 1024;
|
2012-06-11 16:57:27 +00:00
|
|
|
if (shared->is_toplevel() &&
|
|
|
|
(frame_count > 1 || shared->SourceSize() > kMaxToplevelSourceSize)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not record non-optimizable functions.
|
|
|
|
if (shared->optimization_disabled()) {
|
2012-08-16 11:40:03 +00:00
|
|
|
if (shared->deopt_count() >= FLAG_max_opt_count) {
|
2012-06-11 16:57:27 +00:00
|
|
|
// If optimization was disabled due to many deoptimizations,
|
|
|
|
// then check if the function is hot and try to reenable optimization.
|
|
|
|
int ticks = shared_code->profiler_ticks();
|
|
|
|
if (ticks >= kProfilerTicksBeforeReenablingOptimization) {
|
|
|
|
shared_code->set_profiler_ticks(0);
|
|
|
|
shared->TryReenableOptimization();
|
|
|
|
} else {
|
|
|
|
shared_code->set_profiler_ticks(ticks + 1);
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 14:14:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-11 16:57:27 +00:00
|
|
|
if (!function->IsOptimizable()) continue;
|
|
|
|
|
2013-12-06 09:52:40 +00:00
|
|
|
int ticks = shared_code->profiler_ticks();
|
2012-02-09 10:19:46 +00:00
|
|
|
|
2013-12-06 09:52:40 +00:00
|
|
|
if (ticks >= kProfilerTicksBeforeOptimization) {
|
|
|
|
int typeinfo, total, percentage;
|
|
|
|
GetICCounts(shared_code, &typeinfo, &total, &percentage);
|
|
|
|
if (percentage >= FLAG_type_info_threshold) {
|
|
|
|
// If this particular function hasn't had any ICs patched for enough
|
|
|
|
// ticks, optimize it now.
|
|
|
|
Optimize(function, "hot and stable");
|
|
|
|
} else if (ticks >= kTicksWhenNotEnoughTypeInfo) {
|
|
|
|
Optimize(function, "not much type info but very hot");
|
2012-02-09 10:19:46 +00:00
|
|
|
} else {
|
2012-03-27 12:19:50 +00:00
|
|
|
shared_code->set_profiler_ticks(ticks + 1);
|
2013-12-06 09:52:40 +00:00
|
|
|
if (FLAG_trace_opt_verbose) {
|
|
|
|
PrintF("[not yet optimizing ");
|
|
|
|
function->PrintName();
|
|
|
|
PrintF(", not enough type info: %d/%d (%d%%)]\n",
|
|
|
|
typeinfo, total, percentage);
|
|
|
|
}
|
2012-02-09 10:19:46 +00:00
|
|
|
}
|
2013-12-06 09:52:40 +00:00
|
|
|
} else if (!any_ic_changed_ &&
|
|
|
|
shared_code->instruction_size() < kMaxSizeEarlyOpt) {
|
|
|
|
// If no IC was patched since the last tick and this function is very
|
|
|
|
// small, optimistically optimize it now.
|
|
|
|
Optimize(function, "small function");
|
|
|
|
} else {
|
|
|
|
shared_code->set_profiler_ticks(ticks + 1);
|
2011-03-02 10:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-06 09:52:40 +00:00
|
|
|
any_ic_changed_ = false;
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|