diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2011-07-05 14:40:13 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2011-07-05 14:51:29 -0700 |
commit | 149562555c9bf56457dee9a1ad70c53ed670a776 (patch) | |
tree | f6217cf3c54ddbee03f37247a3c7c75203f868fd /deps/v8/src/runtime-profiler.cc | |
parent | f08720606757577d95bd09b48697c7decbf17f00 (diff) | |
download | node-149562555c9bf56457dee9a1ad70c53ed670a776.tar.gz |
Downgrade V8 to 3.1.8.25
There are serious performance regressions both in V8 and our own legacy
networking stack. Until we correct our own problems we are going back to the
old V8.
Diffstat (limited to 'deps/v8/src/runtime-profiler.cc')
-rw-r--r-- | deps/v8/src/runtime-profiler.cc | 303 |
1 files changed, 193 insertions, 110 deletions
diff --git a/deps/v8/src/runtime-profiler.cc b/deps/v8/src/runtime-profiler.cc index c0eaf9877..df6471e9d 100644 --- a/deps/v8/src/runtime-profiler.cc +++ b/deps/v8/src/runtime-profiler.cc @@ -1,4 +1,4 @@ -// Copyright 2011 the V8 project authors. All rights reserved. +// Copyright 2010 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: @@ -36,16 +36,49 @@ #include "execution.h" #include "global-handles.h" #include "mark-compact.h" -#include "platform.h" #include "scopeinfo.h" +#include "top.h" namespace v8 { namespace internal { +class PendingListNode : public Malloced { + public: + explicit PendingListNode(JSFunction* function); + ~PendingListNode() { Destroy(); } + + PendingListNode* next() const { return next_; } + void set_next(PendingListNode* node) { next_ = node; } + Handle<JSFunction> function() { return Handle<JSFunction>::cast(function_); } + + // If the function is garbage collected before we've had the chance + // to optimize it the weak handle will be null. + bool IsValid() { return !function_.is_null(); } + + // Returns the number of microseconds this node has been pending. + int Delay() const { return static_cast<int>(OS::Ticks() - start_); } + + private: + void Destroy(); + static void WeakCallback(v8::Persistent<v8::Value> object, void* data); + + PendingListNode* next_; + Handle<Object> function_; // Weak handle. + int64_t start_; +}; + + +enum SamplerState { + IN_NON_JS_STATE = 0, + IN_JS_STATE = 1 +}; + + // Optimization sampler constants. static const int kSamplerFrameCount = 2; static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; +static const int kSamplerWindowSize = 16; static const int kSamplerTicksBetweenThresholdAdjustment = 32; @@ -54,50 +87,63 @@ static const int kSamplerThresholdMin = 1; static const int kSamplerThresholdDelta = 1; static const int kSamplerThresholdSizeFactorInit = 3; +static const int kSamplerThresholdSizeFactorMin = 1; +static const int kSamplerThresholdSizeFactorDelta = 1; static const int kSizeLimit = 1500; +static int sampler_threshold = kSamplerThresholdInit; +static int sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit; -Atomic32 RuntimeProfiler::state_ = 0; -// TODO(isolates): Create the semaphore lazily and clean it up when no -// longer required. -#ifdef ENABLE_LOGGING_AND_PROFILING -Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0); -#endif +static int sampler_ticks_until_threshold_adjustment = + kSamplerTicksBetweenThresholdAdjustment; -#ifdef DEBUG -bool RuntimeProfiler::has_been_globally_setup_ = false; -#endif -bool RuntimeProfiler::enabled_ = false; +// The ratio of ticks spent in JS code in percent. +static Atomic32 js_ratio; +static Object* sampler_window[kSamplerWindowSize] = { NULL, }; +static int sampler_window_position = 0; +static int sampler_window_weight[kSamplerWindowSize] = { 0, }; -RuntimeProfiler::RuntimeProfiler(Isolate* isolate) - : isolate_(isolate), - sampler_threshold_(kSamplerThresholdInit), - sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), - sampler_ticks_until_threshold_adjustment_( - kSamplerTicksBetweenThresholdAdjustment), - sampler_window_position_(0) { - ClearSampleBuffer(); + +// Support for pending 'optimize soon' requests. +static PendingListNode* optimize_soon_list = NULL; + + +PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) { + function_ = GlobalHandles::Create(function); + start_ = OS::Ticks(); + GlobalHandles::MakeWeak(function_.location(), this, &WeakCallback); } -void RuntimeProfiler::GlobalSetup() { - ASSERT(!has_been_globally_setup_); - enabled_ = V8::UseCrankshaft() && FLAG_opt; -#ifdef DEBUG - has_been_globally_setup_ = true; -#endif +void PendingListNode::Destroy() { + if (!IsValid()) return; + GlobalHandles::Destroy(function_.location()); + function_= Handle<Object>::null(); +} + + +void PendingListNode::WeakCallback(v8::Persistent<v8::Value>, void* data) { + reinterpret_cast<PendingListNode*>(data)->Destroy(); +} + + +static bool IsOptimizable(JSFunction* function) { + Code* code = function->code(); + return code->kind() == Code::FUNCTION && code->optimizable(); } -void RuntimeProfiler::Optimize(JSFunction* function) { - ASSERT(function->IsOptimizable()); +static void Optimize(JSFunction* function, bool eager, int delay) { + ASSERT(IsOptimizable(function)); if (FLAG_trace_opt) { - PrintF("[marking "); + PrintF("[marking (%s) ", eager ? "eagerly" : "lazily"); function->PrintName(); - PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address())); PrintF(" for recompilation"); + if (delay > 0) { + PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000); + } PrintF("]\n"); } @@ -106,13 +152,11 @@ void RuntimeProfiler::Optimize(JSFunction* function) { } -void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { +static void AttemptOnStackReplacement(JSFunction* function) { // See AlwaysFullCompiler (in compiler.cc) comment on why we need // Debug::has_break_points(). ASSERT(function->IsMarkedForLazyRecompilation()); - if (!FLAG_use_osr || - isolate_->DebuggerHasBreakPoints() || - function->IsBuiltin()) { + if (!FLAG_use_osr || Debug::has_break_points() || function->IsBuiltin()) { return; } @@ -125,7 +169,7 @@ void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { // 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. - if (shared->uses_arguments()) return; + if (shared->scope_info()->HasArgumentsShadow()) return; // We're using on-stack replacement: patch the unoptimized code so that // any back edge in any unoptimized frame will trigger on-stack @@ -142,8 +186,7 @@ void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { Object* check_code; MaybeObject* maybe_check_code = check_stub.TryGetCode(); if (maybe_check_code->ToObject(&check_code)) { - Code* replacement_code = - isolate_->builtins()->builtin(Builtins::kOnStackReplacement); + Code* replacement_code = Builtins::builtin(Builtins::OnStackReplacement); Code* unoptimized_code = shared->code(); Deoptimizer::PatchStackCheckCode(unoptimized_code, Code::cast(check_code), @@ -152,19 +195,21 @@ void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { } -void RuntimeProfiler::ClearSampleBuffer() { - memset(sampler_window_, 0, sizeof(sampler_window_)); - memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_)); +static void ClearSampleBuffer() { + for (int i = 0; i < kSamplerWindowSize; i++) { + sampler_window[i] = NULL; + sampler_window_weight[i] = 0; + } } -int RuntimeProfiler::LookupSample(JSFunction* function) { +static int LookupSample(JSFunction* function) { int weight = 0; for (int i = 0; i < kSamplerWindowSize; i++) { - Object* sample = sampler_window_[i]; + Object* sample = sampler_window[i]; if (sample != NULL) { if (function == sample) { - weight += sampler_window_weight_[i]; + weight += sampler_window_weight[i]; } } } @@ -172,17 +217,31 @@ int RuntimeProfiler::LookupSample(JSFunction* function) { } -void RuntimeProfiler::AddSample(JSFunction* function, int weight) { +static void AddSample(JSFunction* function, int weight) { ASSERT(IsPowerOf2(kSamplerWindowSize)); - sampler_window_[sampler_window_position_] = function; - sampler_window_weight_[sampler_window_position_] = weight; - sampler_window_position_ = (sampler_window_position_ + 1) & + sampler_window[sampler_window_position] = function; + sampler_window_weight[sampler_window_position] = weight; + sampler_window_position = (sampler_window_position + 1) & (kSamplerWindowSize - 1); } void RuntimeProfiler::OptimizeNow() { - HandleScope scope(isolate_); + HandleScope scope; + PendingListNode* current = optimize_soon_list; + while (current != NULL) { + PendingListNode* next = current->next(); + if (current->IsValid()) { + Handle<JSFunction> function = current->function(); + int delay = current->Delay(); + if (IsOptimizable(*function)) { + Optimize(*function, true, delay); + } + } + delete current; + current = next; + } + optimize_soon_list = NULL; // Run through the JavaScript frames and collect them. If we already // have a sample of the function, we mark it for optimizations @@ -190,7 +249,7 @@ void RuntimeProfiler::OptimizeNow() { JSFunction* samples[kSamplerFrameCount]; int sample_count = 0; int frame_count = 0; - for (JavaScriptFrameIterator it(isolate_); + for (JavaScriptFrameIterator it; frame_count++ < kSamplerFrameCount && !it.done(); it.Advance()) { JavaScriptFrame* frame = it.frame(); @@ -198,14 +257,14 @@ void RuntimeProfiler::OptimizeNow() { // Adjust threshold each time we have processed // a certain number of ticks. - if (sampler_ticks_until_threshold_adjustment_ > 0) { - sampler_ticks_until_threshold_adjustment_--; - if (sampler_ticks_until_threshold_adjustment_ <= 0) { + if (sampler_ticks_until_threshold_adjustment > 0) { + sampler_ticks_until_threshold_adjustment--; + if (sampler_ticks_until_threshold_adjustment <= 0) { // If the threshold is not already at the minimum // modify and reset the ticks until next adjustment. - if (sampler_threshold_ > kSamplerThresholdMin) { - sampler_threshold_ -= kSamplerThresholdDelta; - sampler_ticks_until_threshold_adjustment_ = + if (sampler_threshold > kSamplerThresholdMin) { + sampler_threshold -= kSamplerThresholdDelta; + sampler_ticks_until_threshold_adjustment = kSamplerTicksBetweenThresholdAdjustment; } } @@ -220,18 +279,32 @@ void RuntimeProfiler::OptimizeNow() { } // Do not record non-optimizable functions. - if (!function->IsOptimizable()) continue; + if (!IsOptimizable(function)) continue; samples[sample_count++] = function; int function_size = function->shared()->SourceSize(); int threshold_size_factor = (function_size > kSizeLimit) - ? sampler_threshold_size_factor_ + ? sampler_threshold_size_factor : 1; - int threshold = sampler_threshold_ * threshold_size_factor; + int threshold = sampler_threshold * threshold_size_factor; + int current_js_ratio = NoBarrier_Load(&js_ratio); + + // Adjust threshold depending on the ratio of time spent + // in JS code. + if (current_js_ratio < 20) { + // If we spend less than 20% of the time in JS code, + // do not optimize. + continue; + } else if (current_js_ratio < 75) { + // Below 75% of time spent in JS code, only optimize very + // frequently used functions. + threshold *= 3; + } if (LookupSample(function) >= threshold) { - Optimize(function); + Optimize(function, false, 0); + CompilationCache::MarkForEagerOptimizing(Handle<JSFunction>(function)); } } @@ -244,27 +317,59 @@ void RuntimeProfiler::OptimizeNow() { } +void RuntimeProfiler::OptimizeSoon(JSFunction* function) { + if (!IsOptimizable(function)) return; + PendingListNode* node = new PendingListNode(function); + node->set_next(optimize_soon_list); + optimize_soon_list = node; +} + + +#ifdef ENABLE_LOGGING_AND_PROFILING +static void UpdateStateRatio(SamplerState current_state) { + static const int kStateWindowSize = 128; + static SamplerState state_window[kStateWindowSize]; + static int state_window_position = 0; + static int state_counts[2] = { kStateWindowSize, 0 }; + + SamplerState old_state = state_window[state_window_position]; + state_counts[old_state]--; + state_window[state_window_position] = current_state; + state_counts[current_state]++; + ASSERT(IsPowerOf2(kStateWindowSize)); + state_window_position = (state_window_position + 1) & + (kStateWindowSize - 1); + NoBarrier_Store(&js_ratio, state_counts[IN_JS_STATE] * 100 / + kStateWindowSize); +} +#endif + + void RuntimeProfiler::NotifyTick() { #ifdef ENABLE_LOGGING_AND_PROFILING - isolate_->stack_guard()->RequestRuntimeProfilerTick(); + // Record state sample. + SamplerState state = Top::IsInJSState() + ? IN_JS_STATE + : IN_NON_JS_STATE; + UpdateStateRatio(state); + StackGuard::RequestRuntimeProfilerTick(); #endif } void RuntimeProfiler::Setup() { - ASSERT(has_been_globally_setup_); ClearSampleBuffer(); // If the ticker hasn't already started, make sure to do so to get // the ticks for the runtime profiler. - if (IsEnabled()) isolate_->logger()->EnsureTickerStarted(); + if (IsEnabled()) Logger::EnsureTickerStarted(); } void RuntimeProfiler::Reset() { - sampler_threshold_ = kSamplerThresholdInit; - sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit; - sampler_ticks_until_threshold_adjustment_ = + sampler_threshold = kSamplerThresholdInit; + sampler_ticks_until_threshold_adjustment = kSamplerTicksBetweenThresholdAdjustment; + sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit; } @@ -281,60 +386,24 @@ int RuntimeProfiler::SamplerWindowSize() { // Update the pointers in the sampler window after a GC. void RuntimeProfiler::UpdateSamplesAfterScavenge() { for (int i = 0; i < kSamplerWindowSize; i++) { - Object* function = sampler_window_[i]; - if (function != NULL && isolate_->heap()->InNewSpace(function)) { + Object* function = sampler_window[i]; + if (function != NULL && Heap::InNewSpace(function)) { MapWord map_word = HeapObject::cast(function)->map_word(); if (map_word.IsForwardingAddress()) { - sampler_window_[i] = map_word.ToForwardingAddress(); + sampler_window[i] = map_word.ToForwardingAddress(); } else { - sampler_window_[i] = NULL; + sampler_window[i] = NULL; } } } } -void RuntimeProfiler::HandleWakeUp(Isolate* isolate) { -#ifdef ENABLE_LOGGING_AND_PROFILING - // The profiler thread must still be waiting. - ASSERT(NoBarrier_Load(&state_) >= 0); - // In IsolateEnteredJS we have already incremented the counter and - // undid the decrement done by the profiler thread. Increment again - // to get the right count of active isolates. - NoBarrier_AtomicIncrement(&state_, 1); - semaphore_->Signal(); -#endif -} - - -bool RuntimeProfiler::IsSomeIsolateInJS() { - return NoBarrier_Load(&state_) > 0; -} - - -bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() { -#ifdef ENABLE_LOGGING_AND_PROFILING - Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1); - ASSERT(old_state >= -1); - if (old_state != 0) return false; - semaphore_->Wait(); -#endif - return true; -} - - -void RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown() { -#ifdef ENABLE_LOGGING_AND_PROFILING - semaphore_->Signal(); -#endif -} - - void RuntimeProfiler::RemoveDeadSamples() { for (int i = 0; i < kSamplerWindowSize; i++) { - Object* function = sampler_window_[i]; + Object* function = sampler_window[i]; if (function != NULL && !HeapObject::cast(function)->IsMarked()) { - sampler_window_[i] = NULL; + sampler_window[i] = NULL; } } } @@ -342,15 +411,29 @@ void RuntimeProfiler::RemoveDeadSamples() { void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { for (int i = 0; i < kSamplerWindowSize; i++) { - visitor->VisitPointer(&sampler_window_[i]); + visitor->VisitPointer(&sampler_window[i]); } } bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { #ifdef ENABLE_LOGGING_AND_PROFILING - if (!RuntimeProfiler::IsSomeIsolateInJS()) { - return RuntimeProfiler::WaitForSomeIsolateToEnterJS(); + static const int kNonJSTicksThreshold = 100; + // We suspend the runtime profiler thread when not running + // JavaScript. If the CPU profiler is active we must not do this + // because it samples both JavaScript and C++ code. + if (RuntimeProfiler::IsEnabled() && + !CpuProfiler::is_profiling() && + !(FLAG_prof && FLAG_prof_auto)) { + if (Top::IsInJSState()) { + non_js_ticks_ = 0; + } else { + if (non_js_ticks_ < kNonJSTicksThreshold) { + ++non_js_ticks_; + } else { + if (Top::WaitForJSState()) return true; + } + } } #endif return false; |