1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "JsonPrinter.hpp"
7
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11
12 namespace armnn
13 {
14
PrintJsonChildObject(const JsonChildObject & object,size_t & id)15 void JsonPrinter::PrintJsonChildObject(const JsonChildObject& object, size_t& id)
16 {
17 if (object.GetType() == JsonObjectType::Event)
18 {
19 // Increase the Id for new events. This ensures a new event has a unique ID and any measurements belonging
20 // to the event have the same id. This id is appended to the name during the call to PrintLabel() below.
21 id++;
22 }
23
24 PrintLabel(object.m_Label, id);
25 PrintType(object.m_Type);
26
27 if (!object.m_Measurements.empty() || !object.m_Children.empty())
28 {
29 PrintSeparator();
30 PrintNewLine();
31 }
32
33 if (object.GetType() == JsonObjectType::Measurement)
34 {
35 PrintMeasurementsList(object.m_Measurements);
36 PrintSeparator();
37 PrintNewLine();
38 PrintUnit(object.m_Unit);
39 }
40 if (!object.m_Children.empty())
41 {
42 for (unsigned int childIndex = 0; childIndex < object.m_Children.size(); ++childIndex)
43 {
44 PrintJsonChildObject(object.m_Children[childIndex], id);
45 // Only print separator and new line if current child is not the last element.
46 if (&object.m_Children[childIndex] != &object.m_Children.back())
47 {
48 PrintSeparator();
49 PrintNewLine();
50 }
51 }
52 }
53 PrintNewLine();
54 PrintFooter();
55 }
56
PrintHeader()57 void JsonPrinter::PrintHeader()
58 {
59 m_OutputStream << "{" << std::endl;
60 IncrementNumberOfTabs();
61 }
62
PrintArmNNHeader()63 void JsonPrinter::PrintArmNNHeader()
64 {
65 PrintTabs();
66 m_OutputStream << R"("ArmNN": {)" << std::endl;
67 IncrementNumberOfTabs();
68 }
69
MakeKey(const std::string & label,size_t id)70 std::string JsonPrinter::MakeKey(const std::string& label, size_t id)
71 {
72 std::stringstream ss;
73 ss << label << std::string("_#") << id;
74 return ss.str();
75 }
76
PrintLabel(const std::string & label,size_t id)77 void JsonPrinter::PrintLabel(const std::string& label, size_t id)
78 {
79 PrintTabs();
80 m_OutputStream << R"(")" << MakeKey(label, id) << R"(": {)" << std::endl;
81 IncrementNumberOfTabs();
82 }
83
PrintUnit(armnn::Measurement::Unit unit)84 void JsonPrinter::PrintUnit(armnn::Measurement::Unit unit)
85 {
86 PrintTabs();
87 m_OutputStream << R"("unit": ")";
88 m_OutputStream << armnn::Measurement::ToString(unit);
89 m_OutputStream << R"(")";
90 }
91
PrintType(armnn::JsonObjectType type)92 void JsonPrinter::PrintType(armnn::JsonObjectType type)
93 {
94 auto ToString = [](armnn::JsonObjectType type)
95 {
96 switch (type)
97 {
98 case JsonObjectType::Measurement:
99 {
100 return "Measurement";
101 }
102 case JsonObjectType::Event:
103 {
104 return "Event";
105 }
106 default:
107 {
108 return "Unknown";
109 }
110 }
111 };
112 PrintTabs();
113 m_OutputStream << R"("type": ")";
114 m_OutputStream << ToString(type);
115 m_OutputStream << R"(")";
116 }
117
118
PrintMeasurementsList(const std::vector<double> & measurementsVector)119 void JsonPrinter::PrintMeasurementsList(const std::vector<double>& measurementsVector)
120 {
121 if (measurementsVector.empty())
122 {
123 return;
124 }
125
126 PrintTabs();
127 m_OutputStream << R"("raw": [)" << std::endl;
128 IncrementNumberOfTabs();
129 PrintTabs();
130 auto iter = measurementsVector.begin();
131 m_OutputStream << *iter;
132 for (iter = std::next(iter); iter != measurementsVector.end(); ++iter)
133 {
134 m_OutputStream << "," << std::endl;
135 PrintTabs();
136 m_OutputStream << *iter;
137 }
138 m_OutputStream << std::endl;
139 DecrementNumberOfTabs();
140 PrintTabs();
141 m_OutputStream << "]";
142 }
143
PrintTabs()144 void JsonPrinter::PrintTabs()
145 {
146 unsigned int numTabs = m_NumTabs;
147 while (numTabs-- > 0)
148 {
149 m_OutputStream << "\t";
150 }
151 }
152
PrintSeparator()153 void JsonPrinter::PrintSeparator()
154 {
155 m_OutputStream << ",";
156 }
157
PrintNewLine()158 void JsonPrinter::PrintNewLine()
159 {
160 m_OutputStream << std::endl;
161 }
162
PrintFooter()163 void JsonPrinter::PrintFooter()
164 {
165 DecrementNumberOfTabs();
166 PrintTabs();
167 m_OutputStream << "}";
168 }
169
DecrementNumberOfTabs()170 void JsonPrinter::DecrementNumberOfTabs()
171 {
172 if (m_NumTabs == 0)
173 {
174 return;
175 }
176 --m_NumTabs;
177 }
178
IncrementNumberOfTabs()179 void JsonPrinter::IncrementNumberOfTabs()
180 {
181 ++m_NumTabs;
182 }
183
184 } // namespace armnn