xref: /aosp_15_r20/test/dittosuite/src/instruction_set.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
1*6fa2df46SAndroid Build Coastguard Worker // Copyright (C) 2021 The Android Open Source Project
2*6fa2df46SAndroid Build Coastguard Worker //
3*6fa2df46SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*6fa2df46SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*6fa2df46SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*6fa2df46SAndroid Build Coastguard Worker //
7*6fa2df46SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
8*6fa2df46SAndroid Build Coastguard Worker //
9*6fa2df46SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*6fa2df46SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*6fa2df46SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*6fa2df46SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*6fa2df46SAndroid Build Coastguard Worker // limitations under the License.
14*6fa2df46SAndroid Build Coastguard Worker 
15*6fa2df46SAndroid Build Coastguard Worker #include <ditto/instruction_set.h>
16*6fa2df46SAndroid Build Coastguard Worker 
17*6fa2df46SAndroid Build Coastguard Worker #include <variant>
18*6fa2df46SAndroid Build Coastguard Worker 
19*6fa2df46SAndroid Build Coastguard Worker #include <ditto/logger.h>
20*6fa2df46SAndroid Build Coastguard Worker #include <ditto/shared_variables.h>
21*6fa2df46SAndroid Build Coastguard Worker 
22*6fa2df46SAndroid Build Coastguard Worker namespace dittosuite {
23*6fa2df46SAndroid Build Coastguard Worker 
24*6fa2df46SAndroid Build Coastguard Worker template <class... Ts>
25*6fa2df46SAndroid Build Coastguard Worker struct overloaded : Ts... {
26*6fa2df46SAndroid Build Coastguard Worker   using Ts::operator()...;
27*6fa2df46SAndroid Build Coastguard Worker };
28*6fa2df46SAndroid Build Coastguard Worker template <class... Ts>
29*6fa2df46SAndroid Build Coastguard Worker overloaded(Ts...) -> overloaded<Ts...>;
30*6fa2df46SAndroid Build Coastguard Worker 
InstructionSet(const Params & params,std::vector<std::unique_ptr<Instruction>> instructions,int list_key,int item_key,Order access_order,Reseeding reseeding,uint32_t seed)31*6fa2df46SAndroid Build Coastguard Worker InstructionSet::InstructionSet(const Params& params,
32*6fa2df46SAndroid Build Coastguard Worker                                std::vector<std::unique_ptr<Instruction>> instructions, int list_key,
33*6fa2df46SAndroid Build Coastguard Worker                                int item_key, Order access_order, Reseeding reseeding, uint32_t seed)
34*6fa2df46SAndroid Build Coastguard Worker     : Instruction(kName, params),
35*6fa2df46SAndroid Build Coastguard Worker       instructions_(std::move(instructions)),
36*6fa2df46SAndroid Build Coastguard Worker       list_key_(list_key),
37*6fa2df46SAndroid Build Coastguard Worker       item_key_(item_key),
38*6fa2df46SAndroid Build Coastguard Worker       access_order_(access_order),
39*6fa2df46SAndroid Build Coastguard Worker       reseeding_(reseeding),
40*6fa2df46SAndroid Build Coastguard Worker       seed_(seed),
41*6fa2df46SAndroid Build Coastguard Worker       gen_(seed) {}
42*6fa2df46SAndroid Build Coastguard Worker 
InstructionSet(const Params & params,std::vector<std::unique_ptr<Instruction>> instructions)43*6fa2df46SAndroid Build Coastguard Worker InstructionSet::InstructionSet(const Params& params,
44*6fa2df46SAndroid Build Coastguard Worker                                std::vector<std::unique_ptr<Instruction>> instructions)
45*6fa2df46SAndroid Build Coastguard Worker     : Instruction(kName, params),
46*6fa2df46SAndroid Build Coastguard Worker       instructions_(std::move(instructions)),
47*6fa2df46SAndroid Build Coastguard Worker       list_key_(-1),
48*6fa2df46SAndroid Build Coastguard Worker       item_key_(-1) {}
49*6fa2df46SAndroid Build Coastguard Worker 
SetUp()50*6fa2df46SAndroid Build Coastguard Worker void InstructionSet::SetUp() {
51*6fa2df46SAndroid Build Coastguard Worker   if (reseeding_ == Reseeding::kEachRoundOfCycles) {
52*6fa2df46SAndroid Build Coastguard Worker     gen_.seed(seed_);
53*6fa2df46SAndroid Build Coastguard Worker   }
54*6fa2df46SAndroid Build Coastguard Worker   Instruction::SetUp();
55*6fa2df46SAndroid Build Coastguard Worker }
56*6fa2df46SAndroid Build Coastguard Worker 
SetUpSingle()57*6fa2df46SAndroid Build Coastguard Worker void InstructionSet::SetUpSingle() {
58*6fa2df46SAndroid Build Coastguard Worker   if (reseeding_ == Reseeding::kEachCycle) {
59*6fa2df46SAndroid Build Coastguard Worker     gen_.seed(seed_);
60*6fa2df46SAndroid Build Coastguard Worker   }
61*6fa2df46SAndroid Build Coastguard Worker   Instruction::SetUpSingle();
62*6fa2df46SAndroid Build Coastguard Worker }
63*6fa2df46SAndroid Build Coastguard Worker 
RunSingle()64*6fa2df46SAndroid Build Coastguard Worker void InstructionSet::RunSingle() {
65*6fa2df46SAndroid Build Coastguard Worker   if (list_key_ != -1 && item_key_ != -1) {
66*6fa2df46SAndroid Build Coastguard Worker     std::visit(overloaded{[&](const std::vector<std::string>& list) {
67*6fa2df46SAndroid Build Coastguard Worker                             std::uniform_int_distribution<> uniform_distribution(0,
68*6fa2df46SAndroid Build Coastguard Worker                                                                                  list.size() - 1);
69*6fa2df46SAndroid Build Coastguard Worker                             for (std::size_t i = 0; i < list.size(); ++i) {
70*6fa2df46SAndroid Build Coastguard Worker                               switch (access_order_) {
71*6fa2df46SAndroid Build Coastguard Worker                                 case Order::kSequential: {
72*6fa2df46SAndroid Build Coastguard Worker                                   SharedVariables::Set(item_key_, list[i]);
73*6fa2df46SAndroid Build Coastguard Worker                                   break;
74*6fa2df46SAndroid Build Coastguard Worker                                 }
75*6fa2df46SAndroid Build Coastguard Worker                                 case Order::kRandom: {
76*6fa2df46SAndroid Build Coastguard Worker                                   SharedVariables::Set(item_key_, list[uniform_distribution(gen_)]);
77*6fa2df46SAndroid Build Coastguard Worker                                   break;
78*6fa2df46SAndroid Build Coastguard Worker                                 }
79*6fa2df46SAndroid Build Coastguard Worker                               }
80*6fa2df46SAndroid Build Coastguard Worker                               RunInstructions();
81*6fa2df46SAndroid Build Coastguard Worker                             }
82*6fa2df46SAndroid Build Coastguard Worker                           },
83*6fa2df46SAndroid Build Coastguard Worker                           [](int) {
84*6fa2df46SAndroid Build Coastguard Worker                             LOGE("Input for InstructionSet is not iterable.");
85*6fa2df46SAndroid Build Coastguard Worker                             exit(EXIT_FAILURE);
86*6fa2df46SAndroid Build Coastguard Worker                           },
87*6fa2df46SAndroid Build Coastguard Worker                           [](pthread_mutex_t) {
88*6fa2df46SAndroid Build Coastguard Worker                             LOGE("Input for InstructionSet is not iterable.");
89*6fa2df46SAndroid Build Coastguard Worker                             exit(EXIT_FAILURE);
90*6fa2df46SAndroid Build Coastguard Worker                           },
91*6fa2df46SAndroid Build Coastguard Worker                           [](const std::string&) {
92*6fa2df46SAndroid Build Coastguard Worker                             LOGE("Input for InstructionSet is not iterable.");
93*6fa2df46SAndroid Build Coastguard Worker                             exit(EXIT_FAILURE);
94*6fa2df46SAndroid Build Coastguard Worker                           }},
95*6fa2df46SAndroid Build Coastguard Worker                SharedVariables::Get(list_key_));
96*6fa2df46SAndroid Build Coastguard Worker   } else {
97*6fa2df46SAndroid Build Coastguard Worker     RunInstructions();
98*6fa2df46SAndroid Build Coastguard Worker   }
99*6fa2df46SAndroid Build Coastguard Worker }
100*6fa2df46SAndroid Build Coastguard Worker 
RunInstructions()101*6fa2df46SAndroid Build Coastguard Worker void InstructionSet::RunInstructions() {
102*6fa2df46SAndroid Build Coastguard Worker   for (const auto& instruction : instructions_) {
103*6fa2df46SAndroid Build Coastguard Worker     instruction->SetUp();
104*6fa2df46SAndroid Build Coastguard Worker     instruction->Run();
105*6fa2df46SAndroid Build Coastguard Worker     instruction->TearDown();
106*6fa2df46SAndroid Build Coastguard Worker   }
107*6fa2df46SAndroid Build Coastguard Worker }
108*6fa2df46SAndroid Build Coastguard Worker 
109*6fa2df46SAndroid Build Coastguard Worker // The duration for an instruction set is a sum of its child instruction durations.
CollectResults(const std::string & prefix)110*6fa2df46SAndroid Build Coastguard Worker std::unique_ptr<Result> InstructionSet::CollectResults(const std::string& prefix) {
111*6fa2df46SAndroid Build Coastguard Worker   auto result = std::make_unique<Result>(prefix + name_, repeat_);
112*6fa2df46SAndroid Build Coastguard Worker   std::vector<double> duration;
113*6fa2df46SAndroid Build Coastguard Worker   for (const auto& instruction : instructions_) {
114*6fa2df46SAndroid Build Coastguard Worker     auto sub_result = instruction->CollectResults("");
115*6fa2df46SAndroid Build Coastguard Worker     auto samples = sub_result->GetSamples("duration");
116*6fa2df46SAndroid Build Coastguard Worker     auto repeat = sub_result->GetRepeat();
117*6fa2df46SAndroid Build Coastguard Worker 
118*6fa2df46SAndroid Build Coastguard Worker     if (duration.empty()) {
119*6fa2df46SAndroid Build Coastguard Worker       duration.resize(samples.size() / repeat);
120*6fa2df46SAndroid Build Coastguard Worker     }
121*6fa2df46SAndroid Build Coastguard Worker 
122*6fa2df46SAndroid Build Coastguard Worker     for (std::size_t i = 0; i < samples.size() / repeat; ++i) {
123*6fa2df46SAndroid Build Coastguard Worker       for (int j = 0; j < repeat; ++j) {
124*6fa2df46SAndroid Build Coastguard Worker         duration[i] += samples[i * repeat + j];
125*6fa2df46SAndroid Build Coastguard Worker       }
126*6fa2df46SAndroid Build Coastguard Worker     }
127*6fa2df46SAndroid Build Coastguard Worker 
128*6fa2df46SAndroid Build Coastguard Worker     result->AddSubResult(std::move(sub_result));
129*6fa2df46SAndroid Build Coastguard Worker   }
130*6fa2df46SAndroid Build Coastguard Worker   result->AddMeasurement("duration", duration);
131*6fa2df46SAndroid Build Coastguard Worker   return result;
132*6fa2df46SAndroid Build Coastguard Worker }
133*6fa2df46SAndroid Build Coastguard Worker 
134*6fa2df46SAndroid Build Coastguard Worker }  // namespace dittosuite
135