• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/instruction.h>
16 
17 #include <ditto/shared_variables.h>
18 
19 namespace dittosuite {
20 
Instruction(SyscallInterface & syscall,const std::string & name,int repeat)21 Instruction::Instruction(SyscallInterface& syscall, const std::string& name, int repeat)
22     : syscall_(syscall), name_(name), repeat_(repeat) {}
23 
SetUp()24 void Instruction::SetUp() {}
25 
Run()26 void Instruction::Run() {
27   for (int i = 0; i < repeat_; i++) {
28     SetUpSingle();
29     RunSingle();
30     TearDownSingle();
31   }
32 }
33 
RunSynchronized(pthread_barrier_t * barrier)34 void Instruction::RunSynchronized(pthread_barrier_t* barrier) {
35   pthread_barrier_wait(barrier);
36   Instruction::Run();
37 }
38 
SpawnThread(pthread_barrier_t * barrier)39 std::thread Instruction::SpawnThread(pthread_barrier_t* barrier) {
40   return std::thread([=] { RunSynchronized(barrier); });
41 }
42 
TearDown()43 void Instruction::TearDown() {}
44 
SetUpSingle()45 void Instruction::SetUpSingle() {
46   time_sampler_.MeasureStart();
47 }
48 
TearDownSingle()49 void Instruction::TearDownSingle() {
50   time_sampler_.MeasureEnd();
51 }
52 
CollectResults(const std::string & prefix)53 std::unique_ptr<Result> Instruction::CollectResults(const std::string& prefix) {
54   auto result = std::make_unique<Result>(prefix + name_, repeat_);
55   result->AddMeasurement("duration", TimespecToDoubleNanos(time_sampler_.GetSamples()));
56   return result;
57 }
58 
SetAbsolutePathKey(int absolute_path_key)59 void Instruction::SetAbsolutePathKey(int absolute_path_key) {
60   absolute_path_key_ = absolute_path_key;
61 }
62 
GetAbsolutePath()63 std::string Instruction::GetAbsolutePath() {
64   return std::get<std::string>(SharedVariables::Get(absolute_path_key_));
65 }
66 
67 int Instruction::absolute_path_key_;
68 
69 }  // namespace dittosuite
70