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