Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rbd_replay / ActionTypes.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #ifndef CEPH_RBD_REPLAY_ACTION_TYPES_H
5 #define CEPH_RBD_REPLAY_ACTION_TYPES_H
6
7 #include "include/int_types.h"
8 #include "include/buffer_fwd.h"
9 #include "include/encoding.h"
10 #include <iosfwd>
11 #include <list>
12 #include <string>
13 #include <vector>
14 #include <boost/variant/variant.hpp>
15
16 namespace ceph { class Formatter; }
17
18 namespace rbd_replay {
19 namespace action {
20
21 typedef uint64_t imagectx_id_t;
22 typedef uint64_t thread_id_t;
23
24 /// Even IDs are normal actions, odd IDs are completions.
25 typedef uint32_t action_id_t;
26
27 static const std::string BANNER("rbd-replay-trace");
28
29 /**
30  * Dependencies link actions to earlier actions or completions.
31  * If an action has a dependency \c d then it waits until \c d.time_delta
32  * nanoseconds after the action or completion with ID \c d.id has fired.
33  */
34 struct Dependency {
35   /// ID of the action or completion to wait for.
36   action_id_t id;
37
38   /// Nanoseconds of delay to wait until after the action or completion fires.
39   uint64_t time_delta;
40
41   /**
42    * @param id ID of the action or completion to wait for.
43    * @param time_delta Nanoseconds of delay to wait after the action or
44    *                   completion fires.
45    */
46   Dependency() : id(0), time_delta(0) {
47   }
48   Dependency(action_id_t id, uint64_t time_delta)
49     : id(id), time_delta(time_delta) {
50   }
51
52   void encode(bufferlist &bl) const;
53   void decode(bufferlist::iterator &it);
54   void decode(__u8 version, bufferlist::iterator &it);
55   void dump(Formatter *f) const;
56
57   static void generate_test_instances(std::list<Dependency *> &o);
58 };
59
60 WRITE_CLASS_ENCODER(Dependency);
61
62 typedef std::vector<Dependency> Dependencies;
63
64 enum ActionType {
65   ACTION_TYPE_START_THREAD    = 0,
66   ACTION_TYPE_STOP_THREAD     = 1,
67   ACTION_TYPE_READ            = 2,
68   ACTION_TYPE_WRITE           = 3,
69   ACTION_TYPE_AIO_READ        = 4,
70   ACTION_TYPE_AIO_WRITE       = 5,
71   ACTION_TYPE_OPEN_IMAGE      = 6,
72   ACTION_TYPE_CLOSE_IMAGE     = 7,
73   ACTION_TYPE_AIO_OPEN_IMAGE  = 8,
74   ACTION_TYPE_AIO_CLOSE_IMAGE = 9,
75   ACTION_TYPE_DISCARD         = 10,
76   ACTION_TYPE_AIO_DISCARD     = 11
77 };
78
79 struct ActionBase {
80   action_id_t id;
81   thread_id_t thread_id;
82   Dependencies dependencies;
83
84   ActionBase() : id(0), thread_id(0) {
85   }
86   ActionBase(action_id_t id, thread_id_t thread_id,
87              const Dependencies &dependencies)
88     : id(id), thread_id(thread_id), dependencies(dependencies) {
89   }
90
91   void encode(bufferlist &bl) const;
92   void decode(__u8 version, bufferlist::iterator &it);
93   void dump(Formatter *f) const;
94 };
95
96 struct StartThreadAction : public ActionBase {
97   static const ActionType ACTION_TYPE = ACTION_TYPE_START_THREAD;
98
99   StartThreadAction() {
100   }
101   StartThreadAction(action_id_t id, thread_id_t thread_id,
102                     const Dependencies &dependencies)
103     : ActionBase(id, thread_id, dependencies) {
104   }
105 };
106
107 struct StopThreadAction : public ActionBase {
108   static const ActionType ACTION_TYPE = ACTION_TYPE_STOP_THREAD;
109
110   StopThreadAction() {
111   }
112   StopThreadAction(action_id_t id, thread_id_t thread_id,
113                    const Dependencies &dependencies)
114     : ActionBase(id, thread_id, dependencies) {
115   }
116 };
117
118 struct ImageActionBase : public ActionBase {
119   imagectx_id_t imagectx_id;
120
121   ImageActionBase() : imagectx_id(0) {
122   }
123   ImageActionBase(action_id_t id, thread_id_t thread_id,
124                   const Dependencies &dependencies, imagectx_id_t imagectx_id)
125     : ActionBase(id, thread_id, dependencies), imagectx_id(imagectx_id) {
126   }
127
128   void encode(bufferlist &bl) const;
129   void decode(__u8 version, bufferlist::iterator &it);
130   void dump(Formatter *f) const;
131 };
132
133 struct IoActionBase : public ImageActionBase {
134   uint64_t offset;
135   uint64_t length;
136
137   IoActionBase() : offset(0), length(0) {
138   }
139   IoActionBase(action_id_t id, thread_id_t thread_id,
140                const Dependencies &dependencies, imagectx_id_t imagectx_id,
141                uint64_t offset, uint64_t length)
142     : ImageActionBase(id, thread_id, dependencies, imagectx_id),
143       offset(offset), length(length) {
144   }
145
146   void encode(bufferlist &bl) const;
147   void decode(__u8 version, bufferlist::iterator &it);
148   void dump(Formatter *f) const;
149 };
150
151 struct ReadAction : public IoActionBase {
152   static const ActionType ACTION_TYPE = ACTION_TYPE_READ;
153
154   ReadAction() {
155   }
156   ReadAction(action_id_t id, thread_id_t thread_id,
157              const Dependencies &dependencies, imagectx_id_t imagectx_id,
158              uint64_t offset, uint64_t length)
159     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
160   }
161 };
162
163 struct WriteAction : public IoActionBase {
164   static const ActionType ACTION_TYPE = ACTION_TYPE_WRITE;
165
166   WriteAction() {
167   }
168   WriteAction(action_id_t id, thread_id_t thread_id,
169               const Dependencies &dependencies, imagectx_id_t imagectx_id,
170               uint64_t offset, uint64_t length)
171     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
172   }
173 };
174
175 struct DiscardAction : public IoActionBase {
176   static const ActionType ACTION_TYPE = ACTION_TYPE_DISCARD;
177
178   DiscardAction() {
179   }
180   DiscardAction(action_id_t id, thread_id_t thread_id,
181                 const Dependencies &dependencies, imagectx_id_t imagectx_id,
182                 uint64_t offset, uint64_t length)
183     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
184   }
185 };
186
187 struct AioReadAction : public IoActionBase {
188   static const ActionType ACTION_TYPE = ACTION_TYPE_AIO_READ;
189
190   AioReadAction() {
191   }
192   AioReadAction(action_id_t id, thread_id_t thread_id,
193                 const Dependencies &dependencies, imagectx_id_t imagectx_id,
194                 uint64_t offset, uint64_t length)
195     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
196   }
197 };
198
199 struct AioWriteAction : public IoActionBase {
200   static const ActionType ACTION_TYPE = ACTION_TYPE_AIO_WRITE;
201
202   AioWriteAction() {
203   }
204   AioWriteAction(action_id_t id, thread_id_t thread_id,
205                  const Dependencies &dependencies, imagectx_id_t imagectx_id,
206                  uint64_t offset, uint64_t length)
207     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
208   }
209 };
210
211 struct AioDiscardAction : public IoActionBase {
212   static const ActionType ACTION_TYPE = ACTION_TYPE_AIO_DISCARD;
213
214   AioDiscardAction() {
215   }
216   AioDiscardAction(action_id_t id, thread_id_t thread_id,
217                    const Dependencies &dependencies, imagectx_id_t imagectx_id,
218                    uint64_t offset, uint64_t length)
219     : IoActionBase(id, thread_id, dependencies, imagectx_id, offset, length) {
220   }
221 };
222
223 struct OpenImageAction : public ImageActionBase {
224   static const ActionType ACTION_TYPE = ACTION_TYPE_OPEN_IMAGE;
225
226   std::string name;
227   std::string snap_name;
228   bool read_only;
229
230   OpenImageAction() : read_only(false) {
231   }
232   OpenImageAction(action_id_t id, thread_id_t thread_id,
233                   const Dependencies &dependencies, imagectx_id_t imagectx_id,
234                   const std::string &name, const std::string &snap_name,
235                   bool read_only)
236     : ImageActionBase(id, thread_id, dependencies, imagectx_id),
237       name(name), snap_name(snap_name), read_only(read_only) {
238   }
239
240   void encode(bufferlist &bl) const;
241   void decode(__u8 version, bufferlist::iterator &it);
242   void dump(Formatter *f) const;
243 };
244
245 struct CloseImageAction : public ImageActionBase {
246   static const ActionType ACTION_TYPE = ACTION_TYPE_CLOSE_IMAGE;
247
248   CloseImageAction() {
249   }
250   CloseImageAction(action_id_t id, thread_id_t thread_id,
251                    const Dependencies &dependencies, imagectx_id_t imagectx_id)
252     : ImageActionBase(id, thread_id, dependencies, imagectx_id) {
253   }
254 };
255
256 struct AioOpenImageAction : public ImageActionBase {
257   static const ActionType ACTION_TYPE = ACTION_TYPE_AIO_OPEN_IMAGE;
258
259   std::string name;
260   std::string snap_name;
261   bool read_only;
262
263   AioOpenImageAction() : read_only(false) {
264   }
265   AioOpenImageAction(action_id_t id, thread_id_t thread_id,
266                      const Dependencies &dependencies, imagectx_id_t imagectx_id,
267                      const std::string &name, const std::string &snap_name,
268                      bool read_only)
269     : ImageActionBase(id, thread_id, dependencies, imagectx_id),
270       name(name), snap_name(snap_name), read_only(read_only) {
271   }
272
273   void encode(bufferlist &bl) const;
274   void decode(__u8 version, bufferlist::iterator &it);
275   void dump(Formatter *f) const;
276 };
277
278 struct AioCloseImageAction : public ImageActionBase {
279   static const ActionType ACTION_TYPE = ACTION_TYPE_AIO_CLOSE_IMAGE;
280
281   AioCloseImageAction() {
282   }
283   AioCloseImageAction(action_id_t id, thread_id_t thread_id,
284                       const Dependencies &dependencies, imagectx_id_t imagectx_id)
285     : ImageActionBase(id, thread_id, dependencies, imagectx_id) {
286   }
287 };
288
289 struct UnknownAction {
290   static const ActionType ACTION_TYPE = static_cast<ActionType>(-1);
291
292   void encode(bufferlist &bl) const;
293   void decode(__u8 version, bufferlist::iterator &it);
294   void dump(Formatter *f) const;
295 };
296
297 typedef boost::variant<StartThreadAction,
298                        StopThreadAction,
299                        ReadAction,
300                        WriteAction,
301                        DiscardAction,
302                        AioReadAction,
303                        AioWriteAction,
304                        AioDiscardAction,
305                        OpenImageAction,
306                        CloseImageAction,
307                        AioOpenImageAction,
308                        AioCloseImageAction,
309                        UnknownAction> Action;
310
311 class ActionEntry {
312 public:
313   Action action;
314
315   ActionEntry() : action(UnknownAction()) {
316   }
317   ActionEntry(const Action &action) : action(action) {
318   }
319
320   void encode(bufferlist &bl) const;
321   void decode(bufferlist::iterator &it);
322   void decode_unversioned(bufferlist::iterator &it);
323   void dump(Formatter *f) const;
324
325   static void generate_test_instances(std::list<ActionEntry *> &o);
326
327 private:
328   void decode(__u8 version, bufferlist::iterator &it);
329 };
330
331 WRITE_CLASS_ENCODER(ActionEntry);
332
333 } // namespace action
334 } // namespace rbd_replay
335
336 std::ostream &operator<<(std::ostream &out,
337                          const rbd_replay::action::ActionType &type);
338
339 using rbd_replay::action::decode;
340 using rbd_replay::action::encode;
341
342 #endif // CEPH_RBD_REPLAY_ACTION_TYPES_H