blob: 5f10fcc36070a1ebd311c59f7f9ea83a58af4fa1 [file] [log] [blame]
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TRACING_SRC_CORE_SERVICE_IMPL_H_
#define TRACING_SRC_CORE_SERVICE_IMPL_H_
#include <functional>
#include <map>
#include <memory>
#include "tracing/core/basic_types.h"
#include "tracing/core/service.h"
namespace perfetto {
namespace base {
class TaskRunner;
} // namespace base
class DataSourceConfig;
class Producer;
class SharedMemory;
// The tracing service business logic.
class ServiceImpl : public Service {
public:
// The implementation behind the service endpoint exposed to each producer.
class ProducerEndpointImpl : public Service::ProducerEndpoint {
public:
ProducerEndpointImpl(ProducerID,
ServiceImpl*,
base::TaskRunner*,
Producer*,
std::unique_ptr<SharedMemory>);
~ProducerEndpointImpl() override;
Producer* producer() const { return producer_; }
SharedMemory* shared_memory() const { return shared_memory_.get(); }
// Service::ProducerEndpoint implementation.
void RegisterDataSource(const DataSourceDescriptor&,
RegisterDataSourceCallback) override;
void UnregisterDataSource(DataSourceID) override;
void NotifySharedMemoryUpdate(
const std::vector<uint32_t>& changed_pages) override;
private:
ProducerEndpointImpl(const ProducerEndpointImpl&) = delete;
ProducerEndpointImpl& operator=(const ProducerEndpointImpl&) = delete;
ProducerID const id_;
ServiceImpl* const service_;
base::TaskRunner* const task_runner_;
Producer* producer_;
std::unique_ptr<SharedMemory> shared_memory_;
DataSourceID last_data_source_id_ = 0;
};
explicit ServiceImpl(std::unique_ptr<SharedMemory::Factory>,
base::TaskRunner*);
~ServiceImpl() override;
// Called by the ProducerEndpointImpl dtor.
void DisconnectProducer(ProducerID);
// Service implementation.
std::unique_ptr<Service::ProducerEndpoint> ConnectProducer(
Producer*) override;
void set_observer_for_testing(ObserverForTesting*) override;
// Exposed mainly for testing.
size_t num_producers() const { return producers_.size(); }
ProducerEndpointImpl* GetProducer(ProducerID) const;
private:
ServiceImpl(const ServiceImpl&) = delete;
ServiceImpl& operator=(const ServiceImpl&) = delete;
std::unique_ptr<SharedMemory::Factory> shm_factory_;
base::TaskRunner* const task_runner_;
ProducerID last_producer_id_ = 0;
std::map<ProducerID, ProducerEndpointImpl*> producers_;
ObserverForTesting* observer_ = nullptr;
};
} // namespace perfetto
#endif // TRACING_SRC_CORE_SERVICE_IMPL_H_