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 #pragma once
16 
17 #include <pthread.h>
18 
19 #include <memory>
20 #include <string>
21 #include <thread>
22 
23 #include <ditto/multithreading_utils.h>
24 #include <ditto/result.h>
25 #include <ditto/sampler.h>
26 #include <ditto/syscall.h>
27 #include <ditto/tracer.h>
28 
29 namespace dittosuite {
30 
31 enum class Order { kSequential, kRandom };
32 enum class Reseeding { kOnce, kEachRoundOfCycles, kEachCycle };
33 enum class FreePolicy { kKeep, kFreeLastPeriod, kFreeEveryPeriod };
34 
35 class Instruction {
36  public:
37   struct Params {
38     Params(SyscallInterface& syscall, int repeat = 1, uint64_t period_us = 0,
39            uint64_t offset_us = 0)
syscall_Params40         : syscall_(syscall), repeat_(repeat), period_us_(period_us), offset_us_(offset_us) {}
41     SyscallInterface& syscall_;
42     int repeat_;
43     uint64_t period_us_;
44     uint64_t offset_us_;
45   };
46 
47   explicit Instruction(const std::string& name, const Params& params);
48   virtual ~Instruction() = default;
49 
50   virtual void SetUp();
51   void Run();
52   void RunSynchronized(pthread_barrier_t* barrier, const MultithreadingParams& params);
53   std::thread SpawnThread(pthread_barrier_t* barrier, const MultithreadingParams& params);
54   virtual void TearDown();
55 
56   virtual std::unique_ptr<Result> CollectResults(const std::string& prefix);
57 
58   static void SetAbsolutePathKey(int absolute_path_key);
59   static void SetArgv(char** argv);
60   static void SetArgc(int argc);
61 
62  protected:
63   virtual void SetUpSingle();
64   virtual void RunSingle() = 0;
65   /* This function is executed after every RunSingle(). In some cases, for
66    * example in the implementation of a producer-consumer, the consumer should
67    * know at what time it should stop with its execution, and this can be
68    * handled by the producer to send a special message at the last
69    * TearDownSingle. The last iteration of TearDownSingle has the `is_last`
70    * value set to true, false otherwise. */
71   virtual void TearDownSingle(bool is_last);
72 
73   std::string GetAbsolutePath();
74 
75   static int absolute_path_key_;
76   static char **argv_;
77   static int argc_;
78   std::string name_;
79   SyscallInterface& syscall_;
80   int repeat_;
81   uint64_t period_us_;
82   uint64_t offset_us_;
83   TimeSampler time_sampler_;
84   Tracer tracer_;
85 
86  private:
87   timespec next_wakeup_;
88 };
89 
90 }  // namespace dittosuite
91