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 scoped_refptr<base::SequencedTaskRunner> GetOrCreateTaskRunner(); HasTaskRunner()47 bool HasTaskRunner() const { return !!task_runner_; } 48 49 // These are only used on Android when talking to the system Perfetto service. 50 void AddFileDescriptorWatch(perfetto::base::PlatformHandle, 51 std::function<void()>) override; 52 void RemoveFileDescriptorWatch(perfetto::base::PlatformHandle) override; 53 54 // Tests will shut down all task runners in between runs, so we need 55 // to re-create any static instances on each SetUp(); 56 void ResetTaskRunnerForTesting( 57 scoped_refptr<base::SequencedTaskRunner> task_runner); 58 59 private: 60 void OnDeferredTasksDrainTimer(); 61 62 scoped_refptr<base::SequencedTaskRunner> task_runner_; 63 #if (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 64 // FDControllerAndCallback keeps track of the state of FD watching: 65 // * |controller| has value: FD watching is added. |callback| is nullopt. 66 // * |controller| is nullptr: FD watching is pending for add. |callback| has 67 // a value. 68 // It's safe to call RemoveFileDescriptorWatch in either of the above states. 69 // |controller| and |callback| can't be both non-null after returning from 70 // AddFileDescriptorWatch or RemoveFileDescriptorWatch. 71 struct FDControllerAndCallback { 72 std::unique_ptr<base::FileDescriptorWatcher::Controller> controller; 73 base::CancelableOnceClosure callback; 74 75 FDControllerAndCallback(); 76 ~FDControllerAndCallback(); 77 }; 78 std::map<int, FDControllerAndCallback> fd_controllers_; 79 #endif // (BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_NACL)) || BUILDFLAG(IS_FUCHSIA) 80 }; 81 82 } // namespace tracing 83 } // namespace base 84 85 #endif // BASE_TRACING_PERFETTO_TASK_RUNNER_H_ 86