perfetto_platform.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/tracing/perfetto_platform.h"
  5. #include "base/strings/strcat.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "base/task/deferred_sequenced_task_runner.h"
  8. #include "base/task/task_traits.h"
  9. #include "base/task/thread_pool.h"
  10. #include "base/trace_event/trace_event.h"
  11. #include "base/tracing/perfetto_task_runner.h"
  12. #include "base/tracing_buildflags.h"
  13. #include "build/build_config.h"
  14. #include "third_party/perfetto/protos/perfetto/trace/track_event/process_descriptor.gen.h"
  15. #include "third_party/perfetto/protos/perfetto/trace/track_event/thread_descriptor.gen.h"
  16. #if BUILDFLAG(IS_ANDROID)
  17. #include "base/android/build_info.h"
  18. #endif // BUILDFLAG(IS_ANDROID)
  19. #if !BUILDFLAG(IS_NACL)
  20. #include "third_party/perfetto/include/perfetto/ext/base/thread_task_runner.h"
  21. #endif
  22. namespace base {
  23. namespace tracing {
  24. namespace {
  25. constexpr char kProcessNamePrefix[] = "org.chromium-";
  26. } // namespace
  27. PerfettoPlatform::PerfettoPlatform(TaskRunnerType task_runner_type)
  28. : task_runner_type_(task_runner_type),
  29. deferred_task_runner_(new DeferredSequencedTaskRunner()),
  30. thread_local_object_([](void* object) {
  31. delete static_cast<ThreadLocalObject*>(object);
  32. }) {
  33. ThreadIdNameManager::GetInstance()->AddObserver(this);
  34. }
  35. PerfettoPlatform::~PerfettoPlatform() {
  36. ThreadIdNameManager::GetInstance()->RemoveObserver(this);
  37. }
  38. void PerfettoPlatform::StartTaskRunner(
  39. scoped_refptr<SequencedTaskRunner> task_runner) {
  40. DCHECK_EQ(task_runner_type_, TaskRunnerType::kThreadPool);
  41. DCHECK(!did_start_task_runner_);
  42. deferred_task_runner_->StartWithTaskRunner(task_runner);
  43. did_start_task_runner_ = true;
  44. }
  45. SequencedTaskRunner* PerfettoPlatform::task_runner() const {
  46. return deferred_task_runner_.get();
  47. }
  48. PerfettoPlatform::ThreadLocalObject*
  49. PerfettoPlatform::GetOrCreateThreadLocalObject() {
  50. auto* object = static_cast<ThreadLocalObject*>(thread_local_object_.Get());
  51. if (!object) {
  52. object = ThreadLocalObject::CreateInstance().release();
  53. thread_local_object_.Set(object);
  54. }
  55. return object;
  56. }
  57. std::unique_ptr<perfetto::base::TaskRunner> PerfettoPlatform::CreateTaskRunner(
  58. const CreateTaskRunnerArgs&) {
  59. switch (task_runner_type_) {
  60. case TaskRunnerType::kBuiltin:
  61. #if !BUILDFLAG(IS_NACL)
  62. return std::make_unique<perfetto::base::ThreadTaskRunner>(
  63. perfetto::base::ThreadTaskRunner::CreateAndStart());
  64. #else
  65. DCHECK(false);
  66. return nullptr;
  67. #endif
  68. case TaskRunnerType::kThreadPool:
  69. // We can't create a real task runner yet because the ThreadPool may not
  70. // be initialized. Instead, we point Perfetto to a buffering task runner
  71. // which will become active as soon as the thread pool is up (see
  72. // StartTaskRunner).
  73. return std::make_unique<PerfettoTaskRunner>(deferred_task_runner_);
  74. }
  75. }
  76. // This method is used by the SDK to determine the producer name.
  77. // Note that we override the producer name for the mojo backend in ProducerHost,
  78. // and thus this only affects the producer name for the system backend.
  79. std::string PerfettoPlatform::GetCurrentProcessName() {
  80. const char* host_package_name = nullptr;
  81. #if BUILDFLAG(IS_ANDROID)
  82. host_package_name = android::BuildInfo::GetInstance()->host_package_name();
  83. #endif // BUILDFLAG(IS_ANDROID)
  84. // On Android we want to include if this is webview inside of an app or
  85. // Android Chrome. To aid this we add the host_package_name to differentiate
  86. // the various apps and sources.
  87. std::string process_name;
  88. if (host_package_name) {
  89. process_name = StrCat(
  90. {kProcessNamePrefix, host_package_name, "-",
  91. NumberToString(trace_event::TraceLog::GetInstance()->process_id())});
  92. } else {
  93. process_name = StrCat(
  94. {kProcessNamePrefix,
  95. NumberToString(trace_event::TraceLog::GetInstance()->process_id())});
  96. }
  97. return process_name;
  98. }
  99. void PerfettoPlatform::OnThreadNameChanged(const char* name) {
  100. #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
  101. int process_id = trace_event::TraceLog::GetInstance()->process_id();
  102. if (perfetto::Tracing::IsInitialized()) {
  103. auto track = perfetto::ThreadTrack::Current();
  104. auto desc = track.Serialize();
  105. desc.mutable_thread()->set_thread_name(name);
  106. desc.mutable_thread()->set_pid(process_id);
  107. perfetto::TrackEvent::SetTrackDescriptor(track, std::move(desc));
  108. }
  109. #endif // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
  110. }
  111. } // namespace tracing
  112. } // namespace base