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 #ifndef BASE_TRACING_PERFETTO_PLATFORM_H_ 6 #define BASE_TRACING_PERFETTO_PLATFORM_H_ 7 8 #include "base/base_export.h" 9 #include "base/memory/raw_ptr.h" 10 #include "base/memory/scoped_refptr.h" 11 #include "base/threading/thread_local_storage.h" 12 #include "third_party/perfetto/include/perfetto/base/thread_utils.h" 13 #include "third_party/perfetto/include/perfetto/tracing/platform.h" 14 15 namespace base { 16 17 namespace tracing { 18 19 class PerfettoTaskRunner; 20 21 class BASE_EXPORT PerfettoPlatform : public perfetto::Platform { 22 public: 23 // Specifies the type of task runner used by Perfetto. 24 // TODO(skyostil): Move all scenarios to use the default task runner to 25 // avoid problems with unexpected re-entrancy and IPC deadlocks. 26 enum class TaskRunnerType { 27 // Use Perfetto's own task runner which runs tasks on a dedicated (internal) 28 // thread. 29 kBuiltin, 30 // Use base::ThreadPool. 31 kThreadPool, 32 }; 33 34 explicit PerfettoPlatform(PerfettoTaskRunner* task_runner); 35 ~PerfettoPlatform() override; 36 37 // perfetto::Platform implementation: 38 ThreadLocalObject* GetOrCreateThreadLocalObject() override; 39 std::unique_ptr<perfetto::base::TaskRunner> CreateTaskRunner( 40 const CreateTaskRunnerArgs&) override; 41 std::string GetCurrentProcessName() override; 42 43 // Chrome uses different thread IDs than Perfetto on Mac. So we need to 44 // override this method to keep Perfetto tracks consistent with Chrome 45 // thread IDs. 46 perfetto::base::PlatformThreadId GetCurrentThreadId() override; 47 48 private: 49 const TaskRunnerType task_runner_type_ = TaskRunnerType::kThreadPool; 50 raw_ptr<PerfettoTaskRunner> task_runner_; 51 ThreadLocalStorage::Slot thread_local_object_; 52 }; 53 54 } // namespace tracing 55 } // namespace base 56 57 #endif // BASE_TRACING_PERFETTO_PLATFORM_H_ 58