kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [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" |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 38 | #include "mark-compact.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 39 | #include "platform.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 40 | #include "scopeinfo.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 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 | // Optimization sampler constants. |
| 73 | static const int kSamplerFrameCount = 2; |
| 74 | static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 75 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 76 | static const int kSamplerTicksBetweenThresholdAdjustment = 32; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 77 | |
| 78 | static const int kSamplerThresholdInit = 3; |
| 79 | static const int kSamplerThresholdMin = 1; |
| 80 | static const int kSamplerThresholdDelta = 1; |
| 81 | |
| 82 | static const int kSamplerThresholdSizeFactorInit = 3; |
| 83 | static const int kSamplerThresholdSizeFactorMin = 1; |
| 84 | static const int kSamplerThresholdSizeFactorDelta = 1; |
| 85 | |
| 86 | static const int kSizeLimit = 1500; |
| 87 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 88 | |
| 89 | PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 90 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 91 | function_ = global_handles->Create(function); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 92 | start_ = OS::Ticks(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 93 | global_handles->MakeWeak(function_.location(), this, &WeakCallback); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | void PendingListNode::Destroy() { |
| 98 | if (!IsValid()) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 99 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
| 100 | global_handles->Destroy(function_.location()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 101 | function_= Handle<Object>::null(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void PendingListNode::WeakCallback(v8::Persistent<v8::Value>, void* data) { |
| 106 | reinterpret_cast<PendingListNode*>(data)->Destroy(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static bool IsOptimizable(JSFunction* function) { |
| 111 | Code* code = function->code(); |
| 112 | return code->kind() == Code::FUNCTION && code->optimizable(); |
| 113 | } |
| 114 | |
| 115 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 116 | Atomic32 RuntimeProfiler::state_ = 0; |
| 117 | // TODO(isolates): Create the semaphore lazily and clean it up when no |
| 118 | // longer required. |
| 119 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 120 | Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0); |
| 121 | #endif |
| 122 | |
| 123 | |
| 124 | RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
| 125 | : isolate_(isolate), |
| 126 | sampler_threshold_(kSamplerThresholdInit), |
| 127 | sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), |
| 128 | sampler_ticks_until_threshold_adjustment_( |
| 129 | kSamplerTicksBetweenThresholdAdjustment), |
| 130 | js_ratio_(0), |
| 131 | sampler_window_position_(0), |
| 132 | optimize_soon_list_(NULL), |
| 133 | state_window_position_(0) { |
| 134 | state_counts_[0] = kStateWindowSize; |
| 135 | state_counts_[1] = 0; |
| 136 | memset(state_window_, 0, sizeof(state_window_)); |
| 137 | ClearSampleBuffer(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | bool RuntimeProfiler::IsEnabled() { |
| 142 | return V8::UseCrankshaft() && FLAG_opt; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void RuntimeProfiler::Optimize(JSFunction* function, bool eager, int delay) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 147 | ASSERT(IsOptimizable(function)); |
| 148 | if (FLAG_trace_opt) { |
| 149 | PrintF("[marking (%s) ", eager ? "eagerly" : "lazily"); |
| 150 | function->PrintName(); |
| 151 | PrintF(" for recompilation"); |
| 152 | if (delay > 0) { |
| 153 | PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000); |
| 154 | } |
| 155 | PrintF("]\n"); |
| 156 | } |
| 157 | |
| 158 | // The next call to the function will trigger optimization. |
| 159 | function->MarkForLazyRecompilation(); |
| 160 | } |
| 161 | |
| 162 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 163 | void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 164 | // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
| 165 | // Debug::has_break_points(). |
| 166 | ASSERT(function->IsMarkedForLazyRecompilation()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 167 | if (!FLAG_use_osr || |
| 168 | isolate_->debug()->has_break_points() || |
| 169 | function->IsBuiltin()) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | SharedFunctionInfo* shared = function->shared(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 174 | // If the code is not optimizable or references context slots, don't try OSR. |
| 175 | if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) { |
| 176 | return; |
| 177 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 178 | |
| 179 | // We are not prepared to do OSR for a function that already has an |
| 180 | // allocated arguments object. The optimized code would bypass it for |
| 181 | // arguments accesses, which is unsound. Don't try OSR. |
| 182 | if (shared->scope_info()->HasArgumentsShadow()) return; |
| 183 | |
| 184 | // We're using on-stack replacement: patch the unoptimized code so that |
| 185 | // any back edge in any unoptimized frame will trigger on-stack |
| 186 | // replacement for that frame. |
| 187 | if (FLAG_trace_osr) { |
| 188 | PrintF("[patching stack checks in "); |
| 189 | function->PrintName(); |
| 190 | PrintF(" for on-stack replacement]\n"); |
| 191 | } |
| 192 | |
| 193 | // Get the stack check stub code object to match against. We aren't |
| 194 | // prepared to generate it, but we don't expect to have to. |
| 195 | StackCheckStub check_stub; |
| 196 | Object* check_code; |
| 197 | MaybeObject* maybe_check_code = check_stub.TryGetCode(); |
| 198 | if (maybe_check_code->ToObject(&check_code)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 199 | Code* replacement_code = |
| 200 | isolate_->builtins()->builtin(Builtins::OnStackReplacement); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 201 | Code* unoptimized_code = shared->code(); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 202 | Deoptimizer::PatchStackCheckCode(unoptimized_code, |
| 203 | Code::cast(check_code), |
| 204 | replacement_code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 209 | void RuntimeProfiler::ClearSampleBuffer() { |
| 210 | memset(sampler_window_, 0, sizeof(sampler_window_)); |
| 211 | memset(sampler_window_weight_, 0, sizeof(sampler_window_weight_)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 215 | int RuntimeProfiler::LookupSample(JSFunction* function) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 216 | int weight = 0; |
| 217 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 218 | Object* sample = sampler_window_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 219 | if (sample != NULL) { |
| 220 | if (function == sample) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 221 | weight += sampler_window_weight_[i]; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | return weight; |
| 226 | } |
| 227 | |
| 228 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 229 | void RuntimeProfiler::AddSample(JSFunction* function, int weight) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 230 | ASSERT(IsPowerOf2(kSamplerWindowSize)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 231 | sampler_window_[sampler_window_position_] = function; |
| 232 | sampler_window_weight_[sampler_window_position_] = weight; |
| 233 | sampler_window_position_ = (sampler_window_position_ + 1) & |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 234 | (kSamplerWindowSize - 1); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void RuntimeProfiler::OptimizeNow() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 239 | HandleScope scope(isolate_); |
| 240 | PendingListNode* current = optimize_soon_list_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 241 | while (current != NULL) { |
| 242 | PendingListNode* next = current->next(); |
| 243 | if (current->IsValid()) { |
| 244 | Handle<JSFunction> function = current->function(); |
| 245 | int delay = current->Delay(); |
| 246 | if (IsOptimizable(*function)) { |
| 247 | Optimize(*function, true, delay); |
| 248 | } |
| 249 | } |
| 250 | delete current; |
| 251 | current = next; |
| 252 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 253 | optimize_soon_list_ = NULL; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 254 | |
| 255 | // Run through the JavaScript frames and collect them. If we already |
| 256 | // have a sample of the function, we mark it for optimizations |
| 257 | // (eagerly or lazily). |
| 258 | JSFunction* samples[kSamplerFrameCount]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 259 | int sample_count = 0; |
| 260 | int frame_count = 0; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 261 | for (JavaScriptFrameIterator it; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 262 | frame_count++ < kSamplerFrameCount && !it.done(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 263 | it.Advance()) { |
| 264 | JavaScriptFrame* frame = it.frame(); |
| 265 | JSFunction* function = JSFunction::cast(frame->function()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 266 | |
| 267 | // Adjust threshold each time we have processed |
| 268 | // a certain number of ticks. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 269 | if (sampler_ticks_until_threshold_adjustment_ > 0) { |
| 270 | sampler_ticks_until_threshold_adjustment_--; |
| 271 | if (sampler_ticks_until_threshold_adjustment_ <= 0) { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 272 | // If the threshold is not already at the minimum |
| 273 | // modify and reset the ticks until next adjustment. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 274 | if (sampler_threshold_ > kSamplerThresholdMin) { |
| 275 | sampler_threshold_ -= kSamplerThresholdDelta; |
| 276 | sampler_ticks_until_threshold_adjustment_ = |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 277 | kSamplerTicksBetweenThresholdAdjustment; |
| 278 | } |
| 279 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 280 | } |
| 281 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 282 | if (function->IsMarkedForLazyRecompilation()) { |
| 283 | Code* unoptimized = function->shared()->code(); |
| 284 | int nesting = unoptimized->allow_osr_at_loop_nesting_level(); |
| 285 | if (nesting == 0) AttemptOnStackReplacement(function); |
| 286 | int new_nesting = Min(nesting + 1, Code::kMaxLoopNestingMarker); |
| 287 | unoptimized->set_allow_osr_at_loop_nesting_level(new_nesting); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Do not record non-optimizable functions. |
| 291 | if (!IsOptimizable(function)) continue; |
| 292 | samples[sample_count++] = function; |
| 293 | |
| 294 | int function_size = function->shared()->SourceSize(); |
| 295 | int threshold_size_factor = (function_size > kSizeLimit) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 296 | ? sampler_threshold_size_factor_ |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 297 | : 1; |
| 298 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 299 | int threshold = sampler_threshold_ * threshold_size_factor; |
| 300 | int current_js_ratio = NoBarrier_Load(&js_ratio_); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 301 | |
| 302 | // Adjust threshold depending on the ratio of time spent |
| 303 | // in JS code. |
| 304 | if (current_js_ratio < 20) { |
| 305 | // If we spend less than 20% of the time in JS code, |
| 306 | // do not optimize. |
| 307 | continue; |
| 308 | } else if (current_js_ratio < 75) { |
| 309 | // Below 75% of time spent in JS code, only optimize very |
| 310 | // frequently used functions. |
| 311 | threshold *= 3; |
| 312 | } |
| 313 | |
| 314 | if (LookupSample(function) >= threshold) { |
| 315 | Optimize(function, false, 0); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 316 | isolate_->compilation_cache()->MarkForEagerOptimizing( |
| 317 | Handle<JSFunction>(function)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | // Add the collected functions as samples. It's important not to do |
| 322 | // this as part of collecting them because this will interfere with |
| 323 | // the sample lookup in case of recursive functions. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 324 | for (int i = 0; i < sample_count; i++) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 325 | AddSample(samples[i], kSamplerFrameWeight[i]); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | void RuntimeProfiler::OptimizeSoon(JSFunction* function) { |
| 331 | if (!IsOptimizable(function)) return; |
| 332 | PendingListNode* node = new PendingListNode(function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 333 | node->set_next(optimize_soon_list_); |
| 334 | optimize_soon_list_ = node; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 338 | #ifdef ENABLE_LOGGING_AND_PROFILING |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 339 | void RuntimeProfiler::UpdateStateRatio(SamplerState current_state) { |
| 340 | SamplerState old_state = state_window_[state_window_position_]; |
| 341 | state_counts_[old_state]--; |
| 342 | state_window_[state_window_position_] = current_state; |
| 343 | state_counts_[current_state]++; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 344 | ASSERT(IsPowerOf2(kStateWindowSize)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 345 | state_window_position_ = (state_window_position_ + 1) & |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 346 | (kStateWindowSize - 1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 347 | NoBarrier_Store(&js_ratio_, state_counts_[IN_JS_STATE] * 100 / |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 348 | kStateWindowSize); |
| 349 | } |
| 350 | #endif |
| 351 | |
| 352 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 353 | void RuntimeProfiler::NotifyTick() { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 354 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 355 | // Record state sample. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 356 | SamplerState state = IsSomeIsolateInJS() |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 357 | ? IN_JS_STATE |
| 358 | : IN_NON_JS_STATE; |
| 359 | UpdateStateRatio(state); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 360 | isolate_->stack_guard()->RequestRuntimeProfilerTick(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 361 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 365 | void RuntimeProfiler::Setup() { |
| 366 | ClearSampleBuffer(); |
| 367 | // If the ticker hasn't already started, make sure to do so to get |
| 368 | // the ticks for the runtime profiler. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 369 | if (IsEnabled()) isolate_->logger()->EnsureTickerStarted(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
| 373 | void RuntimeProfiler::Reset() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 374 | sampler_threshold_ = kSamplerThresholdInit; |
| 375 | sampler_threshold_size_factor_ = kSamplerThresholdSizeFactorInit; |
| 376 | sampler_ticks_until_threshold_adjustment_ = |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 377 | kSamplerTicksBetweenThresholdAdjustment; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | |
| 381 | void RuntimeProfiler::TearDown() { |
| 382 | // Nothing to do. |
| 383 | } |
| 384 | |
| 385 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 386 | int RuntimeProfiler::SamplerWindowSize() { |
| 387 | return kSamplerWindowSize; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 391 | // Update the pointers in the sampler window after a GC. |
| 392 | void RuntimeProfiler::UpdateSamplesAfterScavenge() { |
| 393 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 394 | Object* function = sampler_window_[i]; |
| 395 | if (function != NULL && isolate_->heap()->InNewSpace(function)) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 396 | MapWord map_word = HeapObject::cast(function)->map_word(); |
| 397 | if (map_word.IsForwardingAddress()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 398 | sampler_window_[i] = map_word.ToForwardingAddress(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 399 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 400 | sampler_window_[i] = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 407 | void RuntimeProfiler::HandleWakeUp(Isolate* isolate) { |
| 408 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 409 | // The profiler thread must still be waiting. |
| 410 | ASSERT(NoBarrier_Load(&state_) >= 0); |
| 411 | // In IsolateEnteredJS we have already incremented the counter and |
| 412 | // undid the decrement done by the profiler thread. Increment again |
| 413 | // to get the right count of active isolates. |
| 414 | NoBarrier_AtomicIncrement(&state_, 1); |
| 415 | semaphore_->Signal(); |
| 416 | isolate->ResetEagerOptimizingData(); |
| 417 | #endif |
| 418 | } |
| 419 | |
| 420 | |
| 421 | bool RuntimeProfiler::IsSomeIsolateInJS() { |
| 422 | return NoBarrier_Load(&state_) > 0; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() { |
| 427 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 428 | Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1); |
| 429 | ASSERT(old_state >= -1); |
| 430 | if (old_state != 0) return false; |
| 431 | semaphore_->Wait(); |
| 432 | #endif |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown() { |
| 438 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 439 | semaphore_->Signal(); |
| 440 | #endif |
| 441 | } |
| 442 | |
| 443 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 444 | void RuntimeProfiler::RemoveDeadSamples() { |
| 445 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 446 | Object* function = sampler_window_[i]; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 447 | if (function != NULL && !HeapObject::cast(function)->IsMarked()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 448 | sampler_window_[i] = NULL; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
| 455 | for (int i = 0; i < kSamplerWindowSize; i++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 456 | visitor->VisitPointer(&sampler_window_[i]); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 457 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
| 461 | bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 462 | #ifdef ENABLE_LOGGING_AND_PROFILING |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 463 | static const int kNonJSTicksThreshold = 100; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 464 | if (RuntimeProfiler::IsSomeIsolateInJS()) { |
| 465 | non_js_ticks_ = 0; |
| 466 | } else { |
| 467 | if (non_js_ticks_ < kNonJSTicksThreshold) { |
| 468 | ++non_js_ticks_; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 469 | } else { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame^] | 470 | return RuntimeProfiler::WaitForSomeIsolateToEnterJS(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 473 | #endif |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | } } // namespace v8::internal |