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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* 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.
*
*/
#include <ctime>
#include "common/ceph_time.h"
#include "include/rados.h"
#include "gtest/gtest.h"
using ceph::real_clock;
using ceph::real_time;
using ceph::real_clock;
using ceph::real_time;
using ceph::coarse_real_clock;
using ceph::coarse_mono_clock;
using ceph::timespan;
using ceph::signedspan;
using std::chrono::seconds;
using std::chrono::microseconds;
using std::chrono::nanoseconds;
static_assert(!real_clock::is_steady, "ceph::real_clock must not be steady.");
static_assert(!coarse_real_clock::is_steady,
"ceph::coarse_real_clock must not be steady.");
static_assert(mono_clock::is_steady, "ceph::mono_clock must be steady.");
static_assert(coarse_mono_clock::is_steady,
"ceph::coarse_mono_clock must be steady.");
// Before this file was written.
static constexpr uint32_t bs = 1440701569;
static constexpr uint32_t bns = 123456789;
static constexpr uint32_t bus = 123456;
static constexpr time_t btt = bs;
static constexpr struct timespec bts = { bs, bns };
static struct ceph_timespec bcts = { init_le32(bs), init_le32(bns) };
static constexpr struct timeval btv = { bs, bus };
static constexpr double bd = bs + ((double)bns / 1000000000.);
template<typename Clock>
static void system_clock_sanity() {
static const typename Clock::time_point brt(seconds(bs) + nanoseconds(bns));
const typename Clock::time_point now(Clock::now());
ASSERT_GT(now, brt);
ASSERT_GT(Clock::to_time_t(now), btt);
ASSERT_GT(Clock::to_timespec(now).tv_sec, bts.tv_sec);
ASSERT_LT(Clock::to_timespec(now).tv_nsec, 1000000000L);
ASSERT_GT(Clock::to_ceph_timespec(now).tv_sec, bcts.tv_sec);
ASSERT_LT(Clock::to_ceph_timespec(now).tv_nsec, 1000000000UL);
ASSERT_GT(Clock::to_timeval(now).tv_sec, btv.tv_sec);
ASSERT_LT(Clock::to_timeval(now).tv_usec, 1000000L);
}
template<typename Clock>
static void system_clock_conversions() {
static typename Clock::time_point brt(seconds(bs) +
nanoseconds(bns));
ASSERT_EQ(Clock::to_time_t(brt), btt);
ASSERT_EQ(Clock::from_time_t(btt) + nanoseconds(bns), brt);
{
const struct timespec tts = Clock::to_timespec(brt);
ASSERT_EQ(tts.tv_sec, bts.tv_sec);
ASSERT_EQ(tts.tv_nsec, bts.tv_nsec);
}
ASSERT_EQ(Clock::from_timespec(bts), brt);
{
struct timespec tts;
Clock::to_timespec(brt, tts);
ASSERT_EQ(tts.tv_sec, bts.tv_sec);
ASSERT_EQ(tts.tv_nsec, bts.tv_nsec);
}
{
const struct ceph_timespec tcts = Clock::to_ceph_timespec(brt);
ASSERT_EQ(tcts.tv_sec, bcts.tv_sec);
ASSERT_EQ(tcts.tv_nsec, bcts.tv_nsec);
}
ASSERT_EQ(Clock::from_ceph_timespec(bcts), brt);
{
struct ceph_timespec tcts;
Clock::to_ceph_timespec(brt, tcts);
ASSERT_EQ(tcts.tv_sec, bcts.tv_sec);
ASSERT_EQ(tcts.tv_nsec, bcts.tv_nsec);
}
{
const struct timeval ttv = Clock::to_timeval(brt);
ASSERT_EQ(ttv.tv_sec, btv.tv_sec);
ASSERT_EQ(ttv.tv_usec, btv.tv_usec);
}
ASSERT_EQ(Clock::from_timeval(btv), brt - nanoseconds(bns - bus * 1000));
{
struct timeval ttv;
Clock::to_timeval(brt, ttv);
ASSERT_EQ(ttv.tv_sec, btv.tv_sec);
ASSERT_EQ(ttv.tv_usec, btv.tv_usec);
}
ASSERT_EQ(Clock::to_double(brt), bd);
// Fudge factor
ASSERT_LT(std::abs((Clock::from_double(bd) - brt).count()), 30);
}
TEST(RealClock, Sanity) {
system_clock_sanity<real_clock>();
}
TEST(RealClock, Conversions) {
system_clock_conversions<real_clock>();
}
TEST(CoarseRealClock, Sanity) {
system_clock_sanity<coarse_real_clock>();
}
TEST(CoarseRealClock, Conversions) {
system_clock_conversions<coarse_real_clock>();
}
TEST(TimePoints, SignedSubtraciton) {
ceph::real_time rta(std::chrono::seconds(3));
ceph::real_time rtb(std::chrono::seconds(5));
ceph::coarse_real_time crta(std::chrono::seconds(3));
ceph::coarse_real_time crtb(std::chrono::seconds(5));
ceph::mono_time mta(std::chrono::seconds(3));
ceph::mono_time mtb(std::chrono::seconds(5));
ceph::coarse_mono_time cmta(std::chrono::seconds(3));
ceph::coarse_mono_time cmtb(std::chrono::seconds(5));
ASSERT_LT(rta - rtb, ceph::signedspan::zero());
ASSERT_LT((rta - rtb).count(), 0);
ASSERT_GT(rtb - rta, ceph::signedspan::zero());
ASSERT_GT((rtb - rta).count(), 0);
ASSERT_LT(crta - crtb, ceph::signedspan::zero());
ASSERT_LT((crta - crtb).count(), 0);
ASSERT_GT(crtb - crta, ceph::signedspan::zero());
ASSERT_GT((crtb - crta).count(), 0);
ASSERT_LT(mta - mtb, ceph::signedspan::zero());
ASSERT_LT((mta - mtb).count(), 0);
ASSERT_GT(mtb - mta, ceph::signedspan::zero());
ASSERT_GT((mtb - mta).count(), 0);
ASSERT_LT(cmta - cmtb, ceph::signedspan::zero());
ASSERT_LT((cmta - cmtb).count(), 0);
ASSERT_GT(cmtb - cmta, ceph::signedspan::zero());
ASSERT_GT((cmtb - cmta).count(), 0);
}
|