summaryrefslogtreecommitdiffstats
path: root/src/common/io_exerciser/ObjectModel.cc
blob: 589f6434282b360db00ce899df4952101f62c9bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "ObjectModel.h"

#include <algorithm>
#include <execution>
#include <iterator>

using ObjectModel = ceph::io_exerciser::ObjectModel;

ObjectModel::ObjectModel(const std::string& oid, uint64_t block_size, int seed) :
  Model(oid, block_size), created(false)
{
  rng.seed(seed);
}

int ObjectModel::get_seed(uint64_t offset) const
{
  ceph_assert(offset < contents.size());
  return contents[offset];
}

std::vector<int> ObjectModel::get_seed_offsets(int seed) const
{
  std::vector<int> offsets;
  for (size_t i = 0; i < contents.size(); i++)
  {
    if (contents[i] == seed)
    {
      offsets.push_back(i);
    }
  }

  return offsets;
}

std::string ObjectModel::to_string(int mask) const
{
  if (!created) {
    return "Object does not exist";
  }
  std::string result = "{";
  for (uint64_t i = 0; i < contents.size(); i++) {
    if (i != 0) {
      result += ",";
    }
    result += std::to_string(contents[i] & mask);
  }
  result += "}";
  return result;
}

bool ObjectModel::readyForIoOp(IoOp& op)
{
  return true;
}

void ObjectModel::applyIoOp(IoOp& op)
{
  auto generate_random = [&rng = rng]() {
    return rng();
  };

  switch (op.op) {
  case OpType::BARRIER:
    reads.clear();
    writes.clear();
    break;

  case OpType::CREATE:
    ceph_assert(!created);
    ceph_assert(reads.empty());
    ceph_assert(writes.empty());
    created = true;
    contents.resize(op.length1);
    std::generate(std::execution::seq, contents.begin(), contents.end(),
                  generate_random);
    break;

  case OpType::REMOVE:
    ceph_assert(created);
    ceph_assert(reads.empty());
    ceph_assert(writes.empty());
    created = false;
    contents.resize(0);
    break;

  case OpType::READ3:
    ceph_assert(created);
    ceph_assert(op.offset3 + op.length3 <= contents.size());
    // Not allowed: read overlapping with parallel write
    ceph_assert(!writes.intersects(op.offset3, op.length3));
    reads.union_insert(op.offset3, op.length3);
    [[fallthrough]];

  case OpType::READ2:
    ceph_assert(created);
    ceph_assert(op.offset2 + op.length2 <= contents.size());
    // Not allowed: read overlapping with parallel write
    ceph_assert(!writes.intersects(op.offset2, op.length2));
    reads.union_insert(op.offset2, op.length2);
    [[fallthrough]];

  case OpType::READ:
    ceph_assert(created);
    ceph_assert(op.offset1 + op.length1 <= contents.size());
    // Not allowed: read overlapping with parallel write
    ceph_assert(!writes.intersects(op.offset1, op.length1));
    reads.union_insert(op.offset1, op.length1);
    num_io++;
    break;

  case OpType::WRITE3:
    ceph_assert(created);
    // Not allowed: write overlapping with parallel read or write
    ceph_assert(!reads.intersects(op.offset3, op.length3));
    ceph_assert(!writes.intersects(op.offset3, op.length3));
    writes.union_insert(op.offset3, op.length3);
    ceph_assert(op.offset3 + op.length3 <= contents.size());
    std::generate(std::execution::seq,
                  std::next(contents.begin(), op.offset3),
                  std::next(contents.begin(), op.offset3 + op.length3),
                  generate_random);
    [[fallthrough]];

  case OpType::WRITE2:
    ceph_assert(created);
    // Not allowed: write overlapping with parallel read or write
    ceph_assert(!reads.intersects(op.offset2, op.length2));
    ceph_assert(!writes.intersects(op.offset2, op.length2));
    writes.union_insert(op.offset2, op.length2);
    ceph_assert(op.offset2 + op.length2 <= contents.size());
    std::generate(std::execution::seq,
                  std::next(contents.begin(), op.offset2),
                  std::next(contents.begin(), op.offset2 + op.length2),
                  generate_random);
    [[fallthrough]];

  case OpType::WRITE:
    ceph_assert(created);
    // Not allowed: write overlapping with parallel read or write
    ceph_assert(!reads.intersects(op.offset1, op.length1));
    ceph_assert(!writes.intersects(op.offset1, op.length1));
    writes.union_insert(op.offset1, op.length1);
    ceph_assert(op.offset1 + op.length1 <= contents.size());
    std::generate(std::execution::seq,
                  std::next(contents.begin(), op.offset1),
                  std::next(contents.begin(), op.offset1 + op.length1),
                  generate_random);
    num_io++;
    break;
  default:
    break;
  }
}

void ObjectModel::encode(ceph::buffer::list& bl) const {
  ENCODE_START(1, 1, bl);
  encode(created, bl);
  if (created) {
    encode(contents, bl);
  }
  ENCODE_FINISH(bl);
}

void ObjectModel::decode(ceph::buffer::list::const_iterator& bl) {
  DECODE_START(1, bl);
  DECODE_OLDEST(1);
  decode(created, bl);
  if (created) {
    decode(contents, bl);
  } else {
    contents.resize(0);
  }
  DECODE_FINISH(bl);
}