xref: /aosp_15_r20/test/dittosuite/include/ditto/read_write_file.h (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
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 <sys/types.h>
18 
19 #include <cstdint>
20 #include <memory>
21 #include <random>
22 #include <vector>
23 
24 #include <ditto/instruction.h>
25 
26 namespace dittosuite {
27 
28 class ReadWriteFile : public Instruction {
29  public:
30   explicit ReadWriteFile(const std::string& name, const Params& params, int64_t size,
31                          int64_t block_size, int64_t starting_offset, Order access_order,
32                          uint32_t seed, Reseeding reseeding, int input_fd_key);
33   std::unique_ptr<Result> CollectResults(const std::string& prefix) override;
34 
35  protected:
36   virtual void SetUpSingle() override;
37   virtual void RunSingle() override;
38   void TearDownSingle(bool is_last) override;
39 
40   BandwidthSampler bandwidth_sampler_;
41   int64_t size_;
42   int64_t block_size_;
43   int64_t starting_offset_;
44   Order access_order_;
45   std::mt19937_64 gen_;
46   uint64_t seed_;
47   Reseeding reseeding_;
48   int input_fd_key_;
49   bool update_size_;
50   bool update_block_size_;
51 
52   struct Unit {
53     int64_t count;
54     int64_t offset;
55   };
56 
57   std::vector<Unit> units_;
58   std::unique_ptr<char[]> buffer_;
59 
60  private:
61   void SetUp() override;
62 };
63 
64 class WriteFile : public ReadWriteFile {
65  public:
66   inline static const std::string kName = "write_file";
67 
68   explicit WriteFile(const Params& params, int64_t size, int64_t block_size,
69                      int64_t starting_offset, Order access_order, uint32_t seed,
70                      Reseeding reseeding, bool fsync, int input_fd_key);
71 
72  private:
73   void RunSingle() override;
74 
75   bool fsync_;
76 };
77 
78 class ReadFile : public ReadWriteFile {
79  public:
80   inline static const std::string kName = "read_file";
81 
82   explicit ReadFile(const Params& params, int64_t size, int64_t block_size, int64_t starting_offset,
83                     Order access_order, uint32_t seed, Reseeding reseeding, int fadvise,
84                     int input_fd_key);
85 
86  private:
87   void SetUpSingle() override;
88   void RunSingle() override;
89 
90   int fadvise_;
91 };
92 
93 }  // namespace dittosuite
94