summaryrefslogtreecommitdiffstats
path: root/src/dmclock/sim/src/sim_recs.h
blob: 01063007261359dd9f6d96b7ca4ee1f97c6700bf (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

/*
 * Copyright (C) 2016 Red Hat Inc.
 *
 * Author: J. Eric Ivancich <ivancich@redhat.com>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 2.1, as published by the Free Software Foundation.  See file
 * COPYING.
 */


#pragma once


#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>

#include <sys/time.h>

#include <cmath>
#include <limits>
#include <string>
#include <mutex>
#include <iostream>
#include <functional>


using ClientId = unsigned;
using ServerId = unsigned;


namespace crimson {
  namespace qos_simulation {

    using Cost = uint32_t;

    inline void debugger() {
      raise(SIGCONT);
    }

    template<typename T>
    void time_stats(std::mutex& mtx,
		    T& time_accumulate,
		    std::function<void()> code) {
      auto t1 = std::chrono::steady_clock::now();
      code();
      auto t2 = std::chrono::steady_clock::now();
      auto duration = t2 - t1;
      auto cast_duration = std::chrono::duration_cast<T>(duration);
      std::lock_guard<std::mutex> lock(mtx);
      time_accumulate += cast_duration;
    }

    // unfortunately it's hard for the compiler to infer the types,
    // and therefore when called the template params might have to be
    // explicit
    template<typename T, typename R>
    R time_stats_w_return(std::mutex& mtx,
			  T& time_accumulate,
			  std::function<R()> code) {
      auto t1 = std::chrono::steady_clock::now();
      R result = code();
      auto t2 = std::chrono::steady_clock::now();
      auto duration = t2 - t1;
      auto cast_duration = std::chrono::duration_cast<T>(duration);
      std::lock_guard<std::mutex> lock(mtx);
      time_accumulate += cast_duration;
      return result;
    }

    template<typename T>
    void count_stats(std::mutex& mtx,
		     T& counter) {
      std::lock_guard<std::mutex> lock(mtx);
      ++counter;
    }

    struct TestRequest {
      ServerId server; // allows debugging
      uint32_t epoch;
      uint32_t op;

      TestRequest(ServerId _server,
		  uint32_t _epoch,
		  uint32_t _op) :
	server(_server),
	epoch(_epoch),
	op(_op)
      {
	// empty
      }

      TestRequest(const TestRequest& r) :
	TestRequest(r.server, r.epoch, r.op)
      {
	// empty
      }
    }; // struct TestRequest


    struct TestResponse {
      uint32_t epoch;

      explicit TestResponse(uint32_t _epoch) :
	epoch(_epoch)
      {
	// empty
      }

      TestResponse(const TestResponse& r) :
	epoch(r.epoch)
      {
	// empty
      }

      friend std::ostream& operator<<(std::ostream& out, const TestResponse& resp) {
	out << "{ ";
	out << "epoch:" << resp.epoch;
	out << " }";
	return out;
      }
    }; // class TestResponse

  }; // namespace qos_simulation
}; // namespace crimson