Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [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 | #include "perfetto/ftrace_reader/ftrace_controller.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <stdint.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
Hector Dearman | c61e883 | 2018-01-25 12:10:12 +0000 | [diff] [blame] | 24 | #include <sys/wait.h> |
Hector Dearman | 0134e7f | 2018-02-13 12:35:44 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 26 | |
| 27 | #include <array> |
| 28 | #include <string> |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 29 | #include <utility> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 30 | |
Hector Dearman | c61e883 | 2018-01-25 12:10:12 +0000 | [diff] [blame] | 31 | #include "perfetto/base/build_config.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 32 | #include "perfetto/base/logging.h" |
Primiano Tucci | 8934c6c | 2018-03-15 11:39:27 +0000 | [diff] [blame] | 33 | #include "perfetto/base/time.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 34 | #include "perfetto/base/utils.h" |
Florian Mayer | ba3eeae | 2018-03-12 11:48:21 +0000 | [diff] [blame] | 35 | #include "src/ftrace_reader/cpu_reader.h" |
| 36 | #include "src/ftrace_reader/event_info.h" |
| 37 | #include "src/ftrace_reader/ftrace_config_muxer.h" |
| 38 | #include "src/ftrace_reader/ftrace_procfs.h" |
| 39 | #include "src/ftrace_reader/proto_translation_table.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 40 | |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 41 | #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 42 | |
| 43 | namespace perfetto { |
| 44 | namespace { |
| 45 | |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 46 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
Florian Mayer | dbd0878 | 2018-03-21 14:07:51 +0000 | [diff] [blame] | 47 | constexpr const char* kTracingPaths[] = { |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 48 | "/sys/kernel/tracing/", "/sys/kernel/debug/tracing/", nullptr, |
| 49 | }; |
| 50 | #else |
Florian Mayer | dbd0878 | 2018-03-21 14:07:51 +0000 | [diff] [blame] | 51 | constexpr const char* kTracingPaths[] = { |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 52 | "/sys/kernel/debug/tracing/", nullptr, |
| 53 | }; |
| 54 | #endif |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 55 | |
Florian Mayer | dbd0878 | 2018-03-21 14:07:51 +0000 | [diff] [blame] | 56 | constexpr int kDefaultDrainPeriodMs = 100; |
| 57 | constexpr int kMinDrainPeriodMs = 1; |
| 58 | constexpr int kMaxDrainPeriodMs = 1000 * 60; |
Hector Dearman | 6ea67b3 | 2018-02-01 15:45:15 +0000 | [diff] [blame] | 59 | |
Hector Dearman | 6ea67b3 | 2018-02-01 15:45:15 +0000 | [diff] [blame] | 60 | uint32_t ClampDrainPeriodMs(uint32_t drain_period_ms) { |
| 61 | if (drain_period_ms == 0) { |
| 62 | return kDefaultDrainPeriodMs; |
| 63 | } |
| 64 | if (drain_period_ms < kMinDrainPeriodMs || |
| 65 | kMaxDrainPeriodMs < drain_period_ms) { |
| 66 | PERFETTO_LOG("drain_period_ms was %u should be between %u and %u", |
| 67 | drain_period_ms, kMinDrainPeriodMs, kMaxDrainPeriodMs); |
| 68 | return kDefaultDrainPeriodMs; |
| 69 | } |
| 70 | return drain_period_ms; |
| 71 | } |
| 72 | |
Hector Dearman | 0134e7f | 2018-02-13 12:35:44 +0000 | [diff] [blame] | 73 | void WriteToFile(const char* path, const char* str) { |
| 74 | int fd = open(path, O_WRONLY); |
| 75 | if (fd == -1) |
| 76 | return; |
Hector Dearman | 87d529c | 2018-02-13 15:11:11 +0000 | [diff] [blame] | 77 | perfetto::base::ignore_result(write(fd, str, strlen(str))); |
| 78 | perfetto::base::ignore_result(close(fd)); |
Hector Dearman | 0134e7f | 2018-02-13 12:35:44 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void ClearFile(const char* path) { |
| 82 | int fd = open(path, O_WRONLY | O_TRUNC); |
| 83 | if (fd == -1) |
| 84 | return; |
Hector Dearman | 87d529c | 2018-02-13 15:11:11 +0000 | [diff] [blame] | 85 | perfetto::base::ignore_result(close(fd)); |
Hector Dearman | 0134e7f | 2018-02-13 12:35:44 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Hector Dearman | 0134e7f | 2018-02-13 12:35:44 +0000 | [diff] [blame] | 90 | // Method of last resort to reset ftrace state. |
| 91 | // We don't know what state the rest of the system and process is so as far |
| 92 | // as possible avoid allocations. |
| 93 | void HardResetFtraceState() { |
| 94 | WriteToFile("/sys/kernel/debug/tracing/tracing_on", "0"); |
| 95 | WriteToFile("/sys/kernel/debug/tracing/buffer_size_kb", "4"); |
| 96 | WriteToFile("/sys/kernel/debug/tracing/events/enable", "0"); |
| 97 | ClearFile("/sys/kernel/debug/tracing/trace"); |
| 98 | |
| 99 | WriteToFile("/sys/kernel/tracing/tracing_on", "0"); |
| 100 | WriteToFile("/sys/kernel/tracing/buffer_size_kb", "4"); |
| 101 | WriteToFile("/sys/kernel/tracing/events/enable", "0"); |
| 102 | ClearFile("/sys/kernel/tracing/trace"); |
| 103 | } |
| 104 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 105 | // static |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 106 | // TODO(taylori): Add a test for tracing paths in integration tests. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 107 | std::unique_ptr<FtraceController> FtraceController::Create( |
| 108 | base::TaskRunner* runner) { |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 109 | size_t index = 0; |
| 110 | std::unique_ptr<FtraceProcfs> ftrace_procfs = nullptr; |
| 111 | while (!ftrace_procfs && kTracingPaths[index]) { |
| 112 | ftrace_procfs = FtraceProcfs::Create(kTracingPaths[index++]); |
| 113 | } |
| 114 | |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 115 | if (!ftrace_procfs) |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 116 | return nullptr; |
Isabelle Taylor | c31a431 | 2018-01-22 16:44:28 +0000 | [diff] [blame] | 117 | |
Hector Dearman | 77af133 | 2017-12-15 11:55:33 +0000 | [diff] [blame] | 118 | auto table = ProtoTranslationTable::Create( |
| 119 | ftrace_procfs.get(), GetStaticEventInfo(), GetStaticCommonFieldsInfo()); |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 120 | |
| 121 | std::unique_ptr<FtraceConfigMuxer> model = std::unique_ptr<FtraceConfigMuxer>( |
| 122 | new FtraceConfigMuxer(ftrace_procfs.get(), table.get())); |
| 123 | return std::unique_ptr<FtraceController>(new FtraceController( |
| 124 | std::move(ftrace_procfs), std::move(table), std::move(model), runner)); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | FtraceController::FtraceController(std::unique_ptr<FtraceProcfs> ftrace_procfs, |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 128 | std::unique_ptr<ProtoTranslationTable> table, |
| 129 | std::unique_ptr<FtraceConfigMuxer> model, |
| 130 | base::TaskRunner* task_runner) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 131 | : ftrace_procfs_(std::move(ftrace_procfs)), |
Primiano Tucci | 68323b0 | 2017-12-18 10:54:13 +0100 | [diff] [blame] | 132 | table_(std::move(table)), |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 133 | ftrace_config_muxer_(std::move(model)), |
| 134 | task_runner_(task_runner), |
Primiano Tucci | 68323b0 | 2017-12-18 10:54:13 +0100 | [diff] [blame] | 135 | weak_factory_(this) {} |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 136 | |
| 137 | FtraceController::~FtraceController() { |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 138 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 139 | for (const auto* sink : sinks_) |
| 140 | ftrace_config_muxer_->RemoveConfig(sink->id_); |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 141 | sinks_.clear(); |
| 142 | StopIfNeeded(); |
| 143 | } |
| 144 | |
| 145 | uint64_t FtraceController::NowMs() const { |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 146 | return static_cast<uint64_t>(base::GetWallTimeMs().count()); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Hector Dearman | 680a28b | 2018-01-11 18:34:24 +0000 | [diff] [blame] | 149 | // static |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 150 | void FtraceController::DrainCPUs(base::WeakPtr<FtraceController> weak_this, |
| 151 | size_t generation) { |
Hector Dearman | 680a28b | 2018-01-11 18:34:24 +0000 | [diff] [blame] | 152 | // The controller might be gone. |
| 153 | if (!weak_this) |
| 154 | return; |
Hector Dearman | 680a28b | 2018-01-11 18:34:24 +0000 | [diff] [blame] | 155 | // We might have stopped tracing then quickly re-enabled it, in this case |
| 156 | // we don't want to end up with two periodic tasks for each CPU: |
| 157 | if (weak_this->generation_ != generation) |
| 158 | return; |
| 159 | |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 160 | PERFETTO_DCHECK_THREAD(weak_this->thread_checker_); |
| 161 | std::bitset<kMaxCpus> cpus_to_drain; |
| 162 | { |
| 163 | std::unique_lock<std::mutex> lock(weak_this->lock_); |
| 164 | // We might have stopped caring about events. |
| 165 | if (!weak_this->listening_for_raw_trace_data_) |
| 166 | return; |
| 167 | std::swap(cpus_to_drain, weak_this->cpus_to_drain_); |
| 168 | } |
| 169 | |
| 170 | for (size_t cpu = 0; cpu < weak_this->ftrace_procfs_->NumberOfCpus(); cpu++) { |
| 171 | if (!cpus_to_drain[cpu]) |
| 172 | continue; |
| 173 | weak_this->OnRawFtraceDataAvailable(cpu); |
| 174 | } |
| 175 | |
| 176 | // If we filled up any SHM pages while draining the data, we will have posted |
| 177 | // a task to notify traced about this. Only unblock the readers after this |
| 178 | // notification is sent to make it less likely that they steal CPU time away |
| 179 | // from traced. |
| 180 | weak_this->task_runner_->PostTask( |
| 181 | std::bind(&FtraceController::UnblockReaders, weak_this)); |
| 182 | } |
| 183 | |
| 184 | // static |
| 185 | void FtraceController::UnblockReaders( |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 186 | const base::WeakPtr<FtraceController>& weak_this) { |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 187 | if (!weak_this) |
| 188 | return; |
| 189 | // Unblock all waiting readers to start moving more data into their |
| 190 | // respective staging pipes. |
| 191 | weak_this->data_drained_.notify_all(); |
Hector Dearman | 680a28b | 2018-01-11 18:34:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 194 | void FtraceController::StartIfNeeded() { |
| 195 | if (sinks_.size() > 1) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 196 | return; |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 197 | PERFETTO_CHECK(!sinks_.empty()); |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 198 | { |
| 199 | std::unique_lock<std::mutex> lock(lock_); |
| 200 | PERFETTO_CHECK(!listening_for_raw_trace_data_); |
| 201 | listening_for_raw_trace_data_ = true; |
| 202 | } |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 203 | generation_++; |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 204 | base::WeakPtr<FtraceController> weak_this = weak_factory_.GetWeakPtr(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 205 | for (size_t cpu = 0; cpu < ftrace_procfs_->NumberOfCpus(); cpu++) { |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 206 | readers_.emplace( |
| 207 | cpu, std::unique_ptr<CpuReader>(new CpuReader( |
| 208 | table_.get(), cpu, ftrace_procfs_->OpenPipeForCpu(cpu), |
| 209 | std::bind(&FtraceController::OnDataAvailable, this, weak_this, |
| 210 | generation_, cpu, GetDrainPeriodMs())))); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Hector Dearman | 6ea67b3 | 2018-02-01 15:45:15 +0000 | [diff] [blame] | 214 | uint32_t FtraceController::GetDrainPeriodMs() { |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 215 | if (sinks_.empty()) |
Hector Dearman | 6ea67b3 | 2018-02-01 15:45:15 +0000 | [diff] [blame] | 216 | return kDefaultDrainPeriodMs; |
| 217 | uint32_t min_drain_period_ms = kMaxDrainPeriodMs + 1; |
| 218 | for (const FtraceSink* sink : sinks_) { |
| 219 | if (sink->config().drain_period_ms() < min_drain_period_ms) |
| 220 | min_drain_period_ms = sink->config().drain_period_ms(); |
| 221 | } |
| 222 | return ClampDrainPeriodMs(min_drain_period_ms); |
| 223 | } |
| 224 | |
Hector Dearman | e4cc95d | 2018-01-09 14:32:24 +0000 | [diff] [blame] | 225 | void FtraceController::ClearTrace() { |
| 226 | ftrace_procfs_->ClearTrace(); |
| 227 | } |
| 228 | |
| 229 | void FtraceController::DisableAllEvents() { |
| 230 | ftrace_procfs_->DisableAllEvents(); |
| 231 | } |
| 232 | |
| 233 | void FtraceController::WriteTraceMarker(const std::string& s) { |
| 234 | ftrace_procfs_->WriteTraceMarker(s); |
| 235 | } |
| 236 | |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 237 | void FtraceController::StopIfNeeded() { |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 238 | if (!sinks_.empty()) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 239 | return; |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 240 | { |
| 241 | // Unblock any readers that are waiting for us to drain data. |
| 242 | std::unique_lock<std::mutex> lock(lock_); |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 243 | listening_for_raw_trace_data_ = false; |
| 244 | cpus_to_drain_.reset(); |
| 245 | } |
| 246 | data_drained_.notify_all(); |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 247 | readers_.clear(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 250 | void FtraceController::OnRawFtraceDataAvailable(size_t cpu) { |
| 251 | PERFETTO_CHECK(cpu < ftrace_procfs_->NumberOfCpus()); |
| 252 | CpuReader* reader = readers_[cpu].get(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 253 | using BundleHandle = |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 254 | protozero::MessageHandle<protos::pbzero::FtraceEventBundle>; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 255 | std::array<const EventFilter*, kMaxSinks> filters{}; |
| 256 | std::array<BundleHandle, kMaxSinks> bundles{}; |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 257 | std::array<FtraceMetadata*, kMaxSinks> metadatas{}; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 258 | size_t sink_count = sinks_.size(); |
| 259 | size_t i = 0; |
| 260 | for (FtraceSink* sink : sinks_) { |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 261 | filters[i] = sink->event_filter(); |
| 262 | metadatas[i] = sink->metadata_mutable(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 263 | bundles[i++] = sink->GetBundleForCpu(cpu); |
| 264 | } |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 265 | reader->Drain(filters, bundles, metadatas); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 266 | i = 0; |
| 267 | for (FtraceSink* sink : sinks_) |
| 268 | sink->OnBundleComplete(cpu, std::move(bundles[i++])); |
| 269 | PERFETTO_DCHECK(sinks_.size() == sink_count); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | std::unique_ptr<FtraceSink> FtraceController::CreateSink( |
Florian Mayer | 380e004 | 2018-03-08 10:23:34 +0000 | [diff] [blame] | 273 | FtraceConfig config, |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 274 | FtraceSink::Delegate* delegate) { |
| 275 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 276 | if (sinks_.size() >= kMaxSinks) |
| 277 | return nullptr; |
Hector Dearman | cb4ba32 | 2018-02-09 14:12:27 +0000 | [diff] [blame] | 278 | if (!ValidConfig(config)) |
| 279 | return nullptr; |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 280 | |
| 281 | FtraceConfigId id = ftrace_config_muxer_->RequestConfig(config); |
| 282 | if (!id) |
| 283 | return nullptr; |
| 284 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 285 | auto controller_weak = weak_factory_.GetWeakPtr(); |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 286 | auto filter = std::unique_ptr<EventFilter>(new EventFilter( |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 287 | *table_, FtraceEventsAsSet(*ftrace_config_muxer_->GetConfig(id)))); |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 288 | |
Florian Mayer | 380e004 | 2018-03-08 10:23:34 +0000 | [diff] [blame] | 289 | auto sink = std::unique_ptr<FtraceSink>( |
| 290 | new FtraceSink(std::move(controller_weak), id, std::move(config), |
| 291 | std::move(filter), delegate)); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 292 | Register(sink.get()); |
| 293 | return sink; |
| 294 | } |
| 295 | |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 296 | void FtraceController::OnDataAvailable( |
| 297 | base::WeakPtr<FtraceController> weak_this, |
| 298 | size_t generation, |
| 299 | size_t cpu, |
| 300 | uint32_t drain_period_ms) { |
| 301 | // Called on the worker thread. |
| 302 | PERFETTO_DCHECK(cpu < ftrace_procfs_->NumberOfCpus()); |
| 303 | std::unique_lock<std::mutex> lock(lock_); |
| 304 | if (!listening_for_raw_trace_data_) |
| 305 | return; |
| 306 | if (cpus_to_drain_.none()) { |
| 307 | // If this was the first CPU to wake up, schedule a drain for the next drain |
| 308 | // interval. |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 309 | uint32_t delay_ms = drain_period_ms - (NowMs() % drain_period_ms); |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 310 | task_runner_->PostDelayedTask( |
| 311 | std::bind(&FtraceController::DrainCPUs, weak_this, generation), |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 312 | delay_ms); |
Sami Kyostila | 247110b | 2018-02-14 10:18:45 +0000 | [diff] [blame] | 313 | } |
| 314 | cpus_to_drain_[cpu] = true; |
| 315 | |
| 316 | // Wait until the main thread has finished draining. |
| 317 | // TODO(skyostil): The threads waiting here will all try to grab lock_ |
| 318 | // when woken up. Find a way to avoid this. |
| 319 | data_drained_.wait(lock, [this, cpu] { |
| 320 | return !cpus_to_drain_[cpu] || !listening_for_raw_trace_data_; |
| 321 | }); |
| 322 | } |
| 323 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 324 | void FtraceController::Register(FtraceSink* sink) { |
| 325 | PERFETTO_DCHECK_THREAD(thread_checker_); |
| 326 | auto it_and_inserted = sinks_.insert(sink); |
| 327 | PERFETTO_DCHECK(it_and_inserted.second); |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 328 | StartIfNeeded(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void FtraceController::Unregister(FtraceSink* sink) { |
| 332 | PERFETTO_DCHECK_THREAD(thread_checker_); |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 333 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 334 | size_t removed = sinks_.erase(sink); |
| 335 | PERFETTO_DCHECK(removed == 1); |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 336 | |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 337 | ftrace_config_muxer_->RemoveConfig(sink->id_); |
| 338 | |
Hector Dearman | 5c87a75 | 2017-12-07 12:40:04 +0000 | [diff] [blame] | 339 | StopIfNeeded(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | FtraceSink::FtraceSink(base::WeakPtr<FtraceController> controller_weak, |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 343 | FtraceConfigId id, |
Hector Dearman | c61e883 | 2018-01-25 12:10:12 +0000 | [diff] [blame] | 344 | FtraceConfig config, |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 345 | std::unique_ptr<EventFilter> filter, |
| 346 | Delegate* delegate) |
| 347 | : controller_weak_(std::move(controller_weak)), |
Hector Dearman | 5013971 | 2018-02-23 15:43:41 +0000 | [diff] [blame] | 348 | id_(id), |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 349 | config_(std::move(config)), |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 350 | filter_(std::move(filter)), |
| 351 | delegate_(delegate){}; |
| 352 | |
| 353 | FtraceSink::~FtraceSink() { |
| 354 | if (controller_weak_) |
| 355 | controller_weak_->Unregister(this); |
| 356 | }; |
| 357 | |
| 358 | const std::set<std::string>& FtraceSink::enabled_events() { |
| 359 | return filter_->enabled_names(); |
| 360 | } |
| 361 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 362 | FtraceMetadata::FtraceMetadata() { |
| 363 | // A lot of the time there will only be a small number of inodes. |
Anna Zappone | 8ce3087 | 2018-03-19 17:01:15 +0000 | [diff] [blame] | 364 | inode_and_device.reserve(10); |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 365 | pids.reserve(10); |
| 366 | } |
| 367 | |
Anna Zappone | 8ce3087 | 2018-03-19 17:01:15 +0000 | [diff] [blame] | 368 | void FtraceMetadata::AddDevice(BlockDeviceID device_id) { |
| 369 | last_seen_device_id = device_id; |
Florian Mayer | 3174615 | 2018-03-23 15:42:05 +0000 | [diff] [blame] | 370 | #if PERFETTO_DCHECK_IS_ON() |
| 371 | seen_device_id = true; |
| 372 | #endif |
Anna Zappone | 4ea73c0 | 2018-03-09 16:01:21 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Anna Zappone | 8ce3087 | 2018-03-19 17:01:15 +0000 | [diff] [blame] | 375 | void FtraceMetadata::AddInode(Inode inode_number) { |
Florian Mayer | 3174615 | 2018-03-23 15:42:05 +0000 | [diff] [blame] | 376 | #if PERFETTO_DCHECK_IS_ON() |
| 377 | PERFETTO_DCHECK(seen_device_id); |
| 378 | #endif |
Florian Mayer | b8e235b | 2018-03-22 20:32:50 +0000 | [diff] [blame] | 379 | static int32_t cached_pid = 0; |
| 380 | if (!cached_pid) |
| 381 | cached_pid = getpid(); |
| 382 | |
| 383 | PERFETTO_DCHECK(last_seen_common_pid); |
| 384 | PERFETTO_DCHECK(cached_pid == getpid()); |
| 385 | // Ignore own scanning activity. |
| 386 | if (cached_pid != last_seen_common_pid) { |
| 387 | inode_and_device.push_back( |
| 388 | std::make_pair(inode_number, last_seen_device_id)); |
| 389 | } |
Anna Zappone | 4ea73c0 | 2018-03-09 16:01:21 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Florian Mayer | b8e235b | 2018-03-22 20:32:50 +0000 | [diff] [blame] | 392 | void FtraceMetadata::AddCommonPid(int32_t pid) { |
| 393 | last_seen_common_pid = pid; |
| 394 | } |
| 395 | |
Hector Dearman | f19e7c5 | 2018-03-06 10:25:01 +0000 | [diff] [blame] | 396 | void FtraceMetadata::AddPid(int32_t pid) { |
| 397 | // Speculative optimization aginst repated pid's while keeping |
| 398 | // faster insertion than a set. |
Florian Mayer | 22e4b39 | 2018-03-08 10:20:11 +0000 | [diff] [blame] | 399 | if (!pids.empty() && pids.back() == pid) |
Hector Dearman | f19e7c5 | 2018-03-06 10:25:01 +0000 | [diff] [blame] | 400 | return; |
| 401 | pids.push_back(pid); |
| 402 | } |
| 403 | |
Florian Mayer | 3174615 | 2018-03-23 15:42:05 +0000 | [diff] [blame] | 404 | void FtraceMetadata::FinishEvent() { |
| 405 | last_seen_device_id = 0; |
| 406 | #if PERFETTO_DCHECK_IS_ON() |
| 407 | seen_device_id = false; |
| 408 | #endif |
| 409 | last_seen_common_pid = 0; |
| 410 | } |
| 411 | |
Hector Dearman | f19e7c5 | 2018-03-06 10:25:01 +0000 | [diff] [blame] | 412 | void FtraceMetadata::Clear() { |
Anna Zappone | 8ce3087 | 2018-03-19 17:01:15 +0000 | [diff] [blame] | 413 | inode_and_device.clear(); |
Hector Dearman | f19e7c5 | 2018-03-06 10:25:01 +0000 | [diff] [blame] | 414 | pids.clear(); |
| 415 | overwrite_count = 0; |
Florian Mayer | 3174615 | 2018-03-23 15:42:05 +0000 | [diff] [blame] | 416 | FinishEvent(); |
Hector Dearman | f19e7c5 | 2018-03-06 10:25:01 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 419 | FtraceSink::Delegate::~Delegate() = default; |
| 420 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 421 | } // namespace perfetto |