xref: /aosp_15_r20/external/perfetto/src/tracing/platform_posix.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "perfetto/base/build_config.h"
18 
19 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
20     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
21     PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA) || \
22     PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
23 
24 #include "perfetto/ext/base/file_utils.h"
25 #include "perfetto/ext/base/thread_task_runner.h"
26 #include "perfetto/tracing/internal/tracing_tls.h"
27 #include "perfetto/tracing/platform.h"
28 #include "perfetto/tracing/trace_writer_base.h"
29 
30 #include <pthread.h>
31 #include <stdlib.h>
32 
33 namespace perfetto {
34 
35 namespace {
36 
37 class PlatformPosix : public Platform {
38  public:
39   PlatformPosix();
40   ~PlatformPosix() override;
41 
42   ThreadLocalObject* GetOrCreateThreadLocalObject() override;
43 
44   std::unique_ptr<base::TaskRunner> CreateTaskRunner(
45       const CreateTaskRunnerArgs&) override;
46   std::string GetCurrentProcessName() override;
47   void Shutdown() override;
48 
49  private:
50   pthread_key_t tls_key_{};
51 };
52 
53 PlatformPosix* g_instance = nullptr;
54 
55 using ThreadLocalObject = Platform::ThreadLocalObject;
56 
PlatformPosix()57 PlatformPosix::PlatformPosix() {
58   PERFETTO_CHECK(!g_instance);
59   g_instance = this;
60   auto tls_dtor = [](void* obj) {
61     // The Posix TLS implementation resets the key before calling this dtor.
62     // Here we re-reset it to the object we are about to delete. This is to
63     // handle re-entrant usages of tracing in the PostTask done during the dtor
64     // (see comments in TracingTLS::~TracingTLS()). Chromium's platform
65     // implementation (which does NOT use this platform impl) has a similar
66     // workaround (https://crrev.com/c/2748300).
67     pthread_setspecific(g_instance->tls_key_, obj);
68     delete static_cast<ThreadLocalObject*>(obj);
69     pthread_setspecific(g_instance->tls_key_, nullptr);
70   };
71   PERFETTO_CHECK(pthread_key_create(&tls_key_, tls_dtor) == 0);
72 }
73 
~PlatformPosix()74 PlatformPosix::~PlatformPosix() {
75   // pthread_key_delete doesn't call destructors, so do it manually for the
76   // calling thread.
77   void* tls_ptr = pthread_getspecific(tls_key_);
78   delete static_cast<ThreadLocalObject*>(tls_ptr);
79 
80   pthread_key_delete(tls_key_);
81   g_instance = nullptr;
82 }
83 
Shutdown()84 void PlatformPosix::Shutdown() {
85   PERFETTO_CHECK(g_instance == this);
86   delete this;
87   PERFETTO_CHECK(!g_instance);
88   // We're not clearing out the instance in GetDefaultPlatform() since it's not
89   // possible to re-initialize Perfetto after calling this function anyway.
90 }
91 
GetOrCreateThreadLocalObject()92 ThreadLocalObject* PlatformPosix::GetOrCreateThreadLocalObject() {
93   // In chromium this should be implemented using base::ThreadLocalStorage.
94   void* tls_ptr = pthread_getspecific(tls_key_);
95 
96   // This is needed to handle re-entrant calls during TLS dtor.
97   // See comments in platform.cc and aosp/1712371 .
98   ThreadLocalObject* tls = static_cast<ThreadLocalObject*>(tls_ptr);
99   if (!tls) {
100     tls = ThreadLocalObject::CreateInstance().release();
101     pthread_setspecific(tls_key_, tls);
102   }
103   return tls;
104 }
105 
CreateTaskRunner(const CreateTaskRunnerArgs & args)106 std::unique_ptr<base::TaskRunner> PlatformPosix::CreateTaskRunner(
107     const CreateTaskRunnerArgs& args) {
108   return std::unique_ptr<base::TaskRunner>(new base::ThreadTaskRunner(
109       base::ThreadTaskRunner::CreateAndStart(args.name_for_debugging)));
110 }
111 
GetCurrentProcessName()112 std::string PlatformPosix::GetCurrentProcessName() {
113 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
114     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
115   std::string cmdline;
116   base::ReadFile("/proc/self/cmdline", &cmdline);
117   return cmdline.substr(0, cmdline.find('\0'));
118 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
119   return std::string(getprogname());
120 #else
121   return "unknown_producer";
122 #endif
123 }
124 
125 }  // namespace
126 
127 // static
GetDefaultPlatform()128 Platform* Platform::GetDefaultPlatform() {
129   static PlatformPosix* instance = new PlatformPosix();
130   return instance;
131 }
132 
133 }  // namespace perfetto
134 #endif  // OS_LINUX || OS_ANDROID || OS_APPLE || OS_FUCHSIA
135