blob: 05b6e6a5106bef7a27da086825ce2b1182e42702 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5
6#pragma once
7
Keith Davis554fa092021-07-20 11:25:22 +01008#include <string>
telsoa01c577f2c2018-08-31 09:22:23 +01009#include <map>
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010010#include <set>
Keith Davis554fa092021-07-20 11:25:22 +010011#include <sstream>
telsoa01c577f2c2018-08-31 09:22:23 +010012
Keith Davis5a64f222021-08-04 10:35:20 +010013#include <ProfilingGuid.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010014#include "Instrument.hpp"
Keith Davis554fa092021-07-20 11:25:22 +010015#include "JsonUtils.hpp"
telsoa01c577f2c2018-08-31 09:22:23 +010016
17namespace armnn
18{
19
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010020enum class JsonObjectType
21{
22 Measurement,
Keith Davis554fa092021-07-20 11:25:22 +010023 Event,
24 ExecObjectDesc
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010025};
26
telsoa01c577f2c2018-08-31 09:22:23 +010027struct JsonChildObject
28{
Keith Davis554fa092021-07-20 11:25:22 +010029 // Object type changes according to the JsonObjectType specified in enum
telsoa01c577f2c2018-08-31 09:22:23 +010030 JsonChildObject(const std::string& label)
Keith Davis5a64f222021-08-04 10:35:20 +010031 : m_Label(label),
32 m_Unit(Measurement::Unit::TIME_MS),
33 m_Type(JsonObjectType::Event),
Keith Davisec22ad22021-10-22 14:17:19 +010034 m_Guid(armnn::EmptyOptional()),
35 m_DetailsOnly(false)
telsoa01c577f2c2018-08-31 09:22:23 +010036 {}
37 JsonChildObject(const JsonChildObject&) = default;
38
39 void AddMeasurement(const double measurement)
40 {
41 m_Measurements.push_back(measurement);
42 }
43
Keith Davis554fa092021-07-20 11:25:22 +010044 void SetAndParseDetails(std::string layerDetailsStr)
45 {
46 std::stringstream layerDetails(layerDetailsStr);
47 std::string stringLine;
48 while (std::getline(layerDetails, stringLine, '\n'))
49 {
50 m_LayerDetailsList.push_back(stringLine);
51 }
52 }
53
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000054 void SetGuid(arm::pipe::ProfilingGuid guid)
Keith Davis5a64f222021-08-04 10:35:20 +010055 {
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000056 m_Guid = Optional<arm::pipe::ProfilingGuid>(guid);
Keith Davis5a64f222021-08-04 10:35:20 +010057 }
58
telsoa01c577f2c2018-08-31 09:22:23 +010059 void AddChild(const JsonChildObject& childObject)
60 {
61 m_Children.push_back(childObject);
62 }
63
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010064 JsonChildObject& GetChild(const unsigned int index)
telsoa01c577f2c2018-08-31 09:22:23 +010065 {
66 return m_Children[index];
67 }
68
69 void SetUnit(const Measurement::Unit unit)
70 {
71 m_Unit = unit;
72 }
73
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010074 size_t NumChildren() const
75 {
76 return m_Children.size();
77 }
78
79 void SetType(JsonObjectType type)
80 {
81 m_Type = type;
82 }
83
84 JsonObjectType GetType() const
85 {
86 return m_Type;
87 }
88
Keith Davisec22ad22021-10-22 14:17:19 +010089 void EnableDetailsOnly()
90 {
91 m_DetailsOnly = true;
92 }
93
94 bool IsDetailsOnlyEnabled() const
95 {
96 return m_DetailsOnly;
97 }
98
telsoa01c577f2c2018-08-31 09:22:23 +010099 ~JsonChildObject() = default;
100
101 std::string m_Label;
102 Measurement::Unit m_Unit;
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100103 JsonObjectType m_Type;
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000104 Optional<arm::pipe::ProfilingGuid> m_Guid;
telsoa01c577f2c2018-08-31 09:22:23 +0100105 std::vector<double> m_Measurements;
Keith Davis554fa092021-07-20 11:25:22 +0100106 std::vector<std::string> m_LayerDetailsList;
telsoa01c577f2c2018-08-31 09:22:23 +0100107 std::vector<JsonChildObject> m_Children;
108
109private:
Keith Davisec22ad22021-10-22 14:17:19 +0100110 bool m_DetailsOnly;
telsoa01c577f2c2018-08-31 09:22:23 +0100111 JsonChildObject() = delete;
112};
113
Keith Davis554fa092021-07-20 11:25:22 +0100114class JsonPrinter : public JsonUtils
telsoa01c577f2c2018-08-31 09:22:23 +0100115{
116public:
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100117 void PrintJsonChildObject(const JsonChildObject& object, size_t& id);
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100118 void PrintLabel(const std::string& label, size_t id);
telsoa01c577f2c2018-08-31 09:22:23 +0100119 void PrintUnit(armnn::Measurement::Unit unit);
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100120 void PrintType(armnn::JsonObjectType type);
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000121 void PrintGuid(arm::pipe::ProfilingGuid guid);
telsoa01c577f2c2018-08-31 09:22:23 +0100122 void PrintMeasurementsList(const std::vector<double>& measurementsVector);
123
124public:
Keith Davis554fa092021-07-20 11:25:22 +0100125 JsonPrinter(std::ostream& outputStream)
126 : JsonUtils(outputStream), m_OutputStream(outputStream)
telsoa01c577f2c2018-08-31 09:22:23 +0100127 {}
128
129private:
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100130 std::string MakeKey(const std::string& label, size_t id);
telsoa01c577f2c2018-08-31 09:22:23 +0100131
Keith Davis554fa092021-07-20 11:25:22 +0100132 std::ostream& m_OutputStream;
telsoa01c577f2c2018-08-31 09:22:23 +0100133};
134
135} // namespace armnn