Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / dmclock / src / dmclock_util.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) 2017 Red Hat Inc.
6  */
7
8
9 #pragma once
10
11
12 #include <unistd.h>
13 #include <assert.h>
14 #include <sys/time.h>
15
16 #include <limits>
17 #include <cmath>
18 #include <chrono>
19
20
21 namespace crimson {
22   namespace dmclock {
23     // we're using double to represent time, but we could change it by
24     // changing the following declarations (and by making sure a min
25     // function existed)
26     using Time = double;
27     static const Time TimeZero = 0.0;
28     static const Time TimeMax = std::numeric_limits<Time>::max();
29     static const double NaN = nan("");
30
31
32     inline Time get_time() {
33 #if defined(__linux__)
34       struct timespec now;
35       auto result = clock_gettime(CLOCK_REALTIME, &now);
36       (void) result; // reference result in case assert is compiled out
37       assert(0 == result);
38       return now.tv_sec + (now.tv_nsec / 1.0e9);
39 #else
40       struct timeval now;
41       auto result = gettimeofday(&now, NULL);
42       (void) result; // reference result in case assert is compiled out
43       assert(0 == result);
44       return now.tv_sec + (now.tv_usec / 1.0e6);
45 #endif
46     }
47
48     std::string format_time(const Time& time, uint modulo = 1000);
49
50     void debugger();
51
52   } // namespace dmclock
53 } // namespace crimson