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