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