From 3184c907b2420e6c66485529f336251b2b62aecf Mon Sep 17 00:00:00 2001 From: Sadik Armagan Date: Wed, 18 Mar 2020 10:57:30 +0000 Subject: IVGCVSW-4463 Change ProfilingService to a member of runtime from a singleton Signed-off-by: Sadik Armagan Change-Id: I345c39a10a4693a500aa1687d9a5cee76da791c3 --- include/armnn/INetwork.hpp | 2 - include/armnn/Types.hpp | 2 + src/armnn/BackendRegistry.cpp | 9 -- src/armnn/Layer.cpp | 2 +- src/armnn/LoadedNetwork.cpp | 30 +++-- src/armnn/LoadedNetwork.hpp | 10 +- src/armnn/Network.cpp | 6 +- src/armnn/Network.hpp | 3 - src/armnn/Runtime.cpp | 20 +-- src/armnn/Runtime.hpp | 7 + src/armnn/test/RuntimeTests.cpp | 27 ++-- src/backends/backendsCommon/Workload.hpp | 2 +- .../backendsCommon/test/BackendProfilingTests.cpp | 22 ++-- src/profiling/ProfilingService.cpp | 16 ++- src/profiling/ProfilingService.hpp | 143 +++++++++++---------- src/profiling/RegisterBackendCounters.cpp | 2 +- src/profiling/RegisterBackendCounters.hpp | 13 +- src/profiling/TimelineUtilityMethods.cpp | 39 +++--- src/profiling/TimelineUtilityMethods.hpp | 12 +- src/profiling/backends/BackendProfiling.cpp | 5 +- src/profiling/backends/BackendProfiling.hpp | 6 +- .../test/FileOnlyProfilingDecoratorTests.cpp | 5 +- src/profiling/test/ProfilingMocks.hpp | 8 +- src/profiling/test/ProfilingTestUtils.cpp | 21 +-- src/profiling/test/ProfilingTestUtils.hpp | 6 +- src/profiling/test/ProfilingTests.cpp | 112 ++++++++-------- src/profiling/test/ProfilingTests.hpp | 15 ++- src/profiling/test/SendTimelinePacketTests.cpp | 10 +- src/profiling/test/TimelineUtilityMethodsTests.cpp | 34 +++-- .../profiling/gatordmock/tests/GatordMockTests.cpp | 2 +- 30 files changed, 322 insertions(+), 269 deletions(-) diff --git a/include/armnn/INetwork.hpp b/include/armnn/INetwork.hpp index 7f1817c5cf..c976b82c0b 100644 --- a/include/armnn/INetwork.hpp +++ b/include/armnn/INetwork.hpp @@ -111,8 +111,6 @@ public: virtual Status PrintGraph() = 0; - virtual profiling::ProfilingGuid GetGuid() const = 0; - /// Adds an input layer to the network. /// @param id - User generated id to uniquely identify a particular input. The same id needs to be specified. /// when passing the inputs to the IRuntime::EnqueueWorkload() function. diff --git a/include/armnn/Types.hpp b/include/armnn/Types.hpp index 9779958ee0..e58cecf2f4 100644 --- a/include/armnn/Types.hpp +++ b/include/armnn/Types.hpp @@ -252,6 +252,8 @@ static constexpr uint64_t MIN_STATIC_GUID = 1llu << 63; class ProfilingGuid { public: + ProfilingGuid() : m_Guid(0) {} + ProfilingGuid(uint64_t guid) : m_Guid(guid) {} operator uint64_t() const { return m_Guid; } diff --git a/src/armnn/BackendRegistry.cpp b/src/armnn/BackendRegistry.cpp index 35e82f2e67..a79cdd0bb2 100644 --- a/src/armnn/BackendRegistry.cpp +++ b/src/armnn/BackendRegistry.cpp @@ -5,7 +5,6 @@ #include #include -#include namespace armnn { @@ -24,19 +23,11 @@ void BackendRegistry::Register(const BackendId& id, BackendRegistry::FactoryFunc std::string(id) + " already registered as IBackend factory", CHECK_LOCATION()); } - if (profiling::ProfilingService::Instance().IsProfilingEnabled()) - { - profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::REGISTERED_BACKENDS); - } m_Factories[id] = factory; } void BackendRegistry::Deregister(const BackendId& id) { - if (profiling::ProfilingService::Instance().IsProfilingEnabled()) - { - profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::UNREGISTERED_BACKENDS); - } m_Factories.erase(id); } diff --git a/src/armnn/Layer.cpp b/src/armnn/Layer.cpp index 9de812c6e5..29d85b5a4c 100644 --- a/src/armnn/Layer.cpp +++ b/src/armnn/Layer.cpp @@ -194,7 +194,7 @@ Layer::Layer(unsigned int numInputSlots, , m_Type(type) , m_BackendId() , m_BackendHint(EmptyOptional()) -, m_Guid(profiling::ProfilingService::Instance().NextGuid()) +, m_Guid(profiling::ProfilingService::GetNextGuid()) { IgnoreUnused(layout); m_InputSlots.reserve(numInputSlots); diff --git a/src/armnn/LoadedNetwork.cpp b/src/armnn/LoadedNetwork.cpp index 69e42ba38f..f3d742c515 100644 --- a/src/armnn/LoadedNetwork.cpp +++ b/src/armnn/LoadedNetwork.cpp @@ -20,7 +20,6 @@ #include #include -#include #include #include @@ -84,7 +83,8 @@ void AddWorkloadStructure(std::unique_ptr& timelineUtils std::unique_ptr LoadedNetwork::MakeLoadedNetwork(std::unique_ptr net, std::string& errorMessage, - const INetworkProperties& networkProperties) + const INetworkProperties& networkProperties, + profiling::ProfilingService& profilingService) { std::unique_ptr loadedNetwork; @@ -98,7 +98,7 @@ std::unique_ptr LoadedNetwork::MakeLoadedNetwork(std::unique_ptr< try { - loadedNetwork.reset(new LoadedNetwork(std::move(net), networkProperties)); + loadedNetwork.reset(new LoadedNetwork(std::move(net), networkProperties, profilingService)); } catch (const armnn::RuntimeException& error) { @@ -117,10 +117,12 @@ std::unique_ptr LoadedNetwork::MakeLoadedNetwork(std::unique_ptr< } LoadedNetwork::LoadedNetwork(std::unique_ptr net, - const INetworkProperties& networkProperties) : + const INetworkProperties& networkProperties, + profiling::ProfilingService& profilingService) : m_OptimizedNetwork(std::move(net)), m_IsImportEnabled(networkProperties.m_ImportEnabled), - m_IsExportEnabled(networkProperties.m_ExportEnabled) + m_IsExportEnabled(networkProperties.m_ExportEnabled), + m_ProfilingService(profilingService) { // Create a profiler and register it for the current thread. m_Profiler = std::make_shared(); @@ -191,7 +193,8 @@ LoadedNetwork::LoadedNetwork(std::unique_ptr net, } ProfilingGuid networkGuid = m_OptimizedNetwork->GetGuid(); - std::unique_ptr timelineUtils = TimelineUtilityMethods::GetTimelineUtils(); + std::unique_ptr timelineUtils = + TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); if (timelineUtils) { timelineUtils->CreateTypedEntity(networkGuid, LabelsAndEventClasses::NETWORK_GUID); @@ -449,8 +452,9 @@ Status LoadedNetwork::EnqueueWorkload(const InputTensors& inputTensors, EnqueueOutput(*outputLayer, pin.GetTensorHandle(), pin.GetTensorInfo()); } - std::unique_ptr timelineUtils = TimelineUtilityMethods::GetTimelineUtils(); - ProfilingGuid inferenceGuid = ProfilingService::Instance().NextGuid(); + std::unique_ptr timelineUtils = + TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); + ProfilingGuid inferenceGuid = m_ProfilingService.GetNextGuid(); if (timelineUtils) { // Add inference timeline trace if profiling is enabled. @@ -463,9 +467,9 @@ Status LoadedNetwork::EnqueueWorkload(const InputTensors& inputTensors, bool executionSucceeded = true; { - if (profiling::ProfilingService::Instance().IsProfilingEnabled()) + if (m_ProfilingService.IsProfilingEnabled()) { - profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::INFERENCES_RUN); + m_ProfilingService.IncrementCounterValue(armnn::profiling::INFERENCES_RUN); } ARMNN_SCOPED_PROFILING_EVENT(Compute::Undefined, "Execute"); ARMNN_SCOPED_HEAP_PROFILING("Executing"); @@ -535,7 +539,8 @@ void LoadedNetwork::EnqueueInput(const BindableLayer& layer, ITensorHandle* tens BOOST_ASSERT_MSG(inputWorkload, "No input workload created"); - std::unique_ptr timelineUtils = TimelineUtilityMethods::GetTimelineUtils(); + std::unique_ptr timelineUtils = + TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); if (timelineUtils) { // Add Input Workload to the post-optimisation network structure @@ -627,7 +632,8 @@ void LoadedNetwork::EnqueueOutput(const BindableLayer& layer, ITensorHandle* ten std::make_unique(outputQueueDescriptor, info); BOOST_ASSERT_MSG(outputWorkload, "No output workload created"); - std::unique_ptr timelineUtils = TimelineUtilityMethods::GetTimelineUtils(); + std::unique_ptr timelineUtils = + TimelineUtilityMethods::GetTimelineUtils(m_ProfilingService); if (timelineUtils) { // Add Output Workload to the post-optimisation network structure diff --git a/src/armnn/LoadedNetwork.hpp b/src/armnn/LoadedNetwork.hpp index ab2c8be1cd..01e3442508 100644 --- a/src/armnn/LoadedNetwork.hpp +++ b/src/armnn/LoadedNetwork.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,8 @@ public: static std::unique_ptr MakeLoadedNetwork(std::unique_ptr net, std::string & errorMessage, - const INetworkProperties& networkProperties); + const INetworkProperties& networkProperties, + profiling::ProfilingService& profilingService); // NOTE we return by reference as the purpose of this method is only to provide // access to the private m_Profiler and in theory we should not need to increment @@ -57,7 +59,9 @@ public: private: void AllocateWorkingMemory(); - LoadedNetwork(std::unique_ptr net, const INetworkProperties& networkProperties); + LoadedNetwork(std::unique_ptr net, + const INetworkProperties& networkProperties, + profiling::ProfilingService& profilingService); void EnqueueInput(const BindableLayer& layer, ITensorHandle* tensorHandle, const TensorInfo& tensorInfo); @@ -92,6 +96,8 @@ private: bool m_IsExportEnabled=false; TensorHandleFactoryRegistry m_TensorHandleFactoryRegistry; + + profiling::ProfilingService& m_ProfilingService; }; } diff --git a/src/armnn/Network.cpp b/src/armnn/Network.cpp index 3663727e48..9eef7b2fb6 100644 --- a/src/armnn/Network.cpp +++ b/src/armnn/Network.cpp @@ -1023,8 +1023,7 @@ IOptimizedNetworkPtr Optimize(const INetwork& inNetwork, } Network::Network() -: m_Graph(std::make_unique()), - m_Guid(profiling::ProfilingService::Instance().NextGuid()) +: m_Graph(std::make_unique()) { } @@ -1680,8 +1679,7 @@ void Network::Accept(ILayerVisitor& visitor) const } OptimizedNetwork::OptimizedNetwork(std::unique_ptr graph) - : m_Graph(std::move(graph)), - m_Guid(profiling::ProfilingService::Instance().NextGuid()) + : m_Graph(std::move(graph)), m_Guid(profiling::ProfilingService::GetNextGuid()) { } diff --git a/src/armnn/Network.hpp b/src/armnn/Network.hpp index 089b46c9ca..17eacba48a 100644 --- a/src/armnn/Network.hpp +++ b/src/armnn/Network.hpp @@ -35,8 +35,6 @@ public: Status PrintGraph() override; - profiling::ProfilingGuid GetGuid() const final { return m_Guid; }; - IConnectableLayer* AddInputLayer(LayerBindingId id, const char* name=nullptr) override; IConnectableLayer* AddArgMinMaxLayer(const ArgMinMaxDescriptor& desc, @@ -259,7 +257,6 @@ private: const char* name); std::unique_ptr m_Graph; - profiling::ProfilingGuid m_Guid; }; class OptimizedNetwork final : public IOptimizedNetwork diff --git a/src/armnn/Runtime.cpp b/src/armnn/Runtime.cpp index b1017c58ed..26636a81f7 100644 --- a/src/armnn/Runtime.cpp +++ b/src/armnn/Runtime.cpp @@ -11,8 +11,6 @@ #include #include -#include - #include #include @@ -75,7 +73,8 @@ Status Runtime::LoadNetwork(NetworkId& networkIdOut, unique_ptr loadedNetwork = LoadedNetwork::MakeLoadedNetwork( std::unique_ptr(boost::polymorphic_downcast(rawNetwork)), errorMessage, - networkProperties); + networkProperties, + m_ProfilingService); if (!loadedNetwork) { @@ -94,9 +93,9 @@ Status Runtime::LoadNetwork(NetworkId& networkIdOut, context.second->AfterLoadNetwork(networkIdOut); } - if (profiling::ProfilingService::Instance().IsProfilingEnabled()) + if (m_ProfilingService.IsProfilingEnabled()) { - profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_LOADS); + m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_LOADS); } return Status::Success; @@ -125,9 +124,10 @@ Status Runtime::UnloadNetwork(NetworkId networkId) ARMNN_LOG(warning) << "WARNING: Runtime::UnloadNetwork(): " << networkId << " not found!"; return Status::Failure; } - if (profiling::ProfilingService::Instance().IsProfilingEnabled()) + + if (m_ProfilingService.IsProfilingEnabled()) { - profiling::ProfilingService::Instance().IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS); + m_ProfilingService.IncrementCounterValue(armnn::profiling::NETWORK_UNLOADS); } } @@ -158,7 +158,7 @@ Runtime::Runtime(const CreationOptions& options) ARMNN_LOG(info) << "ArmNN v" << ARMNN_VERSION << "\n"; // pass configuration info to the profiling service - armnn::profiling::ProfilingService::Instance().ConfigureProfilingService(options.m_ProfilingOptions); + m_ProfilingService.ConfigureProfilingService(options.m_ProfilingOptions); // Load any available/compatible dynamic backend before the runtime // goes through the backend registry @@ -185,7 +185,7 @@ Runtime::Runtime(const CreationOptions& options) unique_ptr profilingIface = std::make_unique(armnn::profiling::BackendProfiling( - options, armnn::profiling::ProfilingService::Instance(), id)); + options, m_ProfilingService, id)); // Backends may also provide a profiling context. Ask for it now. auto profilingContext = backend->CreateBackendProfilingContext(options, profilingIface); @@ -196,7 +196,7 @@ Runtime::Runtime(const CreationOptions& options) if(profilingContext->EnableProfiling(true)) { // Pass the context onto the profiling service. - armnn::profiling::ProfilingService::Instance().AddBackendProfilingContext(id, profilingContext); + m_ProfilingService.AddBackendProfilingContext(id, profilingContext); } else { diff --git a/src/armnn/Runtime.hpp b/src/armnn/Runtime.hpp index 2ad3c9633c..477b1169b1 100644 --- a/src/armnn/Runtime.hpp +++ b/src/armnn/Runtime.hpp @@ -14,6 +14,8 @@ #include +#include + #include #include @@ -80,6 +82,8 @@ public: private: friend void RuntimeLoadedNetworksReserve(armnn::Runtime* runtime); // See RuntimeTests.cpp + friend profiling::ProfilingService& GetProfilingService(armnn::Runtime* runtime); // See RuntimeTests.cpp + int GenerateNetworkId(); LoadedNetwork* GetLoadedNetworkPtr(NetworkId networkId) const; @@ -109,6 +113,9 @@ private: /// List of dynamic backends loaded in the runtime std::vector m_DynamicBackends; + + /// Profiling Service Instance + profiling::ProfilingService m_ProfilingService; }; } // namespace armnn diff --git a/src/armnn/test/RuntimeTests.cpp b/src/armnn/test/RuntimeTests.cpp index e3cbe03c62..9ced7e907c 100644 --- a/src/armnn/test/RuntimeTests.cpp +++ b/src/armnn/test/RuntimeTests.cpp @@ -30,6 +30,11 @@ void RuntimeLoadedNetworksReserve(armnn::Runtime* runtime) runtime->m_LoadedNetworks.reserve(1); } +profiling::ProfilingService& GetProfilingService(armnn::Runtime* runtime) +{ + return runtime->m_ProfilingService; +} + } BOOST_AUTO_TEST_SUITE(Runtime) @@ -327,7 +332,7 @@ BOOST_AUTO_TEST_CASE(ProfilingDisable) // Create runtime in which the test will run armnn::IRuntime::CreationOptions options; - armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); + armnn::Runtime runtime(options); // build up the structure of the network INetworkPtr net(INetwork::Create()); @@ -348,13 +353,13 @@ BOOST_AUTO_TEST_CASE(ProfilingDisable) // optimize the network std::vector backends = { armnn::Compute::CpuRef }; - IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); + IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); // Load it into the runtime. It should succeed. armnn::NetworkId netId; - BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success); + BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); - profiling::ProfilingServiceRuntimeHelper profilingServiceHelper; + profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime)); profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager(); auto readableBuffer = bufferManager.GetReadableBuffer(); @@ -370,7 +375,7 @@ BOOST_AUTO_TEST_CASE(ProfilingEnableCpuRef) // Create runtime in which the test will run armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); + armnn::Runtime runtime(options); // build up the structure of the network INetworkPtr net(INetwork::Create()); @@ -390,15 +395,15 @@ BOOST_AUTO_TEST_CASE(ProfilingEnableCpuRef) // optimize the network std::vector backends = { armnn::Compute::CpuRef }; - IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); + IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); ProfilingGuid optNetGuid = optNet->GetGuid(); // Load it into the runtime. It should succeed. armnn::NetworkId netId; - BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success); + BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); - profiling::ProfilingServiceRuntimeHelper profilingServiceHelper; + profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime)); profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager(); auto readableBuffer = bufferManager.GetReadableBuffer(); @@ -681,15 +686,15 @@ BOOST_AUTO_TEST_CASE(ProfilingEnableCpuRef) InputTensors inputTensors { - { 0, ConstTensor(runtime->GetInputTensorInfo(netId, 0), inputData.data()) } + {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} }; OutputTensors outputTensors { - { 0, Tensor(runtime->GetOutputTensorInfo(netId, 0), outputData.data()) } + {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} }; // Does the inference. - runtime->EnqueueWorkload(netId, inputTensors, outputTensors); + runtime.EnqueueWorkload(netId, inputTensors, outputTensors); // Get readable buffer for inference timeline auto inferenceReadableBuffer = bufferManager.GetReadableBuffer(); diff --git a/src/backends/backendsCommon/Workload.hpp b/src/backends/backendsCommon/Workload.hpp index d7434c0d01..56736cebb3 100644 --- a/src/backends/backendsCommon/Workload.hpp +++ b/src/backends/backendsCommon/Workload.hpp @@ -31,7 +31,7 @@ public: BaseWorkload(const QueueDescriptor& descriptor, const WorkloadInfo& info) : m_Data(descriptor), - m_Guid(profiling::ProfilingService::Instance().NextGuid()) + m_Guid(profiling::ProfilingService::GetNextGuid()) { m_Data.Validate(info); } diff --git a/src/backends/backendsCommon/test/BackendProfilingTests.cpp b/src/backends/backendsCommon/test/BackendProfilingTests.cpp index b9e0e45c0a..1cf67c016d 100644 --- a/src/backends/backendsCommon/test/BackendProfilingTests.cpp +++ b/src/backends/backendsCommon/test/BackendProfilingTests.cpp @@ -14,6 +14,8 @@ #include "ProfilingUtils.hpp" #include "RequestCounterDirectoryCommandHandler.hpp" +#include + #include #include #include @@ -113,17 +115,16 @@ BOOST_AUTO_TEST_CASE(BackendProfilingCounterRegisterMockBackendTest) { // Reset the profiling service to the uninitialized state armnn::IRuntime::CreationOptions options; - options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); - profilingService.ConfigureProfilingService(options.m_ProfilingOptions, true); + options.m_ProfilingOptions.m_EnableProfiling = true;; armnn::MockBackendInitialiser initialiser; // Create a runtime - armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); +// armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); + armnn::Runtime runtime(options); // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered armnn::BackendId mockId = armnn::MockBackendId(); - const armnn::profiling::ICounterMappings& counterMap = profilingService.GetCounterMappings(); + const armnn::profiling::ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings(); BOOST_CHECK(counterMap.GetGlobalId(0, mockId) == 5); BOOST_CHECK(counterMap.GetGlobalId(1, mockId) == 6); BOOST_CHECK(counterMap.GetGlobalId(2, mockId) == 7); @@ -131,7 +132,7 @@ BOOST_AUTO_TEST_CASE(BackendProfilingCounterRegisterMockBackendTest) BOOST_CHECK(counterMap.GetGlobalId(4, mockId) == 9); BOOST_CHECK(counterMap.GetGlobalId(5, mockId) == 10); options.m_ProfilingOptions.m_EnableProfiling = false; - profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true); + GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true); } BOOST_AUTO_TEST_CASE(TestBackendCounters) @@ -149,7 +150,7 @@ BOOST_AUTO_TEST_CASE(TestBackendCounters) armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; std::unique_ptr cpuBackendProfilingPtr = std::make_unique(options, profilingService, cpuAccId); @@ -397,7 +398,7 @@ BOOST_AUTO_TEST_CASE(TestBackendCounterLogging) armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; std::unique_ptr cpuBackendProfilingPtr = std::make_unique(options, profilingService, cpuAccId); @@ -449,7 +450,7 @@ BOOST_AUTO_TEST_CASE(BackendProfilingContextGetSendTimelinePacket) // Reset the profiling service to the uninitialized state armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ConfigureProfilingService(options.m_ProfilingOptions, true); armnn::MockBackendInitialiser initialiser; @@ -482,8 +483,6 @@ BOOST_AUTO_TEST_CASE(GetProfilingGuidGenerator) // Reset the profiling service to the uninitialized state armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); - profilingService.ConfigureProfilingService(options.m_ProfilingOptions, true); armnn::MockBackendInitialiser initialiser; // Create a runtime. During this the mock backend will be registered and context returned. @@ -506,7 +505,6 @@ BOOST_AUTO_TEST_CASE(GetProfilingGuidGenerator) // Reset the profiling servie after the test. options.m_ProfilingOptions.m_EnableProfiling = false; - profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/profiling/ProfilingService.cpp b/src/profiling/ProfilingService.cpp index f111de15db..e42ef134dc 100644 --- a/src/profiling/ProfilingService.cpp +++ b/src/profiling/ProfilingService.cpp @@ -17,6 +17,18 @@ namespace armnn namespace profiling { +ProfilingGuidGenerator ProfilingService::m_GuidGenerator; + +ProfilingDynamicGuid ProfilingService::GetNextGuid() +{ + return m_GuidGenerator.NextGuid(); +} + +ProfilingStaticGuid ProfilingService::GetStaticId(const std::string& str) +{ + return m_GuidGenerator.GenerateStaticId(str); +} + void ProfilingService::ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService) { @@ -285,12 +297,12 @@ uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid) ProfilingDynamicGuid ProfilingService::NextGuid() { - return m_GuidGenerator.NextGuid(); + return ProfilingService::GetNextGuid(); } ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str) { - return m_GuidGenerator.GenerateStaticId(str); + return ProfilingService::GetStaticId(str); } std::unique_ptr ProfilingService::GetSendTimelinePacket() const diff --git a/src/profiling/ProfilingService.hpp b/src/profiling/ProfilingService.hpp index 4629c126bf..f6e409daeb 100644 --- a/src/profiling/ProfilingService.hpp +++ b/src/profiling/ProfilingService.hpp @@ -48,13 +48,73 @@ public: using CounterIndices = std::vector*>; using CounterValues = std::list>; - // Getter for the singleton instance - static ProfilingService& Instance() + // Default constructor/destructor kept protected for testing + ProfilingService() + : m_Options() + , m_CounterDirectory() + , m_ProfilingConnectionFactory(new ProfilingConnectionFactory()) + , m_ProfilingConnection() + , m_StateMachine() + , m_CounterIndex() + , m_CounterValues() + , m_CommandHandlerRegistry() + , m_PacketVersionResolver() + , m_CommandHandler(1000, + false, + m_CommandHandlerRegistry, + m_PacketVersionResolver) + , m_BufferManager() + , m_SendCounterPacket(m_BufferManager) + , m_SendThread(m_StateMachine, m_BufferManager, m_SendCounterPacket) + , m_SendTimelinePacket(m_BufferManager) + , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this, m_CounterIdMap, m_BackendProfilingContexts) + , m_ConnectionAcknowledgedCommandHandler(0, + 1, + m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(), + m_CounterDirectory, + m_SendCounterPacket, + m_SendTimelinePacket, + m_StateMachine) + , m_RequestCounterDirectoryCommandHandler(0, + 3, + m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(), + m_CounterDirectory, + m_SendCounterPacket, + m_SendTimelinePacket, + m_StateMachine) + , m_PeriodicCounterSelectionCommandHandler(0, + 4, + m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), + m_BackendProfilingContexts, + m_CounterIdMap, + m_Holder, + MAX_ARMNN_COUNTER, + m_PeriodicCounterCapture, + *this, + m_SendCounterPacket, + m_StateMachine) + , m_PerJobCounterSelectionCommandHandler(0, + 5, + m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(), + m_StateMachine) + , m_TimelinePacketWriterFactory(m_BufferManager) + , m_MaxGlobalCounterId(armnn::profiling::INFERENCES_RUN) { - static ProfilingService instance; - return instance; + // Register the "Connection Acknowledged" command handler + m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler); + + // Register the "Request Counter Directory" command handler + m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler); + + // Register the "Periodic Counter Selection" command handler + m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler); + + // Register the "Per-Job Counter Selection" command handler + m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler); } + ~ProfilingService(); + // Resets the profiling options, optionally clears the profiling service entirely void ResetExternalProfilingOptions(const ExternalProfilingOptions& options, bool resetProfilingService = false); ProfilingState ConfigureProfilingService(const ExternalProfilingOptions& options, @@ -111,8 +171,12 @@ public: /// Check if the profiling is enabled bool IsEnabled() { return m_Options.m_EnableProfiling; } + static ProfilingDynamicGuid GetNextGuid(); + + static ProfilingStaticGuid GetStaticId(const std::string& str); + private: - // Copy/move constructors/destructors and copy/move assignment operators are deleted + //Copy/move constructors/destructors and copy/move assignment operators are deleted ProfilingService(const ProfilingService&) = delete; ProfilingService(ProfilingService&&) = delete; ProfilingService& operator=(const ProfilingService&) = delete; @@ -149,78 +213,15 @@ private: RequestCounterDirectoryCommandHandler m_RequestCounterDirectoryCommandHandler; PeriodicCounterSelectionCommandHandler m_PeriodicCounterSelectionCommandHandler; PerJobCounterSelectionCommandHandler m_PerJobCounterSelectionCommandHandler; - ProfilingGuidGenerator m_GuidGenerator; + TimelinePacketWriterFactory m_TimelinePacketWriterFactory; std::unordered_map> m_BackendProfilingContexts; uint16_t m_MaxGlobalCounterId; -protected: - // Default constructor/destructor kept protected for testing - ProfilingService() - : m_Options() - , m_CounterDirectory() - , m_ProfilingConnectionFactory(new ProfilingConnectionFactory()) - , m_ProfilingConnection() - , m_StateMachine() - , m_CounterIndex() - , m_CounterValues() - , m_CommandHandlerRegistry() - , m_PacketVersionResolver() - , m_CommandHandler(1000, - false, - m_CommandHandlerRegistry, - m_PacketVersionResolver) - , m_BufferManager() - , m_SendCounterPacket(m_BufferManager) - , m_SendThread(m_StateMachine, m_BufferManager, m_SendCounterPacket) - , m_SendTimelinePacket(m_BufferManager) - , m_PeriodicCounterCapture(m_Holder, m_SendCounterPacket, *this, m_CounterIdMap, m_BackendProfilingContexts) - , m_ConnectionAcknowledgedCommandHandler(0, - 1, - m_PacketVersionResolver.ResolvePacketVersion(0, 1).GetEncodedValue(), - m_CounterDirectory, - m_SendCounterPacket, - m_SendTimelinePacket, - m_StateMachine) - , m_RequestCounterDirectoryCommandHandler(0, - 3, - m_PacketVersionResolver.ResolvePacketVersion(0, 3).GetEncodedValue(), - m_CounterDirectory, - m_SendCounterPacket, - m_SendTimelinePacket, - m_StateMachine) - , m_PeriodicCounterSelectionCommandHandler(0, - 4, - m_PacketVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), - m_BackendProfilingContexts, - m_CounterIdMap, - m_Holder, - MAX_ARMNN_COUNTER, - m_PeriodicCounterCapture, - *this, - m_SendCounterPacket, - m_StateMachine) - , m_PerJobCounterSelectionCommandHandler(0, - 5, - m_PacketVersionResolver.ResolvePacketVersion(0, 5).GetEncodedValue(), - m_StateMachine) - , m_TimelinePacketWriterFactory(m_BufferManager) - , m_MaxGlobalCounterId(armnn::profiling::INFERENCES_RUN) - { - // Register the "Connection Acknowledged" command handler - m_CommandHandlerRegistry.RegisterFunctor(&m_ConnectionAcknowledgedCommandHandler); - - // Register the "Request Counter Directory" command handler - m_CommandHandlerRegistry.RegisterFunctor(&m_RequestCounterDirectoryCommandHandler); + static ProfilingGuidGenerator m_GuidGenerator; - // Register the "Periodic Counter Selection" command handler - m_CommandHandlerRegistry.RegisterFunctor(&m_PeriodicCounterSelectionCommandHandler); - - // Register the "Per-Job Counter Selection" command handler - m_CommandHandlerRegistry.RegisterFunctor(&m_PerJobCounterSelectionCommandHandler); - } - ~ProfilingService(); +protected: // Protected methods for testing void SwapProfilingConnectionFactory(ProfilingService& instance, diff --git a/src/profiling/RegisterBackendCounters.cpp b/src/profiling/RegisterBackendCounters.cpp index 36f6106c00..035a2ca8a9 100644 --- a/src/profiling/RegisterBackendCounters.cpp +++ b/src/profiling/RegisterBackendCounters.cpp @@ -59,7 +59,7 @@ uint16_t RegisterBackendCounters::RegisterCounter(const uint16_t uid, counterSetUid); m_CurrentMaxGlobalCounterID = counterPtr->m_MaxCounterUid; // register mappings - IRegisterCounterMapping& counterIdMap = ProfilingService::Instance().GetCounterMappingRegistry(); + IRegisterCounterMapping& counterIdMap = m_ProfilingService.GetCounterMappingRegistry(); uint16_t globalCounterId = counterPtr->m_Uid; if (globalCounterId == counterPtr->m_MaxCounterUid) { diff --git a/src/profiling/RegisterBackendCounters.hpp b/src/profiling/RegisterBackendCounters.hpp index 8f1fa049b6..f81f487472 100644 --- a/src/profiling/RegisterBackendCounters.hpp +++ b/src/profiling/RegisterBackendCounters.hpp @@ -20,10 +20,12 @@ class RegisterBackendCounters : public IRegisterBackendCounters { public: - RegisterBackendCounters(uint16_t currentMaxGlobalCounterID, const BackendId& backendId) - : m_CurrentMaxGlobalCounterID(currentMaxGlobalCounterID), - m_CounterDirectory(ProfilingService::Instance().GetCounterRegistry()), - m_BackendId(backendId) {} + RegisterBackendCounters( + uint16_t currentMaxGlobalCounterID, const BackendId& backendId, ProfilingService& profilingService) + : m_CurrentMaxGlobalCounterID(currentMaxGlobalCounterID), + m_BackendId(backendId), + m_ProfilingService(profilingService), + m_CounterDirectory(m_ProfilingService.GetCounterRegistry()) {} ~RegisterBackendCounters() = default; @@ -51,8 +53,9 @@ public: private: uint16_t m_CurrentMaxGlobalCounterID; - ICounterRegistry& m_CounterDirectory; const BackendId& m_BackendId; + ProfilingService& m_ProfilingService; + ICounterRegistry& m_CounterDirectory; }; } // namespace profiling diff --git a/src/profiling/TimelineUtilityMethods.cpp b/src/profiling/TimelineUtilityMethods.cpp index 972916104e..7310b759d2 100644 --- a/src/profiling/TimelineUtilityMethods.cpp +++ b/src/profiling/TimelineUtilityMethods.cpp @@ -4,7 +4,6 @@ // #include "TimelineUtilityMethods.hpp" -#include "ProfilingService.hpp" #include "LabelsAndEventClasses.hpp" namespace armnn @@ -13,12 +12,12 @@ namespace armnn namespace profiling { -std::unique_ptr TimelineUtilityMethods::GetTimelineUtils() +std::unique_ptr TimelineUtilityMethods::GetTimelineUtils(ProfilingService& profilingService) { - if (ProfilingService::Instance().IsEnabled()) + if (profilingService.IsProfilingEnabled()) { - std::unique_ptr sendTimelinepacket = ProfilingService::Instance().GetSendTimelinePacket(); - return std::make_unique(sendTimelinepacket); + std::unique_ptr sendTimelinepacket = profilingService.GetSendTimelinePacket(); + return std::make_unique(sendTimelinepacket, profilingService); } else { @@ -92,7 +91,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::CreateNamedTypedEntity(const std::s } // Generate dynamic GUID of the entity - ProfilingDynamicGuid entityGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid entityGuid = m_ProfilingService.GetNextGuid(); CreateNamedTypedEntity(entityGuid, name, type); @@ -155,7 +154,7 @@ ProfilingStaticGuid TimelineUtilityMethods::DeclareLabel(const std::string& labe } // Generate a static GUID for the given label name - ProfilingStaticGuid labelGuid = ProfilingService::Instance().GenerateStaticId(labelName); + ProfilingStaticGuid labelGuid = m_ProfilingService.GetStaticId(labelName); // Send the new label to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName); @@ -178,7 +177,7 @@ void TimelineUtilityMethods::MarkEntityWithLabel(ProfilingGuid entityGuid, ProfilingStaticGuid labelGuid = DeclareLabel(labelName); // Generate a GUID for the label relationship - ProfilingDynamicGuid relationshipGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipGuid = m_ProfilingService.GetNextGuid(); // Send the new label link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, @@ -187,7 +186,7 @@ void TimelineUtilityMethods::MarkEntityWithLabel(ProfilingGuid entityGuid, labelGuid); // Generate a GUID for the label relationship - ProfilingDynamicGuid relationshipLabelGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipLabelGuid = m_ProfilingService.GetNextGuid(); // Send the new label link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, @@ -200,7 +199,7 @@ void TimelineUtilityMethods::MarkEntityWithType(ProfilingGuid entityGuid, ProfilingStaticGuid typeNameGuid) { // Generate a GUID for the label relationship - ProfilingDynamicGuid relationshipGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipGuid = m_ProfilingService.GetNextGuid(); // Send the new label link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, @@ -209,7 +208,7 @@ void TimelineUtilityMethods::MarkEntityWithType(ProfilingGuid entityGuid, typeNameGuid); // Generate a GUID for the label relationship - ProfilingDynamicGuid relationshipLabelGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipLabelGuid = m_ProfilingService.GetNextGuid(); // Send the new label link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink, @@ -250,7 +249,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::CreateNamedTypedChildEntity(Profili ProfilingDynamicGuid childEntityGuid = CreateNamedTypedEntity(entityName, entityType); // Generate a GUID for the retention link relationship - ProfilingDynamicGuid retentionLinkGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid retentionLinkGuid = m_ProfilingService.GetNextGuid(); // Send the new retention link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::RetentionLink, @@ -284,7 +283,7 @@ void TimelineUtilityMethods::CreateNamedTypedChildEntity(ProfilingGuid childEnti CreateNamedTypedEntity(childEntityGuid, entityName, entityType); // Generate a GUID for the retention link relationship - ProfilingDynamicGuid retentionLinkGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid retentionLinkGuid = m_ProfilingService.GetNextGuid(); // Send the new retention link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::RetentionLink, @@ -309,7 +308,7 @@ void TimelineUtilityMethods::CreateNamedTypedChildEntity(ProfilingGuid childEnti CreateNamedTypedEntity(childEntityGuid, entityName, typeGuid); // Generate a GUID for the retention link relationship - ProfilingDynamicGuid retentionLinkGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid retentionLinkGuid = m_ProfilingService.GetNextGuid(); // Send the new retention link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::RetentionLink, @@ -323,7 +322,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::CreateRelationship(ProfilingRelatio ProfilingGuid tailGuid) { // Generate a GUID for the relationship - ProfilingDynamicGuid relationshipGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipGuid = m_ProfilingService.GetNextGuid(); // Send the new retention link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(relationshipType, @@ -338,7 +337,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::CreateConnectionRelationship(Profil ProfilingGuid tailGuid) { // Generate a GUID for the relationship - ProfilingDynamicGuid relationshipGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid relationshipGuid = m_ProfilingService.GetNextGuid(); // Send the new retention link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(relationshipType, @@ -368,13 +367,13 @@ ProfilingDynamicGuid TimelineUtilityMethods::RecordEvent(ProfilingGuid entityGui std::thread::id threadId = std::this_thread::get_id(); // Generate a GUID for the event - ProfilingDynamicGuid eventGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid eventGuid = m_ProfilingService.GetNextGuid(); // Send the new timeline event to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineEventBinaryPacket(timestamp, threadId, eventGuid); // Generate a GUID for the execution link - ProfilingDynamicGuid executionLinkId = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid executionLinkId = m_ProfilingService.GetNextGuid(); // Send the new execution link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::ExecutionLink, @@ -383,7 +382,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::RecordEvent(ProfilingGuid entityGui eventGuid); // Generate a GUID for the data relationship link - ProfilingDynamicGuid eventClassLinkId = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid eventClassLinkId = m_ProfilingService.GetNextGuid(); // Send the new data relationship link to the external profiling service, this call throws in case of error m_SendTimelinePacket->SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::DataLink, @@ -397,7 +396,7 @@ ProfilingDynamicGuid TimelineUtilityMethods::RecordEvent(ProfilingGuid entityGui ProfilingDynamicGuid TimelineUtilityMethods::RecordWorkloadInferenceAndStartOfLifeEvent(ProfilingGuid workloadGuid, ProfilingGuid inferenceGuid) { - ProfilingDynamicGuid workloadInferenceGuid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid workloadInferenceGuid = m_ProfilingService.GetNextGuid(); CreateTypedEntity(workloadInferenceGuid, LabelsAndEventClasses::WORKLOAD_EXECUTION_GUID); CreateRelationship(ProfilingRelationshipType::RetentionLink, inferenceGuid, workloadInferenceGuid); CreateRelationship(ProfilingRelationshipType::RetentionLink, workloadGuid, workloadInferenceGuid); diff --git a/src/profiling/TimelineUtilityMethods.hpp b/src/profiling/TimelineUtilityMethods.hpp index c33dd92193..df56cd6f42 100644 --- a/src/profiling/TimelineUtilityMethods.hpp +++ b/src/profiling/TimelineUtilityMethods.hpp @@ -5,7 +5,9 @@ #pragma once +#include "ProfilingService.hpp" #include "armnn/profiling/ISendTimelinePacket.hpp" + #include namespace armnn @@ -20,13 +22,14 @@ public: // static factory method which will return a pointer to a timelie utility methods // object if profiling is enabled. Otherwise will return a null unique_ptr - static std::unique_ptr GetTimelineUtils(); + static std::unique_ptr GetTimelineUtils(ProfilingService& profilingService); - TimelineUtilityMethods(std::unique_ptr& sendTimelinePacket) - : m_SendTimelinePacket(std::move(sendTimelinePacket)) {} + TimelineUtilityMethods( + std::unique_ptr& sendTimelinePacket, ProfilingService& profilingService) + : m_SendTimelinePacket(std::move(sendTimelinePacket)), m_ProfilingService(profilingService) {} TimelineUtilityMethods(TimelineUtilityMethods&& other) - : m_SendTimelinePacket(std::move(other.m_SendTimelinePacket)) {} + : m_SendTimelinePacket(std::move(other.m_SendTimelinePacket)), m_ProfilingService(other.m_ProfilingService) {} TimelineUtilityMethods(const TimelineUtilityMethods& other) = delete; @@ -89,6 +92,7 @@ public: private: std::unique_ptr m_SendTimelinePacket; + profiling::ProfilingService& m_ProfilingService; }; } // namespace profiling diff --git a/src/profiling/backends/BackendProfiling.cpp b/src/profiling/backends/BackendProfiling.cpp index 0926879f67..4e6de93062 100644 --- a/src/profiling/backends/BackendProfiling.cpp +++ b/src/profiling/backends/BackendProfiling.cpp @@ -15,7 +15,8 @@ namespace profiling std::unique_ptr BackendProfiling::GetCounterRegistrationInterface(uint16_t currentMaxGlobalCounterID) { - return std::make_unique(RegisterBackendCounters(currentMaxGlobalCounterID, m_BackendId)); + return std::make_unique( + RegisterBackendCounters(currentMaxGlobalCounterID, m_BackendId, m_ProfilingService)); } std::unique_ptr BackendProfiling::GetSendTimelinePacket() @@ -73,7 +74,7 @@ std::vector BackendProfiling::GetActiveCounters() for (auto globalCounterId : globalCounterIds) { // Get pair of local counterId and backendId using globalCounterId const std::pair& backendCounterIdPair = - ProfilingService::Instance().GetCounterMappings().GetBackendId(globalCounterId); + m_ProfilingService.GetCounterMappings().GetBackendId(globalCounterId); if (backendCounterIdPair.second == m_BackendId) { activeCounterIds.emplace_back(backendCounterIdPair.first, diff --git a/src/profiling/backends/BackendProfiling.hpp b/src/profiling/backends/BackendProfiling.hpp index c0f3eea978..71d0af16ff 100644 --- a/src/profiling/backends/BackendProfiling.hpp +++ b/src/profiling/backends/BackendProfiling.hpp @@ -5,7 +5,7 @@ #pragma once -#include "IProfilingService.hpp" +#include "ProfilingService.hpp" #include namespace armnn @@ -18,7 +18,7 @@ class BackendProfiling : public IBackendProfiling { public: BackendProfiling(const IRuntime::CreationOptions& options, - IProfilingService& profilingService, + ProfilingService& profilingService, const BackendId& backendId) : m_Options(options), m_ProfilingService(profilingService), @@ -44,7 +44,7 @@ public: private: IRuntime::CreationOptions m_Options; - IProfilingService& m_ProfilingService; + ProfilingService& m_ProfilingService; BackendId m_BackendId; }; } // namespace profiling diff --git a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp index 7db42de416..baadb8555d 100644 --- a/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp +++ b/src/profiling/test/FileOnlyProfilingDecoratorTests.cpp @@ -30,8 +30,9 @@ class FileOnlyHelperService : public ProfilingService // Wait for a notification from the send thread bool WaitForPacketsSent(uint32_t timeout = 1000) { - return ProfilingService::WaitForPacketSent(ProfilingService::Instance(), timeout); + return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); } + armnn::profiling::ProfilingService m_ProfilingService; }; BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests) @@ -52,7 +53,7 @@ BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd, * boost::unit_test::disabled FileOnlyHelperService helper; // Enable the profiling service - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); // Bring the profiling service to the "WaitingForAck" state profilingService.Update(); diff --git a/src/profiling/test/ProfilingMocks.hpp b/src/profiling/test/ProfilingMocks.hpp index 944aea6454..eeb641e878 100644 --- a/src/profiling/test/ProfilingMocks.hpp +++ b/src/profiling/test/ProfilingMocks.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include @@ -620,7 +620,7 @@ private: Counters m_Counters; }; -class MockProfilingService : public IProfilingService, public IRegisterCounterMapping +class MockProfilingService : public ProfilingService { public: MockProfilingService(MockBufferManager& mockBufferManager, @@ -670,12 +670,12 @@ public: void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, - const armnn::BackendId& backendId) override + const armnn::BackendId& backendId) { m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId); } - void Reset() override + void Reset() { m_CounterMapping.Reset(); } diff --git a/src/profiling/test/ProfilingTestUtils.cpp b/src/profiling/test/ProfilingTestUtils.cpp index 89b5926da7..17291bad64 100644 --- a/src/profiling/test/ProfilingTestUtils.cpp +++ b/src/profiling/test/ProfilingTestUtils.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include @@ -78,7 +79,8 @@ void VerifyTimelineLabelBinaryPacketData(Optional guid, } else { - BOOST_CHECK(readProfilingGuid == ProfilingService::Instance().GenerateStaticId(label)); + armnn::profiling::ProfilingService profilingService; + BOOST_CHECK(readProfilingGuid == profilingService.GetStaticId(label)); } // Check the SWTrace label @@ -294,9 +296,8 @@ void VerifyPostOptimisationStructureTestImpl(armnn::BackendId backendId) // Create runtime in which test will run armnn::IRuntime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; - armnn::profiling::ProfilingService& profilingService = armnn::profiling::ProfilingService::Instance(); - profilingService.ConfigureProfilingService(options.m_ProfilingOptions, true); - armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); +// armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); + armnn::Runtime runtime(options); // build up the structure of the network INetworkPtr net(INetwork::Create()); @@ -354,15 +355,15 @@ void VerifyPostOptimisationStructureTestImpl(armnn::BackendId backendId) // optimize the network std::vector backends = { backendId }; - IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec()); + IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); ProfilingGuid optNetGuid = optNet->GetGuid(); // Load it into the runtime. It should success. armnn::NetworkId netId; - BOOST_TEST(runtime->LoadNetwork(netId, std::move(optNet)) == Status::Success); + BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); - profiling::ProfilingServiceRuntimeHelper profilingServiceHelper; + profiling::ProfilingServiceRuntimeHelper profilingServiceHelper(GetProfilingService(&runtime)); profiling::BufferManager& bufferManager = profilingServiceHelper.GetProfilingBufferManager(); auto readableBuffer = bufferManager.GetReadableBuffer(); @@ -762,15 +763,15 @@ void VerifyPostOptimisationStructureTestImpl(armnn::BackendId backendId) InputTensors inputTensors { - { 0, ConstTensor(runtime->GetInputTensorInfo(netId, 0), inputData.data()) } + {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} }; OutputTensors outputTensors { - { 0, Tensor(runtime->GetOutputTensorInfo(netId, 0), outputData.data()) } + {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} }; // Does the inference. - runtime->EnqueueWorkload(netId, inputTensors, outputTensors); + runtime.EnqueueWorkload(netId, inputTensors, outputTensors); // Get readable buffer for inference timeline auto inferenceReadableBuffer = bufferManager.GetReadableBuffer(); diff --git a/src/profiling/test/ProfilingTestUtils.hpp b/src/profiling/test/ProfilingTestUtils.hpp index 175c9cc167..459d62435b 100644 --- a/src/profiling/test/ProfilingTestUtils.hpp +++ b/src/profiling/test/ProfilingTestUtils.hpp @@ -59,13 +59,15 @@ namespace profiling class ProfilingServiceRuntimeHelper : public ProfilingService { public: - ProfilingServiceRuntimeHelper() = default; + ProfilingServiceRuntimeHelper(ProfilingService& profilingService) + : m_ProfilingService(profilingService) {} ~ProfilingServiceRuntimeHelper() = default; BufferManager& GetProfilingBufferManager() { - return GetBufferManager(ProfilingService::Instance()); + return GetBufferManager(m_ProfilingService); } + armnn::profiling::ProfilingService& m_ProfilingService; }; } // namespace profiling diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp index 0e91696af8..29c5299bd3 100644 --- a/src/profiling/test/ProfilingTests.cpp +++ b/src/profiling/test/ProfilingTests.cpp @@ -645,7 +645,7 @@ BOOST_AUTO_TEST_CASE(CaptureDataMethods) BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled) { armnn::Runtime::CreationOptions::ExternalProfilingOptions options; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); @@ -655,7 +655,7 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled) BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterDirectory) { armnn::Runtime::CreationOptions::ExternalProfilingOptions options; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); const ICounterDirectory& counterDirectory0 = profilingService.GetCounterDirectory(); @@ -679,7 +679,7 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceCounterValues) { armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); profilingService.Update(); @@ -2332,9 +2332,6 @@ BOOST_AUTO_TEST_CASE(RequestCounterDirectoryCommandHandlerTest2) BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; - // Calculate the size of a Stream Metadata packet std::string processName = GetProcessName().substr(0, 60); unsigned int processNameSize = processName.empty() ? 0 : boost::numeric_cast(processName.size()) + 1; @@ -2343,9 +2340,12 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket) // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "WaitingForAck" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2397,15 +2397,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodConnectionAcknowledgedPacket) BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; - // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2455,15 +2455,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodRequestCounterDirectoryPacket) BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInvalidCounterUid) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; - // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2533,15 +2533,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacketInval BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCounters) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; - // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2597,15 +2597,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketNoCo BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSingleCounter) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; - // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2674,14 +2674,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketSing BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMultipleCounters) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2752,14 +2753,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPeriodicCounterSelectionPacketMult BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Try to disconnect the profiling service while in the "Uninitialised" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Disconnect(); @@ -2809,14 +2811,15 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisconnect) BOOST_AUTO_TEST_CASE(CheckProfilingServiceGoodPerJobCounterSelectionPacket) { - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -2877,7 +2880,7 @@ BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn) { armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.ConfigureProfilingService(options); // should get as far as NOT_CONNECTED @@ -2890,7 +2893,7 @@ BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOn) BOOST_AUTO_TEST_CASE(CheckConfigureProfilingServiceOff) { armnn::Runtime::CreationOptions::ExternalProfilingOptions options; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.ConfigureProfilingService(options); // should not move from Uninitialised @@ -2906,7 +2909,7 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled) LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning); armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); @@ -2936,7 +2939,7 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime) // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning); armnn::Runtime::CreationOptions::ExternalProfilingOptions options; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); @@ -2970,8 +2973,7 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket) { // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning); - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; + // Redirect the standard output to a local stream so that we can parse the warning message std::stringstream ss; @@ -2980,9 +2982,12 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadConnectionAcknowledgedPacket) // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "WaitingForAck" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -3031,8 +3036,6 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket) { // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning); - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; // Redirect the standard output to a local stream so that we can parse the warning message std::stringstream ss; @@ -3041,9 +3044,12 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadRequestCounterDirectoryPacket) // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); helper.ForceTransitionToState(ProfilingState::NotConnected); @@ -3094,8 +3100,6 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket) { // Locally reduce log level to "Warning", as this test needs to parse a warning message from the standard output LogLevelSwapper logLevelSwapper(armnn::LogSeverity::Warning); - // Swap the profiling connection factory in the profiling service instance with our mock one - SwapProfilingConnectionFactoryHelper helper; // Redirect the standard output to a local stream so that we can parse the warning message std::stringstream ss; @@ -3104,9 +3108,12 @@ BOOST_AUTO_TEST_CASE(CheckProfilingServiceBadPeriodicCounterSelectionPacket) // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + // Swap the profiling connection factory in the profiling service instance with our mock one + SwapProfilingConnectionFactoryHelper helper(profilingService); + // Bring the profiling service to the "Active" state BOOST_CHECK(profilingService.GetCurrentState() == ProfilingState::Uninitialised); profilingService.Update(); // Initialize the counter directory @@ -3198,14 +3205,16 @@ BOOST_AUTO_TEST_CASE(CheckRegisterBackendCounters) uint16_t globalCounterIds = armnn::profiling::INFERENCES_RUN; armnn::BackendId cpuRefId(armnn::Compute::CpuRef); - RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId); - // Reset the profiling service to the uninitialized state armnn::Runtime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); + RegisterBackendCounters registerBackendCounters(globalCounterIds, cpuRefId, profilingService); + + + BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty()); registerBackendCounters.RegisterCategory("categoryOne"); auto categoryOnePtr = profilingService.GetCounterDirectory().GetCategory("categoryOne"); @@ -3248,7 +3257,7 @@ BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery) options.m_ProfilingOptions.m_EnableProfiling = true; // Reset the profiling service to the uninitialized state - ProfilingService& profilingService = ProfilingService::Instance(); + ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true); const armnn::BackendId cpuRefId(armnn::Compute::CpuRef); @@ -3261,7 +3270,7 @@ BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery) uint16_t initialNumGlobalCounterIds = armnn::profiling::INFERENCES_RUN; // Create RegisterBackendCounters for CpuRef - RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId); + RegisterBackendCounters registerBackendCountersCpuRef(initialNumGlobalCounterIds, cpuRefId, profilingService); // Create 'testCategory' in CounterDirectory (backend agnostic) BOOST_CHECK(profilingService.GetCounterDirectory().GetCategories().empty()); @@ -3298,7 +3307,7 @@ BOOST_AUTO_TEST_CASE(CheckCounterStatusQuery) BOOST_CHECK(backendMapping.second == cpuRefId); // Create RegisterBackendCounters for CpuAcc - RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId); + RegisterBackendCounters registerBackendCountersCpuAcc(currentNumGlobalCounterIds, cpuAccId, profilingService); // Register the backend counter for CpuAcc and validate GetGlobalId and GetBackendId currentNumGlobalCounterIds = registerBackendCountersCpuAcc.RegisterCounter( @@ -3384,6 +3393,7 @@ BOOST_AUTO_TEST_CASE(CheckRegisterCounters) armnn::Runtime::CreationOptions options; options.m_ProfilingOptions.m_EnableProfiling = true; MockBufferManager mockBuffer(1024); + CaptureData captureData; MockProfilingService mockProfilingService( mockBuffer, options.m_ProfilingOptions.m_EnableProfiling, captureData); diff --git a/src/profiling/test/ProfilingTests.hpp b/src/profiling/test/ProfilingTests.hpp index 008110392c..a5971e0a4b 100644 --- a/src/profiling/test/ProfilingTests.hpp +++ b/src/profiling/test/ProfilingTests.hpp @@ -204,13 +204,15 @@ class SwapProfilingConnectionFactoryHelper : public ProfilingService public: using MockProfilingConnectionFactoryPtr = std::unique_ptr; - SwapProfilingConnectionFactoryHelper() + SwapProfilingConnectionFactoryHelper(armnn::profiling::ProfilingService& profilingService) : ProfilingService() + , m_ProfilingService(profilingService) , m_MockProfilingConnectionFactory(new MockProfilingConnectionFactory()) , m_BackupProfilingConnectionFactory(nullptr) + { BOOST_CHECK(m_MockProfilingConnectionFactory); - SwapProfilingConnectionFactory(ProfilingService::Instance(), + SwapProfilingConnectionFactory(m_ProfilingService, m_MockProfilingConnectionFactory.get(), m_BackupProfilingConnectionFactory); BOOST_CHECK(m_BackupProfilingConnectionFactory); @@ -219,20 +221,20 @@ public: { BOOST_CHECK(m_BackupProfilingConnectionFactory); IProfilingConnectionFactory* temp = nullptr; - SwapProfilingConnectionFactory(ProfilingService::Instance(), + SwapProfilingConnectionFactory(m_ProfilingService, m_BackupProfilingConnectionFactory, temp); } MockProfilingConnection* GetMockProfilingConnection() { - IProfilingConnection* profilingConnection = GetProfilingConnection(ProfilingService::Instance()); + IProfilingConnection* profilingConnection = GetProfilingConnection(m_ProfilingService); return boost::polymorphic_downcast(profilingConnection); } void ForceTransitionToState(ProfilingState newState) { - TransitionToState(ProfilingService::Instance(), newState); + TransitionToState(m_ProfilingService, newState); } long WaitForPacketsSent(MockProfilingConnection* mockProfilingConnection, @@ -247,7 +249,7 @@ public: { std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); // Wait for a notification from the send thread - ProfilingService::WaitForPacketSent(ProfilingService::Instance(), timeout); + ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); @@ -268,6 +270,7 @@ public: } private: + armnn::profiling::ProfilingService& m_ProfilingService; MockProfilingConnectionFactoryPtr m_MockProfilingConnectionFactory; IProfilingConnectionFactory* m_BackupProfilingConnectionFactory; }; diff --git a/src/profiling/test/SendTimelinePacketTests.cpp b/src/profiling/test/SendTimelinePacketTests.cpp index 2e64c75b90..c03e745574 100644 --- a/src/profiling/test/SendTimelinePacketTests.cpp +++ b/src/profiling/test/SendTimelinePacketTests.cpp @@ -397,18 +397,18 @@ BOOST_AUTO_TEST_CASE(GetGuidsFromProfilingService) { armnn::IRuntime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); - ProfilingStaticGuid staticGuid = profilingService.GenerateStaticId("dummy"); + ProfilingStaticGuid staticGuid = profilingService.GetStaticId("dummy"); std::hash hasher; uint64_t hash = static_cast(hasher("dummy")); ProfilingStaticGuid expectedStaticValue(hash | MIN_STATIC_GUID); BOOST_CHECK(staticGuid == expectedStaticValue); - ProfilingDynamicGuid dynamicGuid = profilingService.NextGuid(); + ProfilingDynamicGuid dynamicGuid = profilingService.GetNextGuid(); uint64_t dynamicGuidValue = static_cast(dynamicGuid); ++dynamicGuidValue; ProfilingDynamicGuid expectedDynamicValue(dynamicGuidValue); - dynamicGuid = profilingService.NextGuid(); + dynamicGuid = profilingService.GetNextGuid(); BOOST_CHECK(dynamicGuid == expectedDynamicValue); } @@ -416,7 +416,7 @@ BOOST_AUTO_TEST_CASE(GetTimelinePackerWriterFromProfilingService) { armnn::IRuntime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - ProfilingService& profilingService = ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); std::unique_ptr writer = profilingService.GetSendTimelinePacket(); diff --git a/src/profiling/test/TimelineUtilityMethodsTests.cpp b/src/profiling/test/TimelineUtilityMethodsTests.cpp index fed31c3def..f83a9622ca 100644 --- a/src/profiling/test/TimelineUtilityMethodsTests.cpp +++ b/src/profiling/test/TimelineUtilityMethodsTests.cpp @@ -23,11 +23,13 @@ BOOST_AUTO_TEST_SUITE(TimelineUtilityMethodsTests) BOOST_AUTO_TEST_CASE(CreateTypedLabelTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; + std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. - ProfilingService::Instance().NextGuid(); + profilingService.NextGuid(); ProfilingGuid entityGuid(123); const std::string entityName = "some entity"; @@ -78,8 +80,9 @@ BOOST_AUTO_TEST_CASE(CreateTypedLabelTest) BOOST_AUTO_TEST_CASE(SendWellKnownLabelsAndEventClassesTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); BOOST_CHECK_NO_THROW(timelineUtilityMethods.SendWellKnownLabelsAndEventClasses()); @@ -178,8 +181,9 @@ BOOST_AUTO_TEST_CASE(SendWellKnownLabelsAndEventClassesTest) BOOST_AUTO_TEST_CASE(CreateNamedTypedChildEntityTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); ProfilingDynamicGuid childEntityGuid(0); ProfilingGuid parentEntityGuid(123); @@ -187,7 +191,7 @@ BOOST_AUTO_TEST_CASE(CreateNamedTypedChildEntityTest) const std::string entityType = "some type"; // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. - ProfilingService::Instance().NextGuid(); + profilingService.NextGuid(); BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedChildEntity(parentEntityGuid, "", entityType), InvalidArgumentException); @@ -276,11 +280,12 @@ BOOST_AUTO_TEST_CASE(CreateNamedTypedChildEntityTest) BOOST_AUTO_TEST_CASE(DeclareLabelTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. - ProfilingService::Instance().NextGuid(); + profilingService.NextGuid(); // Try declaring an invalid (empty) label BOOST_CHECK_THROW(timelineUtilityMethods.DeclareLabel(""), InvalidArgumentException); @@ -304,8 +309,9 @@ BOOST_AUTO_TEST_CASE(DeclareLabelTest) BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); // Invalid name BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("", "Type"), InvalidArgumentException); @@ -313,7 +319,7 @@ BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest) // Invalid type BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("Name", ""), InvalidArgumentException); - ProfilingDynamicGuid guid = ProfilingService::Instance().NextGuid(); + ProfilingDynamicGuid guid = profilingService.NextGuid(); // CreatedNamedTypedEntity with Guid - Invalid name BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity(guid, "", "Type"), @@ -328,14 +334,15 @@ BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest) BOOST_AUTO_TEST_CASE(CreateNameTypeEntityTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); const std::string entityName = "Entity0"; const std::string entityType = "Type0"; // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. - ProfilingService::Instance().NextGuid(); + profilingService.NextGuid(); ProfilingDynamicGuid guid = timelineUtilityMethods.CreateNamedTypedEntity(entityName, entityType); BOOST_CHECK(guid != ProfilingGuid(0)); @@ -407,10 +414,11 @@ BOOST_AUTO_TEST_CASE(CreateNameTypeEntityTest) BOOST_AUTO_TEST_CASE(RecordEventTest) { MockBufferManager mockBufferManager(1024); + ProfilingService profilingService; std::unique_ptr sendTimelinePacket = std::make_unique(mockBufferManager); - TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket); + TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket, profilingService); // Generate first guid to ensure that the named typed entity guid is not 0 on local single test. - ProfilingService::Instance().NextGuid(); + profilingService.NextGuid(); ProfilingGuid entityGuid(123); ProfilingStaticGuid eventClassGuid(456); diff --git a/tests/profiling/gatordmock/tests/GatordMockTests.cpp b/tests/profiling/gatordmock/tests/GatordMockTests.cpp index bba848588e..71ee5ed013 100644 --- a/tests/profiling/gatordmock/tests/GatordMockTests.cpp +++ b/tests/profiling/gatordmock/tests/GatordMockTests.cpp @@ -159,7 +159,7 @@ BOOST_AUTO_TEST_CASE(GatorDMockEndToEnd) // Enable the profiling service. armnn::IRuntime::CreationOptions::ExternalProfilingOptions options; options.m_EnableProfiling = true; - profiling::ProfilingService& profilingService = profiling::ProfilingService::Instance(); + armnn::profiling::ProfilingService profilingService; profilingService.ResetExternalProfilingOptions(options, true); // Bring the profiling service to the "WaitingForAck" state -- cgit v1.2.1