86{
87 struct Stat
88 {
90 uint64_t count = 0;
91 uint64_t max = 0;
92 uint64_t min = std::numeric_limits<uint64_t>::max();
93
94 void accumulate(uint64_t val)
95 {
97 count++;
98 max = std::max(max, val);
99 min = std::min(min, val);
100 }
101 };
102
105
106 auto &exec_data =
root[
"Execution_Data"] = Json::Value{Json::objectValue};
107
108
109 {
110 std::unordered_map<std::string, Stat> mem_stats;
111 for (const auto &recorder : recorders)
112 {
113 for (const auto &evt : recorder->counter_events())
114 {
115 auto &mem_stat = mem_stats[evt.name];
116 uint64_t val = std::stoull(evt.values.at("value"));
117 mem_stat.accumulate(val);
118 }
119 }
120
121 auto &mem = exec_data["memory"] = Json::Value{Json::objectValue};
122 for (const auto &[key, val] : mem_stats)
123 {
124 mem[key]["Avg_Size"] = val.sum / val.count;
125 mem[key]["Max_Size"] = val.max;
126 mem[key]["Min_Size"] = val.min;
127 mem[key]["Runtime"] = "NA";
128 }
129 }
130
131
132 {
133
134
135
136
137
138 std::unordered_map<std::string, std::unordered_map<std::string, Stat>> stats;
139 std::unordered_map<std::string, std::unordered_map<std::string, uint64_t>> begin_timestamps;
140 for (const auto &recorder : recorders)
141 {
142 for (const auto &evt : recorder->duration_events())
143 {
144 std::string evt_name = getLabel(*evt);
145 std::string evt_tid = getBackend(*evt);
146
147 auto &stat = stats[evt_tid][evt_name];
148 auto &begin_ts = begin_timestamps[evt_tid][evt_name];
149 uint64_t timestamp = std::stoull(evt->ts);
150 if (evt->ph == "B")
151 {
152 if (begin_ts != 0)
153 throw std::runtime_error{"Invalid Data"};
154 begin_ts = timestamp;
155 }
156 else if (evt->ph == "E")
157 {
158 if (begin_ts == 0 || timestamp < begin_ts)
159 throw std::runtime_error{"Invalid Data"};
160 stat.accumulate(timestamp - begin_ts);
161 begin_ts = 0;
162 }
163 else
164 throw std::runtime_error{"Invalid Data - invalid value for \"ph\" : \"" + evt->ph + "\""};
165 }
166 }
167
168 for (const auto &kv : begin_timestamps)
169 for (const auto &kv2 : kv.second)
170 if (kv2.second != 0)
171 throw
std::runtime_error{
"Invalid Data - B and E pair does not match."};
172
173 for (const auto &[tid, stat_map] : stats)
174 {
175 auto &json_tid = exec_data[tid] = Json::Value{Json::objectValue};
176 for (const auto &[name, val] : stat_map)
177 {
178 json_tid[
name][
"Avg_Time"] = val.sum / val.count;
179 json_tid[
name][
"Max_Time"] = val.max;
180 json_tid[
name][
"Min_Time"] = val.min;
181 json_tid[
name][
"Runtime"] = tid;
182 }
183 }
184 }
185
187}
#define SNPE_JSON_SCHEMA_VERSION
Version of SNPE format In version 1.
Op * root(Op *)
Return the root Op from a given Op node.