1 /* 2 * Copyright (C) 2017 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 #ifndef SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_ 18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_ 19 20 #include <stdint.h> 21 #include <unistd.h> 22 23 #include <bitset> 24 #include <functional> 25 #include <map> 26 #include <memory> 27 #include <set> 28 #include <string> 29 30 #include "perfetto/base/gtest_prod_util.h" 31 #include "perfetto/base/task_runner.h" 32 #include "perfetto/base/utils.h" 33 #include "perfetto/base/weak_ptr.h" 34 #include "perfetto/tracing/core/basic_types.h" 35 #include "src/traced/probes/ftrace/ftrace_config.h" 36 #include "src/traced/probes/ftrace/ftrace_thread_sync.h" 37 38 namespace perfetto { 39 40 class CpuReader; 41 class FtraceConfigMuxer; 42 class FtraceDataSource; 43 class FtraceProcfs; 44 class ProtoTranslationTable; 45 struct FtraceStats; 46 47 // Method of last resort to reset ftrace state. 48 void HardResetFtraceState(); 49 50 // Utility class for controlling ftrace. 51 class FtraceController { 52 public: 53 static const char* const kTracingPaths[]; 54 55 class Observer { 56 public: 57 virtual ~Observer(); 58 virtual void OnFtraceDataWrittenIntoDataSourceBuffers() = 0; 59 }; 60 61 // The passed Observer must outlive the returned FtraceController instance. 62 static std::unique_ptr<FtraceController> Create(base::TaskRunner*, Observer*); 63 virtual ~FtraceController(); 64 65 // These two methods are called by CpuReader(s) from their worker threads. 66 static void OnCpuReaderRead(size_t cpu, int generation, FtraceThreadSync*); 67 static void OnCpuReaderFlush(size_t cpu, int generation, FtraceThreadSync*); 68 69 void DisableAllEvents(); 70 void WriteTraceMarker(const std::string& s); 71 void ClearTrace(); 72 73 bool AddDataSource(FtraceDataSource*) PERFETTO_WARN_UNUSED_RESULT; 74 bool StartDataSource(FtraceDataSource*); 75 void RemoveDataSource(FtraceDataSource*); 76 77 // Force a read of the ftrace buffers, including kernel buffer pages that 78 // are not full. Will call OnFtraceFlushComplete() on all 79 // |started_data_sources_| once all workers have flushed (or timed out). 80 void Flush(FlushRequestID); 81 82 void DumpFtraceStats(FtraceStats*); 83 84 base::WeakPtr<FtraceController> GetWeakPtr() { 85 return weak_factory_.GetWeakPtr(); 86 } 87 88 protected: 89 // Protected for testing. 90 FtraceController(std::unique_ptr<FtraceProcfs>, 91 std::unique_ptr<ProtoTranslationTable>, 92 std::unique_ptr<FtraceConfigMuxer>, 93 base::TaskRunner*, 94 Observer*); 95 96 virtual void OnDrainCpuForTesting(size_t /*cpu*/) {} 97 98 // Protected and virtual for testing. 99 virtual uint64_t NowMs() const; 100 101 private: 102 friend class TestFtraceController; 103 FRIEND_TEST(FtraceControllerIntegrationTest, EnableDisableEvent); 104 105 FtraceController(const FtraceController&) = delete; 106 FtraceController& operator=(const FtraceController&) = delete; 107 108 void OnFlushTimeout(FlushRequestID); 109 void DrainCPUs(int generation); 110 void UnblockReaders(); 111 void NotifyFlushCompleteToStartedDataSources(FlushRequestID); 112 void IssueThreadSyncCmd(FtraceThreadSync::Cmd, 113 std::unique_lock<std::mutex> = {}); 114 115 uint32_t GetDrainPeriodMs(); 116 117 void StartIfNeeded(); 118 void StopIfNeeded(); 119 120 base::TaskRunner* const task_runner_; 121 Observer* const observer_; 122 FtraceThreadSync thread_sync_; 123 std::unique_ptr<FtraceProcfs> ftrace_procfs_; 124 std::unique_ptr<ProtoTranslationTable> table_; 125 std::unique_ptr<FtraceConfigMuxer> ftrace_config_muxer_; 126 int generation_ = 0; 127 FlushRequestID cur_flush_request_id_ = 0; 128 bool atrace_running_ = false; 129 std::vector<std::unique_ptr<CpuReader>> cpu_readers_; 130 std::set<FtraceDataSource*> data_sources_; 131 std::set<FtraceDataSource*> started_data_sources_; 132 PERFETTO_THREAD_CHECKER(thread_checker_) 133 base::WeakPtrFactory<FtraceController> weak_factory_; // Keep last. 134 }; 135 136 } // namespace perfetto 137 138 #endif // SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_ 139