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/task_runner.h"
31 #include "perfetto/ext/base/paged_memory.h"
32 #include "perfetto/ext/base/utils.h"
33 #include "perfetto/ext/base/weak_ptr.h"
34 #include "perfetto/ext/tracing/core/basic_types.h"
35 #include "src/traced/probes/ftrace/cpu_reader.h"
36 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
37 
38 namespace perfetto {
39 
40 class FtraceConfigMuxer;
41 class FtraceDataSource;
42 class FtraceProcfs;
43 class LazyKernelSymbolizer;
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 
54   class Observer {
55    public:
56     virtual ~Observer();
57     virtual void OnFtraceDataWrittenIntoDataSourceBuffers() = 0;
58   };
59 
60   // The passed Observer must outlive the returned FtraceController instance.
61   static std::unique_ptr<FtraceController> Create(base::TaskRunner*, Observer*);
62   virtual ~FtraceController();
63 
64   void DisableAllEvents();
65   void WriteTraceMarker(const std::string& s);
66   void ClearTrace();
67 
68   bool AddDataSource(FtraceDataSource*) PERFETTO_WARN_UNUSED_RESULT;
69   bool StartDataSource(FtraceDataSource*);
70   void RemoveDataSource(FtraceDataSource*);
71 
72   // Force a read of the ftrace buffers. Will call OnFtraceFlushComplete() on
73   // all |started_data_sources_|.
74   void Flush(FlushRequestID);
75 
76   void DumpFtraceStats(FtraceStats*);
77 
GetWeakPtr()78   base::WeakPtr<FtraceController> GetWeakPtr() {
79     return weak_factory_.GetWeakPtr();
80   }
81 
82  protected:
83   // Protected for testing.
84   FtraceController(std::unique_ptr<FtraceProcfs>,
85                    std::unique_ptr<ProtoTranslationTable>,
86                    std::unique_ptr<FtraceConfigMuxer>,
87                    base::TaskRunner*,
88                    Observer*);
89 
90   // Protected and virtual for testing.
91   virtual uint64_t NowMs() const;
92 
93  private:
94   friend class TestFtraceController;
95 
96   struct PerCpuState {
PerCpuStatePerCpuState97     PerCpuState(std::unique_ptr<CpuReader> _reader, size_t _period_page_quota)
98         : reader(std::move(_reader)), period_page_quota(_period_page_quota) {}
99     std::unique_ptr<CpuReader> reader;
100     size_t period_page_quota = 0;
101   };
102 
103   FtraceController(const FtraceController&) = delete;
104   FtraceController& operator=(const FtraceController&) = delete;
105 
106   // Periodic task that reads all per-cpu ftrace buffers.
107   void ReadTick(int generation);
108 
109   uint32_t GetDrainPeriodMs();
110 
111   void StartIfNeeded();
112   void StopIfNeeded();
113 
114   base::TaskRunner* const task_runner_;
115   Observer* const observer_;
116   base::PagedMemory parsing_mem_;
117   std::unique_ptr<LazyKernelSymbolizer> symbolizer_;
118   std::unique_ptr<FtraceProcfs> ftrace_procfs_;
119   std::unique_ptr<ProtoTranslationTable> table_;
120   std::unique_ptr<FtraceConfigMuxer> ftrace_config_muxer_;
121   int generation_ = 0;
122   bool atrace_running_ = false;
123   std::vector<PerCpuState> per_cpu_;  // empty if tracing isn't active
124   std::set<FtraceDataSource*> data_sources_;
125   std::set<FtraceDataSource*> started_data_sources_;
126   base::WeakPtrFactory<FtraceController> weak_factory_;  // Keep last.
127 };
128 
129 }  // namespace perfetto
130 
131 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_
132