aboutsummaryrefslogtreecommitdiff
path: root/tests/profiling/timelineDecoder/tests/TimelineTests.cpp
blob: a9c352b5faf48ca8fcceb6bee8d231cf6072a481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Copyright © 2019 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "../TimelineCaptureCommandHandler.hpp"
#include "../TimelineDirectoryCaptureCommandHandler.hpp"
#include "../ITimelineDecoder.h"
#include "../TimelineModel.h"
#include "TimelineTestFunctions.hpp"

#include <CommandHandlerFunctor.hpp>
#include <ProfilingService.hpp>
#include <PacketBuffer.hpp>
#include <TimelinePacketWriterFactory.hpp>

#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test_suite.hpp>

BOOST_AUTO_TEST_SUITE(TimelineDecoderTests)

using namespace armnn;

void SendTimelinePacketToCommandHandler(const unsigned char* packetBuffer,
                                        profiling::CommandHandlerFunctor &CommandHandler)
{
    uint32_t uint32_t_size = sizeof(uint32_t);
    unsigned int offset = 0;

    uint32_t header[2];
    header[0] = profiling::ReadUint32(packetBuffer, offset);
    offset += uint32_t_size;
    header[1] = profiling::ReadUint32(packetBuffer, offset);
    offset += uint32_t_size;

    uint32_t PacketDataLength  = header[1] & 0x00FFFFFF;

    std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(PacketDataLength);

    std::memcpy(uniquePacketData.get(), packetBuffer + offset, PacketDataLength);

    armnn::profiling::Packet packet = armnn::profiling::Packet(header[0], PacketDataLength, uniquePacketData);

    CommandHandler(packet);
}

BOOST_AUTO_TEST_CASE(TimelineDirecotryTest)
{
    uint32_t uint32_t_size = sizeof(uint32_t);

    profiling::BufferManager bufferManager(5);
    profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);

    std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
            timelinePacketWriterFactory.GetSendTimelinePacket();

    profiling::PacketVersionResolver packetVersionResolver;

    gatordmock::TimelineDirectoryCaptureCommandHandler timelineDirectoryCaptureCommandHandler(
            1, 0, packetVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(), true);

    sendTimelinePacket->SendTimelineMessageDirectoryPackage();
    sendTimelinePacket->Commit();

    std::vector<profiling::SwTraceMessage> swTraceBufferMessages;

    unsigned int offset = uint32_t_size * 2;

    std::unique_ptr<profiling::IPacketBuffer> packetBuffer = bufferManager.GetReadableBuffer();

    uint32_t declarationSize = profiling::ReadUint32(packetBuffer, offset);
    offset += uint32_t_size;
    for(uint32_t i = 0; i < declarationSize; ++i)
    {
        swTraceBufferMessages.push_back(profiling::ReadSwTraceMessage(packetBuffer->GetReadableData(), offset));
    }

    SendTimelinePacketToCommandHandler(packetBuffer->GetReadableData(), timelineDirectoryCaptureCommandHandler);

    for(uint32_t index = 0; index < declarationSize; ++index)
    {
        profiling::SwTraceMessage& bufferMessage = swTraceBufferMessages[index];
        profiling::SwTraceMessage& handlerMessage = timelineDirectoryCaptureCommandHandler.m_SwTraceMessages[index];

        BOOST_CHECK(bufferMessage.name == handlerMessage.name);
        BOOST_CHECK(bufferMessage.uiName == handlerMessage.uiName);
        BOOST_CHECK(bufferMessage.id == handlerMessage.id);

        BOOST_CHECK(bufferMessage.argTypes.size() == handlerMessage.argTypes.size());
        for(uint32_t i = 0; i < bufferMessage.argTypes.size(); ++i)
        {
            BOOST_CHECK(bufferMessage.argTypes[i] == handlerMessage.argTypes[i]);
        }

        BOOST_CHECK(bufferMessage.argNames.size() == handlerMessage.argNames.size());
        for(uint32_t i = 0; i < bufferMessage.argNames.size(); ++i)
        {
            BOOST_CHECK(bufferMessage.argNames[i] == handlerMessage.argNames[i]);
        }
    }
}

BOOST_AUTO_TEST_CASE(TimelineCaptureTest)
{
    uint32_t threadId_size = sizeof(std::thread::id);

    profiling::BufferManager bufferManager(50);
    profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);

    std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
        timelinePacketWriterFactory.GetSendTimelinePacket();

    profiling::PacketVersionResolver packetVersionResolver;

    Model* modelPtr;
    CreateModel(&modelPtr);

    gatordmock::TimelineCaptureCommandHandler timelineCaptureCommandHandler(
        1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), modelPtr, true);

    BOOST_CHECK(SetEntityCallback(PushEntity, modelPtr)             == ErrorCode_Success);
    BOOST_CHECK(SetEventClassCallback(PushEventClass, modelPtr)     == ErrorCode_Success);
    BOOST_CHECK(SetEventCallback(PushEvent, modelPtr)               == ErrorCode_Success);
    BOOST_CHECK(SetLabelCallback(PushLabel, modelPtr)               == ErrorCode_Success);
    BOOST_CHECK(SetRelationshipCallback(PushRelationship, modelPtr) == ErrorCode_Success);

    const uint64_t entityGuid = 22222u;

    const uint64_t eventClassGuid = 33333u;

    const uint64_t timestamp = 111111u;
    const uint64_t eventGuid = 55555u;

    const std::thread::id threadId = std::this_thread::get_id();;

    const uint64_t labelGuid = 11111u;
    std::string labelName = "test_label";

    const uint64_t relationshipGuid = 44444u;
    const uint64_t headGuid = 111111u;
    const uint64_t tailGuid = 222222u;

    for (int i = 0; i < 10; ++i)
    {
        // Send entity
        sendTimelinePacket->SendTimelineEntityBinaryPacket(entityGuid);
        sendTimelinePacket->Commit();
        SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
                                           timelineCaptureCommandHandler);

        // Send event class
        sendTimelinePacket->SendTimelineEventClassBinaryPacket(eventClassGuid);
        sendTimelinePacket->Commit();
        SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
                                           timelineCaptureCommandHandler);

        // Send event
        sendTimelinePacket->SendTimelineEventBinaryPacket(timestamp, threadId, eventGuid);
        sendTimelinePacket->Commit();
        SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
                                           timelineCaptureCommandHandler);

        // Send label
        sendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName);
        sendTimelinePacket->Commit();
        SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
                                           timelineCaptureCommandHandler);

        // Send relationship
        profiling::ProfilingRelationshipType relationshipType = profiling::ProfilingRelationshipType::DataLink;
        sendTimelinePacket->SendTimelineRelationshipBinaryPacket(relationshipType,
                                                                 relationshipGuid,
                                                                 headGuid,
                                                                 tailGuid);
        sendTimelinePacket->Commit();
        SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
                                           timelineCaptureCommandHandler);
    }

    for (int i = 0; i < 10; ++i)
    {
        BOOST_CHECK(modelPtr->m_Entities[i]->m_Guid == entityGuid);

        BOOST_CHECK(modelPtr->m_EventClasses[i]->m_Guid == eventClassGuid);

        BOOST_CHECK(modelPtr->m_Events[i]->m_TimeStamp == timestamp);

        std::vector<uint8_t> readThreadId(threadId_size, 0);
        profiling::ReadBytes(modelPtr->m_Events[i]->m_ThreadId, 0, threadId_size, readThreadId.data());
        BOOST_CHECK(readThreadId == threadId);

        BOOST_CHECK(modelPtr->m_Events[i]->m_Guid == eventGuid);

        BOOST_CHECK(modelPtr->m_Labels[i]->m_Guid == labelGuid);
        BOOST_CHECK(std::string(modelPtr->m_Labels[i]->m_Name) == labelName);

        BOOST_CHECK(modelPtr->m_Relationships[i]->m_RelationshipType == RelationshipType::DataLink);
        BOOST_CHECK(modelPtr->m_Relationships[i]->m_Guid == relationshipGuid);
        BOOST_CHECK(modelPtr->m_Relationships[i]->m_HeadGuid == headGuid);
        BOOST_CHECK(modelPtr->m_Relationships[i]->m_TailGuid == tailGuid);
    }

    DestroyModel(&modelPtr);
}

BOOST_AUTO_TEST_SUITE_END()