Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / dmclock / sim / src / sim_recs.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 /*
5  * Copyright (C) 2016 Red Hat Inc.
6  */
7
8
9 #pragma once
10
11
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <signal.h>
16
17 #include <sys/time.h>
18
19 #include <cmath>
20 #include <limits>
21 #include <string>
22 #include <mutex>
23 #include <iostream>
24 #include <functional>
25
26
27 using ClientId = uint;
28 using ServerId = uint;
29
30
31 namespace crimson {
32   namespace qos_simulation {
33
34     inline void debugger() {
35       raise(SIGCONT);
36     }
37
38     template<typename T>
39     void time_stats(std::mutex& mtx,
40                     T& time_accumulate,
41                     std::function<void()> code) {
42       auto t1 = std::chrono::steady_clock::now();
43       code();
44       auto t2 = std::chrono::steady_clock::now();
45       auto duration = t2 - t1;
46       auto cast_duration = std::chrono::duration_cast<T>(duration);
47       std::lock_guard<std::mutex> lock(mtx);
48       time_accumulate += cast_duration;
49     }
50
51     // unfortunately it's hard for the compiler to infer the types,
52     // and therefore when called the template params might have to be
53     // explicit
54     template<typename T, typename R>
55     R time_stats_w_return(std::mutex& mtx,
56                           T& time_accumulate,
57                           std::function<R()> code) {
58       auto t1 = std::chrono::steady_clock::now();
59       R result = code();
60       auto t2 = std::chrono::steady_clock::now();
61       auto duration = t2 - t1;
62       auto cast_duration = std::chrono::duration_cast<T>(duration);
63       std::lock_guard<std::mutex> lock(mtx);
64       time_accumulate += cast_duration;
65       return result;
66     }
67
68     template<typename T>
69     void count_stats(std::mutex& mtx,
70                      T& counter) {
71       std::lock_guard<std::mutex> lock(mtx);
72       ++counter;
73     }
74
75     struct TestRequest {
76       ServerId server; // allows debugging
77       uint32_t epoch;
78       uint32_t op;
79
80       TestRequest(ServerId _server,
81                   uint32_t _epoch,
82                   uint32_t _op) :
83         server(_server),
84         epoch(_epoch),
85         op(_op)
86       {
87         // empty
88       }
89
90       TestRequest(const TestRequest& r) :
91         TestRequest(r.server, r.epoch, r.op)
92       {
93         // empty
94       }
95     }; // struct TestRequest
96
97
98     struct TestResponse {
99       uint32_t epoch;
100
101       TestResponse(uint32_t _epoch) :
102         epoch(_epoch)
103       {
104         // empty
105       }
106
107       TestResponse(const TestResponse& r) :
108         epoch(r.epoch)
109       {
110         // empty
111       }
112
113       friend std::ostream& operator<<(std::ostream& out, const TestResponse& resp) {
114         out << "{ ";
115         out << "epoch:" << resp.epoch;
116         out << " }";
117         return out;
118       }
119     }; // class TestResponse
120
121   }; // namespace qos_simulation
122 }; // namespace crimson