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 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 17 | #ifndef SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ |
| 18 | #define SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 19 | |
| 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <memory> |
Primiano Tucci | 14219ff | 2019-02-27 12:41:05 +0100 | [diff] [blame] | 23 | #include <mutex> |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 24 | #include <set> |
Stephen Nusko | 5984729 | 2019-03-22 13:54:08 +0000 | [diff] [blame] | 25 | #include <vector> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 26 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 27 | #include "perfetto/base/logging.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame^] | 28 | #include "perfetto/ext/base/optional.h" |
| 29 | #include "perfetto/ext/base/time.h" |
| 30 | #include "perfetto/ext/base/weak_ptr.h" |
| 31 | #include "perfetto/ext/tracing/core/basic_types.h" |
| 32 | #include "perfetto/ext/tracing/core/commit_data_request.h" |
| 33 | #include "perfetto/ext/tracing/core/data_source_descriptor.h" |
| 34 | #include "perfetto/ext/tracing/core/observable_events.h" |
| 35 | #include "perfetto/ext/tracing/core/shared_memory_abi.h" |
| 36 | #include "perfetto/ext/tracing/core/trace_config.h" |
| 37 | #include "perfetto/ext/tracing/core/trace_stats.h" |
| 38 | #include "perfetto/ext/tracing/core/tracing_service.h" |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 39 | #include "src/tracing/core/id_allocator.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 40 | |
| 41 | namespace perfetto { |
| 42 | |
| 43 | namespace base { |
| 44 | class TaskRunner; |
| 45 | } // namespace base |
| 46 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 47 | class Consumer; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 48 | class DataSourceConfig; |
| 49 | class Producer; |
| 50 | class SharedMemory; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 51 | class SharedMemoryArbiterImpl; |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 52 | class TraceBuffer; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 53 | class TraceConfig; |
Sami Kyostila | fbccb3c | 2018-03-21 14:00:47 +0000 | [diff] [blame] | 54 | class TracePacket; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 55 | |
| 56 | // The tracing service business logic. |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 57 | class TracingServiceImpl : public TracingService { |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 58 | private: |
| 59 | struct DataSourceInstance; |
| 60 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 61 | public: |
Primiano Tucci | 1a1951d | 2018-04-04 21:08:16 +0200 | [diff] [blame] | 62 | static constexpr size_t kDefaultShmSize = 256 * 1024ul; |
Primiano Tucci | e7ca7c6 | 2018-04-07 08:28:03 +0200 | [diff] [blame] | 63 | static constexpr size_t kMaxShmSize = 32 * 1024 * 1024ul; |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 64 | static constexpr uint32_t kDataSourceStopTimeoutMs = 5000; |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 65 | static constexpr uint8_t kSyncMarker[] = {0x82, 0x47, 0x7a, 0x76, 0xb2, 0x8d, |
| 66 | 0x42, 0xba, 0x81, 0xdc, 0x33, 0x32, |
| 67 | 0x6d, 0x57, 0xa0, 0x79}; |
Primiano Tucci | 1a1951d | 2018-04-04 21:08:16 +0200 | [diff] [blame] | 68 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 69 | // The implementation behind the service endpoint exposed to each producer. |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 70 | class ProducerEndpointImpl : public TracingService::ProducerEndpoint { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 71 | public: |
| 72 | ProducerEndpointImpl(ProducerID, |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 73 | uid_t uid, |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 74 | TracingServiceImpl*, |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 75 | base::TaskRunner*, |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 76 | Producer*, |
Oystein Eftevaag | c8d2f07 | 2019-03-29 09:41:04 -0700 | [diff] [blame] | 77 | const std::string& producer_name, |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 78 | bool in_process, |
| 79 | bool smb_scraping_enabled); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 80 | ~ProducerEndpointImpl() override; |
| 81 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 82 | // TracingService::ProducerEndpoint implementation. |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 83 | void RegisterDataSource(const DataSourceDescriptor&) override; |
| 84 | void UnregisterDataSource(const std::string& name) override; |
Eric Seckler | 1c4e1ac | 2018-11-29 10:23:14 +0000 | [diff] [blame] | 85 | void RegisterTraceWriter(uint32_t writer_id, |
| 86 | uint32_t target_buffer) override; |
| 87 | void UnregisterTraceWriter(uint32_t writer_id) override; |
Primiano Tucci | 3e69ed9 | 2018-03-14 14:52:29 +0000 | [diff] [blame] | 88 | void CommitData(const CommitDataRequest&, CommitDataCallback) override; |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 89 | void SetSharedMemory(std::unique_ptr<SharedMemory>); |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 90 | std::unique_ptr<TraceWriter> CreateTraceWriter(BufferID) override; |
Oystein Eftevaag | f4cccb5 | 2019-04-17 13:31:29 -0700 | [diff] [blame] | 91 | SharedMemoryArbiter* GetInProcessShmemArbiter() override; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 92 | void NotifyFlushComplete(FlushRequestID) override; |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 93 | void NotifyDataSourceStarted(DataSourceInstanceID) override; |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 94 | void NotifyDataSourceStopped(DataSourceInstanceID) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 95 | SharedMemory* shared_memory() const override; |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 96 | size_t shared_buffer_page_size_kb() const override; |
Stephen Nusko | 1393ffd | 2019-03-22 13:54:58 +0000 | [diff] [blame] | 97 | void ActivateTriggers(const std::vector<std::string>&) override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 98 | |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 99 | void OnTracingSetup(); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 100 | void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&); |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 101 | void StartDataSource(DataSourceInstanceID, const DataSourceConfig&); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 102 | void StopDataSource(DataSourceInstanceID); |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 103 | void Flush(FlushRequestID, const std::vector<DataSourceInstanceID>&); |
Eric Seckler | 6dc2359 | 2018-11-30 10:59:06 +0000 | [diff] [blame] | 104 | void OnFreeBuffers(const std::vector<BufferID>& target_buffers); |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 105 | void ClearIncrementalState(const std::vector<DataSourceInstanceID>&); |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 106 | |
Eric Seckler | dd0ad10 | 2018-12-06 11:32:04 +0000 | [diff] [blame] | 107 | bool is_allowed_target_buffer(BufferID buffer_id) const { |
| 108 | return allowed_target_buffers_.count(buffer_id); |
| 109 | } |
| 110 | |
Eric Seckler | f3f524b | 2018-12-13 09:09:34 +0000 | [diff] [blame] | 111 | base::Optional<BufferID> buffer_id_for_writer(WriterID writer_id) const { |
| 112 | const auto it = writers_.find(writer_id); |
| 113 | if (it != writers_.end()) |
| 114 | return it->second; |
| 115 | return base::nullopt; |
| 116 | } |
| 117 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 118 | private: |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 119 | friend class TracingServiceImpl; |
| 120 | friend class TracingServiceImplTest; |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 121 | friend class TracingIntegrationTest; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 122 | ProducerEndpointImpl(const ProducerEndpointImpl&) = delete; |
| 123 | ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete; |
| 124 | |
| 125 | ProducerID const id_; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 126 | const uid_t uid_; |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 127 | TracingServiceImpl* const service_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 128 | base::TaskRunner* const task_runner_; |
| 129 | Producer* producer_; |
| 130 | std::unique_ptr<SharedMemory> shared_memory_; |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 131 | size_t shared_buffer_page_size_kb_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 132 | SharedMemoryABI shmem_abi_; |
Primiano Tucci | 1a1951d | 2018-04-04 21:08:16 +0200 | [diff] [blame] | 133 | size_t shmem_size_hint_bytes_ = 0; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 134 | const std::string name_; |
Oystein Eftevaag | c8d2f07 | 2019-03-29 09:41:04 -0700 | [diff] [blame] | 135 | bool in_process_; |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 136 | bool smb_scraping_enabled_; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 137 | |
Eric Seckler | 6dc2359 | 2018-11-30 10:59:06 +0000 | [diff] [blame] | 138 | // Set of the global target_buffer IDs that the producer is configured to |
| 139 | // write into in any active tracing session. |
| 140 | std::set<BufferID> allowed_target_buffers_; |
| 141 | |
Eric Seckler | f3f524b | 2018-12-13 09:09:34 +0000 | [diff] [blame] | 142 | // Maps registered TraceWriter IDs to their target buffers as registered by |
| 143 | // the producer. Note that producers aren't required to register their |
| 144 | // writers, so we may see commits of chunks with WriterIDs that aren't |
| 145 | // contained in this map. However, if a producer does register a writer, the |
| 146 | // service will prevent the writer from writing into any other buffer than |
| 147 | // the one associated with it here. The BufferIDs stored in this map are |
| 148 | // untrusted, so need to be verified against |allowed_target_buffers_| |
| 149 | // before use. |
| 150 | std::map<WriterID, BufferID> writers_; |
| 151 | |
Oystein Eftevaag | c8d2f07 | 2019-03-29 09:41:04 -0700 | [diff] [blame] | 152 | // This is used only in in-process configurations. |
Primiano Tucci | 14219ff | 2019-02-27 12:41:05 +0100 | [diff] [blame] | 153 | // SharedMemoryArbiterImpl methods themselves are thread-safe. |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 154 | std::unique_ptr<SharedMemoryArbiterImpl> inproc_shmem_arbiter_; |
Primiano Tucci | 14219ff | 2019-02-27 12:41:05 +0100 | [diff] [blame] | 155 | |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 156 | PERFETTO_THREAD_CHECKER(thread_checker_) |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 157 | base::WeakPtrFactory<ProducerEndpointImpl> weak_ptr_factory_; // Keep last. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 160 | // The implementation behind the service endpoint exposed to each consumer. |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 161 | class ConsumerEndpointImpl : public TracingService::ConsumerEndpoint { |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 162 | public: |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 163 | ConsumerEndpointImpl(TracingServiceImpl*, |
| 164 | base::TaskRunner*, |
| 165 | Consumer*, |
| 166 | uid_t uid); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 167 | ~ConsumerEndpointImpl() override; |
| 168 | |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 169 | void NotifyOnTracingDisabled(); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 170 | base::WeakPtr<ConsumerEndpointImpl> GetWeakPtr(); |
| 171 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 172 | // TracingService::ConsumerEndpoint implementation. |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 173 | void EnableTracing(const TraceConfig&, base::ScopedFile) override; |
Oystein Eftevaag | cb6e4c8 | 2019-03-06 15:38:26 -0800 | [diff] [blame] | 174 | void ChangeTraceConfig(const TraceConfig& cfg) override; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 175 | void StartTracing() override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 176 | void DisableTracing() override; |
| 177 | void ReadBuffers() override; |
| 178 | void FreeBuffers() override; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 179 | void Flush(uint32_t timeout_ms, FlushCallback) override; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 180 | void Detach(const std::string& key) override; |
| 181 | void Attach(const std::string& key) override; |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 182 | void GetTraceStats() override; |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 183 | void ObserveEvents(uint32_t enabled_event_types) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 184 | |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 185 | // If |observe_data_source_instances == true|, will queue a task to notify |
| 186 | // the consumer about the state change. |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 187 | void OnDataSourceInstanceStateChange(const ProducerEndpointImpl&, |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 188 | const DataSourceInstance&); |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 189 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 190 | private: |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 191 | friend class TracingServiceImpl; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 192 | ConsumerEndpointImpl(const ConsumerEndpointImpl&) = delete; |
| 193 | ConsumerEndpointImpl& operator=(const ConsumerEndpointImpl&) = delete; |
| 194 | |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 195 | // Returns a pointer to an ObservableEvents object that the caller can fill |
| 196 | // and schedules a task to send the ObservableEvents to the consumer. |
| 197 | ObservableEvents* AddObservableEvents(); |
| 198 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 199 | base::TaskRunner* const task_runner_; |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 200 | TracingServiceImpl* const service_; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 201 | Consumer* const consumer_; |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 202 | uid_t const uid_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 203 | TracingSessionID tracing_session_id_ = 0; |
Eric Seckler | 7b0c945 | 2019-03-18 13:14:36 +0000 | [diff] [blame] | 204 | |
| 205 | // Whether the consumer is interested in DataSourceInstance state change |
| 206 | // events. |
| 207 | uint32_t enabled_observable_event_types_ = ObservableEventType::kNone; |
| 208 | // ObservableEvents that will be sent to the consumer. If set, a task to |
| 209 | // flush the events to the consumer has been queued. |
| 210 | std::unique_ptr<ObservableEvents> observable_events_; |
| 211 | |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 212 | PERFETTO_THREAD_CHECKER(thread_checker_) |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 213 | base::WeakPtrFactory<ConsumerEndpointImpl> weak_ptr_factory_; // Keep last. |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 216 | explicit TracingServiceImpl(std::unique_ptr<SharedMemory::Factory>, |
| 217 | base::TaskRunner*); |
| 218 | ~TracingServiceImpl() override; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 219 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 220 | // Called by ProducerEndpointImpl. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 221 | void DisconnectProducer(ProducerID); |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 222 | void RegisterDataSource(ProducerID, const DataSourceDescriptor&); |
| 223 | void UnregisterDataSource(ProducerID, const std::string& name); |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 224 | void CopyProducerPageIntoLogBuffer(ProducerID, |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame] | 225 | uid_t, |
| 226 | WriterID, |
| 227 | ChunkID, |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 228 | BufferID, |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame] | 229 | uint16_t num_fragments, |
| 230 | uint8_t chunk_flags, |
Eric Seckler | b77b27e | 2018-12-17 11:42:52 +0000 | [diff] [blame] | 231 | bool chunk_complete, |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame] | 232 | const uint8_t* src, |
| 233 | size_t size); |
| 234 | void ApplyChunkPatches(ProducerID, |
| 235 | const std::vector<CommitDataRequest::ChunkToPatch>&); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 236 | void NotifyFlushDoneForProducer(ProducerID, FlushRequestID); |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 237 | void NotifyDataSourceStarted(ProducerID, const DataSourceInstanceID); |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 238 | void NotifyDataSourceStopped(ProducerID, const DataSourceInstanceID); |
Stephen Nusko | 5984729 | 2019-03-22 13:54:08 +0000 | [diff] [blame] | 239 | void ActivateTriggers(ProducerID, const std::vector<std::string>& triggers); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 240 | |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 241 | // Called by ConsumerEndpointImpl. |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 242 | bool DetachConsumer(ConsumerEndpointImpl*, const std::string& key); |
| 243 | bool AttachConsumer(ConsumerEndpointImpl*, const std::string& key); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 244 | void DisconnectConsumer(ConsumerEndpointImpl*); |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 245 | bool EnableTracing(ConsumerEndpointImpl*, |
| 246 | const TraceConfig&, |
| 247 | base::ScopedFile); |
Oystein Eftevaag | cb6e4c8 | 2019-03-06 15:38:26 -0800 | [diff] [blame] | 248 | void ChangeTraceConfig(ConsumerEndpointImpl*, const TraceConfig&); |
| 249 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 250 | bool StartTracing(TracingSessionID); |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 251 | void DisableTracing(TracingSessionID, bool disable_immediately = false); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 252 | void Flush(TracingSessionID tsid, |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 253 | uint32_t timeout_ms, |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 254 | ConsumerEndpoint::FlushCallback); |
| 255 | void FlushAndDisableTracing(TracingSessionID); |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 256 | void ReadBuffers(TracingSessionID, ConsumerEndpointImpl*); |
| 257 | void FreeBuffers(TracingSessionID); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 258 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 259 | // Service implementation. |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 260 | std::unique_ptr<TracingService::ProducerEndpoint> ConnectProducer( |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 261 | Producer*, |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 262 | uid_t uid, |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 263 | const std::string& producer_name, |
Oystein Eftevaag | c8d2f07 | 2019-03-29 09:41:04 -0700 | [diff] [blame] | 264 | size_t shared_memory_size_hint_bytes = 0, |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 265 | bool in_process = false, |
| 266 | ProducerSMBScrapingMode smb_scraping_mode = |
| 267 | ProducerSMBScrapingMode::kDefault) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 268 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 269 | std::unique_ptr<TracingService::ConsumerEndpoint> ConnectConsumer( |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 270 | Consumer*, |
| 271 | uid_t) override; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 272 | |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 273 | // Set whether SMB scraping should be enabled by default or not. Producers can |
| 274 | // override this setting for their own SMBs. |
Eric Seckler | a01e28a | 2019-01-08 11:21:04 +0000 | [diff] [blame] | 275 | void SetSMBScrapingEnabled(bool enabled) override { |
| 276 | smb_scraping_enabled_ = enabled; |
| 277 | } |
| 278 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 279 | // Exposed mainly for testing. |
| 280 | size_t num_producers() const { return producers_.size(); } |
| 281 | ProducerEndpointImpl* GetProducer(ProducerID) const; |
| 282 | |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 283 | uint32_t override_data_source_test_timeout_ms_for_testing = 0; |
| 284 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 285 | private: |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 286 | friend class TracingServiceImplTest; |
Eric Seckler | 062ed50 | 2019-05-23 09:02:57 +0100 | [diff] [blame] | 287 | friend class TracingIntegrationTest; |
Primiano Tucci | 081d46a | 2018-02-28 11:09:43 +0000 | [diff] [blame] | 288 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 289 | struct RegisteredDataSource { |
| 290 | ProducerID producer_id; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 291 | DataSourceDescriptor descriptor; |
| 292 | }; |
| 293 | |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 294 | // Represents an active data source for a tracing session. |
| 295 | struct DataSourceInstance { |
Primiano Tucci | 5403e4f | 2018-11-27 10:07:03 +0000 | [diff] [blame] | 296 | DataSourceInstance(DataSourceInstanceID id, |
| 297 | const DataSourceConfig& cfg, |
| 298 | const std::string& ds_name, |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 299 | bool notify_on_start, |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 300 | bool notify_on_stop, |
| 301 | bool handles_incremental_state_invalidation) |
Primiano Tucci | 5403e4f | 2018-11-27 10:07:03 +0000 | [diff] [blame] | 302 | : instance_id(id), |
| 303 | config(cfg), |
| 304 | data_source_name(ds_name), |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 305 | will_notify_on_start(notify_on_start), |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 306 | will_notify_on_stop(notify_on_stop), |
| 307 | handles_incremental_state_clear( |
| 308 | handles_incremental_state_invalidation) {} |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 309 | DataSourceInstance(const DataSourceInstance&) = delete; |
| 310 | DataSourceInstance& operator=(const DataSourceInstance&) = delete; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 311 | |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 312 | DataSourceInstanceID instance_id; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 313 | DataSourceConfig config; |
Primiano Tucci | 9daa483 | 2018-03-28 23:28:17 +0100 | [diff] [blame] | 314 | std::string data_source_name; |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 315 | bool will_notify_on_start; |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 316 | bool will_notify_on_stop; |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 317 | bool handles_incremental_state_clear; |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 318 | |
| 319 | enum DataSourceInstanceState { |
| 320 | CONFIGURED, |
| 321 | STARTING, |
| 322 | STARTED, |
| 323 | STOPPING, |
| 324 | STOPPED |
| 325 | }; |
| 326 | DataSourceInstanceState state = CONFIGURED; |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 329 | struct PendingFlush { |
| 330 | std::set<ProducerID> producers; |
| 331 | ConsumerEndpoint::FlushCallback callback; |
| 332 | explicit PendingFlush(decltype(callback) cb) : callback(std::move(cb)) {} |
| 333 | }; |
| 334 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 335 | // Holds the state of a tracing session. A tracing session is uniquely bound |
| 336 | // a specific Consumer. Each Consumer can own one or more sessions. |
| 337 | struct TracingSession { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 338 | enum State { |
| 339 | DISABLED = 0, |
| 340 | CONFIGURED, |
| 341 | STARTED, |
| 342 | DISABLING_WAITING_STOP_ACKS |
| 343 | }; |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 344 | |
| 345 | TracingSession(TracingSessionID, ConsumerEndpointImpl*, const TraceConfig&); |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 346 | |
| 347 | size_t num_buffers() const { return buffers_index.size(); } |
| 348 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 349 | uint32_t delay_to_next_write_period_ms() const { |
Sami Kyostila | 01c45f0 | 2018-03-29 15:43:10 +0100 | [diff] [blame] | 350 | PERFETTO_DCHECK(write_period_ms > 0); |
| 351 | return write_period_ms - |
| 352 | (base::GetWallTimeMs().count() % write_period_ms); |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Florian Mayer | e563f66 | 2019-01-09 11:04:50 +0000 | [diff] [blame] | 355 | uint32_t flush_timeout_ms() { |
| 356 | uint32_t timeout_ms = config.flush_timeout_ms(); |
| 357 | return timeout_ms ? timeout_ms : kDefaultFlushTimeoutMs; |
| 358 | } |
| 359 | |
Eric Seckler | d0ac7ca | 2019-02-06 09:13:45 +0000 | [diff] [blame] | 360 | PacketSequenceID GetPacketSequenceID(ProducerID producer_id, |
| 361 | WriterID writer_id) { |
| 362 | auto key = std::make_pair(producer_id, writer_id); |
| 363 | auto it = packet_sequence_ids.find(key); |
| 364 | if (it != packet_sequence_ids.end()) |
| 365 | return it->second; |
| 366 | // We shouldn't run out of sequence IDs (producer ID is 16 bit, writer IDs |
| 367 | // are limited to 1024). |
| 368 | static_assert(kMaxPacketSequenceID > kMaxProducerID * kMaxWriterID, |
| 369 | "PacketSequenceID value space doesn't cover service " |
| 370 | "sequence ID and all producer/writer ID combinations!"); |
| 371 | PERFETTO_DCHECK(last_packet_sequence_id < kMaxPacketSequenceID); |
| 372 | PacketSequenceID sequence_id = ++last_packet_sequence_id; |
| 373 | packet_sequence_ids[key] = sequence_id; |
| 374 | return sequence_id; |
| 375 | } |
| 376 | |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 377 | DataSourceInstance* GetDataSourceInstance( |
| 378 | ProducerID producer_id, |
| 379 | DataSourceInstanceID instance_id) { |
| 380 | for (auto& inst_kv : data_source_instances) { |
| 381 | if (inst_kv.first != producer_id || |
| 382 | inst_kv.second.instance_id != instance_id) { |
| 383 | continue; |
| 384 | } |
| 385 | return &inst_kv.second; |
| 386 | } |
| 387 | return nullptr; |
| 388 | } |
| 389 | |
| 390 | bool AllDataSourceInstancesStopped() { |
| 391 | for (const auto& inst_kv : data_source_instances) { |
| 392 | if (inst_kv.second.state != DataSourceInstance::STOPPED) |
| 393 | return false; |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 398 | const TracingSessionID id; |
| 399 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 400 | // The consumer that started the session. |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 401 | // Can be nullptr if the consumer detached from the session. |
| 402 | ConsumerEndpointImpl* consumer_maybe_null; |
| 403 | |
| 404 | // Unix uid of the consumer. This is valid even after the consumer detaches |
| 405 | // and does not change for the entire duration of the session. It is used to |
| 406 | // prevent that a consumer re-attaches to a session from a different uid. |
| 407 | uid_t const consumer_uid; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 408 | |
Stephen Nusko | 5984729 | 2019-03-22 13:54:08 +0000 | [diff] [blame] | 409 | // The list of triggers this session received while alive and the time they |
| 410 | // were received at. This is used to insert 'fake' packets back to the |
| 411 | // consumer so they can tell when some event happened. The order matches the |
| 412 | // order they were received. |
Stephen Nusko | 70ea330 | 2019-04-01 19:44:40 +0100 | [diff] [blame] | 413 | struct TriggerInfo { |
| 414 | uint64_t boot_time_ns; |
| 415 | std::string trigger_name; |
| 416 | std::string producer_name; |
| 417 | uid_t producer_uid; |
| 418 | }; |
| 419 | std::vector<TriggerInfo> received_triggers; |
Stephen Nusko | 5984729 | 2019-03-22 13:54:08 +0000 | [diff] [blame] | 420 | |
Oystein Eftevaag | cb6e4c8 | 2019-03-06 15:38:26 -0800 | [diff] [blame] | 421 | // The trace config provided by the Consumer when calling |
| 422 | // EnableTracing(), plus any updates performed by ChangeTraceConfig. |
| 423 | TraceConfig config; |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 424 | |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 425 | // List of data source instances that have been enabled on the various |
| 426 | // producers for this tracing session. |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 427 | // TODO(rsavitski): at the time of writing, the map structure is unused |
| 428 | // (even when the calling code has a key). This is also an opportunity to |
| 429 | // consider an alternative data type, e.g. a map of vectors. |
Sami Kyostila | 06487a2 | 2018-02-27 13:48:38 +0000 | [diff] [blame] | 430 | std::multimap<ProducerID, DataSourceInstance> data_source_instances; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 431 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 432 | // For each Flush(N) request, keeps track of the set of producers for which |
| 433 | // we are still awaiting a NotifyFlushComplete(N) ack. |
| 434 | std::map<FlushRequestID, PendingFlush> pending_flushes; |
| 435 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 436 | // Maps a per-trace-session buffer index into the corresponding global |
| 437 | // BufferID (shared namespace amongst all consumers). This vector has as |
| 438 | // many entries as |config.buffers_size()|. |
| 439 | std::vector<BufferID> buffers_index; |
Sami Kyostila | fbccb3c | 2018-03-21 14:00:47 +0000 | [diff] [blame] | 440 | |
Eric Seckler | d0ac7ca | 2019-02-06 09:13:45 +0000 | [diff] [blame] | 441 | std::map<std::pair<ProducerID, WriterID>, PacketSequenceID> |
| 442 | packet_sequence_ids; |
| 443 | PacketSequenceID last_packet_sequence_id = kServicePacketSequenceID; |
| 444 | |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 445 | // When the last snapshots (clock, stats, sync marker) were emitted into |
| 446 | // the output stream. |
| 447 | base::TimeMillis last_snapshot_time = {}; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 448 | |
Sami Kyostila | 200bd2e | 2018-03-26 12:24:10 +0100 | [diff] [blame] | 449 | // Whether we mirrored the trace config back to the trace output yet. |
| 450 | bool did_emit_config = false; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 451 | |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 452 | // Whether we put the system info into the trace output yet. |
| 453 | bool did_emit_system_info = false; |
| 454 | |
Stephen Nusko | 70ea330 | 2019-04-01 19:44:40 +0100 | [diff] [blame] | 455 | // The number of received triggers we've emitted into the trace output. |
| 456 | size_t num_triggers_emitted_into_trace = 0; |
| 457 | |
Hector Dearman | e004a57 | 2019-05-13 17:51:43 +0100 | [diff] [blame] | 458 | // Initial clock snapshot, captured at trace start time (when state goes |
| 459 | // to TracingSession::STARTED). Emitted into the trace when the consumer |
| 460 | // first begins reading the trace. |
| 461 | std::vector<TracePacket> initial_clock_snapshot_; |
| 462 | |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 463 | State state = DISABLED; |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 464 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 465 | // If the consumer detached the session, this variable defines the key used |
| 466 | // for identifying the session later when reattaching. |
| 467 | std::string detach_key; |
| 468 | |
Primiano Tucci | 2ffd1a5 | 2018-03-27 01:01:30 +0100 | [diff] [blame] | 469 | // This is set when the Consumer calls sets |write_into_file| == true in the |
| 470 | // TraceConfig. In this case this represents the file we should stream the |
| 471 | // trace packets into, rather than returning it to the consumer via |
| 472 | // OnTraceData(). |
| 473 | base::ScopedFile write_into_file; |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 474 | uint32_t write_period_ms = 0; |
| 475 | uint64_t max_file_size_bytes = 0; |
| 476 | uint64_t bytes_written_into_file = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 477 | }; |
| 478 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 479 | TracingServiceImpl(const TracingServiceImpl&) = delete; |
| 480 | TracingServiceImpl& operator=(const TracingServiceImpl&) = delete; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 481 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 482 | DataSourceInstance* SetupDataSource(const TraceConfig::DataSource&, |
| 483 | const TraceConfig::ProducerConfig&, |
| 484 | const RegisteredDataSource&, |
| 485 | TracingSession*); |
Oystein Eftevaag | 1269b4a | 2018-01-10 16:29:38 -0800 | [diff] [blame] | 486 | |
Primiano Tucci | 081d46a | 2018-02-28 11:09:43 +0000 | [diff] [blame] | 487 | // Returns the next available ProducerID that is not in |producers_|. |
| 488 | ProducerID GetNextProducerID(); |
| 489 | |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 490 | // Returns a pointer to the |tracing_sessions_| entry or nullptr if the |
| 491 | // session doesn't exists. |
| 492 | TracingSession* GetTracingSession(TracingSessionID); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 493 | |
Primiano Tucci | 9ba1d84 | 2018-12-20 17:31:04 +0100 | [diff] [blame] | 494 | // Returns a pointer to the |tracing_sessions_| entry, matching the given |
| 495 | // uid and detach key, or nullptr if no such session exists. |
| 496 | TracingSession* GetDetachedSession(uid_t, const std::string& key); |
| 497 | |
Lalit Maganti | 485faff | 2018-03-06 11:51:35 +0000 | [diff] [blame] | 498 | // Update the memory guard rail by using the latest information from the |
| 499 | // shared memory and trace buffers. |
| 500 | void UpdateMemoryGuardrail(); |
| 501 | |
Eric Seckler | 4ff03e5 | 2019-03-15 10:10:30 +0000 | [diff] [blame] | 502 | void StartDataSourceInstance(ProducerEndpointImpl* producer, |
| 503 | TracingSession* tracing_session, |
| 504 | DataSourceInstance* instance); |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 505 | void SnapshotSyncMarker(std::vector<TracePacket>*); |
Primiano Tucci | 80cb418 | 2019-05-14 09:37:56 +0100 | [diff] [blame] | 506 | void SnapshotClocks(std::vector<TracePacket>*, bool set_root_timestamp); |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 507 | void SnapshotStats(TracingSession*, std::vector<TracePacket>*); |
Eric Seckler | eaf29ed | 2019-01-23 09:53:55 +0000 | [diff] [blame] | 508 | TraceStats GetTraceStats(TracingSession* tracing_session); |
Sami Kyostila | 200bd2e | 2018-03-26 12:24:10 +0100 | [diff] [blame] | 509 | void MaybeEmitTraceConfig(TracingSession*, std::vector<TracePacket>*); |
Hector Dearman | 685f752 | 2019-03-12 14:28:56 +0000 | [diff] [blame] | 510 | void MaybeEmitSystemInfo(TracingSession*, std::vector<TracePacket>*); |
Stephen Nusko | 70ea330 | 2019-04-01 19:44:40 +0100 | [diff] [blame] | 511 | void MaybeEmitReceivedTriggers(TracingSession*, std::vector<TracePacket>*); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 512 | void OnFlushTimeout(TracingSessionID, FlushRequestID); |
Primiano Tucci | baeecf1 | 2018-07-25 12:02:20 +0100 | [diff] [blame] | 513 | void OnDisableTracingTimeout(TracingSessionID); |
| 514 | void DisableTracingNotifyConsumerAndFlushFile(TracingSession*); |
Primiano Tucci | caa5780 | 2018-11-25 11:07:07 +0000 | [diff] [blame] | 515 | void PeriodicFlushTask(TracingSessionID, bool post_next_only); |
Eric Seckler | a01e28a | 2019-01-08 11:21:04 +0000 | [diff] [blame] | 516 | void CompleteFlush(TracingSessionID tsid, |
| 517 | ConsumerEndpoint::FlushCallback callback, |
| 518 | bool success); |
| 519 | void ScrapeSharedMemoryBuffers(TracingSession* tracing_session, |
| 520 | ProducerEndpointImpl* producer); |
Ryan Savitski | 33868d5 | 2019-05-13 10:56:14 +0100 | [diff] [blame] | 521 | void PeriodicClearIncrementalStateTask(TracingSessionID, bool post_next_only); |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 522 | TraceBuffer* GetBufferByID(BufferID); |
Stephen Nusko | 5984729 | 2019-03-22 13:54:08 +0000 | [diff] [blame] | 523 | void OnStartTriggersTimeout(TracingSessionID tsid); |
Primiano Tucci | ecf9e4a | 2018-03-14 14:51:58 +0000 | [diff] [blame] | 524 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 525 | base::TaskRunner* const task_runner_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 526 | std::unique_ptr<SharedMemory::Factory> shm_factory_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 527 | ProducerID last_producer_id_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 528 | DataSourceInstanceID last_data_source_instance_id_ = 0; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 529 | TracingSessionID last_tracing_session_id_ = 0; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 530 | FlushRequestID last_flush_request_id_ = 0; |
Primiano Tucci | 5e33cad | 2018-04-30 14:41:25 +0100 | [diff] [blame] | 531 | uid_t uid_ = 0; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 532 | |
| 533 | // Buffer IDs are global across all consumers (because a Producer can produce |
| 534 | // data for more than one trace session, hence more than one consumer). |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 535 | IdAllocator<BufferID> buffer_ids_; |
Primiano Tucci | 5358933 | 2017-12-19 11:31:13 +0100 | [diff] [blame] | 536 | |
| 537 | std::multimap<std::string /*name*/, RegisteredDataSource> data_sources_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 538 | std::map<ProducerID, ProducerEndpointImpl*> producers_; |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 539 | std::set<ConsumerEndpointImpl*> consumers_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 540 | std::map<TracingSessionID, TracingSession> tracing_sessions_; |
Hector Dearman | 6214c8f | 2018-03-27 16:16:22 +0100 | [diff] [blame] | 541 | std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_; |
Primiano Tucci | 20d441d | 2018-01-16 09:25:51 +0000 | [diff] [blame] | 542 | |
Eric Seckler | a01e28a | 2019-01-08 11:21:04 +0000 | [diff] [blame] | 543 | bool smb_scraping_enabled_ = false; |
Florian Mayer | 61c5548 | 2018-03-06 14:43:54 +0000 | [diff] [blame] | 544 | bool lockdown_mode_ = false; |
Primiano Tucci | 9754d0d | 2018-09-15 12:41:46 +0100 | [diff] [blame] | 545 | uint32_t min_write_period_ms_ = 100; // Overridable for testing. |
| 546 | |
| 547 | uint8_t sync_marker_packet_[32]; // Lazily initialized. |
| 548 | size_t sync_marker_packet_size_ = 0; |
Florian Mayer | 61c5548 | 2018-03-06 14:43:54 +0000 | [diff] [blame] | 549 | |
Eric Seckler | 2c72bd8 | 2019-02-08 15:01:34 +0000 | [diff] [blame] | 550 | // Stats. |
| 551 | uint64_t chunks_discarded_ = 0; |
| 552 | uint64_t patches_discarded_ = 0; |
| 553 | |
Florian Mayer | cd08ec6 | 2018-01-31 17:49:25 +0000 | [diff] [blame] | 554 | PERFETTO_THREAD_CHECKER(thread_checker_) |
| 555 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 556 | base::WeakPtrFactory<TracingServiceImpl> |
| 557 | weak_ptr_factory_; // Keep at the end. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | } // namespace perfetto |
| 561 | |
Florian Mayer | 6a1a4d5 | 2018-06-08 16:47:07 +0100 | [diff] [blame] | 562 | #endif // SRC_TRACING_CORE_TRACING_SERVICE_IMPL_H_ |