2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "exec/JSONExecTime.h"
18 #include "backend/IConfig.h"
26 * @brief Helper function for reading string from stream
28 * @param str Output string
29 * @param stream File stream
31 void readString(std::string &str, std::ifstream &stream)
45 * @brief Helper function for reading bool from stream
47 * @param quant Output bool
48 * @param stream File stream
50 void readBool(bool &quant, std::ifstream &stream)
58 void printString(const std::string &str, std::ofstream &stream) { stream << "\"" << str << "\""; }
60 void printBool(bool quant, std::ofstream &stream) { stream << "\"" << quant << "\""; }
62 void JSON::readOperation(const std::string &backend, const std::string &operation, bool quant,
63 std::ifstream &stream)
70 int number_of_closed_braces = 0;
71 int number_of_commas = 0;
81 number_of_closed_braces++;
86 number_of_closed_braces--;
91 if (std::isdigit(buf))
93 int_buf.push_back(buf);
99 if (number_of_closed_braces == 1)
102 if ((buf == ']' && number_of_closed_braces == 0) ||
103 (buf == ',' && number_of_closed_braces == -1))
105 switch (number_of_commas % 2)
109 size = static_cast<uint32_t>(std::atoi(int_buf.c_str()));
114 time = static_cast<int64_t>(std::atol(int_buf.c_str()));
115 auto bf = _backends.find(backend);
116 if (bf != _backends.end())
118 _measurements[bf->second][operation][quant][size] = time;
119 } // we ignore the records for unsupported backends
128 void JSON::printOperation(const std::map<uint32_t, int64_t> &operation_info,
129 std::ofstream &stream) const
131 for (const auto &items : operation_info)
133 stream << "[" << items.first << ", " << items.second << "], ";
135 stream.seekp(-2, std::ofstream::end);
138 void JSON::uploadOperationsExecTime() const
140 std::ofstream stream(_measurement_file);
141 if (!stream.is_open())
143 throw std::runtime_error("Failed to save backend config file");
148 for (const auto &backend : _measurements)
150 printString(backend.first->config()->id(), stream);
152 for (const auto &operation : backend.second)
154 printString(operation.first, stream);
156 for (const auto &type : operation.second)
158 printBool(type.first, stream);
160 printOperation(type.second, stream);
163 stream.seekp(-2, std::ofstream::end);
166 stream.seekp(-2, std::ofstream::end);
169 stream.seekp(-2, std::ofstream::end);
175 void JSON::loadOperationsExecTime()
177 std::ifstream stream(_measurement_file);
178 if (stream.is_open())
181 std::string operation;
184 int number_of_open_braces = 0;
186 while (stream.good())
192 number_of_open_braces++;
195 number_of_open_braces--;
199 if (number_of_open_braces == 1)
201 // read backend string
202 readString(backend, stream);
204 if (number_of_open_braces == 2)
206 // read operation string
207 readString(operation, stream);
209 if (number_of_open_braces == 3)
211 // read operation string
212 readBool(quant, stream);
218 // reading and creating all info for operation
219 readOperation(backend, operation, quant, stream);