• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bool HardResetFtraceState();
49 
50 // Stores the a snapshot of the timestamps from ftrace's trace clock
51 // and CLOCK_BOOTITME.
52 //
53 // This is used when the "boot" (i.e. CLOCK_BOOTITME) is not available
54 // for timestamping trace events (on Android O- and 3.x Linux kernels).
55 // Trace processor can use this data to sync clocks just as it would
56 // with ClockSnapshot packets.
57 struct FtraceClockSnapshot {
58   // The timestamp according to the ftrace clock.
59   int64_t ftrace_clock_ts = 0;
60 
61   // The timestamp according to CLOCK_BOOTTIME.
62   int64_t boot_clock_ts = 0;
63 };
64 
65 // Utility class for controlling ftrace.
66 class FtraceController {
67  public:
68 
69   class Observer {
70    public:
71     virtual ~Observer();
72     virtual void OnFtraceDataWrittenIntoDataSourceBuffers() = 0;
73   };
74 
75   // The passed Observer must outlive the returned FtraceController instance.
76   static std::unique_ptr<FtraceController> Create(base::TaskRunner*, Observer*);
77   virtual ~FtraceController();
78 
79   void DisableAllEvents();
80   void WriteTraceMarker(const std::string& s);
81   void ClearTrace();
82 
83   bool AddDataSource(FtraceDataSource*) PERFETTO_WARN_UNUSED_RESULT;
84   bool StartDataSource(FtraceDataSource*);
85   void RemoveDataSource(FtraceDataSource*);
86 
87   // Force a read of the ftrace buffers. Will call OnFtraceFlushComplete() on
88   // all |started_data_sources_|.
89   void Flush(FlushRequestID);
90 
91   void DumpFtraceStats(FtraceStats*);
92 
GetWeakPtr()93   base::WeakPtr<FtraceController> GetWeakPtr() {
94     return weak_factory_.GetWeakPtr();
95   }
96 
97  protected:
98   // Protected for testing.
99   FtraceController(std::unique_ptr<FtraceProcfs>,
100                    std::unique_ptr<ProtoTranslationTable>,
101                    std::unique_ptr<FtraceConfigMuxer>,
102                    base::TaskRunner*,
103                    Observer*);
104 
105   // Protected and virtual for testing.
106   virtual uint64_t NowMs() const;
107 
108  private:
109   friend class TestFtraceController;
110 
111   struct PerCpuState {
PerCpuStatePerCpuState112     PerCpuState(std::unique_ptr<CpuReader> _reader, size_t _period_page_quota)
113         : reader(std::move(_reader)), period_page_quota(_period_page_quota) {}
114     std::unique_ptr<CpuReader> reader;
115     size_t period_page_quota = 0;
116   };
117 
118   FtraceController(const FtraceController&) = delete;
119   FtraceController& operator=(const FtraceController&) = delete;
120 
121   // Periodic task that reads all per-cpu ftrace buffers.
122   void ReadTick(int generation);
123 
124   uint32_t GetDrainPeriodMs();
125 
126   void StartIfNeeded();
127   void StopIfNeeded();
128 
129   void MaybeSnapshotFtraceClock();
130 
131   base::TaskRunner* const task_runner_;
132   Observer* const observer_;
133   base::PagedMemory parsing_mem_;
134   base::ScopedFile cpu_zero_stats_fd_;
135   std::unique_ptr<LazyKernelSymbolizer> symbolizer_;
136   std::unique_ptr<FtraceProcfs> ftrace_procfs_;
137   std::unique_ptr<ProtoTranslationTable> table_;
138   std::unique_ptr<FtraceConfigMuxer> ftrace_config_muxer_;
139   std::unique_ptr<FtraceClockSnapshot> ftrace_clock_snapshot_;
140   int generation_ = 0;
141   bool atrace_running_ = false;
142   std::vector<PerCpuState> per_cpu_;  // empty if tracing isn't active
143   std::set<FtraceDataSource*> data_sources_;
144   std::set<FtraceDataSource*> started_data_sources_;
145   base::WeakPtrFactory<FtraceController> weak_factory_;  // Keep last.
146 };
147 
148 }  // namespace perfetto
149 
150 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_
151