• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_APPLE)
22 
23 #include "perfetto/ext/base/file_utils.h"
24 #include "perfetto/ext/base/thread_task_runner.h"
25 #include "perfetto/tracing/internal/tracing_tls.h"
26 #include "perfetto/tracing/platform.h"
27 #include "perfetto/tracing/trace_writer_base.h"
28 
29 #include <pthread.h>
30 #include <stdlib.h>
31 
32 namespace perfetto {
33 
34 namespace {
35 
36 class PlatformPosix : public Platform {
37  public:
38   PlatformPosix();
39   ~PlatformPosix() override;
40 
41   ThreadLocalObject* GetOrCreateThreadLocalObject() override;
42 
43   std::unique_ptr<base::TaskRunner> CreateTaskRunner(
44       const CreateTaskRunnerArgs&) override;
45   std::string GetCurrentProcessName() override;
46 
47  private:
48   pthread_key_t tls_key_{};
49 };
50 
51 PlatformPosix* g_instance = nullptr;
52 
53 using ThreadLocalObject = Platform::ThreadLocalObject;
54 
PlatformPosix()55 PlatformPosix::PlatformPosix() {
56   PERFETTO_CHECK(!g_instance);
57   g_instance = this;
58   auto tls_dtor = [](void* obj) {
59     // The Posix TLS implementation resets the key before calling this dtor.
60     // Here we re-reset it to the object we are about to delete. This is to
61     // handle re-entrant usages of tracing in the PostTask done during the dtor
62     // (see comments in TracingTLS::~TracingTLS()). Chromium's platform
63     // implementation (which does NOT use this platform impl) has a similar
64     // workaround (https://crrev.com/c/2748300).
65     pthread_setspecific(g_instance->tls_key_, obj);
66     delete static_cast<ThreadLocalObject*>(obj);
67     pthread_setspecific(g_instance->tls_key_, nullptr);
68   };
69   PERFETTO_CHECK(pthread_key_create(&tls_key_, tls_dtor) == 0);
70 }
71 
~PlatformPosix()72 PlatformPosix::~PlatformPosix() {
73   pthread_key_delete(tls_key_);
74   g_instance = nullptr;
75 }
76 
GetOrCreateThreadLocalObject()77 ThreadLocalObject* PlatformPosix::GetOrCreateThreadLocalObject() {
78   // In chromium this should be implemented using base::ThreadLocalStorage.
79   void* tls_ptr = pthread_getspecific(tls_key_);
80 
81   // This is needed to handle re-entrant calls during TLS dtor.
82   // See comments in platform.cc and aosp/1712371 .
83   ThreadLocalObject* tls = static_cast<ThreadLocalObject*>(tls_ptr);
84   if (!tls) {
85     tls = ThreadLocalObject::CreateInstance().release();
86     pthread_setspecific(tls_key_, tls);
87   }
88   return tls;
89 }
90 
CreateTaskRunner(const CreateTaskRunnerArgs & args)91 std::unique_ptr<base::TaskRunner> PlatformPosix::CreateTaskRunner(
92     const CreateTaskRunnerArgs& args) {
93   return std::unique_ptr<base::TaskRunner>(new base::ThreadTaskRunner(
94       base::ThreadTaskRunner::CreateAndStart(args.name_for_debugging)));
95 }
96 
GetCurrentProcessName()97 std::string PlatformPosix::GetCurrentProcessName() {
98 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
99     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
100   std::string cmdline;
101   base::ReadFile("/proc/self/cmdline", &cmdline);
102   return cmdline.substr(0, cmdline.find('\0'));
103 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
104   return std::string(getprogname());
105 #else
106   return "unknown_producer";
107 #endif
108 }
109 
110 }  // namespace
111 
112 // static
GetDefaultPlatform()113 Platform* Platform::GetDefaultPlatform() {
114   static PlatformPosix* instance = new PlatformPosix();
115   return instance;
116 }
117 
118 }  // namespace perfetto
119 #endif  // OS_LINUX || OS_ANDROID || OS_APPLE
120