Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: src/runtime-profiler.cc

Issue 7348008: Merge up to 8597 to experimental/gc from the bleeding edge. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/runtime-profiler.h ('k') | src/scanner.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
171 // Debug::has_break_points(). 111 // Debug::has_break_points().
172 ASSERT(function->IsMarkedForLazyRecompilation()); 112 ASSERT(function->IsMarkedForLazyRecompilation());
173 if (!FLAG_use_osr || 113 if (!FLAG_use_osr ||
174 isolate_->DebuggerHasBreakPoints() || 114 isolate_->DebuggerHasBreakPoints() ||
175 function->IsBuiltin()) { 115 function->IsBuiltin()) {
176 return; 116 return;
177 } 117 }
178 118
179 SharedFunctionInfo* shared = function->shared(); 119 SharedFunctionInfo* shared = function->shared();
180 // If the code is not optimizable or references context slots, don't try OSR. 120 // If the code is not optimizable or references context slots, don't try OSR.
181 if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) { 121 if (!shared->code()->optimizable() || !shared->allows_lazy_compilation()) {
182 return; 122 return;
183 } 123 }
184 124
185 // We are not prepared to do OSR for a function that already has an 125 // We are not prepared to do OSR for a function that already has an
186 // allocated arguments object. The optimized code would bypass it for 126 // allocated arguments object. The optimized code would bypass it for
187 // arguments accesses, which is unsound. Don't try OSR. 127 // arguments accesses, which is unsound. Don't try OSR.
188 if (shared->scope_info()->HasArgumentsShadow()) return; 128 if (shared->uses_arguments()) return;
189 129
190 // We're using on-stack replacement: patch the unoptimized code so that 130 // We're using on-stack replacement: patch the unoptimized code so that
191 // any back edge in any unoptimized frame will trigger on-stack 131 // any back edge in any unoptimized frame will trigger on-stack
192 // replacement for that frame. 132 // replacement for that frame.
193 if (FLAG_trace_osr) { 133 if (FLAG_trace_osr) {
194 PrintF("[patching stack checks in "); 134 PrintF("[patching stack checks in ");
195 function->PrintName(); 135 function->PrintName();
196 PrintF(" for on-stack replacement]\n"); 136 PrintF(" for on-stack replacement]\n");
197 } 137 }
198 138
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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() {
438 #ifdef ENABLE_LOGGING_AND_PROFILING 316 #ifdef ENABLE_LOGGING_AND_PROFILING
439 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1); 317 Atomic32 old_state = NoBarrier_CompareAndSwap(&state_, 0, -1);
440 ASSERT(old_state >= -1); 318 ASSERT(old_state >= -1);
441 if (old_state != 0) return false; 319 if (old_state != 0) return false;
442 semaphore_->Wait(); 320 semaphore_->Wait();
443 #endif 321 #endif
444 return true; 322 return true;
445 } 323 }
446 324
447 325
448 void RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown() { 326 void RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(Thread* thread) {
449 #ifdef ENABLE_LOGGING_AND_PROFILING 327 #ifdef ENABLE_LOGGING_AND_PROFILING
450 semaphore_->Signal(); 328 // Do a fake increment. If the profiler is waiting on the semaphore,
329 // the returned state is 0, which can be left as an initial state in
330 // case profiling is restarted later. If the profiler is not
331 // waiting, the increment will prevent it from waiting, but has to
332 // be undone after the profiler is stopped.
333 Atomic32 new_state = NoBarrier_AtomicIncrement(&state_, 1);
334 ASSERT(new_state >= 0);
335 if (new_state == 0) {
336 // The profiler thread is waiting. Wake it up. It must check for
337 // stop conditions before attempting to wait again.
338 semaphore_->Signal();
339 }
340 thread->Join();
341 // The profiler thread is now stopped. Undo the increment in case it
342 // was not waiting.
343 if (new_state != 0) {
344 NoBarrier_AtomicIncrement(&state_, -1);
345 }
451 #endif 346 #endif
452 } 347 }
453 348
454 349
455 void RuntimeProfiler::RemoveDeadSamples() { 350 void RuntimeProfiler::RemoveDeadSamples() {
456 for (int i = 0; i < kSamplerWindowSize; i++) { 351 for (int i = 0; i < kSamplerWindowSize; i++) {
457 Object* function = sampler_window_[i]; 352 Object* function = sampler_window_[i];
458 if (function != NULL && 353 if (function != NULL &&
459 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) { 354 !Marking::MarkBitFrom(HeapObject::cast(function)).Get()) {
460 sampler_window_[i] = NULL; 355 sampler_window_[i] = NULL;
461 } 356 }
462 } 357 }
463 } 358 }
464 359
465 360
466 void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) { 361 void RuntimeProfiler::UpdateSamplesAfterCompact(ObjectVisitor* visitor) {
467 for (int i = 0; i < kSamplerWindowSize; i++) { 362 for (int i = 0; i < kSamplerWindowSize; i++) {
468 visitor->VisitPointer(&sampler_window_[i]); 363 visitor->VisitPointer(&sampler_window_[i]);
469 } 364 }
470 } 365 }
471 366
472 367
473 bool RuntimeProfilerRateLimiter::SuspendIfNecessary() { 368 bool RuntimeProfilerRateLimiter::SuspendIfNecessary() {
474 #ifdef ENABLE_LOGGING_AND_PROFILING 369 #ifdef ENABLE_LOGGING_AND_PROFILING
475 static const int kNonJSTicksThreshold = 100; 370 if (!RuntimeProfiler::IsSomeIsolateInJS()) {
476 if (RuntimeProfiler::IsSomeIsolateInJS()) { 371 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
477 non_js_ticks_ = 0;
478 } else {
479 if (non_js_ticks_ < kNonJSTicksThreshold) {
480 ++non_js_ticks_;
481 } else {
482 return RuntimeProfiler::WaitForSomeIsolateToEnterJS();
483 }
484 } 372 }
485 #endif 373 #endif
486 return false; 374 return false;
487 } 375 }
488 376
489 377
490 } } // namespace v8::internal 378 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime-profiler.h ('k') | src/scanner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698