Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "src/traced/probes/probes_producer.h" |
| 18 | |
| 19 | #include <stdio.h> |
Anna Zappone | 4ea73c0 | 2018-03-09 16:01:21 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Primiano Tucci | 5252660 | 2018-03-29 22:53:10 +0100 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 23 | #include <queue> |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | #include "perfetto/base/logging.h" |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 27 | #include "perfetto/base/utils.h" |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 28 | #include "perfetto/base/weak_ptr.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 29 | #include "perfetto/traced/traced.h" |
| 30 | #include "perfetto/tracing/core/data_source_config.h" |
| 31 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 32 | #include "perfetto/tracing/core/ftrace_config.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 33 | #include "perfetto/tracing/core/trace_config.h" |
| 34 | #include "perfetto/tracing/core/trace_packet.h" |
Primiano Tucci | c2eb510 | 2018-05-15 10:40:01 +0100 | [diff] [blame] | 35 | #include "perfetto/tracing/ipc/producer_ipc_client.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 36 | #include "src/traced/probes/filesystem/inode_file_data_source.h" |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 37 | #include "src/traced/probes/ftrace/ftrace_data_source.h" |
| 38 | #include "src/traced/probes/probes_data_source.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 39 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 40 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 41 | #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" |
Hector Dearman | 3afb1e0 | 2018-04-27 16:46:52 +0100 | [diff] [blame] | 42 | #include "perfetto/trace/ftrace/ftrace_stats.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 43 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 44 | |
| 45 | namespace perfetto { |
| 46 | namespace { |
| 47 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 48 | constexpr uint32_t kInitialConnectionBackoffMs = 100; |
| 49 | constexpr uint32_t kMaxConnectionBackoffMs = 30 * 1000; |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 50 | constexpr char kFtraceSourceName[] = "linux.ftrace"; |
| 51 | constexpr char kProcessStatsSourceName[] = "linux.process_stats"; |
| 52 | constexpr char kInodeMapSourceName[] = "linux.inode_file_map"; |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 53 | constexpr char kSysStatsSourceName[] = "linux.sys_stats"; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 54 | |
| 55 | } // namespace. |
| 56 | |
| 57 | // State transition diagram: |
| 58 | // +----------------------------+ |
| 59 | // v + |
| 60 | // NotStarted -> NotConnected -> Connecting -> Connected |
| 61 | // ^ + |
| 62 | // +--------------+ |
| 63 | // |
| 64 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 65 | ProbesProducer::ProbesProducer() : weak_factory_(this) {} |
| 66 | ProbesProducer::~ProbesProducer() { |
| 67 | // The ftrace data sources must be deleted before the ftrace controller. |
| 68 | data_sources_.clear(); |
| 69 | ftrace_.reset(); |
| 70 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 71 | |
| 72 | void ProbesProducer::OnConnect() { |
| 73 | PERFETTO_DCHECK(state_ == kConnecting); |
| 74 | state_ = kConnected; |
| 75 | ResetConnectionBackoff(); |
| 76 | PERFETTO_LOG("Connected to the service"); |
| 77 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 78 | { |
| 79 | DataSourceDescriptor desc; |
| 80 | desc.set_name(kFtraceSourceName); |
| 81 | endpoint_->RegisterDataSource(desc); |
| 82 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 83 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 84 | { |
| 85 | DataSourceDescriptor desc; |
| 86 | desc.set_name(kProcessStatsSourceName); |
| 87 | endpoint_->RegisterDataSource(desc); |
| 88 | } |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 89 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 90 | { |
| 91 | DataSourceDescriptor desc; |
| 92 | desc.set_name(kInodeMapSourceName); |
| 93 | endpoint_->RegisterDataSource(desc); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | DataSourceDescriptor desc; |
| 98 | desc.set_name(kSysStatsSourceName); |
| 99 | endpoint_->RegisterDataSource(desc); |
| 100 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void ProbesProducer::OnDisconnect() { |
| 104 | PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 105 | PERFETTO_LOG("Disconnected from tracing service"); |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 106 | if (state_ == kConnected) |
| 107 | return task_runner_->PostTask([this] { this->Restart(); }); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 108 | |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 109 | state_ = kNotConnected; |
| 110 | IncreaseConnectionBackoff(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 111 | task_runner_->PostDelayedTask([this] { this->Connect(); }, |
| 112 | connection_backoff_ms_); |
| 113 | } |
| 114 | |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 115 | void ProbesProducer::Restart() { |
| 116 | // We lost the connection with the tracing service. At this point we need |
| 117 | // to reset all the data sources. Trying to handle that manually is going to |
| 118 | // be error prone. What we do here is simply desroying the instance and |
| 119 | // recreating it again. |
| 120 | // TODO(hjd): Add e2e test for this. |
| 121 | |
| 122 | base::TaskRunner* task_runner = task_runner_; |
| 123 | const char* socket_name = socket_name_; |
| 124 | |
| 125 | // Invoke destructor and then the constructor again. |
| 126 | this->~ProbesProducer(); |
| 127 | new (this) ProbesProducer(); |
| 128 | |
| 129 | ConnectWithRetries(socket_name, task_runner); |
| 130 | } |
| 131 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 132 | void ProbesProducer::SetupDataSource(DataSourceInstanceID instance_id, |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 133 | const DataSourceConfig& config) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 134 | PERFETTO_DLOG("SetupDataSource(id=%" PRIu64 ", name=%s)", instance_id, |
| 135 | config.name().c_str()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 136 | PERFETTO_DCHECK(data_sources_.count(instance_id) == 0); |
Primiano Tucci | 03de28f | 2018-08-01 11:29:46 +0100 | [diff] [blame] | 137 | TracingSessionID session_id = config.tracing_session_id(); |
| 138 | PERFETTO_CHECK(session_id > 0); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 139 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 140 | std::unique_ptr<ProbesDataSource> data_source; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 141 | if (config.name() == kFtraceSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 142 | data_source = CreateFtraceDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 143 | } else if (config.name() == kInodeMapSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 144 | data_source = CreateInodeFileDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 145 | } else if (config.name() == kProcessStatsSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 146 | data_source = CreateProcessStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 147 | } else if (config.name() == kSysStatsSourceName) { |
| 148 | data_source = CreateSysStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (!data_source) { |
| 152 | PERFETTO_ELOG("Failed to create data source '%s'", config.name().c_str()); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 156 | session_data_sources_.emplace(session_id, data_source.get()); |
| 157 | data_sources_[instance_id] = std::move(data_source); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 158 | } |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 159 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 160 | void ProbesProducer::StartDataSource(DataSourceInstanceID instance_id, |
| 161 | const DataSourceConfig& config) { |
| 162 | PERFETTO_DLOG("StartDataSource(id=%" PRIu64 ", name=%s)", instance_id, |
| 163 | config.name().c_str()); |
| 164 | auto it = data_sources_.find(instance_id); |
| 165 | if (it == data_sources_.end()) { |
| 166 | PERFETTO_ELOG("Data source id=%" PRIu64 " not found", instance_id); |
| 167 | return; |
| 168 | } |
| 169 | ProbesDataSource* data_source = it->second.get(); |
| 170 | if (data_source->started) |
| 171 | return; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 172 | if (config.trace_duration_ms() != 0) { |
| 173 | uint32_t timeout = 5000 + 2 * config.trace_duration_ms(); |
| 174 | watchdogs_.emplace( |
| 175 | instance_id, base::Watchdog::GetInstance()->CreateFatalTimer(timeout)); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 176 | } |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 177 | data_source->started = true; |
| 178 | data_source->Start(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 181 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateFtraceDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 182 | TracingSessionID session_id, |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 183 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 184 | const DataSourceConfig& config) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 185 | // Don't retry if FtraceController::Create() failed once. |
| 186 | // This can legitimately happen on user builds where we cannot access the |
| 187 | // debug paths, e.g., because of SELinux rules. |
| 188 | if (ftrace_creation_failed_) |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 189 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 190 | |
| 191 | // Lazily create on the first instance. |
| 192 | if (!ftrace_) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 193 | ftrace_ = FtraceController::Create(task_runner_, this); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 194 | |
| 195 | if (!ftrace_) { |
| 196 | PERFETTO_ELOG("Failed to create FtraceController"); |
| 197 | ftrace_creation_failed_ = true; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 198 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | ftrace_->DisableAllEvents(); |
| 202 | ftrace_->ClearTrace(); |
| 203 | } |
| 204 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 205 | PERFETTO_LOG("Ftrace setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 206 | config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 207 | const BufferID buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 208 | std::unique_ptr<FtraceDataSource> data_source(new FtraceDataSource( |
| 209 | ftrace_->GetWeakPtr(), session_id, config.ftrace_config(), |
| 210 | endpoint_->CreateTraceWriter(buffer_id))); |
| 211 | if (!ftrace_->AddDataSource(data_source.get())) { |
| 212 | PERFETTO_ELOG( |
| 213 | "Failed to start tracing (too many concurrent sessions or ftrace is " |
| 214 | "already in use)"); |
| 215 | return nullptr; |
Hector Dearman | ee3c49d | 2018-02-28 14:10:22 +0000 | [diff] [blame] | 216 | } |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 217 | return std::move(data_source); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 220 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateInodeFileDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 221 | TracingSessionID session_id, |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 222 | DataSourceInstanceID id, |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 223 | DataSourceConfig source_config) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 224 | PERFETTO_LOG("Inode file map setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")", |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 225 | id, source_config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 226 | auto buffer_id = static_cast<BufferID>(source_config.target_buffer()); |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 227 | if (system_inodes_.empty()) |
Hector Dearman | 7fabd70 | 2018-03-28 12:37:15 +0100 | [diff] [blame] | 228 | CreateStaticDeviceToInodeMap("/system", &system_inodes_); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 229 | return std::unique_ptr<InodeFileDataSource>(new InodeFileDataSource( |
| 230 | std::move(source_config), task_runner_, session_id, &system_inodes_, |
| 231 | &cache_, endpoint_->CreateTraceWriter(buffer_id))); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 234 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateProcessStatsDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 235 | TracingSessionID session_id, |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 236 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 237 | const DataSourceConfig& config) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 238 | base::ignore_result(id); |
| 239 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 240 | auto data_source = |
| 241 | std::unique_ptr<ProcessStatsDataSource>(new ProcessStatsDataSource( |
| 242 | session_id, endpoint_->CreateTraceWriter(buffer_id), config)); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 243 | return std::move(data_source); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 246 | std::unique_ptr<SysStatsDataSource> ProbesProducer::CreateSysStatsDataSource( |
| 247 | TracingSessionID session_id, |
| 248 | DataSourceInstanceID id, |
| 249 | const DataSourceConfig& config) { |
| 250 | base::ignore_result(id); |
| 251 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 252 | auto data_source = std::unique_ptr<SysStatsDataSource>( |
| 253 | new SysStatsDataSource(task_runner_, session_id, |
| 254 | endpoint_->CreateTraceWriter(buffer_id), config)); |
| 255 | return data_source; |
| 256 | } |
| 257 | |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 258 | void ProbesProducer::StopDataSource(DataSourceInstanceID id) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 259 | PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 260 | auto it = data_sources_.find(id); |
| 261 | if (it == data_sources_.end()) { |
| 262 | PERFETTO_ELOG("Cannot stop data source id=%" PRIu64 ", not found", id); |
| 263 | return; |
| 264 | } |
| 265 | ProbesDataSource* data_source = it->second.get(); |
| 266 | TracingSessionID session_id = data_source->tracing_session_id; |
| 267 | auto range = session_data_sources_.equal_range(session_id); |
| 268 | for (auto kv = range.first; kv != range.second; kv++) { |
| 269 | if (kv->second != data_source) |
| 270 | continue; |
| 271 | session_data_sources_.erase(kv); |
| 272 | break; |
| 273 | } |
| 274 | data_sources_.erase(it); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 275 | watchdogs_.erase(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 278 | void ProbesProducer::OnTracingSetup() {} |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 279 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 280 | void ProbesProducer::Flush(FlushRequestID flush_request_id, |
| 281 | const DataSourceInstanceID* data_source_ids, |
| 282 | size_t num_data_sources) { |
| 283 | for (size_t i = 0; i < num_data_sources; i++) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 284 | auto it = data_sources_.find(data_source_ids[i]); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 285 | if (it == data_sources_.end() || !it->second->started) |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 286 | continue; |
| 287 | it->second->Flush(); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 288 | } |
| 289 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 290 | } |
| 291 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 292 | // This function is called by the FtraceController in batches, whenever it has |
| 293 | // read one or more pages from one or more cpus and written that into the |
| 294 | // userspace tracing buffer. If more than one ftrace data sources are active, |
| 295 | // this call typically happens after writing for all session has been handled. |
| 296 | void ProbesProducer::OnFtraceDataWrittenIntoDataSourceBuffers() { |
| 297 | TracingSessionID last_session_id = 0; |
| 298 | FtraceMetadata* metadata = nullptr; |
| 299 | InodeFileDataSource* inode_data_source = nullptr; |
| 300 | ProcessStatsDataSource* ps_data_source = nullptr; |
| 301 | |
| 302 | // unordered_multimap guarantees that entries with the same key are contiguous |
| 303 | // in the iteration. |
| 304 | for (auto it = session_data_sources_.begin(); /* check below*/; it++) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 305 | // If this is the last iteration or the session id has changed, |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 306 | // dispatch the metadata update to the linked data sources, if any. |
| 307 | if (it == session_data_sources_.end() || it->first != last_session_id) { |
| 308 | bool has_inodes = metadata && !metadata->inode_and_device.empty(); |
| 309 | bool has_pids = metadata && !metadata->pids.empty(); |
| 310 | if (has_inodes && inode_data_source) |
| 311 | inode_data_source->OnInodes(metadata->inode_and_device); |
| 312 | if (has_pids && ps_data_source) |
| 313 | ps_data_source->OnPids(metadata->pids); |
| 314 | if (metadata) |
| 315 | metadata->Clear(); |
| 316 | metadata = nullptr; |
| 317 | inode_data_source = nullptr; |
| 318 | ps_data_source = nullptr; |
| 319 | if (it == session_data_sources_.end()) |
| 320 | break; |
| 321 | last_session_id = it->first; |
| 322 | } |
| 323 | ProbesDataSource* ds = it->second; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame^] | 324 | if (!ds->started) |
| 325 | continue; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 326 | switch (ds->type_id) { |
| 327 | case FtraceDataSource::kTypeId: |
| 328 | metadata = static_cast<FtraceDataSource*>(ds)->mutable_metadata(); |
| 329 | break; |
| 330 | case InodeFileDataSource::kTypeId: |
| 331 | inode_data_source = static_cast<InodeFileDataSource*>(ds); |
| 332 | break; |
| 333 | case ProcessStatsDataSource::kTypeId: |
| 334 | ps_data_source = static_cast<ProcessStatsDataSource*>(ds); |
| 335 | break; |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 336 | case SysStatsDataSource::kTypeId: |
| 337 | break; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 338 | default: |
| 339 | PERFETTO_DCHECK(false); |
| 340 | } // switch (type_id) |
| 341 | } // for (session_data_sources_) |
| 342 | } |
| 343 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 344 | void ProbesProducer::ConnectWithRetries(const char* socket_name, |
| 345 | base::TaskRunner* task_runner) { |
| 346 | PERFETTO_DCHECK(state_ == kNotStarted); |
| 347 | state_ = kNotConnected; |
| 348 | |
| 349 | ResetConnectionBackoff(); |
| 350 | socket_name_ = socket_name; |
| 351 | task_runner_ = task_runner; |
| 352 | Connect(); |
| 353 | } |
| 354 | |
| 355 | void ProbesProducer::Connect() { |
| 356 | PERFETTO_DCHECK(state_ == kNotConnected); |
| 357 | state_ = kConnecting; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 358 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 359 | socket_name_, this, "perfetto.traced_probes", task_runner_); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void ProbesProducer::IncreaseConnectionBackoff() { |
| 363 | connection_backoff_ms_ *= 2; |
| 364 | if (connection_backoff_ms_ > kMaxConnectionBackoffMs) |
| 365 | connection_backoff_ms_ = kMaxConnectionBackoffMs; |
| 366 | } |
| 367 | |
| 368 | void ProbesProducer::ResetConnectionBackoff() { |
| 369 | connection_backoff_ms_ = kInitialConnectionBackoffMs; |
| 370 | } |
| 371 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 372 | } // namespace perfetto |