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