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 | |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 132 | void ProbesProducer::CreateDataSourceInstance(DataSourceInstanceID instance_id, |
| 133 | const DataSourceConfig& config) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 134 | PERFETTO_DCHECK(data_sources_.count(instance_id) == 0); |
Primiano Tucci | 03de28f | 2018-08-01 11:29:46 +0100 | [diff] [blame] | 135 | TracingSessionID session_id = config.tracing_session_id(); |
| 136 | PERFETTO_CHECK(session_id > 0); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 137 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 138 | std::unique_ptr<ProbesDataSource> data_source; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 139 | if (config.name() == kFtraceSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 140 | data_source = CreateFtraceDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 141 | } else if (config.name() == kInodeMapSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 142 | data_source = CreateInodeFileDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 143 | } else if (config.name() == kProcessStatsSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 144 | data_source = CreateProcessStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame^] | 145 | } else if (config.name() == kSysStatsSourceName) { |
| 146 | data_source = CreateSysStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (!data_source) { |
| 150 | PERFETTO_ELOG("Failed to create data source '%s'", config.name().c_str()); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 154 | session_data_sources_.emplace(session_id, data_source.get()); |
| 155 | data_sources_[instance_id] = std::move(data_source); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 156 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 157 | if (config.trace_duration_ms() != 0) { |
| 158 | uint32_t timeout = 5000 + 2 * config.trace_duration_ms(); |
| 159 | watchdogs_.emplace( |
| 160 | instance_id, base::Watchdog::GetInstance()->CreateFatalTimer(timeout)); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 164 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateFtraceDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 165 | TracingSessionID session_id, |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 166 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 167 | const DataSourceConfig& config) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 168 | // Don't retry if FtraceController::Create() failed once. |
| 169 | // This can legitimately happen on user builds where we cannot access the |
| 170 | // debug paths, e.g., because of SELinux rules. |
| 171 | if (ftrace_creation_failed_) |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 172 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 173 | |
| 174 | // Lazily create on the first instance. |
| 175 | if (!ftrace_) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 176 | ftrace_ = FtraceController::Create(task_runner_, this); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 177 | |
| 178 | if (!ftrace_) { |
| 179 | PERFETTO_ELOG("Failed to create FtraceController"); |
| 180 | ftrace_creation_failed_ = true; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 181 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ftrace_->DisableAllEvents(); |
| 185 | ftrace_->ClearTrace(); |
| 186 | } |
| 187 | |
| 188 | PERFETTO_LOG("Ftrace start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 189 | config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 190 | const BufferID buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 191 | std::unique_ptr<FtraceDataSource> data_source(new FtraceDataSource( |
| 192 | ftrace_->GetWeakPtr(), session_id, config.ftrace_config(), |
| 193 | endpoint_->CreateTraceWriter(buffer_id))); |
| 194 | if (!ftrace_->AddDataSource(data_source.get())) { |
| 195 | PERFETTO_ELOG( |
| 196 | "Failed to start tracing (too many concurrent sessions or ftrace is " |
| 197 | "already in use)"); |
| 198 | return nullptr; |
Hector Dearman | ee3c49d | 2018-02-28 14:10:22 +0000 | [diff] [blame] | 199 | } |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 200 | return std::move(data_source); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 203 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateInodeFileDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 204 | TracingSessionID session_id, |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 205 | DataSourceInstanceID id, |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 206 | DataSourceConfig source_config) { |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 207 | PERFETTO_LOG("Inode file map start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", |
| 208 | id, source_config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 209 | auto buffer_id = static_cast<BufferID>(source_config.target_buffer()); |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 210 | if (system_inodes_.empty()) |
Hector Dearman | 7fabd70 | 2018-03-28 12:37:15 +0100 | [diff] [blame] | 211 | CreateStaticDeviceToInodeMap("/system", &system_inodes_); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 212 | return std::unique_ptr<InodeFileDataSource>(new InodeFileDataSource( |
| 213 | std::move(source_config), task_runner_, session_id, &system_inodes_, |
| 214 | &cache_, endpoint_->CreateTraceWriter(buffer_id))); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 217 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateProcessStatsDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 218 | TracingSessionID session_id, |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 219 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 220 | const DataSourceConfig& config) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 221 | base::ignore_result(id); |
| 222 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 223 | auto data_source = |
| 224 | std::unique_ptr<ProcessStatsDataSource>(new ProcessStatsDataSource( |
| 225 | session_id, endpoint_->CreateTraceWriter(buffer_id), config)); |
Primiano Tucci | e8d7595 | 2018-05-02 11:30:12 +0100 | [diff] [blame] | 226 | if (config.process_stats_config().scan_all_processes_on_start()) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 227 | data_source->WriteAllProcesses(); |
Primiano Tucci | e8d7595 | 2018-05-02 11:30:12 +0100 | [diff] [blame] | 228 | } |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 229 | return std::move(data_source); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame^] | 232 | std::unique_ptr<SysStatsDataSource> ProbesProducer::CreateSysStatsDataSource( |
| 233 | TracingSessionID session_id, |
| 234 | DataSourceInstanceID id, |
| 235 | const DataSourceConfig& config) { |
| 236 | base::ignore_result(id); |
| 237 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 238 | auto data_source = std::unique_ptr<SysStatsDataSource>( |
| 239 | new SysStatsDataSource(task_runner_, session_id, |
| 240 | endpoint_->CreateTraceWriter(buffer_id), config)); |
| 241 | return data_source; |
| 242 | } |
| 243 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 244 | void ProbesProducer::TearDownDataSourceInstance(DataSourceInstanceID id) { |
| 245 | PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 246 | auto it = data_sources_.find(id); |
| 247 | if (it == data_sources_.end()) { |
| 248 | PERFETTO_ELOG("Cannot stop data source id=%" PRIu64 ", not found", id); |
| 249 | return; |
| 250 | } |
| 251 | ProbesDataSource* data_source = it->second.get(); |
| 252 | TracingSessionID session_id = data_source->tracing_session_id; |
| 253 | auto range = session_data_sources_.equal_range(session_id); |
| 254 | for (auto kv = range.first; kv != range.second; kv++) { |
| 255 | if (kv->second != data_source) |
| 256 | continue; |
| 257 | session_data_sources_.erase(kv); |
| 258 | break; |
| 259 | } |
| 260 | data_sources_.erase(it); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 261 | watchdogs_.erase(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 264 | void ProbesProducer::OnTracingSetup() {} |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 265 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 266 | void ProbesProducer::Flush(FlushRequestID flush_request_id, |
| 267 | const DataSourceInstanceID* data_source_ids, |
| 268 | size_t num_data_sources) { |
| 269 | for (size_t i = 0; i < num_data_sources; i++) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 270 | auto it = data_sources_.find(data_source_ids[i]); |
| 271 | if (it == data_sources_.end()) |
| 272 | continue; |
| 273 | it->second->Flush(); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 274 | } |
| 275 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 276 | } |
| 277 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 278 | // This function is called by the FtraceController in batches, whenever it has |
| 279 | // read one or more pages from one or more cpus and written that into the |
| 280 | // userspace tracing buffer. If more than one ftrace data sources are active, |
| 281 | // this call typically happens after writing for all session has been handled. |
| 282 | void ProbesProducer::OnFtraceDataWrittenIntoDataSourceBuffers() { |
| 283 | TracingSessionID last_session_id = 0; |
| 284 | FtraceMetadata* metadata = nullptr; |
| 285 | InodeFileDataSource* inode_data_source = nullptr; |
| 286 | ProcessStatsDataSource* ps_data_source = nullptr; |
| 287 | |
| 288 | // unordered_multimap guarantees that entries with the same key are contiguous |
| 289 | // in the iteration. |
| 290 | for (auto it = session_data_sources_.begin(); /* check below*/; it++) { |
| 291 | // If this is the last iteration or this is the session id has changed, |
| 292 | // dispatch the metadata update to the linked data sources, if any. |
| 293 | if (it == session_data_sources_.end() || it->first != last_session_id) { |
| 294 | bool has_inodes = metadata && !metadata->inode_and_device.empty(); |
| 295 | bool has_pids = metadata && !metadata->pids.empty(); |
| 296 | if (has_inodes && inode_data_source) |
| 297 | inode_data_source->OnInodes(metadata->inode_and_device); |
| 298 | if (has_pids && ps_data_source) |
| 299 | ps_data_source->OnPids(metadata->pids); |
| 300 | if (metadata) |
| 301 | metadata->Clear(); |
| 302 | metadata = nullptr; |
| 303 | inode_data_source = nullptr; |
| 304 | ps_data_source = nullptr; |
| 305 | if (it == session_data_sources_.end()) |
| 306 | break; |
| 307 | last_session_id = it->first; |
| 308 | } |
| 309 | ProbesDataSource* ds = it->second; |
| 310 | switch (ds->type_id) { |
| 311 | case FtraceDataSource::kTypeId: |
| 312 | metadata = static_cast<FtraceDataSource*>(ds)->mutable_metadata(); |
| 313 | break; |
| 314 | case InodeFileDataSource::kTypeId: |
| 315 | inode_data_source = static_cast<InodeFileDataSource*>(ds); |
| 316 | break; |
| 317 | case ProcessStatsDataSource::kTypeId: |
| 318 | ps_data_source = static_cast<ProcessStatsDataSource*>(ds); |
| 319 | break; |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame^] | 320 | case SysStatsDataSource::kTypeId: |
| 321 | break; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 322 | default: |
| 323 | PERFETTO_DCHECK(false); |
| 324 | } // switch (type_id) |
| 325 | } // for (session_data_sources_) |
| 326 | } |
| 327 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 328 | void ProbesProducer::ConnectWithRetries(const char* socket_name, |
| 329 | base::TaskRunner* task_runner) { |
| 330 | PERFETTO_DCHECK(state_ == kNotStarted); |
| 331 | state_ = kNotConnected; |
| 332 | |
| 333 | ResetConnectionBackoff(); |
| 334 | socket_name_ = socket_name; |
| 335 | task_runner_ = task_runner; |
| 336 | Connect(); |
| 337 | } |
| 338 | |
| 339 | void ProbesProducer::Connect() { |
| 340 | PERFETTO_DCHECK(state_ == kNotConnected); |
| 341 | state_ = kConnecting; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 342 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 343 | socket_name_, this, "perfetto.traced_probes", task_runner_); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void ProbesProducer::IncreaseConnectionBackoff() { |
| 347 | connection_backoff_ms_ *= 2; |
| 348 | if (connection_backoff_ms_ > kMaxConnectionBackoffMs) |
| 349 | connection_backoff_ms_ = kMaxConnectionBackoffMs; |
| 350 | } |
| 351 | |
| 352 | void ProbesProducer::ResetConnectionBackoff() { |
| 353 | connection_backoff_ms_ = kInitialConnectionBackoffMs; |
| 354 | } |
| 355 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 356 | } // namespace perfetto |