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/multithreading.h>
16*6fa2df46SAndroid Build Coastguard Worker
17*6fa2df46SAndroid Build Coastguard Worker namespace dittosuite {
18*6fa2df46SAndroid Build Coastguard Worker
Multithreading(const Instruction::Params & params,std::vector<std::unique_ptr<Instruction>> instructions,std::vector<MultithreadingParams> thread_params)19*6fa2df46SAndroid Build Coastguard Worker Multithreading::Multithreading(const Instruction::Params& params,
20*6fa2df46SAndroid Build Coastguard Worker std::vector<std::unique_ptr<Instruction>> instructions,
21*6fa2df46SAndroid Build Coastguard Worker std::vector<MultithreadingParams> thread_params)
22*6fa2df46SAndroid Build Coastguard Worker : Instruction(kName, params),
23*6fa2df46SAndroid Build Coastguard Worker instructions_(std::move(instructions)),
24*6fa2df46SAndroid Build Coastguard Worker thread_params_(std::move(thread_params)) {}
25*6fa2df46SAndroid Build Coastguard Worker
SetUpSingle()26*6fa2df46SAndroid Build Coastguard Worker void Multithreading::SetUpSingle() {
27*6fa2df46SAndroid Build Coastguard Worker for (const auto& instruction : instructions_) {
28*6fa2df46SAndroid Build Coastguard Worker instruction->SetUp();
29*6fa2df46SAndroid Build Coastguard Worker }
30*6fa2df46SAndroid Build Coastguard Worker threads_.clear();
31*6fa2df46SAndroid Build Coastguard Worker
32*6fa2df46SAndroid Build Coastguard Worker Instruction::SetUpSingle();
33*6fa2df46SAndroid Build Coastguard Worker }
34*6fa2df46SAndroid Build Coastguard Worker
RunSingle()35*6fa2df46SAndroid Build Coastguard Worker void Multithreading::RunSingle() {
36*6fa2df46SAndroid Build Coastguard Worker pthread_barrier_init(&barrier_, NULL, instructions_.size());
37*6fa2df46SAndroid Build Coastguard Worker for (size_t i = 0; i < instructions_.size(); ++i) {
38*6fa2df46SAndroid Build Coastguard Worker threads_.push_back(instructions_[i]->SpawnThread(&barrier_, thread_params_[i]));
39*6fa2df46SAndroid Build Coastguard Worker }
40*6fa2df46SAndroid Build Coastguard Worker }
41*6fa2df46SAndroid Build Coastguard Worker
TearDownSingle(bool is_last)42*6fa2df46SAndroid Build Coastguard Worker void Multithreading::TearDownSingle(bool is_last) {
43*6fa2df46SAndroid Build Coastguard Worker for (auto& thread : threads_) {
44*6fa2df46SAndroid Build Coastguard Worker thread.join();
45*6fa2df46SAndroid Build Coastguard Worker }
46*6fa2df46SAndroid Build Coastguard Worker
47*6fa2df46SAndroid Build Coastguard Worker Instruction::TearDownSingle(is_last);
48*6fa2df46SAndroid Build Coastguard Worker
49*6fa2df46SAndroid Build Coastguard Worker pthread_barrier_destroy(&barrier_);
50*6fa2df46SAndroid Build Coastguard Worker for (const auto& instruction : instructions_) {
51*6fa2df46SAndroid Build Coastguard Worker instruction->TearDown();
52*6fa2df46SAndroid Build Coastguard Worker }
53*6fa2df46SAndroid Build Coastguard Worker }
54*6fa2df46SAndroid Build Coastguard Worker
CollectResults(const std::string & prefix)55*6fa2df46SAndroid Build Coastguard Worker std::unique_ptr<Result> Multithreading::CollectResults(const std::string& prefix) {
56*6fa2df46SAndroid Build Coastguard Worker auto result = std::make_unique<Result>(prefix + name_, repeat_);
57*6fa2df46SAndroid Build Coastguard Worker result->AddMeasurement("duration", TimespecToDoubleNanos(time_sampler_.GetSamples()));
58*6fa2df46SAndroid Build Coastguard Worker for (std::size_t i = 0; i < instructions_.size(); ++i) {
59*6fa2df46SAndroid Build Coastguard Worker std::string child_name = thread_params_[i].name_;
60*6fa2df46SAndroid Build Coastguard Worker result->AddSubResult(instructions_[i]->CollectResults(child_name + "/"));
61*6fa2df46SAndroid Build Coastguard Worker }
62*6fa2df46SAndroid Build Coastguard Worker return result;
63*6fa2df46SAndroid Build Coastguard Worker }
64*6fa2df46SAndroid Build Coastguard Worker
65*6fa2df46SAndroid Build Coastguard Worker } // namespace dittosuite
66