1 // Copyright 2018 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_TASK_RUNNER_H_ 6 #define BASE_TRACING_PERFETTO_TASK_RUNNER_H_ 7 8 #include "base/base_export.h" 9 #include "base/cancelable_callback.h" 10 #include "base/synchronization/lock.h" 11 #include "base/task/sequenced_task_runner.h" 12 #include "base/timer/timer.h" 13 #include "build/build_config.h" 14 #include "third_party/perfetto/include/perfetto/base/task_runner.h" 15 16 #if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 17 // Needed for base::FileDescriptorWatcher::Controller and for implementing 18 // AddFileDescriptorWatch & RemoveFileDescriptorWatch. 19 #include <map> 20 #include "base/files/file_descriptor_watcher_posix.h" 21 #endif // (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 22 23 namespace base { 24 namespace tracing { 25 26 // This wraps a base::TaskRunner implementation to be able 27 // to provide it to Perfetto. 28 class BASE_EXPORT PerfettoTaskRunner : public perfetto::base::TaskRunner { 29 public: 30 explicit PerfettoTaskRunner( 31 scoped_refptr<base::SequencedTaskRunner> task_runner); 32 ~PerfettoTaskRunner() override; 33 PerfettoTaskRunner(const PerfettoTaskRunner&) = delete; 34 void operator=(const PerfettoTaskRunner&) = delete; 35 36 // perfetto::base::TaskRunner implementation. Only called by 37 // the Perfetto implementation itself. 38 void PostTask(std::function<void()> task) override; 39 void PostDelayedTask(std::function<void()> task, uint32_t delay_ms) override; 40 // This in Chrome would more correctly be called "RunsTasksInCurrentSequence". 41 // Perfetto calls this to determine wheather CommitData requests should be 42 // flushed synchronously. RunsTasksInCurrentSequence is sufficient for that 43 // use case. 44 bool RunsTasksOnCurrentThread() const override; 45 46 void SetTaskRunner(scoped_refptr<base::SequencedTaskRunner> task_runner); 47 scoped_refptr<base::SequencedTaskRunner> GetOrCreateTaskRunner(); HasTaskRunner()48 bool HasTaskRunner() const { return !!task_runner_; } 49 50 // These are only used on Android when talking to the system Perfetto service. 51 void AddFileDescriptorWatch(perfetto::base::PlatformHandle, 52 std::function<void()>) override; 53 void RemoveFileDescriptorWatch(perfetto::base::PlatformHandle) override; 54 55 // Tests will shut down all task runners in between runs, so we need 56 // to re-create any static instances on each SetUp(); 57 void ResetTaskRunnerForTesting( 58 scoped_refptr<base::SequencedTaskRunner> task_runner); 59 60 private: 61 void OnDeferredTasksDrainTimer(); 62 63 scoped_refptr<base::SequencedTaskRunner> task_runner_; 64 #if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 65 // FDControllerAndCallback keeps track of the state of FD watching: 66 // * |controller| has value: FD watching is added. |callback| is nullopt. 67 // * |controller| is nullptr: FD watching is pending for add. |callback| has 68 // a value. 69 // It's safe to call RemoveFileDescriptorWatch in either of the above states. 70 // |controller| and |callback| can't be both non-null after returning from 71 // AddFileDescriptorWatch or RemoveFileDescriptorWatch. 72 struct FDControllerAndCallback { 73 std::unique_ptr<base::FileDescriptorWatcher::Controller> controller; 74 base::CancelableOnceClosure callback; 75 76 FDControllerAndCallback(); 77 ~FDControllerAndCallback(); 78 }; 79 std::map<int, FDControllerAndCallback> fd_controllers_; 80 #endif // (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 81 }; 82 83 } // namespace tracing 84 } // namespace base 85 86 #endif // BASE_TRACING_PERFETTO_TASK_RUNNER_H_ 87