]> git.proxmox.com Git - ceph.git/blob - ceph/src/dmclock/sim/src/sim_recs.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / 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 * Author: J. Eric Ivancich <ivancich@redhat.com>
8 *
9 * This is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License version
11 * 2.1, as published by the Free Software Foundation. See file
12 * COPYING.
13 */
14
15
16 #pragma once
17
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <assert.h>
22 #include <signal.h>
23
24 #include <sys/time.h>
25
26 #include <cmath>
27 #include <limits>
28 #include <string>
29 #include <mutex>
30 #include <iostream>
31 #include <functional>
32
33
34 using ClientId = uint;
35 using ServerId = uint;
36
37
38 namespace crimson {
39 namespace qos_simulation {
40
41 using Cost = uint32_t;
42
43 inline void debugger() {
44 raise(SIGCONT);
45 }
46
47 template<typename T>
48 void time_stats(std::mutex& mtx,
49 T& time_accumulate,
50 std::function<void()> code) {
51 auto t1 = std::chrono::steady_clock::now();
52 code();
53 auto t2 = std::chrono::steady_clock::now();
54 auto duration = t2 - t1;
55 auto cast_duration = std::chrono::duration_cast<T>(duration);
56 std::lock_guard<std::mutex> lock(mtx);
57 time_accumulate += cast_duration;
58 }
59
60 // unfortunately it's hard for the compiler to infer the types,
61 // and therefore when called the template params might have to be
62 // explicit
63 template<typename T, typename R>
64 R time_stats_w_return(std::mutex& mtx,
65 T& time_accumulate,
66 std::function<R()> code) {
67 auto t1 = std::chrono::steady_clock::now();
68 R result = code();
69 auto t2 = std::chrono::steady_clock::now();
70 auto duration = t2 - t1;
71 auto cast_duration = std::chrono::duration_cast<T>(duration);
72 std::lock_guard<std::mutex> lock(mtx);
73 time_accumulate += cast_duration;
74 return result;
75 }
76
77 template<typename T>
78 void count_stats(std::mutex& mtx,
79 T& counter) {
80 std::lock_guard<std::mutex> lock(mtx);
81 ++counter;
82 }
83
84 struct TestRequest {
85 ServerId server; // allows debugging
86 uint32_t epoch;
87 uint32_t op;
88
89 TestRequest(ServerId _server,
90 uint32_t _epoch,
91 uint32_t _op) :
92 server(_server),
93 epoch(_epoch),
94 op(_op)
95 {
96 // empty
97 }
98
99 TestRequest(const TestRequest& r) :
100 TestRequest(r.server, r.epoch, r.op)
101 {
102 // empty
103 }
104 }; // struct TestRequest
105
106
107 struct TestResponse {
108 uint32_t epoch;
109
110 explicit TestResponse(uint32_t _epoch) :
111 epoch(_epoch)
112 {
113 // empty
114 }
115
116 TestResponse(const TestResponse& r) :
117 epoch(r.epoch)
118 {
119 // empty
120 }
121
122 friend std::ostream& operator<<(std::ostream& out, const TestResponse& resp) {
123 out << "{ ";
124 out << "epoch:" << resp.epoch;
125 out << " }";
126 return out;
127 }
128 }; // class TestResponse
129
130 }; // namespace qos_simulation
131 }; // namespace crimson