1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <ditto/parser.h>
16 
17 #include <fcntl.h>
18 
19 #include <cstdlib>
20 #include <fstream>
21 
22 #include <ditto/embedded_benchmarks.h>
23 #include <ditto/instruction_factory.h>
24 #include <ditto/logger.h>
25 #include <ditto/shared_variables.h>
26 
27 #include <google/protobuf/text_format.h>
28 
29 namespace dittosuite {
30 
GetParser()31 Parser& Parser::GetParser() {
32   static Parser parser;
33   return parser;
34 }
35 
__Parse(std::string json_benchmark,const std::vector<std::string> & parameters)36 std::unique_ptr<dittosuiteproto::Benchmark> Parser::__Parse(
37     std::string json_benchmark, const std::vector<std::string>& parameters)
38 
39 {
40   std::unique_ptr<dittosuiteproto::Benchmark> benchmark =
41       std::make_unique<dittosuiteproto::Benchmark>();
42 
43   for (std::size_t i = 0; i < parameters.size(); i++) {
44     std::string to_replace("$PARAMETER_" + std::to_string(i + 1) + "$");
45     auto position = json_benchmark.find(to_replace);
46     if (position == std::string::npos) {
47       LOGW(to_replace + " does not exist in .ditto file");
48       continue;
49     }
50     json_benchmark.replace(position, to_replace.size(), parameters[i]);
51   }
52 
53   if (!google::protobuf::TextFormat::ParseFromString(json_benchmark, benchmark.get())) {
54     LOGF("Error while parsing .ditto file");
55   }
56 
57   std::list<int> thread_ids({InstructionFactory::GenerateThreadId()});
58   auto absolute_path_key = SharedVariables::GetKey(thread_ids, "absolute_path");
59   SharedVariables::Set(absolute_path_key, benchmark->global().absolute_path());
60   Instruction::SetAbsolutePathKey(absolute_path_key);
61 
62   if (benchmark->has_init()) {
63     init_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->init());
64   }
65   main_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->main());
66   if (benchmark->has_clean_up()) {
67     clean_up_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->clean_up());
68   }
69 
70   SharedVariables::ClearKeys();
71 
72   return benchmark;
73 }
74 
ParseEmbedded(const std::string & embedded_benchmark,const std::vector<std::string> & parameters)75 std::unique_ptr<dittosuiteproto::Benchmark> Parser::ParseEmbedded(
76     const std::string& embedded_benchmark, const std::vector<std::string>& parameters) {
77   auto json_benchmark_it = ditto_static_config.find(embedded_benchmark);
78   if (json_benchmark_it == ditto_static_config.end()) {
79     LOGF("The requested benchmark is invalid: " + embedded_benchmark);
80   }
81 
82   return __Parse(json_benchmark_it->second, parameters);
83 }
84 
ParseFile(const std::string & file_path,const std::vector<std::string> & parameters)85 std::unique_ptr<dittosuiteproto::Benchmark> Parser::ParseFile(
86     const std::string& file_path, const std::vector<std::string>& parameters) {
87   std::ifstream file(file_path);
88   if (!file.is_open()) {
89     LOGF("Provided .ditto file was not found: " + file_path);
90   }
91 
92   std::string json_benchmark((std::istreambuf_iterator<char>(file)),
93                              (std::istreambuf_iterator<char>()));
94 
95   return __Parse(json_benchmark, parameters);
96 }
97 
GetInit()98 std::unique_ptr<Instruction> Parser::GetInit() {
99   return std::move(init_);
100 }
101 
GetMain()102 std::unique_ptr<Instruction> Parser::GetMain() {
103   return std::move(main_);
104 }
105 
GetCleanUp()106 std::unique_ptr<Instruction> Parser::GetCleanUp() {
107   return std::move(clean_up_);
108 }
109 
110 }  // namespace dittosuite
111