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 #ifndef INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_ 18 #define INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_ 19 20 #include <stddef.h> 21 22 #include <functional> 23 #include <memory> 24 #include <vector> 25 26 #include "perfetto/base/export.h" 27 #include "perfetto/tracing/core/basic_types.h" 28 #include "perfetto/tracing/core/tracing_service.h" 29 30 namespace perfetto { 31 32 namespace base { 33 class TaskRunner; 34 } 35 36 class CommitDataRequest; 37 class StartupTraceWriter; 38 class StartupTraceWriterRegistry; 39 class SharedMemory; 40 class TraceWriter; 41 42 // Used by the Producer-side of the transport layer to vend TraceWriters 43 // from the SharedMemory it receives from the Service-side. 44 class PERFETTO_EXPORT SharedMemoryArbiter { 45 public: 46 virtual ~SharedMemoryArbiter(); 47 48 // Creates a new TraceWriter and assigns it a new WriterID. The WriterID is 49 // written in each chunk header owned by a given TraceWriter and is used by 50 // the Service to reconstruct TracePackets written by the same TraceWriter. 51 // Returns null impl of TraceWriter if all WriterID slots are exhausted. 52 virtual std::unique_ptr<TraceWriter> CreateTraceWriter( 53 BufferID target_buffer) = 0; 54 55 // Binds the provided unbound StartupTraceWriterRegistry to the arbiter's SMB. 56 // Normally this happens when the perfetto service has been initialized and we 57 // want to rebind all the writers created in the early startup phase. 58 // 59 // All StartupTraceWriters created by the registry are bound to the arbiter 60 // and the given target buffer. The writers may not be bound immediately if 61 // they are concurrently being written to or if this method isn't called on 62 // the arbiter's TaskRunner. The registry will retry on the arbiter's 63 // TaskRunner until all writers were bound successfully. 64 // 65 // By calling this method, the registry's ownership is transferred to the 66 // arbiter. The arbiter will delete the registry once all writers were bound. 67 // 68 // TODO(eseckler): Make target buffer assignment more flexible (i.e. per 69 // writer). For now, embedders can use multiple registries instead. 70 virtual void BindStartupTraceWriterRegistry( 71 std::unique_ptr<StartupTraceWriterRegistry>, 72 BufferID target_buffer) = 0; 73 74 // Notifies the service that all data for the given FlushRequestID has been 75 // committed in the shared memory buffer. 76 virtual void NotifyFlushComplete(FlushRequestID) = 0; 77 78 // Implemented in src/core/shared_memory_arbiter_impl.cc . 79 static std::unique_ptr<SharedMemoryArbiter> CreateInstance( 80 SharedMemory*, 81 size_t page_size, 82 TracingService::ProducerEndpoint*, 83 base::TaskRunner*); 84 }; 85 86 } // namespace perfetto 87 88 #endif // INCLUDE_PERFETTO_TRACING_CORE_SHARED_MEMORY_ARBITER_H_ 89