2012-02-09 10:19:46 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/runtime-profiler.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/assembler.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/code-stubs.h"
|
|
|
|
#include "src/compilation-cache.h"
|
|
|
|
#include "src/execution.h"
|
|
|
|
#include "src/full-codegen.h"
|
|
|
|
#include "src/global-handles.h"
|
2014-08-05 08:18:22 +00:00
|
|
|
#include "src/heap/mark-compact.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/isolate-inl.h"
|
|
|
|
#include "src/scopeinfo.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-20 11:42:56 +00:00
|
|
|
static void GetICCounts(SharedFunctionInfo* shared,
|
|
|
|
int* ic_with_type_info_count, int* ic_generic_count,
|
|
|
|
int* ic_total_count, int* type_info_percentage,
|
|
|
|
int* generic_percentage) {
|
|
|
|
Code* shared_code = shared->code();
|
2012-02-20 12:57:23 +00:00
|
|
|
*ic_total_count = 0;
|
2014-08-05 17:06:01 +00:00
|
|
|
*ic_generic_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();
|
2014-08-05 17:06:01 +00:00
|
|
|
*ic_generic_count = info->ic_generic_count();
|
2012-02-20 12:57:23 +00:00
|
|
|
*ic_total_count = info->ic_total_count();
|
|
|
|
}
|
2014-10-20 11:42:56 +00:00
|
|
|
|
|
|
|
// Harvest vector-ics as well
|
|
|
|
TypeFeedbackVector* vector = shared->feedback_vector();
|
|
|
|
*ic_with_type_info_count += vector->ic_with_type_info_count();
|
|
|
|
*ic_generic_count += vector->ic_generic_count();
|
|
|
|
|
2014-08-05 17:06:01 +00:00
|
|
|
if (*ic_total_count > 0) {
|
|
|
|
*type_info_percentage = 100 * *ic_with_type_info_count / *ic_total_count;
|
|
|
|
*generic_percentage = 100 * *ic_generic_count / *ic_total_count;
|
|
|
|
} else {
|
|
|
|
*type_info_percentage = 100; // Compared against lower bound.
|
|
|
|
*generic_percentage = 0; // Compared against upper bound.
|
|
|
|
}
|
2012-02-20 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 10:19:46 +00:00
|
|
|
void RuntimeProfiler::Optimize(JSFunction* function, const char* reason) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(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) {
|
2014-08-05 17:06:01 +00:00
|
|
|
int typeinfo, generic, total, type_percentage, generic_percentage;
|
2014-10-20 11:42:56 +00:00
|
|
|
GetICCounts(function->shared(), &typeinfo, &generic, &total,
|
2014-08-05 17:06:01 +00:00
|
|
|
&type_percentage, &generic_percentage);
|
|
|
|
PrintF(", ICs with typeinfo: %d/%d (%d%%)", typeinfo, total,
|
|
|
|
type_percentage);
|
|
|
|
PrintF(", generic ICs: %d/%d (%d%%)", generic, total, generic_percentage);
|
2012-02-20 12:57:23 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
PrintF("]\n");
|
|
|
|
}
|
|
|
|
|
2014-11-05 11:12:27 +00:00
|
|
|
function->shared()->set_optimize_next_closure(true);
|
|
|
|
function->AttemptConcurrentOptimization();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 09:31:30 +00:00
|
|
|
void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function,
|
|
|
|
int loop_nesting_levels) {
|
|
|
|
SharedFunctionInfo* shared = function->shared();
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-06-24 09:31:30 +00:00
|
|
|
for (int i = 0; i < loop_nesting_levels; i++) {
|
|
|
|
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
|
|
|
|
2014-06-24 09:31:30 +00:00
|
|
|
if (FLAG_always_osr) {
|
|
|
|
AttemptOnStackReplacement(function, Code::kMaxLoopNestingMarker);
|
2013-06-26 08:43:27 +00:00
|
|
|
// 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 {
|
2014-06-24 09:31:30 +00:00
|
|
|
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) {
|
2014-08-05 17:06:01 +00:00
|
|
|
int typeinfo, generic, total, type_percentage, generic_percentage;
|
2014-10-20 11:42:56 +00:00
|
|
|
GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
|
2014-08-05 17:06:01 +00:00
|
|
|
&generic_percentage);
|
|
|
|
if (type_percentage >= FLAG_type_info_threshold &&
|
|
|
|
generic_percentage <= FLAG_generic_ic_threshold) {
|
2013-12-06 09:52:40 +00:00
|
|
|
// 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();
|
2014-08-05 17:06:01 +00:00
|
|
|
PrintF(", not enough type info: %d/%d (%d%%)]\n", typeinfo, total,
|
|
|
|
type_percentage);
|
2013-12-06 09:52:40 +00:00
|
|
|
}
|
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.
|
2014-08-05 17:06:01 +00:00
|
|
|
int typeinfo, generic, total, type_percentage, generic_percentage;
|
2014-10-20 11:42:56 +00:00
|
|
|
GetICCounts(shared, &typeinfo, &generic, &total, &type_percentage,
|
2014-08-05 17:06:01 +00:00
|
|
|
&generic_percentage);
|
|
|
|
if (type_percentage >= FLAG_type_info_threshold &&
|
|
|
|
generic_percentage <= FLAG_generic_ic_threshold) {
|
|
|
|
Optimize(function, "small function");
|
|
|
|
} else {
|
|
|
|
shared_code->set_profiler_ticks(ticks + 1);
|
|
|
|
}
|
2013-12-06 09:52:40 +00:00
|
|
|
} 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
|