• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/tracing/perfetto_platform.h"
6 
7 #include "base/strings/strcat.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/task/task_traits.h"
10 #include "base/task/thread_pool.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/tracing/perfetto_task_runner.h"
13 #include "base/tracing_buildflags.h"
14 #include "build/build_config.h"
15 
16 #if BUILDFLAG(IS_ANDROID)
17 #include "base/android/build_info.h"
18 #endif  // BUILDFLAG(IS_ANDROID)
19 
20 #if !BUILDFLAG(IS_NACL)
21 #include "third_party/perfetto/include/perfetto/ext/base/thread_task_runner.h"
22 #endif
23 
24 namespace base {
25 namespace tracing {
26 
27 namespace {
28 constexpr char kProcessNamePrefix[] = "org.chromium-";
29 }  // namespace
30 
PerfettoPlatform(PerfettoTaskRunner * task_runner)31 PerfettoPlatform::PerfettoPlatform(PerfettoTaskRunner* task_runner)
32     : task_runner_(task_runner), thread_local_object_([](void* object) {
33         delete static_cast<ThreadLocalObject*>(object);
34       }) {}
35 
36 PerfettoPlatform::~PerfettoPlatform() = default;
37 
38 PerfettoPlatform::ThreadLocalObject*
GetOrCreateThreadLocalObject()39 PerfettoPlatform::GetOrCreateThreadLocalObject() {
40   auto* object = static_cast<ThreadLocalObject*>(thread_local_object_.Get());
41   if (!object) {
42     object = ThreadLocalObject::CreateInstance().release();
43     thread_local_object_.Set(object);
44   }
45   return object;
46 }
47 
CreateTaskRunner(const CreateTaskRunnerArgs &)48 std::unique_ptr<perfetto::base::TaskRunner> PerfettoPlatform::CreateTaskRunner(
49     const CreateTaskRunnerArgs&) {
50   // TODO(b/242965112): Add support for the builtin task runner
51   return std::make_unique<PerfettoTaskRunner>(
52       task_runner_->GetOrCreateTaskRunner());
53 }
54 
55 // This method is used by the SDK to determine the producer name.
56 // Note that we override the producer name for the mojo backend in ProducerHost,
57 // and thus this only affects the producer name for the system backend.
GetCurrentProcessName()58 std::string PerfettoPlatform::GetCurrentProcessName() {
59   const char* host_package_name = nullptr;
60 #if BUILDFLAG(IS_ANDROID)
61   host_package_name = android::BuildInfo::GetInstance()->host_package_name();
62 #endif  // BUILDFLAG(IS_ANDROID)
63 
64   // On Android we want to include if this is webview inside of an app or
65   // Android Chrome. To aid this we add the host_package_name to differentiate
66   // the various apps and sources.
67   std::string process_name;
68   if (host_package_name) {
69     process_name = StrCat(
70         {kProcessNamePrefix, host_package_name, "-",
71          NumberToString(trace_event::TraceLog::GetInstance()->process_id())});
72   } else {
73     process_name = StrCat(
74         {kProcessNamePrefix,
75          NumberToString(trace_event::TraceLog::GetInstance()->process_id())});
76   }
77   return process_name;
78 }
79 
GetCurrentThreadId()80 perfetto::base::PlatformThreadId PerfettoPlatform::GetCurrentThreadId() {
81   return base::PlatformThread::CurrentId();
82 }
83 
84 }  // namespace tracing
85 }  // namespace base
86