blob: 9e8ecb48e5c129f48f898fff22c9bb296442a290 [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),
34 m_Guid(armnn::EmptyOptional())
telsoa01c577f2c2018-08-31 09:22:23 +010035 {}
36 JsonChildObject(const JsonChildObject&) = default;
37
38 void AddMeasurement(const double measurement)
39 {
40 m_Measurements.push_back(measurement);
41 }
42
Keith Davis554fa092021-07-20 11:25:22 +010043 void SetAndParseDetails(std::string layerDetailsStr)
44 {
45 std::stringstream layerDetails(layerDetailsStr);
46 std::string stringLine;
47 while (std::getline(layerDetails, stringLine, '\n'))
48 {
49 m_LayerDetailsList.push_back(stringLine);
50 }
51 }
52
Keith Davis5a64f222021-08-04 10:35:20 +010053 void SetGuid(profiling::ProfilingGuid guid)
54 {
55 m_Guid = Optional<profiling::ProfilingGuid>(guid);
56 }
57
telsoa01c577f2c2018-08-31 09:22:23 +010058 void AddChild(const JsonChildObject& childObject)
59 {
60 m_Children.push_back(childObject);
61 }
62
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010063 JsonChildObject& GetChild(const unsigned int index)
telsoa01c577f2c2018-08-31 09:22:23 +010064 {
65 return m_Children[index];
66 }
67
68 void SetUnit(const Measurement::Unit unit)
69 {
70 m_Unit = unit;
71 }
72
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010073 size_t NumChildren() const
74 {
75 return m_Children.size();
76 }
77
78 void SetType(JsonObjectType type)
79 {
80 m_Type = type;
81 }
82
83 JsonObjectType GetType() const
84 {
85 return m_Type;
86 }
87
telsoa01c577f2c2018-08-31 09:22:23 +010088 ~JsonChildObject() = default;
89
90 std::string m_Label;
91 Measurement::Unit m_Unit;
Derek Lamberti6b4dfc22019-08-07 17:01:57 +010092 JsonObjectType m_Type;
Keith Davis5a64f222021-08-04 10:35:20 +010093 Optional<profiling::ProfilingGuid> m_Guid;
telsoa01c577f2c2018-08-31 09:22:23 +010094 std::vector<double> m_Measurements;
Keith Davis554fa092021-07-20 11:25:22 +010095 std::vector<std::string> m_LayerDetailsList;
telsoa01c577f2c2018-08-31 09:22:23 +010096 std::vector<JsonChildObject> m_Children;
97
98private:
99 JsonChildObject() = delete;
100};
101
Keith Davis554fa092021-07-20 11:25:22 +0100102class JsonPrinter : public JsonUtils
telsoa01c577f2c2018-08-31 09:22:23 +0100103{
104public:
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100105 void PrintJsonChildObject(const JsonChildObject& object, size_t& id);
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100106 void PrintLabel(const std::string& label, size_t id);
telsoa01c577f2c2018-08-31 09:22:23 +0100107 void PrintUnit(armnn::Measurement::Unit unit);
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100108 void PrintType(armnn::JsonObjectType type);
Keith Davis5a64f222021-08-04 10:35:20 +0100109 void PrintGuid(armnn::profiling::ProfilingGuid guid);
telsoa01c577f2c2018-08-31 09:22:23 +0100110 void PrintMeasurementsList(const std::vector<double>& measurementsVector);
111
112public:
Keith Davis554fa092021-07-20 11:25:22 +0100113 JsonPrinter(std::ostream& outputStream)
114 : JsonUtils(outputStream), m_OutputStream(outputStream)
telsoa01c577f2c2018-08-31 09:22:23 +0100115 {}
116
117private:
Derek Lamberti6b4dfc22019-08-07 17:01:57 +0100118 std::string MakeKey(const std::string& label, size_t id);
telsoa01c577f2c2018-08-31 09:22:23 +0100119
Keith Davis554fa092021-07-20 11:25:22 +0100120 std::ostream& m_OutputStream;
telsoa01c577f2c2018-08-31 09:22:23 +0100121};
122
123} // namespace armnn