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/logger.h>
18 #include <ditto/shared_variables.h>
19 #include <ditto/timespec_utils.h>
20 #include <ditto/tracer.h>
21 
22 namespace dittosuite {
23 
Instruction(const std::string & name,const Params & params)24 Instruction::Instruction(const std::string& name, const Params& params)
25     : name_(name),
26       syscall_(params.syscall_),
27       repeat_(params.repeat_),
28       period_us_(params.period_us_),
29       offset_us_(params.offset_us_),
30       next_wakeup_() {}
31 
SetUp()32 void Instruction::SetUp() {
33   if (period_us_ || offset_us_) {
34     clock_gettime(CLOCK_MONOTONIC, &next_wakeup_);
35     LOGD("Instruction::SetUp, next wakeup at: " + TimespecToString(next_wakeup_));
36     if (offset_us_) {
37       next_wakeup_ = next_wakeup_ + MicrosToTimespec(offset_us_);
38     }
39   }
40 }
41 
Run()42 void Instruction::Run() {
43   for (int i = 0; i < repeat_; i++) {
44     SetUpSingle();
45     RunSingle();
46     TearDownSingle(i == repeat_ - 1);
47   }
48 }
49 
RunSynchronized(pthread_barrier_t * barrier,const MultithreadingParams & params)50 void Instruction::RunSynchronized(pthread_barrier_t* barrier, const MultithreadingParams& params) {
51   int ret = pthread_setname_np(pthread_self(), params.name_.c_str());
52   if (ret) {
53     if (ret == ERANGE) {
54       LOGF("Name too long for thread, max 15 chars allowed: " + params.name_);
55     } else {
56       LOGF("Error setting thread name: " + std::to_string(ret));
57     }
58   }
59 
60   if (params.sched_attr_.IsSet()) {
61     params.sched_attr_.Set();
62   }
63   if (params.sched_affinity_.IsSet()) {
64     params.sched_affinity_.Set();
65   }
66 
67   pthread_barrier_wait(barrier);
68   Instruction::Run();
69 }
70 
SpawnThread(pthread_barrier_t * barrier,const MultithreadingParams & params)71 std::thread Instruction::SpawnThread(pthread_barrier_t* barrier,
72                                      const MultithreadingParams& params) {
73   return std::thread([=, this] { RunSynchronized(barrier, params); });
74 }
75 
TearDown()76 void Instruction::TearDown() {}
77 
SetUpSingle()78 void Instruction::SetUpSingle() {
79   if (period_us_ || offset_us_) {
80     if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_wakeup_, nullptr)) {
81       PLOGF("Offset clock interrupted");
82     }
83     next_wakeup_ = next_wakeup_ + MicrosToTimespec(period_us_);
84   }
85   tracer_.Start(name_);
86   time_sampler_.MeasureStart();
87 }
88 
TearDownSingle(bool)89 void Instruction::TearDownSingle(bool /*is_last*/) {
90   time_sampler_.MeasureEnd();
91   tracer_.End(name_);
92 }
93 
CollectResults(const std::string & prefix)94 std::unique_ptr<Result> Instruction::CollectResults(const std::string& prefix) {
95   auto result = std::make_unique<Result>(prefix + name_, repeat_);
96   result->AddMeasurement("duration", TimespecToDoubleNanos(time_sampler_.GetSamples()));
97   return result;
98 }
99 
SetAbsolutePathKey(int absolute_path_key)100 void Instruction::SetAbsolutePathKey(int absolute_path_key) {
101   absolute_path_key_ = absolute_path_key;
102 }
103 
SetArgv(char ** argv)104 void Instruction::SetArgv(char** argv) {
105   argv_ = argv;
106 }
107 
SetArgc(int argc)108 void Instruction::SetArgc(int argc) {
109   argc_ = argc;
110 }
111 
GetAbsolutePath()112 std::string Instruction::GetAbsolutePath() {
113   return std::get<std::string>(SharedVariables::Get(absolute_path_key_));
114 }
115 
116 int Instruction::absolute_path_key_;
117 char** Instruction::argv_;
118 int Instruction::argc_;
119 
120 }  // namespace dittosuite
121