OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "execution.h" | 36 #include "execution.h" |
37 #include "global-handles.h" | 37 #include "global-handles.h" |
38 #include "mark-compact.h" | 38 #include "mark-compact.h" |
39 #include "platform.h" | 39 #include "platform.h" |
40 #include "scopeinfo.h" | 40 #include "scopeinfo.h" |
41 | 41 |
42 namespace v8 { | 42 namespace v8 { |
43 namespace internal { | 43 namespace internal { |
44 | 44 |
45 | 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. | 46 // Optimization sampler constants. |
73 static const int kSamplerFrameCount = 2; | 47 static const int kSamplerFrameCount = 2; |
74 static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; | 48 static const int kSamplerFrameWeight[kSamplerFrameCount] = { 2, 1 }; |
75 | 49 |
76 static const int kSamplerTicksBetweenThresholdAdjustment = 32; | 50 static const int kSamplerTicksBetweenThresholdAdjustment = 32; |
77 | 51 |
78 static const int kSamplerThresholdInit = 3; | 52 static const int kSamplerThresholdInit = 3; |
79 static const int kSamplerThresholdMin = 1; | 53 static const int kSamplerThresholdMin = 1; |
80 static const int kSamplerThresholdDelta = 1; | 54 static const int kSamplerThresholdDelta = 1; |
81 | 55 |
82 static const int kSamplerThresholdSizeFactorInit = 3; | 56 static const int kSamplerThresholdSizeFactorInit = 3; |
83 static const int kSamplerThresholdSizeFactorMin = 1; | |
84 static const int kSamplerThresholdSizeFactorDelta = 1; | |
85 | 57 |
86 static const int kSizeLimit = 1500; | 58 static const int kSizeLimit = 1500; |
87 | 59 |
88 | 60 |
89 PendingListNode::PendingListNode(JSFunction* function) : next_(NULL) { | |
90 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | |
91 function_ = global_handles->Create(function); | |
92 start_ = OS::Ticks(); | |
93 global_handles->MakeWeak(function_.location(), this, &WeakCallback); | |
94 } | |
95 | |
96 | |
97 void PendingListNode::Destroy() { | |
98 if (!IsValid()) return; | |
99 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | |
100 global_handles->Destroy(function_.location()); | |
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 Atomic32 RuntimeProfiler::state_ = 0; | 61 Atomic32 RuntimeProfiler::state_ = 0; |
111 // TODO(isolates): Create the semaphore lazily and clean it up when no | 62 // TODO(isolates): Create the semaphore lazily and clean it up when no |
112 // longer required. | 63 // longer required. |
113 #ifdef ENABLE_LOGGING_AND_PROFILING | 64 #ifdef ENABLE_LOGGING_AND_PROFILING |
114 Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0); | 65 Semaphore* RuntimeProfiler::semaphore_ = OS::CreateSemaphore(0); |
115 #endif | 66 #endif |
116 | 67 |
117 #ifdef DEBUG | 68 #ifdef DEBUG |
118 bool RuntimeProfiler::has_been_globally_setup_ = false; | 69 bool RuntimeProfiler::has_been_globally_setup_ = false; |
119 #endif | 70 #endif |
120 bool RuntimeProfiler::enabled_ = false; | 71 bool RuntimeProfiler::enabled_ = false; |
121 | 72 |
122 | 73 |
123 RuntimeProfiler::RuntimeProfiler(Isolate* isolate) | 74 RuntimeProfiler::RuntimeProfiler(Isolate* isolate) |
124 : isolate_(isolate), | 75 : isolate_(isolate), |
125 sampler_threshold_(kSamplerThresholdInit), | 76 sampler_threshold_(kSamplerThresholdInit), |
126 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), | 77 sampler_threshold_size_factor_(kSamplerThresholdSizeFactorInit), |
127 sampler_ticks_until_threshold_adjustment_( | 78 sampler_ticks_until_threshold_adjustment_( |
128 kSamplerTicksBetweenThresholdAdjustment), | 79 kSamplerTicksBetweenThresholdAdjustment), |
129 js_ratio_(0), | 80 sampler_window_position_(0) { |
130 sampler_window_position_(0), | |
131 optimize_soon_list_(NULL), | |
132 state_window_position_(0), | |
133 state_window_ticks_(0) { | |
134 state_counts_[IN_NON_JS_STATE] = kStateWindowSize; | |
135 state_counts_[IN_JS_STATE] = 0; | |
136 STATIC_ASSERT(IN_NON_JS_STATE == 0); | |
137 memset(state_window_, 0, sizeof(state_window_)); | |
138 ClearSampleBuffer(); | 81 ClearSampleBuffer(); |
139 } | 82 } |
140 | 83 |
141 | 84 |
142 void RuntimeProfiler::GlobalSetup() { | 85 void RuntimeProfiler::GlobalSetup() { |
143 ASSERT(!has_been_globally_setup_); | 86 ASSERT(!has_been_globally_setup_); |
144 enabled_ = V8::UseCrankshaft() && FLAG_opt; | 87 enabled_ = V8::UseCrankshaft() && FLAG_opt; |
145 #ifdef DEBUG | 88 #ifdef DEBUG |
146 has_been_globally_setup_ = true; | 89 has_been_globally_setup_ = true; |
147 #endif | 90 #endif |
148 } | 91 } |
149 | 92 |
150 | 93 |
151 void RuntimeProfiler::Optimize(JSFunction* function, bool eager, int delay) { | 94 void RuntimeProfiler::Optimize(JSFunction* function) { |
152 ASSERT(function->IsOptimizable()); | 95 ASSERT(function->IsOptimizable()); |
153 if (FLAG_trace_opt) { | 96 if (FLAG_trace_opt) { |
154 PrintF("[marking (%s) ", eager ? "eagerly" : "lazily"); | 97 PrintF("[marking "); |
155 function->PrintName(); | 98 function->PrintName(); |
156 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address())); | 99 PrintF(" 0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(function->address())); |
157 PrintF(" for recompilation"); | 100 PrintF(" for recompilation"); |
158 if (delay > 0) { | |
159 PrintF(" (delayed %0.3f ms)", static_cast<double>(delay) / 1000); | |
160 } | |
161 PrintF("]\n"); | 101 PrintF("]\n"); |
162 } | 102 } |
163 | 103 |
164 // The next call to the function will trigger optimization. | 104 // The next call to the function will trigger optimization. |
165 function->MarkForLazyRecompilation(); | 105 function->MarkForLazyRecompilation(); |
166 } | 106 } |
167 | 107 |
168 | 108 |
169 void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { | 109 void RuntimeProfiler::AttemptOnStackReplacement(JSFunction* function) { |
170 // See AlwaysFullCompiler (in compiler.cc) comment on why we need | 110 // See AlwaysFullCompiler (in compiler.cc) comment on why we need |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 ASSERT(IsPowerOf2(kSamplerWindowSize)); | 176 ASSERT(IsPowerOf2(kSamplerWindowSize)); |
237 sampler_window_[sampler_window_position_] = function; | 177 sampler_window_[sampler_window_position_] = function; |
238 sampler_window_weight_[sampler_window_position_] = weight; | 178 sampler_window_weight_[sampler_window_position_] = weight; |
239 sampler_window_position_ = (sampler_window_position_ + 1) & | 179 sampler_window_position_ = (sampler_window_position_ + 1) & |
240 (kSamplerWindowSize - 1); | 180 (kSamplerWindowSize - 1); |
241 } | 181 } |
242 | 182 |
243 | 183 |
244 void RuntimeProfiler::OptimizeNow() { | 184 void RuntimeProfiler::OptimizeNow() { |
245 HandleScope scope(isolate_); | 185 HandleScope scope(isolate_); |
246 PendingListNode* current = optimize_soon_list_; | |
247 while (current != NULL) { | |
248 PendingListNode* next = current->next(); | |
249 if (current->IsValid()) { | |
250 Handle<JSFunction> function = current->function(); | |
251 int delay = current->Delay(); | |
252 if (function->IsOptimizable()) { | |
253 Optimize(*function, true, delay); | |
254 } | |
255 } | |
256 delete current; | |
257 current = next; | |
258 } | |
259 optimize_soon_list_ = NULL; | |
260 | 186 |
261 // Run through the JavaScript frames and collect them. If we already | 187 // Run through the JavaScript frames and collect them. If we already |
262 // have a sample of the function, we mark it for optimizations | 188 // have a sample of the function, we mark it for optimizations |
263 // (eagerly or lazily). | 189 // (eagerly or lazily). |
264 JSFunction* samples[kSamplerFrameCount]; | 190 JSFunction* samples[kSamplerFrameCount]; |
265 int sample_count = 0; | 191 int sample_count = 0; |
266 int frame_count = 0; | 192 int frame_count = 0; |
267 for (JavaScriptFrameIterator it(isolate_); | 193 for (JavaScriptFrameIterator it(isolate_); |
268 frame_count++ < kSamplerFrameCount && !it.done(); | 194 frame_count++ < kSamplerFrameCount && !it.done(); |
269 it.Advance()) { | 195 it.Advance()) { |
(...skipping 26 matching lines...) Expand all Loading... |
296 // Do not record non-optimizable functions. | 222 // Do not record non-optimizable functions. |
297 if (!function->IsOptimizable()) continue; | 223 if (!function->IsOptimizable()) continue; |
298 samples[sample_count++] = function; | 224 samples[sample_count++] = function; |
299 | 225 |
300 int function_size = function->shared()->SourceSize(); | 226 int function_size = function->shared()->SourceSize(); |
301 int threshold_size_factor = (function_size > kSizeLimit) | 227 int threshold_size_factor = (function_size > kSizeLimit) |
302 ? sampler_threshold_size_factor_ | 228 ? sampler_threshold_size_factor_ |
303 : 1; | 229 : 1; |
304 | 230 |
305 int threshold = sampler_threshold_ * threshold_size_factor; | 231 int threshold = sampler_threshold_ * threshold_size_factor; |
306 int current_js_ratio = NoBarrier_Load(&js_ratio_); | |
307 | |
308 // Adjust threshold depending on the ratio of time spent | |
309 // in JS code. | |
310 if (current_js_ratio < 20) { | |
311 // If we spend less than 20% of the time in JS code, | |
312 // do not optimize. | |
313 continue; | |
314 } else if (current_js_ratio < 75) { | |
315 // Below 75% of time spent in JS code, only optimize very | |
316 // frequently used functions. | |
317 threshold *= 3; | |
318 } | |
319 | 232 |
320 if (LookupSample(function) >= threshold) { | 233 if (LookupSample(function) >= threshold) { |
321 Optimize(function, false, 0); | 234 Optimize(function); |
322 isolate_->compilation_cache()->MarkForEagerOptimizing( | |
323 Handle<JSFunction>(function)); | |
324 } | 235 } |
325 } | 236 } |
326 | 237 |
327 // Add the collected functions as samples. It's important not to do | 238 // Add the collected functions as samples. It's important not to do |
328 // this as part of collecting them because this will interfere with | 239 // this as part of collecting them because this will interfere with |
329 // the sample lookup in case of recursive functions. | 240 // the sample lookup in case of recursive functions. |
330 for (int i = 0; i < sample_count; i++) { | 241 for (int i = 0; i < sample_count; i++) { |
331 AddSample(samples[i], kSamplerFrameWeight[i]); | 242 AddSample(samples[i], kSamplerFrameWeight[i]); |
332 } | 243 } |
333 } | 244 } |
334 | 245 |
335 | 246 |
336 void RuntimeProfiler::OptimizeSoon(JSFunction* function) { | |
337 if (!function->IsOptimizable()) return; | |
338 PendingListNode* node = new PendingListNode(function); | |
339 node->set_next(optimize_soon_list_); | |
340 optimize_soon_list_ = node; | |
341 } | |
342 | |
343 | |
344 #ifdef ENABLE_LOGGING_AND_PROFILING | |
345 void RuntimeProfiler::UpdateStateRatio(SamplerState current_state) { | |
346 SamplerState old_state = state_window_[state_window_position_]; | |
347 state_counts_[old_state]--; | |
348 state_window_[state_window_position_] = current_state; | |
349 state_counts_[current_state]++; | |
350 ASSERT(IsPowerOf2(kStateWindowSize)); | |
351 state_window_position_ = (state_window_position_ + 1) & | |
352 (kStateWindowSize - 1); | |
353 // Note: to calculate correct ratio we have to track how many valid | |
354 // ticks are actually in the state window, because on profiler | |
355 // startup this number can be less than the window size. | |
356 state_window_ticks_ = Min(kStateWindowSize, state_window_ticks_ + 1); | |
357 NoBarrier_Store(&js_ratio_, state_counts_[IN_JS_STATE] * 100 / | |
358 state_window_ticks_); | |
359 } | |
360 #endif | |
361 | |
362 | |
363 void RuntimeProfiler::NotifyTick() { | 247 void RuntimeProfiler::NotifyTick() { |
364 #ifdef ENABLE_LOGGING_AND_PROFILING | 248 #ifdef ENABLE_LOGGING_AND_PROFILING |
365 // Record state sample. | |
366 SamplerState state = IsSomeIsolateInJS() | |
367 ? IN_JS_STATE | |
368 : IN_NON_JS_STATE; | |
369 UpdateStateRatio(state); | |
370 isolate_->stack_guard()->RequestRuntimeProfilerTick(); | 249 isolate_->stack_guard()->RequestRuntimeProfilerTick(); |
371 #endif | 250 #endif |
372 } | 251 } |
373 | 252 |
374 | 253 |
375 void RuntimeProfiler::Setup() { | 254 void RuntimeProfiler::Setup() { |
376 ASSERT(has_been_globally_setup_); | 255 ASSERT(has_been_globally_setup_); |
377 ClearSampleBuffer(); | 256 ClearSampleBuffer(); |
378 // If the ticker hasn't already started, make sure to do so to get | 257 // If the ticker hasn't already started, make sure to do so to get |
379 // the ticks for the runtime profiler. | 258 // the ticks for the runtime profiler. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 | 296 |
418 void RuntimeProfiler::HandleWakeUp(Isolate* isolate) { | 297 void RuntimeProfiler::HandleWakeUp(Isolate* isolate) { |
419 #ifdef ENABLE_LOGGING_AND_PROFILING | 298 #ifdef ENABLE_LOGGING_AND_PROFILING |
420 // The profiler thread must still be waiting. | 299 // The profiler thread must still be waiting. |
421 ASSERT(NoBarrier_Load(&state_) >= 0); | 300 ASSERT(NoBarrier_Load(&state_) >= 0); |
422 // In IsolateEnteredJS we have already incremented the counter and | 301 // In IsolateEnteredJS we have already incremented the counter and |
423 // undid the decrement done by the profiler thread. Increment again | 302 // undid the decrement done by the profiler thread. Increment again |
424 // to get the right count of active isolates. | 303 // to get the right count of active isolates. |
425 NoBarrier_AtomicIncrement(&state_, 1); | 304 NoBarrier_AtomicIncrement(&state_, 1); |
426 semaphore_->Signal(); | 305 semaphore_->Signal(); |
427 isolate->ResetEagerOptimizingData(); | |
428 #endif | 306 #endif |
429 } | 307 } |
430 | 308 |
431 | 309 |
432 bool RuntimeProfiler::IsSomeIsolateInJS() { | 310 bool RuntimeProfiler::IsSomeIsolateInJS() { |
433 return NoBarrier_Load(&state_) > 0; | 311 return NoBarrier_Load(&state_) > 0; |
434 } | 312 } |
435 | 313 |
436 | 314 |
437 bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() { | 315 bool RuntimeProfiler::WaitForSomeIsolateToEnterJS() { |
(...skipping 26 matching lines...) Expand all Loading... |
464 | 342 |
465 void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { | 343 void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { |
466 for (int i = 0; i < kSamplerWindowSize; i++) { | 344 for (int i = 0; i < kSamplerWindowSize; i++) { |
467 visitor->VisitPointer(&sampler_window_[i]); | 345 visitor->VisitPointer(&sampler_window_[i]); |
468 } | 346 } |
469 } | 347 } |
470 | 348 |
471 | 349 |
472 bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { | 350 bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { |
473 #ifdef ENABLE_LOGGING_AND_PROFILING | 351 #ifdef ENABLE_LOGGING_AND_PROFILING |
474 static const int kNonJSTicksThreshold = 100; | 352 if (!RuntimeProfiler::IsSomeIsolateInJS()) { |
475 if (RuntimeProfiler::IsSomeIsolateInJS()) { | 353 return RuntimeProfiler::WaitForSomeIsolateToEnterJS(); |
476 non_js_ticks_ = 0; | |
477 } else { | |
478 if (non_js_ticks_ < kNonJSTicksThreshold) { | |
479 ++non_js_ticks_; | |
480 } else { | |
481 return RuntimeProfiler::WaitForSomeIsolateToEnterJS(); | |
482 } | |
483 } | 354 } |
484 #endif | 355 #endif |
485 return false; | 356 return false; |
486 } | 357 } |
487 | 358 |
488 | 359 |
489 } } // namespace v8::internal | 360 } } // namespace v8::internal |
OLD | NEW |