set(CMAKE_CXX_FLAGS "-Wall -Werror -std=c++1z")
set(SRC
+ ${CMAKE_CURRENT_SOURCE_DIR}/session_data.cpp
${CMAKE_CURRENT_SOURCE_DIR}/writers.cpp
${CMAKE_CURRENT_SOURCE_DIR}/writer_text.cpp
${CMAKE_CURRENT_SOURCE_DIR}/writer_py.cpp
*/
-#include <boost/program_options.hpp>
#include <stdexcept>
#include <string>
-#include <iostream>
#include <vector>
#include <parser.h>
#include "writers.h"
#include "streams.h"
-
-namespace po = boost::program_options;
+#include "session_data.h"
parser.finish_file();
}
-static Parser get_parser(po::variables_map &vm, std::iostream &out)
+static Parser get_parser(const SessionData &sd, std::iostream &out)
{
- auto dw = get_data_writer(vm["type"].as<std::string>(), out);
- ParserOptions opt(vm["version"].as<std::string>(),
- vm["cpu_num"].as<int>(), dw);
+ auto dw = get_data_writer(sd.get_type(), out);
+ ParserOptions opt(sd.get_version(), sd.get_cpu_num(), sd.get_devs_num(), dw);
return Parser(opt);
}
-static po::variables_map parse_args(int argc, char **argv)
-{
- po::variables_map vm;
-
- try {
- po::options_description desc("Options");
- desc.add_options()
- ("help,h", "Print help message")
- ("input,i", po::value<std::string>(), "Input trace file")
- ("output,o", po::value<std::string>(), "Output file")
- ("cpu_num,c", po::value<int>()->required(), "Target CPUs number")
- ("version,v", po::value<std::string>()->required(),
- "Protocol version")
- ("type,t", po::value<std::string>()->required(),
- "Output type(t(ext), p(y), j(son), c(sv))")
- ;
-
- po::positional_options_description p;
- p.add("input", -1);
-
- try {
- po::store(po::command_line_parser(argc, argv).options(desc)
- .positional(p).run(), vm);
-
- if (vm.count("help")) {
- std::cout << "SWAP Trace Parser" << std::endl << desc
- << std::endl;
- exit(0);
- }
-
- po::notify(vm);
-
- } catch (po::error& e) {
- std::cerr << "ERROR: " << e.what() << std::endl;
- std::cerr << desc << std::endl;
- exit(EINVAL);
- }
-
- } catch (std::exception& e) {
- std::cerr << "Unhandled exception occured: " << e.what() << std::endl;
- exit(EINVAL);
- }
-
- return vm;
-}
-
int main(int argc, char **argv)
{
StreamIn str_in;
StreamOut str_out;
- po::variables_map vm = parse_args(argc, argv);
-
+ const SessionData sd(argc, argv);
- if (vm.count("input"))
- str_in.set_path(vm["input"].as<std::string>());
+ if (!sd.get_input().empty())
+ str_in.set_path(sd.get_input());
- if (vm.count("output"))
- str_out.set_path(vm["output"].as<std::string>());
+ if (!sd.get_output().empty())
+ str_out.set_path(sd.get_output());
- const Parser &parser = get_parser(vm, str_out.get_stream());
+ const Parser &parser = get_parser(sd, str_out.get_stream());
parse_loop(parser, str_in.get_stream());
--- /dev/null
+/*
+ * SWAP Trace Parser
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Alexander Aksenov <a.aksenov@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#include <boost/property_tree/json_parser.hpp>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/program_options.hpp>
+#include <stdexcept>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <map>
+#include "session_data.h"
+
+
+namespace po = boost::program_options;
+namespace pt = boost::property_tree;
+
+
+// Keys for neccessary data in JSON session file
+static const std::string DEVS_NUM_STR = "device_num";
+static const std::string CPU_NUM_STR = "num_of_cpu";
+static const std::string VER_STR = "version";
+
+
+po::variables_map SessionData::parse_cmdline(int argc, char **argv)
+{
+ po::variables_map vm;
+
+ try {
+ po::options_description desc("Options");
+ desc.add_options()
+ ("help,h", "Print help message")
+ ("input,i", po::value<std::string>(), "Input trace file")
+ ("session,s", po::value<std::string>(), "Trace's session data")
+ ("output,o", po::value<std::string>(), "Output file")
+ ("cpu_num,c", po::value<int>()->default_value(4), "Target CPUs number")
+ ("devs_num,e", po::value<int>()->default_value(5), "Target energy devices number")
+ ("version,v", po::value<std::string>()->default_value("4.2"),
+ "Protocol version")
+ ("type,t", po::value<std::string>()->required(),
+ "Output type(t(ext), p(y), j(son), c(sv))")
+ ;
+
+ po::positional_options_description p;
+ p.add("input", -1);
+
+ try {
+ po::store(po::command_line_parser(argc, argv).options(desc)
+ .positional(p).run(), vm);
+
+ if (vm.count("help")) {
+ std::cout << "SWAP Trace Parser" << std::endl << desc
+ << std::endl;
+ exit(0);
+ }
+
+ po::notify(vm);
+
+ } catch (po::error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ std::cerr << desc << std::endl;
+ exit(EINVAL);
+ }
+
+ } catch (std::exception& e) {
+ std::cerr << "Unhandled exception occured: " << e.what() << std::endl;
+ exit(EINVAL);
+ }
+
+ return vm;
+}
+
+std::map<std::string, std::string> parse_file(const std::string &file_path)
+{
+ std::map<std::string, std::string> data;
+
+ try {
+ std::stringstream ss;
+ std::ifstream file(file_path);
+
+ if (file) {
+ ss << file.rdbuf();
+ file.close();
+ } else {
+ throw std::runtime_error("Cannot open file <" + file_path + ">");
+ }
+
+ pt::ptree pt;
+ pt::read_json(ss, pt);
+ pt::ptree::iterator it;
+
+ for (it = pt.begin(); it != pt.end(); ++it) {
+ std::string key = it->first;
+ std::string val = it->second.get_value<std::string>();
+ data.insert(std::make_pair(key, val));
+ }
+ } catch (std::exception const &e) {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return data;
+}
+
+void SessionData::fill_by_file(const po::variables_map &vm)
+{
+ const std::string data_path = vm["session"].as<std::string>();
+ const std::map<std::string, std::string> data = parse_file(data_path);
+
+ if (data.count(VER_STR))
+ ver_ = data.at(VER_STR);
+ else
+ ver_ = vm["version"].as<std::string>();
+
+ if (data.count(CPU_NUM_STR))
+ cpu_num_ = std::stoi(data.at(CPU_NUM_STR));
+ else
+ cpu_num_ = vm["cpu_num"].as<int>();
+
+ if (data.count(DEVS_NUM_STR))
+ devs_num_ = std::stoi(data.at(DEVS_NUM_STR));
+ else
+ devs_num_ = vm["devs_num"].as<int>();
+}
+
+void SessionData::fill_by_cmdline(const po::variables_map &vm)
+{
+ ver_ = vm["version"].as<std::string>();
+ cpu_num_ = vm["cpu_num"].as<int>();
+ devs_num_ = vm["devs_num"].as<int>();
+}
+
+void SessionData::fill_data(const po::variables_map &vm)
+{
+ type_ = vm["type"].as<std::string>();
+
+ if (vm.count("input"))
+ input_ = vm["input"].as<std::string>();
+
+ if (vm.count("output"))
+ output_ = vm["output"].as<std::string>();
+
+ if (vm.count("session"))
+ fill_by_file(vm);
+ else
+ fill_by_cmdline(vm);
+}
+
+SessionData::SessionData(int argc, char **argv)
+{
+ po::variables_map vm = parse_cmdline(argc, argv);
+
+ fill_data(vm);
+}
--- /dev/null
+/*
+ * SWAP Trace Parser
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ *
+ * Alexander Aksenov <a.aksenov@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - Samsung RnD Institute Russia
+ *
+ */
+
+
+#ifndef __SESSION_DATA_H__
+#define __SESSION_DATA_H__
+
+#include <boost/program_options.hpp>
+#include <string>
+
+namespace po = boost::program_options;
+
+class SessionData
+{
+public:
+ SessionData(int argc, char **argv);
+
+ std::string get_type() const { return type_; }
+ std::string get_version() const { return ver_; }
+ std::string get_input() const { return input_; }
+ std::string get_output() const { return output_; }
+ int get_cpu_num() const { return cpu_num_; }
+ int get_devs_num() const { return devs_num_; }
+
+private:
+ std::string type_;
+ std::string ver_;
+ std::string input_;
+ std::string output_;
+ int cpu_num_;
+ int devs_num_;
+
+ boost::program_options::variables_map parse_cmdline(int argc, char **argv);
+ void fill_data(const boost::program_options::variables_map &vm);
+ void fill_by_file(const boost::program_options::variables_map &vm);
+ void fill_by_cmdline(const boost::program_options::variables_map &vm);
+};
+
+#endif /* __SESSION_DATA_H__ */
#define __STREAMS_H__
+#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
{
const std::string version;
const int cpu_num;
+ const int devs_num;
std::shared_ptr<DataWriter> writer;
- ParserOptions(const std::string &version, const int cpu_num,
+ ParserOptions(const std::string &version, const int cpu_num, const int devs_num,
std::shared_ptr<DataWriter> writer) :
version(version),
cpu_num(cpu_num),
+ devs_num(devs_num),
writer(writer)
{}
};
const std::string v41 = "4.1";
const std::string v42 = "4.2";
const int cpu_num = init_data.cpu_num;
+ const int devs_num = init_data.devs_num;
std::shared_ptr<DataWriter> dw = init_data.writer;
if (!init_data.version.compare(v30))
- protocol_ = std::make_unique<Protocol30>(dw, cpu_num);
+ protocol_ = std::make_unique<Protocol30>(dw, cpu_num, devs_num);
else if (!init_data.version.compare(v40))
- protocol_ = std::make_unique<Protocol40>(dw, cpu_num);
+ protocol_ = std::make_unique<Protocol40>(dw, cpu_num, devs_num);
else if (!init_data.version.compare(v41))
- protocol_ = std::make_unique<Protocol41>(dw, cpu_num);
+ protocol_ = std::make_unique<Protocol41>(dw, cpu_num, devs_num);
else if (!init_data.version.compare(v42))
- protocol_ = std::make_unique<Protocol42>(dw, cpu_num);
+ protocol_ = std::make_unique<Protocol42>(dw, cpu_num, devs_num);
else
throw std::logic_error((boost::format("Wrong protocol version <%1%>. \
Suppotred versions are <%2%>, \
class Protocol
{
public:
- Protocol(std::shared_ptr<DataWriter> writer, const int cpu_num) :
+ Protocol(std::shared_ptr<DataWriter> writer, const int cpu_num, const int devs_num) :
writer_(writer),
- cpu_num_(cpu_num)
+ cpu_num_(cpu_num),
+ devs_num_(devs_num)
{}
virtual void start_file() const { writer_->start_file(get_version()); }
protected:
std::shared_ptr<DataWriter> writer_;
const int cpu_num_;
+ const int devs_num_;
};
#endif /* __PROTOCOL_H__ */
writer_->output_val("energy", buf.get_val<uint32_t>());
writer_->start_array("energy/device");
- for (int i = 0; i != __MAGIC__DEVICES_COUNT; ++i)
+ for (int i = 0; i != devs_num_; ++i)
writer_->output_val(std::to_string(i), buf.get_val<uint32_t>());
writer_->finish_array();
writer_->start_array("energy/device|app");
- for (int i = 0; i != __MAGIC__DEVICES_COUNT; ++i)
+ for (int i = 0; i != devs_num_; ++i)
writer_->output_val(std::to_string(i), buf.get_val<uint32_t>());
writer_->finish_array();
}
class Buffer;
-enum {
- __MAGIC__DEVICES_COUNT = 5,
-};
enum Messages30 {
MSG_PROCESS_INFO_v30 = 0x0001,
writer_->output_val("energy", buf.get_val<uint32_t>());
writer_->start_array("energy/device");
- for (int i = 0; i != __MAGIC__DEVICES_COUNT; ++i)
+ for (int i = 0; i != devs_num_; ++i)
writer_->output_val(std::to_string(i), buf.get_val<uint32_t>());
writer_->finish_array();
writer_->start_array("energy/device|app");
- for (int i = 0; i != __MAGIC__DEVICES_COUNT; ++i)
+ for (int i = 0; i != devs_num_; ++i)
writer_->output_val(std::to_string(i), buf.get_val<uint32_t>());
writer_->finish_array();
}