| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 | 413 |
| 414 | 414 |
| 415 class SamplerThread : public Thread { | 415 class SamplerThread : public Thread { |
| 416 public: | 416 public: |
| 417 static const int kSamplerThreadStackSize = 64 * KB; | 417 static const int kSamplerThreadStackSize = 64 * KB; |
| 418 | 418 |
| 419 explicit SamplerThread(int interval) | 419 explicit SamplerThread(int interval) |
| 420 : Thread(Thread::Options("SamplerThread", kSamplerThreadStackSize)), | 420 : Thread(Thread::Options("SamplerThread", kSamplerThreadStackSize)), |
| 421 interval_(interval) {} | 421 interval_(interval) {} |
| 422 | 422 |
| 423 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 423 static void SetUp() { if (!mutex_) mutex_ = new Mutex(); } |
| 424 static void TearDown() { delete mutex_; } | 424 static void TearDown() { delete mutex_; mutex_ = NULL; } |
| 425 | 425 |
| 426 static void AddActiveSampler(Sampler* sampler) { | 426 static void AddActiveSampler(Sampler* sampler) { |
| 427 bool need_to_start = false; | 427 bool need_to_start = false; |
| 428 ScopedLock lock(mutex_); | 428 LockGuard<Mutex> lock_guard(mutex_); |
| 429 if (instance_ == NULL) { | 429 if (instance_ == NULL) { |
| 430 // Start a thread that will send SIGPROF signal to VM threads, | 430 // Start a thread that will send SIGPROF signal to VM threads, |
| 431 // when CPU profiling will be enabled. | 431 // when CPU profiling will be enabled. |
| 432 instance_ = new SamplerThread(sampler->interval()); | 432 instance_ = new SamplerThread(sampler->interval()); |
| 433 need_to_start = true; | 433 need_to_start = true; |
| 434 } | 434 } |
| 435 | 435 |
| 436 ASSERT(sampler->IsActive()); | 436 ASSERT(sampler->IsActive()); |
| 437 ASSERT(!instance_->active_samplers_.Contains(sampler)); | 437 ASSERT(!instance_->active_samplers_.Contains(sampler)); |
| 438 ASSERT(instance_->interval_ == sampler->interval()); | 438 ASSERT(instance_->interval_ == sampler->interval()); |
| 439 instance_->active_samplers_.Add(sampler); | 439 instance_->active_samplers_.Add(sampler); |
| 440 | 440 |
| 441 #if defined(USE_SIGNALS) | 441 #if defined(USE_SIGNALS) |
| 442 SignalHandler::EnsureInstalled(); | 442 SignalHandler::EnsureInstalled(); |
| 443 #endif | 443 #endif |
| 444 if (need_to_start) instance_->StartSynchronously(); | 444 if (need_to_start) instance_->StartSynchronously(); |
| 445 } | 445 } |
| 446 | 446 |
| 447 static void RemoveActiveSampler(Sampler* sampler) { | 447 static void RemoveActiveSampler(Sampler* sampler) { |
| 448 SamplerThread* instance_to_remove = NULL; | 448 SamplerThread* instance_to_remove = NULL; |
| 449 { | 449 { |
| 450 ScopedLock lock(mutex_); | 450 LockGuard<Mutex> lock_guard(mutex_); |
| 451 | 451 |
| 452 ASSERT(sampler->IsActive()); | 452 ASSERT(sampler->IsActive()); |
| 453 bool removed = instance_->active_samplers_.RemoveElement(sampler); | 453 bool removed = instance_->active_samplers_.RemoveElement(sampler); |
| 454 ASSERT(removed); | 454 ASSERT(removed); |
| 455 USE(removed); | 455 USE(removed); |
| 456 | 456 |
| 457 // We cannot delete the instance immediately as we need to Join() the | 457 // We cannot delete the instance immediately as we need to Join() the |
| 458 // thread but we are holding mutex_ and the thread may try to acquire it. | 458 // thread but we are holding mutex_ and the thread may try to acquire it. |
| 459 if (instance_->active_samplers_.is_empty()) { | 459 if (instance_->active_samplers_.is_empty()) { |
| 460 instance_to_remove = instance_; | 460 instance_to_remove = instance_; |
| 461 instance_ = NULL; | 461 instance_ = NULL; |
| 462 #if defined(USE_SIGNALS) | 462 #if defined(USE_SIGNALS) |
| 463 SignalHandler::Restore(); | 463 SignalHandler::Restore(); |
| 464 #endif | 464 #endif |
| 465 } | 465 } |
| 466 } | 466 } |
| 467 | 467 |
| 468 if (!instance_to_remove) return; | 468 if (!instance_to_remove) return; |
| 469 instance_to_remove->Join(); | 469 instance_to_remove->Join(); |
| 470 delete instance_to_remove; | 470 delete instance_to_remove; |
| 471 } | 471 } |
| 472 | 472 |
| 473 // Implement Thread::Run(). | 473 // Implement Thread::Run(). |
| 474 virtual void Run() { | 474 virtual void Run() { |
| 475 while (true) { | 475 while (true) { |
| 476 { | 476 { |
| 477 ScopedLock lock(mutex_); | 477 LockGuard<Mutex> lock_guard(mutex_); |
| 478 if (active_samplers_.is_empty()) break; | 478 if (active_samplers_.is_empty()) break; |
| 479 // When CPU profiling is enabled both JavaScript and C++ code is | 479 // When CPU profiling is enabled both JavaScript and C++ code is |
| 480 // profiled. We must not suspend. | 480 // profiled. We must not suspend. |
| 481 for (int i = 0; i < active_samplers_.length(); ++i) { | 481 for (int i = 0; i < active_samplers_.length(); ++i) { |
| 482 Sampler* sampler = active_samplers_.at(i); | 482 Sampler* sampler = active_samplers_.at(i); |
| 483 if (!sampler->isolate()->IsInitialized()) continue; | 483 if (!sampler->isolate()->IsInitialized()) continue; |
| 484 if (!sampler->IsProfiling()) continue; | 484 if (!sampler->IsProfiling()) continue; |
| 485 sampler->DoSample(); | 485 sampler->DoSample(); |
| 486 } | 486 } |
| 487 } | 487 } |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 #endif // USE_SIMULATOR | 659 #endif // USE_SIMULATOR |
| 660 SampleStack(state); | 660 SampleStack(state); |
| 661 } | 661 } |
| 662 ResumeThread(profiled_thread); | 662 ResumeThread(profiled_thread); |
| 663 } | 663 } |
| 664 | 664 |
| 665 #endif // USE_SIGNALS | 665 #endif // USE_SIGNALS |
| 666 | 666 |
| 667 | 667 |
| 668 } } // namespace v8::internal | 668 } } // namespace v8::internal |
| OLD | NEW |