xref: /aosp_15_r20/test/dittosuite/src/parser.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
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->global().has_mutex()) {
63     pthread_mutex_t mux_orig;
64 
65     auto mutex_key = SharedVariables::GetKey(thread_ids, benchmark->global().mutex().name());
66     SharedVariables::Set(mutex_key, mux_orig);
67     pthread_mutex_t* mux = std::get_if<pthread_mutex_t>(SharedVariables::GetPointer(mutex_key));
68     pthread_mutex_init(mux, nullptr);
69   }
70 
71   if (benchmark->has_init()) {
72     init_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->init());
73   }
74   main_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->main());
75   if (benchmark->has_clean_up()) {
76     clean_up_ = InstructionFactory::CreateFromProtoInstruction(thread_ids, benchmark->clean_up());
77   }
78 
79   SharedVariables::ClearKeys();
80 
81   return benchmark;
82 }
83 
ParseEmbedded(const std::string & embedded_benchmark,const std::vector<std::string> & parameters)84 std::unique_ptr<dittosuiteproto::Benchmark> Parser::ParseEmbedded(
85     const std::string& embedded_benchmark, const std::vector<std::string>& parameters) {
86   auto json_benchmark_it = ditto_static_config.find(embedded_benchmark);
87   if (json_benchmark_it == ditto_static_config.end()) {
88     LOGF("The requested benchmark is invalid: " + embedded_benchmark);
89   }
90 
91   return __Parse(json_benchmark_it->second, parameters);
92 }
93 
ParseFile(const std::string & file_path,const std::vector<std::string> & parameters)94 std::unique_ptr<dittosuiteproto::Benchmark> Parser::ParseFile(
95     const std::string& file_path, const std::vector<std::string>& parameters) {
96   std::ifstream file(file_path);
97   if (!file.is_open()) {
98     LOGF("Provided .ditto file was not found: " + file_path);
99   }
100 
101   std::string json_benchmark((std::istreambuf_iterator<char>(file)),
102                              (std::istreambuf_iterator<char>()));
103 
104   return __Parse(json_benchmark, parameters);
105 }
106 
GetInit()107 std::unique_ptr<Instruction> Parser::GetInit() {
108   return std::move(init_);
109 }
110 
GetMain()111 std::unique_ptr<Instruction> Parser::GetMain() {
112   return std::move(main_);
113 }
114 
GetCleanUp()115 std::unique_ptr<Instruction> Parser::GetCleanUp() {
116   return std::move(clean_up_);
117 }
118 
119 }  // namespace dittosuite
120