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