blob: d3f9e3d05a716f48765ca2e75e4414a31e68e718 [file] [log] [blame]
Primiano Tuccide82dae2018-06-04 16:17:49 +02001/*
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 <unistd.h>
21
Primiano Tuccide82dae2018-06-04 16:17:49 +020022#include <bitset>
23#include <condition_variable>
Primiano Tuccifd8240d2018-08-01 09:34:54 +010024#include <functional>
Primiano Tuccide82dae2018-06-04 16:17:49 +020025#include <map>
26#include <memory>
27#include <mutex>
28#include <set>
29#include <string>
Primiano Tuccide82dae2018-06-04 16:17:49 +020030
31#include "gtest/gtest_prod.h"
Primiano Tuccide82dae2018-06-04 16:17:49 +020032#include "perfetto/base/task_runner.h"
Primiano Tuccifd8240d2018-08-01 09:34:54 +010033#include "perfetto/base/utils.h"
Primiano Tuccide82dae2018-06-04 16:17:49 +020034#include "perfetto/base/weak_ptr.h"
Primiano Tuccifd8240d2018-08-01 09:34:54 +010035#include "src/traced/probes/ftrace/ftrace_config.h"
Primiano Tuccide82dae2018-06-04 16:17:49 +020036
37namespace perfetto {
38
Primiano Tuccifd8240d2018-08-01 09:34:54 +010039class CpuReader;
40class FtraceConfigMuxer;
41class FtraceDataSource;
42class FtraceProcfs;
43class ProtoTranslationTable;
44struct FtraceStats;
Primiano Tuccide82dae2018-06-04 16:17:49 +020045
46// Method of last resort to reset ftrace state.
47void HardResetFtraceState();
48
Primiano Tuccide82dae2018-06-04 16:17:49 +020049// Utility class for controlling ftrace.
50class FtraceController {
51 public:
Primiano Tuccifd8240d2018-08-01 09:34:54 +010052 class Observer {
53 public:
54 virtual ~Observer();
55 virtual void OnFtraceDataWrittenIntoDataSourceBuffers() = 0;
56 };
Primiano Tuccide82dae2018-06-04 16:17:49 +020057
Primiano Tuccifd8240d2018-08-01 09:34:54 +010058 // The passed Observer must outlive the returned FtraceController instance.
59 static std::unique_ptr<FtraceController> Create(base::TaskRunner*, Observer*);
60 virtual ~FtraceController();
Primiano Tuccide82dae2018-06-04 16:17:49 +020061
62 void DisableAllEvents();
63 void WriteTraceMarker(const std::string& s);
64 void ClearTrace();
65
Primiano Tuccifd8240d2018-08-01 09:34:54 +010066 bool AddDataSource(FtraceDataSource*) PERFETTO_WARN_UNUSED_RESULT;
67 void RemoveDataSource(FtraceDataSource*);
68
69 void DumpFtraceStats(FtraceStats*);
70
71 base::WeakPtr<FtraceController> GetWeakPtr() {
72 return weak_factory_.GetWeakPtr();
73 }
74
Primiano Tuccide82dae2018-06-04 16:17:49 +020075 protected:
76 // Protected for testing.
77 FtraceController(std::unique_ptr<FtraceProcfs>,
78 std::unique_ptr<ProtoTranslationTable>,
79 std::unique_ptr<FtraceConfigMuxer>,
Primiano Tuccifd8240d2018-08-01 09:34:54 +010080 base::TaskRunner*,
81 Observer*);
Primiano Tuccide82dae2018-06-04 16:17:49 +020082
Primiano Tuccifd8240d2018-08-01 09:34:54 +010083 virtual void OnDrainCpuForTesting(size_t /*cpu*/) {}
Primiano Tuccide82dae2018-06-04 16:17:49 +020084
85 // Protected and virtual for testing.
86 virtual uint64_t NowMs() const;
87
88 private:
Primiano Tuccide82dae2018-06-04 16:17:49 +020089 friend class TestFtraceController;
90 FRIEND_TEST(FtraceControllerIntegrationTest, EnableDisableEvent);
91
Primiano Tuccifd8240d2018-08-01 09:34:54 +010092 static constexpr size_t kMaxCpus = 64;
93
Primiano Tuccide82dae2018-06-04 16:17:49 +020094 FtraceController(const FtraceController&) = delete;
95 FtraceController& operator=(const FtraceController&) = delete;
96
97 // Called on a worker thread when |cpu| has at least one page of data
98 // available for reading.
99 void OnDataAvailable(base::WeakPtr<FtraceController>,
100 size_t generation,
101 size_t cpu,
102 uint32_t drain_period_ms);
103
104 static void DrainCPUs(base::WeakPtr<FtraceController>, size_t generation);
105 static void UnblockReaders(const base::WeakPtr<FtraceController>&);
106
107 uint32_t GetDrainPeriodMs();
108
Primiano Tuccide82dae2018-06-04 16:17:49 +0200109 void StartIfNeeded();
110 void StopIfNeeded();
111
112 // Begin lock-protected members.
113 std::mutex lock_;
114 std::condition_variable data_drained_;
115 std::bitset<kMaxCpus> cpus_to_drain_;
116 bool listening_for_raw_trace_data_ = false;
117 // End lock-protected members.
118
Primiano Tuccifd8240d2018-08-01 09:34:54 +0100119 base::TaskRunner* const task_runner_;
120 Observer* const observer_;
Primiano Tuccide82dae2018-06-04 16:17:49 +0200121 std::unique_ptr<FtraceProcfs> ftrace_procfs_;
122 std::unique_ptr<ProtoTranslationTable> table_;
123 std::unique_ptr<FtraceConfigMuxer> ftrace_config_muxer_;
124 size_t generation_ = 0;
125 bool atrace_running_ = false;
Primiano Tuccib6de48b2018-07-26 16:00:44 +0100126 std::map<size_t, std::unique_ptr<CpuReader>> cpu_readers_;
Primiano Tuccifd8240d2018-08-01 09:34:54 +0100127 std::set<FtraceDataSource*> data_sources_;
128 base::WeakPtrFactory<FtraceController> weak_factory_; // Keep last.
Primiano Tuccide82dae2018-06-04 16:17:49 +0200129 PERFETTO_THREAD_CHECKER(thread_checker_)
130};
131
132} // namespace perfetto
133
134#endif // SRC_TRACED_PROBES_FTRACE_FTRACE_CONTROLLER_H_