Trace Parser: getting trace data from file 19/160119/11
authorAlexander Aksenov <a.aksenov@samsung.com>
Tue, 14 Nov 2017 11:46:38 +0000 (14:46 +0300)
committerAlexander Aksenov <a.aksenov@samsung.com>
Thu, 16 Nov 2017 17:09:08 +0000 (20:09 +0300)
- add getting device and protocol data about trace from session data file
or from default values;
- remove magic numbers from protocol parsing.

Change-Id: I1964f8db0287f5d2455e34fce2df436b0aea8f7e
Signed-off-by: Alexander Aksenov <a.aksenov@samsung.com>
src/cli/trace_parser/exe/CMakeLists.txt
src/cli/trace_parser/exe/main.cpp
src/cli/trace_parser/exe/session_data.cpp [new file with mode: 0644]
src/cli/trace_parser/exe/session_data.h [new file with mode: 0644]
src/cli/trace_parser/exe/streams.h
src/cli/trace_parser/include/parser.h
src/cli/trace_parser/lib/parser.cpp
src/cli/trace_parser/lib/protocol.h
src/cli/trace_parser/lib/protocol30.cpp
src/cli/trace_parser/lib/protocol30.h
src/cli/trace_parser/lib/protocol42.cpp

index 3397b80..474baa7 100644 (file)
@@ -7,6 +7,7 @@ find_package(Boost COMPONENTS program_options REQUIRED)
 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
index 5683c98..09dd6d0 100644 (file)
  */
 
 
-#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"
 
 
 
@@ -66,75 +63,27 @@ static void parse_loop(const Parser &parser, std::iostream &ios)
     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());
 
diff --git a/src/cli/trace_parser/exe/session_data.cpp b/src/cli/trace_parser/exe/session_data.cpp
new file mode 100644 (file)
index 0000000..f9aa829
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+ *  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);
+}
diff --git a/src/cli/trace_parser/exe/session_data.h b/src/cli/trace_parser/exe/session_data.h
new file mode 100644 (file)
index 0000000..f8347c1
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ *  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__ */
index 2460687..7a4377d 100644 (file)
@@ -31,6 +31,7 @@
 #define __STREAMS_H__
 
 
+#include <iostream>
 #include <sstream>
 #include <fstream>
 #include <string>
index a19d34e..8479f57 100644 (file)
@@ -60,12 +60,14 @@ struct ParserOptions
 {
     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)
     {}
 };
index 2410a4d..d27cbe0 100644 (file)
@@ -44,16 +44,17 @@ Parser::Parser(const ParserOptions &init_data)
     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%>, \
index 5a0bb44..83116ea 100644 (file)
 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()); }
@@ -56,6 +57,7 @@ public:
 protected:
     std::shared_ptr<DataWriter> writer_;
     const int cpu_num_;
+    const int devs_num_;
 };
 
 #endif /* __PROTOCOL_H__ */
index 27b5215..50438b1 100644 (file)
@@ -240,12 +240,12 @@ void Protocol30::parse_system(Buffer &buf) const
     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();
 }
index 3fe4ea4..302d231 100644 (file)
@@ -34,9 +34,6 @@
 
 class Buffer;
 
-enum {
-    __MAGIC__DEVICES_COUNT = 5,
-};
 
 enum Messages30 {
     MSG_PROCESS_INFO_v30              = 0x0001,
index df9009f..441fa25 100644 (file)
@@ -116,12 +116,12 @@ void Protocol42::parse_system(Buffer &buf) const
     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();
 }