• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/instruction_factory.h>
23 #include <ditto/logger.h>
24 #include <ditto/shared_variables.h>
25 
26 #include <google/protobuf/text_format.h>
27 
28 #ifdef __ANDROID__
29 #include <benchmark.pb.h>
30 #else
31 #include "schema/benchmark.pb.h"
32 #endif
33 
34 namespace dittosuite {
35 
GetParser()36 Parser& Parser::GetParser() {
37   static Parser parser;
38   return parser;
39 }
40 
Parse(const std::string & file_path,const std::vector<std::string> & parameters)41 void Parser::Parse(const std::string& file_path, const std::vector<std::string>& parameters) {
42   std::unique_ptr<dittosuiteproto::Benchmark> benchmark =
43       std::make_unique<dittosuiteproto::Benchmark>();
44 
45   std::ifstream file(file_path);
46   if (!file.is_open()) {
47     LOGF("Provided .ditto file was not found: " + file_path);
48   }
49 
50   std::string file_contents((std::istreambuf_iterator<char>(file)),
51                             (std::istreambuf_iterator<char>()));
52 
53   for (std::size_t i = 0; i < parameters.size(); i++) {
54     std::string to_replace("$PARAMETER_" + std::to_string(i + 1) + "$");
55     auto position = file_contents.find(to_replace);
56     if (position == std::string::npos) {
57       LOGW(to_replace + " does not exist in .ditto file");
58       continue;
59     }
60     file_contents.replace(position, to_replace.size(), parameters[i]);
61   }
62 
63   if (!google::protobuf::TextFormat::ParseFromString(file_contents, benchmark.get())) {
64     LOGF("Error while parsing .ditto file");
65   }
66 
67   std::list<int> thread_ids({InstructionFactory::GenerateThreadId()});
68   auto absolute_path_key = SharedVariables::GetKey(thread_ids, "absolute_path");
69   SharedVariables::Set(absolute_path_key, benchmark->global().absolute_path());
70   Instruction::SetAbsolutePathKey(absolute_path_key);
71 
72   if (benchmark->has_init()) {
73     init_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->init());
74   }
75   main_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->main());
76   if (benchmark->has_clean_up()) {
77     clean_up_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->clean_up());
78   }
79 
80   SharedVariables::ClearKeys();
81 }
82 
GetInit()83 std::unique_ptr<Instruction> Parser::GetInit() {
84   return std::move(init_);
85 }
86 
GetMain()87 std::unique_ptr<Instruction> Parser::GetMain() {
88   return std::move(main_);
89 }
90 
GetCleanUp()91 std::unique_ptr<Instruction> Parser::GetCleanUp() {
92   return std::move(clean_up_);
93 }
94 
95 }  // namespace dittosuite
96