xref: /aosp_15_r20/external/armnn/profiling/client/src/ConnectionAcknowledgedCommandHandler.cpp (revision 89c4ff92f2867872bb9e2354d150bf0c8c502810)
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "ConnectionAcknowledgedCommandHandler.hpp"
7 
8 #include <client/include/TimelineUtilityMethods.hpp>
9 
10 #include <common/include/ProfilingException.hpp>
11 
12 #include <fmt/format.h>
13 
14 namespace arm
15 {
16 
17 namespace pipe
18 {
19 
operator ()(const arm::pipe::Packet & packet)20 void ConnectionAcknowledgedCommandHandler::operator()(const arm::pipe::Packet& packet)
21 {
22     ProfilingState currentState = m_StateMachine.GetCurrentState();
23     switch (currentState)
24     {
25     case ProfilingState::Uninitialised:
26     case ProfilingState::NotConnected:
27         throw arm::pipe::ProfilingException(fmt::format("Connection Acknowledged Command Handler invoked while in an "
28                                             "wrong state: {}",
29                                             GetProfilingStateName(currentState)));
30     case ProfilingState::WaitingForAck:
31         // Process the packet
32         if (!(packet.GetPacketFamily() == 0u && packet.GetPacketId() == 1u))
33         {
34             throw arm::pipe::InvalidArgumentException(fmt::format("Expected Packet family = 0, id = 1 but "
35                                                                   "received family = {}, id = {}",
36                                                                   packet.GetPacketFamily(),
37                                                                   packet.GetPacketId()));
38         }
39 
40         // Once a Connection Acknowledged packet has been received, move to the Active state immediately
41         m_StateMachine.TransitionToState(ProfilingState::Active);
42         // Send the counter directory packet.
43         m_SendCounterPacket.SendCounterDirectoryPacket(m_CounterDirectory);
44 
45         if (m_TimelineEnabled)
46         {
47             m_SendTimelinePacket.SendTimelineMessageDirectoryPackage();
48             TimelineUtilityMethods::SendWellKnownLabelsAndEventClasses(m_SendTimelinePacket);
49         }
50 
51         if (m_BackendProfilingContext.has_value())
52         {
53             for (auto backendContext : m_BackendProfilingContext.value())
54             {
55                 // Enable profiling on the backend and assert that it returns true
56                 if(!backendContext.second->EnableProfiling(true))
57                 {
58                     throw arm::pipe::BackendProfilingException(
59                             "Unable to enable profiling on Backend Id: " + backendContext.first);
60                 }
61             }
62         }
63 
64         // At this point signal any external processes waiting on the profiling system
65         // to come up that profiling is fully active
66         m_ProfilingServiceStatus.NotifyProfilingServiceActive();
67         break;
68     case ProfilingState::Active:
69         return; // NOP
70     default:
71         throw arm::pipe::ProfilingException(fmt::format("Unknown profiling service state: {}",
72                                             static_cast<int>(currentState)));
73     }
74 }
75 
76 } // namespace pipe
77 
78 } // namespace arm
79