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