| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/devtools/v8_sampling_profiler.h" | 5 #include "content/renderer/devtools/v8_sampling_profiler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/cancellation_flag.h" | 14 #include "base/synchronization/cancellation_flag.h" |
| 15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
| 18 #include "base/trace_event/trace_event_argument.h" | 17 #include "base/trace_event/trace_event_argument.h" |
| 19 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 20 #include "content/renderer/devtools/lock_free_circular_queue.h" | 19 #include "content/renderer/devtools/lock_free_circular_queue.h" |
| 21 #include "content/renderer/render_thread_impl.h" | 20 #include "content/renderer/render_thread_impl.h" |
| 22 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 23 | 22 |
| 24 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
| 25 #include <signal.h> | 24 #include <signal.h> |
| 26 #define USE_SIGNALS | 25 #define USE_SIGNALS |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 } | 572 } |
| 574 | 573 |
| 575 void V8SamplingThread::Stop() { | 574 void V8SamplingThread::Stop() { |
| 576 cancellation_flag_.Set(); | 575 cancellation_flag_.Set(); |
| 577 base::PlatformThread::Join(sampling_thread_handle_); | 576 base::PlatformThread::Join(sampling_thread_handle_); |
| 578 } | 577 } |
| 579 | 578 |
| 580 V8SamplingProfiler::V8SamplingProfiler(bool underTest) | 579 V8SamplingProfiler::V8SamplingProfiler(bool underTest) |
| 581 : sampling_thread_(nullptr), | 580 : sampling_thread_(nullptr), |
| 582 render_thread_sampler_(Sampler::CreateForCurrentThread()), | 581 render_thread_sampler_(Sampler::CreateForCurrentThread()), |
| 583 task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 582 weak_factory_(this) { |
| 584 DCHECK(underTest || RenderThreadImpl::current()); | 583 DCHECK(underTest || RenderThreadImpl::current()); |
| 585 // Force the "v8.cpu_profile*" categories to show up in the trace viewer. | 584 // Force the "v8.cpu_profile*" categories to show up in the trace viewer. |
| 586 TraceLog::GetCategoryGroupEnabled( | 585 TraceLog::GetCategoryGroupEnabled( |
| 587 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")); | 586 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile")); |
| 588 TraceLog::GetCategoryGroupEnabled( | 587 TraceLog::GetCategoryGroupEnabled( |
| 589 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile.hires")); | 588 TRACE_DISABLED_BY_DEFAULT("v8.cpu_profile.hires")); |
| 590 TraceLog::GetInstance()->AddEnabledStateObserver(this); | 589 TraceLog::GetInstance()->AddAsyncEnabledStateObserver( |
| 590 weak_factory_.GetWeakPtr()); |
| 591 } | 591 } |
| 592 | 592 |
| 593 V8SamplingProfiler::~V8SamplingProfiler() { | 593 V8SamplingProfiler::~V8SamplingProfiler() { |
| 594 TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | 594 TraceLog::GetInstance()->RemoveAsyncEnabledStateObserver(this); |
| 595 DCHECK(!sampling_thread_.get()); | 595 DCHECK(!sampling_thread_.get()); |
| 596 } | 596 } |
| 597 | 597 |
| 598 void V8SamplingProfiler::StartSamplingThread() { | 598 void V8SamplingProfiler::StartSamplingThread() { |
| 599 DCHECK(!sampling_thread_.get()); | 599 DCHECK(!sampling_thread_.get()); |
| 600 sampling_thread_.reset(new V8SamplingThread( | 600 sampling_thread_.reset(new V8SamplingThread( |
| 601 render_thread_sampler_.get(), waitable_event_for_testing_.get())); | 601 render_thread_sampler_.get(), waitable_event_for_testing_.get())); |
| 602 sampling_thread_->Start(); | 602 sampling_thread_->Start(); |
| 603 } | 603 } |
| 604 | 604 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 617 return; | 617 return; |
| 618 | 618 |
| 619 // Do not enable sampling profiler in continuous mode, as losing | 619 // Do not enable sampling profiler in continuous mode, as losing |
| 620 // Jit code events may not be afforded. | 620 // Jit code events may not be afforded. |
| 621 // TODO(alph): add support of infinite recording of meta trace events. | 621 // TODO(alph): add support of infinite recording of meta trace events. |
| 622 base::trace_event::TraceRecordMode record_mode = | 622 base::trace_event::TraceRecordMode record_mode = |
| 623 TraceLog::GetInstance()->GetCurrentTraceConfig().GetTraceRecordMode(); | 623 TraceLog::GetInstance()->GetCurrentTraceConfig().GetTraceRecordMode(); |
| 624 if (record_mode == base::trace_event::TraceRecordMode::RECORD_CONTINUOUSLY) | 624 if (record_mode == base::trace_event::TraceRecordMode::RECORD_CONTINUOUSLY) |
| 625 return; | 625 return; |
| 626 | 626 |
| 627 task_runner_->PostTask(FROM_HERE, | 627 StartSamplingThread(); |
| 628 base::Bind(&V8SamplingProfiler::StartSamplingThread, | |
| 629 base::Unretained(this))); | |
| 630 } | 628 } |
| 631 | 629 |
| 632 void V8SamplingProfiler::OnTraceLogDisabled() { | 630 void V8SamplingProfiler::OnTraceLogDisabled() { |
| 633 task_runner_->PostTask(FROM_HERE, | 631 StopSamplingThread(); |
| 634 base::Bind(&V8SamplingProfiler::StopSamplingThread, | |
| 635 base::Unretained(this))); | |
| 636 } | 632 } |
| 637 | 633 |
| 638 void V8SamplingProfiler::EnableSamplingEventForTesting(int code_added_events, | 634 void V8SamplingProfiler::EnableSamplingEventForTesting(int code_added_events, |
| 639 int sample_events) { | 635 int sample_events) { |
| 640 render_thread_sampler_->SetEventsToCollectForTest(code_added_events, | 636 render_thread_sampler_->SetEventsToCollectForTest(code_added_events, |
| 641 sample_events); | 637 sample_events); |
| 642 waitable_event_for_testing_.reset(new base::WaitableEvent(false, false)); | 638 waitable_event_for_testing_.reset(new base::WaitableEvent(false, false)); |
| 643 } | 639 } |
| 644 | 640 |
| 645 void V8SamplingProfiler::WaitSamplingEventForTesting() { | 641 void V8SamplingProfiler::WaitSamplingEventForTesting() { |
| 646 waitable_event_for_testing_->Wait(); | 642 waitable_event_for_testing_->Wait(); |
| 647 } | 643 } |
| 648 | 644 |
| 649 } // namespace content | 645 } // namespace content |
| OLD | NEW |