Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 | // 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" |
| 33 | #include "code-stubs.h" |
| 34 | #include "compilation-cache.h" |
| 35 | #include "deoptimizer.h" |
| 36 | #include "execution.h" |
| 37 | #include "global-handles.h" |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 38 | #include "mark-compact.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 39 | #include "scopeinfo.h" |
| 40 | #include "top.h" |
| 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
| 45 | |
| 46 | class PendingListNode : public Malloced { |
| 47 | public: |
| 48 | explicit PendingListNode(JSFunction* function); |
| 49 | ~PendingListNode() { Destroy(); } |
| 50 | |
| 51 | PendingListNode* next() const { return next_; } |
| 52 | void set_next(PendingListNode* node) { next_ = node; } |
| 53 | Handle<JSFunction> function() { return Handle<JSFunction>::cast(function_); } |
| 54 | |
| 55 | // If the function is garbage collected before we've had the chance |
| 56 | // to optimize it the weak handle will be null. |
| 57 | bool IsValid() { return !function_.is_null(); } |
| 58 | |
| 59 | // Returns the number of microseconds this node has been pending. |
| 60 | int Delay() const { return static_cast<int>(OS::Ticks() - start_); } |
| 61 | |
| 62 | private: |
| 63 | void Destroy(); |
| 64 | static void WeakCallback(v8::Persistent<v8::Value> object, void* data); |
| 65 | |
| 66 | PendingListNode* next_; |
| 67 | Handle<Object> function_; // Weak handle. |
| 68 | int64_t start_; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | enum SamplerState { |
| 73 | IN_NON_JS_STATE = 0, |
| 74 | IN_JS_STATE = 1 |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | // Optimization sampler constants. |
| 79 | static const int kSamplerFrameCount = 2; |
| 80 | static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; |
| 81 | static const int kSamplerWindowSize = 16; |
| 82 | |
| 83 | static const int kSamplerTicksBetweenThresholdAdjustment = 32; |
| 84 | |
| 85 | static const int kSamplerThresholdInit = 3; |
| 86 | static const int kSamplerThresholdMin = 1; |
| 87 | static const int kSamplerThresholdDelta = 1; |
| 88 | |
| 89 | static const int kSamplerThresholdSizeFactorInit = 3; |
| 90 | static const int kSamplerThresholdSizeFactorMin = 1; |
| 91 | static const int kSamplerThresholdSizeFactorDelta = 1; |
| 92 | |
| 93 | static const int kSizeLimit = 1500; |
| 94 | |
| 95 | static int sampler_threshold = kSamplerThresholdInit; |
| 96 | static int sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit; |
| 97 | |
| 98 | static int sampler_ticks_until_threshold_adjustment = |
| 99 | kSamplerTicksBetweenThresholdAdjustment; |
| 100 | |
| 101 | // The ratio of ticks spent in JS code in percent. |
| 102 | static Atomic32 js_ratio; |
| 103 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 104 | static Object* sampler_window[kSamplerWindowSize] = { NULL, }; |
| 105 | static int sampler_window_position = 0; |
| 106 | static int sampler_window_weight[kSamplerWindowSize] = { 0, }; |
| 107 | |
| 108 | |
| 109 | // Support for pending 'optimize soon' requests. |
| 110 | static PendingListNode* optimize_soon_list = NULL; |
| 111 | |
| 112 | |
| 113 | PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) { |
| 114 | function_ = GlobalHandles::Create(function); |
| 115 | start_ = OS::Ticks(); |
| 116 | GlobalHandles::MakeWeak(function_.location(), this, &WeakCallback); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void PendingListNode::Destroy() { |
| 121 | if (!IsValid()) return; |
| 122 | GlobalHandles::Destroy(function_.location()); |
| 123 | function_= Handle<Object>::null(); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void PendingListNode::WeakCallback(v8::Persistent<v8::Value>, void* data) { |
| 128 | reinterpret_cast<PendingListNode*>(data)->Destroy(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static bool IsOptimizable(JSFunction* function) { |
| 133 | Code* code = function->code(); |
| 134 | return code->kind() == Code::FUNCTION && code->optimizable(); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static void Optimize(JSFunction* function, bool eager, int delay) { |
| 139 | ASSERT(IsOptimizable(function)); |
| 140 | if (FLAG_trace_opt) { |
| 141 | PrintF("[marking (%s) ", eager ? "eagerly" : "lazily"); |
| 142 | function->PrintName(); |
| 143 | PrintF(" for recompilation"); |
| 144 | if (delay > 0) { |
| 145 | PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000); |
| 146 | } |
| 147 | PrintF("]\n"); |
| 148 | } |
| 149 | |
| 150 | // The next call to the function will trigger optimization. |
| 151 | function->MarkForLazyRecompilation(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static void AttemptOnStackReplacement(JSFunction* function) { |
| 156 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 157 | // Debug::has_break_points(). |
| 158 | ASSERT(function->IsMarkedForLazyRecompilation()); |
| 159 | if (!FLAG_use_osr || Debug::has_break_points() || function->IsBuiltin()) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | SharedFunctionInfo* shared = function->shared(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 164 | // If the code is not optimizable or references context slots, don't try OSR. |
| 165 | if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) { |
| 166 | return; |
| 167 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 168 | |
| 169 | // We are not prepared to do OSR for a function that already has an |
| 170 | // allocated arguments object. The optimized code would bypass it for |
| 171 | // arguments accesses, which is unsound. Don't try OSR. |
| 172 | if (shared->scope_info()->HasArgumentsShadow()) return; |
| 173 | |
| 174 | // We're using on-stack replacement: patch the unoptimized code so that |
| 175 | // any back edge in any unoptimized frame will trigger on-stack |
| 176 | // replacement for that frame. |
| 177 | if (FLAG_trace_osr) { |
| 178 | PrintF("[patching stack checks in "); |
| 179 | function->PrintName(); |
| 180 | PrintF(" for on-stack replacement]\n"); |
| 181 | } |
| 182 | |
| 183 | // Get the stack check stub code object to match against. We aren't |
| 184 | // prepared to generate it, but we don't expect to have to. |
| 185 | StackCheckStub check_stub; |
| 186 | Object* check_code; |
| 187 | MaybeObject* maybe_check_code = check_stub.TryGetCode(); |
| 188 | if (maybe_check_code->ToObject(&check_code)) { |
| 189 | Code* replacement_code = Builtins::builtin(Builtins::OnStackReplacement); |
| 190 | Code* unoptimized_code = shared->code(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 191 | Deoptimizer::PatchStackCheckCode(unoptimized_code, |
| 192 | Code::cast(check_code), |
| 193 | replacement_code); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static void ClearSampleBuffer() { |
| 199 | for (int i = 0; i < kSamplerWindowSize; i++) { |
| 200 | sampler_window[i] = NULL; |
| 201 | sampler_window_weight[i] = 0; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 206 | static int LookupSample(JSFunction* function) { |
| 207 | int weight = 0; |
| 208 | for (int i = 0; i < kSamplerWindowSize; i++) { |
| 209 | Object* sample = sampler_window[i]; |
| 210 | if (sample != NULL) { |
| 211 | if (function == sample) { |
| 212 | weight += sampler_window_weight[i]; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | return weight; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void AddSample(JSFunction* function, int weight) { |
| 221 | ASSERT(IsPowerOf2(kSamplerWindowSize)); |
| 222 | sampler_window[sampler_window_position] = function; |
| 223 | sampler_window_weight[sampler_window_position] = weight; |
| 224 | sampler_window_position = (sampler_window_position + 1) & |
| 225 | (kSamplerWindowSize - 1); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void RuntimeProfiler::OptimizeNow() { |
| 230 | HandleScope scope; |
| 231 | PendingListNode* current = optimize_soon_list; |
| 232 | while (current != NULL) { |
| 233 | PendingListNode* next = current->next(); |
| 234 | if (current->IsValid()) { |
| 235 | Handle<JSFunction> function = current->function(); |
| 236 | int delay = current->Delay(); |
| 237 | if (IsOptimizable(*function)) { |
| 238 | Optimize(*function, true, delay); |
| 239 | } |
| 240 | } |
| 241 | delete current; |
| 242 | current = next; |
| 243 | } |
| 244 | optimize_soon_list = NULL; |
| 245 | |
| 246 | // Run through the JavaScript frames and collect them. If we already |
| 247 | // have a sample of the function, we mark it for optimizations |
| 248 | // (eagerly or lazily). |
| 249 | JSFunction* samples[kSamplerFrameCount]; |
| 250 | int sample_count = 0; |
| 251 | int frame_count = 0; |
| 252 | for (JavaScriptFrameIterator it; |
| 253 | frame_count++ < kSamplerFrameCount && !it.done(); |
| 254 | it.Advance()) { |
| 255 | JavaScriptFrame* frame = it.frame(); |
| 256 | JSFunction* function = JSFunction::cast(frame->function()); |
| 257 | |
| 258 | // Adjust threshold each time we have processed |
| 259 | // a certain number of ticks. |
| 260 | if (sampler_ticks_until_threshold_adjustment > 0) { |
| 261 | sampler_ticks_until_threshold_adjustment--; |
| 262 | if (sampler_ticks_until_threshold_adjustment <= 0) { |
| 263 | // If the threshold is not already at the minimum |
| 264 | // modify and reset the ticks until next adjustment. |
| 265 | if (sampler_threshold > kSamplerThresholdMin) { |
| 266 | sampler_threshold -= kSamplerThresholdDelta; |
| 267 | sampler_ticks_until_threshold_adjustment = |
| 268 | kSamplerTicksBetweenThresholdAdjustment; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (function->IsMarkedForLazyRecompilation()) { |
| 274 | Code* unoptimized = function->shared()->code(); |
| 275 | int nesting = unoptimized->allow_osr_at_loop_nesting_level(); |
| 276 | if (nesting == 0) AttemptOnStackReplacement(function); |
| 277 | int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker); |
| 278 | unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting); |
| 279 | } |
| 280 | |
| 281 | // Do not record non-optimizable functions. |
| 282 | if (!IsOptimizable(function)) continue; |
| 283 | samples[sample_count++] = function; |
| 284 | |
| 285 | int function_size = function->shared()->SourceSize(); |
| 286 | int threshold_size_factor = (function_size > kSizeLimit) |
| 287 | ? sampler_threshold_size_factor |
| 288 | : 1; |
| 289 | |
| 290 | int threshold = sampler_threshold * threshold_size_factor; |
| 291 | int current_js_ratio = NoBarrier_Load(&js_ratio); |
| 292 | |
| 293 | // Adjust threshold depending on the ratio of time spent |
| 294 | // in JS code. |
| 295 | if (current_js_ratio < 20) { |
| 296 | // If we spend less than 20% of the time in JS code, |
| 297 | // do not optimize. |
| 298 | continue; |
| 299 | } else if (current_js_ratio < 75) { |
| 300 | // Below 75% of time spent in JS code, only optimize very |
| 301 | // frequently used functions. |
| 302 | threshold *= 3; |
| 303 | } |
| 304 | |
| 305 | if (LookupSample(function) >= threshold) { |
| 306 | Optimize(function, false, 0); |
| 307 | CompilationCache::MarkForEagerOptimizing(Handle<JSFunction>(function)); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Add the collected functions as samples. It's important not to do |
| 312 | // this as part of collecting them because this will interfere with |
| 313 | // the sample lookup in case of recursive functions. |
| 314 | for (int i = 0; i < sample_count; i++) { |
| 315 | AddSample(samples[i], kSamplerFrameWeight[i]); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void RuntimeProfiler::OptimizeSoon(JSFunction* function) { |
| 321 | if (!IsOptimizable(function)) return; |
| 322 | PendingListNode* node = new PendingListNode(function); |
| 323 | node->set_next(optimize_soon_list); |
| 324 | optimize_soon_list = node; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 329 | static void UpdateStateRatio(SamplerState current_state) { |
| 330 | static const int kStateWindowSize = 128; |
| 331 | static SamplerState state_window[kStateWindowSize]; |
| 332 | static int state_window_position = 0; |
| 333 | static int state_counts[2] = { kStateWindowSize, 0 }; |
| 334 | |
| 335 | SamplerState old_state = state_window[state_window_position]; |
| 336 | state_counts[old_state]--; |
| 337 | state_window[state_window_position] = current_state; |
| 338 | state_counts[current_state]++; |
| 339 | ASSERT(IsPowerOf2(kStateWindowSize)); |
| 340 | state_window_position = (state_window_position + 1) & |
| 341 | (kStateWindowSize - 1); |
| 342 | NoBarrier_Store(&js_ratio, state_counts[IN_JS_STATE] * 100 / |
| 343 | kStateWindowSize); |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | |
| 348 | void RuntimeProfiler::NotifyTick() { |
| 349 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 350 | // Record state sample. |
| 351 | SamplerState state = Top::IsInJSState() |
| 352 | ? IN_JS_STATE |
| 353 | : IN_NON_JS_STATE; |
| 354 | UpdateStateRatio(state); |
| 355 | StackGuard::RequestRuntimeProfilerTick(); |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 360 | void RuntimeProfiler::Setup() { |
| 361 | ClearSampleBuffer(); |
| 362 | // If the ticker hasn't already started, make sure to do so to get |
| 363 | // the ticks for the runtime profiler. |
| 364 | if (IsEnabled()) Logger::EnsureTickerStarted(); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | void RuntimeProfiler::Reset() { |
| 369 | sampler_threshold = kSamplerThresholdInit; |
| 370 | sampler_ticks_until_threshold_adjustment = |
| 371 | kSamplerTicksBetweenThresholdAdjustment; |
| 372 | sampler_threshold_size_factor = kSamplerThresholdSizeFactorInit; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | void RuntimeProfiler::TearDown() { |
| 377 | // Nothing to do. |
| 378 | } |
| 379 | |
| 380 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 381 | int RuntimeProfiler::SamplerWindowSize() { |
| 382 | return kSamplerWindowSize; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 386 | // Update the pointers in the sampler window after a GC. |
| 387 | void RuntimeProfiler::UpdateSamplesAfterScavenge() { |
| 388 | for (int i = 0; i < kSamplerWindowSize; i++) { |
| 389 | Object* function = sampler_window[i]; |
| 390 | if (function != NULL && Heap::InNewSpace(function)) { |
| 391 | MapWord map_word = HeapObject::cast(function)->map_word(); |
| 392 | if (map_word.IsForwardingAddress()) { |
| 393 | sampler_window[i] = map_word.ToForwardingAddress(); |
| 394 | } else { |
| 395 | sampler_window[i] = NULL; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void RuntimeProfiler::RemoveDeadSamples() { |
| 403 | for (int i = 0; i < kSamplerWindowSize; i++) { |
| 404 | Object* function = sampler_window[i]; |
| 405 | if (function != NULL && !HeapObject::cast(function)->IsMarked()) { |
| 406 | sampler_window[i] = NULL; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | |
| 412 | void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
| 413 | for (int i = 0; i < kSamplerWindowSize; i++) { |
| 414 | visitor->VisitPointer(&sampler_window[i]); |
| 415 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
| 419 | bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { |
| 420 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 421 | static const int kNonJSTicksThreshold = 100; |
| 422 | // We suspend the runtime profiler thread when not running |
| 423 | // JavaScript. If the CPU profiler is active we must not do this |
| 424 | // because it samples both JavaScript and C++ code. |
| 425 | if (RuntimeProfiler::IsEnabled() && |
| 426 | !CpuProfiler::is_profiling() && |
| 427 | !(FLAG_prof && FLAG_prof_auto)) { |
| 428 | if (Top::IsInJSState()) { |
| 429 | non_js_ticks_ = 0; |
| 430 | } else { |
| 431 | if (non_js_ticks_ < kNonJSTicksThreshold) { |
| 432 | ++non_js_ticks_; |
| 433 | } else { |
| 434 | if (Top::WaitForJSState()) return true; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | #endif |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | } } // namespace v8::internal |